More JSLint

git-svn-id: http://svg-edit.googlecode.com/svn/trunk@2642 eee81c28-f429-11dd-99c0-75d572ba1ddd
master
Brett Zamir 2014-01-31 01:05:03 +00:00
parent 0df050aeb4
commit d12694f9fa
5 changed files with 111 additions and 83 deletions

View File

@ -1,3 +1,5 @@
/*globals $, svgedit, svgroot*/
/*jslint vars: true, eqeq: true, continue: true*/
/**
* Package: svgedit.path
*
@ -13,7 +15,7 @@
// 3) math.js
// 4) svgutils.js
(function() {
(function() {'use strict';
if (!svgedit.path) {
svgedit.path = {};
@ -75,12 +77,13 @@ svgedit.path.insertItemBefore = function(elem, newseg, index) {
}
var len = list.numberOfItems;
var arr = [];
for (var i=0; i<len; i++) {
var i;
for (i=0; i < len; i++) {
var cur_seg = list.getItem(i);
arr.push(cur_seg);
}
list.clear();
for (var i=0; i<len; i++) {
for (i=0; i < len; i++) {
if (i == index) { //index+1
list.appendItem(newseg);
}
@ -92,7 +95,8 @@ svgedit.path.insertItemBefore = function(elem, newseg, index) {
svgedit.path.ptObjToArr = function(type, seg_item) {
var arr = segData[type], len = arr.length;
var out = Array(len);
for (var i=0; i<len; i++) {
var i;
for (i = 0; i < len; i++) {
out[i] = seg_item[arr[i]];
}
return out;
@ -122,7 +126,7 @@ svgedit.path.getPointFromGrip = function(pt, path) {
};
if (path.matrix) {
var pt = svgedit.math.transformPoint(out.x, out.y, path.imatrix);
pt = svgedit.math.transformPoint(out.x, out.y, path.imatrix);
out.x = pt.x;
out.y = pt.y;
}
@ -156,7 +160,9 @@ svgedit.path.addPointGrip = function(index, x, y) {
var grip = $('#pathpointgrip_'+index);
grip.dblclick(function() {
if (svgedit.path.path) svgedit.path.path.setSegType();
if (svgedit.path.path) {
svgedit.path.path.setSegType();
}
});
}
if (x && y) {
@ -182,7 +188,7 @@ svgedit.path.getGripContainer = function() {
svgedit.path.addCtrlGrip = function(id) {
var pointGrip = svgedit.utilities.getElem('ctrlpointgrip_'+id);
if (pointGrip) return pointGrip;
if (pointGrip) {return pointGrip;}
pointGrip = document.createElementNS(NS.SVG, 'circle');
svgedit.utilities.assignAttributes(pointGrip, {
@ -202,7 +208,7 @@ svgedit.path.addCtrlGrip = function(id) {
svgedit.path.getCtrlLine = function(id) {
var ctrlLine = svgedit.utilities.getElem('ctrlLine_'+id);
if (ctrlLine) return ctrlLine;
if (ctrlLine) {return ctrlLine;}
ctrlLine = document.createElementNS(NS.SVG, 'line');
svgedit.utilities.assignAttributes(ctrlLine, {
@ -234,7 +240,7 @@ svgedit.path.getPointGrip = function(seg, update) {
svgedit.path.getControlPoints = function(seg) {
var item = seg.item;
var index = seg.index;
if (!('x1' in item) || !('x2' in item)) return null;
if (!('x1' in item) || !('x2' in item)) {return null;}
var cpt = {};
var pointGripContainer = svgedit.path.getGripContainer();
@ -243,7 +249,8 @@ svgedit.path.getControlPoints = function(seg) {
var seg_items = [prev, item];
for (var i=1; i<3; i++) {
var i;
for (i = 1; i < 3; i++) {
var id = index + 'c' + i;
var ctrlLine = cpt['c' + i + '_line'] = svgedit.path.getCtrlLine(id);
@ -286,12 +293,13 @@ svgedit.path.replacePathSeg = function(type, index, pts, elem) {
var segList = path.pathSegList;
var len = segList.numberOfItems;
var arr = [];
for (var i=0; i<len; i++) {
var i;
for (i = 0; i < len; i++) {
var cur_seg = segList.getItem(i);
arr.push(cur_seg);
}
segList.clear();
for (var i=0; i<len; i++) {
for (i = 0; i < len; i++) {
if (i == index) {
segList.appendItem(seg);
} else {
@ -332,8 +340,9 @@ svgedit.path.getSegSelector = function(seg, update) {
svgedit.path.replacePathSeg(2, 0, [pt.x, pt.y], segLine);
var pts = svgedit.path.ptObjToArr(seg.type, seg.item, true);
for (var i=0; i < pts.length; i+=2) {
var pt = svgedit.path.getGripPt(seg, {x:pts[i], y:pts[i+1]});
var i;
for (i = 0; i < pts.length; i += 2) {
pt = svgedit.path.getGripPt(seg, {x:pts[i], y:pts[i+1]});
pts[i] = pt.x;
pts[i+1] = pt.y;
}
@ -406,7 +415,8 @@ svgedit.path.Segment = function(index, item) {
};
svgedit.path.Segment.prototype.showCtrlPts = function(y) {
for (var i in this.ctrlpts) {
var i;
for (i in this.ctrlpts) {
this.ctrlpts[i].setAttribute('display', y ? 'inline' : 'none');
}
};
@ -484,14 +494,14 @@ svgedit.path.Segment.prototype.move = function(dx, dy) {
if (this.mate) {
// The last point of a closed subpath has a 'mate',
// which is the 'M' segment of the subpath
var item = this.mate.item;
item = this.mate.item;
var pts = [item.x += dx, item.y += dy];
svgedit.path.replacePathSeg(this.mate.type, this.mate.index, pts);
// Has no grip, so does not need 'updating'?
}
this.update(true);
if (this.next) this.next.update(true);
if (this.next) {this.next.update(true);}
};
svgedit.path.Segment.prototype.setLinked = function(num) {
@ -499,12 +509,12 @@ svgedit.path.Segment.prototype.setLinked = function(num) {
if (num == 2) {
anum = 1;
seg = this.next;
if (!seg) return;
if (!seg) {return;}
pt = this.item;
} else {
anum = 2;
seg = this.prev;
if (!seg) return;
if (!seg) {return;}
pt = seg.item;
}
@ -566,7 +576,8 @@ svgedit.path.Path.prototype.init = function() {
this.first_seg = null;
// Set up segs array
for (var i=0; i < len; i++) {
var i;
for (i = 0; i < len; i++) {
var item = segList.getItem(i);
var segment = new svgedit.path.Segment(i, item);
segment.path = this;
@ -576,16 +587,16 @@ svgedit.path.Path.prototype.init = function() {
var segs = this.segs;
var start_i = null;
for (var i=0; i < len; i++) {
for (i = 0; i < len; i++) {
var seg = segs[i];
var next_seg = (i+1) >= len ? null : segs[i+1];
var prev_seg = (i-1) < 0 ? null : segs[i-1];
var start_seg;
if (seg.type === 2) {
if (prev_seg && prev_seg.type !== 1) {
// New sub-path, last one is open,
// so add a grip to last sub-path's first point
var start_seg = segs[start_i];
start_seg = segs[start_i];
start_seg.next = segs[start_i+1];
start_seg.next.prev = start_seg;
start_seg.addGrip();
@ -608,7 +619,7 @@ svgedit.path.Path.prototype.init = function() {
if (seg.type !== 1) {
// Last seg, doesn't close so add a grip
// to last sub-path's first point
var start_seg = segs[start_i];
start_seg = segs[start_i];
start_seg.next = segs[start_i+1];
start_seg.next.prev = start_seg;
start_seg.addGrip();
@ -634,17 +645,18 @@ svgedit.path.Path.prototype.init = function() {
};
svgedit.path.Path.prototype.eachSeg = function(fn) {
var i;
var len = this.segs.length;
for (var i=0; i < len; i++) {
for (i = 0; i < len; i++) {
var ret = fn.call(this.segs[i], i);
if (ret === false) break;
if (ret === false) {break;}
}
};
svgedit.path.Path.prototype.addSeg = function(index) {
// Adds a new segment
var seg = this.segs[index];
if (!seg.prev) return;
if (!seg.prev) {return;}
var prev = seg.prev;
var newseg;
@ -683,9 +695,10 @@ svgedit.path.Path.prototype.deleteSeg = function(index) {
seg.show(false);
var next = seg.next;
var pt;
if (seg.mate) {
// Make the next point be the "M" point
var pt = [next.item.x, next.item.y];
pt = [next.item.x, next.item.y];
svgedit.path.replacePathSeg(2, next.index, pt);
// Reposition last node
@ -695,7 +708,7 @@ svgedit.path.Path.prototype.deleteSeg = function(index) {
} else if (!seg.prev) {
// First node of open path, make next point the M
var item = seg.item;
var pt = [next.item.x, next.item.y];
pt = [next.item.x, next.item.y];
svgedit.path.replacePathSeg(2, seg.next.index, pt);
list.removeItem(index);
} else {
@ -707,7 +720,7 @@ svgedit.path.Path.prototype.subpathIsClosed = function(index) {
var closed = false;
// Check if subpath is already open
svgedit.path.path.eachSeg(function(i) {
if (i <= index) return true;
if (i <= index) {return true;}
if (this.type === 2) {
// Found M first, so open
return false;
@ -782,7 +795,7 @@ svgedit.path.Path.prototype.setSegType = function(new_type) {
// Selected seg
var cur = this.segs[sel_pt];
var prev = cur.prev;
if (!prev) continue;
if (!prev) {continue;}
if (!new_type) { // double-click, so just toggle
text = 'Toggle Path Segment Type';
@ -877,12 +890,14 @@ svgedit.path.Path.prototype.update = function() {
svgedit.path.getPath_ = function(elem) {
var p = pathData[elem.id];
if (!p) p = pathData[elem.id] = new svgedit.path.Path(elem);
if (!p) {
p = pathData[elem.id] = new svgedit.path.Path(elem);
}
return p;
};
svgedit.path.removePath_ = function(id) {
if (id in pathData) delete pathData[id];
if (id in pathData) {delete pathData[id];}
};
var getRotVals = function(x, y) {
@ -919,7 +934,7 @@ var getRotVals = function(x, y) {
svgedit.path.recalcRotatedPath = function() {
var current_path = svgedit.path.path.elem;
var angle = svgedit.utilities.getRotationAngle(current_path, true);
if (!angle) return;
if (!angle) {return;}
// selectedBBoxes[0] = svgedit.path.path.oldbbox;
var box = svgedit.utilities.getBBox(current_path),
oldbox = svgedit.path.path.oldbbox,//selectedBBoxes[0],
@ -943,7 +958,7 @@ svgedit.path.recalcRotatedPath = function() {
i -= 1;
var seg = list.getItem(i),
type = seg.pathSegType;
if (type == 1) continue;
if (type == 1) {continue;}
var rvals = getRotVals(seg.x, seg.y),
points = [rvals.x, rvals.y];
@ -973,4 +988,4 @@ svgedit.path.clearData = function() {
pathData = {};
};
})();
}());

View File

@ -1,3 +1,5 @@
/*globals $*/
/*jslint vars: true, eqeq: true, continue: true*/
/**
* Recalculate.
*
@ -63,7 +65,7 @@ svgedit.recalculate.updateClipPath = function(attr, tx, ty) {
// Returns:
// Undo command object with the resulting change
svgedit.recalculate.recalculateDimensions = function(selected) {
if (selected == null) return null;
if (selected == null) {return null;}
// Firefox Issue - 1081
if (selected.nodeName == "svg" && navigator.userAgent.indexOf("Firefox/20") >= 0) {
@ -72,10 +74,10 @@ svgedit.recalculate.recalculateDimensions = function(selected) {
var svgroot = context_.getSVGRoot();
var tlist = svgedit.transformlist.getTransformList(selected);
var k;
// remove any unnecessary transforms
if (tlist && tlist.numberOfItems > 0) {
var k = tlist.numberOfItems;
k = tlist.numberOfItems;
while (k--) {
var xform = tlist.getItem(k);
if (xform.type === 0) {
@ -96,7 +98,7 @@ svgedit.recalculate.recalculateDimensions = function(selected) {
}
// End here if all it has is a rotation
if (tlist.numberOfItems === 1 &&
svgedit.utilities.getRotationAngle(selected)) return null;
svgedit.utilities.getRotationAngle(selected)) {return null;}
}
// if this element had no transforms, we are done
@ -109,7 +111,7 @@ svgedit.recalculate.recalculateDimensions = function(selected) {
// TODO: Make this work for more than 2
if (tlist) {
var k = tlist.numberOfItems;
k = tlist.numberOfItems;
var mxs = [];
while (k--) {
var xform = tlist.getItem(k);
@ -187,19 +189,20 @@ svgedit.recalculate.recalculateDimensions = function(selected) {
case 'polygon':
case 'polyline':
initial = {};
initial['points'] = selected.getAttribute('points');
initial.points = selected.getAttribute('points');
var list = selected.points;
var len = list.numberOfItems;
changes['points'] = new Array(len);
for (var i = 0; i < len; ++i) {
changes.points = new Array(len);
var i;
for (i = 0; i < len; ++i) {
var pt = list.getItem(i);
changes['points'][i] = {x:pt.x, y:pt.y};
changes.points[i] = {x:pt.x, y:pt.y};
}
break;
case 'path':
initial = {};
initial['d'] = selected.getAttribute('d');
changes['d'] = selected.getAttribute('d');
initial.d = selected.getAttribute('d');
changes.d = selected.getAttribute('d');
break;
} // switch on element type to get initial values
@ -246,7 +249,8 @@ svgedit.recalculate.recalculateDimensions = function(selected) {
// FIXME: This blows up if the angle is exactly 0!
var s = 2/a;
}
for (var i = 0; i < tlist.numberOfItems; ++i) {
var i;
for (i = 0; i < tlist.numberOfItems; ++i) {
var xform = tlist.getItem(i);
if (xform.type == 4) {
// extract old center through mystical arts
@ -288,7 +292,7 @@ svgedit.recalculate.recalculateDimensions = function(selected) {
var childTlist = svgedit.transformlist.getTransformList(child);
// some children might not have a transform (<metadata>, <defs>, etc)
if (!childTlist) continue;
if (!childTlist) {continue;}
var m = svgedit.math.transformListToTransform(childTlist).matrix;
@ -473,7 +477,7 @@ svgedit.recalculate.recalculateDimensions = function(selected) {
context_.setStartTransform(child.getAttribute('transform'));
var childTlist = svgedit.transformlist.getTransformList(child);
if (!childTlist) continue;
if (!childTlist) {continue;}
var em = svgedit.math.matrixMultiply(m, svgedit.math.transformListToTransform(childTlist).matrix);
var e2m = svgroot.createSVGTransform();

View File

@ -1,3 +1,5 @@
/*globals $, svgedit*/
/*jslint vars: true, eqeq: true*/
/**
* Package: svgedit.sanitize
*
@ -12,7 +14,7 @@
// 2) browser.js
// 3) svgutils.js
(function() {
(function() {'use strict';
if (!svgedit.sanitize) {
svgedit.sanitize = {};
@ -138,12 +140,12 @@ svgedit.sanitize.sanitizeSvg = function(node) {
var allowedAttrs = svgWhiteList_[node.nodeName];
var allowedAttrsNS = svgWhiteListNS_[node.nodeName];
var i;
// if this element is supported, sanitize it
if (typeof allowedAttrs !== 'undefined') {
var seAttrs = [];
var i = node.attributes.length;
i = node.attributes.length;
while (i--) {
// if the attribute is not in our whitelist, then remove it
// could use jQuery's inArray(), but I don't know if that's any better
@ -173,6 +175,7 @@ svgedit.sanitize.sanitizeSvg = function(node) {
case 'patternTransform':
var val = attr.nodeValue.replace(/(\d)-/g, '$1 -');
node.setAttribute(attrName, val);
break;
}
}
@ -202,8 +205,7 @@ svgedit.sanitize.sanitizeSvg = function(node) {
var href = svgedit.utilities.getHref(node);
if (href &&
['filter', 'linearGradient', 'pattern',
'radialGradient', 'textPath', 'use'].indexOf(node.nodeName) >= 0)
{
'radialGradient', 'textPath', 'use'].indexOf(node.nodeName) >= 0) {
// TODO: we simply check if the first character is a #, is this bullet-proof?
if (href[0] != '#') {
// remove the attribute (but keep the element)
@ -248,9 +250,9 @@ svgedit.sanitize.sanitizeSvg = function(node) {
parent.removeChild(node);
// call sanitizeSvg on each of those children
var i = children.length;
i = children.length;
while (i--) { svgedit.sanitize.sanitizeSvg(children[i]); }
}
};
})();
}());

View File

@ -1,3 +1,5 @@
/*globals $, svgedit*/
/*jslint vars: true, eqeq: true, forin: true*/
/**
* Package: svedit.select
*
@ -13,7 +15,7 @@
// 3) math.js
// 4) svgutils.js
(function() {
(function() {'use strict';
if (!svgedit.select) {
svgedit.select = {};
@ -96,10 +98,11 @@ svgedit.select.Selector.prototype.reset = function(e) {
// Parameters:
// angle - Float indicating current rotation angle in degrees
svgedit.select.Selector.prototype.updateGripCursors = function(angle) {
var dir_arr = [];
var steps = Math.round(angle / 45);
if (steps < 0) steps += 8;
for (var dir in selectorManager_.selectorGrips) {
var dir,
dir_arr = [],
steps = Math.round(angle / 45);
if (steps < 0) {steps += 8;}
for (dir in selectorManager_.selectorGrips) {
dir_arr.push(dir);
}
while (steps > 0) {
@ -107,7 +110,7 @@ svgedit.select.Selector.prototype.updateGripCursors = function(angle) {
steps--;
}
var i = 0;
for (var dir in selectorManager_.selectorGrips) {
for (dir in selectorManager_.selectorGrips) {
selectorManager_.selectorGrips[dir].setAttribute('style', ('cursor:' + dir_arr[i] + '-resize'));
i++;
}
@ -169,8 +172,8 @@ svgedit.select.Selector.prototype.resize = function() {
}
// apply the transforms
var l = bbox.x, t = bbox.y, w = bbox.width, h = bbox.height,
bbox = {x:l, y:t, width:w, height:h};
var l = bbox.x, t = bbox.y, w = bbox.width, h = bbox.height;
bbox = {x:l, y:t, width:w, height:h};
// we need to handle temporary transforms too
// if skewed, get its transformed box, then find its axis-aligned bbox
@ -206,12 +209,12 @@ svgedit.select.Selector.prototype.resize = function() {
maxx = tl.x,
maxy = tl.y;
var Min = Math.min, Max = Math.max;
var min = Math.min, max = Math.max;
minx = Min(minx, Min(nbox.tr.x, Min(nbox.bl.x, nbox.br.x) ) ) - offset;
miny = Min(miny, Min(nbox.tr.y, Min(nbox.bl.y, nbox.br.y) ) ) - offset;
maxx = Max(maxx, Max(nbox.tr.x, Max(nbox.bl.x, nbox.br.x) ) ) + offset;
maxy = Max(maxy, Max(nbox.tr.y, Max(nbox.bl.y, nbox.br.y) ) ) + offset;
minx = min(minx, min(nbox.tr.x, min(nbox.bl.x, nbox.br.x) ) ) - offset;
miny = min(miny, min(nbox.tr.y, min(nbox.bl.y, nbox.br.y) ) ) - offset;
maxx = max(maxx, max(nbox.tr.x, max(nbox.bl.x, nbox.br.x) ) ) + offset;
maxy = max(maxy, max(nbox.tr.y, max(nbox.bl.y, nbox.br.y) ) ) + offset;
nbax = minx;
nbay = miny;
@ -241,8 +244,8 @@ svgedit.select.Selector.prototype.resize = function() {
'e': [nbax + nbaw, nbay + (nbah)/2],
's': [nbax + (nbaw)/2, nbay + nbah]
};
for (var dir in this.gripCoords) {
var dir;
for (dir in this.gripCoords) {
var coords = this.gripCoords[dir];
selectedGrips[dir].setAttribute('cx', coords[0]);
selectedGrips[dir].setAttribute('cy', coords[1]);
@ -320,7 +323,8 @@ svgedit.select.SelectorManager.prototype.initGroup = function() {
this.rubberBandBox = null;
// add the corner grips
for (var dir in this.selectorGrips) {
var dir;
for (dir in this.selectorGrips) {
var grip = svgFactory_.createSVGElement({
'element': 'circle',
'attr': {
@ -369,7 +373,7 @@ svgedit.select.SelectorManager.prototype.initGroup = function() {
);
$.data(this.rotateGrip, 'type', 'rotate');
if ($('#canvasBackground').length) return;
if ($('#canvasBackground').length) {return;}
var dims = config_.dimensions;
var canvasbg = svgFactory_.createSVGElement({
@ -412,14 +416,15 @@ svgedit.select.SelectorManager.prototype.initGroup = function() {
// Parameters:
// elem - DOM element to get the selector for
svgedit.select.SelectorManager.prototype.requestSelector = function(elem) {
if (elem == null) return null;
var N = this.selectors.length;
if (elem == null) {return null;}
var i,
N = this.selectors.length;
// If we've already acquired one for this element, return it.
if (typeof(this.selectorMap[elem.id]) == 'object') {
this.selectorMap[elem.id].locked = true;
return this.selectorMap[elem.id];
}
for (var i = 0; i < N; ++i) {
for (i = 0; i < N; ++i) {
if (this.selectors[i] && !this.selectors[i].locked) {
this.selectors[i].locked = true;
this.selectors[i].reset(elem);
@ -440,10 +445,11 @@ svgedit.select.SelectorManager.prototype.requestSelector = function(elem) {
// Parameters:
// elem - DOM element to remove the selector for
svgedit.select.SelectorManager.prototype.releaseSelector = function(elem) {
if (elem == null) return;
var N = this.selectors.length,
if (elem == null) {return;}
var i,
N = this.selectors.length,
sel = this.selectorMap[elem.id];
for (var i = 0; i < N; ++i) {
for (i = 0; i < N; ++i) {
if (this.selectors[i] && this.selectors[i] == sel) {
if (sel.locked == false) {
// TODO(codedread): Ensure this exists in this module.
@ -525,4 +531,4 @@ svgedit.select.getSelectorManager = function() {
return selectorManager_;
};
})();
}());

View File

@ -1,7 +1,8 @@
/*globals $, svgedit*/
/**
*
* Licensed under the MIT License
* main object, loaded first so other modules have the garanty of its existence
* main object, loaded first so other modules have the guarantee of its existence
*/
svgedit = {
@ -18,7 +19,7 @@ svgedit = {
};
// return the svgedit.NS with key values switched and lowercase
svgedit.getReverseNS = function() {
svgedit.getReverseNS = function() {'use strict';
var reverseNS = {};
$.each(this.NS, function(name, URI) {
reverseNS[URI] = name.toLowerCase();