Rename document module to draw. Add getNumLayers() to draw module.

git-svn-id: http://svg-edit.googlecode.com/svn/trunk@1925 eee81c28-f429-11dd-99c0-75d572ba1ddd
master
Jeff Schiller 2011-01-16 23:07:35 +00:00
parent 71dc44137d
commit 0db154bff4
8 changed files with 118 additions and 133 deletions

View File

@ -31,7 +31,7 @@ build/$(PACKAGE):
--js sanitize.js \ --js sanitize.js \
--js history.js \ --js history.js \
--js select.js \ --js select.js \
--js document.js \ --js draw.js \
--js svgcanvas.js \ --js svgcanvas.js \
--js svg-editor.js \ --js svg-editor.js \
--js locale/locale.js \ --js locale/locale.js \

View File

@ -1,24 +1,11 @@
/** /**
* Package: svgedit.document * Package: svgedit.draw
* *
* Licensed under the Apache License, Version 2 * 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: // Dependencies:
// 1) jQuery // 1) jQuery
@ -27,34 +14,34 @@ if (!window.svgedit) {
window.svgedit = {}; window.svgedit = {};
} }
if (!svgedit.document) { if (!svgedit.draw) {
svgedit.document = {}; svgedit.draw = {};
} }
var svg_ns = "http://www.w3.org/2000/svg"; var svg_ns = "http://www.w3.org/2000/svg";
var se_ns = "http://svg-edit.googlecode.com"; 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 name {String} Layer name
* @param child {SVGGElement} Layer SVG group. * @param child {SVGGElement} Layer SVG group.
*/ */
svgedit.document.Layer = function(name, group) { svgedit.draw.Layer = function(name, group) {
this.name_ = name; this.name_ = name;
this.group_ = group; this.group_ = group;
}; };
svgedit.document.Layer.prototype.getName = function() { svgedit.draw.Layer.prototype.getName = function() {
return this.name_; return this.name_;
}; };
svgedit.document.Layer.prototype.getGroup = function() { svgedit.draw.Layer.prototype.getGroup = function() {
return this.group_; 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 * @param svgElem {SVGSVGElement} The SVG DOM Element that this JS object
* encapsulates. If the svgElem has a se:nonce attribute on it, then * 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_" * @param opt_idPrefix {String} The ID prefix to use. Defaults to "svg_"
* if not specified. * if not specified.
*/ */
svgedit.document.Document = function(svgElem, opt_idPrefix) { svgedit.draw.Drawing = function(svgElem, opt_idPrefix) {
if (!svgElem || !svgElem.tagName || !svgElem.namespaceURI || if (!svgElem || !svgElem.tagName || !svgElem.namespaceURI ||
svgElem.tagName != 'svg' || svgElem.namespaceURI != svg_ns) { svgElem.tagName != 'svg' || svgElem.namespaceURI != svg_ns) {
throw "Error: svgedit.document.Document instance initialized without a <svg> element"; throw "Error: svgedit.draw.Drawing instance initialized without a <svg> element";
} }
this.svgElem_ = svgElem; this.svgElem_ = svgElem;
@ -81,7 +68,7 @@ svgedit.document.Document = function(svgElem, opt_idPrefix) {
this.nonce_ = this.svgElem_.getAttributeNS(se_ns, 'nonce') || ""; 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) { if(this.svgElem_.querySelector) {
// querySelector lookup // querySelector lookup
return this.svgElem_.querySelector('#'+id); 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_; return this.svgElem_;
} }
svgedit.document.Document.prototype.getNonce = function() { svgedit.draw.Drawing.prototype.getNonce = function() {
return this.nonce_; return this.nonce_;
}; };
@ -103,7 +90,7 @@ svgedit.document.Document.prototype.getNonce = function() {
* Returns the latest object id as a string. * Returns the latest object id as a string.
* @return {String} The latest object Id. * @return {String} The latest object Id.
*/ */
svgedit.document.Document.prototype.getId = function() { svgedit.draw.Drawing.prototype.getId = function() {
return this.nonce_ ? return this.nonce_ ?
this.idPrefix + this.nonce_ +'_' + this.obj_num : this.idPrefix + this.nonce_ +'_' + this.obj_num :
this.idPrefix + 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. * Returns the next object Id as a string.
* @return {String} The next object Id to use. * @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 oldObjNum = this.obj_num;
var restoreOldObjNum = false; 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, * @return {boolean} Returns true if the id was valid to be released,
* false otherwise. * 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 // confirm if this is a valid id for this Document, else return false
var front = this.idPrefix + (this.nonce_ ? this.nonce_ +'_' : ''); var front = this.idPrefix + (this.nonce_ ? this.nonce_ +'_' : '');
if (typeof id != typeof '' || id.indexOf(front) != 0) { if (typeof id != typeof '' || id.indexOf(front) != 0) {
@ -175,4 +162,13 @@ svgedit.document.Document.prototype.releaseId = function(id) {
return true; 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;
};
})(); })();

View File

@ -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 //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<f.length;i++){q.push(f[i].name)};q //for(var i=0,q=[],f = document.querySelectorAll("div.CFunction h3.CTitle a");i<f.length;i++){q.push(f[i].name)};q
//var functions = ["clearSelection", "addToSelection", "removeFromSelection", "open", "save", "getSvgString", "setSvgString", "createLayer", "deleteCurrentLayer", "getNumLayers", "getLayer", "getCurrentLayer", "setCurrentLayer", "renameCurrentLayer", "setCurrentLayerPosition", "getLayerVisibility", "setLayerVisibility", "moveSelectedToLayer", "getLayerOpacity", "setLayerOpacity", "clear"]; //var functions = ["clearSelection", "addToSelection", "removeFromSelection", "open", "save", "getSvgString", "setSvgString", "createLayer", "deleteCurrentLayer", "getLayer", "getCurrentLayer", "setCurrentLayer", "renameCurrentLayer", "setCurrentLayerPosition", "getLayerVisibility", "setLayerVisibility", "moveSelectedToLayer", "getLayerOpacity", "setLayerOpacity", "clear"];
//Newer, well, it extracts things that aren't documented as well. All functions accessible through the normal thingy can now be accessed though the API //Newer, well, it extracts things that aren't documented as well. All functions accessible through the normal thingy can now be accessed though the API

View File

@ -26,7 +26,7 @@
<script type="text/javascript" src="sanitize.js"></script> <script type="text/javascript" src="sanitize.js"></script>
<script type="text/javascript" src="history.js"></script> <script type="text/javascript" src="history.js"></script>
<script type="text/javascript" src="select.js"></script> <script type="text/javascript" src="select.js"></script>
<script type="text/javascript" src="document.js"></script> <script type="text/javascript" src="draw.js"></script>
<script type="text/javascript" src="svgcanvas.js"></script> <script type="text/javascript" src="svgcanvas.js"></script>
<script type="text/javascript" src="svg-editor.js"></script> <script type="text/javascript" src="svg-editor.js"></script>
<script type="text/javascript" src="locale/locale.js"></script> <script type="text/javascript" src="locale/locale.js"></script>

View File

@ -3538,7 +3538,7 @@
}); });
$('#layer_new').click(function() { $('#layer_new').click(function() {
var i = svgCanvas.getNumLayers(); var i = svgCanvas.getCurrentDrawing().getNumLayers();
do { do {
var uniqName = uiStrings.layers.layer + " " + ++i; var uniqName = uiStrings.layers.layer + " " + ++i;
} while(svgCanvas.hasLayer(uniqName)); } while(svgCanvas.hasLayer(uniqName));
@ -3583,7 +3583,7 @@
} }
function mergeLayer() { function mergeLayer() {
if($('#layerlist tr.layersel').index() == svgCanvas.getNumLayers()-1) return; if($('#layerlist tr.layersel').index() == svgCanvas.getCurrentDrawing().getNumLayers()-1) return;
svgCanvas.mergeLayer(); svgCanvas.mergeLayer();
updateContextPanel(); updateContextPanel();
populateLayers(); populateLayers();
@ -3591,7 +3591,7 @@
function moveLayer(pos) { function moveLayer(pos) {
var curIndex = $('#layerlist tr.layersel').index(); var curIndex = $('#layerlist tr.layersel').index();
var total = svgCanvas.getNumLayers(); var total = svgCanvas.getCurrentDrawing().getNumLayers();
if(curIndex > 0 || curIndex < total-1) { if(curIndex > 0 || curIndex < total-1) {
curIndex += pos; curIndex += pos;
svgCanvas.setCurrentLayerPosition(total-curIndex-1); svgCanvas.setCurrentLayerPosition(total-curIndex-1);
@ -3695,7 +3695,7 @@
// this function highlights the layer passed in (by fading out the other layers) // 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 // if no layer is passed in, this function restores the other layers
var toggleHighlightLayer = function(layerNameToHighlight) { 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); } for (var i = 0; i < curNames.length; ++i) { curNames[i] = svgCanvas.getLayer(i); }
if (layerNameToHighlight) { if (layerNameToHighlight) {
@ -3718,7 +3718,7 @@
layerlist.empty(); layerlist.empty();
selLayerNames.empty(); selLayerNames.empty();
var currentlayer = svgCanvas.getCurrentLayer(); var currentlayer = svgCanvas.getCurrentLayer();
var layer = svgCanvas.getNumLayers(); var layer = svgCanvas.getCurrentDrawing().getNumLayers();
var icon = $.getSvgIcon('eye'); var icon = $.getSvgIcon('eye');
// we get the layers in the reverse z-order (the layer rendered on top is listed first) // we get the layers in the reverse z-order (the layer rendered on top is listed first)
while (layer--) { while (layer--) {

View File

@ -19,7 +19,7 @@
// 7) sanitize.js // 7) sanitize.js
// 8) history.js // 8) history.js
// 9) select.js // 9) select.js
// 10) document.js // 10) draw.js
if(!window.console) { if(!window.console) {
window.console = {}; window.console = {};
@ -184,15 +184,15 @@ var randomize_ids = false;
// Set nonce if randomize_ids = true // Set nonce if randomize_ids = true
if (randomize_ids) svgcontent.setAttributeNS(se_ns, 'se:nonce', nonce); if (randomize_ids) svgcontent.setAttributeNS(se_ns, 'se:nonce', nonce);
// Current svgedit.document.Document object // Current svgedit.draw.Drawing object
// @type {svgedit.document.Document} // @type {svgedit.draw.Drawing}
var current_doc = new svgedit.document.Document(svgcontent, idprefix); var current_drawing = new svgedit.draw.Drawing(svgcontent, idprefix);
// Function: getCurrentDoc // Function: getCurrentDrawing
// Returns the current Document JS object. // Returns the current Drawing.
// @return {svgedit.document.Document} // @return {svgedit.draw.Drawing}
canvas.getCurrentDoc = function() { canvas.getCurrentDrawing = function() {
return current_doc; return current_drawing;
}; };
// Float displaying the current zoom level (1 = 100%, .5 = 50%, etc) // 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 // Object to contain editor event names and callback functions
var events = {}; var events = {};
// Function: getId getId = c.getId = function() { return current_drawing.getId(); };
// Returns the last created DOM element ID string getNextId = c.getNextId = function() { return current_drawing.getNextId(); };
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;
};
// Function: call // Function: call
// Run the callback function associated with the given event // Run the callback function associated with the given event
@ -3468,7 +3457,7 @@ var getMouseTarget = this.getMouseTarget = function(evt) {
}); });
if (!keep && element != null) { if (!keep && element != null) {
current_doc.releaseId(getId()); current_drawing.releaseId(getId());
element.parentNode.removeChild(element); element.parentNode.removeChild(element);
element = null; element = null;
@ -6688,7 +6677,7 @@ this.setSvgString = function(xmlString) {
if (extensions["Arrows"]) call("setarrownonce", nonce) ; 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 // change image href vals if possible
content.find('image').each(function() { content.find('image').each(function() {
@ -6965,7 +6954,7 @@ this.importSvgString = function(xmlString) {
// Function: identifyLayers // Function: identifyLayers
// Updates layer system // Updates layer system
var identifyLayers = canvas.identifyLayers = function() { var identifyLayers = canvas.identifyLayers = function() {
current_doc.all_layers = []; current_drawing.all_layers = [];
leaveContext(); leaveContext();
var numchildren = svgcontent.childNodes.length; var numchildren = svgcontent.childNodes.length;
// loop through all children of svgcontent // loop through all children of svgcontent
@ -6987,7 +6976,7 @@ var identifyLayers = canvas.identifyLayers = function() {
// store layer and name in global variable // store layer and name in global variable
if (name) { if (name) {
layernames.push(name); layernames.push(name);
current_doc.all_layers.push( [name,child] ); current_drawing.all_layers.push( [name,child] );
current_layer = child; current_layer = child;
svgedit.utilities.walkTree(child, function(e){e.setAttribute("style", "pointer-events:inherit");}); svgedit.utilities.walkTree(child, function(e){e.setAttribute("style", "pointer-events:inherit");});
current_layer.setAttribute("style", "pointer-events:none"); current_layer.setAttribute("style", "pointer-events:none");
@ -7018,7 +7007,7 @@ var identifyLayers = canvas.identifyLayers = function() {
current_layer.appendChild(orphans[j]); current_layer.appendChild(orphans[j]);
} }
current_layer = svgcontent.appendChild(current_layer); 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");}); svgedit.utilities.walkTree(current_layer, function(e){e.setAttribute("style","pointer-events:inherit");});
current_layer.setAttribute("style","pointer-events:all"); 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 // Deletes the current layer from the drawing and then clears the selection. This function
// then calls the 'changed' handler. This is an undoable action. // then calls the 'changed' handler. This is an undoable action.
this.deleteCurrentLayer = function() { 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"); var batchCmd = new BatchCommand("Delete Layer");
// actually delete from the DOM and store in our Undo History // actually delete from the DOM and store in our Undo History
var parent = current_layer.parentNode; var parent = current_layer.parentNode;
@ -7090,7 +7079,7 @@ this.deleteCurrentLayer = function() {
addCommandToHistory(batchCmd); addCommandToHistory(batchCmd);
clearSelection(); clearSelection();
identifyLayers(); 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]); call("changed", [svgcontent]);
return true; return true;
} }
@ -7100,20 +7089,13 @@ this.deleteCurrentLayer = function() {
// Function: hasLayer // Function: hasLayer
// Check if layer with given name already exists // Check if layer with given name already exists
this.hasLayer = function(name) { this.hasLayer = function(name) {
for(var i = 0; i < current_doc.all_layers.length; i++) { for(var i = 0; i < current_drawing.all_layers.length; i++) {
if(current_doc.all_layers[i][0] == name) return true; if(current_drawing.all_layers[i][0] == name) return true;
} }
return false; return false;
}; };
// Function: getNumLayers this.getNumLayers = function() { return current_drawing.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;
};
// Function: getLayer // Function: getLayer
// Returns the name of the ith layer. If the index is out of range, an empty string is returned. // 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: // Returns:
// The name of the ith layer // The name of the ith layer
this.getLayer = function(i) { this.getLayer = function(i) {
if (i >= 0 && i < canvas.getNumLayers()) { if (i >= 0 && i < current_drawing.getNumLayers()) {
return current_doc.all_layers[i][0]; return current_drawing.all_layers[i][0];
} }
return ""; return "";
}; };
@ -7137,9 +7119,9 @@ this.getLayer = function(i) {
// Returns: // Returns:
// The name of the currently active layer. // The name of the currently active layer.
this.getCurrentLayer = function() { this.getCurrentLayer = function() {
for (var i = 0; i < current_doc.all_layers.length; ++i) { for (var i = 0; i < current_drawing.getNumLayers(); ++i) {
if (current_doc.all_layers[i][1] == current_layer) { if (current_drawing.all_layers[i][1] == current_layer) {
return current_doc.all_layers[i][0]; return current_drawing.all_layers[i][0];
} }
} }
return ""; return "";
@ -7156,12 +7138,12 @@ this.getCurrentLayer = function() {
// true if the current layer was switched, otherwise false // true if the current layer was switched, otherwise false
this.setCurrentLayer = function(name) { this.setCurrentLayer = function(name) {
name = svgedit.utilities.toXml(name); name = svgedit.utilities.toXml(name);
for (var i = 0; i < current_doc.all_layers.length; ++i) { for (var i = 0; i < current_drawing.all_layers.length; ++i) {
if (name == current_doc.all_layers[i][0]) { if (name == current_drawing.all_layers[i][0]) {
if (current_layer != current_doc.all_layers[i][1]) { if (current_layer != current_drawing.all_layers[i][1]) {
clearSelection(); clearSelection();
current_layer.setAttribute("style", "pointer-events:none"); 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"); current_layer.setAttribute("style", "pointer-events:all");
} }
return true; return true;
@ -7187,11 +7169,11 @@ this.renameCurrentLayer = function(newname) {
if (!canvas.setCurrentLayer(newname)) { if (!canvas.setCurrentLayer(newname)) {
var batchCmd = new BatchCommand("Rename Layer"); var batchCmd = new BatchCommand("Rename Layer");
// find the index of the layer // find the index of the layer
for (var i = 0; i < current_doc.all_layers.length; ++i) { for (var i = 0; i < current_drawing.all_layers.length; ++i) {
if (current_doc.all_layers[i][1] == oldLayer) break; if (current_drawing.all_layers[i][1] == oldLayer) break;
} }
var oldname = current_doc.all_layers[i][0]; var oldname = current_drawing.all_layers[i][0];
current_doc.all_layers[i][0] = svgedit.utilities.toXml(newname); current_drawing.all_layers[i][0] = svgedit.utilities.toXml(newname);
// now change the underlying title element contents // now change the underlying title element contents
var len = oldLayer.childNodes.length; var len = oldLayer.childNodes.length;
@ -7227,31 +7209,31 @@ this.renameCurrentLayer = function(newname) {
// Returns: // Returns:
// true if the current layer position was changed, false otherwise. // true if the current layer position was changed, false otherwise.
this.setCurrentLayerPosition = function(newpos) { this.setCurrentLayerPosition = function(newpos) {
if (current_layer && newpos >= 0 && newpos < current_doc.all_layers.length) { if (current_layer && newpos >= 0 && newpos < current_drawing.all_layers.length) {
for (var oldpos = 0; oldpos < current_doc.all_layers.length; ++oldpos) { for (var oldpos = 0; oldpos < current_drawing.all_layers.length; ++oldpos) {
if (current_doc.all_layers[oldpos][1] == current_layer) break; if (current_drawing.all_layers[oldpos][1] == current_layer) break;
} }
// some unknown error condition (current_layer not in all_layers) // 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 (oldpos != newpos) {
// if our new position is below us, we need to insert before the node after newpos // if our new position is below us, we need to insert before the node after newpos
var refLayer = null; var refLayer = null;
var oldNextSibling = current_layer.nextSibling; var oldNextSibling = current_layer.nextSibling;
if (newpos > oldpos ) { if (newpos > oldpos ) {
if (newpos < current_doc.all_layers.length-1) { if (newpos < current_drawing.all_layers.length-1) {
refLayer = current_doc.all_layers[newpos+1][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 // if our new position is above us, we need to insert before the node at newpos
else { else {
refLayer = current_doc.all_layers[newpos][1]; refLayer = current_drawing.all_layers[newpos][1];
} }
svgcontent.insertBefore(current_layer, refLayer); svgcontent.insertBefore(current_layer, refLayer);
addCommandToHistory(new MoveElementCommand(current_layer, oldNextSibling, svgcontent)); addCommandToHistory(new MoveElementCommand(current_layer, oldNextSibling, svgcontent));
identifyLayers(); identifyLayers();
canvas.setCurrentLayer(current_doc.all_layers[newpos][0]); canvas.setCurrentLayer(current_drawing.all_layers[newpos][0]);
return true; return true;
} }
@ -7272,9 +7254,9 @@ this.setCurrentLayerPosition = function(newpos) {
this.getLayerVisibility = function(layername) { this.getLayerVisibility = function(layername) {
// find the layer // find the layer
var layer = null; var layer = null;
for (var i = 0; i < current_doc.all_layers.length; ++i) { for (var i = 0; i < current_drawing.all_layers.length; ++i) {
if (current_doc.all_layers[i][0] == layername) { if (current_drawing.all_layers[i][0] == layername) {
layer = current_doc.all_layers[i][1]; layer = current_drawing.all_layers[i][1];
break; break;
} }
} }
@ -7295,9 +7277,9 @@ this.getLayerVisibility = function(layername) {
this.setLayerVisibility = function(layername, bVisible) { this.setLayerVisibility = function(layername, bVisible) {
// find the layer // find the layer
var layer = null; var layer = null;
for (var i = 0; i < current_doc.all_layers.length; ++i) { for (var i = 0; i < current_drawing.all_layers.length; ++i) {
if (current_doc.all_layers[i][0] == layername) { if (current_drawing.all_layers[i][0] == layername) {
layer = current_doc.all_layers[i][1]; layer = current_drawing.all_layers[i][1];
break; break;
} }
} }
@ -7329,9 +7311,9 @@ this.setLayerVisibility = function(layername, bVisible) {
this.moveSelectedToLayer = function(layername) { this.moveSelectedToLayer = function(layername) {
// find the layer // find the layer
var layer = null; var layer = null;
for (var i = 0; i < current_doc.all_layers.length; ++i) { for (var i = 0; i < current_drawing.all_layers.length; ++i) {
if (current_doc.all_layers[i][0] == layername) { if (current_drawing.all_layers[i][0] == layername) {
layer = current_doc.all_layers[i][1]; layer = current_drawing.all_layers[i][1];
break; break;
} }
} }
@ -7397,7 +7379,7 @@ this.mergeLayer = function(skipHistory) {
this.mergeAllLayers = function() { this.mergeAllLayers = function() {
var batchCmd = new BatchCommand("Merge all Layers"); 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) { while($(svgcontent).children('g').length > 1) {
batchCmd.addSubCommand(canvas.mergeLayer(true)); 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 // 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 // if layername is not a valid layer
this.getLayerOpacity = function(layername) { this.getLayerOpacity = function(layername) {
for (var i = 0; i < current_doc.all_layers.length; ++i) { for (var i = 0; i < current_drawing.all_layers.length; ++i) {
if (current_doc.all_layers[i][0] == layername) { if (current_drawing.all_layers[i][0] == layername) {
var g = current_doc.all_layers[i][1]; var g = current_drawing.all_layers[i][1];
var opacity = g.getAttribute("opacity"); var opacity = g.getAttribute("opacity");
if (!opacity) { if (!opacity) {
opacity = "1.0"; opacity = "1.0";
@ -7444,9 +7426,9 @@ this.getLayerOpacity = function(layername) {
// opacity - a float value in the range 0.0-1.0 // opacity - a float value in the range 0.0-1.0
this.setLayerOpacity = function(layername, opacity) { this.setLayerOpacity = function(layername, opacity) {
if (opacity < 0.0 || opacity > 1.0) return; if (opacity < 0.0 || opacity > 1.0) return;
for (var i = 0; i < current_doc.all_layers.length; ++i) { for (var i = 0; i < current_drawing.all_layers.length; ++i) {
if (current_doc.all_layers[i][0] == layername) { if (current_drawing.all_layers[i][0] == layername) {
var g = current_doc.all_layers[i][1]; var g = current_drawing.all_layers[i][1];
g.setAttribute("opacity", opacity); g.setAttribute("opacity", opacity);
break; break;
} }
@ -7522,7 +7504,7 @@ this.clear = function() {
} }
} }
// create empty first layer // create empty first layer
current_doc.all_layers = []; current_drawing.all_layers = [];
canvas.createLayer("Layer 1"); canvas.createLayer("Layer 1");

View File

@ -11,7 +11,7 @@
<iframe src='svgutils_test.html' width='100%' height='300'></iframe> <iframe src='svgutils_test.html' width='100%' height='300'></iframe>
<iframe src='history_test.html' width='100%' height='300'></iframe> <iframe src='history_test.html' width='100%' height='300'></iframe>
<iframe src='select_test.html' width='100%' height='300'></iframe> <iframe src='select_test.html' width='100%' height='300'></iframe>
<iframe src='document_test.html' width='100%' height='300'></iframe> <iframe src='draw_test.html' width='100%' height='300'></iframe>
</body> </body>
<script> <script>
window.setTimeout(function() { window.setTimeout(function() {

View File

@ -3,7 +3,7 @@
<head> <head>
<link rel='stylesheet' href='qunit/qunit.css' type='text/css'/> <link rel='stylesheet' href='qunit/qunit.css' type='text/css'/>
<script src='../editor/jquery.js'></script> <script src='../editor/jquery.js'></script>
<script type='text/javascript' src='../editor/document.js'></script> <script type='text/javascript' src='../editor/draw.js'></script>
<script type='text/javascript' src='qunit/qunit.js'></script> <script type='text/javascript' src='qunit/qunit.js'></script>
<script type='text/javascript'> <script type='text/javascript'>
$(function() { $(function() {
@ -26,28 +26,28 @@
svg_n.setAttributeNS(XMLNSNS, 'xmlns:se', SENS); svg_n.setAttributeNS(XMLNSNS, 'xmlns:se', SENS);
svg_n.setAttributeNS(SENS, 'se:nonce', NONCE); svg_n.setAttributeNS(SENS, 'se:nonce', NONCE);
test('Test document module', function() { test('Test draw module', function() {
expect(4); expect(4);
ok(svgedit.document); ok(svgedit.draw);
equals(typeof svgedit.document, typeof {}); equals(typeof svgedit.draw, typeof {});
ok(svgedit.document.Document); ok(svgedit.draw.Drawing);
equals(typeof svgedit.document.Document, typeof function(){}); equals(typeof svgedit.draw.Drawing, typeof function(){});
}); });
test('Test document creation', function() { test('Test document creation', function() {
expect(3); expect(3);
try { try {
var doc = new svgedit.document.Document(); var doc = new svgedit.draw.Drawing();
ok(false, 'Created document without a valid <svg> element'); ok(false, 'Created drawing without a valid <svg> element');
} catch(e) { } catch(e) {
ok(true); ok(true);
} }
try { try {
var doc = new svgedit.document.Document(svg); var doc = new svgedit.draw.Drawing(svg);
ok(doc); ok(doc);
equals(typeof doc, typeof {}); equals(typeof doc, typeof {});
} catch(e) { } catch(e) {
@ -58,10 +58,10 @@
test('Test nonce', function() { test('Test nonce', function() {
expect(2); expect(2);
var doc = new svgedit.document.Document(svg); var doc = new svgedit.draw.Drawing(svg);
equals(doc.getNonce(), ""); equals(doc.getNonce(), "");
doc = new svgedit.document.Document(svg_n); doc = new svgedit.draw.Drawing(svg_n);
equals(doc.getNonce(), NONCE); equals(doc.getNonce(), NONCE);
}); });
@ -72,7 +72,7 @@
elem2.id = 'svg_2'; elem2.id = 'svg_2';
svg.appendChild(elem2); svg.appendChild(elem2);
var doc = new svgedit.document.Document(svg); var doc = new svgedit.draw.Drawing(svg);
equals(doc.getId(), "svg_0"); equals(doc.getId(), "svg_0");
@ -93,7 +93,7 @@
expect(7); expect(7);
var prefix = 'Bar-'; var prefix = 'Bar-';
var doc = new svgedit.document.Document(svg, prefix); var doc = new svgedit.draw.Drawing(svg, prefix);
equals(doc.getId(), prefix+"0"); equals(doc.getId(), prefix+"0");
@ -118,7 +118,7 @@
elem2.id = prefix+'_2'; elem2.id = prefix+'_2';
svg_n.appendChild(elem2); svg_n.appendChild(elem2);
var doc = new svgedit.document.Document(svg_n); var doc = new svgedit.draw.Drawing(svg_n);
equals(doc.getId(), prefix+"_0"); equals(doc.getId(), prefix+"_0");
@ -137,7 +137,7 @@
expect(7); expect(7);
var PREFIX = 'Bar-'; var PREFIX = 'Bar-';
var doc = new svgedit.document.Document(svg_n, PREFIX); var doc = new svgedit.draw.Drawing(svg_n, PREFIX);
var prefix = PREFIX + NONCE + "_"; var prefix = PREFIX + NONCE + "_";
equals(doc.getId(), prefix+"0"); equals(doc.getId(), prefix+"0");
@ -157,7 +157,7 @@
test('Test releaseId()', function() { test('Test releaseId()', function() {
expect(6); expect(6);
var doc = new svgedit.document.Document(svg); var doc = new svgedit.draw.Drawing(svg);
var firstId = doc.getNextId(); var firstId = doc.getNextId();
var secondId = doc.getNextId(); var secondId = doc.getNextId();
@ -171,11 +171,18 @@
ok(doc.releaseId(firstId)); ok(doc.releaseId(firstId));
ok(!doc.releaseId(firstId)); ok(!doc.releaseId(firstId));
}); });
test('Test getNumLayers', function() {
expect(2);
var doc = new svgedit.draw.Drawing(svg);
equals(typeof doc.getNumLayers, typeof function() {});
equals(doc.getNumLayers(), 0);
});
}); });
</script> </script>
</head> </head>
<body> <body>
<h1 id='qunit-header'>Unit Tests for document.js</h1> <h1 id='qunit-header'>Unit Tests for draw.js</h1>
<h2 id='qunit-banner'></h2> <h2 id='qunit-banner'></h2>
<h2 id='qunit-userAgent'></h2> <h2 id='qunit-userAgent'></h2>
<ol id='qunit-tests'> <ol id='qunit-tests'>