2018-05-16 00:53:27 +00:00
|
|
|
/* eslint-disable no-var */
|
|
|
|
/* globals $ svgedit */
|
2016-05-02 02:58:41 +00:00
|
|
|
/**
|
|
|
|
* Package: svgedit.history
|
|
|
|
*
|
|
|
|
* Licensed under the MIT License
|
|
|
|
*
|
|
|
|
* Copyright(c) 2011 Jeff Schiller
|
|
|
|
* Copyright(c) 2016 Flint O'Brien
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Dependencies:
|
|
|
|
// 1) svgedit.js
|
|
|
|
// 2) draw.js
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
(function () {
|
|
|
|
'use strict';
|
2016-05-02 02:58:41 +00:00
|
|
|
|
|
|
|
if (!svgedit.draw) {
|
2018-05-18 04:02:30 +00:00
|
|
|
svgedit.draw = {};
|
2016-05-02 02:58:41 +00:00
|
|
|
}
|
|
|
|
var NS = svgedit.NS;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class encapsulates the concept of a layer in the drawing. It can be constructed with
|
|
|
|
* an existing group element or, with three parameters, will create a new layer group element.
|
2016-05-04 13:38:29 +00:00
|
|
|
*
|
|
|
|
* Usage:
|
2016-05-04 13:54:20 +00:00
|
|
|
* new Layer'name', group) // Use the existing group for this layer.
|
|
|
|
* new Layer('name', group, svgElem) // Create a new group and add it to the DOM after group.
|
|
|
|
* new Layer('name', null, svgElem) // Create a new group and add it to the DOM as the last layer.
|
2016-05-04 13:38:29 +00:00
|
|
|
*
|
2016-05-02 02:58:41 +00:00
|
|
|
* @param {string} name - Layer name
|
2016-05-04 13:38:29 +00:00
|
|
|
* @param {SVGGElement|null} group - An existing SVG group element or null.
|
2018-05-18 06:41:43 +00:00
|
|
|
* If group and no svgElem, use group for this layer.
|
|
|
|
* If group and svgElem, create a new group element and insert it in the DOM after group.
|
|
|
|
* If no group and svgElem, create a new group element and insert it in the DOM as the last layer.
|
2016-05-04 13:38:29 +00:00
|
|
|
* @param {SVGGElement=} svgElem - The SVG DOM element. If defined, use this to add
|
2018-05-18 06:41:43 +00:00
|
|
|
* a new layer to the document.
|
2016-05-02 02:58:41 +00:00
|
|
|
*/
|
2018-05-16 00:53:27 +00:00
|
|
|
var Layer = svgedit.draw.Layer = function (name, group, svgElem) {
|
2018-05-18 04:02:30 +00:00
|
|
|
this.name_ = name;
|
|
|
|
this.group_ = svgElem ? null : group;
|
|
|
|
|
|
|
|
if (svgElem) {
|
|
|
|
// Create a group element with title and add it to the DOM.
|
|
|
|
var svgdoc = svgElem.ownerDocument;
|
|
|
|
this.group_ = svgdoc.createElementNS(NS.SVG, 'g');
|
|
|
|
var layerTitle = svgdoc.createElementNS(NS.SVG, 'title');
|
|
|
|
layerTitle.textContent = name;
|
|
|
|
this.group_.appendChild(layerTitle);
|
|
|
|
if (group) {
|
|
|
|
$(group).after(this.group_);
|
|
|
|
} else {
|
|
|
|
svgElem.appendChild(this.group_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addLayerClass(this.group_);
|
|
|
|
svgedit.utilities.walkTree(this.group_, function (e) {
|
|
|
|
e.setAttribute('style', 'pointer-events:inherit');
|
|
|
|
});
|
|
|
|
|
|
|
|
this.group_.setAttribute('style', svgElem ? 'pointer-events:all' : 'pointer-events:none');
|
2016-05-02 02:58:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {string} CLASS_NAME - class attribute assigned to all layer groups.
|
|
|
|
*/
|
|
|
|
Layer.CLASS_NAME = 'layer';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {RegExp} CLASS_REGEX - Used to test presence of class Layer.CLASS_NAME
|
|
|
|
*/
|
|
|
|
Layer.CLASS_REGEX = new RegExp('(\\s|^)' + Layer.CLASS_NAME + '(\\s|$)');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the layer's name.
|
|
|
|
* @returns {string} The layer name
|
|
|
|
*/
|
2018-05-16 00:53:27 +00:00
|
|
|
Layer.prototype.getName = function () {
|
2018-05-18 04:02:30 +00:00
|
|
|
return this.name_;
|
2016-05-02 02:58:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the group element for this layer.
|
|
|
|
* @returns {SVGGElement} The layer SVG group
|
|
|
|
*/
|
2018-05-16 00:53:27 +00:00
|
|
|
Layer.prototype.getGroup = function () {
|
2018-05-18 04:02:30 +00:00
|
|
|
return this.group_;
|
2016-05-02 02:58:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Active this layer so it takes pointer events.
|
|
|
|
*/
|
2018-05-16 00:53:27 +00:00
|
|
|
Layer.prototype.activate = function () {
|
2018-05-18 04:02:30 +00:00
|
|
|
this.group_.setAttribute('style', 'pointer-events:all');
|
2016-05-02 02:58:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deactive this layer so it does NOT take pointer events.
|
|
|
|
*/
|
2018-05-16 00:53:27 +00:00
|
|
|
Layer.prototype.deactivate = function () {
|
2018-05-18 04:02:30 +00:00
|
|
|
this.group_.setAttribute('style', 'pointer-events:none');
|
2016-05-02 02:58:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set this layer visible or hidden based on 'visible' parameter.
|
|
|
|
* @param {boolean} visible - If true, make visible; otherwise, hide it.
|
|
|
|
*/
|
2018-05-16 00:53:27 +00:00
|
|
|
Layer.prototype.setVisible = function (visible) {
|
2018-05-18 04:02:30 +00:00
|
|
|
var expected = visible === undefined || visible ? 'inline' : 'none';
|
|
|
|
var oldDisplay = this.group_.getAttribute('display');
|
|
|
|
if (oldDisplay !== expected) {
|
|
|
|
this.group_.setAttribute('display', expected);
|
|
|
|
}
|
2016-05-02 02:58:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is this layer visible?
|
|
|
|
* @returns {boolean} True if visible.
|
|
|
|
*/
|
2018-05-16 00:53:27 +00:00
|
|
|
Layer.prototype.isVisible = function () {
|
2018-05-18 04:02:30 +00:00
|
|
|
return this.group_.getAttribute('display') !== 'none';
|
2016-05-02 02:58:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get layer opacity.
|
|
|
|
* @returns {number} Opacity value.
|
|
|
|
*/
|
2018-05-16 00:53:27 +00:00
|
|
|
Layer.prototype.getOpacity = function () {
|
2018-05-18 04:02:30 +00:00
|
|
|
var opacity = this.group_.getAttribute('opacity');
|
|
|
|
if (opacity === null || opacity === undefined) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return parseFloat(opacity);
|
2016-05-02 02:58:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the opacity of this layer. If opacity is not a value between 0.0 and 1.0,
|
|
|
|
* nothing happens.
|
|
|
|
* @param {number} opacity - A float value in the range 0.0-1.0
|
|
|
|
*/
|
2018-05-16 00:53:27 +00:00
|
|
|
Layer.prototype.setOpacity = function (opacity) {
|
2018-05-18 04:02:30 +00:00
|
|
|
if (typeof opacity === 'number' && opacity >= 0.0 && opacity <= 1.0) {
|
|
|
|
this.group_.setAttribute('opacity', opacity);
|
|
|
|
}
|
2016-05-02 02:58:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Append children to this layer.
|
|
|
|
* @param {SVGGElement} children - The children to append to this layer.
|
|
|
|
*/
|
2018-05-16 00:53:27 +00:00
|
|
|
Layer.prototype.appendChildren = function (children) {
|
2018-05-18 04:02:30 +00:00
|
|
|
for (var i = 0; i < children.length; ++i) {
|
|
|
|
this.group_.appendChild(children[i]);
|
|
|
|
}
|
2016-05-02 02:58:41 +00:00
|
|
|
};
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
Layer.prototype.getTitleElement = function () {
|
2018-05-18 04:02:30 +00:00
|
|
|
var len = this.group_.childNodes.length;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
var child = this.group_.childNodes.item(i);
|
|
|
|
if (child && child.tagName === 'title') {
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2016-05-02 02:58:41 +00:00
|
|
|
};
|
|
|
|
|
2016-05-02 22:33:45 +00:00
|
|
|
/**
|
|
|
|
* Set the name of this layer.
|
|
|
|
* @param {string} name - The new name.
|
|
|
|
* @param {svgedit.history.HistoryRecordingService} hrService - History recording service
|
|
|
|
* @returns {string|null} The new name if changed; otherwise, null.
|
|
|
|
*/
|
2018-05-16 00:53:27 +00:00
|
|
|
Layer.prototype.setName = function (name, hrService) {
|
2018-05-18 04:02:30 +00:00
|
|
|
var previousName = this.name_;
|
|
|
|
name = svgedit.utilities.toXml(name);
|
|
|
|
// now change the underlying title element contents
|
|
|
|
var title = this.getTitleElement();
|
|
|
|
if (title) {
|
|
|
|
$(title).empty();
|
|
|
|
title.textContent = name;
|
|
|
|
this.name_ = name;
|
|
|
|
if (hrService) {
|
|
|
|
hrService.changeElement(title, {'#text': previousName});
|
|
|
|
}
|
|
|
|
return this.name_;
|
|
|
|
}
|
|
|
|
return null;
|
2016-05-02 02:58:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove this layer's group from the DOM. No more functions on group can be called after this.
|
|
|
|
* @param {SVGGElement} children - The children to append to this layer.
|
|
|
|
* @returns {SVGGElement} The layer SVG group that was just removed.
|
|
|
|
*/
|
2018-05-16 00:53:27 +00:00
|
|
|
Layer.prototype.removeGroup = function () {
|
2018-05-18 04:02:30 +00:00
|
|
|
var parent = this.group_.parentNode;
|
|
|
|
var group = parent.removeChild(this.group_);
|
|
|
|
this.group_ = undefined;
|
|
|
|
return group;
|
2016-05-02 02:58:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add class Layer.CLASS_NAME to the element (usually class='layer').
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* @param {SVGGElement} elem - The SVG element to update
|
|
|
|
*/
|
2018-05-16 00:53:27 +00:00
|
|
|
function addLayerClass (elem) {
|
2018-05-18 04:02:30 +00:00
|
|
|
var classes = elem.getAttribute('class');
|
|
|
|
if (classes === null || classes === undefined || classes.length === 0) {
|
|
|
|
elem.setAttribute('class', Layer.CLASS_NAME);
|
|
|
|
} else if (!Layer.CLASS_REGEX.test(classes)) {
|
|
|
|
elem.setAttribute('class', classes + ' ' + Layer.CLASS_NAME);
|
|
|
|
}
|
2016-05-02 02:58:41 +00:00
|
|
|
}
|
|
|
|
}());
|