More jshinting and whitespace fixes

This commit is contained in:
mourner 2011-12-09 12:20:59 +02:00
parent cf5cc5bb28
commit dc79b10683
7 changed files with 88 additions and 79 deletions

View File

@ -14,7 +14,7 @@
"bitwise": true,
"boss": false,
"curly": true,
"eqeqeq": true,
"eqeqeq": false, // todo
"eqnull": false,
"evil": false,
"expr": false,
@ -37,7 +37,7 @@
"onevar": false,
"plusplus": false,
"sub": false,
"trailing": false,
"white": false,
"trailing": false, // todo
"white": false, // todo
"indent": 4
}

View File

@ -5,28 +5,29 @@
mobile = typeof orientation != 'undefined' ? true : false,
android = ua.indexOf("android") != -1,
opera = window.opera;
L.Browser = {
ie: ie,
ie6: ie && !window.XMLHttpRequest,
webkit: webkit,
webkit3d: webkit && ('WebKitCSSMatrix' in window) && ('m11' in new WebKitCSSMatrix()),
webkit3d: webkit && ('WebKitCSSMatrix' in window) && ('m11' in new window.WebKitCSSMatrix()),
gecko: ua.indexOf("gecko") != -1,
opera: opera,
android: android,
mobileWebkit: mobile && webkit,
mobileOpera: mobile && opera,
mobile: mobile,
touch: (function() {
var touchSupported = false;
var touchSupported = false,
startName = 'ontouchstart';
// WebKit, etc
if ('ontouchstart' in document.documentElement) {
if (startName in document.documentElement) {
return true;
}
@ -38,12 +39,12 @@
return false;
}
e.setAttribute('ontouchstart', 'return;');
if (typeof e['ontouchstart'] == 'function') {
e.setAttribute(startName, 'return;');
if (typeof e[startName] === 'function') {
touchSupported = true;
}
e.removeAttribute('ontouchstart');
e.removeAttribute(startName);
e = null;
return touchSupported;

View File

@ -2,10 +2,10 @@
* Class powers the OOP facilities of the library. Thanks to John Resig and Dean Edwards for inspiration!
*/
L.Class = function() {};
L.Class = function() {};
L.Class.extend = function(/*Object*/ props) /*-> Class*/ {
// extended class with the new prototype
var NewClass = function() {
if (this.initialize) {
@ -17,16 +17,16 @@ L.Class.extend = function(/*Object*/ props) /*-> Class*/ {
var F = function() {};
F.prototype = this.prototype;
var proto = new F();
proto.constructor = NewClass;
NewClass.prototype = proto;
// add superclass access
NewClass.superclass = this.prototype;
// add class name
//proto.className = props;
//inherit parent's statics
for (var i in this) {
if (this.hasOwnProperty(i) && i != 'prototype' && i != 'superclass') {
@ -39,13 +39,13 @@ L.Class.extend = function(/*Object*/ props) /*-> Class*/ {
L.Util.extend(NewClass, props.statics);
delete props.statics;
}
// mix includes into the prototype
if (props.includes) {
L.Util.extend.apply(null, [proto].concat(props.includes));
delete props.includes;
}
// merge options
if (props.options && proto.options) {
props.options = L.Util.extend({}, proto.options, props.options);
@ -53,14 +53,14 @@ L.Class.extend = function(/*Object*/ props) /*-> Class*/ {
// mix given properties into the prototype
L.Util.extend(proto, props);
// allow inheriting further
NewClass.extend = arguments.callee;
NewClass.extend = L.Class.extend;
// method for adding properties to prototype
NewClass.include = function(props) {
L.Util.extend(this.prototype, props);
};
return NewClass;
};
};

View File

@ -29,19 +29,19 @@ L.Util = {
return obj[key];
};
})(),
requestAnimFrame: (function() {
function timeoutDefer(callback) {
window.setTimeout(callback, 1000 / 60);
}
var requestFn = window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
var requestFn = window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
timeoutDefer;
return function(callback, context, immediate, contextEl) {
callback = context ? L.Util.bind(callback, context) : callback;
if (immediate && requestFn === timeoutDefer) {
@ -52,7 +52,7 @@ L.Util = {
};
})(),
limitExecByInterval: function(fn, time, context) {
limitExecByInterval: function(fn, time, context) {
var lock, execOnUnlock, args;
function exec(){
lock = false;
@ -63,7 +63,7 @@ L.Util = {
}
return function() {
args = arguments;
if (!lock) {
if (!lock) {
lock = true;
setTimeout(exec, time);
fn.apply(context, args);
@ -72,18 +72,18 @@ L.Util = {
}
};
},
falseFn: function() { return false; },
formatNum: function(num, digits) {
var pow = Math.pow(10, digits || 5);
return Math.round(num * pow) / pow;
},
setOptions: function(obj, options) {
obj.options = L.Util.extend({}, obj.options, options);
},
getParamString: function(obj) {
var params = [];
for (var i in obj) {
@ -95,7 +95,7 @@ L.Util = {
},
template: function (str, data) {
return str.replace(/\{ *([^} ]+) *\}/g, function (str, key) {
return str.replace(/\{ *([\w_]+) *\}/g, function (str, key) {
var value = data[key];
if (!data.hasOwnProperty(key)) {
throw new Error('No value provided for variable ' + str);
@ -103,4 +103,4 @@ L.Util = {
return value;
});
}
};
};

View File

@ -40,7 +40,7 @@ L.DomEvent = {
removeListener: function(/*HTMLElement*/ obj, /*String*/ type, /*Function*/ fn) {
var id = L.Util.stamp(fn),
key = '_leaflet_' + type + id;
key = '_leaflet_' + type + id,
handler = obj[key];
if (!handler) { return; }
@ -76,6 +76,7 @@ L.DomEvent = {
return (related != el);
},
/*jshint noarg:false */ // evil magic for IE
_getEvent: function()/*->Event*/ {
var e = window.event;
if (!e) {
@ -88,6 +89,7 @@ L.DomEvent = {
}
return e;
},
/*jshint noarg:false */
stopPropagation: function(/*Event*/ e) {
if (e.stopPropagation) {

View File

@ -1,5 +1,5 @@
/*
* L.LineUtil contains different utility functions for line segments
* L.LineUtil contains different utility functions for line segments
* and polylines (clipping, simplification, distances, etc.)
*/
@ -10,27 +10,27 @@ L.LineUtil = {
*/
simplify: function(/*Point[]*/ points, /*Number*/ tolerance) {
if (!tolerance || !points.length) return points.slice();
// stage 1: vertex reduction
points = this.reducePoints(points, tolerance);
// stage 2: Douglas-Peucker simplification
points = this.simplifyDP(points, tolerance);
return points;
return points;
},
// distance from a point to a segment between two points
pointToSegmentDistance: function(/*Point*/ p, /*Point*/ p1, /*Point*/ p2) {
return Math.sqrt(this._sqPointToSegmentDist(p, p1, p2));
},
closestPointOnSegment: function(/*Point*/ p, /*Point*/ p1, /*Point*/ p2) {
var point = this._sqClosestPointOnSegment(p, p1, p2);
point.distance = Math.sqrt(point._sqDist);
return point;
},
// Douglas-Peucker simplification, see http://en.wikipedia.org/wiki/Douglas-Peucker_algorithm
simplifyDP: function(points, tol) {
var maxDist2 = 0,
@ -56,21 +56,21 @@ L.LineUtil = {
if (maxDist2 >= t2) {
part1 = points.slice(0, index),
part2 = points.slice(index);
part1 = this.simplifyDP(part1, tol),
part2 = this.simplifyDP(part2, tol);
return part1.concat(part2);
} else {
return [points[0], points[len - 1]];
}
},
// reduce points that are too close to each other to a single point
reducePoints: function(points, tol) {
var reducedPoints = [points[0]],
t2 = tol * tol;
for (var i = 1, prev = 0, len = points.length; i < len; i++) {
if (this._sqDist(points[i], points[prev]) < t2) continue;
reducedPoints.push(points[i]);
@ -81,7 +81,9 @@ L.LineUtil = {
}
return reducedPoints;
},
/*jshint bitwise:false */ // temporarily allow bitwise oprations
/*
* Cohen-Sutherland line clipping algorithm.
* Used to avoid rendering parts of a polyline that are not currently visible.
@ -89,13 +91,13 @@ L.LineUtil = {
clipSegment: function(a, b, bounds, useLastCode) {
var min = bounds.min,
max = bounds.max;
var codeA = useLastCode ? this._lastCode : this._getBitCode(a, bounds),
codeB = this._getBitCode(b, bounds);
// save 2nd code to avoid calculating it on the next segment
this._lastCode = codeB;
while (true) {
// if a,b is inside the clip window (trivial accept)
if (!(codeA | codeB)) {
@ -108,7 +110,7 @@ L.LineUtil = {
var codeOut = codeA || codeB,
p = this._getEdgeIntersection(a, b, codeOut, bounds),
newCode = this._getBitCode(p, bounds);
if (codeOut == codeA) {
a = p;
codeA = newCode;
@ -119,13 +121,13 @@ L.LineUtil = {
}
}
},
_getEdgeIntersection: function(a, b, code, bounds) {
var dx = b.x - a.x,
dy = b.y - a.y,
min = bounds.min,
max = bounds.max;
if (code & 8) { // top
return new L.Point(a.x + dx * (max.y - a.y) / dy, max.y);
} else if (code & 4) { // bottom
@ -136,28 +138,30 @@ L.LineUtil = {
return new L.Point(min.x, a.y + dy * (min.x - a.x) / dx);
}
},
_getBitCode: function(/*Point*/ p, bounds) {
var code = 0;
if (p.x < bounds.min.x) code |= 1; // left
else if (p.x > bounds.max.x) code |= 2; // right
if (p.y < bounds.min.y) code |= 4; // bottom
else if (p.y > bounds.max.y) code |= 8; // top
return code;
},
/*jshint bitwise:true */
// square distance (to avoid unnecessary Math.sqrt calls)
_sqDist: function(p1, p2) {
var dx = p2.x - p1.x,
dy = p2.y - p1.y;
return dx * dx + dy * dy;
},
/**
* @return L.Point point on segment with attribute _sqDist - square distance to segment
*/
*/
_sqClosestPointOnSegment: function(p, p1, p2) {
var x2 = p2.x - p1.x,
y2 = p2.y - p1.y,
@ -165,7 +169,7 @@ L.LineUtil = {
if (x2 || y2) {
var dot = (p.x - p1.x) * x2 + (p.y - p1.y) * y2,
t = dot / this._sqDist(p1, p2);
if (t > 1) {
apoint = p2;
} else if (t > 0) {
@ -175,9 +179,9 @@ L.LineUtil = {
apoint._sqDist = this._sqDist(p, apoint);
return apoint;
},
// distance from a point to a segment between two points
_sqPointToSegmentDist: function(p, p1, p2) {
return this._sqClosestPointOnSegment(p, p1, p2)._sqDist;
}
};
};

View File

@ -1,7 +1,9 @@
/*
* L.PolyUtil contains utilify functions for polygons (clipping, etc.).
* L.PolyUtil contains utilify functions for polygons (clipping, etc.).
*/
/*jshint bitwise:false */ // allow bitwise oprations here
L.PolyUtil = {};
/*
@ -17,20 +19,20 @@ L.PolyUtil.clipPolygon = function(points, bounds) {
a, b,
len, edge, p,
lu = L.LineUtil;
for (i = 0, len = points.length; i < len; i++) {
points[i]._code = lu._getBitCode(points[i], bounds);
}
// for each edge (left, bottom, right, top)
for (k = 0; k < 4; k++) {
edge = edges[k];
clippedPoints = [];
for (i = 0, len = points.length, j = len - 1; i < len; j = i++) {
a = points[i];
b = points[j];
// if a is inside the clip window
if (!(a._code & edge)) {
// if b is outside the clip window (a->b goes out of screen)
@ -40,7 +42,7 @@ L.PolyUtil.clipPolygon = function(points, bounds) {
clippedPoints.push(p);
}
clippedPoints.push(a);
// else if b is inside the clip window (a->b enters the screen)
} else if (!(b._code & edge)) {
p = lu._getEdgeIntersection(b, a, edge, bounds);
@ -50,6 +52,6 @@ L.PolyUtil.clipPolygon = function(points, bounds) {
}
points = clippedPoints;
}
return points;
};
};