123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537 |
- (function() {'use strict';
- if (!svgedit.draw) {
- svgedit.draw = {};
- }
- var NS = svgedit.NS;
- var visElems = 'a,circle,ellipse,foreignObject,g,image,line,path,polygon,polyline,rect,svg,text,tspan,use'.split(',');
- var RandomizeModes = {
- LET_DOCUMENT_DECIDE: 0,
- ALWAYS_RANDOMIZE: 1,
- NEVER_RANDOMIZE: 2
- };
- var randomize_ids = RandomizeModes.LET_DOCUMENT_DECIDE;
- svgedit.draw.Layer = function(name, group) {
- this.name_ = name;
- this.group_ = group;
- };
- svgedit.draw.Layer.prototype.getName = function() {
- return this.name_;
- };
- svgedit.draw.Layer.prototype.getGroup = function() {
- return this.group_;
- };
- svgedit.draw.randomizeIds = function(enableRandomization, currentDrawing) {
- randomize_ids = enableRandomization === false ?
- RandomizeModes.NEVER_RANDOMIZE :
- RandomizeModes.ALWAYS_RANDOMIZE;
- 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();
- }
- };
- svgedit.draw.Drawing = function(svgElem, opt_idPrefix) {
- if (!svgElem || !svgElem.tagName || !svgElem.namespaceURI ||
- svgElem.tagName != 'svg' || svgElem.namespaceURI != NS.SVG) {
- throw "Error: svgedit.draw.Drawing instance initialized without a <svg> element";
- }
-
- this.svgElem_ = svgElem;
-
-
- this.obj_num = 0;
-
-
- this.idPrefix = opt_idPrefix || "svg_";
-
-
- this.releasedNums = [];
-
- this.all_layers = [];
-
- this.current_layer = null;
-
- this.nonce_ = '';
- var n = this.svgElem_.getAttributeNS(NS.SE, 'nonce');
-
-
- if (!!n && randomize_ids != RandomizeModes.NEVER_RANDOMIZE) {
- this.nonce_ = n;
- } else if (randomize_ids == RandomizeModes.ALWAYS_RANDOMIZE) {
- this.setNonce(Math.floor(Math.random() * 100001));
- }
- };
- svgedit.draw.Drawing.prototype.getElem_ = function (id) {
- if (this.svgElem_.querySelector) {
-
- return this.svgElem_.querySelector('#' + id);
- }
-
- return $(this.svgElem_).find('[id=' + id + ']')[0];
- };
- svgedit.draw.Drawing.prototype.getSvgElem = function () {
- return this.svgElem_;
- };
- svgedit.draw.Drawing.prototype.getNonce = function() {
- return this.nonce_;
- };
- svgedit.draw.Drawing.prototype.setNonce = function(n) {
- this.svgElem_.setAttributeNS(NS.XMLNS, 'xmlns:se', NS.SE);
- this.svgElem_.setAttributeNS(NS.SE, 'se:nonce', n);
- this.nonce_ = n;
- };
- svgedit.draw.Drawing.prototype.clearNonce = function () {
-
-
- this.nonce_ = '';
- };
- svgedit.draw.Drawing.prototype.getId = function () {
- return this.nonce_ ?
- this.idPrefix + this.nonce_ + '_' + this.obj_num :
- this.idPrefix + this.obj_num;
- };
- svgedit.draw.Drawing.prototype.getNextId = function () {
- var oldObjNum = this.obj_num;
- var restoreOldObjNum = false;
-
-
-
- if (this.releasedNums.length > 0) {
- this.obj_num = this.releasedNums.pop();
- restoreOldObjNum = true;
- } else {
-
- this.obj_num++;
- }
-
- var id = this.getId();
- while (this.getElem_(id)) {
- if (restoreOldObjNum) {
- this.obj_num = oldObjNum;
- restoreOldObjNum = false;
- }
- this.obj_num++;
- id = this.getId();
- }
-
- if (restoreOldObjNum) {
- this.obj_num = oldObjNum;
- }
- return id;
- };
- svgedit.draw.Drawing.prototype.releaseId = function (id) {
-
- var front = this.idPrefix + (this.nonce_ ? this.nonce_ + '_' : '');
- if (typeof id !== 'string' || id.indexOf(front) !== 0) {
- return false;
- }
-
- var num = parseInt(id.substr(front.length), 10);
-
-
- if (typeof num !== 'number' || num <= 0 || this.releasedNums.indexOf(num) != -1) {
- return false;
- }
-
-
- this.releasedNums.push(num);
- return true;
- };
- svgedit.draw.Drawing.prototype.getNumLayers = function() {
- return this.all_layers.length;
- };
- svgedit.draw.Drawing.prototype.hasLayer = function (name) {
- var i;
- for (i = 0; i < this.getNumLayers(); i++) {
- if(this.all_layers[i][0] == name) {return true;}
- }
- return false;
- };
- svgedit.draw.Drawing.prototype.getLayerName = function (i) {
- if (i >= 0 && i < this.getNumLayers()) {
- return this.all_layers[i][0];
- }
- return '';
- };
- svgedit.draw.Drawing.prototype.getCurrentLayer = function() {
- return this.current_layer;
- };
- svgedit.draw.Drawing.prototype.getCurrentLayerName = function () {
- var i;
- for (i = 0; i < this.getNumLayers(); ++i) {
- if (this.all_layers[i][1] == this.current_layer) {
- return this.getLayerName(i);
- }
- }
- return '';
- };
- svgedit.draw.Drawing.prototype.setCurrentLayer = function(name) {
- var i;
- for (i = 0; i < this.getNumLayers(); ++i) {
- if (name == this.getLayerName(i)) {
- if (this.current_layer != this.all_layers[i][1]) {
- this.current_layer.setAttribute("style", "pointer-events:none");
- this.current_layer = this.all_layers[i][1];
- this.current_layer.setAttribute("style", "pointer-events:all");
- }
- return true;
- }
- }
- return false;
- };
- svgedit.draw.Drawing.prototype.deleteCurrentLayer = function() {
- if (this.current_layer && this.getNumLayers() > 1) {
-
- 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;
- };
- svgedit.draw.Drawing.prototype.identifyLayers = function() {
- this.all_layers = [];
- var numchildren = this.svgElem_.childNodes.length;
-
- var orphans = [], layernames = [];
- var a_layer = null;
- var childgroups = false;
- var i;
- for (i = 0; i < numchildren; ++i) {
- var child = this.svgElem_.childNodes.item(i);
-
- if (child && child.nodeType == 1) {
- if (child.tagName == "g") {
- childgroups = true;
- var name = $("title", child).text();
-
- if(!name && svgedit.browser.isOpera() && child.querySelectorAll) {
- name = $(child.querySelectorAll('title')).text();
- }
-
- if (name) {
- layernames.push(name);
- this.all_layers.push( [name, child] );
- a_layer = child;
- svgedit.utilities.walkTree(child, function(e){e.setAttribute("style", "pointer-events:inherit");});
- a_layer.setAttribute("style", "pointer-events:none");
- }
-
- else {
- orphans.push(child);
- }
- }
-
- else if(~visElems.indexOf(child.nodeName)) {
- var bb = svgedit.utilities.getBBox(child);
- orphans.push(child);
- }
- }
- }
-
-
- var svgdoc = this.svgElem_.ownerDocument;
- if (orphans.length > 0 || !childgroups) {
- i = 1;
-
- while (layernames.indexOf(("Layer " + i)) >= 0) { i++; }
- var newname = "Layer " + i;
- a_layer = svgdoc.createElementNS(NS.SVG, "g");
- var layer_title = svgdoc.createElementNS(NS.SVG, "title");
- layer_title.textContent = newname;
- a_layer.appendChild(layer_title);
- var j;
- for (j = 0; j < orphans.length; ++j) {
- a_layer.appendChild(orphans[j]);
- }
- this.svgElem_.appendChild(a_layer);
- this.all_layers.push( [newname, a_layer] );
- }
- svgedit.utilities.walkTree(a_layer, function(e){e.setAttribute("style", "pointer-events:inherit");});
- this.current_layer = a_layer;
- this.current_layer.setAttribute("style", "pointer-events:all");
- };
- svgedit.draw.Drawing.prototype.createLayer = function(name) {
- var svgdoc = this.svgElem_.ownerDocument;
- var new_layer = svgdoc.createElementNS(NS.SVG, "g");
- var layer_title = svgdoc.createElementNS(NS.SVG, "title");
- layer_title.textContent = name;
- new_layer.appendChild(layer_title);
- this.svgElem_.appendChild(new_layer);
- this.identifyLayers();
- return new_layer;
- };
- svgedit.draw.Drawing.prototype.getLayerVisibility = function(layername) {
-
- var layer = null;
- var i;
- for (i = 0; i < this.getNumLayers(); ++i) {
- if (this.getLayerName(i) == layername) {
- layer = this.all_layers[i][1];
- break;
- }
- }
- if (!layer) {return false;}
- return (layer.getAttribute('display') !== 'none');
- };
- svgedit.draw.Drawing.prototype.setLayerVisibility = function(layername, bVisible) {
- if (typeof bVisible !== 'boolean') {
- return null;
- }
-
- var layer = null;
- var i;
- for (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;
- };
- svgedit.draw.Drawing.prototype.getLayerOpacity = function(layername) {
- var i;
- for (i = 0; i < this.getNumLayers(); ++i) {
- if (this.getLayerName(i) == layername) {
- var g = this.all_layers[i][1];
- var opacity = g.getAttribute('opacity');
- if (!opacity) {
- opacity = '1.0';
- }
- return parseFloat(opacity);
- }
- }
- return null;
- };
- svgedit.draw.Drawing.prototype.setLayerOpacity = function(layername, opacity) {
- if (typeof opacity !== 'number' || opacity < 0.0 || opacity > 1.0) {
- return;
- }
- var i;
- for (i = 0; i < this.getNumLayers(); ++i) {
- if (this.getLayerName(i) == layername) {
- var g = this.all_layers[i][1];
- g.setAttribute("opacity", opacity);
- break;
- }
- }
- };
- }());
|