Add createLayer() to Drawing and tests

git-svn-id: http://svg-edit.googlecode.com/svn/trunk@1941 eee81c28-f429-11dd-99c0-75d572ba1ddd
master
Jeff Schiller 2011-01-28 04:37:41 +00:00
parent 3398b37947
commit 3e9f1747ff
3 changed files with 44 additions and 8 deletions

View File

@ -277,7 +277,8 @@ svgedit.draw.Drawing.prototype.setCurrentLayer = function(name) {
}; };
// Function: svgedit.draw.Drawing.identifyLayers // Function: svgedit.draw.Drawing.identifyLayers
// Updates layer system // Updates layer system and sets the current layer to the
// top-most layer (last <g> child of this drawing).
svgedit.draw.Drawing.prototype.identifyLayers = function() { svgedit.draw.Drawing.prototype.identifyLayers = function() {
this.all_layers = []; this.all_layers = [];
var numchildren = this.svgElem_.childNodes.length; var numchildren = this.svgElem_.childNodes.length;
@ -341,5 +342,25 @@ svgedit.draw.Drawing.prototype.identifyLayers = function() {
this.current_layer.setAttribute("style","pointer-events:all"); this.current_layer.setAttribute("style","pointer-events:all");
}; };
// Function: svgedit.draw.Drawing.createLayer
// Creates a new top-level layer in the drawing with the given name and
// sets the current layer to it.
//
// Parameters:
// name - The given name
//
// Returns:
// The SVGGElement of the new layer, which is also the current layer
// of this drawing.
svgedit.draw.Drawing.prototype.createLayer = function(name) {
var svgdoc = this.svgElem_.ownerDocument;
var new_layer = svgdoc.createElementNS(svg_ns, "g");
var layer_title = svgdoc.createElementNS(svg_ns, "title");
layer_title.textContent = name;
new_layer.appendChild(layer_title);
this.svgElem_.appendChild(new_layer);
this.identifyLayers();
return new_layer;
};
})(); })();

View File

@ -6974,16 +6974,10 @@ var identifyLayers = canvas.identifyLayers = function() {
// name - The given name // name - The given name
this.createLayer = function(name) { this.createLayer = function(name) {
var batchCmd = new BatchCommand("Create Layer"); var batchCmd = new BatchCommand("Create Layer");
var new_layer = svgdoc.createElementNS(svgns, "g"); var new_layer = getCurrentDrawing().createLayer(name);
var layer_title = svgdoc.createElementNS(svgns, "title");
layer_title.textContent = name;
new_layer.appendChild(layer_title);
new_layer = svgcontent.appendChild(new_layer);
batchCmd.addSubCommand(new InsertElementCommand(new_layer)); batchCmd.addSubCommand(new InsertElementCommand(new_layer));
addCommandToHistory(batchCmd); addCommandToHistory(batchCmd);
clearSelection(); clearSelection();
identifyLayers();
canvas.setCurrentLayer(name);
call("changed", [new_layer]); call("changed", [new_layer]);
}; };

View File

@ -355,6 +355,27 @@
drawing.setCurrentLayer(LAYER3); drawing.setCurrentLayer(LAYER3);
equals(drawing.getCurrentLayerName(LAYER3), LAYER3); equals(drawing.getCurrentLayerName(LAYER3), LAYER3);
equals(drawing.getCurrentLayer(), drawing.all_layers[2][1]); equals(drawing.getCurrentLayer(), drawing.all_layers[2][1]);
cleanupSvg(svg);
});
test('Test createLayer()', function() {
expect(6);
var drawing = new svgedit.draw.Drawing(svg);
setupSvgWith3Layers(svg);
ok(drawing.createLayer);
equals(typeof drawing.createLayer, typeof function(){});
var NEW_LAYER_NAME = 'Layer A';
var layer_g = drawing.createLayer(NEW_LAYER_NAME);
equals(4, drawing.getNumLayers());
equals(layer_g, drawing.getCurrentLayer());
equals(NEW_LAYER_NAME, drawing.getCurrentLayerName());
equals(NEW_LAYER_NAME, drawing.getLayerName(3));
cleanupSvg(svg);
}); });
}); });