Move getLayer() into Drawing with tests

git-svn-id: http://svg-edit.googlecode.com/svn/trunk@1928 eee81c28-f429-11dd-99c0-75d572ba1ddd
master
Jeff Schiller 2011-01-17 05:02:08 +00:00
parent 8691a37376
commit 97071abd0e
5 changed files with 108 additions and 73 deletions

View File

@ -248,4 +248,20 @@ svgedit.draw.Drawing.prototype.identifyLayers = function() {
}; };
// Function: svgedit.draw.Drawing.getLayer
// Returns the name of the ith layer. If the index is out of range, an empty string is returned.
//
// Parameters:
// i - the zero-based index of the layer you are querying.
//
// Returns:
// The name of the ith layer
svgedit.draw.Drawing.prototype.getLayer = function(i) {
if (i >= 0 && i < this.getNumLayers()) {
return this.all_layers[i][0];
}
return "";
};
})(); })();

View File

@ -66,13 +66,13 @@ 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", "getLayer", "getCurrentLayer", "setCurrentLayer", "renameCurrentLayer", "setCurrentLayerPosition", "getLayerVisibility", "setLayerVisibility", "moveSelectedToLayer", "getLayerOpacity", "setLayerOpacity", "clear"]; //var functions = ["clearSelection", "addToSelection", "removeFromSelection", "open", "save", "getSvgString", "setSvgString", "createLayer", "deleteCurrentLayer", "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
//var l=[];for(var i in svgCanvas){if(typeof svgCanvas[i] == "function"){l.push(i)}}; //var l=[];for(var i in svgCanvas){if(typeof svgCanvas[i] == "function"){l.push(i)}};
//run in svgedit itself //run in svgedit itself
var functions = ["updateElementFromJson", "embedImage", "fixOperaXML", "clearSelection", "addToSelection", "removeFromSelection", "addNodeToSelection", "open", "save", "getSvgString", "setSvgString", "createLayer", "deleteCurrentLayer", "getLayer", "getCurrentLayer", "setCurrentLayer", "renameCurrentLayer", "setCurrentLayerPosition", "getLayerVisibility", "setLayerVisibility", "moveSelectedToLayer", "getLayerOpacity", "setLayerOpacity", "clear", "clearPath", "getNodePoint", "clonePathNode", "deletePathNode", "getResolution", "getImageTitle", "setImageTitle", "setResolution", "setBBoxZoom", "setZoom", "getMode", "setMode", "getStrokeColor", "setStrokeColor", "getFillColor", "setFillColor", "setStrokePaint", "setFillPaint", "getStrokeWidth", "setStrokeWidth", "getStrokeStyle", "setStrokeStyle", "getOpacity", "setOpacity", "getFillOpacity", "setFillOpacity", "getStrokeOpacity", "setStrokeOpacity", "getTransformList", "getBBox", "getRotationAngle", "setRotationAngle", "each", "bind", "setIdPrefix", "getBold", "setBold", "getItalic", "setItalic", "getFontFamily", "setFontFamily", "getFontSize", "setFontSize", "getText", "setTextContent", "setImageURL", "setRectRadius", "setSegType", "quickClone", "changeSelectedAttributeNoUndo", "changeSelectedAttribute", "deleteSelectedElements", "groupSelectedElements", "ungroupSelectedElement", "moveToTopSelectedElement", "moveToBottomSelectedElement", "moveSelectedElements", "getStrokedBBox", "getVisibleElements", "cycleElement", "getUndoStackSize", "getRedoStackSize", "getNextUndoCommandText", "getNextRedoCommandText", "undo", "redo", "cloneSelectedElements", "alignSelectedElements", "getZoom", "getVersion", "setIconSize", "setLang", "setCustomHandlers"] var functions = ["updateElementFromJson", "embedImage", "fixOperaXML", "clearSelection", "addToSelection", "removeFromSelection", "addNodeToSelection", "open", "save", "getSvgString", "setSvgString", "createLayer", "deleteCurrentLayer", "getCurrentLayer", "setCurrentLayer", "renameCurrentLayer", "setCurrentLayerPosition", "getLayerVisibility", "setLayerVisibility", "moveSelectedToLayer", "getLayerOpacity", "setLayerOpacity", "clear", "clearPath", "getNodePoint", "clonePathNode", "deletePathNode", "getResolution", "getImageTitle", "setImageTitle", "setResolution", "setBBoxZoom", "setZoom", "getMode", "setMode", "getStrokeColor", "setStrokeColor", "getFillColor", "setFillColor", "setStrokePaint", "setFillPaint", "getStrokeWidth", "setStrokeWidth", "getStrokeStyle", "setStrokeStyle", "getOpacity", "setOpacity", "getFillOpacity", "setFillOpacity", "getStrokeOpacity", "setStrokeOpacity", "getTransformList", "getBBox", "getRotationAngle", "setRotationAngle", "each", "bind", "setIdPrefix", "getBold", "setBold", "getItalic", "setItalic", "getFontFamily", "setFontFamily", "getFontSize", "setFontSize", "getText", "setTextContent", "setImageURL", "setRectRadius", "setSegType", "quickClone", "changeSelectedAttributeNoUndo", "changeSelectedAttribute", "deleteSelectedElements", "groupSelectedElements", "ungroupSelectedElement", "moveToTopSelectedElement", "moveToBottomSelectedElement", "moveSelectedElements", "getStrokedBBox", "getVisibleElements", "cycleElement", "getUndoStackSize", "getRedoStackSize", "getNextUndoCommandText", "getNextRedoCommandText", "undo", "redo", "cloneSelectedElements", "alignSelectedElements", "getZoom", "getVersion", "setIconSize", "setLang", "setCustomHandlers"]
//TODO: rewrite the following, it's pretty scary. //TODO: rewrite the following, it's pretty scary.
for(var i = 0; i < functions.length; i++){ for(var i = 0; i < functions.length; i++){

View File

@ -3696,7 +3696,7 @@
// 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.getCurrentDrawing().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.getCurrentDrawing().getLayer(i); }
if (layerNameToHighlight) { if (layerNameToHighlight) {
for (var i = 0; i < curNames.length; ++i) { for (var i = 0; i < curNames.length; ++i) {
@ -3722,7 +3722,7 @@
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--) {
var name = svgCanvas.getLayer(layer); var name = svgCanvas.getCurrentDrawing().getLayer(layer);
// contenteditable=\"true\" // contenteditable=\"true\"
var appendstr = "<tr class=\"layer"; var appendstr = "<tr class=\"layer";
if (name == currentlayer) { if (name == currentlayer) {

View File

@ -7033,21 +7033,6 @@ this.deleteCurrentLayer = function() {
return false; return false;
}; };
// Function: getLayer
// Returns the name of the ith layer. If the index is out of range, an empty string is returned.
//
// Parameters:
// i - the zero-based index of the layer you are querying.
//
// Returns:
// The name of the ith layer
this.getLayer = function(i) {
if (i >= 0 && i < current_drawing.getNumLayers()) {
return current_drawing.all_layers[i][0];
}
return "";
};
// Function: getCurrentLayer // Function: getCurrentLayer
// Returns the name of the currently selected layer. If an error occurs, an empty string // Returns the name of the currently selected layer. If an error occurs, an empty string
// is returned. // is returned.

View File

@ -28,6 +28,30 @@
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);
var setupSvgWith3Layers = function(svgElem) {
var layer1 = document.createElementNS(SVGNS, 'g');
var layer1_title = document.createElementNS(SVGNS, 'title');
layer1_title.appendChild(document.createTextNode('Layer 1'));
layer1.appendChild(layer1_title);
svgElem.appendChild(layer1);
var layer2 = document.createElementNS(SVGNS, 'g');
var layer2_title = document.createElementNS(SVGNS, 'title');
layer2_title.appendChild(document.createTextNode('Layer 2'));
layer2.appendChild(layer2_title);
svgElem.appendChild(layer2);
var layer3 = document.createElementNS(SVGNS, 'g');
var layer3_title = document.createElementNS(SVGNS, 'title');
layer3_title.appendChild(document.createTextNode('Layer 3'));
layer3.appendChild(layer3_title);
svgElem.appendChild(layer3);
};
var cleanupSvg = function(svgElem) {
while(svgElem.firstChild) {svgElem.removeChild(svgElem.firstChild);}
};
test('Test draw module', function() { test('Test draw module', function() {
expect(4); expect(4);
@ -88,7 +112,7 @@
equals(doc.getId(), "svg_4"); equals(doc.getId(), "svg_4");
// clean out svg document // clean out svg document
while(svg.firstChild) {svg.removeChild(svg.firstChild);} cleanupSvg(svg);
}); });
test('Test getId() and getNextId() with prefix without nonce', function() { test('Test getId() and getNextId() with prefix without nonce', function() {
@ -108,7 +132,7 @@
equals(doc.getNextId(), prefix+"3"); equals(doc.getNextId(), prefix+"3");
equals(doc.getId(), prefix+"3"); equals(doc.getId(), prefix+"3");
while(svg.firstChild) {svg.removeChild(svg.firstChild);} cleanupSvg(svg);
}); });
test('Test getId() and getNextId() with nonce', function() { test('Test getId() and getNextId() with nonce', function() {
@ -132,7 +156,8 @@
equals(doc.getNextId(), prefix+"_4"); equals(doc.getNextId(), prefix+"_4");
equals(doc.getId(), prefix+"_4"); equals(doc.getId(), prefix+"_4");
while(svg_n.firstChild) {svg_n.removeChild(svg_n.firstChild);}
cleanupSvg(svg_n);
}); });
test('Test getId() and getNextId() with prefix with nonce', function() { test('Test getId() and getNextId() with prefix with nonce', function() {
@ -153,7 +178,7 @@
equals(doc.getNextId(), prefix+"3"); equals(doc.getNextId(), prefix+"3");
equals(doc.getId(), prefix+"3"); equals(doc.getId(), prefix+"3");
while(svg_n.firstChild) {svg_n.removeChild(svg_n.firstChild);} cleanupSvg(svg_n);
}); });
test('Test releaseId()', function() { test('Test releaseId()', function() {
@ -172,22 +197,39 @@
ok(!doc.releaseId("bad-id")); ok(!doc.releaseId("bad-id"));
ok(doc.releaseId(firstId)); ok(doc.releaseId(firstId));
ok(!doc.releaseId(firstId)); ok(!doc.releaseId(firstId));
cleanupSvg(svg);
}); });
test('Test getNumLayers', function() { test('Test getNumLayers', function() {
expect(2); expect(3);
var doc = new svgedit.draw.Drawing(svg); var drawing = new svgedit.draw.Drawing(svg);
equals(typeof doc.getNumLayers, typeof function() {}); equals(typeof drawing.getNumLayers, typeof function() {});
equals(doc.getNumLayers(), 0); equals(drawing.getNumLayers(), 0);
// TODO(codedread): More here once identifyLayers works.
setupSvgWith3Layers(svg);
drawing.identifyLayers();
equals(drawing.getNumLayers(), 3);
cleanupSvg(svg);
}); });
test('Test hasLayer', function() { test('Test hasLayer', function() {
expect(2); expect(5);
var doc = new svgedit.draw.Drawing(svg);
equals(typeof doc.hasLayer, typeof function() {}); setupSvgWith3Layers(svg);
ok(!doc.hasLayer('invalid-layer')); var drawing = new svgedit.draw.Drawing(svg);
// TODO(codedread): More here once identifyLayers works. drawing.identifyLayers();
equals(typeof drawing.hasLayer, typeof function() {});
ok(!drawing.hasLayer('invalid-layer'));
ok(drawing.hasLayer("Layer 3"));
ok(drawing.hasLayer("Layer 2"));
ok(drawing.hasLayer("Layer 1"));
cleanupSvg(svg);
}); });
test('Test identifyLayers() with empty document', function() { test('Test identifyLayers() with empty document', function() {
@ -213,33 +255,17 @@
var firstChild = empty_layer.childNodes.item(0); var firstChild = empty_layer.childNodes.item(0);
equals(firstChild.tagName, 'title'); equals(firstChild.tagName, 'title');
while(svg.firstChild) {svg.removeChild(svg.firstChild);} cleanupSvg(svg);
}); });
test('Test identifyLayers() with some layers', function() { test('Test identifyLayers() with some layers', function() {
expect(5); expect(5);
var layer1 = document.createElementNS(SVGNS, 'g'); var drawing = new svgedit.draw.Drawing(svg);
var layer1_title = document.createElementNS(SVGNS, 'title'); setupSvgWith3Layers(svg);
layer1_title.appendChild(document.createTextNode('Layer 1'));
layer1.appendChild(layer1_title);
var layer2 = document.createElementNS(SVGNS, 'g');
var layer2_title = document.createElementNS(SVGNS, 'title');
layer2_title.appendChild(document.createTextNode('Layer 2'));
layer2.appendChild(layer2_title);
var layer3 = document.createElementNS(SVGNS, 'g');
var layer3_title = document.createElementNS(SVGNS, 'title');
layer3_title.appendChild(document.createTextNode('Layer 3'));
layer3.appendChild(layer3_title);
svg.appendChild(layer1);
svg.appendChild(layer2);
svg.appendChild(layer3);
equals(svg.childNodes.length, 3); equals(svg.childNodes.length, 3);
var drawing = new svgedit.draw.Drawing(svg);
drawing.identifyLayers(); drawing.identifyLayers();
equals(drawing.getNumLayers(), 3); equals(drawing.getNumLayers(), 3);
@ -247,45 +273,53 @@
equals(drawing.all_layers[1][1], svg.childNodes.item(1)); equals(drawing.all_layers[1][1], svg.childNodes.item(1));
equals(drawing.all_layers[2][1], svg.childNodes.item(2)); equals(drawing.all_layers[2][1], svg.childNodes.item(2));
while(svg.firstChild) {svg.removeChild(svg.firstChild);} cleanupSvg(svg);
}); });
test('Test identifyLayers() with some layers and orphans', function() { test('Test identifyLayers() with some layers and orphans', function() {
expect(9); expect(10);
var layer1 = document.createElementNS(SVGNS, 'g'); setupSvgWith3Layers(svg);
var layer1_title = document.createElementNS(SVGNS, 'title');
layer1_title.appendChild(document.createTextNode('Layer 1'));
layer1.appendChild(layer1_title);
svg.appendChild(layer1);
var layer2 = document.createElementNS(SVGNS, 'g');
var layer2_title = document.createElementNS(SVGNS, 'title');
layer2_title.appendChild(document.createTextNode('Layer 2'));
layer2.appendChild(layer2_title);
svg.appendChild(layer2);
var orphan1 = document.createElementNS(SVGNS, 'rect'); var orphan1 = document.createElementNS(SVGNS, 'rect');
var orphan2 = document.createElementNS(SVGNS, 'rect'); var orphan2 = document.createElementNS(SVGNS, 'rect');
svg.appendChild(orphan1); svg.appendChild(orphan1);
svg.appendChild(orphan2); svg.appendChild(orphan2);
equals(svg.childNodes.length, 4); equals(svg.childNodes.length, 5);
var drawing = new svgedit.draw.Drawing(svg); var drawing = new svgedit.draw.Drawing(svg);
drawing.identifyLayers(); drawing.identifyLayers();
equals(drawing.getNumLayers(), 3); equals(drawing.getNumLayers(), 4);
equals(drawing.all_layers[0][1], svg.childNodes.item(0)); equals(drawing.all_layers[0][1], svg.childNodes.item(0));
equals(drawing.all_layers[1][1], svg.childNodes.item(1)); equals(drawing.all_layers[1][1], svg.childNodes.item(1));
equals(drawing.all_layers[2][1], svg.childNodes.item(2)); equals(drawing.all_layers[2][1], svg.childNodes.item(2));
var layer3 = drawing.all_layers[2][1]; equals(drawing.all_layers[3][1], svg.childNodes.item(3));
equals(layer3.tagName, 'g');
equals(layer3.childNodes.length, 3);
equals(layer3.childNodes.item(1), orphan1);
equals(layer3.childNodes.item(2), orphan2);
while(svg.firstChild) {svg.removeChild(svg.firstChild);} var layer4 = drawing.all_layers[3][1];
equals(layer4.tagName, 'g');
equals(layer4.childNodes.length, 3);
equals(layer4.childNodes.item(1), orphan1);
equals(layer4.childNodes.item(2), orphan2);
cleanupSvg(svg);
});
test('Test getLayer()', function() {
expect(4);
var drawing = new svgedit.draw.Drawing(svg);
setupSvgWith3Layers(svg);
drawing.identifyLayers();
equals(drawing.getNumLayers(), 3);
equals(drawing.getLayer(0), "Layer 1");
equals(drawing.getLayer(1), "Layer 2");
equals(drawing.getLayer(2), "Layer 3");
cleanupSvg(svg);
}); });
}); });