Issue 73: Can now move layers up and down

git-svn-id: http://svg-edit.googlecode.com/svn/trunk@674 eee81c28-f429-11dd-99c0-75d572ba1ddd
master
Jeff Schiller 2009-09-22 04:44:56 +00:00
parent 942aa40c9f
commit 5b4c8aa01a
3 changed files with 92 additions and 5 deletions

View File

@ -110,8 +110,8 @@ body {
} }
#svg_editor .layer_buttonpressed { #svg_editor .layer_buttonpressed {
width: 15px; width: 14px;
height: 15px; height: 14px;
padding: 1px; padding: 1px;
border-right: 1px solid #FFFFFF; border-right: 1px solid #FFFFFF;
border-bottom: 1px solid #FFFFFF; border-bottom: 1px solid #FFFFFF;

View File

@ -1086,6 +1086,7 @@ function svg_edit_setup() {
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); }
var newName = prompt("Please enter a unique layer name",""); var newName = prompt("Please enter a unique layer name","");
if (!newName) return;
if (jQuery.inArray(newName, curNames) != -1) { if (jQuery.inArray(newName, curNames) != -1) {
alert("There is already a layer named that!"); alert("There is already a layer named that!");
return; return;
@ -1108,9 +1109,35 @@ function svg_edit_setup() {
} }
}); });
$('#layer_up').click(function() {
// find index position of selected option
var curIndex = $('#layerlist option:selected').prevAll().length;
if (curIndex > 0) {
curIndex--;
svgCanvas.setCurrentLayerPosition(curIndex);
populateLayers();
$('#layerlist option').removeAttr("selected");
$('#layerlist option:eq('+curIndex+')').attr("selected", "selected");
}
});
$('#layer_down').click(function() {
// find index position of selected option
var curIndex = $('#layerlist option:selected').prevAll().length;
var total = $('#layerlist option').length;
if (curIndex < total-1) {
curIndex++;
svgCanvas.setCurrentLayerPosition(curIndex);
populateLayers();
$('#layerlist option').removeAttr("selected");
$('#layerlist option:eq('+curIndex+')').attr("selected", "selected");
}
});
$('#layer_rename').click(function() { $('#layer_rename').click(function() {
var oldName = $('#layerlist option:selected').attr("value"); var oldName = $('#layerlist option:selected').attr("value");
var newName = prompt("Please enter the new layer name",""); var newName = prompt("Please enter the new layer name","");
if (!newName) return;
if (oldName == newName) { if (oldName == newName) {
alert("Layer already has that name"); alert("Layer already has that name");
return; return;

View File

@ -1,9 +1,10 @@
/* /*
Issue 73 (Layers) TODO: Issue 73 (Layers) TODO:
- ensure that the current_layer is ALWAYS selected
- create API for SvgCanvas that allows the client to: - create API for SvgCanvas that allows the client to:
- change layer order - change layer order
- determine how to toggle visibility of layers (UI-wise)
- hide the pointer-events stuff from the serialized SVG source somehow
- create a mouseover region on the sidepanels that is resizable and affects all children within - create a mouseover region on the sidepanels that is resizable and affects all children within
- default the side panel to closed - default the side panel to closed
- add a button that opens the side panel? - add a button that opens the side panel?
@ -756,7 +757,6 @@ function BatchCommand(text) {
}; };
var call = function(event, arg) { var call = function(event, arg) {
console.log(events[event]);
if (events[event]) { if (events[event]) {
return events[event](this,arg); return events[event](this,arg);
} }
@ -2626,7 +2626,6 @@ function BatchCommand(text) {
}; };
// Layer API Functions // Layer API Functions
// TODO: change layer order
this.identifyLayers = function() { this.identifyLayers = function() {
all_layers = []; all_layers = [];
@ -2696,6 +2695,7 @@ function BatchCommand(text) {
} }
return ""; return "";
}; };
this.getCurrentLayer = function() { this.getCurrentLayer = function() {
for (var i = 0; i < all_layers.length; ++i) { for (var i = 0; i < all_layers.length; ++i) {
if (all_layers[i][1] == current_layer) { if (all_layers[i][1] == current_layer) {
@ -2704,6 +2704,7 @@ function BatchCommand(text) {
} }
return ""; return "";
}; };
this.setCurrentLayer = function(name) { this.setCurrentLayer = function(name) {
for (var i = 0; i < all_layers.length; ++i) { for (var i = 0; i < all_layers.length; ++i) {
if (name == all_layers[i][0]) { if (name == all_layers[i][0]) {
@ -2718,6 +2719,7 @@ function BatchCommand(text) {
} }
return false; return false;
}; };
this.renameLayer = function(oldname, newname) { this.renameLayer = function(oldname, newname) {
var rememberCurrentLayer = current_layer; var rememberCurrentLayer = current_layer;
if (canvas.setCurrentLayer(oldname)) { if (canvas.setCurrentLayer(oldname)) {
@ -2751,6 +2753,64 @@ function BatchCommand(text) {
} }
return false; return false;
}; };
this.setCurrentLayerPosition = function(newpos) {
if (current_layer && newpos >= 0 && newpos < all_layers.length) {
for (var oldpos = 0; oldpos < all_layers.length; ++oldpos) {
if (all_layers[oldpos][1] == current_layer) break;
}
// some unknown error condition (current_layer not in all_layers)
if (oldpos == all_layers.length) { return false; }
if (oldpos != newpos) {
// TODO: we could use the following loop to quickly re-establish all_layers
// but i think walking the DOM inside identifyLayers() is less bug-prone :)
/*
var new_layers = new Array(all_layers.length);
var trackIndex = 0;
for (var j = 0; j < new_layers.length; ++j) {
// new position of current_layer
if (newpos == j) {
new_layers[j] = all_layers[oldpos];
}
else {
// make sure we skip over the old position
if (trackIndex == oldpos) trackIndex++;
new_layers[j] = all_layers[trackIndex];
trackIndex++;
}
}
*/
// TODO: use insertBefore(newChild, refChild) to move the layer in the DOM
// TODO: then create a MoveElementCommand and add it to the Undo history
// if our new position is below us, we need to insert before the node after newpos
var refLayer = null;
var oldNextSibling = current_layer.nextSibling;
console.log([oldpos,newpos,all_layers.length]);
if (newpos > oldpos ) {
if (newpos < all_layers.length-1) {
refLayer = all_layers[newpos+1][1];
}
}
// if our new position is above us, we need to insert before the node at newpos
else {
refLayer = all_layers[newpos][1];
}
svgzoom.insertBefore(current_layer, refLayer);
addCommandToHistory(new MoveElementCommand(current_layer, oldNextSibling, svgzoom));
canvas.identifyLayers();
current_layer = all_layers[newpos][1];
return true;
}
}
// TODO: if i differs, then MoveElementCommand
return false;
};
// used internally // used internally
var getLayerName = function(g) { var getLayerName = function(g) {
var name = ""; var name = "";