diff --git a/editor/svgcanvas.js b/editor/svgcanvas.js index b3870949..e571cddb 100644 --- a/editor/svgcanvas.js +++ b/editor/svgcanvas.js @@ -26,7 +26,7 @@ // 12) coords.js // 13) recalculate.js -(function () {'use strict'; +(function () { if (!window.console) { window.console = {}; @@ -1001,7 +1001,7 @@ var recalculateAllSelectedDimensions = this.recalculateAllSelectedDimensions = f var i = selectedElements.length; while (i--) { var elem = selectedElements[i]; -// if (svgedit.utilities.getRotationAngle(elem) && !svgedit.math.hasMatrixTransform(getTransformList(elem))) continue; +// if (svgedit.utilities.getRotationAngle(elem) && !svgedit.math.hasMatrixTransform(getTransformList(elem))) {continue;} var cmd = svgedit.recalculate.recalculateDimensions(elem); if (cmd) { batchCmd.addSubCommand(cmd); @@ -1844,7 +1844,7 @@ var getMouseTarget = this.getMouseTarget = function(evt) { scale = svgroot.createSVGTransform(), translateBack = svgroot.createSVGTransform(); - if (curConfig.gridSnapping){ + if (curConfig.gridSnapping) { left = svgedit.utilities.snapToGrid(left); tx = svgedit.utilities.snapToGrid(tx); top = svgedit.utilities.snapToGrid(top); @@ -1897,7 +1897,7 @@ var getMouseTarget = this.getMouseTarget = function(evt) { var handle = null; if (!window.opera) {svgroot.suspendRedraw(1000);} - if (curConfig.gridSnapping){ + if (curConfig.gridSnapping) { x = svgedit.utilities.snapToGrid(x); y = svgedit.utilities.snapToGrid(y); } @@ -1935,7 +1935,7 @@ var getMouseTarget = this.getMouseTarget = function(evt) { new_y = Math.min(start_y, y); } - if (curConfig.gridSnapping){ + if (curConfig.gridSnapping) { w = svgedit.utilities.snapToGrid(w); h = svgedit.utilities.snapToGrid(h); new_x = svgedit.utilities.snapToGrid(new_x); @@ -1955,7 +1955,7 @@ var getMouseTarget = this.getMouseTarget = function(evt) { cx = c.cx; cy = c.cy; var rad = Math.sqrt( (x-cx)*(x-cx) + (y-cy)*(y-cy) ); - if (curConfig.gridSnapping){ + if (curConfig.gridSnapping) { rad = svgedit.utilities.snapToGrid(rad); } shape.setAttributeNS(null, "r", rad); @@ -1967,7 +1967,7 @@ var getMouseTarget = this.getMouseTarget = function(evt) { // Opera has a problem with suspendRedraw() apparently handle = null; if (!window.opera) {svgroot.suspendRedraw(1000);} - if (curConfig.gridSnapping){ + if (curConfig.gridSnapping) { x = svgedit.utilities.snapToGrid(x); cx = svgedit.utilities.snapToGrid(cx); y = svgedit.utilities.snapToGrid(y); @@ -2015,7 +2015,7 @@ var getMouseTarget = this.getMouseTarget = function(evt) { x *= current_zoom; y *= current_zoom; - if (curConfig.gridSnapping){ + if (curConfig.gridSnapping) { x = svgedit.utilities.snapToGrid(x); y = svgedit.utilities.snapToGrid(y); start_x = svgedit.utilities.snapToGrid(start_x); @@ -2073,7 +2073,7 @@ var getMouseTarget = this.getMouseTarget = function(evt) { cx = center.x; cy = center.y; angle = ((Math.atan2(cy-y, cx-x) * (180/Math.PI))-90) % 360; - if (curConfig.gridSnapping){ + if (curConfig.gridSnapping) { angle = svgedit.utilities.snapToGrid(angle); } if (evt.shiftKey) { // restrict rotations to nice angles (WRS) @@ -2467,7 +2467,7 @@ var getMouseTarget = this.getMouseTarget = function(evt) { //TODO(rafaelcastrocouto): User preference for shift key and zoom factor $(container).bind("mousewheel DOMMouseScroll", function(e){ - //if (!e.shiftKey) return; + //if (!e.shiftKey) {return;} e.preventDefault(); var evt = e.originalEvent; @@ -3264,7 +3264,7 @@ pathActions = canvas.pathActions = function() { if (newPoint) { // First point -// if (!index) return; +// if (!index) {return;} // Set control points var pointGrip1 = svgedit.path.addCtrlGrip('1c1'); @@ -3906,7 +3906,7 @@ pathActions = canvas.pathActions = function() { curx = x; cury = y; } - if (type === 3) last_m = [curx, cury]; + if (type === 3) {last_m = [curx, cury];} addToD([[x, y]]); break; @@ -3993,9 +3993,9 @@ pathActions = canvas.pathActions = function() { // The amount of elements that were removed var removeUnusedDefElems = this.removeUnusedDefElems = function() { var defs = svgcontent.getElementsByTagNameNS(NS.SVG, "defs"); - if (!defs || !defs.length) return 0; + if (!defs || !defs.length) {return 0;} -// if (!defs.firstChild) return; +// if (!defs.firstChild) {return;} var defelem_uses = [], numRemoved = 0; @@ -4005,9 +4005,10 @@ var removeUnusedDefElems = this.removeUnusedDefElems = function() { var all_els = svgcontent.getElementsByTagNameNS(NS.SVG, '*'); var all_len = all_els.length; - for (var i=0; i=0; i--) { + for (i = attrs.length - 1; i >= 0; i--) { attr = attrs.item(i); var attrVal = toXml(attr.nodeValue); //remove bogus attributes added by Gecko - if (moz_attrs.indexOf(attr.localName) >= 0) continue; + if (moz_attrs.indexOf(attr.localName) >= 0) {continue;} if (attrVal != "") { - if (attrVal.indexOf('pointer-events') === 0) continue; - if (attr.localName === "class" && attrVal.indexOf('se_') === 0) continue; + if (attrVal.indexOf('pointer-events') === 0) {continue;} + if (attr.localName === "class" && attrVal.indexOf('se_') === 0) {continue;} out.push(" "); - if (attr.localName === 'd') attrVal = pathActions.convertPath(elem, true); + if (attr.localName === 'd') {attrVal = pathActions.convertPath(elem, true);} if (!isNaN(attrVal)) { attrVal = svgedit.units.shortFloat(attrVal); } else if (unit_re.test(attrVal)) { @@ -4209,7 +4210,7 @@ this.svgToString = function(elem, indent) { && save_options.images === 'embed') { var img = encodableImages[attrVal]; - if (img) attrVal = img; + if (img) {attrVal = img;} } // map various namespaces to our fixed namespace prefixes @@ -4227,7 +4228,7 @@ this.svgToString = function(elem, indent) { indent++; var bOneLine = false; - for (var i=0; i"); } else { @@ -4295,7 +4296,7 @@ this.embedImage = function(val, callback) { encodableImages[val] = false; } last_good_img_url = val; - if (callback) callback(encodableImages[val]); + if (callback) {callback(encodableImages[val]);} }).attr('src', val); }; @@ -4320,7 +4321,7 @@ this.save = function(opts) { // remove the selected outline before serializing clearSelection(); // Update save options if provided - if (opts) $.extend(save_options, opts); + if (opts) {$.extend(save_options, opts);} save_options.apply = true; // no need for doctype, see http://jwatt.org/svg/authoring/#doctype-declaration @@ -4350,7 +4351,7 @@ this.rasterExport = function(imgType, quality) { // Add font/text check if Canvas Text API is not implemented if (!("font" in $('')[0].getContext('2d'))) { - issue_list['text'] = uiStrings.exportNoText; + issue_list.text = uiStrings.exportNoText; } $.each(issue_list, function(sel, descr) { @@ -4384,8 +4385,8 @@ this.getSvgString = function() { // if you're controlling SVG-Edit externally, and want randomized IDs, call // this BEFORE calling svgCanvas.setSvgString // -this.randomizeIds = function() { - if (arguments.length > 0 && arguments[0] == false) { +this.randomizeIds = function(enableRandomization) { + if (arguments.length > 0 && enableRandomization == false) { svgedit.draw.randomizeIds(false, getCurrentDrawing()); } else { svgedit.draw.randomizeIds(true, getCurrentDrawing()); @@ -4419,7 +4420,7 @@ var uniquifyElems = this.uniquifyElems = function(g) { // add this id to our map ids[n.id] = {elem:null, attrs:[], hrefs:[]}; } - ids[n.id]["elem"] = n; + ids[n.id].elem = n; } // now search for all attributes on this element that might refer @@ -4435,7 +4436,7 @@ var uniquifyElems = this.uniquifyElems = function(g) { // add this id to our map ids[refid] = {elem:null, attrs:[], hrefs:[]}; } - ids[refid]["attrs"].push(attrnode); + ids[refid].attrs.push(attrnode); } } }); @@ -4450,16 +4451,17 @@ var uniquifyElems = this.uniquifyElems = function(g) { // add this id to our map ids[refid] = {elem:null, attrs:[], hrefs:[]}; } - ids[refid]["hrefs"].push(n); + ids[refid].hrefs.push(n); } } } }); // in ids, we now have a map of ids, elements and attributes, let's re-identify - for (var oldid in ids) { - if (!oldid) continue; - var elem = ids[oldid]["elem"]; + var oldid; + for (oldid in ids) { + if (!oldid) {continue;} + var elem = ids[oldid].elem; if (elem) { var newid = getNextId(); @@ -4467,7 +4469,7 @@ var uniquifyElems = this.uniquifyElems = function(g) { elem.id = newid; // remap all url() attributes - var attrs = ids[oldid]["attrs"]; + var attrs = ids[oldid].attrs; var j = attrs.length; while (j--) { var attr = attrs[j]; @@ -4475,7 +4477,7 @@ var uniquifyElems = this.uniquifyElems = function(g) { } // remap all href attributes - var hreffers = ids[oldid]["hrefs"]; + var hreffers = ids[oldid].hrefs; var k = hreffers.length; while (k--) { var hreffer = hreffers[k]; @@ -4497,7 +4499,7 @@ var setUseData = this.setUseData = function(parent) { elems.each(function() { var id = getHref(this).substr(1); var ref_elem = svgedit.utilities.getElem(id); - if (!ref_elem) return; + if (!ref_elem) {return;} $(this).data('ref', ref_elem); if (ref_elem.tagName == 'symbol' || ref_elem.tagName == 'svg') { $(this).data('symbol', ref_elem).data('ref', ref_elem); @@ -4521,14 +4523,14 @@ var convertGradients = this.convertGradients = function(elem) { if ($(grad).attr('gradientUnits') === 'userSpaceOnUse') { // TODO: Support more than one element with this ref by duplicating parent grad var elems = $(svgcontent).find('[fill="url(#' + grad.id + ')"],[stroke="url(#' + grad.id + ')"]'); - if (!elems.length) return; + if (!elems.length) {return;} // get object's bounding box var bb = svgedit.utilities.getBBox(elems[0]); // This will occur if the element is inside a or a , // in which we shouldn't need to convert anyway. - if (!bb) return; + if (!bb) {return;} if (grad.tagName === 'linearGradient') { var g_coords = $(grad).attr(['x1', 'y1', 'x2', 'y2']); @@ -4554,7 +4556,8 @@ var convertGradients = this.convertGradients = function(elem) { y2: (g_coords.y2 - bb.y) / bb.height }); grad.removeAttribute('gradientUnits'); - } else { + } + // else { // Note: radialGradient elements cannot be easily converted // because userSpaceOnUse will keep circular gradients, while // objectBoundingBox will x/y scale the gradient according to @@ -4573,7 +4576,7 @@ var convertGradients = this.convertGradients = function(elem) { // }); // // grad.removeAttribute('gradientUnits'); - } + // } } }); }; @@ -4631,7 +4634,8 @@ var convertToGroup = this.convertToGroup = function(elem) { var g = svgdoc.createElementNS(NS.SVG, "g"); var childs = elem.childNodes; - for (var i = 0; i < childs.length; i++) { + var i; + for (i = 0; i < childs.length; i++) { g.appendChild(childs[i].cloneNode(true)); } @@ -4689,7 +4693,7 @@ var convertToGroup = this.convertToGroup = function(elem) { // Give ID for any visible element missing one $(g).find(visElems).each(function() { - if (!this.id) this.id = getNextId(); + if (!this.id) {this.id = getNextId();} }); selectOnly([g]); @@ -4775,7 +4779,7 @@ this.setSvgString = function(xmlString) { // Wrap child SVGs in group elements content.find('svg').each(function() { // Skip if it's in a - if ($(this).closest('defs').length) return; + if ($(this).closest('defs').length) {return;} uniquifyElems(this); @@ -4813,7 +4817,7 @@ this.setSvgString = function(xmlString) { var attrs = { id: 'svgcontent', - overflow: curConfig.show_outside_canvas?'visible':'hidden' + overflow: curConfig.show_outside_canvas ? 'visible' : 'hidden' }; var percs = false; @@ -4830,9 +4834,9 @@ this.setSvgString = function(xmlString) { // Set to 100 if not given var val = content.attr(dim); - if (!val) val = '100%'; + if (!val) {val = '100%';} - if ((val+'').substr(-1) === "%") { + if (String(val).substr(-1) === "%") { // Use user units if percentage given percs = true; } else { @@ -4846,7 +4850,7 @@ this.setSvgString = function(xmlString) { // Give ID for any visible layer children missing one content.children().find(visElems).each(function() { - if (!this.id) this.id = getNextId(); + if (!this.id) {this.id = getNextId();} }); // Percentage width/height, so let's base it on visible elements @@ -4858,12 +4862,12 @@ this.setSvgString = function(xmlString) { // Just in case negative numbers are given or // result from the percs calculation - if (attrs.width <= 0) attrs.width = 100; - if (attrs.height <= 0) attrs.height = 100; + if (attrs.width <= 0) {attrs.width = 100;} + if (attrs.height <= 0) {attrs.height = 100;} content.attr(attrs); - this.contentW = attrs['width']; - this.contentH = attrs['height']; + this.contentW = attrs.width; + this.contentH = attrs.height; batchCmd.addSubCommand(new svgedit.history.InsertElementCommand(svgcontent)); // update root to the correct size @@ -4906,7 +4910,7 @@ this.setSvgString = function(xmlString) { // was obtained // * import should happen in top-left of current zoomed viewport this.importSvgString = function(xmlString) { - + var j, ts; try { // Get unique ID var uid = svgedit.utilities.encode64(xmlString.length + xmlString).substr(0,32); @@ -4921,10 +4925,10 @@ this.importSvgString = function(xmlString) { } var batchCmd = new svgedit.history.BatchCommand("Import SVG"); - + var symbol; if (useExisting) { - var symbol = import_ids[uid].symbol; - var ts = import_ids[uid].xform; + symbol = import_ids[uid].symbol; + ts = import_ids[uid].xform; } else { // convert string into XML document var newDoc = svgedit.utilities.text2xml(xmlString); @@ -4947,8 +4951,9 @@ this.importSvgString = function(xmlString) { innervb = svg.getAttribute("viewBox"), // if no explicit viewbox, create one out of the width and height vb = innervb ? innervb.split(" ") : [0, 0, innerw, innerh]; - for (var j = 0; j < 4; ++j) + for (j = 0; j < 4; ++j) { vb[j] = +(vb[j]); + } // TODO: properly handle preserveAspectRatio var canvasw = +svgcontent.getAttribute("width"), @@ -4956,15 +4961,15 @@ this.importSvgString = function(xmlString) { // imported content should be 1/3 of the canvas on its largest dimension if (innerh > innerw) { - var ts = "scale(" + (canvash/3)/vb[3] + ")"; + ts = "scale(" + (canvash/3)/vb[3] + ")"; } else { - var ts = "scale(" + (canvash/3)/vb[2] + ")"; + ts = "scale(" + (canvash/3)/vb[2] + ")"; } // Hack to make recalculateDimensions understand how to scale ts = "translate(0) " + ts + " translate(0)"; - var symbol = svgdoc.createElementNS(NS.SVG, "symbol"); + symbol = svgdoc.createElementNS(NS.SVG, "symbol"); var defs = svgedit.utilities.findDefs(); if (svgedit.browser.isGecko()) { @@ -4979,7 +4984,8 @@ this.importSvgString = function(xmlString) { symbol.appendChild(first); } var attrs = svg.attributes; - for (var i=0; i < attrs.length; i++) { + var i; + for (i = 0; i < attrs.length; i++) { var attr = attrs[i]; symbol.setAttribute(attr.nodeName, attr.nodeValue); } @@ -5066,9 +5072,10 @@ this.cloneLayer = function(name) { var current_layer = getCurrentDrawing().getCurrentLayer(); $(current_layer).after(new_layer); var childs = current_layer.childNodes; - for (var i = 0; i < childs.length; i++) { + var i; + for (i = 0; i < childs.length; i++) { var ch = childs[i]; - if (ch.localName == 'title') continue; + if (ch.localName == 'title') {continue;} new_layer.appendChild(copyElem(ch)); } @@ -5129,6 +5136,7 @@ this.setCurrentLayer = function(name) { // Returns: // true if the rename succeeded, false otherwise. this.renameCurrentLayer = function(newname) { + var i; var drawing = getCurrentDrawing(); if (drawing.current_layer) { var oldLayer = drawing.current_layer; @@ -5137,15 +5145,15 @@ this.renameCurrentLayer = function(newname) { if (!canvas.setCurrentLayer(newname)) { var batchCmd = new svgedit.history.BatchCommand("Rename Layer"); // find the index of the layer - for (var i = 0; i < drawing.getNumLayers(); ++i) { - if (drawing.all_layers[i][1] == oldLayer) break; + for (i = 0; i < drawing.getNumLayers(); ++i) { + if (drawing.all_layers[i][1] == oldLayer) {break;} } var oldname = drawing.getLayerName(i); drawing.all_layers[i][0] = svgedit.utilities.toXml(newname); // now change the underlying title element contents var len = oldLayer.childNodes.length; - for (var i = 0; i < len; ++i) { + for (i = 0; i < len; ++i) { var child = oldLayer.childNodes.item(i); // found the element, now append all the if (child && child.tagName == "title") { @@ -5177,10 +5185,10 @@ this.renameCurrentLayer = function(newname) { // Returns: // true if the current layer position was changed, false otherwise. this.setCurrentLayerPosition = function(newpos) { - var drawing = getCurrentDrawing(); + var oldpos, drawing = getCurrentDrawing(); if (drawing.current_layer && newpos >= 0 && newpos < drawing.getNumLayers()) { - for (var oldpos = 0; oldpos < drawing.getNumLayers(); ++oldpos) { - if (drawing.all_layers[oldpos][1] == drawing.current_layer) break; + for (oldpos = 0; oldpos < drawing.getNumLayers(); ++oldpos) { + if (drawing.all_layers[oldpos][1] == drawing.current_layer) {break;} } // some unknown error condition (current_layer not in all_layers) if (oldpos == drawing.getNumLayers()) { return false; } @@ -5251,24 +5259,25 @@ this.setLayerVisibility = function(layername, bVisible) { // true if the selected elements were moved to the layer, false otherwise. this.moveSelectedToLayer = function(layername) { // find the layer + var i; var layer = null; var drawing = getCurrentDrawing(); - for (var i = 0; i < drawing.getNumLayers(); ++i) { + for (i = 0; i < drawing.getNumLayers(); ++i) { if (drawing.getLayerName(i) == layername) { layer = drawing.all_layers[i][1]; break; } } - if (!layer) return false; + if (!layer) {return false;} var batchCmd = new svgedit.history.BatchCommand("Move Elements to Layer"); // loop for each selected element and move it var selElems = selectedElements; - var i = selElems.length; + i = selElems.length; while (i--) { var elem = selElems[i]; - if (!elem) continue; + if (!elem) {continue;} var oldNextSibling = elem.nextSibling; // TODO: this is pretty brittle! var oldLayer = elem.parentNode; @@ -5285,7 +5294,7 @@ this.mergeLayer = function(skipHistory) { var batchCmd = new svgedit.history.BatchCommand("Merge Layer"); var drawing = getCurrentDrawing(); var prev = $(drawing.current_layer).prev()[0]; - if (!prev) return; + if (!prev) {return;} var childs = drawing.current_layer.childNodes; var len = childs.length; var layerNextSibling = drawing.current_layer.nextSibling; @@ -5338,9 +5347,9 @@ this.mergeAllLayers = function() { // Return from a group context to the regular kind, make any previously // disabled elements enabled again var leaveContext = this.leaveContext = function() { - var len = disabled_elems.length; + var i, len = disabled_elems.length; if (len) { - for (var i = 0; i < len; i++) { + for (i = 0; i < len; i++) { var elem = disabled_elems[i]; var orig = elData(elem, 'orig_opac'); if (orig !== 1) { @@ -5373,7 +5382,7 @@ var setContext = this.setContext = function(elem) { var opac = this.getAttribute('opacity') || 1; // Store the original's opacity elData(this, 'orig_opac', opac); - this.setAttribute('opacity', opac * .33); + this.setAttribute('opacity', opac * 0.33); this.setAttribute('style', 'pointer-events: none'); disabled_elems.push(this); }); @@ -5475,11 +5484,12 @@ this.setConfig = function(opts) { // Function: getTitle // Returns the current group/SVG's title contents this.getTitle = function(elem) { + var i; elem = elem || selectedElements[0]; - if (!elem) return; + if (!elem) {return;} elem = $(elem).data('gsvg') || $(elem).data('symbol') || elem; var childs = elem.childNodes; - for (var i=0; i<childs.length; i++) { + for (i = 0; i < childs.length; i++) { if (childs[i].nodeName == 'title') { return childs[i].textContent; } @@ -5532,11 +5542,12 @@ this.getDocumentTitle = function() { // Parameters: // newtitle - String with the new title this.setDocumentTitle = function(newtitle) { + var i; var childs = svgcontent.childNodes, doc_title = false, old_title = ''; var batchCmd = new svgedit.history.BatchCommand("Change Image Title"); - for (var i=0; i<childs.length; i++) { + for (i = 0; i < childs.length; i++) { if (childs[i].nodeName == 'title') { doc_title = childs[i]; old_title = doc_title.textContent; @@ -5651,10 +5662,10 @@ this.getOffset = function() { // editor_w - Integer with the editor's workarea box's width // editor_h - Integer with the editor's workarea box's height this.setBBoxZoom = function(val, editor_w, editor_h) { - var spacer = .85; + var spacer = 0.85; var bb; var calcZoom = function(bb) { - if (!bb) return false; + if (!bb) {return false;} var w_zoom = Math.round((editor_w / bb.width)*100 * spacer)/100; var h_zoom = Math.round((editor_h / bb.height)*100 * spacer)/100; var zoomlevel = Math.min(w_zoom, h_zoom); @@ -5665,7 +5676,7 @@ this.setBBoxZoom = function(val, editor_w, editor_h) { if (typeof val == 'object') { bb = val; if (bb.width == 0 || bb.height == 0) { - var newzoom = bb.zoom?bb.zoom:current_zoom * bb.factor; + var newzoom = bb.zoom ? bb.zoom : current_zoom * bb.factor; canvas.setZoom(newzoom); return {'zoom': current_zoom, 'bbox': bb}; } @@ -5674,13 +5685,13 @@ this.setBBoxZoom = function(val, editor_w, editor_h) { switch (val) { case 'selection': - if (!selectedElements[0]) return; - var sel_elems = $.map(selectedElements, function(n){ if (n) return n; }); + if (!selectedElements[0]) {return;} + var sel_elems = $.map(selectedElements, function(n){ if (n) {return n;} }); bb = getStrokedBBox(sel_elems); break; case 'canvas': var res = getResolution(); - spacer = .95; + spacer = 0.95; bb = {width:res.w, height:res.h , x:0, y:0}; break; case 'content': @@ -5705,7 +5716,7 @@ this.setZoom = function(zoomlevel) { svgcontent.setAttribute("viewBox", "0 0 " + res.w/zoomlevel + " " + res.h/zoomlevel); current_zoom = zoomlevel; $.each(selectedElements, function(i, elem) { - if (!elem) return; + if (!elem) {return;} selectorManager.requestSelector(elem).resize(); }); pathActions.zoomChange(); @@ -5749,16 +5760,17 @@ this.setColor = function(type, val, preventUndo) { cur_shape[type] = val; cur_properties[type + '_paint'] = {type:"solidColor"}; var elems = []; + function addNonG (e) { + if (e.nodeName != "g") { + elems.push(e); + } + } var i = selectedElements.length; while (i--) { var elem = selectedElements[i]; if (elem) { if (elem.tagName == "g") { - svgedit.utilities.walkTree(elem, function(e){ - if (e.nodeName != "g") { - elems.push(e); - } - }); + svgedit.utilities.walkTree(elem, addNonG); } else { if (type == 'fill') { if (elem.tagName != "polyline" && elem.tagName != "line") { @@ -5786,7 +5798,7 @@ this.setColor = function(type, val, preventUndo) { // Parameters // type - String indicating "fill" or "stroke" to apply to an element var setGradient = this.setGradient = function(type) { - if (!cur_properties[type + '_paint'] || cur_properties[type + '_paint'].type == "solidColor") return; + if (!cur_properties[type + '_paint'] || cur_properties[type + '_paint'].type == "solidColor") {return;} var grad = canvas[type + 'Grad']; // find out if there is a duplicate gradient already in the defs var duplicate_grad = findDuplicateGradient(grad); @@ -5832,10 +5844,10 @@ var findDuplicateGradient = function(grad) { var diff = false; $.each(rad_attrs, function(i, attr) { - if (grad_attrs[attr] != og_attrs[attr]) diff = true; + if (grad_attrs[attr] != og_attrs[attr]) {diff = true;} }); - if (diff) continue; + if (diff) {continue;} } // else could be a duplicate, iterate through stops @@ -5868,8 +5880,9 @@ var findDuplicateGradient = function(grad) { }; function reorientGrads(elem, m) { + var i; var bb = svgedit.utilities.getBBox(elem); - for (var i = 0; i < 2; i++) { + for (i = 0; i < 2; i++) { var type = i === 0 ? 'fill' : 'stroke'; var attrVal = elem.getAttribute(type); if (attrVal && attrVal.indexOf('url(') === 0) { @@ -5963,14 +5976,21 @@ this.setStrokeWidth = function(val) { cur_properties.stroke_width = val; var elems = []; + function addNonG (e) { + if (e.nodeName != 'g') { + elems.push(e); + } + } var i = selectedElements.length; while (i--) { var elem = selectedElements[i]; if (elem) { - if (elem.tagName == "g") - svgedit.utilities.walkTree(elem, function(e){if (e.nodeName!="g") elems.push(e);}); - else + if (elem.tagName == "g") { + svgedit.utilities.walkTree(elem, addNonG); + } + else { elems.push(elem); + } } } if (elems.length > 0) { @@ -5988,14 +6008,21 @@ this.setStrokeWidth = function(val) { this.setStrokeAttr = function(attr, val) { cur_shape[attr.replace('-', '_')] = val; var elems = []; + function addNonG (e) { + if (e.nodeName != 'g') { + elems.push(e); + } + } var i = selectedElements.length; while (i--) { var elem = selectedElements[i]; if (elem) { - if (elem.tagName == "g") - svgedit.utilities.walkTree(elem, function(e){if (e.nodeName!="g") elems.push(e);}); - else + if (elem.tagName == "g") { + svgedit.utilities.walkTree(elem, function(e){if (e.nodeName!="g") {elems.push(e);}}); + } + else { elems.push(elem); + } } } if (elems.length > 0) { @@ -6044,10 +6071,12 @@ this.getStrokeOpacity = function() { // preventUndo - Boolean indicating whether or not this should be an undoable action this.setPaintOpacity = function(type, val, preventUndo) { cur_shape[type + '_opacity'] = val; - if (!preventUndo) + if (!preventUndo) { changeSelectedAttribute(type + "-opacity", val); - else + } + else { changeSelectedAttributeNoUndo(type + "-opacity", val); + } }; // Function: getPaintOpacity @@ -6365,7 +6394,7 @@ this.setTextContent = function(val) { // val - String with the image URL/path this.setImageURL = function(val) { var elem = selectedElements[0]; - if (!elem) return; + if (!elem) {return;} var attrs = $(elem).attr(['width', 'height']); var setsize = (!attrs.width || !attrs.height); @@ -6375,7 +6404,7 @@ this.setImageURL = function(val) { // Do nothing if no URL change or size change if (cur_href !== val) { setsize = true; - } else if (!setsize) return; + } else if (!setsize) {return;} var batchCmd = new svgedit.history.BatchCommand("Change Image URL"); @@ -6411,7 +6440,7 @@ this.setImageURL = function(val) { // val - String with the link URL/path this.setLinkURL = function(val) { var elem = selectedElements[0]; - if (!elem) return; + if (!elem) {return;} if (elem.tagName !== 'a') { // See if parent is an anchor var parents_a = $(elem).parents('a'); @@ -6424,7 +6453,7 @@ this.setLinkURL = function(val) { var cur_href = getHref(elem); - if (cur_href === val) return; + if (cur_href === val) {return;} var batchCmd = new svgedit.history.BatchCommand("Change Link URL"); @@ -6480,7 +6509,7 @@ this.removeHyperlink = function() { // See http://www.w3.org/TR/SVG/paths.html#InterfaceSVGPathSeg for list this.setSegType = function(new_type) { pathActions.setSegType(new_type); -} +}; // TODO(codedread): Remove the getBBox argument and split this function into two. // Function: convertToPath @@ -6497,7 +6526,7 @@ this.convertToPath = function(elem, getBBox) { if (elem == null) { var elems = selectedElements; $.each(selectedElements, function(i, elem) { - if (elem) canvas.convertToPath(elem); + if (elem) {canvas.convertToPath(elem);} }); return; } @@ -6550,9 +6579,10 @@ this.convertToPath = function(elem, getBBox) { var joinSegs = function(segs) { $.each(segs, function(j, seg) { + var i; var l = seg[0], pts = seg[1]; d += l; - for (var i=0; i < pts.length; i+=2) { + for (i = 0; i < pts.length; i+=2) { d += (pts[i] +','+pts[i+1]) + ' '; } }); @@ -6560,12 +6590,14 @@ this.convertToPath = function(elem, getBBox) { // Possibly the cubed root of 6, but 1.81 works best var num = 1.81; - + var a, rx; switch (elem.tagName) { case 'ellipse': case 'circle': - var a = $(elem).attr(['rx', 'ry', 'cx', 'cy']); - var cx = a.cx, cy = a.cy, rx = a.rx, ry = a.ry; + a = $(elem).attr(['rx', 'ry', 'cx', 'cy']); + var cx = a.cx, cy = a.cy; + rx = a.rx; + ry = a.ry; if (elem.tagName == 'circle') { rx = ry = $(elem).attr('r'); } @@ -6583,7 +6615,7 @@ this.convertToPath = function(elem, getBBox) { d = elem.getAttribute('d'); break; case 'line': - var a = $(elem).attr(["x1", "y1", "x2", "y2"]); + a = $(elem).attr(["x1", "y1", "x2", "y2"]); d = "M"+a.x1+","+a.y1+"L"+a.x2+","+a.y2; break; case 'polyline': @@ -6592,10 +6624,11 @@ this.convertToPath = function(elem, getBBox) { break; case 'rect': var r = $(elem).attr(['rx', 'ry']); - var rx = r.rx, ry = r.ry; + rx = r.rx; + ry = r.ry; var b = elem.getBBox(); var x = b.x, y = b.y, w = b.width, h = b.height; - var num = 4-num; // Why? Because! + num = 4 - num; // Why? Because! if (!rx && !ry) { // Regular rect @@ -6683,14 +6716,14 @@ var changeSelectedAttributeNoUndo = function(attr, newValue, elems) { // Editing node pathActions.moveNode(attr, newValue); } - var elems = elems || selectedElements; + elems = elems || selectedElements; var i = elems.length; var no_xy_elems = ['g', 'polyline', 'path']; var good_g_attrs = ['transform', 'opacity', 'filter']; while (i--) { var elem = elems[i]; - if (elem == null) continue; + if (elem == null) {continue;} // Set x,y vals on elements that don't have them if ((attr === 'x' || attr === 'y') && no_xy_elems.indexOf(elem.tagName) >= 0) { @@ -6703,7 +6736,7 @@ var changeSelectedAttributeNoUndo = function(attr, newValue, elems) { // only allow the transform/opacity/filter attribute to change on <g> elements, slightly hacky // TODO: FIXME: This doesn't seem right. Where's the body of this if statement? - if (elem.tagName === "g" && good_g_attrs.indexOf(attr) >= 0); + if (elem.tagName === "g" && good_g_attrs.indexOf(attr) >= 0) {} var oldval = attr === "#text" ? elem.textContent : elem.getAttribute(attr); if (oldval == null) {oldval = "";} if (oldval !== String(newValue)) { @@ -6735,7 +6768,7 @@ var changeSelectedAttributeNoUndo = function(attr, newValue, elems) { } else if (attr == "#href") { setHref(elem, newValue); } - else elem.setAttribute(attr, newValue); + else {elem.setAttribute(attr, newValue);} // Go into "select" mode for text changes // NOTE: Important that this happens AFTER elem.setAttribute() or else attributes like @@ -6750,7 +6783,7 @@ var changeSelectedAttributeNoUndo = function(attr, newValue, elems) { // Use the Firefox ffClone hack for text elements with gradients or // where other text attributes are changed. if (svgedit.browser.isGecko() && elem.nodeName === 'text' && /rotate/.test(elem.getAttribute('transform'))) { - if ((newValue+'').indexOf('url') === 0 || ['font-size', 'font-family', 'x', 'y'].indexOf(attr) >= 0 && elem.textContent) { + if (String(newValue).indexOf('url') === 0 || (['font-size', 'font-family', 'x', 'y'].indexOf(attr) >= 0 && elem.textContent)) { elem = ffClone(elem); } } @@ -6762,7 +6795,7 @@ var changeSelectedAttributeNoUndo = function(attr, newValue, elems) { setTimeout(function() { // Due to element replacement, this element may no longer // be part of the DOM - if (!elem.parentNode) return; + if (!elem.parentNode) {return;} selectorManager.requestSelector(elem).resize(); }, 0); } @@ -6805,7 +6838,7 @@ var changeSelectedAttributeNoUndo = function(attr, newValue, elems) { // newValue - String or number with the new attribute value // elems - The DOM elements to apply the change to var changeSelectedAttribute = this.changeSelectedAttribute = function(attr, val, elems) { - var elems = elems || selectedElements; + elems = elems || selectedElements; canvas.undoMgr.beginUndoableChange(attr, elems); var i = elems.length; @@ -6821,12 +6854,13 @@ var changeSelectedAttribute = this.changeSelectedAttribute = function(attr, val, // Removes all selected elements from the DOM and adds the change to the // history stack this.deleteSelectedElements = function() { + var i; var batchCmd = new svgedit.history.BatchCommand("Delete Elements"); var len = selectedElements.length; var selectedCopy = []; //selectedElements is being deleted - for (var i = 0; i < len; ++i) { + for (i = 0; i < len; ++i) { var selected = selectedElements[i]; - if (selected == null) break; + if (selected == null) {break;} var parent = selected.parentNode; var t = selected; @@ -6849,7 +6883,7 @@ this.deleteSelectedElements = function() { selectedElements[i] = null; batchCmd.addSubCommand(new RemoveElementCommand(elem, nextSibling, parent)); } - if (!batchCmd.isEmpty()) addCommandToHistory(batchCmd); + if (!batchCmd.isEmpty()) {addCommandToHistory(batchCmd);} call("changed", selectedCopy); clearSelection(); }; @@ -6860,12 +6894,13 @@ this.deleteSelectedElements = function() { // TODO: Combine similar code with deleteSelectedElements this.cutSelectedElements = function() { + var i; var batchCmd = new svgedit.history.BatchCommand("Cut Elements"); var len = selectedElements.length; var selectedCopy = []; //selectedElements is being deleted - for (var i = 0; i < len; ++i) { + for (i = 0; i < len; ++i) { var selected = selectedElements[i]; - if (selected == null) break; + if (selected == null) {break;} var parent = selected.parentNode; var t = selected; @@ -6882,7 +6917,7 @@ this.cutSelectedElements = function() { selectedElements[i] = null; batchCmd.addSubCommand(new RemoveElementCommand(elem, nextSibling, parent)); } - if (!batchCmd.isEmpty()) addCommandToHistory(batchCmd); + if (!batchCmd.isEmpty()) {addCommandToHistory(batchCmd);} call("changed", selectedCopy); clearSelection(); @@ -6898,7 +6933,7 @@ this.copySelectedElements = function() { this.pasteElements = function(type, x, y) { var cb = canvas.clipBoard; var len = cb.length; - if (!len) return; + if (!len) {return;} var pasted = []; var batchCmd = new svgedit.history.BatchCommand('Paste elements'); @@ -6907,11 +6942,11 @@ this.pasteElements = function(type, x, y) { while (len--) { var elem = cb[len]; - if (!elem) continue; + if (!elem) {continue;} var copy = copyElem(elem); // See if elem with elem ID is in the DOM already - if (!svgedit.utilities.getElem(elem.id)) copy.id = elem.id; + if (!svgedit.utilities.getElem(elem.id)) {copy.id = elem.id;} pasted.push(copy); (current_group || getCurrentDrawing().getCurrentLayer()).appendChild(copy); @@ -6956,8 +6991,8 @@ this.pasteElements = function(type, x, y) { // Parameters: // type - type of element to group into, defaults to <g> -this.groupSelectedElements = function(type) { - if (!type) type = 'g'; +this.groupSelectedElements = function(type, urlArg) { + if (!type) {type = 'g';} var cmd_str = ''; switch (type) { @@ -6965,7 +7000,7 @@ this.groupSelectedElements = function(type) { cmd_str = "Make hyperlink"; var url = ''; if (arguments.length > 1) { - url = arguments[1]; + url = urlArg; } break; default: @@ -6992,7 +7027,7 @@ this.groupSelectedElements = function(type) { var i = selectedElements.length; while (i--) { var elem = selectedElements[i]; - if (elem == null) continue; + if (elem == null) {continue;} if (elem.parentNode.tagName === 'a' && elem.parentNode.childNodes.length === 1) { elem = elem.parentNode; @@ -7003,7 +7038,7 @@ this.groupSelectedElements = function(type) { g.appendChild(elem); batchCmd.addSubCommand(new svgedit.history.MoveElementCommand(elem, oldNextSibling, oldParent)); } - if (!batchCmd.isEmpty()) addCommandToHistory(batchCmd); + if (!batchCmd.isEmpty()) {addCommandToHistory(batchCmd);} // update selection selectOnly([g], true); @@ -7035,12 +7070,12 @@ var pushGroupProperties = this.pushGroupProperties = function(g, undoable) { var gangle = svgedit.utilities.getRotationAngle(g); var gattrs = $(g).attr(['filter', 'opacity']); - var gfilter, gblur; + var gfilter, gblur, changes; - for (var i = 0; i < len; i++) { + for (i = 0; i < len; i++) { var elem = children[i]; - if (elem.nodeType !== 1) continue; + if (elem.nodeType !== 1) {continue;} if (gattrs.opacity !== null && gattrs.opacity !== 1) { var c_opac = elem.getAttribute('opacity') || 1; @@ -7051,10 +7086,10 @@ var pushGroupProperties = this.pushGroupProperties = function(g, undoable) { if (gattrs.filter) { var cblur = this.getBlur(elem); var orig_cblur = cblur; - if (!gblur) gblur = this.getBlur(g); + if (!gblur) {gblur = this.getBlur(g);} if (cblur) { // Is this formula correct? - cblur = (gblur-0) + (cblur-0); + cblur = Number(gblur) + Number(cblur); } else if (cblur === 0) { cblur = gblur; } @@ -7088,13 +7123,13 @@ var pushGroupProperties = this.pushGroupProperties = function(g, undoable) { var chtlist = svgedit.transformlist.getTransformList(elem); // Don't process gradient transforms - if (~elem.tagName.indexOf('Gradient')) chtlist = null; + if (~elem.tagName.indexOf('Gradient')) {chtlist = null;} // Hopefully not a problem to add this. Necessary for elements like <desc/> - if (!chtlist) continue; + if (!chtlist) {continue;} // Apparently <defs> can get get a transformlist, but we don't want it to have one! - if (elem.tagName === 'defs') continue; + if (elem.tagName === 'defs') {continue;} if (glist.numberOfItems) { // TODO: if the group's transform is just a rotate, we can always transfer the @@ -7162,8 +7197,8 @@ var pushGroupProperties = this.pushGroupProperties = function(g, undoable) { // transfer the group's transform down to each child and then // call svgedit.recalculate.recalculateDimensions() var oldxform = elem.getAttribute("transform"); - var changes = {}; - changes["transform"] = oldxform ? oldxform : ""; + changes = {}; + changes.transform = oldxform || ''; var newxform = svgroot.createSVGTransform(); @@ -7176,14 +7211,14 @@ var pushGroupProperties = this.pushGroupProperties = function(g, undoable) { chtlist.appendItem(newxform); } var cmd = svgedit.recalculate.recalculateDimensions(elem); - if (cmd) batchCmd.addSubCommand(cmd); + if (cmd) {batchCmd.addSubCommand(cmd);} } } // remove transform and make it undo-able if (xform) { - var changes = {}; + changes = {}; changes["transform"] = xform; g.setAttribute("transform", ""); g.removeAttribute("transform"); @@ -7223,7 +7258,7 @@ this.ungroupSelectedElement = function() { var batchCmd = new svgedit.history.BatchCommand("Ungroup Elements"); var cmd = pushGroupProperties(g, true); - if (cmd) batchCmd.addSubCommand(cmd); + if (cmd) {batchCmd.addSubCommand(cmd);} var parent = g.parentNode; var anchor = g.nextSibling; @@ -7256,7 +7291,7 @@ this.ungroupSelectedElement = function() { g = parent.removeChild(g); batchCmd.addSubCommand(new svgedit.history.RemoveElementCommand(g, gNextSibling, parent)); - if (!batchCmd.isEmpty()) addCommandToHistory(batchCmd); + if (!batchCmd.isEmpty()) {addCommandToHistory(batchCmd);} // update selection addToSelection(children); @@ -7318,13 +7353,13 @@ this.moveToBottomSelectedElement = function() { // dir - String that's either 'Up' or 'Down' this.moveUpDownSelected = function(dir) { var selected = selectedElements[0]; - if (!selected) return; + if (!selected) {return;} curBBoxes = []; var closest, found_cur; // jQuery sorts this list var list = $(getIntersectionList(getStrokedBBox([selected]))).toArray(); - if (dir == 'Down') list.reverse(); + if (dir == 'Down') {list.reverse();} $.each(list, function() { if (!found_cur) { @@ -7336,7 +7371,7 @@ this.moveUpDownSelected = function(dir) { closest = this; return false; }); - if (!closest) return; + if (!closest) {return;} var t = selected; var oldParent = t.parentNode; @@ -7413,8 +7448,9 @@ this.moveSelectedElements = function(dx, dy, undoable) { } } if (!batchCmd.isEmpty()) { - if (undoable) + if (undoable) { addCommandToHistory(batchCmd); + } call("changed", selectedElements); return batchCmd; } @@ -7424,6 +7460,7 @@ this.moveSelectedElements = function(dx, dy, undoable) { // Create deep DOM copies (clones) of all selected elements and move them slightly // from their originals this.cloneSelectedElements = function(x, y) { + var i, elem; var batchCmd = new svgedit.history.BatchCommand("Clone Elements"); // find all the elements selected (stop at first null) var len = selectedElements.length; @@ -7431,19 +7468,19 @@ this.cloneSelectedElements = function(x, y) { return ($(b).index() - $(a).index()); //causes an array to be sorted numerically and ascending } selectedElements.sort(sortfunction); - for (var i = 0; i < len; ++i) { - var elem = selectedElements[i]; - if (elem == null) break; + for (i = 0; i < len; ++i) { + elem = selectedElements[i]; + if (elem == null) {break;} } // use slice to quickly get the subset of elements we need var copiedElements = selectedElements.slice(0, i); this.clearSelection(true); // note that we loop in the reverse way because of the way elements are added // to the selectedElements array (top-first) - var i = copiedElements.length; + i = copiedElements.length; while (i--) { // clone each element and replace it within copiedElements - var elem = copiedElements[i] = copyElem(copiedElements[i]); + elem = copiedElements[i] = copyElem(copiedElements[i]); (current_group || getCurrentDrawing().getCurrentLayer()).appendChild(elem); batchCmd.addSubCommand(new svgedit.history.InsertElementCommand(elem)); } @@ -7463,14 +7500,15 @@ this.cloneSelectedElements = function(x, y) { // relative_to - String that must be one of the following: // "selected", "largest", "smallest", "page" this.alignSelectedElements = function(type, relative_to) { + var i, elem; var bboxes = [], angles = []; var minx = Number.MAX_VALUE, maxx = Number.MIN_VALUE, miny = Number.MAX_VALUE, maxy = Number.MIN_VALUE; var curwidth = Number.MIN_VALUE, curheight = Number.MIN_VALUE; var len = selectedElements.length; - if (!len) return; - for (var i = 0; i < len; ++i) { - if (selectedElements[i] == null) break; - var elem = selectedElements[i]; + if (!len) {return;} + for (i = 0; i < len; ++i) { + if (selectedElements[i] == null) {break;} + elem = selectedElements[i]; bboxes[i] = getStrokedBBox([elem]); // now bbox is axis-aligned and handles rotation @@ -7498,10 +7536,10 @@ this.alignSelectedElements = function(type, relative_to) { } break; default: // 'selected' - if (bboxes[i].x < minx) minx = bboxes[i].x; - if (bboxes[i].y < miny) miny = bboxes[i].y; - if (bboxes[i].x + bboxes[i].width > maxx) maxx = bboxes[i].x + bboxes[i].width; - if (bboxes[i].y + bboxes[i].height > maxy) maxy = bboxes[i].y + bboxes[i].height; + if (bboxes[i].x < minx) {minx = bboxes[i].x;} + if (bboxes[i].y < miny) {miny = bboxes[i].y;} + if (bboxes[i].x + bboxes[i].width > maxx) {maxx = bboxes[i].x + bboxes[i].width;} + if (bboxes[i].y + bboxes[i].height > maxy) {maxy = bboxes[i].y + bboxes[i].height;} break; } } // loop for each element to find the bbox and adjust min/max @@ -7515,9 +7553,9 @@ this.alignSelectedElements = function(type, relative_to) { var dx = new Array(len); var dy = new Array(len); - for (var i = 0; i < len; ++i) { - if (selectedElements[i] == null) break; - var elem = selectedElements[i]; + for (i = 0; i < len; ++i) { + if (selectedElements[i] == null) {break;} + elem = selectedElements[i]; var bbox = bboxes[i]; dx[i] = 0; dy[i] = 0; @@ -7637,18 +7675,19 @@ this.setBackground = function(color, url) { // Parameters: // next - Boolean where true = next and false = previous element this.cycleElement = function(next) { + var num; var cur_elem = selectedElements[0]; var elem = false; var all_elems = getVisibleElements(current_group || getCurrentDrawing().getCurrentLayer()); - if (!all_elems.length) return; + if (!all_elems.length) {return;} if (cur_elem == null) { - var num = next?all_elems.length-1:0; + num = next?all_elems.length-1:0; elem = all_elems[num]; } else { var i = all_elems.length; while (i--) { if (all_elems[i] == cur_elem) { - var num = next?i-1:i+1; + num = next ? i - 1 : i + 1; if (num >= all_elems.length) { num = 0; } else if (num < 0) {