diff --git a/Makefile b/Makefile index c6eb9607..cd67f0b5 100644 --- a/Makefile +++ b/Makefile @@ -31,7 +31,7 @@ build/$(PACKAGE): --js sanitize.js \ --js history.js \ --js select.js \ - --js document.js \ + --js draw.js \ --js svgcanvas.js \ --js svg-editor.js \ --js locale/locale.js \ diff --git a/editor/document.js b/editor/draw.js similarity index 74% rename from editor/document.js rename to editor/draw.js index 4a52b4a2..fc97ccea 100644 --- a/editor/document.js +++ b/editor/draw.js @@ -1,24 +1,11 @@ /** - * Package: svgedit.document + * Package: svgedit.draw * * Licensed under the Apache License, Version 2 * - * Copyright(c) 2010 Jeff Schiller + * Copyright(c) 2011 Jeff Schiller */ -/* - TODO: consider renaming this package to "draw" and the class to "Drawing" - - TODOs: - - Phase 1: - - migrate usages of randomizeIds() to proxy into the Document - - Phase 2: - - migrate uniquifyElems into this module - - migrate as many usages of svgcontent in svgcanvas to using a Document instance as possible - - */ // Dependencies: // 1) jQuery @@ -27,34 +14,34 @@ if (!window.svgedit) { window.svgedit = {}; } -if (!svgedit.document) { - svgedit.document = {}; +if (!svgedit.draw) { + svgedit.draw = {}; } var svg_ns = "http://www.w3.org/2000/svg"; var se_ns = "http://svg-edit.googlecode.com"; /** - * This class encapsulates the concept of a layer in the document. + * This class encapsulates the concept of a layer in the drawing * @param name {String} Layer name * @param child {SVGGElement} Layer SVG group. */ -svgedit.document.Layer = function(name, group) { +svgedit.draw.Layer = function(name, group) { this.name_ = name; this.group_ = group; }; -svgedit.document.Layer.prototype.getName = function() { +svgedit.draw.Layer.prototype.getName = function() { return this.name_; }; -svgedit.document.Layer.prototype.getGroup = function() { +svgedit.draw.Layer.prototype.getGroup = function() { return this.group_; }; /** - * This class encapsulates the concept of a SVG-edit document. + * This class encapsulates the concept of a SVG-edit drawing * * @param svgElem {SVGSVGElement} The SVG DOM Element that this JS object * encapsulates. If the svgElem has a se:nonce attribute on it, then @@ -62,10 +49,10 @@ svgedit.document.Layer.prototype.getGroup = function() { * @param opt_idPrefix {String} The ID prefix to use. Defaults to "svg_" * if not specified. */ -svgedit.document.Document = function(svgElem, opt_idPrefix) { +svgedit.draw.Drawing = function(svgElem, opt_idPrefix) { if (!svgElem || !svgElem.tagName || !svgElem.namespaceURI || svgElem.tagName != 'svg' || svgElem.namespaceURI != svg_ns) { - throw "Error: svgedit.document.Document instance initialized without a element"; + throw "Error: svgedit.draw.Drawing instance initialized without a element"; } this.svgElem_ = svgElem; @@ -81,7 +68,7 @@ svgedit.document.Document = function(svgElem, opt_idPrefix) { this.nonce_ = this.svgElem_.getAttributeNS(se_ns, 'nonce') || ""; }; -svgedit.document.Document.prototype.getElem_ = function(id) { +svgedit.draw.Drawing.prototype.getElem_ = function(id) { if(this.svgElem_.querySelector) { // querySelector lookup return this.svgElem_.querySelector('#'+id); @@ -91,11 +78,11 @@ svgedit.document.Document.prototype.getElem_ = function(id) { } }; -svgedit.document.Document.prototype.getSvgElem = function() { +svgedit.draw.Drawing.prototype.getSvgElem = function() { return this.svgElem_; } -svgedit.document.Document.prototype.getNonce = function() { +svgedit.draw.Drawing.prototype.getNonce = function() { return this.nonce_; }; @@ -103,7 +90,7 @@ svgedit.document.Document.prototype.getNonce = function() { * Returns the latest object id as a string. * @return {String} The latest object Id. */ -svgedit.document.Document.prototype.getId = function() { +svgedit.draw.Drawing.prototype.getId = function() { return this.nonce_ ? this.idPrefix + this.nonce_ +'_' + this.obj_num : this.idPrefix + this.obj_num; @@ -113,7 +100,7 @@ svgedit.document.Document.prototype.getId = function() { * Returns the next object Id as a string. * @return {String} The next object Id to use. */ -svgedit.document.Document.prototype.getNextId = function() { +svgedit.draw.Drawing.prototype.getNextId = function() { var oldObjNum = this.obj_num; var restoreOldObjNum = false; @@ -154,7 +141,7 @@ svgedit.document.Document.prototype.getNextId = function() { * @return {boolean} Returns true if the id was valid to be released, * false otherwise. */ -svgedit.document.Document.prototype.releaseId = function(id) { +svgedit.draw.Drawing.prototype.releaseId = function(id) { // confirm if this is a valid id for this Document, else return false var front = this.idPrefix + (this.nonce_ ? this.nonce_ +'_' : ''); if (typeof id != typeof '' || id.indexOf(front) != 0) { @@ -175,4 +162,13 @@ svgedit.document.Document.prototype.releaseId = function(id) { return true; }; +// Function: getNumLayers +// Returns the number of layers in the current drawing. +// +// Returns: +// The number of layers in the current drawing. +svgedit.draw.Drawing.prototype.getNumLayers = function() { + return this.all_layers.length; +}; + })(); diff --git a/editor/embedapi.js b/editor/embedapi.js index 40dc92cc..90f50a55 100644 --- a/editor/embedapi.js +++ b/editor/embedapi.js @@ -66,7 +66,7 @@ function embedded_svg_edit(frame){ //Run in firebug on http://svg-edit.googlecode.com/svn/trunk/docs/files/svgcanvas-js.html //for(var i=0,q=[],f = document.querySelectorAll("div.CFunction h3.CTitle a");i - + diff --git a/editor/svg-editor.js b/editor/svg-editor.js index 074c8f3b..d277bd30 100644 --- a/editor/svg-editor.js +++ b/editor/svg-editor.js @@ -3538,7 +3538,7 @@ }); $('#layer_new').click(function() { - var i = svgCanvas.getNumLayers(); + var i = svgCanvas.getCurrentDrawing().getNumLayers(); do { var uniqName = uiStrings.layers.layer + " " + ++i; } while(svgCanvas.hasLayer(uniqName)); @@ -3583,7 +3583,7 @@ } function mergeLayer() { - if($('#layerlist tr.layersel').index() == svgCanvas.getNumLayers()-1) return; + if($('#layerlist tr.layersel').index() == svgCanvas.getCurrentDrawing().getNumLayers()-1) return; svgCanvas.mergeLayer(); updateContextPanel(); populateLayers(); @@ -3591,7 +3591,7 @@ function moveLayer(pos) { var curIndex = $('#layerlist tr.layersel').index(); - var total = svgCanvas.getNumLayers(); + var total = svgCanvas.getCurrentDrawing().getNumLayers(); if(curIndex > 0 || curIndex < total-1) { curIndex += pos; svgCanvas.setCurrentLayerPosition(total-curIndex-1); @@ -3695,7 +3695,7 @@ // this function highlights the layer passed in (by fading out the other layers) // if no layer is passed in, this function restores the other layers var toggleHighlightLayer = function(layerNameToHighlight) { - var curNames = new Array(svgCanvas.getNumLayers()); + var curNames = new Array(svgCanvas.getCurrentDrawing().getNumLayers()); for (var i = 0; i < curNames.length; ++i) { curNames[i] = svgCanvas.getLayer(i); } if (layerNameToHighlight) { @@ -3718,7 +3718,7 @@ layerlist.empty(); selLayerNames.empty(); var currentlayer = svgCanvas.getCurrentLayer(); - var layer = svgCanvas.getNumLayers(); + var layer = svgCanvas.getCurrentDrawing().getNumLayers(); var icon = $.getSvgIcon('eye'); // we get the layers in the reverse z-order (the layer rendered on top is listed first) while (layer--) { diff --git a/editor/svgcanvas.js b/editor/svgcanvas.js index 4455a789..e97e69a4 100644 --- a/editor/svgcanvas.js +++ b/editor/svgcanvas.js @@ -19,7 +19,7 @@ // 7) sanitize.js // 8) history.js // 9) select.js -// 10) document.js +// 10) draw.js if(!window.console) { window.console = {}; @@ -184,15 +184,15 @@ var randomize_ids = false; // Set nonce if randomize_ids = true if (randomize_ids) svgcontent.setAttributeNS(se_ns, 'se:nonce', nonce); -// Current svgedit.document.Document object -// @type {svgedit.document.Document} -var current_doc = new svgedit.document.Document(svgcontent, idprefix); +// Current svgedit.draw.Drawing object +// @type {svgedit.draw.Drawing} +var current_drawing = new svgedit.draw.Drawing(svgcontent, idprefix); -// Function: getCurrentDoc -// Returns the current Document JS object. -// @return {svgedit.document.Document} -canvas.getCurrentDoc = function() { - return current_doc; +// Function: getCurrentDrawing +// Returns the current Drawing. +// @return {svgedit.draw.Drawing} +canvas.getCurrentDrawing = function() { + return current_drawing; }; // Float displaying the current zoom level (1 = 100%, .5 = 50%, etc) @@ -996,19 +996,8 @@ var getId, getNextId, call; // Object to contain editor event names and callback functions var events = {}; - // Function: getId - // Returns the last created DOM element ID string - getId = c.getId = function() { - return current_doc.getId(); - }; - - // Function: getNextId - // Creates and returns a unique ID string for a DOM element - getNextId = c.getNextId = function() { - var id = current_doc.getNextId(); - console.log("getNextId() returned " + id); - return id; - }; + getId = c.getId = function() { return current_drawing.getId(); }; + getNextId = c.getNextId = function() { return current_drawing.getNextId(); }; // Function: call // Run the callback function associated with the given event @@ -3468,7 +3457,7 @@ var getMouseTarget = this.getMouseTarget = function(evt) { }); if (!keep && element != null) { - current_doc.releaseId(getId()); + current_drawing.releaseId(getId()); element.parentNode.removeChild(element); element = null; @@ -6688,7 +6677,7 @@ this.setSvgString = function(xmlString) { if (extensions["Arrows"]) call("setarrownonce", nonce) ; } - current_doc = new svgedit.document.Document(svgcontent, idprefix); + current_drawing = new svgedit.draw.Drawing(svgcontent, idprefix); // change image href vals if possible content.find('image').each(function() { @@ -6965,7 +6954,7 @@ this.importSvgString = function(xmlString) { // Function: identifyLayers // Updates layer system var identifyLayers = canvas.identifyLayers = function() { - current_doc.all_layers = []; + current_drawing.all_layers = []; leaveContext(); var numchildren = svgcontent.childNodes.length; // loop through all children of svgcontent @@ -6987,7 +6976,7 @@ var identifyLayers = canvas.identifyLayers = function() { // store layer and name in global variable if (name) { layernames.push(name); - current_doc.all_layers.push( [name,child] ); + current_drawing.all_layers.push( [name,child] ); current_layer = child; svgedit.utilities.walkTree(child, function(e){e.setAttribute("style", "pointer-events:inherit");}); current_layer.setAttribute("style", "pointer-events:none"); @@ -7018,7 +7007,7 @@ var identifyLayers = canvas.identifyLayers = function() { current_layer.appendChild(orphans[j]); } current_layer = svgcontent.appendChild(current_layer); - current_doc.all_layers.push( [newname, current_layer] ); + current_drawing.all_layers.push( [newname, current_layer] ); } svgedit.utilities.walkTree(current_layer, function(e){e.setAttribute("style","pointer-events:inherit");}); current_layer.setAttribute("style","pointer-events:all"); @@ -7080,7 +7069,7 @@ this.cloneLayer = function(name) { // Deletes the current layer from the drawing and then clears the selection. This function // then calls the 'changed' handler. This is an undoable action. this.deleteCurrentLayer = function() { - if (current_layer && current_doc.all_layers.length > 1) { + if (current_layer && current_drawing.all_layers.length > 1) { var batchCmd = new BatchCommand("Delete Layer"); // actually delete from the DOM and store in our Undo History var parent = current_layer.parentNode; @@ -7090,7 +7079,7 @@ this.deleteCurrentLayer = function() { addCommandToHistory(batchCmd); clearSelection(); identifyLayers(); - canvas.setCurrentLayer(current_doc.all_layers[current_doc.all_layers.length-1][0]); + canvas.setCurrentLayer(current_drawing.all_layers[current_drawing.all_layers.length-1][0]); call("changed", [svgcontent]); return true; } @@ -7100,20 +7089,13 @@ this.deleteCurrentLayer = function() { // Function: hasLayer // Check if layer with given name already exists this.hasLayer = function(name) { - for(var i = 0; i < current_doc.all_layers.length; i++) { - if(current_doc.all_layers[i][0] == name) return true; + for(var i = 0; i < current_drawing.all_layers.length; i++) { + if(current_drawing.all_layers[i][0] == name) return true; } return false; }; -// Function: getNumLayers -// Returns the number of layers in the current drawing. -// -// Returns: -// The number of layers in the current drawing. -this.getNumLayers = function() { - return current_doc.all_layers.length; -}; +this.getNumLayers = function() { return current_drawing.getNumLayers(); }; // Function: getLayer // Returns the name of the ith layer. If the index is out of range, an empty string is returned. @@ -7124,8 +7106,8 @@ this.getNumLayers = function() { // Returns: // The name of the ith layer this.getLayer = function(i) { - if (i >= 0 && i < canvas.getNumLayers()) { - return current_doc.all_layers[i][0]; + if (i >= 0 && i < current_drawing.getNumLayers()) { + return current_drawing.all_layers[i][0]; } return ""; }; @@ -7137,9 +7119,9 @@ this.getLayer = function(i) { // Returns: // The name of the currently active layer. this.getCurrentLayer = function() { - for (var i = 0; i < current_doc.all_layers.length; ++i) { - if (current_doc.all_layers[i][1] == current_layer) { - return current_doc.all_layers[i][0]; + for (var i = 0; i < current_drawing.getNumLayers(); ++i) { + if (current_drawing.all_layers[i][1] == current_layer) { + return current_drawing.all_layers[i][0]; } } return ""; @@ -7156,12 +7138,12 @@ this.getCurrentLayer = function() { // true if the current layer was switched, otherwise false this.setCurrentLayer = function(name) { name = svgedit.utilities.toXml(name); - for (var i = 0; i < current_doc.all_layers.length; ++i) { - if (name == current_doc.all_layers[i][0]) { - if (current_layer != current_doc.all_layers[i][1]) { + for (var i = 0; i < current_drawing.all_layers.length; ++i) { + if (name == current_drawing.all_layers[i][0]) { + if (current_layer != current_drawing.all_layers[i][1]) { clearSelection(); current_layer.setAttribute("style", "pointer-events:none"); - current_layer = current_doc.all_layers[i][1]; + current_layer = current_drawing.all_layers[i][1]; current_layer.setAttribute("style", "pointer-events:all"); } return true; @@ -7187,11 +7169,11 @@ this.renameCurrentLayer = function(newname) { if (!canvas.setCurrentLayer(newname)) { var batchCmd = new BatchCommand("Rename Layer"); // find the index of the layer - for (var i = 0; i < current_doc.all_layers.length; ++i) { - if (current_doc.all_layers[i][1] == oldLayer) break; + for (var i = 0; i < current_drawing.all_layers.length; ++i) { + if (current_drawing.all_layers[i][1] == oldLayer) break; } - var oldname = current_doc.all_layers[i][0]; - current_doc.all_layers[i][0] = svgedit.utilities.toXml(newname); + var oldname = current_drawing.all_layers[i][0]; + current_drawing.all_layers[i][0] = svgedit.utilities.toXml(newname); // now change the underlying title element contents var len = oldLayer.childNodes.length; @@ -7227,31 +7209,31 @@ this.renameCurrentLayer = function(newname) { // Returns: // true if the current layer position was changed, false otherwise. this.setCurrentLayerPosition = function(newpos) { - if (current_layer && newpos >= 0 && newpos < current_doc.all_layers.length) { - for (var oldpos = 0; oldpos < current_doc.all_layers.length; ++oldpos) { - if (current_doc.all_layers[oldpos][1] == current_layer) break; + if (current_layer && newpos >= 0 && newpos < current_drawing.all_layers.length) { + for (var oldpos = 0; oldpos < current_drawing.all_layers.length; ++oldpos) { + if (current_drawing.all_layers[oldpos][1] == current_layer) break; } // some unknown error condition (current_layer not in all_layers) - if (oldpos == current_doc.all_layers.length) { return false; } + if (oldpos == current_drawing.all_layers.length) { return false; } if (oldpos != newpos) { // if our new position is below us, we need to insert before the node after newpos var refLayer = null; var oldNextSibling = current_layer.nextSibling; if (newpos > oldpos ) { - if (newpos < current_doc.all_layers.length-1) { - refLayer = current_doc.all_layers[newpos+1][1]; + if (newpos < current_drawing.all_layers.length-1) { + refLayer = current_drawing.all_layers[newpos+1][1]; } } // if our new position is above us, we need to insert before the node at newpos else { - refLayer = current_doc.all_layers[newpos][1]; + refLayer = current_drawing.all_layers[newpos][1]; } svgcontent.insertBefore(current_layer, refLayer); addCommandToHistory(new MoveElementCommand(current_layer, oldNextSibling, svgcontent)); identifyLayers(); - canvas.setCurrentLayer(current_doc.all_layers[newpos][0]); + canvas.setCurrentLayer(current_drawing.all_layers[newpos][0]); return true; } @@ -7272,9 +7254,9 @@ this.setCurrentLayerPosition = function(newpos) { this.getLayerVisibility = function(layername) { // find the layer var layer = null; - for (var i = 0; i < current_doc.all_layers.length; ++i) { - if (current_doc.all_layers[i][0] == layername) { - layer = current_doc.all_layers[i][1]; + for (var i = 0; i < current_drawing.all_layers.length; ++i) { + if (current_drawing.all_layers[i][0] == layername) { + layer = current_drawing.all_layers[i][1]; break; } } @@ -7295,9 +7277,9 @@ this.getLayerVisibility = function(layername) { this.setLayerVisibility = function(layername, bVisible) { // find the layer var layer = null; - for (var i = 0; i < current_doc.all_layers.length; ++i) { - if (current_doc.all_layers[i][0] == layername) { - layer = current_doc.all_layers[i][1]; + for (var i = 0; i < current_drawing.all_layers.length; ++i) { + if (current_drawing.all_layers[i][0] == layername) { + layer = current_drawing.all_layers[i][1]; break; } } @@ -7329,9 +7311,9 @@ this.setLayerVisibility = function(layername, bVisible) { this.moveSelectedToLayer = function(layername) { // find the layer var layer = null; - for (var i = 0; i < current_doc.all_layers.length; ++i) { - if (current_doc.all_layers[i][0] == layername) { - layer = current_doc.all_layers[i][1]; + for (var i = 0; i < current_drawing.all_layers.length; ++i) { + if (current_drawing.all_layers[i][0] == layername) { + layer = current_drawing.all_layers[i][1]; break; } } @@ -7397,7 +7379,7 @@ this.mergeLayer = function(skipHistory) { this.mergeAllLayers = function() { var batchCmd = new BatchCommand("Merge all Layers"); - current_layer = current_doc.all_layers[current_doc.all_layers.length-1][1]; + current_layer = current_drawing.all_layers[current_drawing.all_layers.length-1][1]; while($(svgcontent).children('g').length > 1) { batchCmd.addSubCommand(canvas.mergeLayer(true)); } @@ -7418,9 +7400,9 @@ this.mergeAllLayers = function() { // The opacity value of the given layer. This will be a value between 0.0 and 1.0, or null // if layername is not a valid layer this.getLayerOpacity = function(layername) { - for (var i = 0; i < current_doc.all_layers.length; ++i) { - if (current_doc.all_layers[i][0] == layername) { - var g = current_doc.all_layers[i][1]; + for (var i = 0; i < current_drawing.all_layers.length; ++i) { + if (current_drawing.all_layers[i][0] == layername) { + var g = current_drawing.all_layers[i][1]; var opacity = g.getAttribute("opacity"); if (!opacity) { opacity = "1.0"; @@ -7444,9 +7426,9 @@ this.getLayerOpacity = function(layername) { // opacity - a float value in the range 0.0-1.0 this.setLayerOpacity = function(layername, opacity) { if (opacity < 0.0 || opacity > 1.0) return; - for (var i = 0; i < current_doc.all_layers.length; ++i) { - if (current_doc.all_layers[i][0] == layername) { - var g = current_doc.all_layers[i][1]; + for (var i = 0; i < current_drawing.all_layers.length; ++i) { + if (current_drawing.all_layers[i][0] == layername) { + var g = current_drawing.all_layers[i][1]; g.setAttribute("opacity", opacity); break; } @@ -7522,7 +7504,7 @@ this.clear = function() { } } // create empty first layer - current_doc.all_layers = []; + current_drawing.all_layers = []; canvas.createLayer("Layer 1"); diff --git a/test/all_tests.html b/test/all_tests.html index 99528c26..2fe0c3ef 100644 --- a/test/all_tests.html +++ b/test/all_tests.html @@ -11,7 +11,7 @@ - + - + -

Unit Tests for document.js

+

Unit Tests for draw.js