2014-01-31 02:13:37 +00:00
|
|
|
/*globals $, svgedit*/
|
2014-03-15 13:27:39 +00:00
|
|
|
/*jslint vars: true, eqeq: true, todo: true*/
|
2011-01-13 06:57:26 +00:00
|
|
|
/**
|
2011-01-16 23:07:35 +00:00
|
|
|
* Package: svgedit.draw
|
2011-01-13 06:57:26 +00:00
|
|
|
*
|
2012-09-16 18:53:27 +00:00
|
|
|
* Licensed under the MIT License
|
2011-01-13 06:57:26 +00:00
|
|
|
*
|
2011-01-16 23:07:35 +00:00
|
|
|
* Copyright(c) 2011 Jeff Schiller
|
2011-01-13 06:57:26 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
// Dependencies:
|
|
|
|
// 1) jQuery
|
2011-01-17 04:36:54 +00:00
|
|
|
// 2) browser.js
|
|
|
|
// 3) svgutils.js
|
2011-01-13 06:57:26 +00:00
|
|
|
|
2014-01-31 02:13:37 +00:00
|
|
|
(function() {'use strict';
|
2011-01-13 06:57:26 +00:00
|
|
|
|
2011-01-16 23:07:35 +00:00
|
|
|
if (!svgedit.draw) {
|
|
|
|
svgedit.draw = {};
|
2011-01-13 06:57:26 +00:00
|
|
|
}
|
2013-02-16 15:02:26 +00:00
|
|
|
// alias
|
|
|
|
var NS = svgedit.NS;
|
2011-02-01 07:22:18 +00:00
|
|
|
|
2013-02-23 16:46:02 +00:00
|
|
|
var visElems = 'a,circle,ellipse,foreignObject,g,image,line,path,polygon,polyline,rect,svg,text,tspan,use'.split(',');
|
2011-02-10 19:09:35 +00:00
|
|
|
|
2011-02-03 16:38:15 +00:00
|
|
|
var RandomizeModes = {
|
|
|
|
LET_DOCUMENT_DECIDE: 0,
|
|
|
|
ALWAYS_RANDOMIZE: 1,
|
|
|
|
NEVER_RANDOMIZE: 2
|
|
|
|
};
|
|
|
|
var randomize_ids = RandomizeModes.LET_DOCUMENT_DECIDE;
|
2011-01-13 06:57:26 +00:00
|
|
|
|
2016-05-01 00:58:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-15 13:27:39 +00:00
|
|
|
* Called to ensure that drawings will or will not have randomized ids.
|
|
|
|
* The currentDrawing will have its nonce set if it doesn't already.
|
|
|
|
* @param {boolean} enableRandomization - flag indicating if documents should have randomized ids
|
|
|
|
* @param {svgedit.draw.Drawing} currentDrawing
|
|
|
|
*/
|
2013-02-23 16:46:02 +00:00
|
|
|
svgedit.draw.randomizeIds = function(enableRandomization, currentDrawing) {
|
|
|
|
randomize_ids = enableRandomization === false ?
|
2011-02-03 16:38:15 +00:00
|
|
|
RandomizeModes.NEVER_RANDOMIZE :
|
|
|
|
RandomizeModes.ALWAYS_RANDOMIZE;
|
2011-02-03 06:45:01 +00:00
|
|
|
|
2013-02-23 16:46:02 +00:00
|
|
|
if (randomize_ids == RandomizeModes.ALWAYS_RANDOMIZE && !currentDrawing.getNonce()) {
|
|
|
|
currentDrawing.setNonce(Math.floor(Math.random() * 100001));
|
|
|
|
} else if (randomize_ids == RandomizeModes.NEVER_RANDOMIZE && currentDrawing.getNonce()) {
|
|
|
|
currentDrawing.clearNonce();
|
2011-02-03 06:45:01 +00:00
|
|
|
}
|
2011-02-01 07:22:18 +00:00
|
|
|
};
|
|
|
|
|
2011-01-13 06:57:26 +00:00
|
|
|
/**
|
2011-01-16 23:07:35 +00:00
|
|
|
* This class encapsulates the concept of a SVG-edit drawing
|
2014-03-15 13:27:39 +00:00
|
|
|
* @param {SVGSVGElement} svgElem - The SVG DOM Element that this JS object
|
2011-01-13 06:57:26 +00:00
|
|
|
* encapsulates. If the svgElem has a se:nonce attribute on it, then
|
|
|
|
* IDs will use the nonce as they are generated.
|
2014-03-15 13:27:39 +00:00
|
|
|
* @param {String=svg_} [opt_idPrefix] - The ID prefix to use.
|
2011-01-13 06:57:26 +00:00
|
|
|
*/
|
2011-01-16 23:07:35 +00:00
|
|
|
svgedit.draw.Drawing = function(svgElem, opt_idPrefix) {
|
2011-01-13 06:57:26 +00:00
|
|
|
if (!svgElem || !svgElem.tagName || !svgElem.namespaceURI ||
|
2013-02-16 15:02:26 +00:00
|
|
|
svgElem.tagName != 'svg' || svgElem.namespaceURI != NS.SVG) {
|
2011-01-16 23:07:35 +00:00
|
|
|
throw "Error: svgedit.draw.Drawing instance initialized without a <svg> element";
|
2011-01-13 06:57:26 +00:00
|
|
|
}
|
|
|
|
|
2011-01-25 16:15:29 +00:00
|
|
|
/**
|
|
|
|
* The SVG DOM Element that represents this drawing.
|
|
|
|
* @type {SVGSVGElement}
|
|
|
|
*/
|
2011-01-13 06:57:26 +00:00
|
|
|
this.svgElem_ = svgElem;
|
2011-01-25 16:15:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The latest object number used in this drawing.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2011-01-13 06:57:26 +00:00
|
|
|
this.obj_num = 0;
|
2011-01-25 16:15:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The prefix to prepend to each element id in the drawing.
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2011-01-13 06:57:26 +00:00
|
|
|
this.idPrefix = opt_idPrefix || "svg_";
|
2011-01-25 16:15:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An array of released element ids to immediately reuse.
|
|
|
|
* @type {Array.<number>}
|
|
|
|
*/
|
2011-01-14 18:18:29 +00:00
|
|
|
this.releasedNums = [];
|
2011-01-13 06:57:26 +00:00
|
|
|
|
2011-01-25 16:15:29 +00:00
|
|
|
/**
|
2016-05-01 01:46:45 +00:00
|
|
|
* The z-ordered array of Layer objects. Each layer has a name
|
|
|
|
* and group element.
|
2011-01-25 16:15:29 +00:00
|
|
|
* The first layer is the one at the bottom of the rendering.
|
2016-05-02 02:58:41 +00:00
|
|
|
* @type {Array.<Layer>}
|
2011-01-25 16:15:29 +00:00
|
|
|
*/
|
2011-01-16 16:28:26 +00:00
|
|
|
this.all_layers = [];
|
|
|
|
|
2016-05-01 00:58:19 +00:00
|
|
|
/**
|
|
|
|
* Map of all_layers by name.
|
2016-05-02 02:58:41 +00:00
|
|
|
*
|
|
|
|
* Note: Layers are ordered, but referenced externally by name; so, we need both container
|
|
|
|
* types depending on which function is called (i.e. all_layers and layer_map).
|
|
|
|
*
|
|
|
|
* @type {Object.<string, Layer>}
|
2016-05-01 00:58:19 +00:00
|
|
|
*/
|
|
|
|
this.layer_map = {};
|
|
|
|
|
2011-01-25 16:15:29 +00:00
|
|
|
/**
|
|
|
|
* The current layer being used.
|
2016-05-01 00:58:19 +00:00
|
|
|
* @type {Layer}
|
2011-01-25 16:15:29 +00:00
|
|
|
*/
|
|
|
|
this.current_layer = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The nonce to use to uniquely identify elements across drawings.
|
|
|
|
* @type {!String}
|
|
|
|
*/
|
2014-03-15 13:27:39 +00:00
|
|
|
this.nonce_ = '';
|
2013-02-16 15:02:26 +00:00
|
|
|
var n = this.svgElem_.getAttributeNS(NS.SE, 'nonce');
|
2011-02-01 07:22:18 +00:00
|
|
|
// If already set in the DOM, use the nonce throughout the document
|
|
|
|
// else, if randomizeIds(true) has been called, create and set the nonce.
|
2011-02-03 16:38:15 +00:00
|
|
|
if (!!n && randomize_ids != RandomizeModes.NEVER_RANDOMIZE) {
|
2011-02-01 07:22:18 +00:00
|
|
|
this.nonce_ = n;
|
2011-02-03 16:38:15 +00:00
|
|
|
} else if (randomize_ids == RandomizeModes.ALWAYS_RANDOMIZE) {
|
|
|
|
this.setNonce(Math.floor(Math.random() * 100001));
|
2011-02-01 07:22:18 +00:00
|
|
|
}
|
2011-01-13 06:57:26 +00:00
|
|
|
};
|
|
|
|
|
2014-03-15 13:27:39 +00:00
|
|
|
/**
|
|
|
|
* @param {string} id Element ID to retrieve
|
|
|
|
* @returns {Element} SVG element within the root SVGSVGElement
|
|
|
|
*/
|
|
|
|
svgedit.draw.Drawing.prototype.getElem_ = function (id) {
|
|
|
|
if (this.svgElem_.querySelector) {
|
2011-01-13 06:57:26 +00:00
|
|
|
// querySelector lookup
|
2014-03-15 13:27:39 +00:00
|
|
|
return this.svgElem_.querySelector('#' + id);
|
2011-01-13 06:57:26 +00:00
|
|
|
}
|
2013-02-15 16:51:48 +00:00
|
|
|
// jQuery lookup: twice as slow as xpath in FF
|
|
|
|
return $(this.svgElem_).find('[id=' + id + ']')[0];
|
2011-01-13 06:57:26 +00:00
|
|
|
};
|
|
|
|
|
2014-03-15 13:27:39 +00:00
|
|
|
/**
|
|
|
|
* @returns {SVGSVGElement}
|
|
|
|
*/
|
|
|
|
svgedit.draw.Drawing.prototype.getSvgElem = function () {
|
2011-01-13 06:57:26 +00:00
|
|
|
return this.svgElem_;
|
2011-01-25 16:15:29 +00:00
|
|
|
};
|
|
|
|
|
2014-03-15 13:27:39 +00:00
|
|
|
/**
|
|
|
|
* @returns {!string|number} The previously set nonce
|
|
|
|
*/
|
2011-01-16 23:07:35 +00:00
|
|
|
svgedit.draw.Drawing.prototype.getNonce = function() {
|
2011-01-13 06:57:26 +00:00
|
|
|
return this.nonce_;
|
|
|
|
};
|
|
|
|
|
2014-03-15 13:27:39 +00:00
|
|
|
/**
|
|
|
|
* @param {!string|number} n The nonce to set
|
|
|
|
*/
|
2011-02-03 06:45:01 +00:00
|
|
|
svgedit.draw.Drawing.prototype.setNonce = function(n) {
|
2013-02-16 15:02:26 +00:00
|
|
|
this.svgElem_.setAttributeNS(NS.XMLNS, 'xmlns:se', NS.SE);
|
|
|
|
this.svgElem_.setAttributeNS(NS.SE, 'se:nonce', n);
|
2011-02-03 06:45:01 +00:00
|
|
|
this.nonce_ = n;
|
|
|
|
};
|
|
|
|
|
2014-03-15 13:27:39 +00:00
|
|
|
/**
|
|
|
|
* Clears any previously set nonce
|
|
|
|
*/
|
|
|
|
svgedit.draw.Drawing.prototype.clearNonce = function () {
|
2011-02-03 16:38:15 +00:00
|
|
|
// We deliberately leave any se:nonce attributes alone,
|
|
|
|
// we just don't use it to randomize ids.
|
2014-03-15 13:27:39 +00:00
|
|
|
this.nonce_ = '';
|
2011-02-03 16:38:15 +00:00
|
|
|
};
|
|
|
|
|
2011-01-13 06:57:26 +00:00
|
|
|
/**
|
|
|
|
* Returns the latest object id as a string.
|
|
|
|
* @return {String} The latest object Id.
|
|
|
|
*/
|
2014-03-15 13:27:39 +00:00
|
|
|
svgedit.draw.Drawing.prototype.getId = function () {
|
2011-01-13 06:57:26 +00:00
|
|
|
return this.nonce_ ?
|
2013-02-15 16:51:48 +00:00
|
|
|
this.idPrefix + this.nonce_ + '_' + this.obj_num :
|
2014-01-31 02:13:37 +00:00
|
|
|
this.idPrefix + this.obj_num;
|
2011-01-13 06:57:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the next object Id as a string.
|
|
|
|
* @return {String} The next object Id to use.
|
|
|
|
*/
|
2014-03-15 13:27:39 +00:00
|
|
|
svgedit.draw.Drawing.prototype.getNextId = function () {
|
2011-01-14 18:18:29 +00:00
|
|
|
var oldObjNum = this.obj_num;
|
|
|
|
var restoreOldObjNum = false;
|
|
|
|
|
|
|
|
// If there are any released numbers in the release stack,
|
|
|
|
// use the last one instead of the next obj_num.
|
|
|
|
// We need to temporarily use obj_num as that is what getId() depends on.
|
|
|
|
if (this.releasedNums.length > 0) {
|
|
|
|
this.obj_num = this.releasedNums.pop();
|
|
|
|
restoreOldObjNum = true;
|
|
|
|
} else {
|
|
|
|
// If we are not using a released id, then increment the obj_num.
|
|
|
|
this.obj_num++;
|
|
|
|
}
|
2011-01-13 06:57:26 +00:00
|
|
|
|
2011-01-14 18:18:29 +00:00
|
|
|
// Ensure the ID does not exist.
|
2011-01-13 06:57:26 +00:00
|
|
|
var id = this.getId();
|
|
|
|
while (this.getElem_(id)) {
|
2011-01-14 18:18:29 +00:00
|
|
|
if (restoreOldObjNum) {
|
|
|
|
this.obj_num = oldObjNum;
|
|
|
|
restoreOldObjNum = false;
|
|
|
|
}
|
2011-01-13 06:57:26 +00:00
|
|
|
this.obj_num++;
|
|
|
|
id = this.getId();
|
|
|
|
}
|
2011-01-14 18:18:29 +00:00
|
|
|
// Restore the old object number if required.
|
|
|
|
if (restoreOldObjNum) {
|
|
|
|
this.obj_num = oldObjNum;
|
|
|
|
}
|
2011-01-13 06:57:26 +00:00
|
|
|
return id;
|
|
|
|
};
|
|
|
|
|
2014-03-15 13:27:39 +00:00
|
|
|
/**
|
|
|
|
* Releases the object Id, letting it be used as the next id in getNextId().
|
|
|
|
* This method DOES NOT remove any elements from the DOM, it is expected
|
|
|
|
* that client code will do this.
|
|
|
|
* @param {string} id - The id to release.
|
|
|
|
* @returns {boolean} True if the id was valid to be released, false otherwise.
|
|
|
|
*/
|
|
|
|
svgedit.draw.Drawing.prototype.releaseId = function (id) {
|
2011-01-14 18:18:29 +00:00
|
|
|
// confirm if this is a valid id for this Document, else return false
|
2013-02-15 16:51:48 +00:00
|
|
|
var front = this.idPrefix + (this.nonce_ ? this.nonce_ + '_' : '');
|
2014-03-15 13:27:39 +00:00
|
|
|
if (typeof id !== 'string' || id.indexOf(front) !== 0) {
|
2011-01-14 18:18:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// extract the obj_num of this id
|
2014-01-31 02:13:37 +00:00
|
|
|
var num = parseInt(id.substr(front.length), 10);
|
2011-01-14 18:18:29 +00:00
|
|
|
|
|
|
|
// if we didn't get a positive number or we already released this number
|
|
|
|
// then return false.
|
2014-03-15 13:27:39 +00:00
|
|
|
if (typeof num !== 'number' || num <= 0 || this.releasedNums.indexOf(num) != -1) {
|
2011-01-14 18:18:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// push the released number into the released queue
|
|
|
|
this.releasedNums.push(num);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2014-03-15 13:27:39 +00:00
|
|
|
/**
|
|
|
|
* Returns the number of layers in the current drawing.
|
|
|
|
* @returns {integer} The number of layers in the current drawing.
|
|
|
|
*/
|
2011-01-16 23:07:35 +00:00
|
|
|
svgedit.draw.Drawing.prototype.getNumLayers = function() {
|
|
|
|
return this.all_layers.length;
|
|
|
|
};
|
|
|
|
|
2014-03-15 13:27:39 +00:00
|
|
|
/**
|
|
|
|
* Check if layer with given name already exists
|
|
|
|
* @param {string} name - The layer name to check
|
|
|
|
*/
|
|
|
|
svgedit.draw.Drawing.prototype.hasLayer = function (name) {
|
2016-05-01 00:58:19 +00:00
|
|
|
return this.layer_map[name] !== undefined;
|
2011-01-16 23:18:51 +00:00
|
|
|
};
|
|
|
|
|
2011-01-26 03:18:59 +00:00
|
|
|
|
2014-03-15 13:27:39 +00:00
|
|
|
/**
|
|
|
|
* Returns the name of the ith layer. If the index is out of range, an empty string is returned.
|
|
|
|
* @param {integer} i - The zero-based index of the layer you are querying.
|
|
|
|
* @returns {string} The name of the ith layer (or the empty string if none found)
|
|
|
|
*/
|
|
|
|
svgedit.draw.Drawing.prototype.getLayerName = function (i) {
|
2016-05-01 00:58:19 +00:00
|
|
|
return i >= 0 && i < this.getNumLayers() ? this.all_layers[i].getName() : '';
|
2011-01-26 03:18:59 +00:00
|
|
|
};
|
|
|
|
|
2014-03-15 13:27:39 +00:00
|
|
|
/**
|
|
|
|
* @returns {SVGGElement} The SVGGElement representing the current layer.
|
|
|
|
*/
|
2011-01-26 03:18:59 +00:00
|
|
|
svgedit.draw.Drawing.prototype.getCurrentLayer = function() {
|
2016-05-01 01:11:02 +00:00
|
|
|
return this.current_layer ? this.current_layer.getGroup() : null;
|
2011-01-26 03:18:59 +00:00
|
|
|
};
|
|
|
|
|
2016-05-02 02:58:41 +00:00
|
|
|
/**
|
|
|
|
* Get a layer by name.
|
|
|
|
* @returns {SVGGElement} The SVGGElement representing the named layer or null.
|
|
|
|
*/
|
|
|
|
svgedit.draw.Drawing.prototype.getLayerByName = function(name) {
|
|
|
|
var layer = this.layer_map[name];
|
|
|
|
return layer ? layer.getGroup() : null;
|
|
|
|
};
|
|
|
|
|
2014-03-15 13:27:39 +00:00
|
|
|
/**
|
|
|
|
* Returns the name of the currently selected layer. If an error occurs, an empty string
|
|
|
|
* is returned.
|
2016-05-01 00:58:19 +00:00
|
|
|
* @returns {string} The name of the currently active layer (or the empty string if none found).
|
2014-03-15 13:27:39 +00:00
|
|
|
*/
|
|
|
|
svgedit.draw.Drawing.prototype.getCurrentLayerName = function () {
|
2016-05-01 00:58:19 +00:00
|
|
|
return this.current_layer ? this.current_layer.getName() : '';
|
2011-01-26 03:18:59 +00:00
|
|
|
};
|
|
|
|
|
2016-05-02 02:58:41 +00:00
|
|
|
/**
|
|
|
|
* Set the current layer's name.
|
|
|
|
* @param {string} name - The new name.
|
2016-05-02 22:33:45 +00:00
|
|
|
* @param {svgedit.history.HistoryRecordingService} hrService - History recording service
|
|
|
|
* @returns {string|null} The new name if changed; otherwise, null.
|
2016-05-02 02:58:41 +00:00
|
|
|
*/
|
2016-05-02 22:33:45 +00:00
|
|
|
svgedit.draw.Drawing.prototype.setCurrentLayerName = function (name, hrService) {
|
|
|
|
var finalName = null;
|
2016-05-04 13:38:29 +00:00
|
|
|
if (this.current_layer) {
|
2016-05-02 22:33:45 +00:00
|
|
|
var oldName = this.current_layer.getName();
|
|
|
|
finalName = this.current_layer.setName(name, hrService);
|
2016-05-04 13:38:29 +00:00
|
|
|
if (finalName) {
|
2016-05-02 22:33:45 +00:00
|
|
|
delete this.layer_map[oldName];
|
|
|
|
this.layer_map[finalName] = this.current_layer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return finalName;
|
2016-05-02 02:58:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the current layer's position.
|
|
|
|
* @param {number} newpos - The zero-based index of the new position of the layer. Range should be 0 to layers-1
|
|
|
|
* @returns {Object} If the name was changed, returns {title:SVGGElement, previousName:string}; otherwise null.
|
|
|
|
*/
|
|
|
|
svgedit.draw.Drawing.prototype.setCurrentLayerPosition = function (newpos) {
|
|
|
|
var layer_count = this.getNumLayers();
|
|
|
|
if (!this.current_layer || newpos < 0 || newpos >= layer_count) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var oldpos;
|
|
|
|
for (oldpos = 0; oldpos < layer_count; ++oldpos) {
|
|
|
|
if (this.all_layers[oldpos] == this.current_layer) {break;}
|
|
|
|
}
|
|
|
|
// some unknown error condition (current_layer not in all_layers)
|
|
|
|
if (oldpos == layer_count) { return null; }
|
|
|
|
|
|
|
|
if (oldpos != newpos) {
|
|
|
|
// if our new position is below us, we need to insert before the node after newpos
|
|
|
|
var refGroup = null;
|
|
|
|
var current_group = this.current_layer.getGroup();
|
|
|
|
var oldNextSibling = current_group.nextSibling;
|
|
|
|
if (newpos > oldpos ) {
|
|
|
|
if (newpos < layer_count-1) {
|
|
|
|
refGroup = this.all_layers[newpos+1].getGroup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if our new position is above us, we need to insert before the node at newpos
|
|
|
|
else {
|
|
|
|
refGroup = this.all_layers[newpos].getGroup();
|
|
|
|
}
|
|
|
|
this.svgElem_.insertBefore(current_group, refGroup);
|
|
|
|
|
|
|
|
this.identifyLayers();
|
|
|
|
this.setCurrentLayer(this.getLayerName(newpos));
|
|
|
|
|
|
|
|
return {
|
|
|
|
currentGroup: current_group,
|
|
|
|
oldNextSibling: oldNextSibling
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
svgedit.draw.Drawing.prototype.mergeLayer = function (hrService) {
|
|
|
|
var current_group = this.current_layer.getGroup();
|
|
|
|
var prevGroup = $(current_group).prev()[0];
|
2016-05-04 13:38:29 +00:00
|
|
|
if (!prevGroup) {return;}
|
2016-05-02 02:58:41 +00:00
|
|
|
|
|
|
|
hrService.startBatchCommand('Merge Layer');
|
|
|
|
|
|
|
|
var layerNextSibling = current_group.nextSibling;
|
|
|
|
hrService.removeElement(current_group, layerNextSibling, this.svgElem_);
|
|
|
|
|
|
|
|
while (current_group.firstChild) {
|
|
|
|
var child = current_group.firstChild;
|
|
|
|
if (child.localName == 'title') {
|
|
|
|
hrService.removeElement(child, child.nextSibling, current_group);
|
|
|
|
current_group.removeChild(child);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
var oldNextSibling = child.nextSibling;
|
|
|
|
prevGroup.appendChild(child);
|
|
|
|
hrService.moveElement(child, oldNextSibling, current_group);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove current layer's group
|
|
|
|
this.current_layer.removeGroup();
|
|
|
|
// Remove the current layer and set the previous layer as the new current layer
|
|
|
|
var index = this.all_layers.indexOf(this.current_layer);
|
|
|
|
if (index > 0) {
|
|
|
|
var name = this.current_layer.getName();
|
|
|
|
this.current_layer = this.all_layers[index-1]
|
|
|
|
this.all_layers.splice(index, 1);
|
|
|
|
delete this.layer_map[name];
|
|
|
|
}
|
|
|
|
|
|
|
|
hrService.endBatchCommand();
|
|
|
|
};
|
|
|
|
|
|
|
|
svgedit.draw.Drawing.prototype.mergeAllLayers = function (hrService) {
|
|
|
|
// Set the current layer to the last layer.
|
|
|
|
this.current_layer = this.all_layers[this.all_layers.length-1];
|
|
|
|
|
|
|
|
hrService.startBatchCommand('Merge all Layers');
|
|
|
|
while (this.all_layers.length > 1) {
|
|
|
|
this.mergeLayer(hrService);
|
|
|
|
}
|
|
|
|
hrService.endBatchCommand();
|
|
|
|
};
|
|
|
|
|
2014-03-16 11:19:37 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
* @param {string} name - The name of the layer you want to switch to.
|
|
|
|
* @returns {boolean} true if the current layer was switched, otherwise false
|
|
|
|
*/
|
2011-01-26 03:18:59 +00:00
|
|
|
svgedit.draw.Drawing.prototype.setCurrentLayer = function(name) {
|
2016-05-01 00:58:19 +00:00
|
|
|
var layer = this.layer_map[name];
|
|
|
|
if (layer) {
|
|
|
|
if (this.current_layer) {
|
|
|
|
this.current_layer.deactivate();
|
2011-01-26 03:18:59 +00:00
|
|
|
}
|
2016-05-01 00:58:19 +00:00
|
|
|
this.current_layer = layer;
|
|
|
|
this.current_layer.activate();
|
|
|
|
return true;
|
2011-01-26 03:18:59 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2011-01-30 17:11:56 +00:00
|
|
|
|
2014-03-16 11:19:37 +00:00
|
|
|
/**
|
|
|
|
* 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 {SVGGElement} The SVGGElement of the layer removed or null.
|
|
|
|
*/
|
2011-01-30 17:11:56 +00:00
|
|
|
svgedit.draw.Drawing.prototype.deleteCurrentLayer = function() {
|
|
|
|
if (this.current_layer && this.getNumLayers() > 1) {
|
2016-05-01 00:58:19 +00:00
|
|
|
var oldLayerGroup = this.current_layer.removeGroup();
|
2011-01-30 17:11:56 +00:00
|
|
|
this.identifyLayers();
|
|
|
|
return oldLayerGroup;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2016-05-01 00:58:19 +00:00
|
|
|
/**
|
|
|
|
* Find the layer name in a group element.
|
|
|
|
* @param group The group element to search in.
|
|
|
|
* @returns {string} The layer name or empty string.
|
|
|
|
*/
|
|
|
|
function findLayerNameInGroup(group) {
|
|
|
|
var name = $("title", group).text();
|
|
|
|
|
|
|
|
// Hack for Opera 10.60
|
2016-05-01 01:30:13 +00:00
|
|
|
if (!name && svgedit.browser.isOpera() && group.querySelectorAll) {
|
2016-05-01 00:58:19 +00:00
|
|
|
name = $(group.querySelectorAll('title')).text();
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a set of names, return a new unique name.
|
2016-05-02 02:58:41 +00:00
|
|
|
* @param {Array.<string>} existingLayerNames - Existing layer names.
|
2016-05-01 00:58:19 +00:00
|
|
|
* @returns {string} - The new name.
|
|
|
|
*/
|
2016-05-01 01:30:13 +00:00
|
|
|
function getNewLayerName(existingLayerNames) {
|
2016-05-01 00:58:19 +00:00
|
|
|
var i = 1;
|
|
|
|
// TODO(codedread): What about internationalization of "Layer"?
|
|
|
|
while (existingLayerNames.indexOf(("Layer " + i)) >= 0) { i++; }
|
|
|
|
return "Layer " + i;
|
|
|
|
}
|
|
|
|
|
2014-03-16 11:19:37 +00:00
|
|
|
/**
|
|
|
|
* Updates layer system and sets the current layer to the
|
|
|
|
* top-most layer (last <g> child of this drawing).
|
|
|
|
*/
|
2011-01-17 04:36:54 +00:00
|
|
|
svgedit.draw.Drawing.prototype.identifyLayers = function() {
|
|
|
|
this.all_layers = [];
|
2016-05-01 00:58:19 +00:00
|
|
|
this.layer_map = {};
|
2011-01-17 04:36:54 +00:00
|
|
|
var numchildren = this.svgElem_.childNodes.length;
|
|
|
|
// loop through all children of SVG element
|
|
|
|
var orphans = [], layernames = [];
|
2016-05-01 00:58:19 +00:00
|
|
|
var layer = null;
|
2011-01-17 04:36:54 +00:00
|
|
|
var childgroups = false;
|
2016-05-01 00:58:19 +00:00
|
|
|
for (var i = 0; i < numchildren; ++i) {
|
2011-01-17 04:36:54 +00:00
|
|
|
var child = this.svgElem_.childNodes.item(i);
|
|
|
|
// for each g, find its layer name
|
|
|
|
if (child && child.nodeType == 1) {
|
|
|
|
if (child.tagName == "g") {
|
|
|
|
childgroups = true;
|
2016-05-01 00:58:19 +00:00
|
|
|
var name = findLayerNameInGroup(child);
|
2011-01-17 04:36:54 +00:00
|
|
|
if (name) {
|
|
|
|
layernames.push(name);
|
2016-05-02 02:58:41 +00:00
|
|
|
layer = new svgedit.draw.Layer(name, child);
|
2016-05-01 00:58:19 +00:00
|
|
|
this.all_layers.push(layer);
|
2016-05-02 02:58:41 +00:00
|
|
|
this.layer_map[name] = layer;
|
2016-05-01 00:58:19 +00:00
|
|
|
} else {
|
|
|
|
// if group did not have a name, it is an orphan
|
2011-01-17 04:36:54 +00:00
|
|
|
orphans.push(child);
|
|
|
|
}
|
2016-05-01 01:30:13 +00:00
|
|
|
} else if (~visElems.indexOf(child.nodeName)) {
|
2016-05-01 00:58:19 +00:00
|
|
|
// Child is "visible" (i.e. not a <title> or <defs> element), so it is an orphan
|
2011-01-17 04:36:54 +00:00
|
|
|
orphans.push(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-01 00:58:19 +00:00
|
|
|
// If orphans or no layers found, create a new layer and add all the orphans to it
|
2011-01-17 04:36:54 +00:00
|
|
|
if (orphans.length > 0 || !childgroups) {
|
2016-05-02 02:58:41 +00:00
|
|
|
layer = new svgedit.draw.Layer(getNewLayerName(layernames), null, this.svgElem_);
|
2016-05-01 00:58:19 +00:00
|
|
|
layer.appendChildren(orphans);
|
|
|
|
this.all_layers.push(layer);
|
2016-05-02 02:58:41 +00:00
|
|
|
this.layer_map[name] = layer;
|
2016-05-01 00:58:19 +00:00
|
|
|
} else {
|
|
|
|
layer.activate();
|
2011-01-17 04:36:54 +00:00
|
|
|
}
|
2016-05-01 00:58:19 +00:00
|
|
|
this.current_layer = layer;
|
2011-01-17 04:36:54 +00:00
|
|
|
};
|
|
|
|
|
2014-03-16 11:19:37 +00:00
|
|
|
/**
|
2016-05-04 13:38:29 +00:00
|
|
|
* Creates a new top-level layer in the drawing with the given name and
|
|
|
|
* makes it the current layer.
|
2016-05-01 00:58:19 +00:00
|
|
|
* @param {string} name - The given name. If the layer name exists, a new name will be generated.
|
2016-05-04 13:38:29 +00:00
|
|
|
* @param {svgedit.history.HistoryRecordingService} hrService - History recording service
|
2014-03-16 11:19:37 +00:00
|
|
|
* @returns {SVGGElement} The SVGGElement of the new layer, which is
|
2016-05-04 13:38:29 +00:00
|
|
|
* also the current layer of this drawing.
|
2014-03-16 11:19:37 +00:00
|
|
|
*/
|
2016-05-04 13:38:29 +00:00
|
|
|
svgedit.draw.Drawing.prototype.createLayer = function(name, hrService) {
|
2016-05-01 01:30:13 +00:00
|
|
|
if (this.current_layer) {
|
2016-05-01 00:58:19 +00:00
|
|
|
this.current_layer.deactivate();
|
|
|
|
}
|
|
|
|
// Check for duplicate name.
|
2016-05-01 01:30:13 +00:00
|
|
|
if (name === undefined || name === null || name === '' || this.layer_map[name]) {
|
2016-05-01 00:58:19 +00:00
|
|
|
name = getNewLayerName(Object.keys(this.layer_map));
|
|
|
|
}
|
2016-05-04 13:38:29 +00:00
|
|
|
|
|
|
|
// Crate new layer and add to DOM as last layer
|
2016-05-02 02:58:41 +00:00
|
|
|
var layer = new svgedit.draw.Layer(name, null, this.svgElem_);
|
2016-05-04 13:38:29 +00:00
|
|
|
// Like to assume hrService exists, but this is backwards compatible with old version of createLayer.
|
|
|
|
if (hrService) {
|
|
|
|
hrService.startBatchCommand('Create Layer');
|
|
|
|
hrService.insertElement(layer.getGroup());
|
|
|
|
hrService.endBatchCommand();
|
|
|
|
}
|
|
|
|
|
2016-05-01 00:58:19 +00:00
|
|
|
this.all_layers.push(layer);
|
2016-05-02 02:58:41 +00:00
|
|
|
this.layer_map[name] = layer;
|
2016-05-01 00:58:19 +00:00
|
|
|
this.current_layer = layer;
|
|
|
|
return layer.getGroup();
|
2011-01-28 04:37:41 +00:00
|
|
|
};
|
2011-01-16 23:18:51 +00:00
|
|
|
|
2016-05-04 13:38:29 +00:00
|
|
|
/**
|
|
|
|
* Creates a copy of the current layer with the given name and makes it the current layer.
|
|
|
|
* @param {string} name - The given name. If the layer name exists, a new name will be generated.
|
|
|
|
* @param {svgedit.history.HistoryRecordingService} hrService - History recording service
|
|
|
|
* @returns {SVGGElement} The SVGGElement of the new layer, which is
|
|
|
|
* also the current layer of this drawing.
|
|
|
|
*/
|
|
|
|
svgedit.draw.Drawing.prototype.cloneLayer = function(name, hrService) {
|
|
|
|
if (!this.current_layer) {return null;}
|
|
|
|
this.current_layer.deactivate();
|
|
|
|
// Check for duplicate name.
|
|
|
|
if (name === undefined || name === null || name === '' || this.layer_map[name]) {
|
|
|
|
name = getNewLayerName(Object.keys(this.layer_map));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create new group and add to DOM just after current_layer
|
|
|
|
var currentGroup = this.current_layer.getGroup();
|
|
|
|
var layer = new svgedit.draw.Layer(name, currentGroup, this.svgElem_);
|
|
|
|
var group = layer.getGroup();
|
|
|
|
|
|
|
|
// Clone children
|
|
|
|
var children = currentGroup.childNodes;
|
|
|
|
var index;
|
|
|
|
for (index = 0; index < children.length; index++) {
|
|
|
|
var ch = children[index];
|
|
|
|
if (ch.localName == 'title') {continue;}
|
|
|
|
group.appendChild(this.copyElem(ch));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hrService) {
|
|
|
|
hrService.startBatchCommand('Duplicate Layer');
|
|
|
|
hrService.insertElement(group);
|
|
|
|
hrService.endBatchCommand();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update layer containers and current_layer.
|
|
|
|
index = this.all_layers.indexOf(this.current_layer);
|
|
|
|
if (index >= 0) {
|
|
|
|
this.all_layers.splice(index + 1, 0, layer);
|
|
|
|
} else {
|
|
|
|
this.all_layers.push(layer);
|
|
|
|
}
|
|
|
|
this.layer_map[name] = layer;
|
|
|
|
this.current_layer = layer;
|
|
|
|
return group;
|
|
|
|
};
|
|
|
|
|
2014-03-16 11:19:37 +00:00
|
|
|
/**
|
|
|
|
* Returns whether the layer is visible. If the layer name is not valid,
|
|
|
|
* then this function returns false.
|
|
|
|
* @param {string} layername - The name of the layer which you want to query.
|
|
|
|
* @returns {boolean} The visibility state of the layer, or false if the layer name was invalid.
|
|
|
|
*/
|
2011-01-28 06:45:34 +00:00
|
|
|
svgedit.draw.Drawing.prototype.getLayerVisibility = function(layername) {
|
2016-05-01 00:58:19 +00:00
|
|
|
var layer = this.layer_map[layername];
|
|
|
|
return layer ? layer.isVisible() : false;
|
2011-01-28 06:45:34 +00:00
|
|
|
};
|
|
|
|
|
2014-03-16 11:19:37 +00:00
|
|
|
/**
|
|
|
|
* Sets the visibility of the layer. If the layer name is not valid, this
|
|
|
|
* function returns false, otherwise it returns true. This is an
|
|
|
|
* undo-able action.
|
|
|
|
* @param {string} layername - The name of the layer to change the visibility
|
|
|
|
* @param {boolean} bVisible - Whether the layer should be visible
|
|
|
|
* @returns {?SVGGElement} The SVGGElement representing the layer if the
|
|
|
|
* layername was valid, otherwise null.
|
|
|
|
*/
|
2011-01-28 07:39:30 +00:00
|
|
|
svgedit.draw.Drawing.prototype.setLayerVisibility = function(layername, bVisible) {
|
2014-03-15 13:27:39 +00:00
|
|
|
if (typeof bVisible !== 'boolean') {
|
2011-01-28 07:39:30 +00:00
|
|
|
return null;
|
|
|
|
}
|
2016-05-01 00:58:19 +00:00
|
|
|
var layer = this.layer_map[layername];
|
2014-01-31 02:13:37 +00:00
|
|
|
if (!layer) {return null;}
|
2016-05-01 00:58:19 +00:00
|
|
|
layer.setVisible(bVisible);
|
|
|
|
return layer.getGroup();
|
2011-01-28 07:39:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-03-15 13:27:39 +00:00
|
|
|
/**
|
|
|
|
* Returns the opacity of the given layer. If the input name is not a layer, null is returned.
|
|
|
|
* @param {string} layername - name of the layer on which to get the opacity
|
|
|
|
* @returns {?number} 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
|
|
|
|
*/
|
2011-01-28 06:45:34 +00:00
|
|
|
svgedit.draw.Drawing.prototype.getLayerOpacity = function(layername) {
|
2016-05-01 00:58:19 +00:00
|
|
|
var layer = this.layer_map[layername];
|
|
|
|
if (!layer) {return null;}
|
|
|
|
return layer.getOpacity();
|
2011-01-28 06:45:34 +00:00
|
|
|
};
|
|
|
|
|
2014-03-16 11:19:37 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
* @param {string} layername - Name of the layer on which to set the opacity
|
|
|
|
* @param {number} opacity - A float value in the range 0.0-1.0
|
|
|
|
*/
|
2011-01-28 07:39:30 +00:00
|
|
|
svgedit.draw.Drawing.prototype.setLayerOpacity = function(layername, opacity) {
|
2014-03-15 13:27:39 +00:00
|
|
|
if (typeof opacity !== 'number' || opacity < 0.0 || opacity > 1.0) {
|
2011-01-28 07:39:30 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-05-01 00:58:19 +00:00
|
|
|
var layer = this.layer_map[layername];
|
|
|
|
if (layer) {
|
|
|
|
layer.setOpacity(opacity);
|
2011-01-28 07:39:30 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-04 13:38:29 +00:00
|
|
|
/**
|
|
|
|
* Create a clone of an element, updating its ID and its children's IDs when needed
|
|
|
|
* @param {Element} el - DOM element to clone
|
|
|
|
* @returns {Element}
|
|
|
|
*/
|
|
|
|
svgedit.draw.Drawing.prototype.copyElem = function(el) {
|
|
|
|
var self = this;
|
|
|
|
var getNextIdClosure = function() { return self.getNextId();}
|
|
|
|
return svgedit.utilities.copyElem(el, getNextIdClosure)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-31 02:13:37 +00:00
|
|
|
}());
|