From d85a7bc8a22f76786223e79638fc5ffb8ef0d27b Mon Sep 17 00:00:00 2001 From: Jeff Schiller Date: Sun, 30 Jan 2011 17:11:56 +0000 Subject: [PATCH] Add deleteCurrentLayer() to Drawing git-svn-id: http://svg-edit.googlecode.com/svn/trunk@1953 eee81c28-f429-11dd-99c0-75d572ba1ddd --- editor/draw.js | 18 ++++++++++++++++++ editor/embedapi.js | 20 ++++++++++++++++++-- editor/svg-editor.js | 8 ++++---- editor/svgcanvas.js | 29 +++++++++-------------------- test/draw_test.html | 32 ++++++++++++++++++++++++++++++-- 5 files changed, 79 insertions(+), 28 deletions(-) diff --git a/editor/draw.js b/editor/draw.js index 6bfa31f4..eca6e6e7 100644 --- a/editor/draw.js +++ b/editor/draw.js @@ -276,6 +276,24 @@ svgedit.draw.Drawing.prototype.setCurrentLayer = function(name) { return false; }; + +// Function: svgedit.draw.Drawing.deleteCurrentLayer +// Deletes the current layer from the drawing and then clears the selection. This function +// then calls the 'changed' handler. This is an undoable action. +// Returns: +// The SVGGElement of the layer removed or null. +svgedit.draw.Drawing.prototype.deleteCurrentLayer = function() { + if (this.current_layer && this.getNumLayers() > 1) { + // actually delete from the DOM and return it + var parent = this.current_layer.parentNode; + var nextSibling = this.current_layer.nextSibling; + var oldLayerGroup = parent.removeChild(this.current_layer); + this.identifyLayers(); + return oldLayerGroup; + } + return null; +}; + // Function: svgedit.draw.Drawing.identifyLayers // Updates layer system and sets the current layer to the // top-most layer (last child of this drawing). diff --git a/editor/embedapi.js b/editor/embedapi.js index 30213617..8debfd6d 100644 --- a/editor/embedapi.js +++ b/editor/embedapi.js @@ -66,13 +66,29 @@ 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'; + link_str = '' + svgCanvas.getCurrentDrawing().getCurrentLayerName() + ''; $(context).parentsUntil('#svgcontent > g').andSelf().each(function() { if(this.id) { @@ -1478,7 +1478,7 @@ var elem = selectedElement; // If element has just been deleted, consider it null if(elem != null && !elem.parentNode) elem = null; - var currentLayerName = svgCanvas.getCurrentLayerName(); + var currentLayerName = svgCanvas.getCurrentDrawing().getCurrentLayerName(); var currentMode = svgCanvas.getMode(); var unit = curConfig.baseUnit !== 'px' ? curConfig.baseUnit : null; @@ -3599,7 +3599,7 @@ } function cloneLayer() { - var name = svgCanvas.getCurrentLayerName() + ' copy'; + var name = svgCanvas.getCurrentDrawing().getCurrentLayerName() + ' copy'; $.prompt(uiStrings.notification.enterUniqueLayerName, name, function(newName) { if (!newName) return; @@ -3748,7 +3748,7 @@ var selLayerNames = $('#selLayerNames'); layerlist.empty(); selLayerNames.empty(); - var currentLayerName = svgCanvas.getCurrentLayerName(); + var currentLayerName = svgCanvas.getCurrentDrawing().getCurrentLayerName(); 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) diff --git a/editor/svgcanvas.js b/editor/svgcanvas.js index 0aa688a3..c9207311 100644 --- a/editor/svgcanvas.js +++ b/editor/svgcanvas.js @@ -7020,33 +7020,22 @@ 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_drawing.current_layer && current_drawing.getNumLayers() > 1) { + var current_layer = current_drawing.getCurrentLayer(); + var nextSibling = current_layer.nextSibling; + var parent = current_layer.parentNode; + current_layer = current_drawing.deleteCurrentLayer(); + if (current_layer) { var batchCmd = new BatchCommand("Delete Layer"); - // actually delete from the DOM and store in our Undo History - var parent = current_drawing.current_layer.parentNode; - var nextSibling = current_drawing.current_layer.nextSibling; - batchCmd.addSubCommand(new RemoveElementCommand(current_drawing.current_layer, nextSibling, parent)); - parent.removeChild(current_drawing.current_layer); + // store in our Undo History + batchCmd.addSubCommand(new RemoveElementCommand(current_layer, nextSibling, parent)); addCommandToHistory(batchCmd); clearSelection(); - identifyLayers(); - canvas.setCurrentLayer(current_drawing.getLayerName(current_drawing.getNumLayers())); - call("changed", [svgcontent]); + call("changed", [parent]); return true; } return false; }; -// Function: getCurrentLayerName -// Returns the name of the currently selected layer in the current drawing. If an error occurs, an empty string -// is returned. -// -// Returns: -// The name of the currently active layer in the current drawing. -this.getCurrentLayerName = function() { - return getCurrentDrawing().getCurrentLayerName(); -}; - // Function: setCurrentLayer // Sets the current layer. If the name is not a valid layer name, then this function returns // false. Otherwise it returns true. This is not an undo-able action. @@ -7417,7 +7406,7 @@ this.setConfig = function(opts) { $.extend(curConfig, opts); } -// Function: getDocumentTitle +// Function: getTitle // Returns the current group/SVG's title contents this.getTitle = function(elem) { elem = elem || selectedElements[0]; diff --git a/test/draw_test.html b/test/draw_test.html index 66266674..eceecfb3 100644 --- a/test/draw_test.html +++ b/test/draw_test.html @@ -330,8 +330,8 @@ var drawing = new svgedit.draw.Drawing(svg); setupSvgWith3Layers(svg); - drawing.identifyLayers(); + ok(drawing.getCurrentLayer); equals(typeof drawing.getCurrentLayer, typeof function(){}); ok(drawing.getCurrentLayer()); @@ -343,8 +343,8 @@ var drawing = new svgedit.draw.Drawing(svg); setupSvgWith3Layers(svg); - drawing.identifyLayers(); + ok(drawing.setCurrentLayer); equals(typeof drawing.setCurrentLayer, typeof function(){}); @@ -391,6 +391,8 @@ ok(drawing.getLayerVisibility(LAYER1)); ok(drawing.getLayerVisibility(LAYER2)); ok(drawing.getLayerVisibility(LAYER3)); + + cleanupSvg(svg); }); test('Test setLayerVisibility()', function() { @@ -413,6 +415,8 @@ drawing.setLayerVisibility(LAYER3, 'test-string'); ok(!drawing.getLayerVisibility(LAYER3)); + + cleanupSvg(svg); }); test('Test getLayerOpacity()', function() { @@ -427,6 +431,8 @@ ok(drawing.getLayerOpacity(LAYER1) == 1.0); ok(drawing.getLayerOpacity(LAYER2) == 1.0); ok(drawing.getLayerOpacity(LAYER3) == 1.0); + + cleanupSvg(svg); }); test('Test setLayerOpacity()', function() { @@ -450,6 +456,28 @@ drawing.setLayerOpacity(LAYER3, 100); ok(drawing.getLayerOpacity(LAYER3) == 1.0); + + cleanupSvg(svg); + }); + + test('Test deleteCurrentLayer()', function() { + expect(6); + + var drawing = new svgedit.draw.Drawing(svg); + setupSvgWith3Layers(svg); + drawing.identifyLayers(); + + drawing.setCurrentLayer(LAYER2); + + var curLayer = drawing.getCurrentLayer(); + equals(curLayer, drawing.all_layers[1][1]); + var deletedLayer = drawing.deleteCurrentLayer(); + + equals(curLayer, deletedLayer); + equals(2, drawing.getNumLayers()); + equals(LAYER1, drawing.all_layers[0][0]); + equals(LAYER3, drawing.all_layers[1][0]); + equals(drawing.getCurrentLayer(), drawing.all_layers[1][1]); }); });