diff --git a/editor/draw.js b/editor/draw.js index 06391c49..6bfa31f4 100644 --- a/editor/draw.js +++ b/editor/draw.js @@ -385,6 +385,37 @@ svgedit.draw.Drawing.prototype.getLayerVisibility = function(layername) { return (layer.getAttribute('display') != 'none'); }; +// Function: svgedit.draw.Drawing.setLayerVisibility +// Sets the visibility of the layer. If the layer name is not valid, this function return +// false, otherwise it returns true. This is an undo-able action. +// +// Parameters: +// layername - the name of the layer to change the visibility +// bVisible - true/false, whether the layer should be visible +// +// Returns: +// The SVGGElement representing the layer if the layername was valid, otherwise null. +svgedit.draw.Drawing.prototype.setLayerVisibility = function(layername, bVisible) { + if (typeof bVisible != typeof true) { + return null; + } + // find the layer + var layer = null; + for (var i = 0; i < this.getNumLayers(); ++i) { + if (this.getLayerName(i) == layername) { + layer = this.all_layers[i][1]; + break; + } + } + if (!layer) return null; + + var oldDisplay = layer.getAttribute("display"); + if (!oldDisplay) oldDisplay = "inline"; + layer.setAttribute("display", bVisible ? "inline" : "none"); + return layer; +}; + + // Function: svgedit.draw.Drawing.getLayerOpacity // Returns the opacity of the given layer. If the input name is not a layer, null is returned. // @@ -408,4 +439,24 @@ svgedit.draw.Drawing.prototype.getLayerOpacity = function(layername) { return null; }; +// Function: svgedit.draw.Drawing.setLayerOpacity +// Sets the opacity of the given layer. If the input name is not a layer, nothing happens. +// If opacity is not a value between 0.0 and 1.0, then nothing happens. +// +// Parameters: +// layername - name of the layer on which to set the opacity +// opacity - a float value in the range 0.0-1.0 +svgedit.draw.Drawing.prototype.setLayerOpacity = function(layername, opacity) { + if (typeof opacity != typeof 1.0 || opacity < 0.0 || opacity > 1.0) { + return; + } + for (var i = 0; i < this.getNumLayers(); ++i) { + if (this.getLayerName(i) == layername) { + var g = this.all_layers[i][1]; + g.setAttribute("opacity", opacity); + break; + } + } +}; + })(); diff --git a/editor/embedapi.js b/editor/embedapi.js index fbf26914..30213617 100644 --- a/editor/embedapi.js +++ b/editor/embedapi.js @@ -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 //for(var i=0,q=[],f = document.querySelectorAll("div.CFunction h3.CTitle a");i"; - if (svgCanvas.getLayerVisibility(name)) { + if (svgCanvas.getCurrentDrawing().getLayerVisibility(name)) { appendstr += "" + name + ""; } else { diff --git a/editor/svgcanvas.js b/editor/svgcanvas.js index d73e7084..6edfd026 100644 --- a/editor/svgcanvas.js +++ b/editor/svgcanvas.js @@ -7151,19 +7151,6 @@ this.setCurrentLayerPosition = function(newpos) { return false; }; -// Function: getLayerVisibility -// Returns whether the layer is visible. If the layer name is not valid, then this function -// returns false. -// -// Parameters: -// layername - the name of the layer which you want to query. -// -// Returns: -// The visibility state of the layer, or false if the layer name was invalid. -this.getLayerVisibility = function(layername) { - return getCurrentDrawing().getLayerVisibility(layername); -}; - // Function: setLayerVisibility // Sets the visibility of the layer. If the layer name is not valid, this function return // false, otherwise it returns true. This is an undo-able action. @@ -7175,27 +7162,21 @@ this.getLayerVisibility = function(layername) { // Returns: // true if the layer's visibility was set, false otherwise this.setLayerVisibility = function(layername, bVisible) { - // find the layer - var layer = null; - for (var i = 0; i < current_drawing.getNumLayers(); ++i) { - if (current_drawing.getLayerName(i) == layername) { - layer = current_drawing.all_layers[i][1]; - break; - } + var drawing = getCurrentDrawing(); + var prevVisibility = drawing.getLayerVisibility(layername); + var layer = drawing.setLayerVisibility(layername, bVisible); + if (layer) { + var oldDisplay = prevVisibility ? 'inline' : 'none'; + addCommandToHistory(new ChangeElementCommand(layer, {'display':oldDisplay}, 'Layer Visibility')); + } else { + return false; } - if (!layer) return false; - var oldDisplay = layer.getAttribute("display"); - if (!oldDisplay) oldDisplay = "inline"; - layer.setAttribute("display", bVisible ? "inline" : "none"); - addCommandToHistory(new ChangeElementCommand(layer, {"display":oldDisplay}, "Layer Visibility")); - - if (layer == current_drawing.current_layer) { + if (layer == drawing.getCurrentLayer()) { clearSelection(); pathActions.clear(); } -// call("changed", [selected]); - +// call("changed", [selected]); return true; }; @@ -7290,40 +7271,6 @@ this.mergeAllLayers = function() { addCommandToHistory(batchCmd); } -// Function: getLayerOpacity -// Returns the opacity of the given layer. If the input name is not a layer, null is returned. -// -// Parameters: -// layername - name of the layer on which to get the opacity -// -// Returns: -// 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) { - return getCurrentDrawing().getLayerOpacity(layername); -}; - -// Function: setLayerOpacity -// Sets the opacity of the given layer. If the input name is not a layer, nothing happens. -// This is not an undo-able action. NOTE: this function exists solely to apply -// a highlighting/de-emphasis effect to a layer, when it is possible for a user to affect -// the opacity of a layer, we will need to allow this function to produce an undo-able action. -// If opacity is not a value between 0.0 and 1.0, then nothing happens. -// -// Parameters: -// layername - name of the layer on which to set the opacity -// 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_drawing.getNumLayers(); ++i) { - if (current_drawing.getLayerName(i) == layername) { - var g = current_drawing.all_layers[i][1]; - g.setAttribute("opacity", opacity); - break; - } - } -}; - // Function: leaveContext // Return from a group context to the regular kind, make any previously // disabled elements enabled again diff --git a/test/draw_test.html b/test/draw_test.html index 946db005..66266674 100644 --- a/test/draw_test.html +++ b/test/draw_test.html @@ -393,6 +393,28 @@ ok(drawing.getLayerVisibility(LAYER3)); }); + test('Test setLayerVisibility()', function() { + expect(6); + + var drawing = new svgedit.draw.Drawing(svg); + setupSvgWith3Layers(svg); + drawing.identifyLayers(); + + ok(drawing.setLayerVisibility); + equals(typeof drawing.setLayerVisibility, typeof function(){}); + + drawing.setLayerVisibility(LAYER3, false); + drawing.setLayerVisibility(LAYER2, true); + drawing.setLayerVisibility(LAYER1, false); + + ok(!drawing.getLayerVisibility(LAYER1)); + ok(drawing.getLayerVisibility(LAYER2)); + ok(!drawing.getLayerVisibility(LAYER3)); + + drawing.setLayerVisibility(LAYER3, 'test-string'); + ok(!drawing.getLayerVisibility(LAYER3)); + }); + test('Test getLayerOpacity()', function() { expect(5); @@ -407,6 +429,29 @@ ok(drawing.getLayerOpacity(LAYER3) == 1.0); }); + test('Test setLayerOpacity()', function() { + expect(6); + + var drawing = new svgedit.draw.Drawing(svg); + setupSvgWith3Layers(svg); + drawing.identifyLayers(); + + ok(drawing.setLayerOpacity); + equals(typeof drawing.setLayerOpacity, typeof function(){}); + + drawing.setLayerOpacity(LAYER1, 0.4); + drawing.setLayerOpacity(LAYER2, 'invalid-string'); + drawing.setLayerOpacity(LAYER3, -1.4); + + ok(drawing.getLayerOpacity(LAYER1) == 0.4); + QUnit.log('layer2 opacity', drawing.getLayerOpacity(LAYER2)); + ok(drawing.getLayerOpacity(LAYER2) == 1.0); + ok(drawing.getLayerOpacity(LAYER3) == 1.0); + + drawing.setLayerOpacity(LAYER3, 100); + ok(drawing.getLayerOpacity(LAYER3) == 1.0); + }); + });