From 168ed34e2fce8cd4307673d488e89bd75bd9c5f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20Manchado=20Vel=C3=A1zquez?= Date: Mon, 2 Mar 2015 22:31:16 +0100 Subject: [PATCH] jshint <- happy --- dice/dice.js | 152 ++++++++++++++++++++++++++++++++------------------- dice/main.js | 29 +++++++--- teal.js | 119 +++++++++++++++++++++++++--------------- 3 files changed, 190 insertions(+), 110 deletions(-) diff --git a/dice/dice.js b/dice/dice.js index 3857156..594da89 100644 --- a/dice/dice.js +++ b/dice/dice.js @@ -1,6 +1,8 @@ +/*global CANNON, THREE, $t, requestAnimationFrame, teal */ + "use strict"; -(function(dice) { +(function() { var randomStorage = [], useRandomStorage = true; @@ -29,7 +31,6 @@ cv.push(new CANNON.Vec3(v[0] * l, v[1] * l, v[2] * l)); } for (var i = 0; i < faces.length; ++i) { - var f = faces[i]; cf.push(faces[i].slice(0, faces[i].length - 1)); } return new CANNON.ConvexPolyhedron(cv, cf); @@ -194,7 +195,9 @@ } function createD10Geometry(radius) { - var a = Math.PI * 2 / 10, k = Math.cos(a), h = 0.105, v = -1; + var a = Math.PI * 2 / 10, + h = 0.105, + v = -1; var vertices = []; for (var i = 0, b = 0; i < 10; ++i, b += a) { vertices.push([Math.cos(b), Math.sin(b), h * (i % 2 ? 1 : -1)]); @@ -232,7 +235,6 @@ } var scale = 50; - var chamfer = 0.6; var materialOptions = { specular: '#171d1f', color: '#ffffff', @@ -242,7 +244,6 @@ }; var defaultLabelColor = '#aaaaaa'; var defaultDieColor = '#202020'; - var d4MaterialCache, d100MaterialCache; var knownDieTypes = ['d4', 'd6', 'd8', 'd10', 'd12', 'd20', 'd100']; var dieInfo = { @@ -290,7 +291,7 @@ this.parseNotation = function(notation) { var dr = /\s*(\d*)([a-z]+)(\d+)(\s*\+\s*(\d+)){0,1}\s*(\+|$)/gi; var ret = { set: [], constant: 0 }, res; - while (res = dr.exec(notation)) { + while ((res = dr.exec(notation))) { var command = res[2]; if (command !== 'd') { continue; @@ -311,7 +312,7 @@ } } return ret; - } + }; this.stringifyNotation = function(nn) { var dict = {}, notation = ''; @@ -328,9 +329,11 @@ } notation += dict[i] + i; } - if (nn.constant) notation += ' + ' + nn.constant; + if (nn.constant) { + notation += ' + ' + nn.constant; + } return notation; - } + }; this.dieBox = function(container, dimensions) { this.cw = container.clientWidth / 2; @@ -347,9 +350,9 @@ scale = Math.sqrt(this.w * this.w + this.h * this.h) / 13; this.useAdaptiveTimestep = true; - this.renderer = window.WebGLRenderingContext - ? new THREE.WebGLRenderer({ antialias: true }) - : new THREE.CanvasRenderer({ antialias: true }); + this.renderer = window.WebGLRenderingContext ? + new THREE.WebGLRenderer({ antialias: true }) : + new THREE.CanvasRenderer({ antialias: true }); this.renderer.setSize(this.cw * 2, this.ch * 2); this.renderer.shadowMapEnabled = true; this.renderer.shadowMapSoft = true; @@ -426,7 +429,7 @@ this.running = false; this.renderer.render(this.scene, this.camera); - } + }; this.dieBox.prototype.createDie = function(type, pos, velocity, angle, axis, labelColor, color) { var dice = createDie(type, labelColor, color); @@ -443,7 +446,7 @@ this.scene.add(dice); this.dices.push(dice); this.world.add(dice.body); - } + }; this.dieBox.prototype.check = function() { var res = true; @@ -452,7 +455,9 @@ if (time - this.running < 10000) { for (var i = 0; i < this.dices.length; ++i) { var dice = this.dices[i]; - if (dice.diceStopped == true) continue; + if (dice.diceStopped === true) { + continue; + } var a = dice.body.angularVelocity, v = dice.body.velocity; if (Math.abs(a.x) < e && Math.abs(a.y) < e && Math.abs(a.z) < e && Math.abs(v.x) < e && Math.abs(v.y) < e && Math.abs(v.z) < e) { @@ -461,11 +466,11 @@ dice.diceStopped = true; continue; } + } else { + dice.diceStopped = (new Date()).getTime(); } - else dice.diceStopped = (new Date()).getTime(); res = false; - } - else { + } else { dice.diceStopped = undefined; res = false; @@ -476,23 +481,30 @@ this.running = false; var values = []; for (var i in this.dices) { - var dice = this.dices[i], invert = dice.dieType == 'd4' ? -1 : 1; + var dice = this.dices[i], + invert = dice.dieType === 'd4' ? -1 : 1; var intersects = (new THREE.Raycaster( new THREE.Vector3(dice.position.x, dice.position.y, 200 * invert), new THREE.Vector3(0, 0, -1 * invert))).intersectObjects([dice]); var matindex = intersects[0].face.materialIndex - 1; - if (dice.dieType == 'd100') matindex *= 10; + if (dice.dieType === 'd100') { + matindex *= 10; + } values.push(matindex); } - if (this.callback) this.callback.call(this, values); + if (this.callback) { + this.callback.call(this, values); + } } - } + }; this.dieBox.prototype.__animate = function(threadid) { var time = (new Date()).getTime(); if (this.useAdaptiveTimestep) { var timeDiff = (time - this.lastTime) / 1000; - if (timeDiff > 3) timeDiff = 1 / 60; + if (timeDiff > 3) { + timeDiff = 1 / 60; + } while (timeDiff > 1.1 / 60) { this.world.step(1 / 60); timeDiff -= 1 / 60; @@ -504,31 +516,37 @@ } for (var i in this.scene.children) { var interact = this.scene.children[i]; - if (interact.body != undefined) { + if (interact.body !== undefined) { interact.body.position.copy(interact.position); interact.body.quaternion.copy(interact.quaternion); } } this.renderer.render(this.scene, this.camera); this.lastTime = this.lastTime ? time : (new Date()).getTime(); - if (this.running == threadid) this.check(); - if (this.running == threadid) { + if (this.running === threadid) { + this.check(); + } + if (this.running === threadid) { (function(t, tid) { requestAnimationFrame(function() { t.__animate(tid); }); })(this, threadid); } - } + }; this.dieBox.prototype.clear = function() { this.running = false; var die; - while (die = this.dices.pop()) { + while ((die = this.dices.pop())) { this.scene.remove(die); - if (die.body) this.world.remove(die.body); + if (die.body) { + this.world.remove(die.body); + } + } + if (this.pane) { + this.scene.remove(this.pane); } - if (this.pane) this.scene.remove(this.pane); this.renderer.render(this.scene, this.camera); - } + }; this.dieBox.prototype.generateVectors = function(rollSpec, coords, boost) { function makeRandomVector(coords) { @@ -537,8 +555,8 @@ x: coords.x * Math.cos(randomAngle) - coords.y * Math.sin(randomAngle), y: coords.x * Math.sin(randomAngle) + coords.y * Math.cos(randomAngle) }; - if (vec.x == 0) vec.x = 0.01; - if (vec.y == 0) vec.y = 0.01; + vec.x = vec.x || 0.01; + vec.y = vec.y || 0.01; return vec; } @@ -551,7 +569,11 @@ z: rnd() * 200 + 200 }; var projector = Math.abs(vec.x / vec.y); - if (projector > 1.0) pos.y /= projector; else pos.x *= projector; + if (projector > 1.0) { + pos.y /= projector; + } else { + pos.x *= projector; + } var velvec = makeRandomVector(coords); var velocity = { x: velvec.x * boost, y: velvec.y * boost, z: -10 }; var inertia = dieInfo[rollSpec.set[i].type].inertia; @@ -572,7 +594,7 @@ }); } return vectors; - } + }; this.dieBox.prototype.roll = function(vectors, callback) { this.clear(); @@ -589,14 +611,18 @@ this.running = (new Date()).getTime(); this.lastTime = 0; this.__animate(this.running); - } + }; this.dieBox.prototype.__selectorAnimate = function(threadid) { var time = (new Date()).getTime(); var timeDiff = (time - this.lastTime) / 1000; - if (timeDiff > 3) timeDiff = 1 / 60; + if (timeDiff > 3) { + timeDiff = 1 / 60; + } var angleChange = 0.3 * timeDiff * Math.PI * Math.min(24000 + threadid - time, 6000) / 6000; - if (angleChange < 0) this.running = false; + if (angleChange < 0) { + this.running = false; + } for (var i in this.dices) { this.dices[i].rotation.y += angleChange; this.dices[i].rotation.x += angleChange / 4; @@ -605,20 +631,22 @@ this.lastTime = time; this.renderer.render(this.scene, this.camera); - if (this.running == threadid) { + if (this.running === threadid) { (function(t, tid) { requestAnimationFrame(function() { t.__selectorAnimate(tid); }); })(this, threadid); } - } + }; this.dieBox.prototype.searchDieByMouse = function(ev) { var intersects = (new THREE.Raycaster(this.camera.position, (new THREE.Vector3((ev.clientX - this.cw) / this.aspect, (ev.clientY - this.ch) / this.aspect, this.w / 9)) .sub(this.camera.position).normalize())).intersectObjects(this.dices); - if (intersects.length) return intersects[0].object.userData; - } + if (intersects.length) { + return intersects[0].object.userData; + } + }; this.dieBox.prototype.drawSelector = function() { this.clear(); @@ -640,7 +668,7 @@ this.running = (new Date()).getTime(); this.lastTime = 0; this.__selectorAnimate(this.running); - } + }; this.dieBox.prototype.bindMouse = function(container, notationGetter, beforeRoll, afterRoll) { var box = this; @@ -649,13 +677,19 @@ box.mouseStart = { x: ev.clientX, y: ev.clientY }; }); $t.bind(container, ['mouseup', 'touchend', 'touchcancel'], function(ev) { - if (box.rolling) return; + if (box.rolling) { + return; + } var coords = {x: ev.clientX - box.mouseStart.x, y: -(ev.clientY - box.mouseStart.y)}; var dist = Math.sqrt(coords.x * coords.x + coords.y * coords.y); - if (dist < Math.sqrt(box.w * box.h * 0.01)) return; + if (dist < Math.sqrt(box.w * box.h * 0.01)) { + return; + } var notation = notationGetter.call(box); - if (notation.set.length == 0) return; + if (notation.set.length === 0) { + return; + } var dieSet = notation.set.map(function(dieType) { return { @@ -666,25 +700,29 @@ }); var dieSpec = {set: dieSet, constant: notation.constant}; - var timeInt = (new Date()).getTime() - box.mouseTime; - if (timeInt > 2000) timeInt = 2000; + var timeInt = Math.min((new Date()).getTime() - box.mouseTime, + 2000); var boost = Math.sqrt((2500 - timeInt) / 2500) * dist * 2; coords.x /= dist; coords.y /= dist; rollDice(box, dieSpec, coords, boost, beforeRoll, afterRoll); }); - } + }; this.dieBox.prototype.bindThrow = function(button, notationGetter, beforeRoll, afterRoll) { var box = this; $t.bind(button, ['mouseup', 'touchend', 'touchcancel'], function(ev) { - if (box.rolling) return; + if (box.rolling) { + return; + } ev.stopPropagation(); var coords = {x: (rnd() * 2 - 1) * box.w, y: -(rnd() * 2 - 1) * box.h}; var dist = Math.sqrt(coords.x * coords.x + coords.y * coords.y); var notation = notationGetter.call(box); - if (notation.set.length == 0) return; + if (notation.set.length === 0) { + return; + } var boost = (rnd() + 3) * dist; coords.x /= dist; coords.y /= dist; @@ -699,21 +737,23 @@ rollDice(box, dieSpec, coords, boost, beforeRoll, afterRoll); }); - } + }; function rollDice(box, notation, coords, boost, beforeRoll, afterRoll) { var vectors = box.generateVectors(notation, coords, boost); box.rolling = true; - if (beforeRoll) beforeRoll.call(box, vectors, notation); + if (beforeRoll) { + beforeRoll.call(box, vectors, notation); + } if (afterRoll) { box.clear(); box.roll(vectors, function(result) { - if (afterRoll) afterRoll.call(box, notation, result); + if (afterRoll) { + afterRoll.call(box, notation, result); + } box.rolling = false; }); } } }).apply(teal.dice = teal.dice || {}); - - diff --git a/dice/main.js b/dice/main.js index 9567cc9..2af081f 100644 --- a/dice/main.js +++ b/dice/main.js @@ -1,6 +1,12 @@ +/*global $t */ + "use strict"; function diceInitialize(container, w, h) { + function onSetChange(/*ev*/) { + set.style.width = set.value.length + 3 + 'ex'; + } + $t.remove($t.id('loading_text')); var canvas = $t.id('canvas'); @@ -12,12 +18,11 @@ function diceInitialize(container, w, h) { var infoDiv = $t.id('info_div'); onSetChange(); - function onSetChange(ev) { set.style.width = set.value.length + 3 + 'ex'; } $t.bind(set, 'keyup', onSetChange); $t.bind(set, 'mousedown', function(ev) { ev.stopPropagation(); }); $t.bind(set, 'mouseup', function(ev) { ev.stopPropagation(); }); - $t.bind(set, 'focus', function(ev) { $t.set(container, { class: '' }); }); - $t.bind(set, 'blur', function(ev) { $t.set(container, { class: 'svg' }); }); + $t.bind(set, 'focus', function() { $t.set(container, { class: '' }); }); + $t.bind(set, 'blur', function() { $t.set(container, { class: 'svg' }); }); $t.bind($t.id('clear'), ['mouseup', 'touchend', 'touchcancel'], function(ev) { ev.stopPropagation(); @@ -33,7 +38,7 @@ function diceInitialize(container, w, h) { box.drawSelector(); } - function beforeRoll(vectors) { + function beforeRoll(/*vectors*/) { infoDiv.style.display = 'none'; selectorDiv.style.display = 'none'; } @@ -44,9 +49,13 @@ function diceInitialize(container, w, h) { function afterRoll(notation, result) { var res = result.join(' '); - if (notation.constant) res += ' +' + notation.constant; - if (result.length > 1) res += ' = ' + + if (notation.constant) { + res += ' +' + notation.constant; + } + if (result.length > 1) { + res += ' = ' + (result.reduce(function(s, a) { return s + a; }) + notation.constant); + } label.innerHTML = res; infoDiv.style.display = 'inline-block'; } @@ -55,13 +64,15 @@ function diceInitialize(container, w, h) { box.bindThrow($t.id('throw'), notationGetter, beforeRoll, afterRoll); $t.bind(container, ['mouseup', 'touchend', 'touchcancel'], function(ev) { - if (selectorDiv.style.display == 'none') { - if (!box.rolling) showSelector(); + if (selectorDiv.style.display === 'none') { + if (!box.rolling) { + showSelector(); + } box.rolling = false; return; } var name = box.searchDieByMouse(ev); - if (name != undefined) { + if (name !== undefined) { var notation = $t.dice.parseNotation(set.value); notation.set.push(name); set.value = $t.dice.stringifyNotation(notation); diff --git a/teal.js b/teal.js index ca4f5b2..ce11f98 100644 --- a/teal.js +++ b/teal.js @@ -1,43 +1,57 @@ +/*global $t, teal */ + "use strict"; window.teal = {}; window.$t = window.teal; teal.copyto = function(obj, res) { - if (obj == null || typeof obj !== 'object') return obj; + if (obj === null || typeof obj !== 'object') { + return obj; + } if (obj instanceof Array) { - for (var i = obj.length - 1; i >= 0; --i) + for (var i = obj.length - 1; i >= 0; --i) { res[i] = $t.copy(obj[i]); + } } else { for (var i in obj) { - if (obj.hasOwnProperty(i)) + if (obj.hasOwnProperty(i)) { res[i] = $t.copy(obj[i]); + } } } return res; -} +}; teal.copy = function(obj) { - if (!obj) return obj; + if (!obj) { + return obj; + } return teal.copyto(obj, new obj.constructor()); -} +}; teal.element = function(name, props, place) { var dom = document.createElement(name); - for (var i in props) dom.setAttribute(i, props[i]); - if (place) place.appendChild(dom); + for (var i in props) { + dom.setAttribute(i, props[i]); + } + if (place) { + place.appendChild(dom); + } return dom; -} +}; teal.id = function(id) { return document.getElementById(id); -} +}; teal.set = function(sel, props) { - for (var i in props) sel.setAttribute(i, props[i]); + for (var i in props) { + sel.setAttribute(i, props[i]); + } return sel; -} +}; teal.clas = function(sel, oldclass, newclass) { var oc = oldclass ? oldclass.split(/\s+/) : [], @@ -45,45 +59,57 @@ teal.clas = function(sel, oldclass, newclass) { classes = (sel.getAttribute('class') || '').split(/\s+/); for (var i in oc) { var ind = classes.indexOf(oc[i]); - if (ind >= 0) classes.splice(ind, 1); + if (ind >= 0) { + classes.splice(ind, 1); + } } for (var i in nc) { - if (classes.indexOf(nc[i]) < 0) classes.push(nc[i]); + if (classes.indexOf(nc[i]) < 0) { + classes.push(nc[i]); + } } sel.setAttribute('class', classes.join(' ')); -} +}; teal.empty = function(sel) { - if (sel.childNodes) - while (sel.childNodes.length) + if (sel.childNodes) { + while (sel.childNodes.length) { sel.removeChild(sel.firstChild); -} + } + } +}; teal.remove = function(sel) { if (sel) { - if (sel.parentNode) sel.parentNode.removeChild(sel); - else for (var i = sel.length - 1; i >= 0; --i) - sel[i].parentNode.removeChild(sel[i]); + if (sel.parentNode) { + sel.parentNode.removeChild(sel); + } else { + for (var i = sel.length - 1; i >= 0; --i) { + sel[i].parentNode.removeChild(sel[i]); + } + } } -} +}; teal.bind = function(sel, eventname, func, bubble) { if (eventname.constructor === Array) { - for (var i in eventname) + for (var i in eventname) { sel.addEventListener(eventname[i], func, bubble ? bubble : false); - } - else + } + } else { sel.addEventListener(eventname, func, bubble ? bubble : false); -} + } +}; teal.unbind = function(sel, eventname, func, bubble) { if (eventname.constructor === Array) { - for (var i in eventname) + for (var i in eventname) { sel.removeEventListener(eventname[i], func, bubble ? bubble : false); - } - else + } + } else { sel.removeEventListener(eventname, func, bubble ? bubble : false); -} + } +}; teal.one = function(sel, eventname, func, bubble) { var oneFunc = function(e) { @@ -91,16 +117,16 @@ teal.one = function(sel, eventname, func, bubble) { teal.unbind(sel, eventname, oneFunc, bubble); }; teal.bind(sel, eventname, oneFunc, bubble); -} +}; teal.raiseEvent = function(sel, eventname, bubble, cancelable) { var evt = document.createEvent('UIEvents'); - evt.initEvent(eventname, bubble == undefined ? true : bubble, - cancelable == undefined ? true : cancelable); + evt.initEvent(eventname, bubble === undefined ? true : bubble, + cancelable === undefined ? true : cancelable); sel.dispatchEvent(evt); -} +}; -if (navigator.appName == 'Microsoft Internet Explorer') { +if (navigator.appName === 'Microsoft Internet Explorer') { teal.getElementsByClass = function(classes, node) { var node = node || document, list = node.getElementsByTagName('*'), @@ -110,40 +136,43 @@ if (navigator.appName == 'Microsoft Internet Explorer') { for (var i = list.length - 1; i >= 0; --i) { for (var j = cl.length - 1; j >= 0; --j) { var clas = list[i].getAttribute('class'); - if (clas && clas.search('\\b' + cl[j] + '\\b') != -1) { + if (clas && clas.search('\\b' + cl[j] + '\\b') !== -1) { result.push(list[i]); break; } } } return result; - } + }; } else { teal.getElementsByClass = function(classes, node) { return (node || document).getElementsByClassName(classes); - } + }; } teal.rpc = function(params, resp) { var ajax = new XMLHttpRequest(), ret; ajax.open("post", 'f', resp ? true : false); ajax.onreadystatechange = function() { - if (ajax.readyState == 4) { - if (resp) resp(JSON.parse(ajax.responseText)); - else ret = JSON.parse(ajax.responseText); + if (ajax.readyState === 4) { + if (resp) { + resp(JSON.parse(ajax.responseText)); + } else { + ret = JSON.parse(ajax.responseText); + } } }; ajax.send(JSON.stringify(params)); return ret; -} +}; teal.uuid = function() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { - var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); + var r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8); return v.toString(16); }); -} +}; teal.getUrlParams = function() { var params = window.location.search.substring(1).split("&"); @@ -153,4 +182,4 @@ teal.getUrlParams = function() { res[keyvalue[0]] = decodeURI(keyvalue[1]); } return res; -} +};