2014-01-31 10:40:52 +00:00
|
|
|
/*globals $, svgedit, svgCanvas*/
|
|
|
|
/*jslint vars: true, eqeq: true, todo: true, bitwise: true, continue: true, forin: true */
|
2011-10-01 04:41:14 +00:00
|
|
|
/*
|
2009-11-25 13:51:42 +00:00
|
|
|
* svgcanvas.js
|
|
|
|
*
|
2012-09-16 18:53:27 +00:00
|
|
|
* Licensed under the MIT License
|
2009-11-25 13:51:42 +00:00
|
|
|
*
|
2010-01-06 04:35:41 +00:00
|
|
|
* Copyright(c) 2010 Alexis Deveria
|
|
|
|
* Copyright(c) 2010 Pavol Rusnak
|
|
|
|
* Copyright(c) 2010 Jeff Schiller
|
2009-11-25 13:51:42 +00:00
|
|
|
*
|
|
|
|
*/
|
2010-01-08 20:36:39 +00:00
|
|
|
|
2010-10-28 06:08:45 +00:00
|
|
|
// Dependencies:
|
|
|
|
// 1) jQuery
|
2011-01-13 07:47:21 +00:00
|
|
|
// 2) browser.js
|
2010-10-28 06:08:45 +00:00
|
|
|
// 3) svgtransformlist.js
|
2010-10-28 16:47:39 +00:00
|
|
|
// 4) math.js
|
2010-11-07 20:14:05 +00:00
|
|
|
// 5) units.js
|
|
|
|
// 6) svgutils.js
|
|
|
|
// 7) sanitize.js
|
2010-11-12 19:08:29 +00:00
|
|
|
// 8) history.js
|
2011-01-13 06:57:26 +00:00
|
|
|
// 9) select.js
|
2011-01-16 23:07:35 +00:00
|
|
|
// 10) draw.js
|
2011-02-04 08:02:46 +00:00
|
|
|
// 11) path.js
|
2013-02-17 04:58:04 +00:00
|
|
|
// 12) coords.js
|
2013-02-20 06:29:25 +00:00
|
|
|
// 13) recalculate.js
|
2010-10-28 06:08:45 +00:00
|
|
|
|
2014-02-17 06:48:40 +00:00
|
|
|
(function () {
|
2014-01-31 10:40:52 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!window.console) {
|
2009-09-22 21:50:55 +00:00
|
|
|
window.console = {};
|
|
|
|
window.console.log = function(str) {};
|
|
|
|
window.console.dir = function(str) {};
|
2009-06-23 05:17:53 +00:00
|
|
|
}
|
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (window.opera) {
|
2010-11-12 19:08:29 +00:00
|
|
|
window.console.log = function(str) { opera.postError(str); };
|
2010-01-20 16:14:32 +00:00
|
|
|
window.console.dir = function(str) {};
|
2009-12-31 15:25:57 +00:00
|
|
|
}
|
|
|
|
|
2014-01-31 10:40:52 +00:00
|
|
|
}());
|
|
|
|
|
2010-06-18 20:35:47 +00:00
|
|
|
// Class: SvgCanvas
|
|
|
|
// The main SvgCanvas class that manages all SVG-related functions
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// container - The container HTML element that should hold the SVG root element
|
|
|
|
// config - An object that contains configuration data
|
2014-01-31 10:40:52 +00:00
|
|
|
$.SvgCanvas = function(container, config) {
|
2013-02-16 15:02:26 +00:00
|
|
|
// Alias Namespace constants
|
|
|
|
var NS = svgedit.NS;
|
2010-11-07 05:20:03 +00:00
|
|
|
|
2010-11-06 00:45:21 +00:00
|
|
|
// Default configuration options
|
|
|
|
var curConfig = {
|
|
|
|
show_outside_canvas: true,
|
2011-01-13 16:17:00 +00:00
|
|
|
selectNew: true,
|
2010-11-06 00:45:21 +00:00
|
|
|
dimensions: [640, 480]
|
|
|
|
};
|
|
|
|
|
|
|
|
// Update config with new one if given
|
2013-02-15 15:51:58 +00:00
|
|
|
if (config) {
|
2010-11-06 00:45:21 +00:00
|
|
|
$.extend(curConfig, config);
|
|
|
|
}
|
2010-11-05 17:00:32 +00:00
|
|
|
|
2010-11-15 09:25:49 +00:00
|
|
|
// Array with width/height of canvas
|
|
|
|
var dimensions = curConfig.dimensions;
|
|
|
|
|
2010-11-12 19:08:29 +00:00
|
|
|
var canvas = this;
|
|
|
|
|
2010-11-07 05:20:03 +00:00
|
|
|
// "document" element associated with the container (same as window.document using default svg-editor.js)
|
2011-01-13 06:57:26 +00:00
|
|
|
// NOTE: This is not actually a SVG document, but a HTML document.
|
2010-11-07 05:20:03 +00:00
|
|
|
var svgdoc = container.ownerDocument;
|
|
|
|
|
2010-11-15 09:25:49 +00:00
|
|
|
// This is a container for the document being edited, not the document itself.
|
|
|
|
var svgroot = svgdoc.importNode(svgedit.utilities.text2xml(
|
2013-02-16 15:02:26 +00:00
|
|
|
'<svg id="svgroot" xmlns="' + NS.SVG + '" xlinkns="' + NS.XLINK + '" ' +
|
2010-11-15 09:25:49 +00:00
|
|
|
'width="' + dimensions[0] + '" height="' + dimensions[1] + '" x="' + dimensions[0] + '" y="' + dimensions[1] + '" overflow="visible">' +
|
|
|
|
'<defs>' +
|
|
|
|
'<filter id="canvashadow" filterUnits="objectBoundingBox">' +
|
|
|
|
'<feGaussianBlur in="SourceAlpha" stdDeviation="4" result="blur"/>'+
|
|
|
|
'<feOffset in="blur" dx="5" dy="5" result="offsetBlur"/>'+
|
|
|
|
'<feMerge>'+
|
|
|
|
'<feMergeNode in="offsetBlur"/>'+
|
|
|
|
'<feMergeNode in="SourceGraphic"/>'+
|
|
|
|
'</feMerge>'+
|
|
|
|
'</filter>'+
|
|
|
|
'</defs>'+
|
|
|
|
'</svg>').documentElement, true);
|
|
|
|
container.appendChild(svgroot);
|
|
|
|
|
2010-11-07 05:20:03 +00:00
|
|
|
// The actual element that represents the final output SVG element
|
2014-04-08 03:21:22 +00:00
|
|
|
var svgcontent = svgdoc.createElementNS(NS.SVG, 'svg');
|
2010-11-07 05:20:03 +00:00
|
|
|
|
2011-02-01 07:22:18 +00:00
|
|
|
// This function resets the svgcontent element while keeping it in the DOM.
|
|
|
|
var clearSvgContentElement = canvas.clearSvgContentElement = function() {
|
|
|
|
while (svgcontent.firstChild) { svgcontent.removeChild(svgcontent.firstChild); }
|
|
|
|
|
|
|
|
// TODO: Clear out all other attributes first?
|
|
|
|
$(svgcontent).attr({
|
|
|
|
id: 'svgcontent',
|
|
|
|
width: dimensions[0],
|
|
|
|
height: dimensions[1],
|
|
|
|
x: dimensions[0],
|
|
|
|
y: dimensions[1],
|
|
|
|
overflow: curConfig.show_outside_canvas ? 'visible' : 'hidden',
|
2013-02-16 15:02:26 +00:00
|
|
|
xmlns: NS.SVG,
|
2014-04-08 03:21:22 +00:00
|
|
|
'xmlns:se': NS.SE,
|
|
|
|
'xmlns:xlink': NS.XLINK
|
2011-02-01 07:22:18 +00:00
|
|
|
}).appendTo(svgroot);
|
|
|
|
|
|
|
|
// TODO: make this string optional and set by the client
|
|
|
|
var comment = svgdoc.createComment(" Created with SVG-edit - http://svg-edit.googlecode.com/ ");
|
|
|
|
svgcontent.appendChild(comment);
|
|
|
|
};
|
|
|
|
clearSvgContentElement();
|
2010-11-15 09:25:49 +00:00
|
|
|
|
2011-01-14 18:18:29 +00:00
|
|
|
// Prefix string for element IDs
|
2014-04-08 03:21:22 +00:00
|
|
|
var idprefix = 'svg_';
|
2011-01-14 18:18:29 +00:00
|
|
|
|
|
|
|
// Function: setIdPrefix
|
|
|
|
// Changes the ID prefix to the given value
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// p - String with the new prefix
|
|
|
|
canvas.setIdPrefix = function(p) {
|
|
|
|
idprefix = p;
|
|
|
|
};
|
|
|
|
|
2011-01-16 23:07:35 +00:00
|
|
|
// Current svgedit.draw.Drawing object
|
|
|
|
// @type {svgedit.draw.Drawing}
|
2011-02-01 07:22:18 +00:00
|
|
|
canvas.current_drawing_ = new svgedit.draw.Drawing(svgcontent, idprefix);
|
2011-01-14 18:18:29 +00:00
|
|
|
|
2011-01-16 23:07:35 +00:00
|
|
|
// Function: getCurrentDrawing
|
|
|
|
// Returns the current Drawing.
|
|
|
|
// @return {svgedit.draw.Drawing}
|
2011-01-25 16:15:29 +00:00
|
|
|
var getCurrentDrawing = canvas.getCurrentDrawing = function() {
|
2011-02-01 07:22:18 +00:00
|
|
|
return canvas.current_drawing_;
|
2011-01-14 18:18:29 +00:00
|
|
|
};
|
2010-11-07 05:20:03 +00:00
|
|
|
|
|
|
|
// Float displaying the current zoom level (1 = 100%, .5 = 50%, etc)
|
|
|
|
var current_zoom = 1;
|
|
|
|
|
2010-11-15 09:25:49 +00:00
|
|
|
// pointer to current group (for in-group editing)
|
|
|
|
var current_group = null;
|
|
|
|
|
|
|
|
// Object containing data for the currently selected styles
|
|
|
|
var all_properties = {
|
|
|
|
shape: {
|
2012-03-26 13:33:48 +00:00
|
|
|
fill: (curConfig.initFill.color == 'none' ? '' : '#') + curConfig.initFill.color,
|
2010-11-15 09:25:49 +00:00
|
|
|
fill_paint: null,
|
|
|
|
fill_opacity: curConfig.initFill.opacity,
|
2014-04-08 03:21:22 +00:00
|
|
|
stroke: '#' + curConfig.initStroke.color,
|
2010-11-15 09:25:49 +00:00
|
|
|
stroke_paint: null,
|
|
|
|
stroke_opacity: curConfig.initStroke.opacity,
|
|
|
|
stroke_width: curConfig.initStroke.width,
|
|
|
|
stroke_dasharray: 'none',
|
|
|
|
stroke_linejoin: 'miter',
|
|
|
|
stroke_linecap: 'butt',
|
|
|
|
opacity: curConfig.initOpacity
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
all_properties.text = $.extend(true, {}, all_properties.shape);
|
|
|
|
$.extend(all_properties.text, {
|
2014-04-08 03:21:22 +00:00
|
|
|
fill: '#000000',
|
2010-11-15 09:25:49 +00:00
|
|
|
stroke_width: 0,
|
|
|
|
font_size: 24,
|
|
|
|
font_family: 'serif'
|
|
|
|
});
|
|
|
|
|
|
|
|
// Current shape style properties
|
|
|
|
var cur_shape = all_properties.shape;
|
|
|
|
|
2010-12-02 17:14:24 +00:00
|
|
|
// Array with all the currently selected elements
|
|
|
|
// default size of 1 until it needs to grow bigger
|
|
|
|
var selectedElements = new Array(1);
|
|
|
|
|
2010-11-15 09:25:49 +00:00
|
|
|
// Function: addSvgElementFromJson
|
|
|
|
// Create a new SVG element based on the given object keys/values and add it to the current layer
|
|
|
|
// The element will be ran through cleanupElement before being returned
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// data - Object with the following keys/values:
|
|
|
|
// * element - tag name of the SVG element to create
|
|
|
|
// * attr - Object with attributes key-values to assign to the new element
|
|
|
|
// * curStyles - Boolean indicating that current style attributes should be applied first
|
|
|
|
//
|
|
|
|
// Returns: The new element
|
|
|
|
var addSvgElementFromJson = this.addSvgElementFromJson = function(data) {
|
2011-02-10 04:10:03 +00:00
|
|
|
var shape = svgedit.utilities.getElem(data.attr.id);
|
2010-11-15 09:25:49 +00:00
|
|
|
// if shape is a path but we need to create a rect/ellipse, then remove the path
|
2011-01-25 16:15:29 +00:00
|
|
|
var current_layer = getCurrentDrawing().getCurrentLayer();
|
2010-11-15 09:25:49 +00:00
|
|
|
if (shape && data.element != shape.tagName) {
|
|
|
|
current_layer.removeChild(shape);
|
|
|
|
shape = null;
|
|
|
|
}
|
|
|
|
if (!shape) {
|
2013-02-16 15:02:26 +00:00
|
|
|
shape = svgdoc.createElementNS(NS.SVG, data.element);
|
2010-11-15 09:25:49 +00:00
|
|
|
if (current_layer) {
|
|
|
|
(current_group || current_layer).appendChild(shape);
|
|
|
|
}
|
|
|
|
}
|
2013-02-15 15:51:58 +00:00
|
|
|
if (data.curStyles) {
|
2011-02-10 04:10:03 +00:00
|
|
|
svgedit.utilities.assignAttributes(shape, {
|
2014-04-08 03:21:22 +00:00
|
|
|
'fill': cur_shape.fill,
|
|
|
|
'stroke': cur_shape.stroke,
|
|
|
|
'stroke-width': cur_shape.stroke_width,
|
|
|
|
'stroke-dasharray': cur_shape.stroke_dasharray,
|
|
|
|
'stroke-linejoin': cur_shape.stroke_linejoin,
|
|
|
|
'stroke-linecap': cur_shape.stroke_linecap,
|
|
|
|
'stroke-opacity': cur_shape.stroke_opacity,
|
|
|
|
'fill-opacity': cur_shape.fill_opacity,
|
|
|
|
'opacity': cur_shape.opacity / 2,
|
|
|
|
'style': 'pointer-events:inherit'
|
2010-11-15 09:25:49 +00:00
|
|
|
}, 100);
|
|
|
|
}
|
2011-02-10 04:10:03 +00:00
|
|
|
svgedit.utilities.assignAttributes(shape, data.attr, 100);
|
|
|
|
svgedit.utilities.cleanupElement(shape);
|
2010-11-15 09:25:49 +00:00
|
|
|
return shape;
|
|
|
|
};
|
|
|
|
|
2010-11-05 17:00:32 +00:00
|
|
|
// import svgtransformlist.js
|
2010-11-12 19:08:29 +00:00
|
|
|
var getTransformList = canvas.getTransformList = svgedit.transformlist.getTransformList;
|
2010-11-05 17:00:32 +00:00
|
|
|
|
|
|
|
// import from math.js.
|
|
|
|
var transformPoint = svgedit.math.transformPoint;
|
2010-11-12 19:08:29 +00:00
|
|
|
var matrixMultiply = canvas.matrixMultiply = svgedit.math.matrixMultiply;
|
|
|
|
var hasMatrixTransform = canvas.hasMatrixTransform = svgedit.math.hasMatrixTransform;
|
|
|
|
var transformListToTransform = canvas.transformListToTransform = svgedit.math.transformListToTransform;
|
2010-11-05 17:00:32 +00:00
|
|
|
var snapToAngle = svgedit.math.snapToAngle;
|
2011-02-09 06:14:47 +00:00
|
|
|
var getMatrix = svgedit.math.getMatrix;
|
2010-11-05 17:00:32 +00:00
|
|
|
|
2010-11-07 16:46:57 +00:00
|
|
|
// initialize from units.js
|
2010-11-07 05:20:03 +00:00
|
|
|
// send in an object implementing the ElementContainer interface (see units.js)
|
|
|
|
svgedit.units.init({
|
|
|
|
getBaseUnit: function() { return curConfig.baseUnit; },
|
2011-02-10 04:10:03 +00:00
|
|
|
getElement: svgedit.utilities.getElem,
|
2014-04-08 03:21:22 +00:00
|
|
|
getHeight: function() { return svgcontent.getAttribute('height')/current_zoom; },
|
|
|
|
getWidth: function() { return svgcontent.getAttribute('width')/current_zoom; },
|
2011-02-02 17:28:43 +00:00
|
|
|
getRoundDigits: function() { return save_options.round_digits; }
|
2010-11-07 05:20:03 +00:00
|
|
|
});
|
2010-11-07 16:46:57 +00:00
|
|
|
// import from units.js
|
2010-11-12 19:08:29 +00:00
|
|
|
var convertToNum = canvas.convertToNum = svgedit.units.convertToNum;
|
2010-11-06 00:45:21 +00:00
|
|
|
|
2010-11-05 17:00:32 +00:00
|
|
|
// import from svgutils.js
|
2010-12-02 17:14:24 +00:00
|
|
|
svgedit.utilities.init({
|
2011-02-04 08:02:46 +00:00
|
|
|
getDOMDocument: function() { return svgdoc; },
|
|
|
|
getDOMContainer: function() { return container; },
|
|
|
|
getSVGRoot: function() { return svgroot; },
|
|
|
|
// TODO: replace this mostly with a way to get the current drawing.
|
2010-12-02 17:14:24 +00:00
|
|
|
getSelectedElements: function() { return selectedElements; },
|
2013-02-17 04:58:04 +00:00
|
|
|
getSVGContent: function() { return svgcontent; },
|
|
|
|
getBaseUnit: function() { return curConfig.baseUnit; },
|
|
|
|
getStepSize: function() { return curConfig.stepSize; }
|
2010-12-02 17:14:24 +00:00
|
|
|
});
|
2013-02-14 15:19:46 +00:00
|
|
|
var findDefs = canvas.findDefs = svgedit.utilities.findDefs;
|
2010-11-12 19:08:29 +00:00
|
|
|
var getUrlFromAttr = canvas.getUrlFromAttr = svgedit.utilities.getUrlFromAttr;
|
|
|
|
var getHref = canvas.getHref = svgedit.utilities.getHref;
|
|
|
|
var setHref = canvas.setHref = svgedit.utilities.setHref;
|
2010-11-05 17:00:32 +00:00
|
|
|
var getPathBBox = svgedit.utilities.getPathBBox;
|
2010-11-12 19:08:29 +00:00
|
|
|
var getBBox = canvas.getBBox = svgedit.utilities.getBBox;
|
|
|
|
var getRotationAngle = canvas.getRotationAngle = svgedit.utilities.getRotationAngle;
|
2011-02-04 08:02:46 +00:00
|
|
|
var getElem = canvas.getElem = svgedit.utilities.getElem;
|
2013-02-14 15:19:46 +00:00
|
|
|
var getRefElem = canvas.getRefElem = svgedit.utilities.getRefElem;
|
2011-02-04 08:02:46 +00:00
|
|
|
var assignAttributes = canvas.assignAttributes = svgedit.utilities.assignAttributes;
|
2011-02-10 04:10:03 +00:00
|
|
|
var cleanupElement = this.cleanupElement = svgedit.utilities.cleanupElement;
|
2010-11-05 17:00:32 +00:00
|
|
|
|
2013-02-17 04:58:04 +00:00
|
|
|
// import from coords.js
|
|
|
|
svgedit.coords.init({
|
|
|
|
getDrawing: function() { return getCurrentDrawing(); },
|
|
|
|
getGridSnapping: function() { return curConfig.gridSnapping; }
|
|
|
|
});
|
|
|
|
var remapElement = this.remapElement = svgedit.coords.remapElement;
|
|
|
|
|
2013-02-20 06:29:25 +00:00
|
|
|
// import from recalculate.js
|
|
|
|
svgedit.recalculate.init({
|
|
|
|
getSVGRoot: function() { return svgroot; },
|
|
|
|
getStartTransform: function() { return startTransform; },
|
|
|
|
setStartTransform: function(transform) { startTransform = transform; }
|
|
|
|
});
|
|
|
|
var recalculateDimensions = this.recalculateDimensions = svgedit.recalculate.recalculateDimensions;
|
|
|
|
|
2010-11-07 20:14:05 +00:00
|
|
|
// import from sanitize.js
|
2013-02-16 15:02:26 +00:00
|
|
|
var nsMap = svgedit.getReverseNS();
|
2010-11-12 19:08:29 +00:00
|
|
|
var sanitizeSvg = canvas.sanitizeSvg = svgedit.sanitize.sanitizeSvg;
|
|
|
|
|
|
|
|
// import from history.js
|
|
|
|
var MoveElementCommand = svgedit.history.MoveElementCommand;
|
|
|
|
var InsertElementCommand = svgedit.history.InsertElementCommand;
|
|
|
|
var RemoveElementCommand = svgedit.history.RemoveElementCommand;
|
|
|
|
var ChangeElementCommand = svgedit.history.ChangeElementCommand;
|
|
|
|
var BatchCommand = svgedit.history.BatchCommand;
|
|
|
|
// Implement the svgedit.history.HistoryEventHandler interface.
|
|
|
|
canvas.undoMgr = new svgedit.history.UndoManager({
|
|
|
|
handleHistoryEvent: function(eventType, cmd) {
|
|
|
|
var EventTypes = svgedit.history.HistoryEventTypes;
|
|
|
|
// TODO: handle setBlurOffsets.
|
|
|
|
if (eventType == EventTypes.BEFORE_UNAPPLY || eventType == EventTypes.BEFORE_APPLY) {
|
|
|
|
canvas.clearSelection();
|
|
|
|
} else if (eventType == EventTypes.AFTER_APPLY || eventType == EventTypes.AFTER_UNAPPLY) {
|
|
|
|
var elems = cmd.elements();
|
|
|
|
canvas.pathActions.clear();
|
2014-04-08 03:21:22 +00:00
|
|
|
call('changed', elems);
|
2010-11-12 19:08:29 +00:00
|
|
|
var cmdType = cmd.type();
|
2011-02-02 17:28:43 +00:00
|
|
|
var isApply = (eventType == EventTypes.AFTER_APPLY);
|
2010-11-12 19:08:29 +00:00
|
|
|
if (cmdType == MoveElementCommand.type()) {
|
|
|
|
var parent = isApply ? cmd.newParent : cmd.oldParent;
|
|
|
|
if (parent == svgcontent) {
|
|
|
|
canvas.identifyLayers();
|
|
|
|
}
|
|
|
|
} else if (cmdType == InsertElementCommand.type() ||
|
|
|
|
cmdType == RemoveElementCommand.type()) {
|
|
|
|
if (cmd.parent == svgcontent) {
|
|
|
|
canvas.identifyLayers();
|
|
|
|
}
|
|
|
|
if (cmdType == InsertElementCommand.type()) {
|
2014-01-31 00:39:35 +00:00
|
|
|
if (isApply) {restoreRefElems(cmd.elem);}
|
2010-11-12 19:08:29 +00:00
|
|
|
} else {
|
2014-01-31 00:39:35 +00:00
|
|
|
if (!isApply) {restoreRefElems(cmd.elem);}
|
2010-11-12 19:08:29 +00:00
|
|
|
}
|
2013-02-15 15:51:58 +00:00
|
|
|
if (cmd.elem.tagName === 'use') {
|
2011-02-02 18:24:44 +00:00
|
|
|
setUseData(cmd.elem);
|
|
|
|
}
|
2010-11-12 19:08:29 +00:00
|
|
|
} else if (cmdType == ChangeElementCommand.type()) {
|
|
|
|
// if we are changing layer names, re-identify all layers
|
2014-04-08 03:21:22 +00:00
|
|
|
if (cmd.elem.tagName == 'title' && cmd.elem.parentNode.parentNode == svgcontent) {
|
2010-11-12 19:08:29 +00:00
|
|
|
canvas.identifyLayers();
|
|
|
|
}
|
|
|
|
var values = isApply ? cmd.newValues : cmd.oldValues;
|
|
|
|
// If stdDeviation was changed, update the blur.
|
2013-02-15 15:51:58 +00:00
|
|
|
if (values.stdDeviation) {
|
|
|
|
canvas.setBlurOffsets(cmd.elem.parentNode, values.stdDeviation);
|
2010-11-12 19:08:29 +00:00
|
|
|
}
|
2012-11-28 05:39:48 +00:00
|
|
|
// This is resolved in later versions of webkit, perhaps we should
|
|
|
|
// have a featured detection for correct 'use' behavior?
|
|
|
|
// ——————————
|
2011-02-04 15:55:47 +00:00
|
|
|
// Remove & Re-add hack for Webkit (issue 775)
|
2013-02-15 15:51:58 +00:00
|
|
|
//if (cmd.elem.tagName === 'use' && svgedit.browser.isWebkit()) {
|
2012-11-28 05:39:48 +00:00
|
|
|
// var elem = cmd.elem;
|
2013-02-15 15:51:58 +00:00
|
|
|
// if (!elem.getAttribute('x') && !elem.getAttribute('y')) {
|
2012-11-28 05:39:48 +00:00
|
|
|
// var parent = elem.parentNode;
|
|
|
|
// var sib = elem.nextSibling;
|
|
|
|
// parent.removeChild(elem);
|
|
|
|
// parent.insertBefore(elem, sib);
|
|
|
|
// }
|
|
|
|
//}
|
2010-11-12 19:08:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
var addCommandToHistory = function(cmd) {
|
|
|
|
canvas.undoMgr.addCommandToHistory(cmd);
|
|
|
|
};
|
|
|
|
|
2010-11-15 09:25:49 +00:00
|
|
|
// import from select.js
|
|
|
|
svgedit.select.init(curConfig, {
|
|
|
|
createSVGElement: function(jsonMap) { return canvas.addSvgElementFromJson(jsonMap); },
|
|
|
|
svgRoot: function() { return svgroot; },
|
|
|
|
svgContent: function() { return svgcontent; },
|
|
|
|
currentZoom: function() { return current_zoom; },
|
|
|
|
// TODO(codedread): Remove when getStrokedBBox() has been put into svgutils.js.
|
|
|
|
getStrokedBBox: function(elems) { return canvas.getStrokedBBox([elems]); }
|
|
|
|
});
|
|
|
|
// this object manages selectors for us
|
|
|
|
var selectorManager = this.selectorManager = svgedit.select.getSelectorManager();
|
2010-11-07 20:14:05 +00:00
|
|
|
|
2011-02-04 08:02:46 +00:00
|
|
|
// Import from path.js
|
|
|
|
svgedit.path.init({
|
2011-02-09 06:14:47 +00:00
|
|
|
getCurrentZoom: function() { return current_zoom; },
|
|
|
|
getSVGRoot: function() { return svgroot; }
|
2011-02-04 08:02:46 +00:00
|
|
|
});
|
|
|
|
|
2010-11-15 09:24:02 +00:00
|
|
|
// Interface strings, usually for title elements
|
|
|
|
var uiStrings = {
|
2014-04-08 03:21:22 +00:00
|
|
|
exportNoBlur: "Blurred elements will appear as un-blurred",
|
|
|
|
exportNoforeignObject: "foreignObject elements will not appear",
|
|
|
|
exportNoDashArray: "Strokes will appear filled",
|
|
|
|
exportNoText: "Text may not appear as expected"
|
2010-11-15 09:24:02 +00:00
|
|
|
};
|
|
|
|
|
2010-11-06 00:45:21 +00:00
|
|
|
var visElems = 'a,circle,ellipse,foreignObject,g,image,line,path,polygon,polyline,rect,svg,text,tspan,use';
|
2014-04-08 03:21:22 +00:00
|
|
|
var ref_attrs = ['clip-path', 'fill', 'filter', 'marker-end', 'marker-mid', 'marker-start', 'mask', 'stroke'];
|
2010-06-30 18:27:36 +00:00
|
|
|
|
2010-09-10 18:51:59 +00:00
|
|
|
var elData = $.data;
|
2010-11-12 19:08:29 +00:00
|
|
|
|
2010-06-21 15:05:41 +00:00
|
|
|
// Animation element to change the opacity of any newly created element
|
2013-02-16 15:02:26 +00:00
|
|
|
var opac_ani = document.createElementNS(NS.SVG, 'animate');
|
2010-06-21 15:05:41 +00:00
|
|
|
$(opac_ani).attr({
|
|
|
|
attributeName: 'opacity',
|
|
|
|
begin: 'indefinite',
|
|
|
|
dur: 1,
|
|
|
|
fill: 'freeze'
|
|
|
|
}).appendTo(svgroot);
|
|
|
|
|
2010-10-08 16:34:24 +00:00
|
|
|
var restoreRefElems = function(elem) {
|
|
|
|
// Look for missing reference elements, restore any found
|
2014-01-31 00:39:35 +00:00
|
|
|
var o, i,
|
|
|
|
attrs = $(elem).attr(ref_attrs);
|
|
|
|
for (o in attrs) {
|
2010-10-08 16:34:24 +00:00
|
|
|
var val = attrs[o];
|
|
|
|
if (val && val.indexOf('url(') === 0) {
|
2013-02-14 15:19:46 +00:00
|
|
|
var id = svgedit.utilities.getUrlFromAttr(val).substr(1);
|
2010-10-08 16:34:24 +00:00
|
|
|
var ref = getElem(id);
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!ref) {
|
2013-02-14 15:19:46 +00:00
|
|
|
svgedit.utilities.findDefs().appendChild(removedElements[id]);
|
2010-10-08 16:34:24 +00:00
|
|
|
delete removedElements[id];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-02-02 19:36:49 +00:00
|
|
|
|
|
|
|
var childs = elem.getElementsByTagName('*');
|
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (childs.length) {
|
2014-01-31 00:39:35 +00:00
|
|
|
for (i = 0, l = childs.length; i < l; i++) {
|
2011-02-02 19:36:49 +00:00
|
|
|
restoreRefElems(childs[i]);
|
|
|
|
}
|
|
|
|
}
|
2010-11-12 19:08:29 +00:00
|
|
|
};
|
2010-06-21 18:06:21 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
(function() {
|
|
|
|
// TODO For Issue 208: this is a start on a thumbnail
|
2014-04-08 03:21:22 +00:00
|
|
|
// var svgthumb = svgdoc.createElementNS(NS.SVG, 'use');
|
2010-06-22 14:52:51 +00:00
|
|
|
// svgthumb.setAttribute('width', '100');
|
|
|
|
// svgthumb.setAttribute('height', '100');
|
2010-11-07 16:46:57 +00:00
|
|
|
// svgedit.utilities.setHref(svgthumb, '#svgcontent');
|
2010-06-22 14:52:51 +00:00
|
|
|
// svgroot.appendChild(svgthumb);
|
|
|
|
|
2014-01-31 10:40:52 +00:00
|
|
|
}());
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2011-01-16 16:28:26 +00:00
|
|
|
// Object to contain image data for raster images that were found encodable
|
|
|
|
var encodableImages = {},
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
// String with image URL of last loadable image
|
|
|
|
last_good_img_url = curConfig.imgPath + 'logo.png',
|
|
|
|
|
2010-09-22 18:42:24 +00:00
|
|
|
// Array with current disabled elements (for in-group editing)
|
|
|
|
disabled_elems = [],
|
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// Object with save options
|
|
|
|
save_options = {round_digits: 5},
|
|
|
|
|
|
|
|
// Boolean indicating whether or not a draw action has been started
|
|
|
|
started = false,
|
|
|
|
|
|
|
|
// String with an element's initial transform attribute value
|
2013-02-20 06:29:25 +00:00
|
|
|
startTransform = null,
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
// String indicating the current editor mode
|
2014-04-08 03:21:22 +00:00
|
|
|
current_mode = 'select',
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
// String with the current direction in which an element is being resized
|
2014-04-08 03:21:22 +00:00
|
|
|
current_resize_mode = 'none',
|
2010-12-01 21:20:52 +00:00
|
|
|
|
|
|
|
// Object with IDs for imported files, to see if one was already added
|
2013-02-15 15:51:58 +00:00
|
|
|
import_ids = {},
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
// Current text style properties
|
|
|
|
cur_text = all_properties.text,
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
// Current general properties
|
|
|
|
cur_properties = cur_shape,
|
|
|
|
|
|
|
|
// Array with selected elements' Bounding box object
|
2011-02-24 16:13:26 +00:00
|
|
|
// selectedBBoxes = new Array(1),
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
// The DOM element that was just selected
|
|
|
|
justSelected = null,
|
2009-08-31 14:54:59 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// DOM element for selection rectangle drawn by the user
|
|
|
|
rubberBox = null,
|
|
|
|
|
|
|
|
// Array of current BBoxes (still needed?)
|
|
|
|
curBBoxes = [],
|
|
|
|
|
|
|
|
// Object to contain all included extensions
|
2010-08-17 18:09:50 +00:00
|
|
|
extensions = {},
|
|
|
|
|
|
|
|
// Canvas point for the most recent right click
|
2010-10-08 16:34:24 +00:00
|
|
|
lastClickPoint = null,
|
|
|
|
|
|
|
|
// Map of deleted reference elements
|
2013-02-15 15:51:58 +00:00
|
|
|
removedElements = {};
|
2010-08-17 18:09:50 +00:00
|
|
|
|
|
|
|
// Clipboard for cut, copy&pasted elements
|
|
|
|
canvas.clipBoard = [];
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
// Should this return an array by default, so extension results aren't overwritten?
|
|
|
|
var runExtensions = this.runExtensions = function(action, vars, returnArray) {
|
2013-02-15 16:51:48 +00:00
|
|
|
var result = returnArray ? [] : false;
|
2010-06-22 14:52:51 +00:00
|
|
|
$.each(extensions, function(name, opts) {
|
2014-02-18 15:06:27 +00:00
|
|
|
if (opts && action in opts) {
|
2013-02-15 15:51:58 +00:00
|
|
|
if (returnArray) {
|
|
|
|
result.push(opts[action](vars));
|
2010-06-22 14:52:51 +00:00
|
|
|
} else {
|
|
|
|
result = opts[action](vars);
|
|
|
|
}
|
|
|
|
}
|
2009-08-31 14:54:59 +00:00
|
|
|
});
|
2010-06-22 14:52:51 +00:00
|
|
|
return result;
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-06-30 18:27:36 +00:00
|
|
|
|
|
|
|
// Function: addExtension
|
|
|
|
// Add an extension to the editor
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// name - String with the ID of the extension
|
|
|
|
// ext_func - Function supplied by the extension with its data
|
|
|
|
this.addExtension = function(name, ext_func) {
|
2014-01-31 10:40:52 +00:00
|
|
|
var ext;
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!(name in extensions)) {
|
2010-06-30 18:27:36 +00:00
|
|
|
// Provide private vars/funcs here. Is there a better way to do this?
|
2013-02-15 15:51:58 +00:00
|
|
|
if ($.isFunction(ext_func)) {
|
2014-01-31 10:40:52 +00:00
|
|
|
ext = ext_func($.extend(canvas.getPrivateMethods(), {
|
2013-02-15 15:51:58 +00:00
|
|
|
svgroot: svgroot,
|
|
|
|
svgcontent: svgcontent,
|
|
|
|
nonce: getCurrentDrawing().getNonce(),
|
|
|
|
selectorManager: selectorManager
|
|
|
|
}));
|
2010-07-01 20:14:12 +00:00
|
|
|
} else {
|
2014-01-31 10:40:52 +00:00
|
|
|
ext = ext_func;
|
2010-07-01 20:14:12 +00:00
|
|
|
}
|
2010-06-30 18:27:36 +00:00
|
|
|
extensions[name] = ext;
|
2014-04-08 03:21:22 +00:00
|
|
|
call('extension_added', ext);
|
2010-06-30 18:27:36 +00:00
|
|
|
} else {
|
2014-04-08 03:21:22 +00:00
|
|
|
console.log('Cannot add extension "' + name + '", an extension by that name already exists.');
|
2010-06-30 18:27:36 +00:00
|
|
|
}
|
|
|
|
};
|
2010-06-22 18:17:42 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// This method rounds the incoming value to the nearest value based on the current_zoom
|
|
|
|
var round = this.round = function(val) {
|
2013-02-15 15:51:58 +00:00
|
|
|
return parseInt(val*current_zoom, 10)/current_zoom;
|
2010-06-22 14:52:51 +00:00
|
|
|
};
|
2009-08-31 14:54:59 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// This method sends back an array or a NodeList full of elements that
|
|
|
|
// intersect the multi-select rubber-band-box on the current_layer only.
|
|
|
|
//
|
|
|
|
// Since the only browser that supports the SVG DOM getIntersectionList is Opera,
|
2014-01-31 10:40:52 +00:00
|
|
|
// we need to provide an implementation here. We brute-force it for now.
|
2010-06-22 14:52:51 +00:00
|
|
|
//
|
|
|
|
// Reference:
|
|
|
|
// Firefox does not implement getIntersectionList(), see https://bugzilla.mozilla.org/show_bug.cgi?id=501421
|
|
|
|
// Webkit does not implement getIntersectionList(), see https://bugs.webkit.org/show_bug.cgi?id=11274
|
|
|
|
var getIntersectionList = this.getIntersectionList = function(rect) {
|
|
|
|
if (rubberBox == null) { return null; }
|
|
|
|
|
2011-01-25 16:15:29 +00:00
|
|
|
var parent = current_group || getCurrentDrawing().getCurrentLayer();
|
2010-09-22 18:42:24 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!curBBoxes.length) {
|
2010-06-22 14:52:51 +00:00
|
|
|
// Cache all bboxes
|
2010-11-14 19:01:00 +00:00
|
|
|
curBBoxes = getVisibleElementsAndBBoxes(parent);
|
2010-01-25 21:22:03 +00:00
|
|
|
}
|
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
var resultList = null;
|
|
|
|
try {
|
2010-09-22 18:42:24 +00:00
|
|
|
resultList = parent.getIntersectionList(rect, null);
|
2010-06-22 14:52:51 +00:00
|
|
|
} catch(e) { }
|
2009-09-17 14:47:54 +00:00
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
if (resultList == null || typeof(resultList.item) != 'function') {
|
2010-06-22 14:52:51 +00:00
|
|
|
resultList = [];
|
2014-02-11 14:02:48 +00:00
|
|
|
var rubberBBox;
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!rect) {
|
2014-02-11 14:02:48 +00:00
|
|
|
rubberBBox = rubberBox.getBBox();
|
2014-01-31 00:39:35 +00:00
|
|
|
var o,
|
|
|
|
bb = {};
|
2010-09-23 19:53:43 +00:00
|
|
|
|
2014-01-31 00:39:35 +00:00
|
|
|
for (o in rubberBBox) {
|
2010-09-23 19:53:43 +00:00
|
|
|
bb[o] = rubberBBox[o] / current_zoom;
|
|
|
|
}
|
2010-08-23 20:16:27 +00:00
|
|
|
rubberBBox = bb;
|
|
|
|
|
2010-08-20 14:52:55 +00:00
|
|
|
} else {
|
2014-02-11 14:02:48 +00:00
|
|
|
rubberBBox = rect;
|
2010-08-20 14:52:55 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
var i = curBBoxes.length;
|
|
|
|
while (i--) {
|
2014-01-31 10:40:52 +00:00
|
|
|
if (!rubberBBox.width) {continue;}
|
|
|
|
if (svgedit.math.rectsIntersect(rubberBBox, curBBoxes[i].bbox)) {
|
2010-06-22 14:52:51 +00:00
|
|
|
resultList.push(curBBoxes[i].elem);
|
|
|
|
}
|
2009-07-01 15:34:15 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
// addToSelection expects an array, but it's ok to pass a NodeList
|
|
|
|
// because using square-bracket notation is allowed:
|
|
|
|
// http://www.w3.org/TR/DOM-Level-2-Core/ecma-script-binding.html
|
|
|
|
return resultList;
|
|
|
|
};
|
2009-08-26 19:59:52 +00:00
|
|
|
|
2010-11-05 15:29:30 +00:00
|
|
|
// TODO(codedread): Migrate this into svgutils.js
|
2010-06-30 18:27:36 +00:00
|
|
|
// Function: getStrokedBBox
|
|
|
|
// Get the bounding box for one or more stroked and/or transformed elements
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// elems - Array with DOM elements to check
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// A single bounding box object
|
2010-11-15 09:25:49 +00:00
|
|
|
getStrokedBBox = this.getStrokedBBox = function(elems) {
|
2014-01-31 00:39:35 +00:00
|
|
|
if (!elems) {elems = getVisibleElements();}
|
|
|
|
if (!elems.length) {return false;}
|
2010-06-30 18:27:36 +00:00
|
|
|
// Make sure the expected BBox is returned if the element is a group
|
|
|
|
var getCheckedBBox = function(elem) {
|
2013-02-15 23:05:23 +00:00
|
|
|
|
2010-06-30 18:27:36 +00:00
|
|
|
try {
|
|
|
|
// TODO: Fix issue with rotated groups. Currently they work
|
|
|
|
// fine in FF, but not in other browsers (same problem mentioned
|
|
|
|
// in Issue 339 comment #2).
|
2013-02-15 23:05:23 +00:00
|
|
|
|
2010-11-08 07:23:26 +00:00
|
|
|
var bb = svgedit.utilities.getBBox(elem);
|
2010-11-07 16:46:57 +00:00
|
|
|
var angle = svgedit.utilities.getRotationAngle(elem);
|
2013-02-15 16:51:48 +00:00
|
|
|
|
2010-11-08 07:23:26 +00:00
|
|
|
if ((angle && angle % 90) ||
|
2013-02-15 16:51:48 +00:00
|
|
|
svgedit.math.hasMatrixTransform(svgedit.transformlist.getTransformList(elem))) {
|
2010-06-30 18:27:36 +00:00
|
|
|
// Accurate way to get BBox of rotated element in Firefox:
|
|
|
|
// Put element in group and get its BBox
|
|
|
|
var good_bb = false;
|
|
|
|
// Get the BBox from the raw path for these elements
|
2013-02-15 16:51:48 +00:00
|
|
|
var elemNames = ['ellipse', 'path', 'line', 'polyline', 'polygon'];
|
2013-02-15 15:51:58 +00:00
|
|
|
if (elemNames.indexOf(elem.tagName) >= 0) {
|
2010-06-30 18:27:36 +00:00
|
|
|
bb = good_bb = canvas.convertToPath(elem, true);
|
2013-02-15 15:51:58 +00:00
|
|
|
} else if (elem.tagName == 'rect') {
|
2010-06-30 18:27:36 +00:00
|
|
|
// Look for radius
|
|
|
|
var rx = elem.getAttribute('rx');
|
|
|
|
var ry = elem.getAttribute('ry');
|
2013-02-15 15:51:58 +00:00
|
|
|
if (rx || ry) {
|
2010-06-30 18:27:36 +00:00
|
|
|
bb = good_bb = canvas.convertToPath(elem, true);
|
|
|
|
}
|
|
|
|
}
|
2013-02-15 23:05:23 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!good_bb) {
|
2010-10-13 19:36:47 +00:00
|
|
|
// Must use clone else FF freaks out
|
2013-02-15 23:05:23 +00:00
|
|
|
var clone = elem.cloneNode(true);
|
2014-04-08 03:21:22 +00:00
|
|
|
var g = document.createElementNS(NS.SVG, 'g');
|
2010-06-30 18:27:36 +00:00
|
|
|
var parent = elem.parentNode;
|
2010-10-13 19:36:47 +00:00
|
|
|
parent.appendChild(g);
|
|
|
|
g.appendChild(clone);
|
2010-11-05 17:00:32 +00:00
|
|
|
bb = svgedit.utilities.bboxToObj(g.getBBox());
|
2010-06-30 18:27:36 +00:00
|
|
|
parent.removeChild(g);
|
|
|
|
}
|
2013-02-15 23:05:23 +00:00
|
|
|
|
2010-06-30 18:27:36 +00:00
|
|
|
// Old method: Works by giving the rotated BBox,
|
|
|
|
// this is (unfortunately) what Opera and Safari do
|
|
|
|
// natively when getting the BBox of the parent group
|
2014-01-31 00:39:35 +00:00
|
|
|
// var angle = angle * Math.PI / 180.0;
|
|
|
|
// var rminx = Number.MAX_VALUE, rminy = Number.MAX_VALUE,
|
|
|
|
// rmaxx = Number.MIN_VALUE, rmaxy = Number.MIN_VALUE;
|
|
|
|
// var cx = round(bb.x + bb.width/2),
|
|
|
|
// cy = round(bb.y + bb.height/2);
|
|
|
|
// var pts = [ [bb.x - cx, bb.y - cy],
|
|
|
|
// [bb.x + bb.width - cx, bb.y - cy],
|
|
|
|
// [bb.x + bb.width - cx, bb.y + bb.height - cy],
|
|
|
|
// [bb.x - cx, bb.y + bb.height - cy] ];
|
|
|
|
// var j = 4;
|
|
|
|
// while (j--) {
|
|
|
|
// var x = pts[j][0],
|
|
|
|
// y = pts[j][1],
|
|
|
|
// r = Math.sqrt( x*x + y*y );
|
|
|
|
// var theta = Math.atan2(y,x) + angle;
|
|
|
|
// x = round(r * Math.cos(theta) + cx);
|
|
|
|
// y = round(r * Math.sin(theta) + cy);
|
|
|
|
//
|
|
|
|
// // now set the bbox for the shape after it's been rotated
|
|
|
|
// if (x < rminx) rminx = x;
|
|
|
|
// if (y < rminy) rminy = y;
|
|
|
|
// if (x > rmaxx) rmaxx = x;
|
|
|
|
// if (y > rmaxy) rmaxy = y;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// bb.x = rminx;
|
|
|
|
// bb.y = rminy;
|
|
|
|
// bb.width = rmaxx - rminx;
|
|
|
|
// bb.height = rmaxy - rminy;
|
2010-06-30 18:27:36 +00:00
|
|
|
}
|
2010-09-01 12:41:25 +00:00
|
|
|
return bb;
|
2013-02-15 23:05:23 +00:00
|
|
|
} catch(e) {
|
2010-06-30 18:27:36 +00:00
|
|
|
console.log(elem, e);
|
|
|
|
return null;
|
2013-02-15 23:05:23 +00:00
|
|
|
}
|
2010-11-12 19:08:29 +00:00
|
|
|
};
|
2009-06-25 02:54:07 +00:00
|
|
|
|
2010-06-30 18:27:36 +00:00
|
|
|
var full_bb;
|
|
|
|
$.each(elems, function() {
|
2014-01-31 00:39:35 +00:00
|
|
|
if (full_bb) {return;}
|
|
|
|
if (!this.parentNode) {return;}
|
2010-06-30 18:27:36 +00:00
|
|
|
full_bb = getCheckedBBox(this);
|
|
|
|
});
|
2013-02-15 23:05:23 +00:00
|
|
|
|
2010-06-30 18:27:36 +00:00
|
|
|
// This shouldn't ever happen...
|
2014-01-31 00:39:35 +00:00
|
|
|
if (full_bb == null) {return null;}
|
2013-02-15 23:05:23 +00:00
|
|
|
|
2010-06-30 18:27:36 +00:00
|
|
|
// full_bb doesn't include the stoke, so this does no good!
|
2014-01-31 00:39:35 +00:00
|
|
|
// if (elems.length == 1) return full_bb;
|
2013-02-15 23:05:23 +00:00
|
|
|
|
2010-06-30 18:27:36 +00:00
|
|
|
var max_x = full_bb.x + full_bb.width;
|
|
|
|
var max_y = full_bb.y + full_bb.height;
|
|
|
|
var min_x = full_bb.x;
|
|
|
|
var min_y = full_bb.y;
|
2013-02-15 23:05:23 +00:00
|
|
|
|
2010-06-30 18:27:36 +00:00
|
|
|
// FIXME: same re-creation problem with this function as getCheckedBBox() above
|
|
|
|
var getOffset = function(elem) {
|
2014-04-08 03:21:22 +00:00
|
|
|
var sw = elem.getAttribute('stroke-width');
|
2010-06-30 18:27:36 +00:00
|
|
|
var offset = 0;
|
2014-04-08 03:21:22 +00:00
|
|
|
if (elem.getAttribute('stroke') != 'none' && !isNaN(sw)) {
|
2010-06-30 18:27:36 +00:00
|
|
|
offset += sw/2;
|
|
|
|
}
|
|
|
|
return offset;
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-06-30 18:27:36 +00:00
|
|
|
var bboxes = [];
|
|
|
|
$.each(elems, function(i, elem) {
|
|
|
|
var cur_bb = getCheckedBBox(elem);
|
2013-02-15 15:51:58 +00:00
|
|
|
if (cur_bb) {
|
2010-06-30 18:27:36 +00:00
|
|
|
var offset = getOffset(elem);
|
|
|
|
min_x = Math.min(min_x, cur_bb.x - offset);
|
|
|
|
min_y = Math.min(min_y, cur_bb.y - offset);
|
|
|
|
bboxes.push(cur_bb);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
full_bb.x = min_x;
|
|
|
|
full_bb.y = min_y;
|
|
|
|
|
|
|
|
$.each(elems, function(i, elem) {
|
|
|
|
var cur_bb = bboxes[i];
|
|
|
|
// ensure that elem is really an element node
|
|
|
|
if (cur_bb && elem.nodeType == 1) {
|
|
|
|
var offset = getOffset(elem);
|
|
|
|
max_x = Math.max(max_x, cur_bb.x + cur_bb.width + offset);
|
|
|
|
max_y = Math.max(max_y, cur_bb.y + cur_bb.height + offset);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
full_bb.width = max_x - min_x;
|
|
|
|
full_bb.height = max_y - min_y;
|
|
|
|
return full_bb;
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-06-30 18:27:36 +00:00
|
|
|
|
|
|
|
// Function: getVisibleElements
|
|
|
|
// Get all elements that have a BBox (excludes <defs>, <title>, etc).
|
|
|
|
// Note that 0-opacity, off-screen etc elements are still considered "visible"
|
|
|
|
// for this function
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// parent - The parent DOM element to search within
|
|
|
|
//
|
|
|
|
// Returns:
|
2010-11-14 19:01:00 +00:00
|
|
|
// An array with all "visible" elements.
|
|
|
|
var getVisibleElements = this.getVisibleElements = function(parent) {
|
2014-01-31 00:39:35 +00:00
|
|
|
if (!parent) {
|
|
|
|
parent = $(svgcontent).children(); // Prevent layers from being included
|
|
|
|
}
|
2010-11-14 19:01:00 +00:00
|
|
|
|
|
|
|
var contentElems = [];
|
|
|
|
$(parent).children().each(function(i, elem) {
|
|
|
|
try {
|
|
|
|
if (elem.getBBox()) {
|
|
|
|
contentElems.push(elem);
|
|
|
|
}
|
|
|
|
} catch(e) {}
|
|
|
|
});
|
|
|
|
return contentElems.reverse();
|
|
|
|
};
|
|
|
|
|
|
|
|
// Function: getVisibleElementsAndBBoxes
|
|
|
|
// Get all elements that have a BBox (excludes <defs>, <title>, etc).
|
|
|
|
// Note that 0-opacity, off-screen etc elements are still considered "visible"
|
|
|
|
// for this function
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// parent - The parent DOM element to search within
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// An array with objects that include:
|
2010-06-30 18:27:36 +00:00
|
|
|
// * elem - The element
|
|
|
|
// * bbox - The element's BBox as retrieved from getStrokedBBox
|
2010-11-14 19:01:00 +00:00
|
|
|
var getVisibleElementsAndBBoxes = this.getVisibleElementsAndBBoxes = function(parent) {
|
2014-01-31 00:39:35 +00:00
|
|
|
if (!parent) {
|
|
|
|
parent = $(svgcontent).children(); // Prevent layers from being included
|
|
|
|
}
|
2010-06-30 18:27:36 +00:00
|
|
|
var contentElems = [];
|
|
|
|
$(parent).children().each(function(i, elem) {
|
|
|
|
try {
|
2010-11-14 19:01:00 +00:00
|
|
|
if (elem.getBBox()) {
|
|
|
|
contentElems.push({'elem':elem, 'bbox':getStrokedBBox([elem])});
|
2010-06-30 18:27:36 +00:00
|
|
|
}
|
|
|
|
} catch(e) {}
|
|
|
|
});
|
|
|
|
return contentElems.reverse();
|
2010-11-14 19:01:00 +00:00
|
|
|
};
|
2010-06-30 18:27:36 +00:00
|
|
|
|
2010-07-16 15:46:54 +00:00
|
|
|
// Function: groupSvgElem
|
|
|
|
// Wrap an SVG element into a group element, mark the group as 'gsvg'
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// elem - SVG element to wrap
|
|
|
|
var groupSvgElem = this.groupSvgElem = function(elem) {
|
2014-04-08 03:21:22 +00:00
|
|
|
var g = document.createElementNS(NS.SVG, 'g');
|
2010-07-16 15:46:54 +00:00
|
|
|
elem.parentNode.replaceChild(g, elem);
|
|
|
|
$(g).append(elem).data('gsvg', elem)[0].id = getNextId();
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-07-16 15:46:54 +00:00
|
|
|
|
2010-06-30 18:27:36 +00:00
|
|
|
// Function: copyElem
|
|
|
|
// Create a clone of an element, updating its ID and its children's IDs when needed
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// el - DOM element to clone
|
|
|
|
//
|
|
|
|
// Returns: The cloned element
|
|
|
|
var copyElem = function(el) {
|
|
|
|
// manually create a copy of the element
|
|
|
|
var new_el = document.createElementNS(el.namespaceURI, el.nodeName);
|
|
|
|
$.each(el.attributes, function(i, attr) {
|
|
|
|
if (attr.localName != '-moz-math-font-style') {
|
|
|
|
new_el.setAttributeNS(attr.namespaceURI, attr.nodeName, attr.nodeValue);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// set the copied element's new id
|
2014-04-08 03:21:22 +00:00
|
|
|
new_el.removeAttribute('id');
|
2010-06-30 18:27:36 +00:00
|
|
|
new_el.id = getNextId();
|
|
|
|
|
|
|
|
// Opera's "d" value needs to be reset for Opera/Win/non-EN
|
|
|
|
// Also needed for webkit (else does not keep curved segments on clone)
|
2013-02-15 15:51:58 +00:00
|
|
|
if (svgedit.browser.isWebkit() && el.nodeName == 'path') {
|
2010-06-30 18:27:36 +00:00
|
|
|
var fixed_d = pathActions.convertPath(el);
|
|
|
|
new_el.setAttribute('d', fixed_d);
|
|
|
|
}
|
|
|
|
|
|
|
|
// now create copies of all children
|
|
|
|
$.each(el.childNodes, function(i, child) {
|
|
|
|
switch(child.nodeType) {
|
|
|
|
case 1: // element node
|
|
|
|
new_el.appendChild(copyElem(child));
|
|
|
|
break;
|
|
|
|
case 3: // text node
|
|
|
|
new_el.textContent = child.nodeValue;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
2010-07-16 15:46:54 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if ($(el).data('gsvg')) {
|
2010-07-16 15:46:54 +00:00
|
|
|
$(new_el).data('gsvg', new_el.firstChild);
|
2013-02-15 15:51:58 +00:00
|
|
|
} else if ($(el).data('symbol')) {
|
2010-07-29 15:09:49 +00:00
|
|
|
var ref = $(el).data('symbol');
|
|
|
|
$(new_el).data('ref', ref).data('symbol', ref);
|
2013-02-15 15:51:58 +00:00
|
|
|
} else if (new_el.tagName == 'image') {
|
2010-06-30 18:27:36 +00:00
|
|
|
preventClickDefault(new_el);
|
|
|
|
}
|
|
|
|
return new_el;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Set scope for these functions
|
2010-10-29 13:13:34 +00:00
|
|
|
var getId, getNextId, call;
|
2014-02-11 13:32:40 +00:00
|
|
|
var textActions, pathActions;
|
2010-06-30 18:27:36 +00:00
|
|
|
|
|
|
|
(function(c) {
|
|
|
|
|
|
|
|
// Object to contain editor event names and callback functions
|
|
|
|
var events = {};
|
|
|
|
|
2011-01-25 16:15:29 +00:00
|
|
|
getId = c.getId = function() { return getCurrentDrawing().getId(); };
|
|
|
|
getNextId = c.getNextId = function() { return getCurrentDrawing().getNextId(); };
|
2013-02-15 23:05:23 +00:00
|
|
|
|
2010-09-23 19:53:43 +00:00
|
|
|
// Function: call
|
2010-06-30 18:27:36 +00:00
|
|
|
// Run the callback function associated with the given event
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// event - String with the event name
|
|
|
|
// arg - Argument to pass through to the callback function
|
|
|
|
call = c.call = function(event, arg) {
|
|
|
|
if (events[event]) {
|
2010-11-12 19:08:29 +00:00
|
|
|
return events[event](this, arg);
|
2010-06-30 18:27:36 +00:00
|
|
|
}
|
|
|
|
};
|
2013-02-15 23:05:23 +00:00
|
|
|
|
2010-06-30 18:27:36 +00:00
|
|
|
// Function: bind
|
|
|
|
// Attaches a callback function to an event
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// event - String indicating the name of the event
|
|
|
|
// f - The callback function to bind to the event
|
2013-02-15 23:05:23 +00:00
|
|
|
//
|
2010-06-30 18:27:36 +00:00
|
|
|
// Return:
|
|
|
|
// The previous event
|
|
|
|
c.bind = function(event, f) {
|
2013-02-15 15:51:58 +00:00
|
|
|
var old = events[event];
|
2010-06-30 18:27:36 +00:00
|
|
|
events[event] = f;
|
|
|
|
return old;
|
|
|
|
};
|
2013-02-15 23:05:23 +00:00
|
|
|
|
2010-06-30 18:27:36 +00:00
|
|
|
}(canvas));
|
|
|
|
|
2010-11-07 20:14:05 +00:00
|
|
|
// Function: canvas.prepareSvg
|
|
|
|
// Runs the SVG Document through the sanitizer and then updates its paths.
|
2010-06-22 14:52:51 +00:00
|
|
|
//
|
|
|
|
// Parameters:
|
2010-11-07 20:14:05 +00:00
|
|
|
// newDoc - The SVG DOM document
|
|
|
|
this.prepareSvg = function(newDoc) {
|
|
|
|
this.sanitizeSvg(newDoc.documentElement);
|
2010-04-28 16:53:36 +00:00
|
|
|
|
2010-11-07 20:14:05 +00:00
|
|
|
// convert paths into absolute commands
|
2014-02-11 13:32:40 +00:00
|
|
|
var i, path, len,
|
2014-04-08 03:21:22 +00:00
|
|
|
paths = newDoc.getElementsByTagNameNS(NS.SVG, 'path');
|
2014-01-31 00:39:35 +00:00
|
|
|
for (i = 0, len = paths.length; i < len; ++i) {
|
|
|
|
path = paths[i];
|
2010-11-07 20:14:05 +00:00
|
|
|
path.setAttribute('d', pathActions.convertPath(path));
|
|
|
|
pathActions.fixEnd(path);
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
};
|
2010-04-07 18:18:28 +00:00
|
|
|
|
2010-06-29 20:43:44 +00:00
|
|
|
// Function: ffClone
|
2011-01-31 20:02:24 +00:00
|
|
|
// Hack for Firefox bugs where text element features aren't updated or get
|
|
|
|
// messed up. See issue 136 and issue 137.
|
2010-06-29 20:43:44 +00:00
|
|
|
// This function clones the element and re-selects it
|
|
|
|
// TODO: Test for this bug on load and add it to "support" object instead of
|
|
|
|
// browser sniffing
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// elem - The (text) DOM element to clone
|
|
|
|
var ffClone = function(elem) {
|
2014-01-31 00:39:35 +00:00
|
|
|
if (!svgedit.browser.isGecko()) {return elem;}
|
2013-02-15 15:51:58 +00:00
|
|
|
var clone = elem.cloneNode(true);
|
2010-06-29 20:43:44 +00:00
|
|
|
elem.parentNode.insertBefore(clone, elem);
|
|
|
|
elem.parentNode.removeChild(elem);
|
|
|
|
selectorManager.releaseSelector(elem);
|
|
|
|
selectedElements[0] = clone;
|
|
|
|
selectorManager.requestSelector(clone).showGrips(true);
|
|
|
|
return clone;
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-06-29 20:43:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
// this.each is deprecated, if any extension used this it can be recreated by doing this:
|
|
|
|
// $(canvas.getRootElem()).children().each(...)
|
|
|
|
|
|
|
|
// this.each = function(cb) {
|
2014-01-31 00:39:35 +00:00
|
|
|
// $(svgroot).children().each(cb);
|
2010-06-29 20:43:44 +00:00
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
// Function: setRotationAngle
|
|
|
|
// Removes any old rotations if present, prepends a new rotation at the
|
|
|
|
// transformed center
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// val - The new rotation angle in degrees
|
|
|
|
// preventUndo - Boolean indicating whether the action should be undoable or not
|
|
|
|
this.setRotationAngle = function(val, preventUndo) {
|
|
|
|
// ensure val is the proper type
|
|
|
|
val = parseFloat(val);
|
|
|
|
var elem = selectedElements[0];
|
2014-04-08 03:21:22 +00:00
|
|
|
var oldTransform = elem.getAttribute('transform');
|
2011-02-24 16:13:26 +00:00
|
|
|
var bbox = svgedit.utilities.getBBox(elem);
|
2010-06-29 20:43:44 +00:00
|
|
|
var cx = bbox.x+bbox.width/2, cy = bbox.y+bbox.height/2;
|
2013-02-14 15:19:46 +00:00
|
|
|
var tlist = svgedit.transformlist.getTransformList(elem);
|
2010-06-29 20:43:44 +00:00
|
|
|
|
|
|
|
// only remove the real rotational transform if present (i.e. at index=0)
|
|
|
|
if (tlist.numberOfItems > 0) {
|
|
|
|
var xform = tlist.getItem(0);
|
|
|
|
if (xform.type == 4) {
|
|
|
|
tlist.removeItem(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// find R_nc and insert it
|
|
|
|
if (val != 0) {
|
2013-02-15 16:51:48 +00:00
|
|
|
var center = svgedit.math.transformPoint(cx, cy, svgedit.math.transformListToTransform(tlist).matrix);
|
2010-06-29 20:43:44 +00:00
|
|
|
var R_nc = svgroot.createSVGTransform();
|
|
|
|
R_nc.setRotate(val, center.x, center.y);
|
2013-02-15 15:51:58 +00:00
|
|
|
if (tlist.numberOfItems) {
|
2010-08-30 17:17:57 +00:00
|
|
|
tlist.insertItemBefore(R_nc, 0);
|
|
|
|
} else {
|
|
|
|
tlist.appendItem(R_nc);
|
|
|
|
}
|
2013-02-15 15:51:58 +00:00
|
|
|
} else if (tlist.numberOfItems == 0) {
|
2014-04-08 03:21:22 +00:00
|
|
|
elem.removeAttribute('transform');
|
2010-06-29 20:43:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!preventUndo) {
|
|
|
|
// we need to undo it, then redo it so it can be undo-able! :)
|
|
|
|
// TODO: figure out how to make changes to transform list undo-able cross-browser?
|
2014-04-08 03:21:22 +00:00
|
|
|
var newTransform = elem.getAttribute('transform');
|
|
|
|
elem.setAttribute('transform', oldTransform);
|
|
|
|
changeSelectedAttribute('transform', newTransform, selectedElements);
|
|
|
|
call('changed', selectedElements);
|
|
|
|
}
|
|
|
|
var pointGripContainer = svgedit.utilities.getElem('pathpointgrip_container');
|
|
|
|
// if (elem.nodeName == 'path' && pointGripContainer) {
|
|
|
|
// pathActions.setPointContainerTransform(elem.getAttribute('transform'));
|
2014-01-31 00:39:35 +00:00
|
|
|
// }
|
2010-06-29 20:43:44 +00:00
|
|
|
var selector = selectorManager.requestSelector(selectedElements[0]);
|
|
|
|
selector.resize();
|
|
|
|
selector.updateGripCursors(val);
|
|
|
|
};
|
|
|
|
|
2010-06-22 18:17:42 +00:00
|
|
|
// Function: recalculateAllSelectedDimensions
|
|
|
|
// Runs recalculateDimensions on the selected elements,
|
|
|
|
// adding the changes to a single batch command
|
|
|
|
var recalculateAllSelectedDimensions = this.recalculateAllSelectedDimensions = function() {
|
2014-04-08 03:21:22 +00:00
|
|
|
var text = (current_resize_mode == 'none' ? 'position' : 'size');
|
2013-02-17 08:21:07 +00:00
|
|
|
var batchCmd = new svgedit.history.BatchCommand(text);
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
var i = selectedElements.length;
|
2013-02-15 15:51:58 +00:00
|
|
|
while (i--) {
|
2010-06-22 14:52:51 +00:00
|
|
|
var elem = selectedElements[i];
|
2014-02-17 06:48:40 +00:00
|
|
|
// if (svgedit.utilities.getRotationAngle(elem) && !svgedit.math.hasMatrixTransform(getTransformList(elem))) {continue;}
|
2013-02-20 06:29:25 +00:00
|
|
|
var cmd = svgedit.recalculate.recalculateDimensions(elem);
|
2010-06-22 14:52:51 +00:00
|
|
|
if (cmd) {
|
|
|
|
batchCmd.addSubCommand(cmd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!batchCmd.isEmpty()) {
|
|
|
|
addCommandToHistory(batchCmd);
|
2014-04-08 03:21:22 +00:00
|
|
|
call('changed', selectedElements);
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// this is how we map paths to our preferred relative segment types
|
|
|
|
var pathMap = [0, 'z', 'M', 'm', 'L', 'l', 'C', 'c', 'Q', 'q', 'A', 'a',
|
|
|
|
'H', 'h', 'V', 'v', 'S', 's', 'T', 't'];
|
2010-06-22 18:17:42 +00:00
|
|
|
|
|
|
|
// Debug tool to easily see the current matrix in the browser's console
|
2010-06-22 14:52:51 +00:00
|
|
|
var logMatrix = function(m) {
|
2013-02-15 16:51:48 +00:00
|
|
|
console.log([m.a, m.b, m.c, m.d, m.e, m.f]);
|
2010-06-22 14:52:51 +00:00
|
|
|
};
|
|
|
|
|
2010-06-22 18:17:42 +00:00
|
|
|
// Root Current Transformation Matrix in user units
|
|
|
|
var root_sctm = null;
|
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// Group: Selection
|
2009-10-13 02:48:27 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// Function: clearSelection
|
2014-01-31 10:40:52 +00:00
|
|
|
// Clears the selection. The 'selected' handler is then called.
|
2010-06-22 18:17:42 +00:00
|
|
|
// Parameters:
|
|
|
|
// noCall - Optional boolean that when true does not call the "selected" handler
|
2010-06-30 18:27:36 +00:00
|
|
|
var clearSelection = this.clearSelection = function(noCall) {
|
2010-06-22 14:52:51 +00:00
|
|
|
if (selectedElements[0] != null) {
|
2014-01-31 00:39:35 +00:00
|
|
|
var i, elem,
|
|
|
|
len = selectedElements.length;
|
|
|
|
for (i = 0; i < len; ++i) {
|
|
|
|
elem = selectedElements[i];
|
|
|
|
if (elem == null) {break;}
|
2010-06-22 14:52:51 +00:00
|
|
|
selectorManager.releaseSelector(elem);
|
|
|
|
selectedElements[i] = null;
|
2009-07-01 15:34:15 +00:00
|
|
|
}
|
2011-02-24 16:13:26 +00:00
|
|
|
// selectedBBoxes[0] = null;
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
2014-04-08 03:21:22 +00:00
|
|
|
if (!noCall) {call('selected', selectedElements);}
|
2010-06-22 14:52:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: do we need to worry about selectedBBoxes here?
|
|
|
|
|
2010-09-17 20:33:33 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// Function: addToSelection
|
2014-01-31 10:40:52 +00:00
|
|
|
// Adds a list of elements to the selection. The 'selected' handler is then called.
|
2010-06-22 14:52:51 +00:00
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// elemsToAdd - an array of DOM elements to add to the selection
|
|
|
|
// showGrips - a boolean flag indicating whether the resize grips should be shown
|
2010-06-30 18:27:36 +00:00
|
|
|
var addToSelection = this.addToSelection = function(elemsToAdd, showGrips) {
|
2010-06-22 14:52:51 +00:00
|
|
|
if (elemsToAdd.length == 0) { return; }
|
|
|
|
// find the first null in our selectedElements array
|
|
|
|
var j = 0;
|
2010-07-16 15:46:54 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
while (j < selectedElements.length) {
|
|
|
|
if (selectedElements[j] == null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++j;
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// now add each element consecutively
|
|
|
|
var i = elemsToAdd.length;
|
|
|
|
while (i--) {
|
|
|
|
var elem = elemsToAdd[i];
|
2014-01-31 00:39:35 +00:00
|
|
|
if (!elem || !svgedit.utilities.getBBox(elem)) {continue;}
|
2010-11-30 20:51:07 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (elem.tagName === 'a' && elem.childNodes.length === 1) {
|
2010-11-30 20:51:07 +00:00
|
|
|
// Make "a" element's child be the selected element
|
|
|
|
elem = elem.firstChild;
|
|
|
|
}
|
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// if it's not already there, add it
|
|
|
|
if (selectedElements.indexOf(elem) == -1) {
|
2010-11-30 20:51:07 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
selectedElements[j] = elem;
|
2010-11-30 20:51:07 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// only the first selectedBBoxes element is ever used in the codebase these days
|
2011-02-24 16:13:26 +00:00
|
|
|
// if (j == 0) selectedBBoxes[0] = svgedit.utilities.getBBox(elem);
|
2010-06-22 14:52:51 +00:00
|
|
|
j++;
|
|
|
|
var sel = selectorManager.requestSelector(elem);
|
2009-10-13 02:35:36 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
if (selectedElements.length > 1) {
|
|
|
|
sel.showGrips(false);
|
2009-07-01 15:34:15 +00:00
|
|
|
}
|
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
2014-04-08 03:21:22 +00:00
|
|
|
call('selected', selectedElements);
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
if (showGrips || selectedElements.length == 1) {
|
|
|
|
selectorManager.requestSelector(selectedElements[0]).showGrips(true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
selectorManager.requestSelector(selectedElements[0]).showGrips(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure the elements are in the correct order
|
|
|
|
// See: http://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-compareDocumentPosition
|
|
|
|
|
2013-02-15 16:51:48 +00:00
|
|
|
selectedElements.sort(function(a, b) {
|
2013-02-15 15:51:58 +00:00
|
|
|
if (a && b && a.compareDocumentPosition) {
|
2010-06-22 14:52:51 +00:00
|
|
|
return 3 - (b.compareDocumentPosition(a) & 6);
|
2013-02-15 15:51:58 +00:00
|
|
|
}
|
|
|
|
if (a == null) {
|
2010-06-22 14:52:51 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Make sure first elements are not null
|
2014-01-31 10:40:52 +00:00
|
|
|
while (selectedElements[0] == null) {
|
|
|
|
selectedElements.shift(0);
|
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
};
|
2009-12-18 05:00:45 +00:00
|
|
|
|
2010-09-17 20:33:33 +00:00
|
|
|
// Function: selectOnly()
|
|
|
|
// Selects only the given elements, shortcut for clearSelection(); addToSelection()
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// elems - an array of DOM elements to be selected
|
|
|
|
var selectOnly = this.selectOnly = function(elems, showGrips) {
|
|
|
|
clearSelection(true);
|
|
|
|
addToSelection(elems, showGrips);
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-09-17 20:33:33 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// TODO: could use slice here to make this faster?
|
|
|
|
// TODO: should the 'selected' handler
|
|
|
|
|
|
|
|
// Function: removeFromSelection
|
|
|
|
// Removes elements from the selection.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// elemsToRemove - an array of elements to remove from selection
|
2010-06-30 18:27:36 +00:00
|
|
|
var removeFromSelection = this.removeFromSelection = function(elemsToRemove) {
|
2010-06-22 14:52:51 +00:00
|
|
|
if (selectedElements[0] == null) { return; }
|
|
|
|
if (elemsToRemove.length == 0) { return; }
|
|
|
|
|
|
|
|
// find every element and remove it from our array copy
|
2014-01-31 10:40:52 +00:00
|
|
|
var i,
|
2010-06-22 14:52:51 +00:00
|
|
|
j = 0,
|
2014-01-31 10:40:52 +00:00
|
|
|
newSelectedItems = new Array(selectedElements.length),
|
2010-06-22 14:52:51 +00:00
|
|
|
len = selectedElements.length;
|
2014-01-31 10:40:52 +00:00
|
|
|
for (i = 0; i < len; ++i) {
|
2010-06-22 14:52:51 +00:00
|
|
|
var elem = selectedElements[i];
|
|
|
|
if (elem) {
|
|
|
|
// keep the item
|
|
|
|
if (elemsToRemove.indexOf(elem) == -1) {
|
|
|
|
newSelectedItems[j] = elem;
|
2009-12-29 14:26:04 +00:00
|
|
|
j++;
|
2013-02-15 16:51:48 +00:00
|
|
|
} else { // remove the item and its selector
|
2010-06-22 14:52:51 +00:00
|
|
|
selectorManager.releaseSelector(elem);
|
2009-07-01 15:34:15 +00:00
|
|
|
}
|
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
// the copy becomes the master now
|
|
|
|
selectedElements = newSelectedItems;
|
|
|
|
};
|
|
|
|
|
2010-06-28 13:10:22 +00:00
|
|
|
// Function: selectAllInCurrentLayer
|
|
|
|
// Clears the selection, then adds all elements in the current layer to the selection.
|
|
|
|
this.selectAllInCurrentLayer = function() {
|
2011-01-25 16:15:29 +00:00
|
|
|
var current_layer = getCurrentDrawing().getCurrentLayer();
|
2010-06-28 13:10:22 +00:00
|
|
|
if (current_layer) {
|
2014-04-08 03:21:22 +00:00
|
|
|
current_mode = 'select';
|
2010-09-24 16:05:46 +00:00
|
|
|
selectOnly($(current_group || current_layer).children());
|
2010-06-28 13:10:22 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-06-22 18:17:42 +00:00
|
|
|
// Function: getMouseTarget
|
|
|
|
// Gets the desired element from a mouse event
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// evt - Event object from the mouse event
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// DOM element we want
|
|
|
|
var getMouseTarget = this.getMouseTarget = function(evt) {
|
2010-06-22 14:52:51 +00:00
|
|
|
if (evt == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
var mouse_target = evt.target;
|
2010-09-22 18:42:24 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// if it was a <use>, Opera and WebKit return the SVGElementInstance
|
2014-01-31 10:40:52 +00:00
|
|
|
if (mouse_target.correspondingUseElement) {mouse_target = mouse_target.correspondingUseElement;}
|
2009-12-13 03:31:30 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// for foreign content, go up until we find the foreignObject
|
|
|
|
// WebKit browsers set the mouse target to the svgcanvas div
|
2013-02-16 15:02:26 +00:00
|
|
|
if ([NS.MATH, NS.HTML].indexOf(mouse_target.namespaceURI) >= 0 &&
|
2014-04-08 03:21:22 +00:00
|
|
|
mouse_target.id != 'svgcanvas')
|
2010-06-22 14:52:51 +00:00
|
|
|
{
|
2014-04-08 03:21:22 +00:00
|
|
|
while (mouse_target.nodeName != 'foreignObject') {
|
2010-06-22 14:52:51 +00:00
|
|
|
mouse_target = mouse_target.parentNode;
|
2014-01-31 10:40:52 +00:00
|
|
|
if (!mouse_target) {return svgroot;}
|
2009-12-13 03:31:30 +00:00
|
|
|
}
|
|
|
|
}
|
2010-04-26 16:53:06 +00:00
|
|
|
|
2010-07-16 15:46:54 +00:00
|
|
|
// Get the desired mouse_target with jQuery selector-fu
|
|
|
|
// If it's root-like, select the root
|
2011-01-25 16:15:29 +00:00
|
|
|
var current_layer = getCurrentDrawing().getCurrentLayer();
|
2013-02-15 15:51:58 +00:00
|
|
|
if ([svgroot, container, svgcontent, current_layer].indexOf(mouse_target) >= 0) {
|
2010-07-16 15:46:54 +00:00
|
|
|
return svgroot;
|
|
|
|
}
|
|
|
|
|
|
|
|
var $target = $(mouse_target);
|
|
|
|
|
|
|
|
// If it's a selection grip, return the grip parent
|
2013-02-15 15:51:58 +00:00
|
|
|
if ($target.closest('#selectorParentGroup').length) {
|
2010-07-16 15:46:54 +00:00
|
|
|
// While we could instead have just returned mouse_target,
|
|
|
|
// this makes it easier to indentify as being a selector grip
|
|
|
|
return selectorManager.selectorParentGroup;
|
|
|
|
}
|
2010-09-23 19:53:43 +00:00
|
|
|
|
2010-09-22 18:42:24 +00:00
|
|
|
while (mouse_target.parentNode !== (current_group || current_layer)) {
|
2010-06-22 14:52:51 +00:00
|
|
|
mouse_target = mouse_target.parentNode;
|
|
|
|
}
|
2010-07-16 15:46:54 +00:00
|
|
|
|
2014-01-31 00:39:35 +00:00
|
|
|
//
|
|
|
|
// // go up until we hit a child of a layer
|
|
|
|
// while (mouse_target.parentNode.parentNode.tagName == 'g') {
|
|
|
|
// mouse_target = mouse_target.parentNode;
|
|
|
|
// }
|
2010-06-22 14:52:51 +00:00
|
|
|
// Webkit bubbles the mouse event all the way up to the div, so we
|
|
|
|
// set the mouse_target to the svgroot like the other browsers
|
2014-04-08 03:21:22 +00:00
|
|
|
// if (mouse_target.nodeName.toLowerCase() == 'div') {
|
2014-01-31 00:39:35 +00:00
|
|
|
// mouse_target = svgroot;
|
|
|
|
// }
|
2009-11-04 15:30:10 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
return mouse_target;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Mouse events
|
|
|
|
(function() {
|
|
|
|
var d_attr = null,
|
|
|
|
start_x = null,
|
|
|
|
start_y = null,
|
2010-09-10 13:23:31 +00:00
|
|
|
r_start_x = null,
|
|
|
|
r_start_y = null,
|
2010-06-22 14:52:51 +00:00
|
|
|
init_bbox = {},
|
|
|
|
freehand = {
|
|
|
|
minx: null,
|
|
|
|
miny: null,
|
|
|
|
maxx: null,
|
|
|
|
maxy: null
|
2013-02-13 16:10:15 +00:00
|
|
|
},
|
2013-02-15 15:51:58 +00:00
|
|
|
sumDistance = 0,
|
2013-02-13 16:10:15 +00:00
|
|
|
controllPoint2 = {x:0, y:0},
|
|
|
|
controllPoint1 = {x:0, y:0},
|
2013-02-15 15:51:58 +00:00
|
|
|
start = {x:0, y:0},
|
|
|
|
end = {x:0, y:0},
|
2013-02-13 16:10:15 +00:00
|
|
|
parameter,
|
|
|
|
nextParameter,
|
2013-02-15 15:51:58 +00:00
|
|
|
bSpline = {x:0, y:0},
|
|
|
|
nextPos = {x:0, y:0},
|
|
|
|
THRESHOLD_DIST = 0.8,
|
|
|
|
STEP_COUNT = 10;
|
2013-02-13 16:10:15 +00:00
|
|
|
|
|
|
|
var getBsplinePoint = function(t) {
|
|
|
|
var spline = {x:0, y:0},
|
2013-02-15 15:51:58 +00:00
|
|
|
p0 = controllPoint2,
|
|
|
|
p1 = controllPoint1,
|
|
|
|
p2 = start,
|
|
|
|
p3 = end,
|
|
|
|
S = 1.0 / 6.0,
|
|
|
|
t2 = t * t,
|
|
|
|
t3 = t2 * t;
|
2013-02-13 16:10:15 +00:00
|
|
|
|
2014-01-31 10:40:52 +00:00
|
|
|
var m = [
|
|
|
|
[-1, 3, -3, 1],
|
|
|
|
[3, -6, 3, 0],
|
|
|
|
[-3, 0, 3, 0],
|
|
|
|
[1, 4, 1, 0]
|
|
|
|
];
|
2013-02-13 16:10:15 +00:00
|
|
|
|
|
|
|
spline.x = S * (
|
|
|
|
(p0.x * m[0][0] + p1.x * m[0][1] + p2.x * m[0][2] + p3.x * m[0][3] ) * t3 +
|
|
|
|
(p0.x * m[1][0] + p1.x * m[1][1] + p2.x * m[1][2] + p3.x * m[1][3] ) * t2 +
|
|
|
|
(p0.x * m[2][0] + p1.x * m[2][1] + p2.x * m[2][2] + p3.x * m[2][3] ) * t +
|
2014-01-31 10:40:52 +00:00
|
|
|
(p0.x * m[3][0] + p1.x * m[3][1] + p2.x * m[3][2] + p3.x * m[3][3] )
|
|
|
|
);
|
2013-02-13 16:10:15 +00:00
|
|
|
spline.y = S * (
|
|
|
|
(p0.y * m[0][0] + p1.y * m[0][1] + p2.y * m[0][2] + p3.y * m[0][3] ) * t3 +
|
|
|
|
(p0.y * m[1][0] + p1.y * m[1][1] + p2.y * m[1][2] + p3.y * m[1][3] ) * t2 +
|
|
|
|
(p0.y * m[2][0] + p1.y * m[2][1] + p2.y * m[2][2] + p3.y * m[2][3] ) * t +
|
2014-01-31 10:40:52 +00:00
|
|
|
(p0.y * m[3][0] + p1.y * m[3][1] + p2.y * m[3][2] + p3.y * m[3][3] )
|
|
|
|
);
|
2013-02-13 16:10:15 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
x:spline.x,
|
|
|
|
y:spline.y
|
2010-06-22 14:52:51 +00:00
|
|
|
};
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-06-22 14:52:51 +00:00
|
|
|
// - when we are in a create mode, the element is added to the canvas
|
2014-01-31 10:40:52 +00:00
|
|
|
// but the action is not recorded until mousing up
|
2010-06-22 14:52:51 +00:00
|
|
|
// - when we are in select mode, select the element, remember the position
|
2014-01-31 10:40:52 +00:00
|
|
|
// and do nothing else
|
|
|
|
var mouseDown = function(evt) {
|
|
|
|
if (canvas.spaceKey || evt.button === 1) {return;}
|
|
|
|
|
2010-08-16 20:26:06 +00:00
|
|
|
var right_click = evt.button === 2;
|
2011-01-30 05:55:33 +00:00
|
|
|
|
2014-01-31 10:40:52 +00:00
|
|
|
if (evt.altKey) { // duplicate when dragging
|
2013-02-15 16:51:48 +00:00
|
|
|
svgCanvas.cloneSelectedElements(0, 0);
|
2011-01-30 05:55:33 +00:00
|
|
|
}
|
|
|
|
|
2013-08-21 06:20:22 +00:00
|
|
|
root_sctm = $('#svgcontent g')[0].getScreenCTM().inverse();
|
2011-02-23 18:30:09 +00:00
|
|
|
|
2013-02-14 15:19:46 +00:00
|
|
|
var pt = svgedit.math.transformPoint( evt.pageX, evt.pageY, root_sctm ),
|
2010-06-22 14:52:51 +00:00
|
|
|
mouse_x = pt.x * current_zoom,
|
|
|
|
mouse_y = pt.y * current_zoom;
|
2010-08-23 20:16:27 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
evt.preventDefault();
|
2010-08-17 18:09:50 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (right_click) {
|
2014-04-08 03:21:22 +00:00
|
|
|
current_mode = 'select';
|
2010-08-17 18:09:50 +00:00
|
|
|
lastClickPoint = pt;
|
|
|
|
}
|
2010-06-28 20:09:34 +00:00
|
|
|
|
|
|
|
// This would seem to be unnecessary...
|
2014-01-31 00:39:35 +00:00
|
|
|
// if (['select', 'resize'].indexOf(current_mode) == -1) {
|
|
|
|
// setGradient();
|
|
|
|
// }
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
var x = mouse_x / current_zoom,
|
|
|
|
y = mouse_y / current_zoom,
|
|
|
|
mouse_target = getMouseTarget(evt);
|
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (mouse_target.tagName === 'a' && mouse_target.childNodes.length === 1) {
|
2010-11-30 20:51:07 +00:00
|
|
|
mouse_target = mouse_target.firstChild;
|
|
|
|
}
|
|
|
|
|
2010-09-10 13:23:31 +00:00
|
|
|
// real_x/y ignores grid-snap value
|
2014-01-31 10:40:52 +00:00
|
|
|
var real_x = x;
|
|
|
|
r_start_x = start_x = x;
|
|
|
|
var real_y = y;
|
|
|
|
r_start_y = start_y = y;
|
2010-09-09 12:46:34 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (curConfig.gridSnapping){
|
2013-02-17 04:58:04 +00:00
|
|
|
x = svgedit.utilities.snapToGrid(x);
|
|
|
|
y = svgedit.utilities.snapToGrid(y);
|
|
|
|
start_x = svgedit.utilities.snapToGrid(start_x);
|
|
|
|
start_y = svgedit.utilities.snapToGrid(start_y);
|
2010-09-09 12:46:34 +00:00
|
|
|
}
|
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// if it is a selector grip, then it must be a single element selected,
|
|
|
|
// set the mouse_target to that and update the mode to rotate/resize
|
2010-09-10 18:51:59 +00:00
|
|
|
|
2010-07-16 15:46:54 +00:00
|
|
|
if (mouse_target == selectorManager.selectorParentGroup && selectedElements[0] != null) {
|
2010-09-10 18:51:59 +00:00
|
|
|
var grip = evt.target;
|
2014-04-08 03:21:22 +00:00
|
|
|
var griptype = elData(grip, 'type');
|
2010-06-22 14:52:51 +00:00
|
|
|
// rotating
|
2014-04-08 03:21:22 +00:00
|
|
|
if (griptype == 'rotate') {
|
|
|
|
current_mode = 'rotate';
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
// resizing
|
2014-04-08 03:21:22 +00:00
|
|
|
else if (griptype == 'resize') {
|
|
|
|
current_mode = 'resize';
|
|
|
|
current_resize_mode = elData(grip, 'dir');
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
mouse_target = selectedElements[0];
|
|
|
|
}
|
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
startTransform = mouse_target.getAttribute('transform');
|
2014-02-11 13:46:39 +00:00
|
|
|
var i, stroke_w,
|
2014-01-31 10:40:52 +00:00
|
|
|
tlist = svgedit.transformlist.getTransformList(mouse_target);
|
2010-06-22 14:52:51 +00:00
|
|
|
switch (current_mode) {
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'select':
|
2010-06-22 14:52:51 +00:00
|
|
|
started = true;
|
2014-04-08 03:21:22 +00:00
|
|
|
current_resize_mode = 'none';
|
2014-01-31 10:40:52 +00:00
|
|
|
if (right_click) {started = false;}
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
if (mouse_target != svgroot) {
|
|
|
|
// if this element is not yet selected, clear selection and select it
|
|
|
|
if (selectedElements.indexOf(mouse_target) == -1) {
|
|
|
|
// only clear selection if shift is not pressed (otherwise, add
|
|
|
|
// element to selection)
|
|
|
|
if (!evt.shiftKey) {
|
|
|
|
// No need to do the call here as it will be done on addToSelection
|
2010-06-30 18:27:36 +00:00
|
|
|
clearSelection(true);
|
2009-12-30 18:06:29 +00:00
|
|
|
}
|
2010-06-30 18:27:36 +00:00
|
|
|
addToSelection([mouse_target]);
|
2010-06-22 14:52:51 +00:00
|
|
|
justSelected = mouse_target;
|
|
|
|
pathActions.clear();
|
2009-10-17 20:29:56 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
// else if it's a path, go into pathedit mode in mouseup
|
2010-08-23 13:58:51 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!right_click) {
|
2010-08-23 13:58:51 +00:00
|
|
|
// insert a dummy transform so if the element(s) are moved it will have
|
|
|
|
// a transform to use for its translate
|
2014-01-31 10:40:52 +00:00
|
|
|
for (i = 0; i < selectedElements.length; ++i) {
|
|
|
|
if (selectedElements[i] == null) {continue;}
|
2013-02-14 15:19:46 +00:00
|
|
|
var slist = svgedit.transformlist.getTransformList(selectedElements[i]);
|
2013-02-15 15:51:58 +00:00
|
|
|
if (slist.numberOfItems) {
|
2010-09-16 12:56:09 +00:00
|
|
|
slist.insertItemBefore(svgroot.createSVGTransform(), 0);
|
|
|
|
} else {
|
|
|
|
slist.appendItem(svgroot.createSVGTransform());
|
|
|
|
}
|
2010-08-23 13:58:51 +00:00
|
|
|
}
|
2009-08-24 17:40:41 +00:00
|
|
|
}
|
2013-02-15 15:51:58 +00:00
|
|
|
} else if (!right_click){
|
2010-06-30 18:27:36 +00:00
|
|
|
clearSelection();
|
2014-04-08 03:21:22 +00:00
|
|
|
current_mode = 'multiselect';
|
2009-12-30 18:06:29 +00:00
|
|
|
if (rubberBox == null) {
|
|
|
|
rubberBox = selectorManager.getRubberBandBox();
|
2009-07-03 02:49:42 +00:00
|
|
|
}
|
2010-09-10 13:23:31 +00:00
|
|
|
r_start_x *= current_zoom;
|
|
|
|
r_start_y *= current_zoom;
|
2014-01-31 00:39:35 +00:00
|
|
|
// console.log('p',[evt.pageX, evt.pageY]);
|
|
|
|
// console.log('c',[evt.clientX, evt.clientY]);
|
|
|
|
// console.log('o',[evt.offsetX, evt.offsetY]);
|
|
|
|
// console.log('s',[start_x, start_y]);
|
2010-08-23 20:16:27 +00:00
|
|
|
|
2013-02-17 04:58:04 +00:00
|
|
|
svgedit.utilities.assignAttributes(rubberBox, {
|
2010-09-10 13:23:31 +00:00
|
|
|
'x': r_start_x,
|
|
|
|
'y': r_start_y,
|
2010-06-22 14:52:51 +00:00
|
|
|
'width': 0,
|
|
|
|
'height': 0,
|
|
|
|
'display': 'inline'
|
2009-12-30 18:06:29 +00:00
|
|
|
}, 100);
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
break;
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'zoom':
|
2010-06-22 14:52:51 +00:00
|
|
|
started = true;
|
|
|
|
if (rubberBox == null) {
|
|
|
|
rubberBox = selectorManager.getRubberBandBox();
|
|
|
|
}
|
2013-02-17 04:58:04 +00:00
|
|
|
svgedit.utilities.assignAttributes(rubberBox, {
|
2010-09-10 13:23:31 +00:00
|
|
|
'x': real_x * current_zoom,
|
|
|
|
'y': real_x * current_zoom,
|
2010-06-22 14:52:51 +00:00
|
|
|
'width': 0,
|
|
|
|
'height': 0,
|
|
|
|
'display': 'inline'
|
|
|
|
}, 100);
|
|
|
|
break;
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'resize':
|
2010-06-22 14:52:51 +00:00
|
|
|
started = true;
|
|
|
|
start_x = x;
|
|
|
|
start_y = y;
|
|
|
|
|
|
|
|
// Getting the BBox from the selection box, since we know we
|
|
|
|
// want to orient around it
|
2011-02-24 16:13:26 +00:00
|
|
|
init_bbox = svgedit.utilities.getBBox($('#selectedBox0')[0]);
|
2010-09-16 12:56:09 +00:00
|
|
|
var bb = {};
|
2010-06-22 14:52:51 +00:00
|
|
|
$.each(init_bbox, function(key, val) {
|
2010-09-16 12:56:09 +00:00
|
|
|
bb[key] = val/current_zoom;
|
2010-06-22 14:52:51 +00:00
|
|
|
});
|
2010-09-16 12:56:09 +00:00
|
|
|
init_bbox = bb;
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
// append three dummy transforms to the tlist so that
|
|
|
|
// we can translate,scale,translate in mousemove
|
2013-02-15 16:51:48 +00:00
|
|
|
var pos = svgedit.utilities.getRotationAngle(mouse_target) ? 1 : 0;
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (svgedit.math.hasMatrixTransform(tlist)) {
|
2010-06-22 14:52:51 +00:00
|
|
|
tlist.insertItemBefore(svgroot.createSVGTransform(), pos);
|
|
|
|
tlist.insertItemBefore(svgroot.createSVGTransform(), pos);
|
|
|
|
tlist.insertItemBefore(svgroot.createSVGTransform(), pos);
|
|
|
|
} else {
|
|
|
|
tlist.appendItem(svgroot.createSVGTransform());
|
|
|
|
tlist.appendItem(svgroot.createSVGTransform());
|
|
|
|
tlist.appendItem(svgroot.createSVGTransform());
|
2010-10-11 17:16:10 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (svgedit.browser.supportsNonScalingStroke()) {
|
2012-11-13 10:37:24 +00:00
|
|
|
// Handle crash for newer Chrome and Safari 6 (Mobile and Desktop):
|
|
|
|
// https://code.google.com/p/svg-edit/issues/detail?id=904
|
|
|
|
// Chromium issue: https://code.google.com/p/chromium/issues/detail?id=114625
|
|
|
|
// TODO: Remove this workaround once vendor fixes the issue
|
|
|
|
var isWebkit = svgedit.browser.isWebkit();
|
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (isWebkit) {
|
2012-02-23 04:13:59 +00:00
|
|
|
var delayedStroke = function(ele) {
|
|
|
|
var _stroke = ele.getAttributeNS(null, 'stroke');
|
|
|
|
ele.removeAttributeNS(null, 'stroke');
|
|
|
|
//Re-apply stroke after delay. Anything higher than 1 seems to cause flicker
|
2013-02-15 15:51:58 +00:00
|
|
|
setTimeout(function() { ele.setAttributeNS(null, 'stroke', _stroke); }, 0);
|
|
|
|
};
|
2012-02-23 03:11:28 +00:00
|
|
|
}
|
2010-10-11 17:16:10 +00:00
|
|
|
mouse_target.style.vectorEffect = 'non-scaling-stroke';
|
2014-01-31 10:40:52 +00:00
|
|
|
if (isWebkit) {delayedStroke(mouse_target);}
|
2012-02-23 04:13:59 +00:00
|
|
|
|
2014-02-10 13:27:34 +00:00
|
|
|
var all = mouse_target.getElementsByTagName('*'),
|
2013-10-14 01:50:42 +00:00
|
|
|
len = all.length;
|
2014-01-31 10:40:52 +00:00
|
|
|
for (i = 0; i < len; i++) {
|
2010-10-11 17:16:10 +00:00
|
|
|
all[i].style.vectorEffect = 'non-scaling-stroke';
|
2014-02-10 13:27:34 +00:00
|
|
|
if (isWebkit) {delayedStroke(all[i]);}
|
2010-10-11 17:16:10 +00:00
|
|
|
}
|
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
break;
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'fhellipse':
|
|
|
|
case 'fhrect':
|
|
|
|
case 'fhpath':
|
2013-02-15 16:51:48 +00:00
|
|
|
start.x = real_x;
|
|
|
|
start.y = real_y;
|
2010-06-22 14:52:51 +00:00
|
|
|
started = true;
|
2014-04-08 03:21:22 +00:00
|
|
|
d_attr = real_x + ',' + real_y + ' ';
|
2014-02-11 13:46:39 +00:00
|
|
|
stroke_w = cur_shape.stroke_width == 0 ? 1 : cur_shape.stroke_width;
|
2010-06-22 14:52:51 +00:00
|
|
|
addSvgElementFromJson({
|
2014-04-08 03:21:22 +00:00
|
|
|
element: 'polyline',
|
|
|
|
curStyles: true,
|
|
|
|
attr: {
|
|
|
|
points: d_attr,
|
|
|
|
id: getNextId(),
|
|
|
|
fill: 'none',
|
|
|
|
opacity: cur_shape.opacity / 2,
|
|
|
|
'stroke-linecap': 'round',
|
|
|
|
style: 'pointer-events:none'
|
2009-09-25 17:59:32 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
});
|
2010-09-17 19:34:23 +00:00
|
|
|
freehand.minx = real_x;
|
|
|
|
freehand.maxx = real_x;
|
|
|
|
freehand.miny = real_y;
|
|
|
|
freehand.maxy = real_y;
|
2010-06-22 14:52:51 +00:00
|
|
|
break;
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'image':
|
2010-06-22 14:52:51 +00:00
|
|
|
started = true;
|
|
|
|
var newImage = addSvgElementFromJson({
|
2014-04-08 03:21:22 +00:00
|
|
|
element: 'image',
|
|
|
|
attr: {
|
|
|
|
x: x,
|
|
|
|
y: y,
|
|
|
|
width: 0,
|
|
|
|
height: 0,
|
|
|
|
id: getNextId(),
|
|
|
|
opacity: cur_shape.opacity / 2,
|
|
|
|
style: 'pointer-events:inherit'
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
});
|
2010-08-16 17:53:15 +00:00
|
|
|
setHref(newImage, last_good_img_url);
|
2010-06-22 14:52:51 +00:00
|
|
|
preventClickDefault(newImage);
|
|
|
|
break;
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'square':
|
2010-06-22 14:52:51 +00:00
|
|
|
// FIXME: once we create the rect, we lose information that this was a square
|
|
|
|
// (for resizing purposes this could be important)
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'rect':
|
2010-06-22 14:52:51 +00:00
|
|
|
started = true;
|
|
|
|
start_x = x;
|
|
|
|
start_y = y;
|
|
|
|
addSvgElementFromJson({
|
2014-04-08 03:21:22 +00:00
|
|
|
element: 'rect',
|
|
|
|
curStyles: true,
|
|
|
|
attr: {
|
|
|
|
x: x,
|
|
|
|
y: y,
|
|
|
|
width: 0,
|
|
|
|
height: 0,
|
|
|
|
id: getNextId(),
|
|
|
|
opacity: cur_shape.opacity / 2
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'line':
|
2010-06-22 14:52:51 +00:00
|
|
|
started = true;
|
2014-02-11 13:46:39 +00:00
|
|
|
stroke_w = cur_shape.stroke_width == 0 ? 1 : cur_shape.stroke_width;
|
2010-06-22 14:52:51 +00:00
|
|
|
addSvgElementFromJson({
|
2014-04-08 03:21:22 +00:00
|
|
|
element: 'line',
|
|
|
|
curStyles: true,
|
|
|
|
attr: {
|
|
|
|
x1: x,
|
|
|
|
y1: y,
|
|
|
|
x2: x,
|
|
|
|
y2: y,
|
|
|
|
id: getNextId(),
|
|
|
|
stroke: cur_shape.stroke,
|
|
|
|
'stroke-width': stroke_w,
|
|
|
|
'stroke-dasharray': cur_shape.stroke_dasharray,
|
|
|
|
'stroke-linejoin': cur_shape.stroke_linejoin,
|
|
|
|
'stroke-linecap': cur_shape.stroke_linecap,
|
|
|
|
'stroke-opacity': cur_shape.stroke_opacity,
|
|
|
|
fill: 'none',
|
|
|
|
opacity: cur_shape.opacity / 2,
|
|
|
|
style: 'pointer-events:none'
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'circle':
|
2010-06-22 14:52:51 +00:00
|
|
|
started = true;
|
|
|
|
addSvgElementFromJson({
|
2014-04-08 03:21:22 +00:00
|
|
|
element: 'circle',
|
|
|
|
curStyles: true,
|
|
|
|
attr: {
|
|
|
|
cx: x,
|
|
|
|
cy: y,
|
|
|
|
r: 0,
|
|
|
|
id: getNextId(),
|
|
|
|
opacity: cur_shape.opacity / 2
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'ellipse':
|
2010-06-22 14:52:51 +00:00
|
|
|
started = true;
|
|
|
|
addSvgElementFromJson({
|
2014-04-08 03:21:22 +00:00
|
|
|
element: 'ellipse',
|
|
|
|
curStyles: true,
|
|
|
|
attr: {
|
|
|
|
cx: x,
|
|
|
|
cy: y,
|
|
|
|
rx: 0,
|
|
|
|
ry: 0,
|
|
|
|
id: getNextId(),
|
|
|
|
opacity: cur_shape.opacity / 2
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'text':
|
2010-06-22 14:52:51 +00:00
|
|
|
started = true;
|
|
|
|
var newText = addSvgElementFromJson({
|
2014-04-08 03:21:22 +00:00
|
|
|
element: 'text',
|
|
|
|
curStyles: true,
|
|
|
|
attr: {
|
|
|
|
x: x,
|
|
|
|
y: y,
|
|
|
|
id: getNextId(),
|
|
|
|
fill: cur_text.fill,
|
|
|
|
'stroke-width': cur_text.stroke_width,
|
|
|
|
'font-size': cur_text.font_size,
|
|
|
|
'font-family': cur_text.font_family,
|
|
|
|
'text-anchor': 'middle',
|
|
|
|
'xml:space': 'preserve',
|
|
|
|
opacity: cur_shape.opacity
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
});
|
2014-04-08 03:21:22 +00:00
|
|
|
// newText.textContent = 'text';
|
2010-06-22 14:52:51 +00:00
|
|
|
break;
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'path':
|
2010-06-22 14:52:51 +00:00
|
|
|
// Fall through
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'pathedit':
|
2010-06-22 14:52:51 +00:00
|
|
|
start_x *= current_zoom;
|
|
|
|
start_y *= current_zoom;
|
|
|
|
pathActions.mouseDown(evt, mouse_target, start_x, start_y);
|
|
|
|
started = true;
|
|
|
|
break;
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'textedit':
|
2010-06-22 14:52:51 +00:00
|
|
|
start_x *= current_zoom;
|
|
|
|
start_y *= current_zoom;
|
|
|
|
textActions.mouseDown(evt, mouse_target, start_x, start_y);
|
|
|
|
started = true;
|
|
|
|
break;
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'rotate':
|
2010-06-22 14:52:51 +00:00
|
|
|
started = true;
|
|
|
|
// we are starting an undoable change (a drag-rotation)
|
2014-04-08 03:21:22 +00:00
|
|
|
canvas.undoMgr.beginUndoableChange('transform', selectedElements);
|
2010-06-22 14:52:51 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// This could occur in an extension
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
var ext_result = runExtensions('mouseDown', {
|
2010-06-22 14:52:51 +00:00
|
|
|
event: evt,
|
|
|
|
start_x: start_x,
|
|
|
|
start_y: start_y,
|
|
|
|
selectedElements: selectedElements
|
|
|
|
}, true);
|
|
|
|
|
|
|
|
$.each(ext_result, function(i, r) {
|
2013-02-15 15:51:58 +00:00
|
|
|
if (r && r.started) {
|
2010-06-22 14:52:51 +00:00
|
|
|
started = true;
|
2009-12-30 18:06:29 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// in this function we do not record any state changes yet (but we do update
|
|
|
|
// any elements that are still being created, moved or resized on the canvas)
|
2014-02-10 13:27:34 +00:00
|
|
|
var mouseMove = function(evt) {
|
2014-01-31 10:40:52 +00:00
|
|
|
if (!started) {return;}
|
|
|
|
if (evt.button === 1 || canvas.spaceKey) {return;}
|
2011-02-23 18:30:09 +00:00
|
|
|
|
2014-02-11 13:46:39 +00:00
|
|
|
var i, xya, c, cx, cy, dx, dy, len, angle, box,
|
2014-02-10 13:27:34 +00:00
|
|
|
selected = selectedElements[0],
|
2013-02-14 15:19:46 +00:00
|
|
|
pt = svgedit.math.transformPoint( evt.pageX, evt.pageY, root_sctm ),
|
2010-06-22 14:52:51 +00:00
|
|
|
mouse_x = pt.x * current_zoom,
|
|
|
|
mouse_y = pt.y * current_zoom,
|
2013-02-14 15:19:46 +00:00
|
|
|
shape = svgedit.utilities.getElem(getId());
|
2010-08-23 20:16:27 +00:00
|
|
|
|
2014-01-31 10:40:52 +00:00
|
|
|
var real_x = mouse_x / current_zoom;
|
|
|
|
x = real_x;
|
|
|
|
var real_y = mouse_y / current_zoom;
|
|
|
|
y = real_y;
|
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (curConfig.gridSnapping){
|
2013-02-17 04:58:04 +00:00
|
|
|
x = svgedit.utilities.snapToGrid(x);
|
|
|
|
y = svgedit.utilities.snapToGrid(y);
|
2010-09-09 12:46:34 +00:00
|
|
|
}
|
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
evt.preventDefault();
|
2014-02-11 14:02:48 +00:00
|
|
|
var tlist;
|
2013-02-15 16:51:48 +00:00
|
|
|
switch (current_mode) {
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'select':
|
2010-06-22 14:52:51 +00:00
|
|
|
// we temporarily use a translate on the element(s) being dragged
|
|
|
|
// this transform is removed upon mousing up and the element is
|
|
|
|
// relocated to the new location
|
2010-10-20 13:53:43 +00:00
|
|
|
if (selectedElements[0] !== null) {
|
2014-02-10 13:27:34 +00:00
|
|
|
dx = x - start_x;
|
|
|
|
dy = y - start_y;
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (curConfig.gridSnapping){
|
2013-02-17 04:58:04 +00:00
|
|
|
dx = svgedit.utilities.snapToGrid(dx);
|
|
|
|
dy = svgedit.utilities.snapToGrid(dy);
|
2010-09-09 12:46:34 +00:00
|
|
|
}
|
|
|
|
|
2013-02-15 16:51:48 +00:00
|
|
|
if (evt.shiftKey) {
|
2014-02-10 13:27:34 +00:00
|
|
|
xya = svgedit.math.snapToAngle(start_x, start_y, x, y);
|
2013-02-15 16:51:48 +00:00
|
|
|
x = xya.x;
|
|
|
|
y = xya.y;
|
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
if (dx != 0 || dy != 0) {
|
2014-02-10 13:27:34 +00:00
|
|
|
len = selectedElements.length;
|
2014-01-31 10:40:52 +00:00
|
|
|
for (i = 0; i < len; ++i) {
|
2014-02-11 14:02:48 +00:00
|
|
|
selected = selectedElements[i];
|
2014-01-31 10:40:52 +00:00
|
|
|
if (selected == null) {break;}
|
2011-02-24 16:13:26 +00:00
|
|
|
// if (i==0) {
|
|
|
|
// var box = svgedit.utilities.getBBox(selected);
|
2014-01-31 00:39:35 +00:00
|
|
|
// selectedBBoxes[i].x = box.x + dx;
|
|
|
|
// selectedBBoxes[i].y = box.y + dy;
|
2011-02-24 16:13:26 +00:00
|
|
|
// }
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
// update the dummy transform in our transform list
|
|
|
|
// to be a translate
|
|
|
|
var xform = svgroot.createSVGTransform();
|
2014-02-11 14:02:48 +00:00
|
|
|
tlist = svgedit.transformlist.getTransformList(selected);
|
2010-07-29 17:49:28 +00:00
|
|
|
// Note that if Webkit and there's no ID for this
|
|
|
|
// element, the dummy transform may have gotten lost.
|
|
|
|
// This results in unexpected behaviour
|
|
|
|
|
2013-02-15 16:51:48 +00:00
|
|
|
xform.setTranslate(dx, dy);
|
2013-02-15 15:51:58 +00:00
|
|
|
if (tlist.numberOfItems) {
|
2010-06-22 14:52:51 +00:00
|
|
|
tlist.replaceItem(xform, 0);
|
|
|
|
} else {
|
|
|
|
tlist.appendItem(xform);
|
|
|
|
}
|
|
|
|
|
|
|
|
// update our internal bbox that we're tracking while dragging
|
|
|
|
selectorManager.requestSelector(selected).resize();
|
2009-12-30 18:06:29 +00:00
|
|
|
}
|
2011-01-27 14:23:33 +00:00
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
call('transition', selectedElements);
|
2009-12-30 18:06:29 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
break;
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'multiselect':
|
2010-09-10 13:23:31 +00:00
|
|
|
real_x *= current_zoom;
|
|
|
|
real_y *= current_zoom;
|
2013-02-17 04:58:04 +00:00
|
|
|
svgedit.utilities.assignAttributes(rubberBox, {
|
2010-09-10 13:23:31 +00:00
|
|
|
'x': Math.min(r_start_x, real_x),
|
|
|
|
'y': Math.min(r_start_y, real_y),
|
|
|
|
'width': Math.abs(real_x - r_start_x),
|
|
|
|
'height': Math.abs(real_y - r_start_y)
|
2013-02-15 16:51:48 +00:00
|
|
|
}, 100);
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
// for each selected:
|
|
|
|
// - if newList contains selected, do nothing
|
|
|
|
// - if newList doesn't contain selected, remove it from selected
|
|
|
|
// - for any newList that was not in selectedElements, add it to selected
|
2014-02-10 13:27:34 +00:00
|
|
|
var elemsToRemove = [], elemsToAdd = [],
|
|
|
|
newList = getIntersectionList();
|
|
|
|
len = selectedElements.length;
|
2010-11-30 20:51:07 +00:00
|
|
|
|
2014-01-31 10:40:52 +00:00
|
|
|
for (i = 0; i < len; ++i) {
|
2010-06-22 14:52:51 +00:00
|
|
|
var ind = newList.indexOf(selectedElements[i]);
|
|
|
|
if (ind == -1) {
|
|
|
|
elemsToRemove.push(selectedElements[i]);
|
2013-02-15 15:51:58 +00:00
|
|
|
} else {
|
2010-06-22 14:52:51 +00:00
|
|
|
newList[ind] = null;
|
2009-12-30 18:06:29 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
len = newList.length;
|
2014-01-31 10:40:52 +00:00
|
|
|
for (i = 0; i < len; ++i) {
|
|
|
|
if (newList[i]) {elemsToAdd.push(newList[i]);}
|
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2014-01-31 10:40:52 +00:00
|
|
|
if (elemsToRemove.length > 0) {
|
2010-06-22 14:52:51 +00:00
|
|
|
canvas.removeFromSelection(elemsToRemove);
|
2014-01-31 10:40:52 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2014-01-31 10:40:52 +00:00
|
|
|
if (elemsToAdd.length > 0) {
|
2010-06-30 18:27:36 +00:00
|
|
|
addToSelection(elemsToAdd);
|
2014-01-31 10:40:52 +00:00
|
|
|
}
|
2010-09-23 19:53:43 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
break;
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'resize':
|
2010-06-22 14:52:51 +00:00
|
|
|
// we track the resize bounding box and translate/scale the selected element
|
|
|
|
// while the mouse is down, when mouse goes up, we use this to recalculate
|
|
|
|
// the shape's coordinates
|
2014-02-11 14:02:48 +00:00
|
|
|
tlist = svgedit.transformlist.getTransformList(selected);
|
|
|
|
var hasMatrix = svgedit.math.hasMatrixTransform(tlist);
|
2014-02-11 13:46:39 +00:00
|
|
|
box = hasMatrix ? init_bbox : svgedit.utilities.getBBox(selected);
|
|
|
|
var left = box.x, top = box.y, width = box.width,
|
2014-02-10 13:27:34 +00:00
|
|
|
height = box.height;
|
|
|
|
dx = (x-start_x);
|
|
|
|
dy = (y-start_y);
|
2010-09-09 12:46:34 +00:00
|
|
|
|
2014-01-31 10:40:52 +00:00
|
|
|
if (curConfig.gridSnapping) {
|
2013-02-17 04:58:04 +00:00
|
|
|
dx = svgedit.utilities.snapToGrid(dx);
|
|
|
|
dy = svgedit.utilities.snapToGrid(dy);
|
|
|
|
height = svgedit.utilities.snapToGrid(height);
|
|
|
|
width = svgedit.utilities.snapToGrid(width);
|
2010-09-09 12:46:34 +00:00
|
|
|
}
|
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// if rotated, adjust the dx,dy values
|
2014-02-11 13:32:40 +00:00
|
|
|
angle = svgedit.utilities.getRotationAngle(selected);
|
2010-06-22 14:52:51 +00:00
|
|
|
if (angle) {
|
|
|
|
var r = Math.sqrt( dx*dx + dy*dy ),
|
2013-02-15 16:51:48 +00:00
|
|
|
theta = Math.atan2(dy, dx) - angle * Math.PI / 180.0;
|
2010-06-22 14:52:51 +00:00
|
|
|
dx = r * Math.cos(theta);
|
|
|
|
dy = r * Math.sin(theta);
|
|
|
|
}
|
|
|
|
|
|
|
|
// if not stretching in y direction, set dy to 0
|
|
|
|
// if not stretching in x direction, set dx to 0
|
2014-04-08 03:21:22 +00:00
|
|
|
if (current_resize_mode.indexOf('n')==-1 && current_resize_mode.indexOf('s')==-1) {
|
2010-06-22 14:52:51 +00:00
|
|
|
dy = 0;
|
|
|
|
}
|
2014-04-08 03:21:22 +00:00
|
|
|
if (current_resize_mode.indexOf('e')==-1 && current_resize_mode.indexOf('w')==-1) {
|
2010-06-22 14:52:51 +00:00
|
|
|
dx = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
var ts = null,
|
|
|
|
tx = 0, ty = 0,
|
|
|
|
sy = height ? (height+dy)/height : 1,
|
|
|
|
sx = width ? (width+dx)/width : 1;
|
|
|
|
// if we are dragging on the north side, then adjust the scale factor and ty
|
2014-04-08 03:21:22 +00:00
|
|
|
if (current_resize_mode.indexOf('n') >= 0) {
|
2010-06-22 14:52:51 +00:00
|
|
|
sy = height ? (height-dy)/height : 1;
|
|
|
|
ty = height;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we dragging on the east side, then adjust the scale factor and tx
|
2014-04-08 03:21:22 +00:00
|
|
|
if (current_resize_mode.indexOf('w') >= 0) {
|
2010-06-22 14:52:51 +00:00
|
|
|
sx = width ? (width-dx)/width : 1;
|
|
|
|
tx = width;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update the transform list with translate,scale,translate
|
|
|
|
var translateOrigin = svgroot.createSVGTransform(),
|
|
|
|
scale = svgroot.createSVGTransform(),
|
|
|
|
translateBack = svgroot.createSVGTransform();
|
2010-09-09 12:46:34 +00:00
|
|
|
|
2014-02-17 06:48:40 +00:00
|
|
|
if (curConfig.gridSnapping) {
|
2013-02-17 04:58:04 +00:00
|
|
|
left = svgedit.utilities.snapToGrid(left);
|
|
|
|
tx = svgedit.utilities.snapToGrid(tx);
|
|
|
|
top = svgedit.utilities.snapToGrid(top);
|
|
|
|
ty = svgedit.utilities.snapToGrid(ty);
|
2010-09-09 12:46:34 +00:00
|
|
|
}
|
|
|
|
|
2013-02-15 16:51:48 +00:00
|
|
|
translateOrigin.setTranslate(-(left+tx), -(top+ty));
|
2013-02-15 15:51:58 +00:00
|
|
|
if (evt.shiftKey) {
|
2014-02-10 13:27:34 +00:00
|
|
|
if (sx == 1) {sx = sy;}
|
|
|
|
else {sy = sx;}
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
2013-02-15 16:51:48 +00:00
|
|
|
scale.setScale(sx, sy);
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2013-02-15 16:51:48 +00:00
|
|
|
translateBack.setTranslate(left+tx, top+ty);
|
2013-02-15 15:51:58 +00:00
|
|
|
if (hasMatrix) {
|
2014-02-11 13:32:40 +00:00
|
|
|
var diff = angle ? 1 : 0;
|
2010-06-22 14:52:51 +00:00
|
|
|
tlist.replaceItem(translateOrigin, 2+diff);
|
|
|
|
tlist.replaceItem(scale, 1+diff);
|
2014-02-10 13:27:34 +00:00
|
|
|
tlist.replaceItem(translateBack, Number(diff));
|
2010-06-22 14:52:51 +00:00
|
|
|
} else {
|
|
|
|
var N = tlist.numberOfItems;
|
|
|
|
tlist.replaceItem(translateBack, N-3);
|
|
|
|
tlist.replaceItem(scale, N-2);
|
|
|
|
tlist.replaceItem(translateOrigin, N-1);
|
|
|
|
}
|
2010-09-16 12:56:09 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
selectorManager.requestSelector(selected).resize();
|
2011-01-27 14:23:33 +00:00
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
call('transition', selectedElements);
|
2011-01-27 14:23:33 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
break;
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'zoom':
|
2010-09-10 13:23:31 +00:00
|
|
|
real_x *= current_zoom;
|
|
|
|
real_y *= current_zoom;
|
2013-02-17 04:58:04 +00:00
|
|
|
svgedit.utilities.assignAttributes(rubberBox, {
|
2010-09-10 13:23:31 +00:00
|
|
|
'x': Math.min(r_start_x*current_zoom, real_x),
|
|
|
|
'y': Math.min(r_start_y*current_zoom, real_y),
|
|
|
|
'width': Math.abs(real_x - r_start_x*current_zoom),
|
|
|
|
'height': Math.abs(real_y - r_start_y*current_zoom)
|
2013-02-15 16:51:48 +00:00
|
|
|
}, 100);
|
2010-06-22 14:52:51 +00:00
|
|
|
break;
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'text':
|
2013-02-17 04:58:04 +00:00
|
|
|
svgedit.utilities.assignAttributes(shape,{
|
2010-06-22 14:52:51 +00:00
|
|
|
'x': x,
|
|
|
|
'y': y
|
2013-02-15 16:51:48 +00:00
|
|
|
}, 1000);
|
2010-06-22 14:52:51 +00:00
|
|
|
break;
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'line':
|
2010-06-22 14:52:51 +00:00
|
|
|
// Opera has a problem with suspendRedraw() apparently
|
|
|
|
var handle = null;
|
2014-02-10 13:27:34 +00:00
|
|
|
if (!window.opera) {svgroot.suspendRedraw(1000);}
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2014-02-17 06:48:40 +00:00
|
|
|
if (curConfig.gridSnapping) {
|
2013-02-17 04:58:04 +00:00
|
|
|
x = svgedit.utilities.snapToGrid(x);
|
|
|
|
y = svgedit.utilities.snapToGrid(y);
|
2010-09-09 12:46:34 +00:00
|
|
|
}
|
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
var x2 = x;
|
|
|
|
var y2 = y;
|
|
|
|
|
2013-02-15 16:51:48 +00:00
|
|
|
if (evt.shiftKey) {
|
2014-02-10 13:27:34 +00:00
|
|
|
xya = svgedit.math.snapToAngle(start_x, start_y, x2, y2);
|
2013-02-15 16:51:48 +00:00
|
|
|
x2 = xya.x;
|
|
|
|
y2 = xya.y;
|
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
shape.setAttributeNS(null, 'x2', x2);
|
|
|
|
shape.setAttributeNS(null, 'y2', y2);
|
2014-02-10 13:27:34 +00:00
|
|
|
if (!window.opera) {svgroot.unsuspendRedraw(handle);}
|
2010-06-22 14:52:51 +00:00
|
|
|
break;
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'foreignObject':
|
2010-06-22 14:52:51 +00:00
|
|
|
// fall through
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'square':
|
2010-06-22 14:52:51 +00:00
|
|
|
// fall through
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'rect':
|
2010-06-22 14:52:51 +00:00
|
|
|
// fall through
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'image':
|
2010-06-22 14:52:51 +00:00
|
|
|
var square = (current_mode == 'square') || evt.shiftKey,
|
|
|
|
w = Math.abs(x - start_x),
|
|
|
|
h = Math.abs(y - start_y),
|
|
|
|
new_x, new_y;
|
2013-02-15 15:51:58 +00:00
|
|
|
if (square) {
|
2010-06-22 14:52:51 +00:00
|
|
|
w = h = Math.max(w, h);
|
|
|
|
new_x = start_x < x ? start_x : start_x - w;
|
|
|
|
new_y = start_y < y ? start_y : start_y - h;
|
|
|
|
} else {
|
2013-02-15 16:51:48 +00:00
|
|
|
new_x = Math.min(start_x, x);
|
|
|
|
new_y = Math.min(start_y, y);
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
2009-12-30 18:06:29 +00:00
|
|
|
|
2014-02-17 06:48:40 +00:00
|
|
|
if (curConfig.gridSnapping) {
|
2013-02-17 04:58:04 +00:00
|
|
|
w = svgedit.utilities.snapToGrid(w);
|
|
|
|
h = svgedit.utilities.snapToGrid(h);
|
|
|
|
new_x = svgedit.utilities.snapToGrid(new_x);
|
|
|
|
new_y = svgedit.utilities.snapToGrid(new_y);
|
2010-09-09 12:46:34 +00:00
|
|
|
}
|
|
|
|
|
2013-02-17 04:58:04 +00:00
|
|
|
svgedit.utilities.assignAttributes(shape,{
|
2010-06-22 14:52:51 +00:00
|
|
|
'width': w,
|
|
|
|
'height': h,
|
|
|
|
'x': new_x,
|
|
|
|
'y': new_y
|
|
|
|
},1000);
|
|
|
|
|
|
|
|
break;
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'circle':
|
|
|
|
c = $(shape).attr(['cx', 'cy']);
|
2014-02-10 13:27:34 +00:00
|
|
|
cx = c.cx;
|
|
|
|
cy = c.cy;
|
|
|
|
var rad = Math.sqrt( (x-cx)*(x-cx) + (y-cy)*(y-cy) );
|
2014-02-17 06:48:40 +00:00
|
|
|
if (curConfig.gridSnapping) {
|
2013-02-17 04:58:04 +00:00
|
|
|
rad = svgedit.utilities.snapToGrid(rad);
|
2010-09-09 12:46:34 +00:00
|
|
|
}
|
2014-04-08 03:21:22 +00:00
|
|
|
shape.setAttributeNS(null, 'r', rad);
|
2010-06-22 14:52:51 +00:00
|
|
|
break;
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'ellipse':
|
|
|
|
c = $(shape).attr(['cx', 'cy']);
|
2014-02-10 13:27:34 +00:00
|
|
|
cx = c.cx;
|
|
|
|
cy = c.cy;
|
2010-06-22 14:52:51 +00:00
|
|
|
// Opera has a problem with suspendRedraw() apparently
|
|
|
|
handle = null;
|
2014-02-10 13:27:34 +00:00
|
|
|
if (!window.opera) {svgroot.suspendRedraw(1000);}
|
2014-02-17 06:48:40 +00:00
|
|
|
if (curConfig.gridSnapping) {
|
2013-02-17 04:58:04 +00:00
|
|
|
x = svgedit.utilities.snapToGrid(x);
|
|
|
|
cx = svgedit.utilities.snapToGrid(cx);
|
|
|
|
y = svgedit.utilities.snapToGrid(y);
|
|
|
|
cy = svgedit.utilities.snapToGrid(cy);
|
2010-09-09 12:46:34 +00:00
|
|
|
}
|
2014-04-08 03:21:22 +00:00
|
|
|
shape.setAttributeNS(null, 'rx', Math.abs(x - cx) );
|
2010-06-22 14:52:51 +00:00
|
|
|
var ry = Math.abs(evt.shiftKey?(x - cx):(y - cy));
|
2014-04-08 03:21:22 +00:00
|
|
|
shape.setAttributeNS(null, 'ry', ry );
|
2014-02-10 13:27:34 +00:00
|
|
|
if (!window.opera) {svgroot.unsuspendRedraw(handle);}
|
2010-06-22 14:52:51 +00:00
|
|
|
break;
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'fhellipse':
|
|
|
|
case 'fhrect':
|
2010-09-17 19:34:23 +00:00
|
|
|
freehand.minx = Math.min(real_x, freehand.minx);
|
|
|
|
freehand.maxx = Math.max(real_x, freehand.maxx);
|
|
|
|
freehand.miny = Math.min(real_y, freehand.miny);
|
|
|
|
freehand.maxy = Math.max(real_y, freehand.maxy);
|
2010-06-22 14:52:51 +00:00
|
|
|
// break; missing on purpose
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'fhpath':
|
|
|
|
// d_attr += + real_x + ',' + real_y + ' ';
|
|
|
|
// shape.setAttributeNS(null, 'points', d_attr);
|
2013-02-13 16:10:15 +00:00
|
|
|
end.x = real_x; end.y = real_y;
|
2014-02-10 13:27:34 +00:00
|
|
|
if (controllPoint2.x && controllPoint2.y) {
|
|
|
|
for (i = 0; i < STEP_COUNT - 1; i++) {
|
2013-02-13 16:10:15 +00:00
|
|
|
parameter = i / STEP_COUNT;
|
|
|
|
nextParameter = (i + 1) / STEP_COUNT;
|
|
|
|
bSpline = getBsplinePoint(nextParameter);
|
|
|
|
nextPos = bSpline;
|
|
|
|
bSpline = getBsplinePoint(parameter);
|
|
|
|
sumDistance += Math.sqrt((nextPos.x - bSpline.x) * (nextPos.x - bSpline.x) + (nextPos.y - bSpline.y) * (nextPos.y - bSpline.y));
|
|
|
|
if (sumDistance > THRESHOLD_DIST) {
|
2014-04-08 03:21:22 +00:00
|
|
|
d_attr += + bSpline.x + ',' + bSpline.y + ' ';
|
|
|
|
shape.setAttributeNS(null, 'points', d_attr);
|
2013-02-13 16:10:15 +00:00
|
|
|
sumDistance -= THRESHOLD_DIST;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-02-15 16:51:48 +00:00
|
|
|
controllPoint2 = {x:controllPoint1.x, y:controllPoint1.y};
|
|
|
|
controllPoint1 = {x:start.x, y:start.y};
|
|
|
|
start = {x:end.x, y:end.y};
|
2010-06-22 14:52:51 +00:00
|
|
|
break;
|
|
|
|
// update path stretch line coordinates
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'path':
|
2010-06-22 14:52:51 +00:00
|
|
|
// fall through
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'pathedit':
|
2010-06-22 14:52:51 +00:00
|
|
|
x *= current_zoom;
|
|
|
|
y *= current_zoom;
|
|
|
|
|
2014-02-17 06:48:40 +00:00
|
|
|
if (curConfig.gridSnapping) {
|
2013-02-17 04:58:04 +00:00
|
|
|
x = svgedit.utilities.snapToGrid(x);
|
|
|
|
y = svgedit.utilities.snapToGrid(y);
|
|
|
|
start_x = svgedit.utilities.snapToGrid(start_x);
|
|
|
|
start_y = svgedit.utilities.snapToGrid(start_y);
|
2010-09-09 12:46:34 +00:00
|
|
|
}
|
2013-02-15 15:51:58 +00:00
|
|
|
if (evt.shiftKey) {
|
2011-02-09 16:30:11 +00:00
|
|
|
var path = svgedit.path.path;
|
2014-02-10 13:27:34 +00:00
|
|
|
var x1, y1;
|
2013-02-15 15:51:58 +00:00
|
|
|
if (path) {
|
2014-02-10 13:27:34 +00:00
|
|
|
x1 = path.dragging?path.dragging[0]:start_x;
|
|
|
|
y1 = path.dragging?path.dragging[1]:start_y;
|
2011-02-09 16:30:11 +00:00
|
|
|
} else {
|
2014-02-10 13:27:34 +00:00
|
|
|
x1 = start_x;
|
|
|
|
y1 = start_y;
|
2011-02-09 16:30:11 +00:00
|
|
|
}
|
2014-02-10 13:27:34 +00:00
|
|
|
xya = svgedit.math.snapToAngle(x1, y1, x, y);
|
2013-02-15 16:51:48 +00:00
|
|
|
x = xya.x;
|
|
|
|
y = xya.y;
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (rubberBox && rubberBox.getAttribute('display') !== 'none') {
|
2010-10-25 16:54:16 +00:00
|
|
|
real_x *= current_zoom;
|
|
|
|
real_y *= current_zoom;
|
2013-02-17 04:58:04 +00:00
|
|
|
svgedit.utilities.assignAttributes(rubberBox, {
|
2010-10-25 16:54:16 +00:00
|
|
|
'x': Math.min(r_start_x*current_zoom, real_x),
|
|
|
|
'y': Math.min(r_start_y*current_zoom, real_y),
|
|
|
|
'width': Math.abs(real_x - r_start_x*current_zoom),
|
|
|
|
'height': Math.abs(real_y - r_start_y*current_zoom)
|
|
|
|
},100);
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
pathActions.mouseMove(x, y);
|
|
|
|
|
|
|
|
break;
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'textedit':
|
2010-06-22 14:52:51 +00:00
|
|
|
x *= current_zoom;
|
|
|
|
y *= current_zoom;
|
2014-01-31 00:39:35 +00:00
|
|
|
// if (rubberBox && rubberBox.getAttribute('display') != 'none') {
|
|
|
|
// svgedit.utilities.assignAttributes(rubberBox, {
|
|
|
|
// 'x': Math.min(start_x,x),
|
|
|
|
// 'y': Math.min(start_y,y),
|
|
|
|
// 'width': Math.abs(x-start_x),
|
|
|
|
// 'height': Math.abs(y-start_y)
|
|
|
|
// },100);
|
|
|
|
// }
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
textActions.mouseMove(mouse_x, mouse_y);
|
|
|
|
|
|
|
|
break;
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'rotate':
|
2014-02-11 13:46:39 +00:00
|
|
|
box = svgedit.utilities.getBBox(selected);
|
2014-02-10 13:27:34 +00:00
|
|
|
cx = box.x + box.width/2;
|
|
|
|
cy = box.y + box.height/2;
|
|
|
|
var m = svgedit.math.getMatrix(selected),
|
2013-02-15 16:51:48 +00:00
|
|
|
center = svgedit.math.transformPoint(cx, cy, m);
|
2010-06-22 14:52:51 +00:00
|
|
|
cx = center.x;
|
|
|
|
cy = center.y;
|
2014-02-11 13:32:40 +00:00
|
|
|
angle = ((Math.atan2(cy-y, cx-x) * (180/Math.PI))-90) % 360;
|
2014-02-17 06:48:40 +00:00
|
|
|
if (curConfig.gridSnapping) {
|
2013-02-17 04:58:04 +00:00
|
|
|
angle = svgedit.utilities.snapToGrid(angle);
|
2010-09-09 12:46:34 +00:00
|
|
|
}
|
2013-02-15 15:51:58 +00:00
|
|
|
if (evt.shiftKey) { // restrict rotations to nice angles (WRS)
|
2010-06-22 14:52:51 +00:00
|
|
|
var snap = 45;
|
|
|
|
angle= Math.round(angle/snap)*snap;
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas.setRotationAngle(angle<-180?(360+angle):angle, true);
|
2014-04-08 03:21:22 +00:00
|
|
|
call('transition', selectedElements);
|
2010-06-22 14:52:51 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2009-11-25 19:35:06 +00:00
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
runExtensions('mouseMove', {
|
2010-06-22 14:52:51 +00:00
|
|
|
event: evt,
|
|
|
|
mouse_x: mouse_x,
|
|
|
|
mouse_y: mouse_y,
|
|
|
|
selected: selected
|
|
|
|
});
|
|
|
|
|
|
|
|
}; // mouseMove()
|
|
|
|
|
2010-06-22 18:17:42 +00:00
|
|
|
// - in create mode, the element's opacity is set properly, we create an InsertElementCommand
|
2014-01-31 10:40:52 +00:00
|
|
|
// and store it on the Undo stack
|
2010-06-22 18:17:42 +00:00
|
|
|
// - in move/resize mode, the element's attributes which were affected by the move/resize are
|
2014-01-31 10:40:52 +00:00
|
|
|
// identified, a ChangeElementCommand is created and stored on the stack for those attrs
|
|
|
|
// this is done in when we recalculate the selected dimensions()
|
2013-02-15 15:51:58 +00:00
|
|
|
var mouseUp = function(evt) {
|
2014-02-10 13:27:34 +00:00
|
|
|
if (evt.button === 2) {return;}
|
2010-06-22 14:52:51 +00:00
|
|
|
var tempJustSelected = justSelected;
|
|
|
|
justSelected = null;
|
2014-02-10 13:27:34 +00:00
|
|
|
if (!started) {return;}
|
2014-02-11 13:32:40 +00:00
|
|
|
var pt = svgedit.math.transformPoint(evt.pageX, evt.pageY, root_sctm),
|
2010-06-22 14:52:51 +00:00
|
|
|
mouse_x = pt.x * current_zoom,
|
|
|
|
mouse_y = pt.y * current_zoom,
|
|
|
|
x = mouse_x / current_zoom,
|
|
|
|
y = mouse_y / current_zoom,
|
2013-02-14 15:19:46 +00:00
|
|
|
element = svgedit.utilities.getElem(getId()),
|
2010-06-22 14:52:51 +00:00
|
|
|
keep = false;
|
2010-09-09 12:46:34 +00:00
|
|
|
|
2010-09-10 13:23:31 +00:00
|
|
|
var real_x = x;
|
|
|
|
var real_y = y;
|
2010-11-07 20:14:05 +00:00
|
|
|
|
2010-10-19 17:20:28 +00:00
|
|
|
// TODO: Make true when in multi-unit mode
|
|
|
|
var useUnit = false; // (curConfig.baseUnit !== 'px');
|
2010-06-22 14:52:51 +00:00
|
|
|
started = false;
|
2014-02-11 13:32:40 +00:00
|
|
|
var attrs, t;
|
2014-02-10 13:27:34 +00:00
|
|
|
switch (current_mode) {
|
2010-06-22 14:52:51 +00:00
|
|
|
// intentionally fall-through to select here
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'resize':
|
|
|
|
case 'multiselect':
|
2010-06-22 14:52:51 +00:00
|
|
|
if (rubberBox != null) {
|
2014-04-08 03:21:22 +00:00
|
|
|
rubberBox.setAttribute('display', 'none');
|
2010-06-22 14:52:51 +00:00
|
|
|
curBBoxes = [];
|
|
|
|
}
|
2014-04-08 03:21:22 +00:00
|
|
|
current_mode = 'select';
|
|
|
|
case 'select':
|
2010-06-22 14:52:51 +00:00
|
|
|
if (selectedElements[0] != null) {
|
|
|
|
// if we only have one selected element
|
|
|
|
if (selectedElements[1] == null) {
|
|
|
|
// set our current stroke/fill properties to the element's
|
|
|
|
var selected = selectedElements[0];
|
2011-03-09 20:00:49 +00:00
|
|
|
switch ( selected.tagName ) {
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'g':
|
|
|
|
case 'use':
|
|
|
|
case 'image':
|
|
|
|
case 'foreignObject':
|
2011-03-09 20:00:49 +00:00
|
|
|
break;
|
|
|
|
default:
|
2014-04-08 03:21:22 +00:00
|
|
|
cur_properties.fill = selected.getAttribute('fill');
|
|
|
|
cur_properties.fill_opacity = selected.getAttribute('fill-opacity');
|
|
|
|
cur_properties.stroke = selected.getAttribute('stroke');
|
|
|
|
cur_properties.stroke_opacity = selected.getAttribute('stroke-opacity');
|
|
|
|
cur_properties.stroke_width = selected.getAttribute('stroke-width');
|
|
|
|
cur_properties.stroke_dasharray = selected.getAttribute('stroke-dasharray');
|
|
|
|
cur_properties.stroke_linejoin = selected.getAttribute('stroke-linejoin');
|
|
|
|
cur_properties.stroke_linecap = selected.getAttribute('stroke-linecap');
|
2009-12-30 18:06:29 +00:00
|
|
|
}
|
2011-03-09 20:00:49 +00:00
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
if (selected.tagName == 'text') {
|
|
|
|
cur_text.font_size = selected.getAttribute('font-size');
|
|
|
|
cur_text.font_family = selected.getAttribute('font-family');
|
2009-12-30 18:06:29 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
selectorManager.requestSelector(selected).showGrips(true);
|
|
|
|
|
|
|
|
// This shouldn't be necessary as it was done on mouseDown...
|
2014-04-08 03:21:22 +00:00
|
|
|
// call('selected', [selected]);
|
2010-02-03 14:50:48 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
// always recalculate dimensions to strip off stray identity transforms
|
|
|
|
recalculateAllSelectedDimensions();
|
|
|
|
// if it was being dragged/resized
|
2010-09-10 13:58:34 +00:00
|
|
|
if (real_x != r_start_x || real_y != r_start_y) {
|
2014-02-10 13:27:34 +00:00
|
|
|
var i, len = selectedElements.length;
|
|
|
|
for (i = 0; i < len; ++i) {
|
|
|
|
if (selectedElements[i] == null) {break;}
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!selectedElements[i].firstChild) {
|
2010-06-22 14:52:51 +00:00
|
|
|
// Not needed for groups (incorrectly resizes elems), possibly not needed at all?
|
|
|
|
selectorManager.requestSelector(selectedElements[i]).resize();
|
2009-12-30 18:06:29 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
2009-12-30 18:06:29 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
// no change in position/size, so maybe we should move to pathedit
|
|
|
|
else {
|
2014-02-11 13:32:40 +00:00
|
|
|
t = evt.target;
|
2014-04-08 03:21:22 +00:00
|
|
|
if (selectedElements[0].nodeName === 'path' && selectedElements[1] == null) {
|
2010-10-15 19:41:37 +00:00
|
|
|
pathActions.select(selectedElements[0]);
|
2010-06-22 14:52:51 +00:00
|
|
|
} // if it was a path
|
|
|
|
// else, if it was selected and this is a shift-click, remove it from selection
|
|
|
|
else if (evt.shiftKey) {
|
2013-02-15 15:51:58 +00:00
|
|
|
if (tempJustSelected != t) {
|
2010-06-22 14:52:51 +00:00
|
|
|
canvas.removeFromSelection([t]);
|
2009-12-30 18:06:29 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
} // no change in mouse position
|
2010-10-11 17:16:10 +00:00
|
|
|
|
|
|
|
// Remove non-scaling stroke
|
2013-02-15 15:51:58 +00:00
|
|
|
if (svgedit.browser.supportsNonScalingStroke()) {
|
2010-10-11 17:16:10 +00:00
|
|
|
var elem = selectedElements[0];
|
2011-02-04 08:02:46 +00:00
|
|
|
if (elem) {
|
2010-10-11 17:16:10 +00:00
|
|
|
elem.removeAttribute('style');
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.utilities.walkTree(elem, function(elem) {
|
|
|
|
elem.removeAttribute('style');
|
|
|
|
});
|
|
|
|
}
|
2010-10-11 17:16:10 +00:00
|
|
|
}
|
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
return;
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'zoom':
|
2010-06-22 14:52:51 +00:00
|
|
|
if (rubberBox != null) {
|
2014-04-08 03:21:22 +00:00
|
|
|
rubberBox.setAttribute('display', 'none');
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
2014-02-10 13:27:34 +00:00
|
|
|
var factor = evt.shiftKey ? 0.5 : 2;
|
2014-04-08 03:21:22 +00:00
|
|
|
call('zoomed', {
|
2010-09-10 13:23:31 +00:00
|
|
|
'x': Math.min(r_start_x, real_x),
|
|
|
|
'y': Math.min(r_start_y, real_y),
|
|
|
|
'width': Math.abs(real_x - r_start_x),
|
|
|
|
'height': Math.abs(real_y - r_start_y),
|
2010-06-22 14:52:51 +00:00
|
|
|
'factor': factor
|
|
|
|
});
|
|
|
|
return;
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'fhpath':
|
2010-06-22 14:52:51 +00:00
|
|
|
// Check that the path contains at least 2 points; a degenerate one-point path
|
|
|
|
// causes problems.
|
|
|
|
// Webkit ignores how we set the points attribute with commas and uses space
|
|
|
|
// to separate all coordinates, see https://bugs.webkit.org/show_bug.cgi?id=29870
|
2013-02-15 15:51:58 +00:00
|
|
|
sumDistance = 0;
|
2013-02-13 16:10:15 +00:00
|
|
|
controllPoint2 = {x:0, y:0};
|
|
|
|
controllPoint1 = {x:0, y:0};
|
2013-02-15 15:51:58 +00:00
|
|
|
start = {x:0, y:0};
|
|
|
|
end = {x:0, y:0};
|
2010-06-22 14:52:51 +00:00
|
|
|
var coords = element.getAttribute('points');
|
|
|
|
var commaIndex = coords.indexOf(',');
|
|
|
|
if (commaIndex >= 0) {
|
|
|
|
keep = coords.indexOf(',', commaIndex+1) >= 0;
|
|
|
|
} else {
|
|
|
|
keep = coords.indexOf(' ', coords.indexOf(' ')+1) >= 0;
|
|
|
|
}
|
|
|
|
if (keep) {
|
|
|
|
element = pathActions.smoothPolylineIntoPath(element);
|
|
|
|
}
|
|
|
|
break;
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'line':
|
|
|
|
attrs = $(element).attr(['x1', 'x2', 'y1', 'y2']);
|
2010-06-22 14:52:51 +00:00
|
|
|
keep = (attrs.x1 != attrs.x2 || attrs.y1 != attrs.y2);
|
|
|
|
break;
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'foreignObject':
|
|
|
|
case 'square':
|
|
|
|
case 'rect':
|
|
|
|
case 'image':
|
|
|
|
attrs = $(element).attr(['width', 'height']);
|
2010-06-22 14:52:51 +00:00
|
|
|
// Image should be kept regardless of size (use inherit dimensions later)
|
2014-04-08 03:21:22 +00:00
|
|
|
keep = (attrs.width != 0 || attrs.height != 0) || current_mode === 'image';
|
2010-06-22 14:52:51 +00:00
|
|
|
break;
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'circle':
|
2010-06-22 14:52:51 +00:00
|
|
|
keep = (element.getAttribute('r') != 0);
|
|
|
|
break;
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'ellipse':
|
|
|
|
attrs = $(element).attr(['rx', 'ry']);
|
2010-06-22 14:52:51 +00:00
|
|
|
keep = (attrs.rx != null || attrs.ry != null);
|
|
|
|
break;
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'fhellipse':
|
2010-06-22 14:52:51 +00:00
|
|
|
if ((freehand.maxx - freehand.minx) > 0 &&
|
|
|
|
(freehand.maxy - freehand.miny) > 0) {
|
|
|
|
element = addSvgElementFromJson({
|
2014-04-08 03:21:22 +00:00
|
|
|
element: 'ellipse',
|
|
|
|
curStyles: true,
|
|
|
|
attr: {
|
|
|
|
cx: (freehand.minx + freehand.maxx) / 2,
|
|
|
|
cy: (freehand.miny + freehand.maxy) / 2,
|
|
|
|
rx: (freehand.maxx - freehand.minx) / 2,
|
|
|
|
ry: (freehand.maxy - freehand.miny) / 2,
|
|
|
|
id: getId()
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
});
|
2014-04-08 03:21:22 +00:00
|
|
|
call('changed',[element]);
|
2009-12-30 18:06:29 +00:00
|
|
|
keep = true;
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
break;
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'fhrect':
|
2010-06-22 14:52:51 +00:00
|
|
|
if ((freehand.maxx - freehand.minx) > 0 &&
|
|
|
|
(freehand.maxy - freehand.miny) > 0) {
|
|
|
|
element = addSvgElementFromJson({
|
2014-04-08 03:21:22 +00:00
|
|
|
element: 'rect',
|
|
|
|
curStyles: true,
|
|
|
|
attr: {
|
|
|
|
x: freehand.minx,
|
|
|
|
y: freehand.miny,
|
|
|
|
width: (freehand.maxx - freehand.minx),
|
|
|
|
height: (freehand.maxy - freehand.miny),
|
|
|
|
id: getId()
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
});
|
2014-04-08 03:21:22 +00:00
|
|
|
call('changed',[element]);
|
2009-12-30 18:06:29 +00:00
|
|
|
keep = true;
|
2010-02-18 15:42:10 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
break;
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'text':
|
2010-06-22 14:52:51 +00:00
|
|
|
keep = true;
|
2010-09-28 16:57:26 +00:00
|
|
|
selectOnly([element]);
|
2010-06-22 14:52:51 +00:00
|
|
|
textActions.start(element);
|
|
|
|
break;
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'path':
|
2010-06-22 14:52:51 +00:00
|
|
|
// set element to null here so that it is not removed nor finalized
|
2009-12-30 18:06:29 +00:00
|
|
|
element = null;
|
2010-06-22 14:52:51 +00:00
|
|
|
// continue to be set to true so that mouseMove happens
|
|
|
|
started = true;
|
2009-12-30 18:06:29 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
var res = pathActions.mouseUp(evt, element, mouse_x, mouse_y);
|
2013-02-15 15:51:58 +00:00
|
|
|
element = res.element;
|
2010-06-22 14:52:51 +00:00
|
|
|
keep = res.keep;
|
|
|
|
break;
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'pathedit':
|
2010-06-22 14:52:51 +00:00
|
|
|
keep = true;
|
|
|
|
element = null;
|
|
|
|
pathActions.mouseUp(evt);
|
|
|
|
break;
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'textedit':
|
2010-06-22 14:52:51 +00:00
|
|
|
keep = false;
|
|
|
|
element = null;
|
|
|
|
textActions.mouseUp(evt, mouse_x, mouse_y);
|
|
|
|
break;
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'rotate':
|
2010-06-22 14:52:51 +00:00
|
|
|
keep = true;
|
|
|
|
element = null;
|
2014-04-08 03:21:22 +00:00
|
|
|
current_mode = 'select';
|
2010-11-12 19:08:29 +00:00
|
|
|
var batchCmd = canvas.undoMgr.finishUndoableChange();
|
2010-06-22 14:52:51 +00:00
|
|
|
if (!batchCmd.isEmpty()) {
|
|
|
|
addCommandToHistory(batchCmd);
|
2009-12-30 18:06:29 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
// perform recalculation to weed out any stray identity transforms that might get stuck
|
|
|
|
recalculateAllSelectedDimensions();
|
2014-04-08 03:21:22 +00:00
|
|
|
call('changed', selectedElements);
|
2010-06-22 14:52:51 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// This could occur in an extension
|
|
|
|
break;
|
|
|
|
}
|
2010-01-29 20:30:10 +00:00
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
var ext_result = runExtensions('mouseUp', {
|
2010-06-22 14:52:51 +00:00
|
|
|
event: evt,
|
|
|
|
mouse_x: mouse_x,
|
|
|
|
mouse_y: mouse_y
|
|
|
|
}, true);
|
2009-12-31 15:25:57 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
$.each(ext_result, function(i, r) {
|
2013-02-15 15:51:58 +00:00
|
|
|
if (r) {
|
2010-06-22 14:52:51 +00:00
|
|
|
keep = r.keep || keep;
|
|
|
|
element = r.element;
|
|
|
|
started = r.started || started;
|
2010-01-18 19:14:08 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
if (!keep && element != null) {
|
2011-01-25 16:15:29 +00:00
|
|
|
getCurrentDrawing().releaseId(getId());
|
2010-06-22 14:52:51 +00:00
|
|
|
element.parentNode.removeChild(element);
|
|
|
|
element = null;
|
2010-04-12 15:59:46 +00:00
|
|
|
|
2014-02-11 13:32:40 +00:00
|
|
|
t = evt.target;
|
2010-04-12 15:59:46 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// if this element is in a group, go up until we reach the top-level group
|
|
|
|
// just below the layer groups
|
|
|
|
// TODO: once we implement links, we also would have to check for <a> elements
|
2014-04-08 03:21:22 +00:00
|
|
|
while (t.parentNode.parentNode.tagName == 'g') {
|
2010-06-22 14:52:51 +00:00
|
|
|
t = t.parentNode;
|
|
|
|
}
|
|
|
|
// if we are not in the middle of creating a path, and we've clicked on some shape,
|
|
|
|
// then go to Select mode.
|
|
|
|
// WebKit returns <div> when the canvas is clicked, Firefox/Opera return <svg>
|
2014-04-08 03:21:22 +00:00
|
|
|
if ( (current_mode != 'path' || !drawn_path) &&
|
|
|
|
t.parentNode.id != 'selectorParentGroup' &&
|
|
|
|
t.id != 'svgcanvas' && t.id != 'svgroot')
|
2010-06-22 14:52:51 +00:00
|
|
|
{
|
|
|
|
// switch into "select" mode if we've clicked on an element
|
2014-04-08 03:21:22 +00:00
|
|
|
canvas.setMode('select');
|
2010-09-17 20:33:33 +00:00
|
|
|
selectOnly([t], true);
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else if (element != null) {
|
|
|
|
canvas.addedNew = true;
|
2010-09-28 16:57:26 +00:00
|
|
|
|
2014-02-11 13:32:40 +00:00
|
|
|
if (useUnit) {svgedit.units.convertAttrs(element);}
|
2010-10-05 17:13:00 +00:00
|
|
|
|
2014-02-11 13:32:40 +00:00
|
|
|
var ani_dur = 0.2, c_ani;
|
2013-02-15 15:51:58 +00:00
|
|
|
if (opac_ani.beginElement && element.getAttribute('opacity') != cur_shape.opacity) {
|
2010-06-22 14:52:51 +00:00
|
|
|
c_ani = $(opac_ani).clone().attr({
|
|
|
|
to: cur_shape.opacity,
|
|
|
|
dur: ani_dur
|
|
|
|
}).appendTo(element);
|
2010-07-29 15:09:49 +00:00
|
|
|
try {
|
|
|
|
// Fails in FF4 on foreignObject
|
|
|
|
c_ani[0].beginElement();
|
|
|
|
} catch(e){}
|
2010-06-22 14:52:51 +00:00
|
|
|
} else {
|
|
|
|
ani_dur = 0;
|
2010-04-12 15:59:46 +00:00
|
|
|
}
|
2010-04-30 15:06:47 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// Ideally this would be done on the endEvent of the animation,
|
|
|
|
// but that doesn't seem to be supported in Webkit
|
|
|
|
setTimeout(function() {
|
2014-02-11 13:32:40 +00:00
|
|
|
if (c_ani) {c_ani.remove();}
|
2014-04-08 03:21:22 +00:00
|
|
|
element.setAttribute('opacity', cur_shape.opacity);
|
|
|
|
element.setAttribute('style', 'pointer-events:inherit');
|
2010-06-22 14:52:51 +00:00
|
|
|
cleanupElement(element);
|
2014-04-08 03:21:22 +00:00
|
|
|
if (current_mode === 'path') {
|
2010-06-22 14:52:51 +00:00
|
|
|
pathActions.toEditMode(element);
|
2013-02-15 15:51:58 +00:00
|
|
|
} else if (curConfig.selectNew) {
|
|
|
|
selectOnly([element], true);
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
// we create the insert command that is stored on the stack
|
|
|
|
// undo means to call cmd.unapply(), redo means to call cmd.apply()
|
2013-02-17 08:21:07 +00:00
|
|
|
addCommandToHistory(new svgedit.history.InsertElementCommand(element));
|
2010-09-09 17:38:33 +00:00
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
call('changed',[element]);
|
2010-06-22 14:52:51 +00:00
|
|
|
}, ani_dur * 1000);
|
2010-04-12 15:59:46 +00:00
|
|
|
}
|
|
|
|
|
2013-02-20 06:29:25 +00:00
|
|
|
startTransform = null;
|
2010-06-22 14:52:51 +00:00
|
|
|
};
|
2010-09-22 18:42:24 +00:00
|
|
|
|
|
|
|
var dblClick = function(evt) {
|
|
|
|
var evt_target = evt.target;
|
|
|
|
var parent = evt_target.parentNode;
|
|
|
|
|
|
|
|
// Do nothing if already in current group
|
2014-02-11 13:32:40 +00:00
|
|
|
if (parent === current_group) {return;}
|
2010-09-22 18:42:24 +00:00
|
|
|
|
|
|
|
var mouse_target = getMouseTarget(evt);
|
2010-10-15 19:41:37 +00:00
|
|
|
var tagName = mouse_target.tagName;
|
2010-09-22 18:42:24 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (tagName === 'text' && current_mode !== 'textedit') {
|
2013-02-14 15:19:46 +00:00
|
|
|
var pt = svgedit.math.transformPoint( evt.pageX, evt.pageY, root_sctm );
|
2010-09-28 16:57:26 +00:00
|
|
|
textActions.select(mouse_target, pt.x, pt.y);
|
|
|
|
}
|
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
if ((tagName === 'g' || tagName === 'a') && svgedit.utilities.getRotationAngle(mouse_target)) {
|
2010-10-07 20:20:43 +00:00
|
|
|
// TODO: Allow method of in-group editing without having to do
|
|
|
|
// this (similar to editing rotated paths)
|
|
|
|
|
|
|
|
// Ungroup and regroup
|
2010-10-20 18:07:30 +00:00
|
|
|
pushGroupProperties(mouse_target);
|
2010-10-07 20:20:43 +00:00
|
|
|
mouse_target = selectedElements[0];
|
|
|
|
clearSelection(true);
|
2010-09-22 18:42:24 +00:00
|
|
|
}
|
2010-09-25 18:24:21 +00:00
|
|
|
// Reset context
|
2013-02-15 15:51:58 +00:00
|
|
|
if (current_group) {
|
2010-09-25 18:24:21 +00:00
|
|
|
leaveContext();
|
|
|
|
}
|
2010-09-22 18:42:24 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if ((parent.tagName !== 'g' && parent.tagName !== 'a') ||
|
2011-01-25 16:15:29 +00:00
|
|
|
parent === getCurrentDrawing().getCurrentLayer() ||
|
|
|
|
mouse_target === selectorManager.selectorParentGroup)
|
|
|
|
{
|
2010-09-22 18:42:24 +00:00
|
|
|
// Escape from in-group edit
|
|
|
|
return;
|
|
|
|
}
|
2010-09-23 19:53:43 +00:00
|
|
|
setContext(mouse_target);
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
// prevent links from being followed in the canvas
|
|
|
|
var handleLinkInCanvas = function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2011-01-17 22:11:35 +00:00
|
|
|
// Added mouseup to the container here.
|
|
|
|
// TODO(codedread): Figure out why after the Closure compiler, the window mouseup is ignored.
|
|
|
|
$(container).mousedown(mouseDown).mousemove(mouseMove).click(handleLinkInCanvas).dblclick(dblClick).mouseup(mouseUp);
|
|
|
|
// $(window).mouseup(mouseUp);
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2013-01-16 11:42:18 +00:00
|
|
|
//TODO(rafaelcastrocouto): User preference for shift key and zoom factor
|
2014-04-08 03:21:22 +00:00
|
|
|
$(container).bind('mousewheel DOMMouseScroll', function(e){
|
2014-02-17 06:48:40 +00:00
|
|
|
//if (!e.shiftKey) {return;}
|
2010-06-22 14:52:51 +00:00
|
|
|
e.preventDefault();
|
2013-01-16 11:42:18 +00:00
|
|
|
var evt = e.originalEvent;
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2013-08-21 06:20:22 +00:00
|
|
|
root_sctm = $('#svgcontent g')[0].getScreenCTM().inverse();
|
2013-02-14 15:19:46 +00:00
|
|
|
var pt = svgedit.math.transformPoint( evt.pageX, evt.pageY, root_sctm );
|
2013-01-16 11:42:18 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
var bbox = {
|
|
|
|
'x': pt.x,
|
|
|
|
'y': pt.y,
|
|
|
|
'width': 0,
|
|
|
|
'height': 0
|
|
|
|
};
|
|
|
|
|
2013-01-16 11:42:18 +00:00
|
|
|
var delta = (evt.wheelDelta) ? evt.wheelDelta : (evt.detail) ? -evt.detail : 0;
|
2014-02-11 13:32:40 +00:00
|
|
|
if (!delta) {return;}
|
2013-01-16 11:42:18 +00:00
|
|
|
|
|
|
|
bbox.factor = Math.max(3/4, Math.min(4/3, (delta)));
|
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
call('zoomed', bbox);
|
2010-06-22 14:52:51 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
}());
|
2010-04-30 15:06:47 +00:00
|
|
|
|
2010-06-30 18:27:36 +00:00
|
|
|
// Function: preventClickDefault
|
|
|
|
// Prevents default browser click behaviour on the given element
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// img - The DOM element to prevent the cilck on
|
|
|
|
var preventClickDefault = function(img) {
|
2013-02-15 15:51:58 +00:00
|
|
|
$(img).click(function(e){e.preventDefault();});
|
|
|
|
};
|
2010-06-30 18:27:36 +00:00
|
|
|
|
2010-06-29 20:43:44 +00:00
|
|
|
// Group: Text edit functions
|
2010-06-22 18:17:42 +00:00
|
|
|
// Functions relating to editing text elements
|
2014-02-11 13:32:40 +00:00
|
|
|
textActions = canvas.textActions = (function() {
|
2010-09-28 16:57:26 +00:00
|
|
|
var curtext;
|
2010-06-22 14:52:51 +00:00
|
|
|
var textinput;
|
|
|
|
var cursor;
|
|
|
|
var selblock;
|
|
|
|
var blinker;
|
|
|
|
var chardata = [];
|
|
|
|
var textbb, transbb;
|
|
|
|
var matrix;
|
|
|
|
var last_x, last_y;
|
|
|
|
var allow_dbl;
|
|
|
|
|
|
|
|
function setCursor(index) {
|
2014-04-08 03:21:22 +00:00
|
|
|
var empty = (textinput.value === '');
|
2010-07-20 13:35:11 +00:00
|
|
|
$(textinput).focus();
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!arguments.length) {
|
|
|
|
if (empty) {
|
2010-06-22 14:52:51 +00:00
|
|
|
index = 0;
|
|
|
|
} else {
|
2014-02-11 13:32:40 +00:00
|
|
|
if (textinput.selectionEnd !== textinput.selectionStart) {return;}
|
2010-06-22 14:52:51 +00:00
|
|
|
index = textinput.selectionEnd;
|
2010-04-12 15:59:46 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var charbb;
|
|
|
|
charbb = chardata[index];
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!empty) {
|
2010-06-22 14:52:51 +00:00
|
|
|
textinput.setSelectionRange(index, index);
|
|
|
|
}
|
2014-04-08 03:21:22 +00:00
|
|
|
cursor = svgedit.utilities.getElem('text_cursor');
|
2010-06-22 14:52:51 +00:00
|
|
|
if (!cursor) {
|
2014-04-08 03:21:22 +00:00
|
|
|
cursor = document.createElementNS(NS.SVG, 'line');
|
2013-02-17 04:58:04 +00:00
|
|
|
svgedit.utilities.assignAttributes(cursor, {
|
2014-04-08 03:21:22 +00:00
|
|
|
id: 'text_cursor',
|
|
|
|
stroke: '#333',
|
2010-06-22 14:52:51 +00:00
|
|
|
'stroke-width': 1
|
2010-04-12 15:59:46 +00:00
|
|
|
});
|
2014-04-08 03:21:22 +00:00
|
|
|
cursor = svgedit.utilities.getElem('selectorParentGroup').appendChild(cursor);
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!blinker) {
|
2010-06-22 14:52:51 +00:00
|
|
|
blinker = setInterval(function() {
|
|
|
|
var show = (cursor.getAttribute('display') === 'none');
|
|
|
|
cursor.setAttribute('display', show?'inline':'none');
|
|
|
|
}, 600);
|
|
|
|
}
|
|
|
|
|
|
|
|
var start_pt = ptToScreen(charbb.x, textbb.y);
|
|
|
|
var end_pt = ptToScreen(charbb.x, (textbb.y + textbb.height));
|
|
|
|
|
2013-02-17 04:58:04 +00:00
|
|
|
svgedit.utilities.assignAttributes(cursor, {
|
2010-06-22 14:52:51 +00:00
|
|
|
x1: start_pt.x,
|
|
|
|
y1: start_pt.y,
|
|
|
|
x2: end_pt.x,
|
|
|
|
y2: end_pt.y,
|
|
|
|
visibility: 'visible',
|
|
|
|
display: 'inline'
|
|
|
|
});
|
|
|
|
|
2014-02-11 13:32:40 +00:00
|
|
|
if (selblock) {selblock.setAttribute('d', '');}
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function setSelection(start, end, skipInput) {
|
2013-02-15 15:51:58 +00:00
|
|
|
if (start === end) {
|
2010-06-22 14:52:51 +00:00
|
|
|
setCursor(end);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!skipInput) {
|
2010-06-22 14:52:51 +00:00
|
|
|
textinput.setSelectionRange(start, end);
|
2010-04-12 15:59:46 +00:00
|
|
|
}
|
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
selblock = svgedit.utilities.getElem('text_selectblock');
|
2010-06-22 14:52:51 +00:00
|
|
|
if (!selblock) {
|
2010-04-12 20:28:39 +00:00
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
selblock = document.createElementNS(NS.SVG, 'path');
|
2013-02-17 04:58:04 +00:00
|
|
|
svgedit.utilities.assignAttributes(selblock, {
|
2014-04-08 03:21:22 +00:00
|
|
|
id: 'text_selectblock',
|
|
|
|
fill: 'green',
|
|
|
|
opacity: 0.5,
|
|
|
|
style: 'pointer-events:none'
|
2010-06-22 14:52:51 +00:00
|
|
|
});
|
2014-04-08 03:21:22 +00:00
|
|
|
svgedit.utilities.getElem('selectorParentGroup').appendChild(selblock);
|
2010-04-12 15:59:46 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
var startbb = chardata[start];
|
2010-06-22 14:52:51 +00:00
|
|
|
var endbb = chardata[end];
|
|
|
|
|
|
|
|
cursor.setAttribute('visibility', 'hidden');
|
|
|
|
|
|
|
|
var tl = ptToScreen(startbb.x, textbb.y),
|
|
|
|
tr = ptToScreen(startbb.x + (endbb.x - startbb.x), textbb.y),
|
|
|
|
bl = ptToScreen(startbb.x, textbb.y + textbb.height),
|
|
|
|
br = ptToScreen(startbb.x + (endbb.x - startbb.x), textbb.y + textbb.height);
|
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
var dstr = 'M' + tl.x + ',' + tl.y
|
|
|
|
+ ' L' + tr.x + ',' + tr.y
|
|
|
|
+ ' ' + br.x + ',' + br.y
|
|
|
|
+ ' ' + bl.x + ',' + bl.y + 'z';
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2013-02-17 04:58:04 +00:00
|
|
|
svgedit.utilities.assignAttributes(selblock, {
|
2010-06-22 14:52:51 +00:00
|
|
|
d: dstr,
|
|
|
|
'display': 'inline'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getIndexFromPoint(mouse_x, mouse_y) {
|
|
|
|
// Position cursor here
|
|
|
|
var pt = svgroot.createSVGPoint();
|
|
|
|
pt.x = mouse_x;
|
|
|
|
pt.y = mouse_y;
|
|
|
|
|
|
|
|
// No content, so return 0
|
2014-02-11 13:32:40 +00:00
|
|
|
if (chardata.length == 1) {return 0;}
|
2010-06-22 14:52:51 +00:00
|
|
|
// Determine if cursor should be on left or right of character
|
|
|
|
var charpos = curtext.getCharNumAtPosition(pt);
|
2013-02-15 15:51:58 +00:00
|
|
|
if (charpos < 0) {
|
2010-06-22 14:52:51 +00:00
|
|
|
// Out of text range, look at mouse coords
|
|
|
|
charpos = chardata.length - 2;
|
2013-02-15 15:51:58 +00:00
|
|
|
if (mouse_x <= chardata[0].x) {
|
2010-06-22 14:52:51 +00:00
|
|
|
charpos = 0;
|
|
|
|
}
|
2013-02-15 15:51:58 +00:00
|
|
|
} else if (charpos >= chardata.length - 2) {
|
2010-06-22 14:52:51 +00:00
|
|
|
charpos = chardata.length - 2;
|
|
|
|
}
|
|
|
|
var charbb = chardata[charpos];
|
|
|
|
var mid = charbb.x + (charbb.width/2);
|
2013-02-15 15:51:58 +00:00
|
|
|
if (mouse_x > mid) {
|
2010-06-22 14:52:51 +00:00
|
|
|
charpos++;
|
|
|
|
}
|
|
|
|
return charpos;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setCursorFromPoint(mouse_x, mouse_y) {
|
|
|
|
setCursor(getIndexFromPoint(mouse_x, mouse_y));
|
|
|
|
}
|
|
|
|
|
|
|
|
function setEndSelectionFromPoint(x, y, apply) {
|
|
|
|
var i1 = textinput.selectionStart;
|
|
|
|
var i2 = getIndexFromPoint(x, y);
|
|
|
|
|
|
|
|
var start = Math.min(i1, i2);
|
|
|
|
var end = Math.max(i1, i2);
|
|
|
|
setSelection(start, end, !apply);
|
|
|
|
}
|
|
|
|
|
|
|
|
function screenToPt(x_in, y_in) {
|
|
|
|
var out = {
|
|
|
|
x: x_in,
|
|
|
|
y: y_in
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
out.x /= current_zoom;
|
|
|
|
out.y /= current_zoom;
|
2010-04-30 15:06:47 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (matrix) {
|
2013-02-14 15:19:46 +00:00
|
|
|
var pt = svgedit.math.transformPoint(out.x, out.y, matrix.inverse());
|
2010-06-22 14:52:51 +00:00
|
|
|
out.x = pt.x;
|
|
|
|
out.y = pt.y;
|
|
|
|
}
|
2010-04-30 15:06:47 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
function ptToScreen(x_in, y_in) {
|
|
|
|
var out = {
|
|
|
|
x: x_in,
|
|
|
|
y: y_in
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-04-13 15:28:52 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (matrix) {
|
2013-02-14 15:19:46 +00:00
|
|
|
var pt = svgedit.math.transformPoint(out.x, out.y, matrix);
|
2010-06-22 14:52:51 +00:00
|
|
|
out.x = pt.x;
|
|
|
|
out.y = pt.y;
|
2010-04-13 15:28:52 +00:00
|
|
|
}
|
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
out.x *= current_zoom;
|
|
|
|
out.y *= current_zoom;
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
function hideCursor() {
|
2013-02-15 15:51:58 +00:00
|
|
|
if (cursor) {
|
2010-06-22 14:52:51 +00:00
|
|
|
cursor.setAttribute('visibility', 'hidden');
|
2010-04-13 15:28:52 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function selectAll(evt) {
|
|
|
|
setSelection(0, curtext.textContent.length);
|
|
|
|
$(this).unbind(evt);
|
|
|
|
}
|
2010-04-13 15:28:52 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
function selectWord(evt) {
|
2014-02-11 13:32:40 +00:00
|
|
|
if (!allow_dbl || !curtext) {return;}
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2013-02-14 15:19:46 +00:00
|
|
|
var ept = svgedit.math.transformPoint( evt.pageX, evt.pageY, root_sctm ),
|
2010-06-22 14:52:51 +00:00
|
|
|
mouse_x = ept.x * current_zoom,
|
|
|
|
mouse_y = ept.y * current_zoom;
|
|
|
|
var pt = screenToPt(mouse_x, mouse_y);
|
|
|
|
|
|
|
|
var index = getIndexFromPoint(pt.x, pt.y);
|
|
|
|
var str = curtext.textContent;
|
|
|
|
var first = str.substr(0, index).replace(/[a-z0-9]+$/i, '').length;
|
|
|
|
var m = str.substr(index).match(/^[a-z0-9]+/i);
|
|
|
|
var last = (m?m[0].length:0) + index;
|
|
|
|
setSelection(first, last);
|
|
|
|
|
|
|
|
// Set tripleclick
|
|
|
|
$(evt.target).click(selectAll);
|
|
|
|
setTimeout(function() {
|
|
|
|
$(evt.target).unbind('click', selectAll);
|
|
|
|
}, 300);
|
2010-04-14 17:30:25 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
select: function(target, x, y) {
|
2010-09-28 16:57:26 +00:00
|
|
|
curtext = target;
|
|
|
|
textActions.toEditMode(x, y);
|
2010-06-22 14:52:51 +00:00
|
|
|
},
|
|
|
|
start: function(elem) {
|
|
|
|
curtext = elem;
|
|
|
|
textActions.toEditMode();
|
|
|
|
},
|
|
|
|
mouseDown: function(evt, mouse_target, start_x, start_y) {
|
|
|
|
var pt = screenToPt(start_x, start_y);
|
|
|
|
|
|
|
|
textinput.focus();
|
|
|
|
setCursorFromPoint(pt.x, pt.y);
|
|
|
|
last_x = start_x;
|
|
|
|
last_y = start_y;
|
2010-04-13 15:28:52 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// TODO: Find way to block native selection
|
|
|
|
},
|
|
|
|
mouseMove: function(mouse_x, mouse_y) {
|
|
|
|
var pt = screenToPt(mouse_x, mouse_y);
|
|
|
|
setEndSelectionFromPoint(pt.x, pt.y);
|
|
|
|
},
|
|
|
|
mouseUp: function(evt, mouse_x, mouse_y) {
|
|
|
|
var pt = screenToPt(mouse_x, mouse_y);
|
2010-04-13 15:28:52 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
setEndSelectionFromPoint(pt.x, pt.y, true);
|
2010-04-13 15:28:52 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// TODO: Find a way to make this work: Use transformed BBox instead of evt.target
|
2014-01-31 00:39:35 +00:00
|
|
|
// if (last_x === mouse_x && last_y === mouse_y
|
|
|
|
// && !svgedit.math.rectsIntersect(transbb, {x: pt.x, y: pt.y, width:0, height:0})) {
|
|
|
|
// textActions.toSelectMode(true);
|
|
|
|
// }
|
2011-02-23 15:56:14 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (
|
2011-02-23 16:44:20 +00:00
|
|
|
evt.target !== curtext
|
|
|
|
&& mouse_x < last_x + 2
|
|
|
|
&& mouse_x > last_x - 2
|
|
|
|
&& mouse_y < last_y + 2
|
|
|
|
&& mouse_y > last_y - 2) {
|
2011-02-23 15:56:14 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
textActions.toSelectMode(true);
|
|
|
|
}
|
2010-04-13 15:52:00 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
},
|
|
|
|
setCursor: setCursor,
|
|
|
|
toEditMode: function(x, y) {
|
|
|
|
allow_dbl = false;
|
2014-04-08 03:21:22 +00:00
|
|
|
current_mode = 'textedit';
|
2010-06-22 14:52:51 +00:00
|
|
|
selectorManager.requestSelector(curtext).showGrips(false);
|
2010-07-20 13:35:11 +00:00
|
|
|
// Make selector group accept clicks
|
|
|
|
var sel = selectorManager.requestSelector(curtext).selectorRect;
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
textActions.init();
|
2010-09-28 16:57:26 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
$(curtext).css('cursor', 'text');
|
|
|
|
|
2014-01-31 00:39:35 +00:00
|
|
|
// if (svgedit.browser.supportsEditableText()) {
|
|
|
|
// curtext.setAttribute('editable', 'simple');
|
|
|
|
// return;
|
|
|
|
// }
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!arguments.length) {
|
2010-06-22 14:52:51 +00:00
|
|
|
setCursor();
|
|
|
|
} else {
|
|
|
|
var pt = screenToPt(x, y);
|
|
|
|
setCursorFromPoint(pt.x, pt.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
allow_dbl = true;
|
|
|
|
}, 300);
|
|
|
|
},
|
|
|
|
toSelectMode: function(selectElem) {
|
2014-04-08 03:21:22 +00:00
|
|
|
current_mode = 'select';
|
2010-06-22 14:52:51 +00:00
|
|
|
clearInterval(blinker);
|
|
|
|
blinker = null;
|
2014-02-11 13:32:40 +00:00
|
|
|
if (selblock) {$(selblock).attr('display', 'none');}
|
|
|
|
if (cursor) {$(cursor).attr('visibility', 'hidden');}
|
2010-06-22 14:52:51 +00:00
|
|
|
$(curtext).css('cursor', 'move');
|
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (selectElem) {
|
2010-06-30 18:27:36 +00:00
|
|
|
clearSelection();
|
2010-04-12 19:11:35 +00:00
|
|
|
$(curtext).css('cursor', 'move');
|
2010-04-12 15:59:46 +00:00
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
call('selected', [curtext]);
|
2010-06-30 18:27:36 +00:00
|
|
|
addToSelection([curtext], true);
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
2013-02-15 15:51:58 +00:00
|
|
|
if (curtext && !curtext.textContent.length) {
|
2010-06-22 14:52:51 +00:00
|
|
|
// No content, so delete
|
|
|
|
canvas.deleteSelectedElements();
|
|
|
|
}
|
|
|
|
|
|
|
|
$(textinput).blur();
|
|
|
|
|
|
|
|
curtext = false;
|
|
|
|
|
2014-01-31 00:39:35 +00:00
|
|
|
// if (svgedit.browser.supportsEditableText()) {
|
|
|
|
// curtext.removeAttribute('editable');
|
|
|
|
// }
|
2010-06-22 14:52:51 +00:00
|
|
|
},
|
|
|
|
setInputElem: function(elem) {
|
|
|
|
textinput = elem;
|
2014-01-31 00:39:35 +00:00
|
|
|
// $(textinput).blur(hideCursor);
|
2010-06-22 14:52:51 +00:00
|
|
|
},
|
|
|
|
clear: function() {
|
2014-04-08 03:21:22 +00:00
|
|
|
if (current_mode == 'textedit') {
|
2010-06-22 14:52:51 +00:00
|
|
|
textActions.toSelectMode();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
init: function(inputElem) {
|
2014-02-11 13:32:40 +00:00
|
|
|
if (!curtext) {return;}
|
|
|
|
var i, end;
|
2014-01-31 00:39:35 +00:00
|
|
|
// if (svgedit.browser.supportsEditableText()) {
|
|
|
|
// curtext.select();
|
|
|
|
// return;
|
|
|
|
// }
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!curtext.parentNode) {
|
2010-06-22 14:52:51 +00:00
|
|
|
// Result of the ffClone, need to get correct element
|
|
|
|
curtext = selectedElements[0];
|
|
|
|
selectorManager.requestSelector(curtext).showGrips(false);
|
|
|
|
}
|
2010-04-12 19:11:35 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
var str = curtext.textContent;
|
|
|
|
var len = str.length;
|
|
|
|
|
|
|
|
var xform = curtext.getAttribute('transform');
|
2010-04-13 15:52:00 +00:00
|
|
|
|
2011-02-24 16:13:26 +00:00
|
|
|
textbb = svgedit.utilities.getBBox(curtext);
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2013-02-14 15:19:46 +00:00
|
|
|
matrix = xform ? svgedit.math.getMatrix(curtext) : null;
|
2010-04-13 15:52:00 +00:00
|
|
|
|
2014-02-11 13:46:39 +00:00
|
|
|
chardata = new Array(len);
|
2010-06-22 14:52:51 +00:00
|
|
|
textinput.focus();
|
|
|
|
|
|
|
|
$(curtext).unbind('dblclick', selectWord).dblclick(selectWord);
|
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!len) {
|
2014-02-11 13:32:40 +00:00
|
|
|
end = {x: textbb.x + (textbb.width/2), width: 0};
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
|
2014-02-11 13:32:40 +00:00
|
|
|
for (i=0; i<len; i++) {
|
2010-06-22 14:52:51 +00:00
|
|
|
var start = curtext.getStartPositionOfChar(i);
|
2014-02-11 13:32:40 +00:00
|
|
|
end = curtext.getEndPositionOfChar(i);
|
2011-02-23 19:45:02 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!svgedit.browser.supportsGoodTextCharPos()) {
|
2011-02-23 15:56:14 +00:00
|
|
|
var offset = canvas.contentW * current_zoom;
|
|
|
|
start.x -= offset;
|
|
|
|
end.x -= offset;
|
2011-02-23 16:44:20 +00:00
|
|
|
|
|
|
|
start.x /= current_zoom;
|
|
|
|
end.x /= current_zoom;
|
2011-02-23 15:56:14 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
// Get a "bbox" equivalent for each character. Uses the
|
|
|
|
// bbox data of the actual text for y, height purposes
|
|
|
|
|
|
|
|
// TODO: Decide if y, width and height are actually necessary
|
|
|
|
chardata[i] = {
|
|
|
|
x: start.x,
|
|
|
|
y: textbb.y, // start.y?
|
|
|
|
width: end.x - start.x,
|
|
|
|
height: textbb.height
|
|
|
|
};
|
2010-04-12 15:59:46 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
// Add a last bbox for cursor at end of text
|
|
|
|
chardata.push({
|
|
|
|
x: end.x,
|
|
|
|
width: 0
|
|
|
|
});
|
|
|
|
setSelection(textinput.selectionStart, textinput.selectionEnd, true);
|
2010-04-12 15:59:46 +00:00
|
|
|
}
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2014-02-11 13:32:40 +00:00
|
|
|
}());
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2011-02-04 08:02:46 +00:00
|
|
|
// TODO: Migrate all of this code into path.js
|
2010-06-29 20:43:44 +00:00
|
|
|
// Group: Path edit functions
|
2010-06-22 18:17:42 +00:00
|
|
|
// Functions relating to editing path elements
|
2014-02-11 13:32:40 +00:00
|
|
|
pathActions = canvas.pathActions = function() {
|
2010-04-12 15:59:46 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
var subpath = false;
|
|
|
|
var current_path;
|
2010-10-27 16:34:15 +00:00
|
|
|
var newPoint, firstCtrl;
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
function resetD(p) {
|
2014-04-08 03:21:22 +00:00
|
|
|
p.setAttribute('d', pathActions.convertPath(p));
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
|
2011-02-04 16:06:25 +00:00
|
|
|
// TODO: Move into path.js
|
2013-02-15 15:51:58 +00:00
|
|
|
svgedit.path.Path.prototype.endChanges = function(text) {
|
2014-02-11 13:32:40 +00:00
|
|
|
if (svgedit.browser.isWebkit()) {resetD(this.elem);}
|
2013-02-17 08:21:07 +00:00
|
|
|
var cmd = new svgedit.history.ChangeElementCommand(this.elem, {d: this.last_d}, text);
|
2013-02-15 15:51:58 +00:00
|
|
|
addCommandToHistory(cmd);
|
2014-04-08 03:21:22 +00:00
|
|
|
call('changed', [this.elem]);
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
svgedit.path.Path.prototype.addPtsToSelection = function(indexes) {
|
2014-02-11 13:32:40 +00:00
|
|
|
var i, seg;
|
|
|
|
if (!$.isArray(indexes)) {indexes = [indexes];}
|
|
|
|
for (i = 0; i< indexes.length; i++) {
|
2013-02-15 15:51:58 +00:00
|
|
|
var index = indexes[i];
|
2014-02-11 13:32:40 +00:00
|
|
|
seg = this.segs[index];
|
2013-02-15 15:51:58 +00:00
|
|
|
if (seg.ptgrip) {
|
|
|
|
if (this.selected_pts.indexOf(index) == -1 && index >= 0) {
|
|
|
|
this.selected_pts.push(index);
|
2010-02-26 18:18:21 +00:00
|
|
|
}
|
2013-02-15 15:51:58 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
2013-02-15 15:51:58 +00:00
|
|
|
this.selected_pts.sort();
|
2014-02-11 13:32:40 +00:00
|
|
|
i = this.selected_pts.length;
|
|
|
|
var grips = new Array(i);
|
2013-02-15 15:51:58 +00:00
|
|
|
// Loop through points to be selected and highlight each
|
|
|
|
while (i--) {
|
|
|
|
var pt = this.selected_pts[i];
|
2014-02-11 13:32:40 +00:00
|
|
|
seg = this.segs[pt];
|
2013-02-15 15:51:58 +00:00
|
|
|
seg.select(true);
|
|
|
|
grips[i] = seg.ptgrip;
|
|
|
|
}
|
|
|
|
// TODO: Correct this:
|
|
|
|
pathActions.canDeleteNodes = true;
|
|
|
|
|
|
|
|
pathActions.closed_subpath = this.subpathIsClosed(this.selected_pts[0]);
|
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
call('selected', grips);
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2014-02-11 13:32:40 +00:00
|
|
|
current_path = null;
|
|
|
|
var drawn_path = null,
|
2010-06-22 14:52:51 +00:00
|
|
|
hasMoved = false;
|
|
|
|
|
|
|
|
// This function converts a polyline (created by the fh_path tool) into
|
|
|
|
// a path element and coverts every three line segments into a single bezier
|
|
|
|
// curve in an attempt to smooth out the free-hand
|
|
|
|
var smoothPolylineIntoPath = function(element) {
|
2014-02-11 13:32:40 +00:00
|
|
|
var i, points = element.points;
|
2010-06-22 14:52:51 +00:00
|
|
|
var N = points.numberOfItems;
|
|
|
|
if (N >= 4) {
|
|
|
|
// loop through every 3 points and convert to a cubic bezier curve segment
|
|
|
|
//
|
|
|
|
// NOTE: this is cheating, it means that every 3 points has the potential to
|
2014-01-31 10:40:52 +00:00
|
|
|
// be a corner instead of treating each point in an equal manner. In general,
|
2010-06-22 14:52:51 +00:00
|
|
|
// this technique does not look that good.
|
|
|
|
//
|
|
|
|
// I am open to better ideas!
|
|
|
|
//
|
|
|
|
// Reading:
|
|
|
|
// - http://www.efg2.com/Lab/Graphics/Jean-YvesQueinecBezierCurves.htm
|
|
|
|
// - http://www.codeproject.com/KB/graphics/BezierSpline.aspx?msg=2956963
|
|
|
|
// - http://www.ian-ko.com/ET_GeoWizards/UserGuide/smooth.htm
|
|
|
|
// - http://www.cs.mtu.edu/~shene/COURSES/cs3621/NOTES/spline/Bezier/bezier-der.html
|
|
|
|
var curpos = points.getItem(0), prevCtlPt = null;
|
|
|
|
var d = [];
|
2014-04-08 03:21:22 +00:00
|
|
|
d.push(['M', curpos.x, ',', curpos.y, ' C'].join(''));
|
2014-02-11 13:32:40 +00:00
|
|
|
for (i = 1; i <= (N-4); i += 3) {
|
2010-06-22 14:52:51 +00:00
|
|
|
var ct1 = points.getItem(i);
|
|
|
|
var ct2 = points.getItem(i+1);
|
|
|
|
var end = points.getItem(i+2);
|
|
|
|
|
|
|
|
// if the previous segment had a control point, we want to smooth out
|
|
|
|
// the control points on both sides
|
|
|
|
if (prevCtlPt) {
|
2011-02-09 06:14:47 +00:00
|
|
|
var newpts = svgedit.path.smoothControlPoints( prevCtlPt, ct1, curpos );
|
2010-06-22 14:52:51 +00:00
|
|
|
if (newpts && newpts.length == 2) {
|
|
|
|
var prevArr = d[d.length-1].split(',');
|
|
|
|
prevArr[2] = newpts[0].x;
|
|
|
|
prevArr[3] = newpts[0].y;
|
|
|
|
d[d.length-1] = prevArr.join(',');
|
|
|
|
ct1 = newpts[1];
|
2010-04-01 20:01:20 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
|
2013-02-15 16:51:48 +00:00
|
|
|
d.push([ct1.x, ct1.y, ct2.x, ct2.y, end.x, end.y].join(','));
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
curpos = end;
|
|
|
|
prevCtlPt = ct2;
|
2009-12-30 18:06:29 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
// handle remaining line segments
|
2014-04-08 03:21:22 +00:00
|
|
|
d.push('L');
|
2014-02-11 13:32:40 +00:00
|
|
|
while (i < N) {
|
2010-06-22 14:52:51 +00:00
|
|
|
var pt = points.getItem(i);
|
2014-04-08 03:21:22 +00:00
|
|
|
d.push([pt.x, pt.y].join(','));
|
2014-02-11 13:32:40 +00:00
|
|
|
i++;
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
2014-04-08 03:21:22 +00:00
|
|
|
d = d.join(' ');
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
// create new path element
|
|
|
|
element = addSvgElementFromJson({
|
2014-04-08 03:21:22 +00:00
|
|
|
element: 'path',
|
|
|
|
curStyles: true,
|
|
|
|
attr: {
|
|
|
|
id: getId(),
|
|
|
|
d: d,
|
|
|
|
fill: 'none'
|
2010-01-01 15:49:50 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
});
|
2011-01-26 17:35:28 +00:00
|
|
|
// No need to call "changed", as this is already done under mouseUp
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
return element;
|
|
|
|
};
|
2010-02-26 18:18:21 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
return {
|
|
|
|
mouseDown: function(evt, mouse_target, start_x, start_y) {
|
2014-02-11 13:46:39 +00:00
|
|
|
var id;
|
2014-04-08 03:21:22 +00:00
|
|
|
if (current_mode === 'path') {
|
2010-10-27 16:34:15 +00:00
|
|
|
mouse_x = start_x;
|
|
|
|
mouse_y = start_y;
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
var x = mouse_x/current_zoom,
|
|
|
|
y = mouse_y/current_zoom,
|
2014-04-08 03:21:22 +00:00
|
|
|
stretchy = svgedit.utilities.getElem('path_stretch_line');
|
2010-10-27 16:34:15 +00:00
|
|
|
newPoint = [x, y];
|
2010-09-10 13:23:31 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (curConfig.gridSnapping){
|
2013-02-17 04:58:04 +00:00
|
|
|
x = svgedit.utilities.snapToGrid(x);
|
|
|
|
y = svgedit.utilities.snapToGrid(y);
|
|
|
|
mouse_x = svgedit.utilities.snapToGrid(mouse_x);
|
|
|
|
mouse_y = svgedit.utilities.snapToGrid(mouse_y);
|
2010-09-10 13:23:31 +00:00
|
|
|
}
|
2010-09-09 12:46:34 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
if (!stretchy) {
|
2014-04-08 03:21:22 +00:00
|
|
|
stretchy = document.createElementNS(NS.SVG, 'path');
|
2013-02-17 04:58:04 +00:00
|
|
|
svgedit.utilities.assignAttributes(stretchy, {
|
2014-04-08 03:21:22 +00:00
|
|
|
id: 'path_stretch_line',
|
|
|
|
stroke: '#22C',
|
|
|
|
'stroke-width': '0.5',
|
|
|
|
fill: 'none'
|
2010-06-22 14:52:51 +00:00
|
|
|
});
|
2014-04-08 03:21:22 +00:00
|
|
|
stretchy = svgedit.utilities.getElem('selectorParentGroup').appendChild(stretchy);
|
2009-12-30 18:06:29 +00:00
|
|
|
}
|
2014-04-08 03:21:22 +00:00
|
|
|
stretchy.setAttribute('display', 'inline');
|
2010-02-26 18:18:21 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
var keep = null;
|
2014-02-11 13:46:39 +00:00
|
|
|
var index;
|
2010-06-22 14:52:51 +00:00
|
|
|
// if pts array is empty, create path element with M at current point
|
2010-10-26 13:56:17 +00:00
|
|
|
if (!drawn_path) {
|
2014-04-08 03:21:22 +00:00
|
|
|
d_attr = 'M' + x + ',' + y + ' ';
|
2010-10-26 13:56:17 +00:00
|
|
|
drawn_path = addSvgElementFromJson({
|
2014-04-08 03:21:22 +00:00
|
|
|
element: 'path',
|
|
|
|
curStyles: true,
|
|
|
|
attr: {
|
|
|
|
d: d_attr,
|
|
|
|
id: getNextId(),
|
|
|
|
opacity: cur_shape.opacity / 2
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
// set stretchy line to first point
|
2010-10-27 16:34:15 +00:00
|
|
|
stretchy.setAttribute('d', ['M', mouse_x, mouse_y, mouse_x, mouse_y].join(' '));
|
2014-02-11 13:46:39 +00:00
|
|
|
index = subpath ? svgedit.path.path.segs.length : 0;
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.addPointGrip(index, mouse_x, mouse_y);
|
2013-02-15 15:51:58 +00:00
|
|
|
} else {
|
2010-06-22 14:52:51 +00:00
|
|
|
// determine if we clicked on an existing point
|
2010-10-26 13:56:17 +00:00
|
|
|
var seglist = drawn_path.pathSegList;
|
|
|
|
var i = seglist.numberOfItems;
|
2010-06-22 14:52:51 +00:00
|
|
|
var FUZZ = 6/current_zoom;
|
|
|
|
var clickOnPoint = false;
|
2013-02-15 15:51:58 +00:00
|
|
|
while (i) {
|
2010-10-26 13:56:17 +00:00
|
|
|
i --;
|
|
|
|
var item = seglist.getItem(i);
|
|
|
|
var px = item.x, py = item.y;
|
2010-06-22 14:52:51 +00:00
|
|
|
// found a matching point
|
|
|
|
if ( x >= (px-FUZZ) && x <= (px+FUZZ) && y >= (py-FUZZ) && y <= (py+FUZZ) ) {
|
|
|
|
clickOnPoint = true;
|
|
|
|
break;
|
|
|
|
}
|
2009-12-30 18:06:29 +00:00
|
|
|
}
|
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// get path element that we are in the process of creating
|
2014-02-11 13:46:39 +00:00
|
|
|
id = getId();
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
// Remove previous path object if previously created
|
2011-02-23 16:24:07 +00:00
|
|
|
svgedit.path.removePath_(id);
|
2009-12-30 18:06:29 +00:00
|
|
|
|
2013-02-14 15:19:46 +00:00
|
|
|
var newpath = svgedit.utilities.getElem(id);
|
2014-02-11 13:46:39 +00:00
|
|
|
var newseg;
|
|
|
|
var s_seg;
|
2010-10-26 13:56:17 +00:00
|
|
|
var len = seglist.numberOfItems;
|
2010-06-22 14:52:51 +00:00
|
|
|
// if we clicked on an existing point, then we are done this path, commit it
|
2013-02-15 16:51:48 +00:00
|
|
|
// (i, i+1) are the x,y that were clicked on
|
2010-06-22 14:52:51 +00:00
|
|
|
if (clickOnPoint) {
|
|
|
|
// if clicked on any other point but the first OR
|
|
|
|
// the first point was clicked on and there are less than 3 points
|
|
|
|
// then leave the path open
|
|
|
|
// otherwise, close the path
|
2010-10-27 16:34:15 +00:00
|
|
|
if (i <= 1 && len >= 2) {
|
2010-06-22 14:52:51 +00:00
|
|
|
// Create end segment
|
2010-10-26 13:56:17 +00:00
|
|
|
var abs_x = seglist.getItem(0).x;
|
|
|
|
var abs_y = seglist.getItem(0).y;
|
2010-10-27 16:34:15 +00:00
|
|
|
|
|
|
|
|
2014-02-11 13:46:39 +00:00
|
|
|
s_seg = stretchy.pathSegList.getItem(1);
|
2013-02-15 15:51:58 +00:00
|
|
|
if (s_seg.pathSegType === 4) {
|
2014-02-11 13:46:39 +00:00
|
|
|
newseg = drawn_path.createSVGPathSegLinetoAbs(abs_x, abs_y);
|
2010-10-27 16:34:15 +00:00
|
|
|
} else {
|
2014-02-11 13:46:39 +00:00
|
|
|
newseg = drawn_path.createSVGPathSegCurvetoCubicAbs(
|
2010-10-27 16:34:15 +00:00
|
|
|
abs_x,
|
|
|
|
abs_y,
|
|
|
|
s_seg.x1 / current_zoom,
|
|
|
|
s_seg.y1 / current_zoom,
|
|
|
|
abs_x,
|
|
|
|
abs_y
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
var endseg = drawn_path.createSVGPathSegClosePath();
|
|
|
|
seglist.appendItem(newseg);
|
|
|
|
seglist.appendItem(endseg);
|
2013-02-15 15:51:58 +00:00
|
|
|
} else if (len < 3) {
|
2010-06-22 14:52:51 +00:00
|
|
|
keep = false;
|
|
|
|
return keep;
|
|
|
|
}
|
|
|
|
$(stretchy).remove();
|
2010-02-26 18:18:21 +00:00
|
|
|
|
2014-02-11 13:46:39 +00:00
|
|
|
// This will signal to commit the path
|
2010-06-22 14:52:51 +00:00
|
|
|
element = newpath;
|
2010-10-26 13:56:17 +00:00
|
|
|
drawn_path = null;
|
2010-06-22 14:52:51 +00:00
|
|
|
started = false;
|
2010-02-26 18:18:21 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (subpath) {
|
|
|
|
if (svgedit.path.path.matrix) {
|
2013-02-17 04:58:04 +00:00
|
|
|
svgedit.coords.remapElement(newpath, {}, svgedit.path.path.matrix.inverse());
|
2009-12-30 18:06:29 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
var new_d = newpath.getAttribute('d');
|
|
|
|
var orig_d = $(svgedit.path.path.elem).attr('d');
|
|
|
|
$(svgedit.path.path.elem).attr('d', orig_d + new_d);
|
2010-06-22 14:52:51 +00:00
|
|
|
$(newpath).remove();
|
2013-02-15 15:51:58 +00:00
|
|
|
if (svgedit.path.path.matrix) {
|
2011-03-07 18:26:12 +00:00
|
|
|
svgedit.path.recalcRotatedPath();
|
2010-03-03 19:26:09 +00:00
|
|
|
}
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.path.init();
|
|
|
|
pathActions.toEditMode(svgedit.path.path.elem);
|
|
|
|
svgedit.path.path.selectPt();
|
2010-06-22 14:52:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2010-10-26 13:56:17 +00:00
|
|
|
// else, create a new point, update path element
|
2010-06-22 14:52:51 +00:00
|
|
|
else {
|
|
|
|
// Checks if current target or parents are #svgcontent
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!$.contains(container, getMouseTarget(evt))) {
|
2010-06-22 14:52:51 +00:00
|
|
|
// Clicked outside canvas, so don't make point
|
2014-04-08 03:21:22 +00:00
|
|
|
console.log('Clicked outside canvas');
|
2010-06-22 14:52:51 +00:00
|
|
|
return false;
|
2009-12-30 18:06:29 +00:00
|
|
|
}
|
2010-02-12 14:23:55 +00:00
|
|
|
|
2010-10-26 13:56:17 +00:00
|
|
|
var num = drawn_path.pathSegList.numberOfItems;
|
|
|
|
var last = drawn_path.pathSegList.getItem(num -1);
|
|
|
|
var lastx = last.x, lasty = last.y;
|
2010-05-26 15:12:57 +00:00
|
|
|
|
2013-02-15 16:51:48 +00:00
|
|
|
if (evt.shiftKey) {
|
|
|
|
var xya = svgedit.math.snapToAngle(lastx, lasty, x, y);
|
|
|
|
x = xya.x;
|
|
|
|
y = xya.y;
|
|
|
|
}
|
2010-10-27 16:34:15 +00:00
|
|
|
|
|
|
|
// Use the segment defined by stretchy
|
2014-02-11 13:46:39 +00:00
|
|
|
s_seg = stretchy.pathSegList.getItem(1);
|
2013-02-15 15:51:58 +00:00
|
|
|
if (s_seg.pathSegType === 4) {
|
2014-02-11 13:46:39 +00:00
|
|
|
newseg = drawn_path.createSVGPathSegLinetoAbs(round(x), round(y));
|
2010-10-27 16:34:15 +00:00
|
|
|
} else {
|
2014-02-11 13:46:39 +00:00
|
|
|
newseg = drawn_path.createSVGPathSegCurvetoCubicAbs(
|
2010-10-27 16:34:15 +00:00
|
|
|
round(x),
|
|
|
|
round(y),
|
|
|
|
s_seg.x1 / current_zoom,
|
|
|
|
s_seg.y1 / current_zoom,
|
|
|
|
s_seg.x2 / current_zoom,
|
|
|
|
s_seg.y2 / current_zoom
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
drawn_path.pathSegList.appendItem(newseg);
|
2010-08-16 18:14:21 +00:00
|
|
|
|
|
|
|
x *= current_zoom;
|
|
|
|
y *= current_zoom;
|
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// set stretchy line to latest point
|
2010-10-27 16:34:15 +00:00
|
|
|
stretchy.setAttribute('d', ['M', x, y, x, y].join(' '));
|
2014-02-11 13:46:39 +00:00
|
|
|
index = num;
|
|
|
|
if (subpath) {index += svgedit.path.path.segs.length;}
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.addPointGrip(index, x, y);
|
2009-12-30 18:06:29 +00:00
|
|
|
}
|
2014-01-31 00:39:35 +00:00
|
|
|
// keep = true;
|
2010-10-27 16:34:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Make sure current_path isn't null at this point
|
2014-02-11 13:46:39 +00:00
|
|
|
if (!svgedit.path.path) {return;}
|
2010-10-27 16:34:15 +00:00
|
|
|
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.path.storeD();
|
2010-10-27 16:34:15 +00:00
|
|
|
|
2014-02-11 13:46:39 +00:00
|
|
|
id = evt.target.id;
|
|
|
|
var cur_pt;
|
2014-04-08 03:21:22 +00:00
|
|
|
if (id.substr(0,14) == 'pathpointgrip_') {
|
2010-10-27 16:34:15 +00:00
|
|
|
// Select this point
|
2014-02-11 13:46:39 +00:00
|
|
|
cur_pt = svgedit.path.path.cur_pt = parseInt(id.substr(14));
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.path.dragging = [start_x, start_y];
|
|
|
|
var seg = svgedit.path.path.segs[cur_pt];
|
2010-10-27 16:34:15 +00:00
|
|
|
|
|
|
|
// only clear selection if shift is not pressed (otherwise, add
|
|
|
|
// node to selection)
|
|
|
|
if (!evt.shiftKey) {
|
2013-02-15 15:51:58 +00:00
|
|
|
if (svgedit.path.path.selected_pts.length <= 1 || !seg.selected) {
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.path.clearSelection();
|
2010-10-27 16:34:15 +00:00
|
|
|
}
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.path.addPtsToSelection(cur_pt);
|
2013-02-15 15:51:58 +00:00
|
|
|
} else if (seg.selected) {
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.path.removePtFromSelection(cur_pt);
|
2010-10-27 16:34:15 +00:00
|
|
|
} else {
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.path.addPtsToSelection(cur_pt);
|
2009-12-30 18:06:29 +00:00
|
|
|
}
|
2014-04-08 03:21:22 +00:00
|
|
|
} else if (id.indexOf('ctrlpointgrip_') == 0) {
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.path.dragging = [start_x, start_y];
|
2010-10-27 16:34:15 +00:00
|
|
|
|
|
|
|
var parts = id.split('_')[1].split('c');
|
2014-02-11 13:46:39 +00:00
|
|
|
cur_pt = Number(parts[0]);
|
|
|
|
var ctrl_num = Number(parts[1]);
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.path.selectPt(cur_pt, ctrl_num);
|
2010-10-27 16:34:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start selection box
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!svgedit.path.path.dragging) {
|
2010-10-27 16:34:15 +00:00
|
|
|
if (rubberBox == null) {
|
|
|
|
rubberBox = selectorManager.getRubberBandBox();
|
|
|
|
}
|
2013-02-17 04:58:04 +00:00
|
|
|
svgedit.utilities.assignAttributes(rubberBox, {
|
2010-10-27 16:34:15 +00:00
|
|
|
'x': start_x * current_zoom,
|
|
|
|
'y': start_y * current_zoom,
|
|
|
|
'width': 0,
|
|
|
|
'height': 0,
|
|
|
|
'display': 'inline'
|
|
|
|
}, 100);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
mouseMove: function(mouse_x, mouse_y) {
|
|
|
|
hasMoved = true;
|
2014-04-08 03:21:22 +00:00
|
|
|
if (current_mode === 'path') {
|
2014-02-11 13:46:39 +00:00
|
|
|
if (!drawn_path) {return;}
|
2010-10-27 16:34:15 +00:00
|
|
|
var seglist = drawn_path.pathSegList;
|
|
|
|
var index = seglist.numberOfItems - 1;
|
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (newPoint) {
|
2010-10-27 16:34:15 +00:00
|
|
|
// First point
|
2014-02-17 06:48:40 +00:00
|
|
|
// if (!index) {return;}
|
2010-10-27 16:34:15 +00:00
|
|
|
|
|
|
|
// Set control points
|
2011-02-04 08:02:46 +00:00
|
|
|
var pointGrip1 = svgedit.path.addCtrlGrip('1c1');
|
|
|
|
var pointGrip2 = svgedit.path.addCtrlGrip('0c2');
|
2010-10-27 16:34:15 +00:00
|
|
|
|
|
|
|
// dragging pointGrip1
|
|
|
|
pointGrip1.setAttribute('cx', mouse_x);
|
|
|
|
pointGrip1.setAttribute('cy', mouse_y);
|
|
|
|
pointGrip1.setAttribute('display', 'inline');
|
|
|
|
|
|
|
|
var pt_x = newPoint[0];
|
|
|
|
var pt_y = newPoint[1];
|
|
|
|
|
|
|
|
// set curve
|
|
|
|
var seg = seglist.getItem(index);
|
|
|
|
var cur_x = mouse_x / current_zoom;
|
|
|
|
var cur_y = mouse_y / current_zoom;
|
|
|
|
var alt_x = (pt_x + (pt_x - cur_x));
|
|
|
|
var alt_y = (pt_y + (pt_y - cur_y));
|
|
|
|
|
|
|
|
pointGrip2.setAttribute('cx', alt_x * current_zoom);
|
|
|
|
pointGrip2.setAttribute('cy', alt_y * current_zoom);
|
|
|
|
pointGrip2.setAttribute('display', 'inline');
|
|
|
|
|
2011-02-04 08:02:46 +00:00
|
|
|
var ctrlLine = svgedit.path.getCtrlLine(1);
|
2013-02-17 04:58:04 +00:00
|
|
|
svgedit.utilities.assignAttributes(ctrlLine, {
|
2010-10-27 16:34:15 +00:00
|
|
|
x1: mouse_x,
|
|
|
|
y1: mouse_y,
|
|
|
|
x2: alt_x * current_zoom,
|
|
|
|
y2: alt_y * current_zoom,
|
|
|
|
display: 'inline'
|
|
|
|
});
|
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (index === 0) {
|
2010-10-27 16:34:15 +00:00
|
|
|
firstCtrl = [mouse_x, mouse_y];
|
|
|
|
} else {
|
|
|
|
var last = seglist.getItem(index - 1);
|
|
|
|
var last_x = last.x;
|
2013-02-15 15:51:58 +00:00
|
|
|
var last_y = last.y;
|
2010-10-27 16:34:15 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (last.pathSegType === 6) {
|
2010-10-27 16:34:15 +00:00
|
|
|
last_x += (last_x - last.x2);
|
|
|
|
last_y += (last_y - last.y2);
|
2013-02-15 15:51:58 +00:00
|
|
|
} else if (firstCtrl) {
|
2011-10-01 04:41:14 +00:00
|
|
|
last_x = firstCtrl[0]/current_zoom;
|
|
|
|
last_y = firstCtrl[1]/current_zoom;
|
2010-10-27 16:34:15 +00:00
|
|
|
}
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.replacePathSeg(6, index, [pt_x, pt_y, last_x, last_y, alt_x, alt_y], drawn_path);
|
2010-10-27 16:34:15 +00:00
|
|
|
}
|
|
|
|
} else {
|
2014-04-08 03:21:22 +00:00
|
|
|
var stretchy = svgedit.utilities.getElem('path_stretch_line');
|
2010-10-27 16:34:15 +00:00
|
|
|
if (stretchy) {
|
|
|
|
var prev = seglist.getItem(index);
|
2013-02-15 15:51:58 +00:00
|
|
|
if (prev.pathSegType === 6) {
|
2010-10-27 16:34:15 +00:00
|
|
|
var prev_x = prev.x + (prev.x - prev.x2);
|
|
|
|
var prev_y = prev.y + (prev.y - prev.y2);
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.replacePathSeg(6, 1, [mouse_x, mouse_y, prev_x * current_zoom, prev_y * current_zoom, mouse_x, mouse_y], stretchy);
|
2013-02-15 15:51:58 +00:00
|
|
|
} else if (firstCtrl) {
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.replacePathSeg(6, 1, [mouse_x, mouse_y, firstCtrl[0], firstCtrl[1], mouse_x, mouse_y], stretchy);
|
2010-10-27 16:34:15 +00:00
|
|
|
} else {
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.replacePathSeg(4, 1, [mouse_x, mouse_y], stretchy);
|
2010-10-27 16:34:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// if we are dragging a point, let's move it
|
2011-02-04 08:02:46 +00:00
|
|
|
if (svgedit.path.path.dragging) {
|
|
|
|
var pt = svgedit.path.getPointFromGrip({
|
|
|
|
x: svgedit.path.path.dragging[0],
|
|
|
|
y: svgedit.path.path.dragging[1]
|
|
|
|
}, svgedit.path.path);
|
|
|
|
var mpt = svgedit.path.getPointFromGrip({
|
2010-10-27 16:34:15 +00:00
|
|
|
x: mouse_x,
|
|
|
|
y: mouse_y
|
2011-02-04 08:02:46 +00:00
|
|
|
}, svgedit.path.path);
|
2010-10-27 16:34:15 +00:00
|
|
|
var diff_x = mpt.x - pt.x;
|
|
|
|
var diff_y = mpt.y - pt.y;
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.path.dragging = [mouse_x, mouse_y];
|
2010-10-27 16:34:15 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (svgedit.path.path.dragctrl) {
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.path.moveCtrl(diff_x, diff_y);
|
2010-10-27 16:34:15 +00:00
|
|
|
} else {
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.path.movePts(diff_x, diff_y);
|
2010-10-27 16:34:15 +00:00
|
|
|
}
|
|
|
|
} else {
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.path.selected_pts = [];
|
|
|
|
svgedit.path.path.eachSeg(function(i) {
|
2010-10-27 16:34:15 +00:00
|
|
|
var seg = this;
|
2014-02-11 13:46:39 +00:00
|
|
|
if (!seg.next && !seg.prev) {return;}
|
2010-10-27 16:34:15 +00:00
|
|
|
|
|
|
|
var item = seg.item;
|
|
|
|
var rbb = rubberBox.getBBox();
|
|
|
|
|
2011-02-04 08:02:46 +00:00
|
|
|
var pt = svgedit.path.getGripPt(seg);
|
2010-10-27 16:34:15 +00:00
|
|
|
var pt_bb = {
|
|
|
|
x: pt.x,
|
|
|
|
y: pt.y,
|
|
|
|
width: 0,
|
|
|
|
height: 0
|
|
|
|
};
|
|
|
|
|
2010-11-08 08:42:46 +00:00
|
|
|
var sel = svgedit.math.rectsIntersect(rbb, pt_bb);
|
2010-10-27 16:34:15 +00:00
|
|
|
|
|
|
|
this.select(sel);
|
|
|
|
//Note that addPtsToSelection is not being run
|
2014-02-11 13:46:39 +00:00
|
|
|
if (sel) {svgedit.path.path.selected_pts.push(seg.index);}
|
2010-10-27 16:34:15 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
},
|
|
|
|
mouseUp: function(evt, element, mouse_x, mouse_y) {
|
|
|
|
|
|
|
|
// Create mode
|
2014-04-08 03:21:22 +00:00
|
|
|
if (current_mode === 'path') {
|
2010-10-27 16:34:15 +00:00
|
|
|
newPoint = null;
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!drawn_path) {
|
2013-02-14 15:19:46 +00:00
|
|
|
element = svgedit.utilities.getElem(getId());
|
2010-10-27 16:34:15 +00:00
|
|
|
started = false;
|
|
|
|
firstCtrl = null;
|
|
|
|
}
|
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
return {
|
2010-10-27 16:34:15 +00:00
|
|
|
keep: true,
|
2010-06-22 14:52:51 +00:00
|
|
|
element: element
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Edit mode
|
|
|
|
|
2011-02-04 08:02:46 +00:00
|
|
|
if (svgedit.path.path.dragging) {
|
|
|
|
var last_pt = svgedit.path.path.cur_pt;
|
2010-01-19 21:14:53 +00:00
|
|
|
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.path.dragging = false;
|
|
|
|
svgedit.path.path.dragctrl = false;
|
|
|
|
svgedit.path.path.update();
|
2010-02-26 18:18:21 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (hasMoved) {
|
2014-04-08 03:21:22 +00:00
|
|
|
svgedit.path.path.endChanges('Move path point(s)');
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
2010-02-26 18:18:21 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!evt.shiftKey && !hasMoved) {
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.path.selectPt(last_pt);
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
2013-02-15 15:51:58 +00:00
|
|
|
} else if (rubberBox && rubberBox.getAttribute('display') != 'none') {
|
2010-06-22 14:52:51 +00:00
|
|
|
// Done with multi-node-select
|
2014-04-08 03:21:22 +00:00
|
|
|
rubberBox.setAttribute('display', 'none');
|
2010-02-26 18:18:21 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (rubberBox.getAttribute('width') <= 2 && rubberBox.getAttribute('height') <= 2) {
|
2010-06-22 14:52:51 +00:00
|
|
|
pathActions.toSelectMode(evt.target);
|
2010-03-03 19:26:09 +00:00
|
|
|
}
|
2010-06-16 13:16:00 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// else, move back to select mode
|
|
|
|
} else {
|
|
|
|
pathActions.toSelectMode(evt.target);
|
|
|
|
}
|
|
|
|
hasMoved = false;
|
|
|
|
},
|
|
|
|
toEditMode: function(element) {
|
2011-02-23 16:24:07 +00:00
|
|
|
svgedit.path.path = svgedit.path.getPath_(element);
|
2014-04-08 03:21:22 +00:00
|
|
|
current_mode = 'pathedit';
|
2010-06-30 18:27:36 +00:00
|
|
|
clearSelection();
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.path.show(true).update();
|
2011-02-24 16:13:26 +00:00
|
|
|
svgedit.path.path.oldbbox = svgedit.utilities.getBBox(svgedit.path.path.elem);
|
2010-06-22 14:52:51 +00:00
|
|
|
subpath = false;
|
|
|
|
},
|
|
|
|
toSelectMode: function(elem) {
|
2011-02-04 08:02:46 +00:00
|
|
|
var selPath = (elem == svgedit.path.path.elem);
|
2014-04-08 03:21:22 +00:00
|
|
|
current_mode = 'select';
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.path.show(false);
|
2010-06-22 14:52:51 +00:00
|
|
|
current_path = false;
|
2010-06-30 18:27:36 +00:00
|
|
|
clearSelection();
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (svgedit.path.path.matrix) {
|
2010-06-22 14:52:51 +00:00
|
|
|
// Rotated, so may need to re-calculate the center
|
2011-03-07 18:26:12 +00:00
|
|
|
svgedit.path.recalcRotatedPath();
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (selPath) {
|
2014-04-08 03:21:22 +00:00
|
|
|
call('selected', [elem]);
|
2010-06-30 18:27:36 +00:00
|
|
|
addToSelection([elem], true);
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
addSubPath: function(on) {
|
2013-02-15 15:51:58 +00:00
|
|
|
if (on) {
|
2010-06-22 14:52:51 +00:00
|
|
|
// Internally we go into "path" mode, but in the UI it will
|
|
|
|
// still appear as if in "pathedit" mode.
|
2014-04-08 03:21:22 +00:00
|
|
|
current_mode = 'path';
|
2010-06-22 14:52:51 +00:00
|
|
|
subpath = true;
|
|
|
|
} else {
|
|
|
|
pathActions.clear(true);
|
2011-02-04 08:02:46 +00:00
|
|
|
pathActions.toEditMode(svgedit.path.path.elem);
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
select: function(target) {
|
2010-10-15 19:41:37 +00:00
|
|
|
if (current_path === target) {
|
2010-06-22 14:52:51 +00:00
|
|
|
pathActions.toEditMode(target);
|
2014-04-08 03:21:22 +00:00
|
|
|
current_mode = 'pathedit';
|
2010-06-22 14:52:51 +00:00
|
|
|
} // going into pathedit mode
|
|
|
|
else {
|
|
|
|
current_path = target;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
reorient: function() {
|
|
|
|
var elem = selectedElements[0];
|
2014-02-11 13:46:39 +00:00
|
|
|
if (!elem) {return;}
|
2013-02-14 15:19:46 +00:00
|
|
|
var angle = svgedit.utilities.getRotationAngle(elem);
|
2014-02-11 13:46:39 +00:00
|
|
|
if (angle == 0) {return;}
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
var batchCmd = new svgedit.history.BatchCommand('Reorient path');
|
2010-06-22 14:52:51 +00:00
|
|
|
var changes = {
|
|
|
|
d: elem.getAttribute('d'),
|
|
|
|
transform: elem.getAttribute('transform')
|
|
|
|
};
|
2013-02-17 08:21:07 +00:00
|
|
|
batchCmd.addSubCommand(new svgedit.history.ChangeElementCommand(elem, changes));
|
2010-06-30 18:27:36 +00:00
|
|
|
clearSelection();
|
2010-06-22 14:52:51 +00:00
|
|
|
this.resetOrientation(elem);
|
|
|
|
|
|
|
|
addCommandToHistory(batchCmd);
|
2010-02-26 18:18:21 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// Set matrix to null
|
2011-02-23 16:24:07 +00:00
|
|
|
svgedit.path.getPath_(elem).show(false).matrix = null;
|
2010-02-26 18:18:21 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
this.clear();
|
|
|
|
|
2010-06-30 18:27:36 +00:00
|
|
|
addToSelection([elem], true);
|
2014-04-08 03:21:22 +00:00
|
|
|
call('changed', selectedElements);
|
2010-06-22 14:52:51 +00:00
|
|
|
},
|
2009-10-05 13:50:44 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
clear: function(remove) {
|
|
|
|
current_path = null;
|
2010-10-27 16:34:15 +00:00
|
|
|
if (drawn_path) {
|
2013-02-14 15:19:46 +00:00
|
|
|
var elem = svgedit.utilities.getElem(getId());
|
2014-04-08 03:21:22 +00:00
|
|
|
$(svgedit.utilities.getElem('path_stretch_line')).remove();
|
2010-06-22 14:52:51 +00:00
|
|
|
$(elem).remove();
|
2014-04-08 03:21:22 +00:00
|
|
|
$(svgedit.utilities.getElem('pathpointgrip_container')).find('*').attr('display', 'none');
|
2010-10-27 16:34:15 +00:00
|
|
|
drawn_path = firstCtrl = null;
|
2010-06-22 14:52:51 +00:00
|
|
|
started = false;
|
2014-04-08 03:21:22 +00:00
|
|
|
} else if (current_mode == 'pathedit') {
|
2010-06-22 14:52:51 +00:00
|
|
|
this.toSelectMode();
|
|
|
|
}
|
2014-02-11 13:46:39 +00:00
|
|
|
if (svgedit.path.path) {svgedit.path.path.init().show(false);}
|
2010-06-22 14:52:51 +00:00
|
|
|
},
|
|
|
|
resetOrientation: function(path) {
|
2014-02-11 13:46:39 +00:00
|
|
|
if (path == null || path.nodeName != 'path') {return false;}
|
2013-02-14 15:19:46 +00:00
|
|
|
var tlist = svgedit.transformlist.getTransformList(path);
|
|
|
|
var m = svgedit.math.transformListToTransform(tlist).matrix;
|
2010-06-22 14:52:51 +00:00
|
|
|
tlist.clear();
|
2014-04-08 03:21:22 +00:00
|
|
|
path.removeAttribute('transform');
|
2010-06-22 14:52:51 +00:00
|
|
|
var segList = path.pathSegList;
|
|
|
|
|
|
|
|
// Opera/win/non-EN throws an error here.
|
|
|
|
// TODO: Find out why!
|
|
|
|
// Presumed fixed in Opera 10.5, so commented out for now
|
|
|
|
|
2014-01-31 00:39:35 +00:00
|
|
|
// try {
|
2010-06-22 14:52:51 +00:00
|
|
|
var len = segList.numberOfItems;
|
2014-01-31 00:39:35 +00:00
|
|
|
// } catch(err) {
|
|
|
|
// var fixed_d = pathActions.convertPath(path);
|
|
|
|
// path.setAttribute('d', fixed_d);
|
|
|
|
// segList = path.pathSegList;
|
|
|
|
// var len = segList.numberOfItems;
|
|
|
|
// }
|
2014-02-11 13:46:39 +00:00
|
|
|
var i, last_x, last_y;
|
2010-12-20 18:38:54 +00:00
|
|
|
|
2014-02-11 13:46:39 +00:00
|
|
|
for (i = 0; i < len; ++i) {
|
2010-06-22 14:52:51 +00:00
|
|
|
var seg = segList.getItem(i);
|
|
|
|
var type = seg.pathSegType;
|
2014-02-11 13:46:39 +00:00
|
|
|
if (type == 1) {continue;}
|
2010-06-22 14:52:51 +00:00
|
|
|
var pts = [];
|
|
|
|
$.each(['',1,2], function(j, n) {
|
|
|
|
var x = seg['x'+n], y = seg['y'+n];
|
2013-02-15 15:51:58 +00:00
|
|
|
if (x !== undefined && y !== undefined) {
|
2013-02-14 15:19:46 +00:00
|
|
|
var pt = svgedit.math.transformPoint(x, y, m);
|
2010-06-22 14:52:51 +00:00
|
|
|
pts.splice(pts.length, 0, pt.x, pt.y);
|
|
|
|
}
|
|
|
|
});
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.replacePathSeg(type, i, pts, path);
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
2010-12-21 19:36:51 +00:00
|
|
|
|
|
|
|
reorientGrads(path, m);
|
2010-06-22 14:52:51 +00:00
|
|
|
},
|
|
|
|
zoomChange: function() {
|
2014-04-08 03:21:22 +00:00
|
|
|
if (current_mode == 'pathedit') {
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.path.update();
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
getNodePoint: function() {
|
2011-02-04 08:02:46 +00:00
|
|
|
var sel_pt = svgedit.path.path.selected_pts.length ? svgedit.path.path.selected_pts[0] : 1;
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2011-02-04 08:02:46 +00:00
|
|
|
var seg = svgedit.path.path.segs[sel_pt];
|
2010-06-22 14:52:51 +00:00
|
|
|
return {
|
|
|
|
x: seg.item.x,
|
|
|
|
y: seg.item.y,
|
|
|
|
type: seg.type
|
|
|
|
};
|
|
|
|
},
|
|
|
|
linkControlPoints: function(linkPoints) {
|
2011-02-04 16:06:25 +00:00
|
|
|
svgedit.path.setLinkControlPoints(linkPoints);
|
2010-06-22 14:52:51 +00:00
|
|
|
},
|
|
|
|
clonePathNode: function() {
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.path.storeD();
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2011-02-04 08:02:46 +00:00
|
|
|
var sel_pts = svgedit.path.path.selected_pts;
|
|
|
|
var segs = svgedit.path.path.segs;
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
var i = sel_pts.length;
|
|
|
|
var nums = [];
|
2010-01-21 20:12:49 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
while (i--) {
|
2010-06-22 14:52:51 +00:00
|
|
|
var pt = sel_pts[i];
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.path.addSeg(pt);
|
2010-02-26 18:18:21 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
nums.push(pt + i);
|
|
|
|
nums.push(pt + i + 1);
|
|
|
|
}
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.path.init().addPtsToSelection(nums);
|
2010-02-26 18:18:21 +00:00
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
svgedit.path.path.endChanges('Clone path node(s)');
|
2010-06-22 14:52:51 +00:00
|
|
|
},
|
|
|
|
opencloseSubPath: function() {
|
2011-02-04 08:02:46 +00:00
|
|
|
var sel_pts = svgedit.path.path.selected_pts;
|
2010-06-22 14:52:51 +00:00
|
|
|
// Only allow one selected node for now
|
2014-02-11 14:02:48 +00:00
|
|
|
if (sel_pts.length !== 1) {return;}
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2011-02-04 08:02:46 +00:00
|
|
|
var elem = svgedit.path.path.elem;
|
2010-06-22 14:52:51 +00:00
|
|
|
var list = elem.pathSegList;
|
2010-03-24 20:13:13 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
var len = list.numberOfItems;
|
2010-03-24 20:13:13 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
var index = sel_pts[0];
|
|
|
|
|
|
|
|
var open_pt = null;
|
|
|
|
var start_item = null;
|
2010-03-24 20:13:13 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// Check if subpath is already open
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.path.eachSeg(function(i) {
|
2013-02-15 15:51:58 +00:00
|
|
|
if (this.type === 2 && i <= index) {
|
2010-06-22 14:52:51 +00:00
|
|
|
start_item = this.item;
|
|
|
|
}
|
2014-02-11 14:02:48 +00:00
|
|
|
if (i <= index) {return true;}
|
2013-02-15 15:51:58 +00:00
|
|
|
if (this.type === 2) {
|
2010-06-22 14:52:51 +00:00
|
|
|
// Found M first, so open
|
|
|
|
open_pt = i;
|
|
|
|
return false;
|
2014-02-11 14:02:48 +00:00
|
|
|
}
|
|
|
|
if (this.type === 1) {
|
2010-06-22 14:52:51 +00:00
|
|
|
// Found Z first, so closed
|
|
|
|
open_pt = false;
|
|
|
|
return false;
|
2010-03-24 20:13:13 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
});
|
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (open_pt == null) {
|
2010-06-22 14:52:51 +00:00
|
|
|
// Single path, so close last seg
|
2011-02-04 08:02:46 +00:00
|
|
|
open_pt = svgedit.path.path.segs.length - 1;
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
2010-03-24 20:13:13 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (open_pt !== false) {
|
2010-06-22 14:52:51 +00:00
|
|
|
// Close this path
|
2010-03-24 20:13:13 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// Create a line going to the previous "M"
|
|
|
|
var newseg = elem.createSVGPathSegLinetoAbs(start_item.x, start_item.y);
|
|
|
|
|
|
|
|
var closer = elem.createSVGPathSegClosePath();
|
2013-02-15 15:51:58 +00:00
|
|
|
if (open_pt == svgedit.path.path.segs.length - 1) {
|
2010-06-22 14:52:51 +00:00
|
|
|
list.appendItem(newseg);
|
|
|
|
list.appendItem(closer);
|
|
|
|
} else {
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.insertItemBefore(elem, closer, open_pt);
|
|
|
|
svgedit.path.insertItemBefore(elem, newseg, open_pt);
|
2010-03-24 20:13:13 +00:00
|
|
|
}
|
|
|
|
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.path.init().selectPt(open_pt+1);
|
2010-06-22 14:52:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// M 1,1 L 2,2 L 3,3 L 1,1 z // open at 2,2
|
|
|
|
// M 2,2 L 3,3 L 1,1
|
|
|
|
|
|
|
|
// M 1,1 L 2,2 L 1,1 z M 4,4 L 5,5 L6,6 L 5,5 z
|
|
|
|
// M 1,1 L 2,2 L 1,1 z [M 4,4] L 5,5 L(M)6,6 L 5,5 z
|
|
|
|
|
2011-02-04 08:02:46 +00:00
|
|
|
var seg = svgedit.path.path.segs[index];
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (seg.mate) {
|
2010-06-22 14:52:51 +00:00
|
|
|
list.removeItem(index); // Removes last "L"
|
|
|
|
list.removeItem(index); // Removes the "Z"
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.path.init().selectPt(index - 1);
|
2010-06-22 14:52:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-11 14:02:48 +00:00
|
|
|
var i, last_m, z_seg;
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
// Find this sub-path's closing point and remove
|
2014-02-11 14:02:48 +00:00
|
|
|
for (i = 0; i<list.numberOfItems; i++) {
|
2010-06-22 14:52:51 +00:00
|
|
|
var item = list.getItem(i);
|
2010-03-24 20:13:13 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (item.pathSegType === 2) {
|
2010-06-22 14:52:51 +00:00
|
|
|
// Find the preceding M
|
|
|
|
last_m = i;
|
2013-02-15 15:51:58 +00:00
|
|
|
} else if (i === index) {
|
2010-06-22 14:52:51 +00:00
|
|
|
// Remove it
|
|
|
|
list.removeItem(last_m);
|
2014-01-31 00:39:35 +00:00
|
|
|
// index--;
|
2013-02-15 15:51:58 +00:00
|
|
|
} else if (item.pathSegType === 1 && index < i) {
|
2010-06-22 14:52:51 +00:00
|
|
|
// Remove the closing seg of this subpath
|
|
|
|
z_seg = i-1;
|
|
|
|
list.removeItem(i);
|
|
|
|
break;
|
2010-03-24 20:13:13 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var num = (index - last_m) - 1;
|
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
while (num--) {
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.insertItemBefore(elem, list.getItem(last_m), z_seg);
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var pt = list.getItem(last_m);
|
|
|
|
|
|
|
|
// Make this point the new "M"
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.replacePathSeg(2, last_m, [pt.x, pt.y]);
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2014-02-11 14:02:48 +00:00
|
|
|
i = index; // i is local here, so has no effect; what is the reason for this?
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.path.init().selectPt(0);
|
2010-06-22 14:52:51 +00:00
|
|
|
},
|
|
|
|
deletePathNode: function() {
|
2014-02-11 14:02:48 +00:00
|
|
|
if (!pathActions.canDeleteNodes) {return;}
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.path.storeD();
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2011-02-04 08:02:46 +00:00
|
|
|
var sel_pts = svgedit.path.path.selected_pts;
|
2010-06-22 14:52:51 +00:00
|
|
|
var i = sel_pts.length;
|
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
while (i--) {
|
2010-06-22 14:52:51 +00:00
|
|
|
var pt = sel_pts[i];
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.path.deleteSeg(pt);
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
var cleanup = function() {
|
2011-02-04 08:02:46 +00:00
|
|
|
var segList = svgedit.path.path.elem.pathSegList;
|
2010-06-22 14:52:51 +00:00
|
|
|
var len = segList.numberOfItems;
|
2010-03-24 20:13:13 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
var remItems = function(pos, count) {
|
2013-02-15 15:51:58 +00:00
|
|
|
while (count--) {
|
2010-06-22 14:52:51 +00:00
|
|
|
segList.removeItem(pos);
|
|
|
|
}
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-01-21 17:38:37 +00:00
|
|
|
|
2014-02-11 14:02:48 +00:00
|
|
|
if (len <= 1) {return true;}
|
2009-12-30 18:06:29 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
while (len--) {
|
2010-06-22 14:52:51 +00:00
|
|
|
var item = segList.getItem(len);
|
2013-02-15 15:51:58 +00:00
|
|
|
if (item.pathSegType === 1) {
|
2010-06-22 14:52:51 +00:00
|
|
|
var prev = segList.getItem(len-1);
|
|
|
|
var nprev = segList.getItem(len-2);
|
2013-02-15 15:51:58 +00:00
|
|
|
if (prev.pathSegType === 2) {
|
2010-06-22 14:52:51 +00:00
|
|
|
remItems(len-1, 2);
|
|
|
|
cleanup();
|
|
|
|
break;
|
2013-02-15 15:51:58 +00:00
|
|
|
} else if (nprev.pathSegType === 2) {
|
2010-06-22 14:52:51 +00:00
|
|
|
remItems(len-2, 3);
|
|
|
|
cleanup();
|
|
|
|
break;
|
2010-03-11 15:27:18 +00:00
|
|
|
}
|
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
} else if (item.pathSegType === 2) {
|
|
|
|
if (len > 0) {
|
2010-06-22 14:52:51 +00:00
|
|
|
var prev_type = segList.getItem(len-1).pathSegType;
|
2014-01-31 10:40:52 +00:00
|
|
|
// Path has M M
|
2013-02-15 15:51:58 +00:00
|
|
|
if (prev_type === 2) {
|
2010-06-22 14:52:51 +00:00
|
|
|
remItems(len-1, 1);
|
2010-03-11 15:27:18 +00:00
|
|
|
cleanup();
|
|
|
|
break;
|
2010-06-22 14:52:51 +00:00
|
|
|
// Entire path ends with Z M
|
2013-02-15 15:51:58 +00:00
|
|
|
} else if (prev_type === 1 && segList.numberOfItems-1 === len) {
|
2010-06-22 14:52:51 +00:00
|
|
|
remItems(len, 1);
|
2010-03-11 15:27:18 +00:00
|
|
|
cleanup();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-02-26 19:44:41 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
return false;
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
cleanup();
|
|
|
|
|
|
|
|
// Completely delete a path with 1 or 0 segments
|
2013-02-15 15:51:58 +00:00
|
|
|
if (svgedit.path.path.elem.pathSegList.numberOfItems <= 1) {
|
2011-02-04 08:02:46 +00:00
|
|
|
pathActions.toSelectMode(svgedit.path.path.elem);
|
2010-06-22 14:52:51 +00:00
|
|
|
canvas.deleteSelectedElements();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.path.init();
|
|
|
|
svgedit.path.path.clearSelection();
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
// TODO: Find right way to select point now
|
|
|
|
// path.selectPt(sel_pt);
|
2013-02-15 15:51:58 +00:00
|
|
|
if (window.opera) { // Opera repaints incorrectly
|
2013-02-15 16:51:48 +00:00
|
|
|
var cp = $(svgedit.path.path.elem);
|
|
|
|
cp.attr('d', cp.attr('d'));
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
2014-04-08 03:21:22 +00:00
|
|
|
svgedit.path.path.endChanges('Delete path node(s)');
|
2010-06-22 14:52:51 +00:00
|
|
|
},
|
|
|
|
smoothPolylineIntoPath: smoothPolylineIntoPath,
|
|
|
|
setSegType: function(v) {
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.path.setSegType(v);
|
2010-06-22 14:52:51 +00:00
|
|
|
},
|
|
|
|
moveNode: function(attr, newValue) {
|
2011-02-04 08:02:46 +00:00
|
|
|
var sel_pts = svgedit.path.path.selected_pts;
|
2014-02-11 14:02:48 +00:00
|
|
|
if (!sel_pts.length) {return;}
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.path.storeD();
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
// Get first selected point
|
2011-02-04 08:02:46 +00:00
|
|
|
var seg = svgedit.path.path.segs[sel_pts[0]];
|
2010-06-22 14:52:51 +00:00
|
|
|
var diff = {x:0, y:0};
|
|
|
|
diff[attr] = newValue - seg.item[attr];
|
|
|
|
|
|
|
|
seg.move(diff.x, diff.y);
|
2014-04-08 03:21:22 +00:00
|
|
|
svgedit.path.path.endChanges('Move path point');
|
2010-06-22 14:52:51 +00:00
|
|
|
},
|
|
|
|
fixEnd: function(elem) {
|
|
|
|
// Adds an extra segment if the last seg before a Z doesn't end
|
|
|
|
// at its M point
|
|
|
|
// M0,0 L0,100 L100,100 z
|
|
|
|
var segList = elem.pathSegList;
|
|
|
|
var len = segList.numberOfItems;
|
2014-02-11 14:02:48 +00:00
|
|
|
var i, last_m;
|
|
|
|
for (i = 0; i < len; ++i) {
|
2010-06-22 14:52:51 +00:00
|
|
|
var item = segList.getItem(i);
|
2013-02-15 15:51:58 +00:00
|
|
|
if (item.pathSegType === 2) {
|
2010-06-22 14:52:51 +00:00
|
|
|
last_m = item;
|
|
|
|
}
|
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (item.pathSegType === 1) {
|
2010-06-22 14:52:51 +00:00
|
|
|
var prev = segList.getItem(i-1);
|
2013-02-15 15:51:58 +00:00
|
|
|
if (prev.x != last_m.x || prev.y != last_m.y) {
|
2010-06-22 14:52:51 +00:00
|
|
|
// Add an L segment here
|
|
|
|
var newseg = elem.createSVGPathSegLinetoAbs(last_m.x, last_m.y);
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.insertItemBefore(elem, newseg, i);
|
2010-06-22 14:52:51 +00:00
|
|
|
// Can this be done better?
|
|
|
|
pathActions.fixEnd(elem);
|
|
|
|
break;
|
2010-02-26 19:44:41 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2014-02-11 14:02:48 +00:00
|
|
|
if (svgedit.browser.isWebkit()) {resetD(elem);}
|
2010-06-22 14:52:51 +00:00
|
|
|
},
|
|
|
|
// Convert a path to one with only absolute or relative values
|
|
|
|
convertPath: function(path, toRel) {
|
2014-02-11 14:02:48 +00:00
|
|
|
var i;
|
2010-06-22 14:52:51 +00:00
|
|
|
var segList = path.pathSegList;
|
|
|
|
var len = segList.numberOfItems;
|
|
|
|
var curx = 0, cury = 0;
|
2014-04-08 03:21:22 +00:00
|
|
|
var d = '';
|
2010-06-22 14:52:51 +00:00
|
|
|
var last_m = null;
|
|
|
|
|
2014-02-11 14:02:48 +00:00
|
|
|
for (i = 0; i < len; ++i) {
|
2010-06-22 14:52:51 +00:00
|
|
|
var seg = segList.getItem(i);
|
|
|
|
// if these properties are not in the segment, set them to zero
|
|
|
|
var x = seg.x || 0,
|
|
|
|
y = seg.y || 0,
|
|
|
|
x1 = seg.x1 || 0,
|
|
|
|
y1 = seg.y1 || 0,
|
|
|
|
x2 = seg.x2 || 0,
|
|
|
|
y2 = seg.y2 || 0;
|
|
|
|
|
|
|
|
var type = seg.pathSegType;
|
|
|
|
var letter = pathMap[type]['to'+(toRel?'Lower':'Upper')+'Case']();
|
|
|
|
|
|
|
|
var addToD = function(pnts, more, last) {
|
|
|
|
var str = '';
|
2014-02-11 14:02:48 +00:00
|
|
|
more = more ? ' ' + more.join(' ') : '';
|
|
|
|
last = last ? ' ' + svgedit.units.shortFloat(last) : '';
|
2010-06-22 14:52:51 +00:00
|
|
|
$.each(pnts, function(i, pnt) {
|
2011-02-02 17:28:43 +00:00
|
|
|
pnts[i] = svgedit.units.shortFloat(pnt);
|
2010-06-22 14:52:51 +00:00
|
|
|
});
|
|
|
|
d += letter + pnts.join(' ') + more + last;
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2009-12-30 18:06:29 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
switch (type) {
|
|
|
|
case 1: // z,Z closepath (Z/z)
|
2014-04-08 03:21:22 +00:00
|
|
|
d += 'z';
|
2010-06-22 14:52:51 +00:00
|
|
|
break;
|
|
|
|
case 12: // absolute horizontal line (H)
|
|
|
|
x -= curx;
|
|
|
|
case 13: // relative horizontal line (h)
|
2013-02-15 15:51:58 +00:00
|
|
|
if (toRel) {
|
2010-06-22 14:52:51 +00:00
|
|
|
curx += x;
|
2010-12-20 18:38:54 +00:00
|
|
|
letter = 'l';
|
2010-06-22 14:52:51 +00:00
|
|
|
} else {
|
|
|
|
x += curx;
|
|
|
|
curx = x;
|
2010-12-20 18:38:54 +00:00
|
|
|
letter = 'L';
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
2010-12-20 18:38:54 +00:00
|
|
|
// Convert to "line" for easier editing
|
|
|
|
addToD([[x, cury]]);
|
2010-06-22 14:52:51 +00:00
|
|
|
break;
|
|
|
|
case 14: // absolute vertical line (V)
|
|
|
|
y -= cury;
|
|
|
|
case 15: // relative vertical line (v)
|
2013-02-15 15:51:58 +00:00
|
|
|
if (toRel) {
|
2010-06-22 14:52:51 +00:00
|
|
|
cury += y;
|
2010-12-20 18:38:54 +00:00
|
|
|
letter = 'l';
|
2010-06-22 14:52:51 +00:00
|
|
|
} else {
|
|
|
|
y += cury;
|
|
|
|
cury = y;
|
2010-12-20 18:38:54 +00:00
|
|
|
letter = 'L';
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
2010-12-20 18:38:54 +00:00
|
|
|
// Convert to "line" for easier editing
|
|
|
|
addToD([[curx, y]]);
|
2010-06-22 14:52:51 +00:00
|
|
|
break;
|
|
|
|
case 2: // absolute move (M)
|
|
|
|
case 4: // absolute line (L)
|
|
|
|
case 18: // absolute smooth quad (T)
|
|
|
|
x -= curx;
|
|
|
|
y -= cury;
|
|
|
|
case 5: // relative line (l)
|
|
|
|
case 3: // relative move (m)
|
|
|
|
// If the last segment was a "z", this must be relative to
|
2013-02-15 15:51:58 +00:00
|
|
|
if (last_m && segList.getItem(i-1).pathSegType === 1 && !toRel) {
|
2010-06-22 14:52:51 +00:00
|
|
|
curx = last_m[0];
|
|
|
|
cury = last_m[1];
|
|
|
|
}
|
2009-12-30 18:06:29 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
case 19: // relative smooth quad (t)
|
2013-02-15 15:51:58 +00:00
|
|
|
if (toRel) {
|
2010-06-22 14:52:51 +00:00
|
|
|
curx += x;
|
|
|
|
cury += y;
|
|
|
|
} else {
|
|
|
|
x += curx;
|
|
|
|
y += cury;
|
|
|
|
curx = x;
|
|
|
|
cury = y;
|
|
|
|
}
|
2014-02-17 06:48:40 +00:00
|
|
|
if (type === 3) {last_m = [curx, cury];}
|
2010-04-23 17:00:03 +00:00
|
|
|
|
2013-02-15 16:51:48 +00:00
|
|
|
addToD([[x, y]]);
|
2010-06-22 14:52:51 +00:00
|
|
|
break;
|
|
|
|
case 6: // absolute cubic (C)
|
|
|
|
x -= curx; x1 -= curx; x2 -= curx;
|
|
|
|
y -= cury; y1 -= cury; y2 -= cury;
|
|
|
|
case 7: // relative cubic (c)
|
2013-02-15 15:51:58 +00:00
|
|
|
if (toRel) {
|
2010-06-22 14:52:51 +00:00
|
|
|
curx += x;
|
|
|
|
cury += y;
|
|
|
|
} else {
|
|
|
|
x += curx; x1 += curx; x2 += curx;
|
|
|
|
y += cury; y1 += cury; y2 += cury;
|
|
|
|
curx = x;
|
|
|
|
cury = y;
|
|
|
|
}
|
2013-02-15 16:51:48 +00:00
|
|
|
addToD([[x1, y1], [x2, y2], [x, y]]);
|
2010-06-22 14:52:51 +00:00
|
|
|
break;
|
|
|
|
case 8: // absolute quad (Q)
|
|
|
|
x -= curx; x1 -= curx;
|
2010-06-22 18:17:42 +00:00
|
|
|
y -= cury; y1 -= cury;
|
|
|
|
case 9: // relative quad (q)
|
2013-02-15 15:51:58 +00:00
|
|
|
if (toRel) {
|
2010-06-22 18:17:42 +00:00
|
|
|
curx += x;
|
|
|
|
cury += y;
|
|
|
|
} else {
|
|
|
|
x += curx; x1 += curx;
|
|
|
|
y += cury; y1 += cury;
|
|
|
|
curx = x;
|
|
|
|
cury = y;
|
|
|
|
}
|
2013-02-15 16:51:48 +00:00
|
|
|
addToD([[x1, y1],[x, y]]);
|
2010-06-22 18:17:42 +00:00
|
|
|
break;
|
|
|
|
case 10: // absolute elliptical arc (A)
|
|
|
|
x -= curx;
|
|
|
|
y -= cury;
|
|
|
|
case 11: // relative elliptical arc (a)
|
2013-02-15 15:51:58 +00:00
|
|
|
if (toRel) {
|
2010-06-22 18:17:42 +00:00
|
|
|
curx += x;
|
|
|
|
cury += y;
|
|
|
|
} else {
|
|
|
|
x += curx;
|
|
|
|
y += cury;
|
|
|
|
curx = x;
|
|
|
|
cury = y;
|
|
|
|
}
|
2013-02-15 16:51:48 +00:00
|
|
|
addToD([[seg.r1, seg.r2]], [
|
2010-06-22 18:17:42 +00:00
|
|
|
seg.angle,
|
|
|
|
(seg.largeArcFlag ? 1 : 0),
|
|
|
|
(seg.sweepFlag ? 1 : 0)
|
2013-02-15 16:51:48 +00:00
|
|
|
], [x, y]
|
2010-06-22 18:17:42 +00:00
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 16: // absolute smooth cubic (S)
|
|
|
|
x -= curx; x2 -= curx;
|
|
|
|
y -= cury; y2 -= cury;
|
|
|
|
case 17: // relative smooth cubic (s)
|
2013-02-15 15:51:58 +00:00
|
|
|
if (toRel) {
|
2010-06-22 18:17:42 +00:00
|
|
|
curx += x;
|
|
|
|
cury += y;
|
|
|
|
} else {
|
|
|
|
x += curx; x2 += curx;
|
|
|
|
y += cury; y2 += cury;
|
|
|
|
curx = x;
|
|
|
|
cury = y;
|
|
|
|
}
|
2013-02-15 16:51:48 +00:00
|
|
|
addToD([[x2, y2],[x, y]]);
|
2010-06-22 18:17:42 +00:00
|
|
|
break;
|
|
|
|
} // switch on path segment type
|
|
|
|
} // for each segment
|
|
|
|
return d;
|
2009-12-30 18:06:29 +00:00
|
|
|
}
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-06-22 18:17:42 +00:00
|
|
|
}();
|
2011-02-04 16:06:25 +00:00
|
|
|
// end pathActions
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
// Group: Serialization
|
|
|
|
|
|
|
|
// Function: removeUnusedDefElems
|
|
|
|
// Looks at DOM elements inside the <defs> to see if they are referred to,
|
|
|
|
// removes them from the DOM if they are not.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// The amount of elements that were removed
|
|
|
|
var removeUnusedDefElems = this.removeUnusedDefElems = function() {
|
2014-04-08 03:21:22 +00:00
|
|
|
var defs = svgcontent.getElementsByTagNameNS(NS.SVG, 'defs');
|
2014-02-17 06:48:40 +00:00
|
|
|
if (!defs || !defs.length) {return 0;}
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2014-02-17 06:48:40 +00:00
|
|
|
// if (!defs.firstChild) {return;}
|
2011-02-02 21:26:41 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
var defelem_uses = [],
|
|
|
|
numRemoved = 0;
|
|
|
|
var attrs = ['fill', 'stroke', 'filter', 'marker-start', 'marker-mid', 'marker-end'];
|
|
|
|
var alen = attrs.length;
|
|
|
|
|
2013-02-16 15:02:26 +00:00
|
|
|
var all_els = svgcontent.getElementsByTagNameNS(NS.SVG, '*');
|
2010-06-22 14:52:51 +00:00
|
|
|
var all_len = all_els.length;
|
|
|
|
|
2014-02-17 06:48:40 +00:00
|
|
|
var i, j;
|
|
|
|
for (i = 0; i < all_len; i++) {
|
2010-06-22 14:52:51 +00:00
|
|
|
var el = all_els[i];
|
2014-02-17 06:48:40 +00:00
|
|
|
for (j = 0; j < alen; j++) {
|
2013-02-14 15:19:46 +00:00
|
|
|
var ref = svgedit.utilities.getUrlFromAttr(el.getAttribute(attrs[j]));
|
2013-02-15 15:51:58 +00:00
|
|
|
if (ref) {
|
2011-02-02 21:26:41 +00:00
|
|
|
defelem_uses.push(ref.substr(1));
|
|
|
|
}
|
2009-12-30 18:06:29 +00:00
|
|
|
}
|
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// gradients can refer to other gradients
|
2010-08-16 17:53:15 +00:00
|
|
|
var href = getHref(el);
|
2010-10-12 15:20:17 +00:00
|
|
|
if (href && href.indexOf('#') === 0) {
|
2010-06-22 14:52:51 +00:00
|
|
|
defelem_uses.push(href.substr(1));
|
2009-12-30 18:06:29 +00:00
|
|
|
}
|
2013-02-15 15:51:58 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
var defelems = $(defs).find('linearGradient, radialGradient, filter, marker, svg, symbol');
|
2014-02-17 06:48:40 +00:00
|
|
|
i = defelems.length;
|
2010-06-22 14:52:51 +00:00
|
|
|
while (i--) {
|
|
|
|
var defelem = defelems[i];
|
|
|
|
var id = defelem.id;
|
2013-02-15 15:51:58 +00:00
|
|
|
if (defelem_uses.indexOf(id) < 0) {
|
2010-10-08 16:34:24 +00:00
|
|
|
// Not found, so remove (but remember)
|
|
|
|
removedElements[id] = defelem;
|
2010-06-22 14:52:51 +00:00
|
|
|
defelem.parentNode.removeChild(defelem);
|
|
|
|
numRemoved++;
|
|
|
|
}
|
|
|
|
}
|
2011-02-02 21:26:41 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
return numRemoved;
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
// Function: svgCanvasToString
|
|
|
|
// Main function to set up the SVG content for output
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// String containing the SVG image for output
|
2010-11-07 05:20:03 +00:00
|
|
|
this.svgCanvasToString = function() {
|
2010-06-22 14:52:51 +00:00
|
|
|
// keep calling it until there are none to remove
|
2013-02-15 15:51:58 +00:00
|
|
|
while (removeUnusedDefElems() > 0) {}
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
pathActions.clear(true);
|
|
|
|
|
|
|
|
// Keep SVG-Edit comment on top
|
|
|
|
$.each(svgcontent.childNodes, function(i, node) {
|
2013-02-15 15:51:58 +00:00
|
|
|
if (i && node.nodeType === 8 && node.data.indexOf('Created with') >= 0) {
|
2010-06-22 14:52:51 +00:00
|
|
|
svgcontent.insertBefore(node, svgcontent.firstChild);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2010-09-22 18:42:24 +00:00
|
|
|
// Move out of in-group editing mode
|
2013-02-15 15:51:58 +00:00
|
|
|
if (current_group) {
|
2010-09-23 19:53:43 +00:00
|
|
|
leaveContext();
|
2010-09-22 18:42:24 +00:00
|
|
|
selectOnly([current_group]);
|
|
|
|
}
|
|
|
|
|
2010-07-16 15:46:54 +00:00
|
|
|
var naked_svgs = [];
|
|
|
|
|
|
|
|
// Unwrap gsvg if it has no special attributes (only id and style)
|
|
|
|
$(svgcontent).find('g:data(gsvg)').each(function() {
|
|
|
|
var attrs = this.attributes;
|
|
|
|
var len = attrs.length;
|
2014-02-17 06:48:40 +00:00
|
|
|
var i;
|
|
|
|
for (i = 0; i < len; i++) {
|
2013-02-15 15:51:58 +00:00
|
|
|
if (attrs[i].nodeName == 'id' || attrs[i].nodeName == 'style') {
|
2010-07-16 15:46:54 +00:00
|
|
|
len--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// No significant attributes, so ungroup
|
2013-02-15 15:51:58 +00:00
|
|
|
if (len <= 0) {
|
2010-07-16 15:46:54 +00:00
|
|
|
var svg = this.firstChild;
|
|
|
|
naked_svgs.push(svg);
|
|
|
|
$(this).replaceWith(svg);
|
|
|
|
}
|
|
|
|
});
|
2010-11-07 05:20:03 +00:00
|
|
|
var output = this.svgToString(svgcontent, 0);
|
2010-07-16 15:46:54 +00:00
|
|
|
|
|
|
|
// Rewrap gsvg
|
2013-02-15 15:51:58 +00:00
|
|
|
if (naked_svgs.length) {
|
2010-07-16 15:46:54 +00:00
|
|
|
$(naked_svgs).each(function() {
|
|
|
|
groupSvgElem(this);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
return output;
|
2010-11-07 05:20:03 +00:00
|
|
|
};
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
// Function: svgToString
|
|
|
|
// Sub function ran on each SVG element to convert it to a string as desired
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// elem - The SVG element to convert
|
|
|
|
// indent - Integer with the amount of spaces to indent this tag
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// String with the given element as an SVG tag
|
2010-11-07 05:20:03 +00:00
|
|
|
this.svgToString = function(elem, indent) {
|
2013-02-15 15:51:58 +00:00
|
|
|
var out = [],
|
|
|
|
toXml = svgedit.utilities.toXml;
|
2010-11-05 17:00:32 +00:00
|
|
|
var unit = curConfig.baseUnit;
|
2010-10-05 17:13:00 +00:00
|
|
|
var unit_re = new RegExp('^-?[\\d\\.]+' + unit + '$');
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
if (elem) {
|
|
|
|
cleanupElement(elem);
|
|
|
|
var attrs = elem.attributes,
|
|
|
|
attr,
|
|
|
|
i,
|
|
|
|
childs = elem.childNodes;
|
|
|
|
|
2014-02-17 06:48:40 +00:00
|
|
|
for (i = 0; i < indent; i++) {out.push(' ');}
|
2014-04-08 03:21:22 +00:00
|
|
|
out.push('<'); out.push(elem.nodeName);
|
2013-02-15 15:51:58 +00:00
|
|
|
if (elem.id === 'svgcontent') {
|
2010-06-22 14:52:51 +00:00
|
|
|
// Process root element separately
|
2010-06-30 18:27:36 +00:00
|
|
|
var res = getResolution();
|
2010-10-19 17:20:28 +00:00
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
var vb = '';
|
2010-10-19 17:20:28 +00:00
|
|
|
// TODO: Allow this by dividing all values by current baseVal
|
|
|
|
// Note that this also means we should properly deal with this on import
|
2014-04-08 03:21:22 +00:00
|
|
|
// if (curConfig.baseUnit !== 'px') {
|
2014-01-31 00:39:35 +00:00
|
|
|
// var unit = curConfig.baseUnit;
|
|
|
|
// var unit_m = svgedit.units.getTypeMap()[unit];
|
|
|
|
// res.w = svgedit.units.shortFloat(res.w / unit_m)
|
|
|
|
// res.h = svgedit.units.shortFloat(res.h / unit_m)
|
|
|
|
// vb = ' viewBox="' + [0, 0, res.w, res.h].join(' ') + '"';
|
|
|
|
// res.w += unit;
|
|
|
|
// res.h += unit;
|
|
|
|
// }
|
2010-10-19 17:20:28 +00:00
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
if (unit !== 'px') {
|
2011-02-02 17:28:43 +00:00
|
|
|
res.w = svgedit.units.convertUnit(res.w, unit) + unit;
|
|
|
|
res.h = svgedit.units.convertUnit(res.h, unit) + unit;
|
2010-10-27 18:15:28 +00:00
|
|
|
}
|
|
|
|
|
2013-02-16 15:02:26 +00:00
|
|
|
out.push(' width="' + res.w + '" height="' + res.h + '"' + vb + ' xmlns="'+NS.SVG+'"');
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
var nsuris = {};
|
|
|
|
|
|
|
|
// Check elements for namespaces, add if found
|
|
|
|
$(elem).find('*').andSelf().each(function() {
|
|
|
|
var el = this;
|
2013-10-14 01:50:42 +00:00
|
|
|
// for some elements have no attribute
|
|
|
|
var uri = this.namespaceURI;
|
|
|
|
if(uri && !nsuris[uri] && nsMap[uri] && nsMap[uri] !== 'xmlns' && nsMap[uri] !== 'xml' ) {
|
2014-01-31 10:40:52 +00:00
|
|
|
nsuris[uri] = true;
|
2014-04-08 03:21:22 +00:00
|
|
|
out.push(' xmlns:' + nsMap[uri] + '="' + uri +'"');
|
2013-10-14 01:50:42 +00:00
|
|
|
}
|
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
$.each(this.attributes, function(i, attr) {
|
|
|
|
var uri = attr.namespaceURI;
|
2013-02-15 15:51:58 +00:00
|
|
|
if (uri && !nsuris[uri] && nsMap[uri] !== 'xmlns' && nsMap[uri] !== 'xml' ) {
|
2010-06-22 14:52:51 +00:00
|
|
|
nsuris[uri] = true;
|
2014-04-08 03:21:22 +00:00
|
|
|
out.push(' xmlns:' + nsMap[uri] + '="' + uri +'"');
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2009-10-16 20:07:15 +00:00
|
|
|
|
2014-02-17 06:48:40 +00:00
|
|
|
i = attrs.length;
|
2013-02-15 16:51:48 +00:00
|
|
|
var attr_names = ['width', 'height', 'xmlns', 'x', 'y', 'viewBox', 'id', 'overflow'];
|
2010-06-22 14:52:51 +00:00
|
|
|
while (i--) {
|
|
|
|
attr = attrs.item(i);
|
|
|
|
var attrVal = toXml(attr.nodeValue);
|
|
|
|
|
|
|
|
// Namespaces have already been dealt with, so skip
|
2014-02-17 06:48:40 +00:00
|
|
|
if (attr.nodeName.indexOf('xmlns:') === 0) {continue;}
|
2009-12-30 18:06:29 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// only serialize attributes we don't use internally
|
2014-04-08 03:21:22 +00:00
|
|
|
if (attrVal != '' && attr_names.indexOf(attr.localName) == -1) {
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!attr.namespaceURI || nsMap[attr.namespaceURI]) {
|
2010-06-22 14:52:51 +00:00
|
|
|
out.push(' ');
|
2014-04-08 03:21:22 +00:00
|
|
|
out.push(attr.nodeName); out.push('="');
|
|
|
|
out.push(attrVal); out.push('"');
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-12-30 18:06:29 +00:00
|
|
|
} else {
|
2011-02-02 21:26:41 +00:00
|
|
|
// Skip empty defs
|
2014-02-17 06:48:40 +00:00
|
|
|
if (elem.nodeName === 'defs' && !elem.firstChild) {return;}
|
2011-02-02 21:26:41 +00:00
|
|
|
|
2010-10-05 18:50:21 +00:00
|
|
|
var moz_attrs = ['-moz-math-font-style', '_moz-math-font-style'];
|
2014-02-17 06:48:40 +00:00
|
|
|
for (i = attrs.length - 1; i >= 0; i--) {
|
2010-06-22 14:52:51 +00:00
|
|
|
attr = attrs.item(i);
|
|
|
|
var attrVal = toXml(attr.nodeValue);
|
|
|
|
//remove bogus attributes added by Gecko
|
2014-02-17 06:48:40 +00:00
|
|
|
if (moz_attrs.indexOf(attr.localName) >= 0) {continue;}
|
2014-04-08 03:21:22 +00:00
|
|
|
if (attrVal != '') {
|
2014-02-17 06:48:40 +00:00
|
|
|
if (attrVal.indexOf('pointer-events') === 0) {continue;}
|
2014-04-08 03:21:22 +00:00
|
|
|
if (attr.localName === 'class' && attrVal.indexOf('se_') === 0) {continue;}
|
|
|
|
out.push(' ');
|
2014-02-17 06:48:40 +00:00
|
|
|
if (attr.localName === 'd') {attrVal = pathActions.convertPath(elem, true);}
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!isNaN(attrVal)) {
|
2011-02-02 17:28:43 +00:00
|
|
|
attrVal = svgedit.units.shortFloat(attrVal);
|
2013-02-15 15:51:58 +00:00
|
|
|
} else if (unit_re.test(attrVal)) {
|
2011-02-02 17:28:43 +00:00
|
|
|
attrVal = svgedit.units.shortFloat(attrVal) + unit;
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Embed images when saving
|
2013-02-15 15:51:58 +00:00
|
|
|
if (save_options.apply
|
2010-09-22 18:42:24 +00:00
|
|
|
&& elem.nodeName === 'image'
|
|
|
|
&& attr.localName === 'href'
|
2010-06-22 14:52:51 +00:00
|
|
|
&& save_options.images
|
2010-09-22 18:42:24 +00:00
|
|
|
&& save_options.images === 'embed')
|
2010-06-22 14:52:51 +00:00
|
|
|
{
|
|
|
|
var img = encodableImages[attrVal];
|
2014-02-17 06:48:40 +00:00
|
|
|
if (img) {attrVal = img;}
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// map various namespaces to our fixed namespace prefixes
|
|
|
|
// (the default xmlns attribute itself does not get a prefix)
|
2013-02-16 15:02:26 +00:00
|
|
|
if (!attr.namespaceURI || attr.namespaceURI == NS.SVG || nsMap[attr.namespaceURI]) {
|
2014-04-08 03:21:22 +00:00
|
|
|
out.push(attr.nodeName); out.push('="');
|
|
|
|
out.push(attrVal); out.push('"');
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
}
|
2009-10-09 15:29:40 +00:00
|
|
|
}
|
2009-06-01 21:24:30 +00:00
|
|
|
}
|
2009-12-17 15:12:54 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
if (elem.hasChildNodes()) {
|
2014-04-08 03:21:22 +00:00
|
|
|
out.push('>');
|
2010-06-22 14:52:51 +00:00
|
|
|
indent++;
|
|
|
|
var bOneLine = false;
|
2010-07-16 15:46:54 +00:00
|
|
|
|
2014-02-17 06:48:40 +00:00
|
|
|
for (i = 0; i < childs.length; i++) {
|
2010-06-22 14:52:51 +00:00
|
|
|
var child = childs.item(i);
|
|
|
|
switch(child.nodeType) {
|
|
|
|
case 1: // element node
|
2014-04-08 03:21:22 +00:00
|
|
|
out.push('\n');
|
2010-11-07 05:20:03 +00:00
|
|
|
out.push(this.svgToString(childs.item(i), indent));
|
2010-06-22 14:52:51 +00:00
|
|
|
break;
|
|
|
|
case 3: // text node
|
2014-04-08 03:21:22 +00:00
|
|
|
var str = child.nodeValue.replace(/^\s+|\s+$/g, '');
|
|
|
|
if (str != '') {
|
2010-06-22 14:52:51 +00:00
|
|
|
bOneLine = true;
|
2014-02-17 06:48:40 +00:00
|
|
|
out.push(String(toXml(str)));
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
break;
|
2012-02-27 17:24:03 +00:00
|
|
|
case 4: // cdata node
|
2014-04-08 03:21:22 +00:00
|
|
|
out.push('\n');
|
|
|
|
out.push(new Array(indent+1).join(' '));
|
|
|
|
out.push('<![CDATA[');
|
2012-02-27 17:24:03 +00:00
|
|
|
out.push(child.nodeValue);
|
2014-04-08 03:21:22 +00:00
|
|
|
out.push(']]>');
|
2012-02-27 17:24:03 +00:00
|
|
|
break;
|
2010-06-22 14:52:51 +00:00
|
|
|
case 8: // comment
|
2014-04-08 03:21:22 +00:00
|
|
|
out.push('\n');
|
|
|
|
out.push(new Array(indent+1).join(' '));
|
|
|
|
out.push('<!--');
|
2010-06-22 14:52:51 +00:00
|
|
|
out.push(child.data);
|
2014-04-08 03:21:22 +00:00
|
|
|
out.push('-->');
|
2010-06-22 14:52:51 +00:00
|
|
|
break;
|
|
|
|
} // switch on node type
|
|
|
|
}
|
|
|
|
indent--;
|
|
|
|
if (!bOneLine) {
|
2014-04-08 03:21:22 +00:00
|
|
|
out.push('\n');
|
2014-02-17 06:48:40 +00:00
|
|
|
for (i = 0; i < indent; i++) {out.push(' ');}
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
2014-04-08 03:21:22 +00:00
|
|
|
out.push('</'); out.push(elem.nodeName); out.push('>');
|
2010-06-22 14:52:51 +00:00
|
|
|
} else {
|
2014-04-08 03:21:22 +00:00
|
|
|
out.push('/>');
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return out.join('');
|
|
|
|
}; // end svgToString()
|
2009-12-30 18:06:29 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// Function: embedImage
|
|
|
|
// Converts a given image file to a data URL when possible, then runs a given callback
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// val - String with the path/URL of the image
|
|
|
|
// callback - Optional function to run when image data is found, supplies the
|
|
|
|
// result (data URL or false) as first parameter.
|
|
|
|
this.embedImage = function(val, callback) {
|
|
|
|
// load in the image and once it's loaded, get the dimensions
|
|
|
|
$(new Image()).load(function() {
|
|
|
|
// create a canvas the same size as the raster image
|
2014-04-08 03:21:22 +00:00
|
|
|
var canvas = document.createElement('canvas');
|
2010-06-22 14:52:51 +00:00
|
|
|
canvas.width = this.width;
|
|
|
|
canvas.height = this.height;
|
|
|
|
// load the raster image into the canvas
|
2014-04-08 03:21:22 +00:00
|
|
|
canvas.getContext('2d').drawImage(this, 0, 0);
|
2010-06-22 14:52:51 +00:00
|
|
|
// retrieve the data: URL
|
|
|
|
try {
|
|
|
|
var urldata = ';svgedit_url=' + encodeURIComponent(val);
|
2013-02-15 16:51:48 +00:00
|
|
|
urldata = canvas.toDataURL().replace(';base64', urldata+';base64');
|
2010-06-22 14:52:51 +00:00
|
|
|
encodableImages[val] = urldata;
|
|
|
|
} catch(e) {
|
|
|
|
encodableImages[val] = false;
|
|
|
|
}
|
|
|
|
last_good_img_url = val;
|
2014-02-17 06:48:40 +00:00
|
|
|
if (callback) {callback(encodableImages[val]);}
|
2013-02-15 16:51:48 +00:00
|
|
|
}).attr('src', val);
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2010-07-05 15:38:06 +00:00
|
|
|
// Function: setGoodImage
|
|
|
|
// Sets a given URL to be a "last good image" URL
|
|
|
|
this.setGoodImage = function(val) {
|
|
|
|
last_good_img_url = val;
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
this.open = function() {
|
|
|
|
// Nothing by default, handled by optional widget/extension
|
|
|
|
};
|
2009-10-13 02:48:27 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// Function: save
|
|
|
|
// Serializes the current drawing into SVG XML text and returns it to the 'saved' handler.
|
2014-01-31 10:40:52 +00:00
|
|
|
// This function also includes the XML prolog. Clients of the SvgCanvas bind their save
|
2010-06-22 14:52:51 +00:00
|
|
|
// function to the 'saved' event.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// Nothing
|
|
|
|
this.save = function(opts) {
|
|
|
|
// remove the selected outline before serializing
|
2010-06-30 18:27:36 +00:00
|
|
|
clearSelection();
|
2010-06-22 14:52:51 +00:00
|
|
|
// Update save options if provided
|
2014-02-17 06:48:40 +00:00
|
|
|
if (opts) {$.extend(save_options, opts);}
|
2010-06-22 14:52:51 +00:00
|
|
|
save_options.apply = true;
|
|
|
|
|
|
|
|
// no need for doctype, see http://jwatt.org/svg/authoring/#doctype-declaration
|
2010-11-07 05:20:03 +00:00
|
|
|
var str = this.svgCanvasToString();
|
2014-04-08 03:21:22 +00:00
|
|
|
call('saved', str);
|
2010-06-22 14:52:51 +00:00
|
|
|
};
|
2009-08-19 03:42:27 +00:00
|
|
|
|
2010-06-22 18:17:42 +00:00
|
|
|
// Function: rasterExport
|
Support PNG, JPEG, BMP, WEBP export formats by change of "Export to PNG" menu item into "Export" with its own pull-down dialog (and a HTML5 range type input element for "quality" selection for JPEG or WEBP files), including changes for the server_opensave extension; change locale key from "export_png" to "export_img" and change the corresponding localized strings (currently CS, DE, ES, FR, IT, NL, PT-BR, RO, SK, all reset to English to avoid translation errors); within the setCustomHandlers() API, deprecate "pngsave" in favor of "exportImage" (chosen to avoid just using the simpler but reserved JS keyword "export") including use within the server_opensave extension; a few JSLint-friendly changes
git-svn-id: http://svg-edit.googlecode.com/svn/trunk@2602 eee81c28-f429-11dd-99c0-75d572ba1ddd
2013-10-28 03:53:30 +00:00
|
|
|
// Generates a Data URL based on the current image, then calls "exported"
|
|
|
|
// with an object including the string, image information, and any issues found
|
|
|
|
this.rasterExport = function(imgType, quality) {
|
2014-01-31 10:40:52 +00:00
|
|
|
var mimeType = 'image/' + imgType.toLowerCase();
|
Support PNG, JPEG, BMP, WEBP export formats by change of "Export to PNG" menu item into "Export" with its own pull-down dialog (and a HTML5 range type input element for "quality" selection for JPEG or WEBP files), including changes for the server_opensave extension; change locale key from "export_png" to "export_img" and change the corresponding localized strings (currently CS, DE, ES, FR, IT, NL, PT-BR, RO, SK, all reset to English to avoid translation errors); within the setCustomHandlers() API, deprecate "pngsave" in favor of "exportImage" (chosen to avoid just using the simpler but reserved JS keyword "export") including use within the server_opensave extension; a few JSLint-friendly changes
git-svn-id: http://svg-edit.googlecode.com/svn/trunk@2602 eee81c28-f429-11dd-99c0-75d572ba1ddd
2013-10-28 03:53:30 +00:00
|
|
|
|
2014-01-31 10:40:52 +00:00
|
|
|
// remove the selected outline before serializing
|
2010-06-30 18:27:36 +00:00
|
|
|
clearSelection();
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
// Check for known CanVG issues
|
|
|
|
var issues = [];
|
|
|
|
|
|
|
|
// Selector and notice
|
|
|
|
var issue_list = {
|
|
|
|
'feGaussianBlur': uiStrings.exportNoBlur,
|
|
|
|
'foreignObject': uiStrings.exportNoforeignObject,
|
|
|
|
'[stroke-dasharray]': uiStrings.exportNoDashArray
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2010-06-22 14:52:51 +00:00
|
|
|
var content = $(svgcontent);
|
|
|
|
|
|
|
|
// Add font/text check if Canvas Text API is not implemented
|
2014-04-08 03:21:22 +00:00
|
|
|
if (!('font' in $('<canvas>')[0].getContext('2d'))) {
|
2014-02-17 06:48:40 +00:00
|
|
|
issue_list.text = uiStrings.exportNoText;
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$.each(issue_list, function(sel, descr) {
|
2013-02-15 15:51:58 +00:00
|
|
|
if (content.find(sel).length) {
|
2010-06-22 14:52:51 +00:00
|
|
|
issues.push(descr);
|
2010-04-20 20:29:30 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
});
|
2010-04-14 17:30:25 +00:00
|
|
|
|
2010-11-07 05:20:03 +00:00
|
|
|
var str = this.svgCanvasToString();
|
2014-04-08 03:21:22 +00:00
|
|
|
call('exported', {svg: str, issues: issues, type: imgType, mimeType: mimeType, quality: quality});
|
2010-06-22 14:52:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Function: getSvgString
|
|
|
|
// Returns the current drawing as raw SVG XML text.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// The current drawing as raw SVG XML text.
|
|
|
|
this.getSvgString = function() {
|
|
|
|
save_options.apply = false;
|
2010-11-07 05:20:03 +00:00
|
|
|
return this.svgCanvasToString();
|
2010-06-22 14:52:51 +00:00
|
|
|
};
|
|
|
|
|
2011-02-01 07:22:18 +00:00
|
|
|
// Function: randomizeIds
|
|
|
|
// This function determines whether to use a nonce in the prefix, when
|
|
|
|
// generating IDs for future documents in SVG-Edit.
|
2010-06-22 14:52:51 +00:00
|
|
|
//
|
2014-01-31 10:40:52 +00:00
|
|
|
// Parameters:
|
|
|
|
// an optional boolean, which, if true, adds a nonce to the prefix. Thus
|
|
|
|
// svgCanvas.randomizeIds() <==> svgCanvas.randomizeIds(true)
|
2010-06-22 14:52:51 +00:00
|
|
|
//
|
|
|
|
// if you're controlling SVG-Edit externally, and want randomized IDs, call
|
|
|
|
// this BEFORE calling svgCanvas.setSvgString
|
|
|
|
//
|
2014-02-17 06:48:40 +00:00
|
|
|
this.randomizeIds = function(enableRandomization) {
|
|
|
|
if (arguments.length > 0 && enableRandomization == false) {
|
2011-02-03 06:45:01 +00:00
|
|
|
svgedit.draw.randomizeIds(false, getCurrentDrawing());
|
2011-02-01 07:22:18 +00:00
|
|
|
} else {
|
2011-02-03 06:45:01 +00:00
|
|
|
svgedit.draw.randomizeIds(true, getCurrentDrawing());
|
2011-02-01 07:22:18 +00:00
|
|
|
}
|
|
|
|
};
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2010-07-16 15:46:54 +00:00
|
|
|
// Function: uniquifyElems
|
|
|
|
// Ensure each element has a unique ID
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// g - The parent element of the tree to give unique IDs
|
|
|
|
var uniquifyElems = this.uniquifyElems = function(g) {
|
|
|
|
var ids = {};
|
2014-01-31 10:40:52 +00:00
|
|
|
// TODO: Handle markers and connectors. These are not yet re-identified properly
|
2011-01-12 06:07:28 +00:00
|
|
|
// as their referring elements do not get remapped.
|
|
|
|
//
|
|
|
|
// <marker id='se_marker_end_svg_7'/>
|
|
|
|
// <polyline id='svg_7' se:connector='svg_1 svg_6' marker-end='url(#se_marker_end_svg_7)'/>
|
|
|
|
//
|
|
|
|
// Problem #1: if svg_1 gets renamed, we do not update the polyline's se:connector attribute
|
|
|
|
// Problem #2: if the polyline svg_7 gets renamed, we do not update the marker id nor the polyline's marker-end attribute
|
2014-04-08 03:21:22 +00:00
|
|
|
var ref_elems = ['filter', 'linearGradient', 'pattern', 'radialGradient', 'symbol', 'textPath', 'use'];
|
2010-10-05 18:50:21 +00:00
|
|
|
|
2010-11-05 17:00:32 +00:00
|
|
|
svgedit.utilities.walkTree(g, function(n) {
|
2010-07-16 15:46:54 +00:00
|
|
|
// if it's an element node
|
|
|
|
if (n.nodeType == 1) {
|
|
|
|
// and the element has an ID
|
|
|
|
if (n.id) {
|
|
|
|
// and we haven't tracked this ID yet
|
|
|
|
if (!(n.id in ids)) {
|
|
|
|
// add this id to our map
|
|
|
|
ids[n.id] = {elem:null, attrs:[], hrefs:[]};
|
|
|
|
}
|
2014-02-17 06:48:40 +00:00
|
|
|
ids[n.id].elem = n;
|
2010-07-16 15:46:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// now search for all attributes on this element that might refer
|
|
|
|
// to other elements
|
2013-02-15 16:51:48 +00:00
|
|
|
$.each(ref_attrs, function(i, attr) {
|
2010-07-16 15:46:54 +00:00
|
|
|
var attrnode = n.getAttributeNode(attr);
|
|
|
|
if (attrnode) {
|
|
|
|
// the incoming file has been sanitized, so we should be able to safely just strip off the leading #
|
2011-01-12 07:34:07 +00:00
|
|
|
var url = svgedit.utilities.getUrlFromAttr(attrnode.value),
|
2010-07-16 15:46:54 +00:00
|
|
|
refid = url ? url.substr(1) : null;
|
|
|
|
if (refid) {
|
|
|
|
if (!(refid in ids)) {
|
|
|
|
// add this id to our map
|
|
|
|
ids[refid] = {elem:null, attrs:[], hrefs:[]};
|
|
|
|
}
|
2014-02-17 06:48:40 +00:00
|
|
|
ids[refid].attrs.push(attrnode);
|
2010-07-16 15:46:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// check xlink:href now
|
2011-01-16 16:28:26 +00:00
|
|
|
var href = svgedit.utilities.getHref(n);
|
2010-07-16 15:46:54 +00:00
|
|
|
// TODO: what if an <image> or <a> element refers to an element internally?
|
2013-02-15 15:51:58 +00:00
|
|
|
if (href && ref_elems.indexOf(n.nodeName) >= 0) {
|
2010-07-16 15:46:54 +00:00
|
|
|
var refid = href.substr(1);
|
2011-01-12 06:51:34 +00:00
|
|
|
if (refid) {
|
|
|
|
if (!(refid in ids)) {
|
|
|
|
// add this id to our map
|
|
|
|
ids[refid] = {elem:null, attrs:[], hrefs:[]};
|
|
|
|
}
|
2014-02-17 06:48:40 +00:00
|
|
|
ids[refid].hrefs.push(n);
|
2010-07-16 15:46:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// in ids, we now have a map of ids, elements and attributes, let's re-identify
|
2014-02-17 06:48:40 +00:00
|
|
|
var oldid;
|
|
|
|
for (oldid in ids) {
|
|
|
|
if (!oldid) {continue;}
|
|
|
|
var elem = ids[oldid].elem;
|
2010-07-16 15:46:54 +00:00
|
|
|
if (elem) {
|
|
|
|
var newid = getNextId();
|
|
|
|
|
|
|
|
// assign element its new id
|
|
|
|
elem.id = newid;
|
|
|
|
|
|
|
|
// remap all url() attributes
|
2014-02-17 06:48:40 +00:00
|
|
|
var attrs = ids[oldid].attrs;
|
2010-07-16 15:46:54 +00:00
|
|
|
var j = attrs.length;
|
|
|
|
while (j--) {
|
|
|
|
var attr = attrs[j];
|
2014-04-08 03:21:22 +00:00
|
|
|
attr.ownerElement.setAttribute(attr.name, 'url(#' + newid + ')');
|
2010-07-16 15:46:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// remap all href attributes
|
2014-02-17 06:48:40 +00:00
|
|
|
var hreffers = ids[oldid].hrefs;
|
2010-07-16 15:46:54 +00:00
|
|
|
var k = hreffers.length;
|
|
|
|
while (k--) {
|
|
|
|
var hreffer = hreffers[k];
|
2014-04-08 03:21:22 +00:00
|
|
|
svgedit.utilities.setHref(hreffer, '#' + newid);
|
2010-07-16 15:46:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-07-16 15:46:54 +00:00
|
|
|
|
2011-01-31 18:22:04 +00:00
|
|
|
// Function setUseData
|
|
|
|
// Assigns reference data for each use element
|
|
|
|
var setUseData = this.setUseData = function(parent) {
|
2011-02-02 18:24:44 +00:00
|
|
|
var elems = $(parent);
|
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (parent.tagName !== 'use') {
|
2011-02-02 18:24:44 +00:00
|
|
|
elems = elems.find('use');
|
|
|
|
}
|
|
|
|
|
|
|
|
elems.each(function() {
|
2011-01-31 18:22:04 +00:00
|
|
|
var id = getHref(this).substr(1);
|
2013-02-14 15:19:46 +00:00
|
|
|
var ref_elem = svgedit.utilities.getElem(id);
|
2014-02-17 06:48:40 +00:00
|
|
|
if (!ref_elem) {return;}
|
2011-01-31 18:22:04 +00:00
|
|
|
$(this).data('ref', ref_elem);
|
2013-02-15 15:51:58 +00:00
|
|
|
if (ref_elem.tagName == 'symbol' || ref_elem.tagName == 'svg') {
|
2011-02-10 18:10:05 +00:00
|
|
|
$(this).data('symbol', ref_elem).data('ref', ref_elem);
|
2011-01-31 18:22:04 +00:00
|
|
|
}
|
|
|
|
});
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2011-01-31 18:22:04 +00:00
|
|
|
|
2010-09-28 18:59:31 +00:00
|
|
|
// Function convertGradients
|
|
|
|
// Converts gradients from userSpaceOnUse to objectBoundingBox
|
|
|
|
var convertGradients = this.convertGradients = function(elem) {
|
2010-09-28 20:21:22 +00:00
|
|
|
var elems = $(elem).find('linearGradient, radialGradient');
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!elems.length && svgedit.browser.isWebkit()) {
|
2010-09-28 20:21:22 +00:00
|
|
|
// Bug in webkit prevents regular *Gradient selector search
|
|
|
|
elems = $(elem).find('*').filter(function() {
|
2010-09-29 18:37:51 +00:00
|
|
|
return (this.tagName.indexOf('Gradient') >= 0);
|
2010-09-28 20:21:22 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
elems.each(function() {
|
2010-09-28 18:59:31 +00:00
|
|
|
var grad = this;
|
2013-02-15 15:51:58 +00:00
|
|
|
if ($(grad).attr('gradientUnits') === 'userSpaceOnUse') {
|
2010-09-28 18:59:31 +00:00
|
|
|
// TODO: Support more than one element with this ref by duplicating parent grad
|
2012-05-23 10:33:22 +00:00
|
|
|
var elems = $(svgcontent).find('[fill="url(#' + grad.id + ')"],[stroke="url(#' + grad.id + ')"]');
|
2014-02-17 06:48:40 +00:00
|
|
|
if (!elems.length) {return;}
|
2010-09-28 18:59:31 +00:00
|
|
|
|
|
|
|
// get object's bounding box
|
2011-02-24 16:13:26 +00:00
|
|
|
var bb = svgedit.utilities.getBBox(elems[0]);
|
2010-09-28 18:59:31 +00:00
|
|
|
|
2010-10-13 14:33:20 +00:00
|
|
|
// This will occur if the element is inside a <defs> or a <symbol>,
|
|
|
|
// in which we shouldn't need to convert anyway.
|
2014-02-17 06:48:40 +00:00
|
|
|
if (!bb) {return;}
|
2010-10-13 14:33:20 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (grad.tagName === 'linearGradient') {
|
2010-09-28 18:59:31 +00:00
|
|
|
var g_coords = $(grad).attr(['x1', 'y1', 'x2', 'y2']);
|
|
|
|
|
2010-10-13 14:05:20 +00:00
|
|
|
// If has transform, convert
|
|
|
|
var tlist = grad.gradientTransform.baseVal;
|
2013-02-15 15:51:58 +00:00
|
|
|
if (tlist && tlist.numberOfItems > 0) {
|
2013-02-14 15:19:46 +00:00
|
|
|
var m = svgedit.math.transformListToTransform(tlist).matrix;
|
|
|
|
var pt1 = svgedit.math.transformPoint(g_coords.x1, g_coords.y1, m);
|
|
|
|
var pt2 = svgedit.math.transformPoint(g_coords.x2, g_coords.y2, m);
|
2010-10-13 14:05:20 +00:00
|
|
|
|
|
|
|
g_coords.x1 = pt1.x;
|
|
|
|
g_coords.y1 = pt1.y;
|
|
|
|
g_coords.x2 = pt2.x;
|
|
|
|
g_coords.y2 = pt2.y;
|
|
|
|
grad.removeAttribute('gradientTransform');
|
|
|
|
}
|
|
|
|
|
2010-09-28 18:59:31 +00:00
|
|
|
$(grad).attr({
|
|
|
|
x1: (g_coords.x1 - bb.x) / bb.width,
|
|
|
|
y1: (g_coords.y1 - bb.y) / bb.height,
|
|
|
|
x2: (g_coords.x2 - bb.x) / bb.width,
|
|
|
|
y2: (g_coords.y2 - bb.y) / bb.height
|
|
|
|
});
|
|
|
|
grad.removeAttribute('gradientUnits');
|
2014-02-17 06:48:40 +00:00
|
|
|
}
|
|
|
|
// else {
|
2010-09-28 18:59:31 +00:00
|
|
|
// Note: radialGradient elements cannot be easily converted
|
|
|
|
// because userSpaceOnUse will keep circular gradients, while
|
|
|
|
// objectBoundingBox will x/y scale the gradient according to
|
|
|
|
// its bbox.
|
|
|
|
|
|
|
|
// For now we'll do nothing, though we should probably have
|
|
|
|
// the gradient be updated as the element is moved, as
|
|
|
|
// inkscape/illustrator do.
|
|
|
|
|
2014-01-31 00:39:35 +00:00
|
|
|
// var g_coords = $(grad).attr(['cx', 'cy', 'r']);
|
|
|
|
//
|
|
|
|
// $(grad).attr({
|
|
|
|
// cx: (g_coords.cx - bb.x) / bb.width,
|
|
|
|
// cy: (g_coords.cy - bb.y) / bb.height,
|
|
|
|
// r: g_coords.r
|
|
|
|
// });
|
|
|
|
//
|
|
|
|
// grad.removeAttribute('gradientUnits');
|
2014-02-17 06:48:40 +00:00
|
|
|
// }
|
2010-09-28 18:59:31 +00:00
|
|
|
}
|
|
|
|
});
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-09-28 18:59:31 +00:00
|
|
|
|
2010-07-29 15:09:49 +00:00
|
|
|
// Function: convertToGroup
|
|
|
|
// Converts selected/given <use> or child SVG element to a group
|
|
|
|
var convertToGroup = this.convertToGroup = function(elem) {
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!elem) {
|
2010-07-22 19:10:51 +00:00
|
|
|
elem = selectedElements[0];
|
|
|
|
}
|
2010-07-29 15:09:49 +00:00
|
|
|
var $elem = $(elem);
|
2013-02-17 08:21:07 +00:00
|
|
|
var batchCmd = new svgedit.history.BatchCommand();
|
2010-07-29 15:09:49 +00:00
|
|
|
var ts;
|
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if ($elem.data('gsvg')) {
|
2010-07-22 19:10:51 +00:00
|
|
|
// Use the gsvg as the new group
|
|
|
|
var svg = elem.firstChild;
|
|
|
|
var pt = $(svg).attr(['x', 'y']);
|
|
|
|
|
|
|
|
$(elem.firstChild.firstChild).unwrap();
|
|
|
|
$(elem).removeData('gsvg');
|
|
|
|
|
2013-02-14 15:19:46 +00:00
|
|
|
var tlist = svgedit.transformlist.getTransformList(elem);
|
2010-07-22 19:10:51 +00:00
|
|
|
var xform = svgroot.createSVGTransform();
|
|
|
|
xform.setTranslate(pt.x, pt.y);
|
|
|
|
tlist.appendItem(xform);
|
2013-02-20 06:29:25 +00:00
|
|
|
svgedit.recalculate.recalculateDimensions(elem);
|
2014-04-08 03:21:22 +00:00
|
|
|
call('selected', [elem]);
|
2013-02-15 15:51:58 +00:00
|
|
|
} else if ($elem.data('symbol')) {
|
2010-07-29 15:09:49 +00:00
|
|
|
elem = $elem.data('symbol');
|
|
|
|
|
|
|
|
ts = $elem.attr('transform');
|
2013-02-15 16:51:48 +00:00
|
|
|
var pos = $elem.attr(['x', 'y']);
|
2010-10-04 16:01:58 +00:00
|
|
|
|
2011-01-31 18:22:04 +00:00
|
|
|
var vb = elem.getAttribute('viewBox');
|
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (vb) {
|
2011-01-31 18:22:04 +00:00
|
|
|
var nums = vb.split(' ');
|
|
|
|
pos.x -= +nums[0];
|
|
|
|
pos.y -= +nums[1];
|
|
|
|
}
|
|
|
|
|
2010-07-29 15:09:49 +00:00
|
|
|
// Not ideal, but works
|
2014-04-08 03:21:22 +00:00
|
|
|
ts += ' translate(' + (pos.x || 0) + ',' + (pos.y || 0) + ')';
|
2010-07-29 15:09:49 +00:00
|
|
|
|
|
|
|
var prev = $elem.prev();
|
|
|
|
|
|
|
|
// Remove <use> element
|
2013-02-17 08:21:07 +00:00
|
|
|
batchCmd.addSubCommand(new svgedit.history.RemoveElementCommand($elem[0], $elem[0].nextSibling, $elem[0].parentNode));
|
2010-07-29 15:09:49 +00:00
|
|
|
$elem.remove();
|
|
|
|
|
|
|
|
// See if other elements reference this symbol
|
|
|
|
var has_more = $(svgcontent).find('use:data(symbol)').length;
|
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
var g = svgdoc.createElementNS(NS.SVG, 'g');
|
2010-07-29 17:49:28 +00:00
|
|
|
var childs = elem.childNodes;
|
2010-07-29 15:09:49 +00:00
|
|
|
|
2014-02-17 06:48:40 +00:00
|
|
|
var i;
|
|
|
|
for (i = 0; i < childs.length; i++) {
|
2010-07-29 15:09:49 +00:00
|
|
|
g.appendChild(childs[i].cloneNode(true));
|
|
|
|
}
|
|
|
|
|
2010-12-20 20:00:17 +00:00
|
|
|
// Duplicate the gradients for Gecko, since they weren't included in the <symbol>
|
2013-02-15 15:51:58 +00:00
|
|
|
if (svgedit.browser.isGecko()) {
|
2013-02-14 15:19:46 +00:00
|
|
|
var dupeGrads = $(svgedit.utilities.findDefs()).children('linearGradient,radialGradient,pattern').clone();
|
2010-12-20 20:00:17 +00:00
|
|
|
$(g).append(dupeGrads);
|
|
|
|
}
|
|
|
|
|
2011-01-31 18:22:04 +00:00
|
|
|
if (ts) {
|
2014-04-08 03:21:22 +00:00
|
|
|
g.setAttribute('transform', ts);
|
2011-01-31 18:22:04 +00:00
|
|
|
}
|
2010-07-29 15:09:49 +00:00
|
|
|
|
|
|
|
var parent = elem.parentNode;
|
|
|
|
|
|
|
|
uniquifyElems(g);
|
2010-12-20 20:00:17 +00:00
|
|
|
|
|
|
|
// Put the dupe gradients back into <defs> (after uniquifying them)
|
2013-02-15 15:51:58 +00:00
|
|
|
if (svgedit.browser.isGecko()) {
|
2010-12-20 20:00:17 +00:00
|
|
|
$(findDefs()).append( $(g).find('linearGradient,radialGradient,pattern') );
|
|
|
|
}
|
2010-07-22 19:10:51 +00:00
|
|
|
|
2010-07-29 15:09:49 +00:00
|
|
|
// now give the g itself a new id
|
|
|
|
g.id = getNextId();
|
|
|
|
|
|
|
|
prev.after(g);
|
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (parent) {
|
|
|
|
if (!has_more) {
|
2010-07-29 15:09:49 +00:00
|
|
|
// remove symbol/svg element
|
2010-11-13 09:58:51 +00:00
|
|
|
var nextSibling = elem.nextSibling;
|
2010-07-29 15:09:49 +00:00
|
|
|
parent.removeChild(elem);
|
2013-02-17 08:21:07 +00:00
|
|
|
batchCmd.addSubCommand(new svgedit.history.RemoveElementCommand(elem, nextSibling, parent));
|
2010-07-29 15:09:49 +00:00
|
|
|
}
|
2013-02-17 08:21:07 +00:00
|
|
|
batchCmd.addSubCommand(new svgedit.history.InsertElementCommand(g));
|
2010-07-29 15:09:49 +00:00
|
|
|
}
|
2010-09-28 20:21:22 +00:00
|
|
|
|
2011-01-31 18:22:04 +00:00
|
|
|
setUseData(g);
|
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (svgedit.browser.isGecko()) {
|
2013-02-14 15:19:46 +00:00
|
|
|
convertGradients(svgedit.utilities.findDefs());
|
2010-12-21 19:36:51 +00:00
|
|
|
} else {
|
|
|
|
convertGradients(g);
|
|
|
|
}
|
2010-09-28 18:59:31 +00:00
|
|
|
|
2010-07-29 15:09:49 +00:00
|
|
|
// recalculate dimensions on the top-level children so that unnecessary transforms
|
|
|
|
// are removed
|
2013-02-15 15:51:58 +00:00
|
|
|
svgedit.utilities.walkTreePost(g, function(n){
|
|
|
|
try {
|
2013-02-20 06:29:25 +00:00
|
|
|
svgedit.recalculate.recalculateDimensions(n);
|
2013-02-15 15:51:58 +00:00
|
|
|
} catch(e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
});
|
2010-07-29 15:09:49 +00:00
|
|
|
|
2010-08-23 15:33:00 +00:00
|
|
|
// Give ID for any visible element missing one
|
|
|
|
$(g).find(visElems).each(function() {
|
2014-02-17 06:48:40 +00:00
|
|
|
if (!this.id) {this.id = getNextId();}
|
2010-08-23 15:33:00 +00:00
|
|
|
});
|
|
|
|
|
2010-09-17 20:33:33 +00:00
|
|
|
selectOnly([g]);
|
2010-07-29 15:09:49 +00:00
|
|
|
|
2011-02-02 18:24:44 +00:00
|
|
|
var cm = pushGroupProperties(g, true);
|
2013-02-15 15:51:58 +00:00
|
|
|
if (cm) {
|
2011-02-02 18:24:44 +00:00
|
|
|
batchCmd.addSubCommand(cm);
|
|
|
|
}
|
|
|
|
|
2010-07-29 15:09:49 +00:00
|
|
|
addCommandToHistory(batchCmd);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
console.log('Unexpected element to ungroup:', elem);
|
2010-07-22 19:10:51 +00:00
|
|
|
}
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-07-22 19:10:51 +00:00
|
|
|
|
2014-01-31 10:40:52 +00:00
|
|
|
//
|
2010-06-22 14:52:51 +00:00
|
|
|
// Function: setSvgString
|
|
|
|
// This function sets the current drawing as the input SVG XML.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// xmlString - The SVG as XML text.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// This function returns false if the set was unsuccessful, true otherwise.
|
|
|
|
this.setSvgString = function(xmlString) {
|
|
|
|
try {
|
|
|
|
// convert string into XML document
|
2010-11-05 17:00:32 +00:00
|
|
|
var newDoc = svgedit.utilities.text2xml(xmlString);
|
2010-11-07 20:14:05 +00:00
|
|
|
|
|
|
|
this.prepareSvg(newDoc);
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
var batchCmd = new svgedit.history.BatchCommand('Change Source');
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
// remove old svg document
|
2010-11-13 09:58:51 +00:00
|
|
|
var nextSibling = svgcontent.nextSibling;
|
2010-06-22 14:52:51 +00:00
|
|
|
var oldzoom = svgroot.removeChild(svgcontent);
|
2013-02-17 08:21:07 +00:00
|
|
|
batchCmd.addSubCommand(new svgedit.history.RemoveElementCommand(oldzoom, nextSibling, svgroot));
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
// set new svg document
|
2012-02-27 17:24:03 +00:00
|
|
|
// If DOM3 adoptNode() available, use it. Otherwise fall back to DOM2 importNode()
|
2013-02-15 15:51:58 +00:00
|
|
|
if (svgdoc.adoptNode) {
|
2012-02-27 17:24:03 +00:00
|
|
|
svgcontent = svgdoc.adoptNode(newDoc.documentElement);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
svgcontent = svgdoc.importNode(newDoc.documentElement, true);
|
2012-02-26 18:08:34 +00:00
|
|
|
}
|
2010-08-23 15:33:00 +00:00
|
|
|
|
2012-02-27 17:24:03 +00:00
|
|
|
svgroot.appendChild(svgcontent);
|
2010-08-23 15:33:00 +00:00
|
|
|
var content = $(svgcontent);
|
|
|
|
|
2011-02-01 07:22:18 +00:00
|
|
|
canvas.current_drawing_ = new svgedit.draw.Drawing(svgcontent, idprefix);
|
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// retrieve or set the nonce
|
2011-02-01 07:22:18 +00:00
|
|
|
var nonce = getCurrentDrawing().getNonce();
|
|
|
|
if (nonce) {
|
2014-04-08 03:21:22 +00:00
|
|
|
call('setnonce', nonce);
|
2011-02-01 07:22:18 +00:00
|
|
|
} else {
|
2014-04-08 03:21:22 +00:00
|
|
|
call('unsetnonce');
|
2011-01-14 18:18:29 +00:00
|
|
|
}
|
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// change image href vals if possible
|
2010-08-23 15:33:00 +00:00
|
|
|
content.find('image').each(function() {
|
2010-06-22 14:52:51 +00:00
|
|
|
var image = this;
|
|
|
|
preventClickDefault(image);
|
2010-08-16 17:53:15 +00:00
|
|
|
var val = getHref(this);
|
2013-03-16 14:07:55 +00:00
|
|
|
if (val) {
|
|
|
|
if (val.indexOf('data:') === 0) {
|
|
|
|
// Check if an SVG-edit data URI
|
|
|
|
var m = val.match(/svgedit_url=(.*?);/);
|
|
|
|
if (m) {
|
|
|
|
var url = decodeURIComponent(m[1]);
|
|
|
|
$(new Image()).load(function () {
|
2013-03-16 14:11:43 +00:00
|
|
|
image.setAttributeNS(NS.XLINK, 'xlink:href', url);
|
2013-03-16 14:07:55 +00:00
|
|
|
}).attr('src', url);
|
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
2013-03-16 14:07:55 +00:00
|
|
|
// Add to encodableImages if it loads
|
|
|
|
canvas.embedImage(val);
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
});
|
2010-07-16 15:46:54 +00:00
|
|
|
|
|
|
|
// Wrap child SVGs in group elements
|
2010-08-23 15:33:00 +00:00
|
|
|
content.find('svg').each(function() {
|
2010-07-29 19:43:52 +00:00
|
|
|
// Skip if it's in a <defs>
|
2014-02-17 06:48:40 +00:00
|
|
|
if ($(this).closest('defs').length) {return;}
|
2010-07-29 19:43:52 +00:00
|
|
|
|
2010-07-16 15:46:54 +00:00
|
|
|
uniquifyElems(this);
|
|
|
|
|
|
|
|
// Check if it already has a gsvg group
|
|
|
|
var pa = this.parentNode;
|
2013-02-15 15:51:58 +00:00
|
|
|
if (pa.childNodes.length === 1 && pa.nodeName === 'g') {
|
2010-07-16 15:46:54 +00:00
|
|
|
$(pa).data('gsvg', this);
|
|
|
|
pa.id = pa.id || getNextId();
|
|
|
|
} else {
|
|
|
|
groupSvgElem(this);
|
|
|
|
}
|
|
|
|
});
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2010-10-13 13:27:39 +00:00
|
|
|
// For Firefox: Put all paint elems in defs
|
2013-02-15 15:51:58 +00:00
|
|
|
if (svgedit.browser.isGecko()) {
|
2013-02-14 15:19:46 +00:00
|
|
|
content.find('linearGradient, radialGradient, pattern').appendTo(svgedit.utilities.findDefs());
|
2010-10-04 16:01:58 +00:00
|
|
|
}
|
|
|
|
|
2010-07-29 15:09:49 +00:00
|
|
|
// Set ref element for <use> elements
|
2010-09-10 13:23:31 +00:00
|
|
|
|
|
|
|
// TODO: This should also be done if the object is re-added through "redo"
|
2011-01-31 18:22:04 +00:00
|
|
|
setUseData(content);
|
2010-07-29 15:09:49 +00:00
|
|
|
|
2010-09-28 18:59:31 +00:00
|
|
|
convertGradients(content[0]);
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
// recalculate dimensions on the top-level children so that unnecessary transforms
|
|
|
|
// are removed
|
2013-02-15 15:51:58 +00:00
|
|
|
svgedit.utilities.walkTreePost(svgcontent, function(n) {
|
|
|
|
try {
|
2013-02-20 06:29:25 +00:00
|
|
|
svgedit.recalculate.recalculateDimensions(n);
|
2013-02-15 15:51:58 +00:00
|
|
|
} catch(e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
});
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
var attrs = {
|
|
|
|
id: 'svgcontent',
|
2014-02-17 06:48:40 +00:00
|
|
|
overflow: curConfig.show_outside_canvas ? 'visible' : 'hidden'
|
2010-06-22 14:52:51 +00:00
|
|
|
};
|
|
|
|
|
2010-07-12 20:19:43 +00:00
|
|
|
var percs = false;
|
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// determine proper size
|
2014-04-08 03:21:22 +00:00
|
|
|
if (content.attr('viewBox')) {
|
|
|
|
var vb = content.attr('viewBox').split(' ');
|
2010-06-22 14:52:51 +00:00
|
|
|
attrs.width = vb[2];
|
|
|
|
attrs.height = vb[3];
|
2009-07-10 05:04:06 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
// handle content that doesn't have a viewBox
|
|
|
|
else {
|
|
|
|
$.each(['width', 'height'], function(i, dim) {
|
|
|
|
// Set to 100 if not given
|
2010-07-12 20:19:43 +00:00
|
|
|
var val = content.attr(dim);
|
|
|
|
|
2014-02-17 06:48:40 +00:00
|
|
|
if (!val) {val = '100%';}
|
2010-07-12 20:19:43 +00:00
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
if (String(val).substr(-1) === '%') {
|
2010-06-22 14:52:51 +00:00
|
|
|
// Use user units if percentage given
|
2010-07-12 20:19:43 +00:00
|
|
|
percs = true;
|
2010-06-22 14:52:51 +00:00
|
|
|
} else {
|
2013-02-14 15:19:46 +00:00
|
|
|
attrs[dim] = svgedit.units.convertToNum(dim, val);
|
2010-02-22 00:56:19 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2010-08-17 18:43:05 +00:00
|
|
|
// identify layers
|
|
|
|
identifyLayers();
|
|
|
|
|
2010-08-23 15:33:00 +00:00
|
|
|
// Give ID for any visible layer children missing one
|
|
|
|
content.children().find(visElems).each(function() {
|
2014-02-17 06:48:40 +00:00
|
|
|
if (!this.id) {this.id = getNextId();}
|
2010-08-23 15:33:00 +00:00
|
|
|
});
|
|
|
|
|
2010-07-12 20:19:43 +00:00
|
|
|
// Percentage width/height, so let's base it on visible elements
|
2013-02-15 15:51:58 +00:00
|
|
|
if (percs) {
|
2010-07-12 20:19:43 +00:00
|
|
|
var bb = getStrokedBBox();
|
2010-08-17 18:43:05 +00:00
|
|
|
attrs.width = bb.width + bb.x;
|
|
|
|
attrs.height = bb.height + bb.y;
|
2010-07-12 20:19:43 +00:00
|
|
|
}
|
|
|
|
|
2010-08-17 18:43:05 +00:00
|
|
|
// Just in case negative numbers are given or
|
|
|
|
// result from the percs calculation
|
2014-02-17 06:48:40 +00:00
|
|
|
if (attrs.width <= 0) {attrs.width = 100;}
|
|
|
|
if (attrs.height <= 0) {attrs.height = 100;}
|
2010-08-17 18:43:05 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
content.attr(attrs);
|
2014-02-17 06:48:40 +00:00
|
|
|
this.contentW = attrs.width;
|
|
|
|
this.contentH = attrs.height;
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2013-02-17 08:21:07 +00:00
|
|
|
batchCmd.addSubCommand(new svgedit.history.InsertElementCommand(svgcontent));
|
2010-06-22 14:52:51 +00:00
|
|
|
// update root to the correct size
|
2014-04-08 03:21:22 +00:00
|
|
|
var changes = content.attr(['width', 'height']);
|
2013-02-17 08:21:07 +00:00
|
|
|
batchCmd.addSubCommand(new svgedit.history.ChangeElementCommand(svgroot, changes));
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
// reset zoom
|
|
|
|
current_zoom = 1;
|
|
|
|
|
|
|
|
// reset transform lists
|
2010-11-05 14:59:59 +00:00
|
|
|
svgedit.transformlist.resetListMap();
|
2010-06-30 18:27:36 +00:00
|
|
|
clearSelection();
|
2011-02-23 16:24:07 +00:00
|
|
|
svgedit.path.clearData();
|
2010-06-22 14:52:51 +00:00
|
|
|
svgroot.appendChild(selectorManager.selectorParentGroup);
|
|
|
|
|
|
|
|
addCommandToHistory(batchCmd);
|
2014-04-08 03:21:22 +00:00
|
|
|
call('changed', [svgcontent]);
|
2010-06-22 14:52:51 +00:00
|
|
|
} catch(e) {
|
|
|
|
console.log(e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Function: importSvgString
|
2010-07-29 15:09:49 +00:00
|
|
|
// This function imports the input SVG XML as a <symbol> in the <defs>, then adds a
|
|
|
|
// <use> to the current layer.
|
2010-06-22 14:52:51 +00:00
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// xmlString - The SVG as XML text.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// This function returns false if the import was unsuccessful, true otherwise.
|
2010-06-28 13:10:22 +00:00
|
|
|
// TODO:
|
|
|
|
// * properly handle if namespace is introduced by imported content (must add to svgcontent
|
|
|
|
// and update all prefixes in the imported node)
|
|
|
|
// * properly handle recalculating dimensions, recalculateDimensions() doesn't handle
|
|
|
|
// arbitrary transform lists, but makes some assumptions about how the transform list
|
|
|
|
// was obtained
|
|
|
|
// * import should happen in top-left of current zoomed viewport
|
2010-07-29 15:09:49 +00:00
|
|
|
this.importSvgString = function(xmlString) {
|
2014-02-17 06:48:40 +00:00
|
|
|
var j, ts;
|
2010-12-01 21:20:52 +00:00
|
|
|
try {
|
|
|
|
// Get unique ID
|
|
|
|
var uid = svgedit.utilities.encode64(xmlString.length + xmlString).substr(0,32);
|
2010-07-16 15:46:54 +00:00
|
|
|
|
2010-12-20 15:19:18 +00:00
|
|
|
var useExisting = false;
|
|
|
|
|
|
|
|
// Look for symbol and make sure symbol exists in image
|
2013-02-15 15:51:58 +00:00
|
|
|
if (import_ids[uid]) {
|
|
|
|
if ( $(import_ids[uid].symbol).parents('#svgroot').length ) {
|
2010-12-20 15:19:18 +00:00
|
|
|
useExisting = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
var batchCmd = new svgedit.history.BatchCommand('Import SVG');
|
2014-02-17 06:48:40 +00:00
|
|
|
var symbol;
|
2013-02-15 15:51:58 +00:00
|
|
|
if (useExisting) {
|
2014-02-17 06:48:40 +00:00
|
|
|
symbol = import_ids[uid].symbol;
|
|
|
|
ts = import_ids[uid].xform;
|
2010-12-01 21:20:52 +00:00
|
|
|
} else {
|
|
|
|
// convert string into XML document
|
|
|
|
var newDoc = svgedit.utilities.text2xml(xmlString);
|
|
|
|
|
|
|
|
this.prepareSvg(newDoc);
|
|
|
|
|
|
|
|
// import new svg document into our document
|
2012-02-27 17:24:03 +00:00
|
|
|
var svg;
|
|
|
|
// If DOM3 adoptNode() available, use it. Otherwise fall back to DOM2 importNode()
|
2013-02-15 15:51:58 +00:00
|
|
|
if (svgdoc.adoptNode) {
|
2012-02-27 17:24:03 +00:00
|
|
|
svg = svgdoc.adoptNode(newDoc.documentElement);
|
2013-02-15 15:51:58 +00:00
|
|
|
} else {
|
2012-02-27 17:24:03 +00:00
|
|
|
svg = svgdoc.importNode(newDoc.documentElement, true);
|
|
|
|
}
|
2010-12-01 21:20:52 +00:00
|
|
|
|
|
|
|
uniquifyElems(svg);
|
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
var innerw = svgedit.units.convertToNum('width', svg.getAttribute('width')),
|
|
|
|
innerh = svgedit.units.convertToNum('height', svg.getAttribute('height')),
|
|
|
|
innervb = svg.getAttribute('viewBox'),
|
2010-12-01 21:20:52 +00:00
|
|
|
// if no explicit viewbox, create one out of the width and height
|
2014-04-08 03:21:22 +00:00
|
|
|
vb = innervb ? innervb.split(' ') : [0, 0, innerw, innerh];
|
2014-02-17 06:48:40 +00:00
|
|
|
for (j = 0; j < 4; ++j) {
|
2010-12-01 21:20:52 +00:00
|
|
|
vb[j] = +(vb[j]);
|
2014-02-17 06:48:40 +00:00
|
|
|
}
|
2010-12-01 21:20:52 +00:00
|
|
|
|
|
|
|
// TODO: properly handle preserveAspectRatio
|
2014-04-08 03:21:22 +00:00
|
|
|
var canvasw = +svgcontent.getAttribute('width'),
|
|
|
|
canvash = +svgcontent.getAttribute('height');
|
2010-12-01 21:20:52 +00:00
|
|
|
// imported content should be 1/3 of the canvas on its largest dimension
|
|
|
|
|
|
|
|
if (innerh > innerw) {
|
2014-04-08 03:21:22 +00:00
|
|
|
ts = 'scale(' + (canvash/3)/vb[3] + ')';
|
2013-02-15 15:51:58 +00:00
|
|
|
} else {
|
2014-04-08 03:21:22 +00:00
|
|
|
ts = 'scale(' + (canvash/3)/vb[2] + ')';
|
2010-12-01 21:20:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hack to make recalculateDimensions understand how to scale
|
2014-04-08 03:21:22 +00:00
|
|
|
ts = 'translate(0) ' + ts + ' translate(0)';
|
2010-12-01 21:20:52 +00:00
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
symbol = svgdoc.createElementNS(NS.SVG, 'symbol');
|
2013-02-14 15:19:46 +00:00
|
|
|
var defs = svgedit.utilities.findDefs();
|
2010-12-01 21:20:52 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (svgedit.browser.isGecko()) {
|
2010-12-01 21:20:52 +00:00
|
|
|
// Move all gradients into root for Firefox, workaround for this bug:
|
|
|
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=353575
|
2011-01-12 07:03:54 +00:00
|
|
|
// TODO: Make this properly undo-able.
|
2010-12-01 21:20:52 +00:00
|
|
|
$(svg).find('linearGradient, radialGradient, pattern').appendTo(defs);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (svg.firstChild) {
|
|
|
|
var first = svg.firstChild;
|
|
|
|
symbol.appendChild(first);
|
|
|
|
}
|
|
|
|
var attrs = svg.attributes;
|
2014-02-17 06:48:40 +00:00
|
|
|
var i;
|
|
|
|
for (i = 0; i < attrs.length; i++) {
|
2010-12-01 21:20:52 +00:00
|
|
|
var attr = attrs[i];
|
|
|
|
symbol.setAttribute(attr.nodeName, attr.nodeValue);
|
|
|
|
}
|
|
|
|
symbol.id = getNextId();
|
|
|
|
|
|
|
|
// Store data
|
|
|
|
import_ids[uid] = {
|
|
|
|
symbol: symbol,
|
|
|
|
xform: ts
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-12-01 21:20:52 +00:00
|
|
|
|
2013-02-14 15:19:46 +00:00
|
|
|
svgedit.utilities.findDefs().appendChild(symbol);
|
2013-02-17 08:21:07 +00:00
|
|
|
batchCmd.addSubCommand(new svgedit.history.InsertElementCommand(symbol));
|
2010-07-16 15:46:54 +00:00
|
|
|
}
|
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
var use_el = svgdoc.createElementNS(NS.SVG, 'use');
|
2011-01-15 06:40:37 +00:00
|
|
|
use_el.id = getNextId();
|
2014-04-08 03:21:22 +00:00
|
|
|
setHref(use_el, '#' + symbol.id);
|
2010-10-13 13:27:39 +00:00
|
|
|
|
2011-01-25 16:15:29 +00:00
|
|
|
(current_group || getCurrentDrawing().getCurrentLayer()).appendChild(use_el);
|
2013-02-17 08:21:07 +00:00
|
|
|
batchCmd.addSubCommand(new svgedit.history.InsertElementCommand(use_el));
|
2010-07-29 15:09:49 +00:00
|
|
|
clearSelection();
|
2010-07-29 17:49:28 +00:00
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
use_el.setAttribute('transform', ts);
|
2013-02-20 06:29:25 +00:00
|
|
|
svgedit.recalculate.recalculateDimensions(use_el);
|
2011-02-10 18:10:05 +00:00
|
|
|
$(use_el).data('symbol', symbol).data('ref', symbol);
|
2010-07-29 15:09:49 +00:00
|
|
|
addToSelection([use_el]);
|
2010-12-01 21:20:52 +00:00
|
|
|
|
2010-07-16 15:46:54 +00:00
|
|
|
// TODO: Find way to add this in a recalculateDimensions-parsable way
|
2014-01-31 00:39:35 +00:00
|
|
|
// if (vb[0] != 0 || vb[1] != 0)
|
2014-04-08 03:21:22 +00:00
|
|
|
// ts = 'translate(' + (-vb[0]) + ',' + (-vb[1]) + ') ' + ts;
|
2011-01-12 07:03:54 +00:00
|
|
|
addCommandToHistory(batchCmd);
|
2014-04-08 03:21:22 +00:00
|
|
|
call('changed', [svgcontent]);
|
2010-07-16 15:46:54 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
} catch(e) {
|
|
|
|
console.log(e);
|
|
|
|
return false;
|
|
|
|
}
|
2010-02-21 01:12:59 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2011-02-10 04:10:03 +00:00
|
|
|
// TODO(codedread): Move all layer/context functions in draw.js
|
2010-06-22 14:52:51 +00:00
|
|
|
// Layer API Functions
|
|
|
|
|
|
|
|
// Group: Layers
|
|
|
|
|
2010-06-28 13:10:22 +00:00
|
|
|
// Function: identifyLayers
|
|
|
|
// Updates layer system
|
2010-11-12 19:08:29 +00:00
|
|
|
var identifyLayers = canvas.identifyLayers = function() {
|
2010-09-23 19:53:43 +00:00
|
|
|
leaveContext();
|
2011-01-26 03:18:59 +00:00
|
|
|
getCurrentDrawing().identifyLayers();
|
2010-06-22 14:52:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Function: createLayer
|
|
|
|
// Creates a new top-level layer in the drawing with the given name, sets the current layer
|
2014-01-31 10:40:52 +00:00
|
|
|
// to it, and then clears the selection. This function then calls the 'changed' handler.
|
2010-06-22 14:52:51 +00:00
|
|
|
// This is an undoable action.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// name - The given name
|
|
|
|
this.createLayer = function(name) {
|
2014-04-08 03:21:22 +00:00
|
|
|
var batchCmd = new svgedit.history.BatchCommand('Create Layer');
|
2011-01-28 04:37:41 +00:00
|
|
|
var new_layer = getCurrentDrawing().createLayer(name);
|
2013-02-17 08:21:07 +00:00
|
|
|
batchCmd.addSubCommand(new svgedit.history.InsertElementCommand(new_layer));
|
2010-06-22 14:52:51 +00:00
|
|
|
addCommandToHistory(batchCmd);
|
2010-06-30 18:27:36 +00:00
|
|
|
clearSelection();
|
2014-04-08 03:21:22 +00:00
|
|
|
call('changed', [new_layer]);
|
2010-06-22 14:52:51 +00:00
|
|
|
};
|
|
|
|
|
2010-08-19 18:30:19 +00:00
|
|
|
// Function: cloneLayer
|
|
|
|
// Creates a new top-level layer in the drawing with the given name, copies all the current layer's contents
|
2014-01-31 10:40:52 +00:00
|
|
|
// to it, and then clears the selection. This function then calls the 'changed' handler.
|
2010-08-19 18:30:19 +00:00
|
|
|
// This is an undoable action.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// name - The given name
|
|
|
|
this.cloneLayer = function(name) {
|
2014-04-08 03:21:22 +00:00
|
|
|
var batchCmd = new svgedit.history.BatchCommand('Duplicate Layer');
|
|
|
|
var new_layer = svgdoc.createElementNS(NS.SVG, 'g');
|
|
|
|
var layer_title = svgdoc.createElementNS(NS.SVG, 'title');
|
2010-08-19 18:30:19 +00:00
|
|
|
layer_title.textContent = name;
|
|
|
|
new_layer.appendChild(layer_title);
|
2011-01-28 06:45:34 +00:00
|
|
|
var current_layer = getCurrentDrawing().getCurrentLayer();
|
|
|
|
$(current_layer).after(new_layer);
|
|
|
|
var childs = current_layer.childNodes;
|
2014-02-17 06:48:40 +00:00
|
|
|
var i;
|
|
|
|
for (i = 0; i < childs.length; i++) {
|
2010-08-19 18:30:19 +00:00
|
|
|
var ch = childs[i];
|
2014-02-17 06:48:40 +00:00
|
|
|
if (ch.localName == 'title') {continue;}
|
2010-08-19 18:30:19 +00:00
|
|
|
new_layer.appendChild(copyElem(ch));
|
|
|
|
}
|
|
|
|
|
|
|
|
clearSelection();
|
|
|
|
identifyLayers();
|
|
|
|
|
2013-02-17 08:21:07 +00:00
|
|
|
batchCmd.addSubCommand(new svgedit.history.InsertElementCommand(new_layer));
|
2010-08-19 18:30:19 +00:00
|
|
|
addCommandToHistory(batchCmd);
|
|
|
|
canvas.setCurrentLayer(name);
|
2014-04-08 03:21:22 +00:00
|
|
|
call('changed', [new_layer]);
|
2010-08-19 18:30:19 +00:00
|
|
|
};
|
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// Function: deleteCurrentLayer
|
|
|
|
// Deletes the current layer from the drawing and then clears the selection. This function
|
2014-01-31 10:40:52 +00:00
|
|
|
// then calls the 'changed' handler. This is an undoable action.
|
2010-06-22 14:52:51 +00:00
|
|
|
this.deleteCurrentLayer = function() {
|
2011-02-01 07:22:18 +00:00
|
|
|
var current_layer = getCurrentDrawing().getCurrentLayer();
|
2011-01-30 17:11:56 +00:00
|
|
|
var nextSibling = current_layer.nextSibling;
|
|
|
|
var parent = current_layer.parentNode;
|
2011-02-01 07:22:18 +00:00
|
|
|
current_layer = getCurrentDrawing().deleteCurrentLayer();
|
2011-01-30 17:11:56 +00:00
|
|
|
if (current_layer) {
|
2014-04-08 03:21:22 +00:00
|
|
|
var batchCmd = new svgedit.history.BatchCommand('Delete Layer');
|
2011-01-30 17:11:56 +00:00
|
|
|
// store in our Undo History
|
2013-02-17 08:21:07 +00:00
|
|
|
batchCmd.addSubCommand(new svgedit.history.RemoveElementCommand(current_layer, nextSibling, parent));
|
2009-09-22 01:00:24 +00:00
|
|
|
addCommandToHistory(batchCmd);
|
2010-06-30 18:27:36 +00:00
|
|
|
clearSelection();
|
2014-04-08 03:21:22 +00:00
|
|
|
call('changed', [parent]);
|
2010-06-22 14:52:51 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Function: setCurrentLayer
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// name - the name of the layer you want to switch to.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// true if the current layer was switched, otherwise false
|
|
|
|
this.setCurrentLayer = function(name) {
|
2011-01-26 03:18:59 +00:00
|
|
|
var result = getCurrentDrawing().setCurrentLayer(svgedit.utilities.toXml(name));
|
|
|
|
if (result) {
|
|
|
|
clearSelection();
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
2011-01-26 03:18:59 +00:00
|
|
|
return result;
|
2010-06-22 14:52:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Function: renameCurrentLayer
|
|
|
|
// Renames the current layer. If the layer name is not valid (i.e. unique), then this function
|
|
|
|
// does nothing and returns false, otherwise it returns true. This is an undo-able action.
|
|
|
|
//
|
|
|
|
// Parameters:
|
2014-01-31 10:40:52 +00:00
|
|
|
// newname - the new name you want to give the current layer. This name must be unique
|
2010-06-22 14:52:51 +00:00
|
|
|
// among all layer names.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// true if the rename succeeded, false otherwise.
|
|
|
|
this.renameCurrentLayer = function(newname) {
|
2014-02-17 06:48:40 +00:00
|
|
|
var i;
|
2011-02-01 07:22:18 +00:00
|
|
|
var drawing = getCurrentDrawing();
|
|
|
|
if (drawing.current_layer) {
|
|
|
|
var oldLayer = drawing.current_layer;
|
2011-01-25 16:15:29 +00:00
|
|
|
// setCurrentLayer will return false if the name doesn't already exist
|
|
|
|
// this means we are free to rename our oldLayer
|
2010-06-22 14:52:51 +00:00
|
|
|
if (!canvas.setCurrentLayer(newname)) {
|
2014-04-08 03:21:22 +00:00
|
|
|
var batchCmd = new svgedit.history.BatchCommand('Rename Layer');
|
2010-06-22 14:52:51 +00:00
|
|
|
// find the index of the layer
|
2014-02-17 06:48:40 +00:00
|
|
|
for (i = 0; i < drawing.getNumLayers(); ++i) {
|
|
|
|
if (drawing.all_layers[i][1] == oldLayer) {break;}
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
2011-02-01 07:22:18 +00:00
|
|
|
var oldname = drawing.getLayerName(i);
|
|
|
|
drawing.all_layers[i][0] = svgedit.utilities.toXml(newname);
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
// now change the underlying title element contents
|
|
|
|
var len = oldLayer.childNodes.length;
|
2014-02-17 06:48:40 +00:00
|
|
|
for (i = 0; i < len; ++i) {
|
2010-06-22 14:52:51 +00:00
|
|
|
var child = oldLayer.childNodes.item(i);
|
|
|
|
// found the <title> element, now append all the
|
2014-04-08 03:21:22 +00:00
|
|
|
if (child && child.tagName == 'title') {
|
2010-06-22 14:52:51 +00:00
|
|
|
// wipe out old name
|
|
|
|
while (child.firstChild) { child.removeChild(child.firstChild); }
|
|
|
|
child.textContent = newname;
|
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
batchCmd.addSubCommand(new svgedit.history.ChangeElementCommand(child, {'#text':oldname}));
|
2010-06-22 14:52:51 +00:00
|
|
|
addCommandToHistory(batchCmd);
|
2014-04-08 03:21:22 +00:00
|
|
|
call('changed', [oldLayer]);
|
2010-06-22 14:52:51 +00:00
|
|
|
return true;
|
2009-09-21 22:16:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-02-01 07:22:18 +00:00
|
|
|
drawing.current_layer = oldLayer;
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Function: setCurrentLayerPosition
|
|
|
|
// Changes the position of the current layer to the new value. If the new index is not valid,
|
|
|
|
// this function does nothing and returns false, otherwise it returns true. This is an
|
|
|
|
// undo-able action.
|
|
|
|
//
|
|
|
|
// Parameters:
|
2014-01-31 10:40:52 +00:00
|
|
|
// newpos - The zero-based index of the new position of the layer. This should be between
|
2010-06-22 14:52:51 +00:00
|
|
|
// 0 and (number of layers - 1)
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// true if the current layer position was changed, false otherwise.
|
|
|
|
this.setCurrentLayerPosition = function(newpos) {
|
2014-02-17 06:48:40 +00:00
|
|
|
var oldpos, drawing = getCurrentDrawing();
|
2011-02-01 07:22:18 +00:00
|
|
|
if (drawing.current_layer && newpos >= 0 && newpos < drawing.getNumLayers()) {
|
2014-02-17 06:48:40 +00:00
|
|
|
for (oldpos = 0; oldpos < drawing.getNumLayers(); ++oldpos) {
|
|
|
|
if (drawing.all_layers[oldpos][1] == drawing.current_layer) {break;}
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
// some unknown error condition (current_layer not in all_layers)
|
2011-02-01 07:22:18 +00:00
|
|
|
if (oldpos == drawing.getNumLayers()) { return false; }
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
if (oldpos != newpos) {
|
|
|
|
// if our new position is below us, we need to insert before the node after newpos
|
|
|
|
var refLayer = null;
|
2011-02-01 07:22:18 +00:00
|
|
|
var oldNextSibling = drawing.current_layer.nextSibling;
|
2010-06-22 14:52:51 +00:00
|
|
|
if (newpos > oldpos ) {
|
2011-02-01 07:22:18 +00:00
|
|
|
if (newpos < drawing.getNumLayers()-1) {
|
|
|
|
refLayer = drawing.all_layers[newpos+1][1];
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// if our new position is above us, we need to insert before the node at newpos
|
|
|
|
else {
|
2011-02-01 07:22:18 +00:00
|
|
|
refLayer = drawing.all_layers[newpos][1];
|
2009-09-21 22:16:44 +00:00
|
|
|
}
|
2011-02-01 07:22:18 +00:00
|
|
|
svgcontent.insertBefore(drawing.current_layer, refLayer);
|
2013-02-17 08:21:07 +00:00
|
|
|
addCommandToHistory(new svgedit.history.MoveElementCommand(drawing.current_layer, oldNextSibling, svgcontent));
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
identifyLayers();
|
2011-02-01 07:22:18 +00:00
|
|
|
canvas.setCurrentLayer(drawing.getLayerName(newpos));
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
return true;
|
2009-09-21 22:16:44 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
2009-09-22 04:44:56 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Function: setLayerVisibility
|
|
|
|
// Sets the visibility of the layer. If the layer name is not valid, this function return
|
|
|
|
// false, otherwise it returns true. This is an undo-able action.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// layername - the name of the layer to change the visibility
|
|
|
|
// bVisible - true/false, whether the layer should be visible
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// true if the layer's visibility was set, false otherwise
|
|
|
|
this.setLayerVisibility = function(layername, bVisible) {
|
2011-01-28 07:39:30 +00:00
|
|
|
var drawing = getCurrentDrawing();
|
|
|
|
var prevVisibility = drawing.getLayerVisibility(layername);
|
|
|
|
var layer = drawing.setLayerVisibility(layername, bVisible);
|
|
|
|
if (layer) {
|
|
|
|
var oldDisplay = prevVisibility ? 'inline' : 'none';
|
2013-02-17 08:21:07 +00:00
|
|
|
addCommandToHistory(new svgedit.history.ChangeElementCommand(layer, {'display':oldDisplay}, 'Layer Visibility'));
|
2011-01-28 07:39:30 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
|
2011-01-28 07:39:30 +00:00
|
|
|
if (layer == drawing.getCurrentLayer()) {
|
2010-06-30 18:27:36 +00:00
|
|
|
clearSelection();
|
2010-06-22 14:52:51 +00:00
|
|
|
pathActions.clear();
|
|
|
|
}
|
2014-04-08 03:21:22 +00:00
|
|
|
// call('changed', [selected]);
|
2010-06-22 14:52:51 +00:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Function: moveSelectedToLayer
|
|
|
|
// Moves the selected elements to layername. If the name is not a valid layer name, then false
|
2014-01-31 10:40:52 +00:00
|
|
|
// is returned. Otherwise it returns true. This is an undo-able action.
|
2010-06-22 14:52:51 +00:00
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// layername - the name of the layer you want to which you want to move the selected elements
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// true if the selected elements were moved to the layer, false otherwise.
|
|
|
|
this.moveSelectedToLayer = function(layername) {
|
|
|
|
// find the layer
|
2014-02-17 06:48:40 +00:00
|
|
|
var i;
|
2010-06-22 14:52:51 +00:00
|
|
|
var layer = null;
|
2011-02-01 07:22:18 +00:00
|
|
|
var drawing = getCurrentDrawing();
|
2014-02-17 06:48:40 +00:00
|
|
|
for (i = 0; i < drawing.getNumLayers(); ++i) {
|
2011-02-01 07:22:18 +00:00
|
|
|
if (drawing.getLayerName(i) == layername) {
|
|
|
|
layer = drawing.all_layers[i][1];
|
2010-06-22 14:52:51 +00:00
|
|
|
break;
|
2009-10-10 21:46:06 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
2014-02-17 06:48:40 +00:00
|
|
|
if (!layer) {return false;}
|
2009-10-10 21:46:06 +00:00
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
var batchCmd = new svgedit.history.BatchCommand('Move Elements to Layer');
|
2009-10-14 05:11:51 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// loop for each selected element and move it
|
|
|
|
var selElems = selectedElements;
|
2014-02-17 06:48:40 +00:00
|
|
|
i = selElems.length;
|
2010-06-22 14:52:51 +00:00
|
|
|
while (i--) {
|
|
|
|
var elem = selElems[i];
|
2014-02-17 06:48:40 +00:00
|
|
|
if (!elem) {continue;}
|
2010-06-22 14:52:51 +00:00
|
|
|
var oldNextSibling = elem.nextSibling;
|
|
|
|
// TODO: this is pretty brittle!
|
|
|
|
var oldLayer = elem.parentNode;
|
|
|
|
layer.appendChild(elem);
|
2013-02-17 08:21:07 +00:00
|
|
|
batchCmd.addSubCommand(new svgedit.history.MoveElementCommand(elem, oldNextSibling, oldLayer));
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addCommandToHistory(batchCmd);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2010-08-19 18:30:19 +00:00
|
|
|
this.mergeLayer = function(skipHistory) {
|
2014-04-08 03:21:22 +00:00
|
|
|
var batchCmd = new svgedit.history.BatchCommand('Merge Layer');
|
2011-02-01 07:22:18 +00:00
|
|
|
var drawing = getCurrentDrawing();
|
|
|
|
var prev = $(drawing.current_layer).prev()[0];
|
2014-02-17 06:48:40 +00:00
|
|
|
if (!prev) {return;}
|
2011-02-01 07:22:18 +00:00
|
|
|
var childs = drawing.current_layer.childNodes;
|
2010-08-19 18:30:19 +00:00
|
|
|
var len = childs.length;
|
2011-02-01 07:22:18 +00:00
|
|
|
var layerNextSibling = drawing.current_layer.nextSibling;
|
2013-02-17 08:21:07 +00:00
|
|
|
batchCmd.addSubCommand(new svgedit.history.RemoveElementCommand(drawing.current_layer, layerNextSibling, svgcontent));
|
2010-08-19 18:30:19 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
while (drawing.current_layer.firstChild) {
|
2011-02-01 07:22:18 +00:00
|
|
|
var ch = drawing.current_layer.firstChild;
|
2013-02-15 15:51:58 +00:00
|
|
|
if (ch.localName == 'title') {
|
2010-11-13 09:58:51 +00:00
|
|
|
var chNextSibling = ch.nextSibling;
|
2013-02-17 08:21:07 +00:00
|
|
|
batchCmd.addSubCommand(new svgedit.history.RemoveElementCommand(ch, chNextSibling, drawing.current_layer));
|
2011-02-01 07:22:18 +00:00
|
|
|
drawing.current_layer.removeChild(ch);
|
2010-08-19 18:30:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
var oldNextSibling = ch.nextSibling;
|
|
|
|
prev.appendChild(ch);
|
2013-02-17 08:21:07 +00:00
|
|
|
batchCmd.addSubCommand(new svgedit.history.MoveElementCommand(ch, oldNextSibling, drawing.current_layer));
|
2010-08-19 18:30:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove current layer
|
2011-02-01 07:22:18 +00:00
|
|
|
svgcontent.removeChild(drawing.current_layer);
|
2010-08-19 18:30:19 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!skipHistory) {
|
2010-08-19 18:30:19 +00:00
|
|
|
clearSelection();
|
|
|
|
identifyLayers();
|
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
call('changed', [svgcontent]);
|
2010-08-19 18:30:19 +00:00
|
|
|
|
|
|
|
addCommandToHistory(batchCmd);
|
|
|
|
}
|
|
|
|
|
2011-02-01 07:22:18 +00:00
|
|
|
drawing.current_layer = prev;
|
2010-08-19 18:30:19 +00:00
|
|
|
return batchCmd;
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-08-19 18:30:19 +00:00
|
|
|
|
|
|
|
this.mergeAllLayers = function() {
|
2014-04-08 03:21:22 +00:00
|
|
|
var batchCmd = new svgedit.history.BatchCommand('Merge all Layers');
|
2011-02-01 07:22:18 +00:00
|
|
|
var drawing = getCurrentDrawing();
|
|
|
|
drawing.current_layer = drawing.all_layers[drawing.getNumLayers()-1][1];
|
2013-02-15 15:51:58 +00:00
|
|
|
while ($(svgcontent).children('g').length > 1) {
|
2010-08-19 18:30:19 +00:00
|
|
|
batchCmd.addSubCommand(canvas.mergeLayer(true));
|
|
|
|
}
|
|
|
|
|
|
|
|
clearSelection();
|
|
|
|
identifyLayers();
|
2014-04-08 03:21:22 +00:00
|
|
|
call('changed', [svgcontent]);
|
2010-08-19 18:30:19 +00:00
|
|
|
addCommandToHistory(batchCmd);
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-08-19 18:30:19 +00:00
|
|
|
|
2010-09-23 19:53:43 +00:00
|
|
|
// Function: leaveContext
|
|
|
|
// Return from a group context to the regular kind, make any previously
|
|
|
|
// disabled elements enabled again
|
|
|
|
var leaveContext = this.leaveContext = function() {
|
2014-02-17 06:48:40 +00:00
|
|
|
var i, len = disabled_elems.length;
|
2013-02-15 15:51:58 +00:00
|
|
|
if (len) {
|
2014-02-17 06:48:40 +00:00
|
|
|
for (i = 0; i < len; i++) {
|
2010-09-22 18:42:24 +00:00
|
|
|
var elem = disabled_elems[i];
|
|
|
|
var orig = elData(elem, 'orig_opac');
|
2013-02-15 15:51:58 +00:00
|
|
|
if (orig !== 1) {
|
2010-09-22 18:42:24 +00:00
|
|
|
elem.setAttribute('opacity', orig);
|
|
|
|
} else {
|
|
|
|
elem.removeAttribute('opacity');
|
|
|
|
}
|
|
|
|
elem.setAttribute('style', 'pointer-events: inherit');
|
|
|
|
}
|
|
|
|
disabled_elems = [];
|
|
|
|
clearSelection(true);
|
2014-04-08 03:21:22 +00:00
|
|
|
call('contextset', null);
|
2010-09-22 18:42:24 +00:00
|
|
|
}
|
|
|
|
current_group = null;
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-09-22 18:42:24 +00:00
|
|
|
|
2010-09-23 19:53:43 +00:00
|
|
|
// Function: setContext
|
|
|
|
// Set the current context (for in-group editing)
|
|
|
|
var setContext = this.setContext = function(elem) {
|
2010-09-23 20:04:44 +00:00
|
|
|
leaveContext();
|
2013-02-15 15:51:58 +00:00
|
|
|
if (typeof elem === 'string') {
|
2013-02-14 15:19:46 +00:00
|
|
|
elem = svgedit.utilities.getElem(elem);
|
2010-09-23 19:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Edit inside this group
|
|
|
|
current_group = elem;
|
|
|
|
|
|
|
|
// Disable other elements
|
|
|
|
$(elem).parentsUntil('#svgcontent').andSelf().siblings().each(function() {
|
|
|
|
var opac = this.getAttribute('opacity') || 1;
|
|
|
|
// Store the original's opacity
|
|
|
|
elData(this, 'orig_opac', opac);
|
2014-02-17 06:48:40 +00:00
|
|
|
this.setAttribute('opacity', opac * 0.33);
|
2010-09-23 19:53:43 +00:00
|
|
|
this.setAttribute('style', 'pointer-events: none');
|
|
|
|
disabled_elems.push(this);
|
|
|
|
});
|
|
|
|
|
|
|
|
clearSelection();
|
2014-04-08 03:21:22 +00:00
|
|
|
call('contextset', current_group);
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-09-23 19:53:43 +00:00
|
|
|
|
2010-06-28 20:09:34 +00:00
|
|
|
// Group: Document functions
|
2009-09-21 02:05:58 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// Function: clear
|
2014-01-31 10:40:52 +00:00
|
|
|
// Clears the current document. This is not an undoable action.
|
2010-06-22 14:52:51 +00:00
|
|
|
this.clear = function() {
|
|
|
|
pathActions.clear();
|
|
|
|
|
2011-02-01 15:36:45 +00:00
|
|
|
clearSelection();
|
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// clear the svgcontent node
|
2011-02-01 07:22:18 +00:00
|
|
|
canvas.clearSvgContentElement();
|
|
|
|
|
|
|
|
// create new document
|
|
|
|
canvas.current_drawing_ = new svgedit.draw.Drawing(svgcontent);
|
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// create empty first layer
|
2014-04-08 03:21:22 +00:00
|
|
|
canvas.createLayer('Layer 1');
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
// clear the undo stack
|
2010-09-24 16:44:20 +00:00
|
|
|
canvas.undoMgr.resetUndoStack();
|
2011-02-01 07:22:18 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// reset the selector manager
|
|
|
|
selectorManager.initGroup();
|
2011-02-01 07:22:18 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// reset the rubber band box
|
|
|
|
rubberBox = selectorManager.getRubberBandBox();
|
2011-02-01 07:22:18 +00:00
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
call('cleared');
|
2010-06-22 14:52:51 +00:00
|
|
|
};
|
|
|
|
|
2010-06-28 13:10:22 +00:00
|
|
|
// Function: linkControlPoints
|
|
|
|
// Alias function
|
|
|
|
this.linkControlPoints = pathActions.linkControlPoints;
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2010-06-28 13:10:22 +00:00
|
|
|
// Function: getContentElem
|
|
|
|
// Returns the content DOM element
|
2010-06-22 14:52:51 +00:00
|
|
|
this.getContentElem = function() { return svgcontent; };
|
2010-06-28 13:10:22 +00:00
|
|
|
|
|
|
|
// Function: getRootElem
|
|
|
|
// Returns the root DOM element
|
2010-06-22 14:52:51 +00:00
|
|
|
this.getRootElem = function() { return svgroot; };
|
2010-06-28 13:10:22 +00:00
|
|
|
|
|
|
|
// Function: getSelectedElems
|
|
|
|
// Returns the array with selected DOM elements
|
2010-06-22 14:52:51 +00:00
|
|
|
this.getSelectedElems = function() { return selectedElements; };
|
2010-03-11 15:27:18 +00:00
|
|
|
|
2010-06-28 13:10:22 +00:00
|
|
|
// Function: getResolution
|
|
|
|
// Returns the current dimensions and zoom level in an object
|
2010-06-30 18:27:36 +00:00
|
|
|
var getResolution = this.getResolution = function() {
|
2014-04-08 03:21:22 +00:00
|
|
|
// var vb = svgcontent.getAttribute('viewBox').split(' ');
|
2014-01-31 00:39:35 +00:00
|
|
|
// return {'w':vb[2], 'h':vb[3], 'zoom': current_zoom};
|
2010-10-27 18:15:28 +00:00
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
var width = svgcontent.getAttribute('width')/current_zoom;
|
|
|
|
var height = svgcontent.getAttribute('height')/current_zoom;
|
2010-10-27 18:15:28 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
return {
|
2010-10-27 18:15:28 +00:00
|
|
|
'w': width,
|
|
|
|
'h': height,
|
2010-06-22 14:52:51 +00:00
|
|
|
'zoom': current_zoom
|
2009-07-10 05:04:06 +00:00
|
|
|
};
|
2010-06-22 14:52:51 +00:00
|
|
|
};
|
|
|
|
|
2010-06-30 18:27:36 +00:00
|
|
|
// Function: getZoom
|
|
|
|
// Returns the current zoom level
|
|
|
|
this.getZoom = function(){return current_zoom;};
|
|
|
|
|
|
|
|
// Function: getVersion
|
|
|
|
// Returns a string which describes the revision number of SvgCanvas.
|
|
|
|
this.getVersion = function() {
|
2014-04-08 03:21:22 +00:00
|
|
|
return 'svgcanvas.js ($Rev$)';
|
2010-06-30 18:27:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Function: setUiStrings
|
|
|
|
// Update interface strings with given values
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// strs - Object with strings (see uiStrings for examples)
|
|
|
|
this.setUiStrings = function(strs) {
|
2011-01-03 18:45:36 +00:00
|
|
|
$.extend(uiStrings, strs.notification);
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-06-30 18:27:36 +00:00
|
|
|
|
|
|
|
// Function: setConfig
|
|
|
|
// Update configuration options with given values
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// opts - Object with options (see curConfig for examples)
|
|
|
|
this.setConfig = function(opts) {
|
|
|
|
$.extend(curConfig, opts);
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-06-30 18:27:36 +00:00
|
|
|
|
2011-01-30 17:11:56 +00:00
|
|
|
// Function: getTitle
|
2010-07-22 19:10:51 +00:00
|
|
|
// Returns the current group/SVG's title contents
|
|
|
|
this.getTitle = function(elem) {
|
2014-02-17 06:48:40 +00:00
|
|
|
var i;
|
2010-07-22 19:10:51 +00:00
|
|
|
elem = elem || selectedElements[0];
|
2014-02-17 06:48:40 +00:00
|
|
|
if (!elem) {return;}
|
2010-07-29 15:09:49 +00:00
|
|
|
elem = $(elem).data('gsvg') || $(elem).data('symbol') || elem;
|
2010-07-22 19:10:51 +00:00
|
|
|
var childs = elem.childNodes;
|
2014-02-17 06:48:40 +00:00
|
|
|
for (i = 0; i < childs.length; i++) {
|
2013-02-15 15:51:58 +00:00
|
|
|
if (childs[i].nodeName == 'title') {
|
2010-06-22 14:52:51 +00:00
|
|
|
return childs[i].textContent;
|
2009-10-19 19:06:07 +00:00
|
|
|
}
|
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
return '';
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2010-07-22 19:10:51 +00:00
|
|
|
// Function: setGroupTitle
|
|
|
|
// Sets the group/SVG's title content
|
|
|
|
// TODO: Combine this with setDocumentTitle
|
|
|
|
this.setGroupTitle = function(val) {
|
|
|
|
var elem = selectedElements[0];
|
|
|
|
elem = $(elem).data('gsvg') || elem;
|
|
|
|
|
|
|
|
var ts = $(elem).children('title');
|
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
var batchCmd = new svgedit.history.BatchCommand('Set Label');
|
2010-07-22 19:10:51 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!val.length) {
|
2010-07-22 19:10:51 +00:00
|
|
|
// Remove title element
|
2010-11-13 09:58:51 +00:00
|
|
|
var tsNextSibling = ts.nextSibling;
|
2013-02-17 08:21:07 +00:00
|
|
|
batchCmd.addSubCommand(new svgedit.history.RemoveElementCommand(ts[0], tsNextSibling, elem));
|
2010-07-22 19:10:51 +00:00
|
|
|
ts.remove();
|
2013-02-15 15:51:58 +00:00
|
|
|
} else if (ts.length) {
|
2010-07-22 19:10:51 +00:00
|
|
|
// Change title contents
|
|
|
|
var title = ts[0];
|
2013-02-17 08:21:07 +00:00
|
|
|
batchCmd.addSubCommand(new svgedit.history.ChangeElementCommand(title, {'#text': title.textContent}));
|
2010-07-22 19:10:51 +00:00
|
|
|
title.textContent = val;
|
|
|
|
} else {
|
|
|
|
// Add title element
|
2014-04-08 03:21:22 +00:00
|
|
|
title = svgdoc.createElementNS(NS.SVG, 'title');
|
2010-07-22 19:10:51 +00:00
|
|
|
title.textContent = val;
|
|
|
|
$(elem).prepend(title);
|
2013-02-17 08:21:07 +00:00
|
|
|
batchCmd.addSubCommand(new svgedit.history.InsertElementCommand(title));
|
2010-07-22 19:10:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addCommandToHistory(batchCmd);
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-07-22 19:10:51 +00:00
|
|
|
|
|
|
|
// Function: getDocumentTitle
|
|
|
|
// Returns the current document title or an empty string if not found
|
|
|
|
this.getDocumentTitle = function() {
|
|
|
|
return canvas.getTitle(svgcontent);
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-07-22 19:10:51 +00:00
|
|
|
|
2010-06-28 13:10:22 +00:00
|
|
|
// Function: setDocumentTitle
|
|
|
|
// Adds/updates a title element for the document with the given name.
|
|
|
|
// This is an undoable action
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// newtitle - String with the new title
|
2010-06-22 14:52:51 +00:00
|
|
|
this.setDocumentTitle = function(newtitle) {
|
2014-02-17 06:48:40 +00:00
|
|
|
var i;
|
2010-06-22 14:52:51 +00:00
|
|
|
var childs = svgcontent.childNodes, doc_title = false, old_title = '';
|
2009-10-19 19:06:07 +00:00
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
var batchCmd = new svgedit.history.BatchCommand('Change Image Title');
|
2009-10-19 19:06:07 +00:00
|
|
|
|
2014-02-17 06:48:40 +00:00
|
|
|
for (i = 0; i < childs.length; i++) {
|
2013-02-15 15:51:58 +00:00
|
|
|
if (childs[i].nodeName == 'title') {
|
2010-06-22 14:52:51 +00:00
|
|
|
doc_title = childs[i];
|
|
|
|
old_title = doc_title.textContent;
|
|
|
|
break;
|
2010-02-02 18:42:43 +00:00
|
|
|
}
|
|
|
|
}
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!doc_title) {
|
2014-04-08 03:21:22 +00:00
|
|
|
doc_title = svgdoc.createElementNS(NS.SVG, 'title');
|
2010-06-22 14:52:51 +00:00
|
|
|
svgcontent.insertBefore(doc_title, svgcontent.firstChild);
|
|
|
|
}
|
2010-02-02 18:42:43 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (newtitle.length) {
|
2010-06-22 14:52:51 +00:00
|
|
|
doc_title.textContent = newtitle;
|
|
|
|
} else {
|
|
|
|
// No title given, so element is not necessary
|
|
|
|
doc_title.parentNode.removeChild(doc_title);
|
|
|
|
}
|
2013-02-17 08:21:07 +00:00
|
|
|
batchCmd.addSubCommand(new svgedit.history.ChangeElementCommand(doc_title, {'#text': old_title}));
|
2010-06-22 14:52:51 +00:00
|
|
|
addCommandToHistory(batchCmd);
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2009-07-30 22:45:59 +00:00
|
|
|
|
2010-06-28 20:09:34 +00:00
|
|
|
// Function: getEditorNS
|
|
|
|
// Returns the editor's namespace URL, optionally adds it to root element
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// add - Boolean to indicate whether or not to add the namespace value
|
2010-06-22 14:52:51 +00:00
|
|
|
this.getEditorNS = function(add) {
|
2013-02-15 15:51:58 +00:00
|
|
|
if (add) {
|
2013-02-16 15:02:26 +00:00
|
|
|
svgcontent.setAttribute('xmlns:se', NS.SE);
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
2013-02-16 15:02:26 +00:00
|
|
|
return NS.SE;
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2009-09-14 00:04:53 +00:00
|
|
|
|
2010-06-28 20:09:34 +00:00
|
|
|
// Function: setResolution
|
|
|
|
// Changes the document's dimensions to the given size
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// x - Number with the width of the new dimensions in user units.
|
|
|
|
// Can also be the string "fit" to indicate "fit to content"
|
|
|
|
// y - Number with the height of the new dimensions in user units.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// Boolean to indicate if resolution change was succesful.
|
|
|
|
// It will fail on "fit to content" option with no content to fit to.
|
2010-06-22 14:52:51 +00:00
|
|
|
this.setResolution = function(x, y) {
|
2010-06-30 18:27:36 +00:00
|
|
|
var res = getResolution();
|
2010-06-22 14:52:51 +00:00
|
|
|
var w = res.w, h = res.h;
|
|
|
|
var batchCmd;
|
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (x == 'fit') {
|
2010-06-22 14:52:51 +00:00
|
|
|
// Get bounding box
|
2010-06-30 18:27:36 +00:00
|
|
|
var bbox = getStrokedBBox();
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (bbox) {
|
2014-04-08 03:21:22 +00:00
|
|
|
batchCmd = new svgedit.history.BatchCommand('Fit Canvas to Content');
|
2010-06-30 18:27:36 +00:00
|
|
|
var visEls = getVisibleElements();
|
|
|
|
addToSelection(visEls);
|
2010-06-22 14:52:51 +00:00
|
|
|
var dx = [], dy = [];
|
|
|
|
$.each(visEls, function(i, item) {
|
|
|
|
dx.push(bbox.x*-1);
|
|
|
|
dy.push(bbox.y*-1);
|
|
|
|
});
|
|
|
|
|
|
|
|
var cmd = canvas.moveSelectedElements(dx, dy, true);
|
|
|
|
batchCmd.addSubCommand(cmd);
|
2010-06-30 18:27:36 +00:00
|
|
|
clearSelection();
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
x = Math.round(bbox.width);
|
|
|
|
y = Math.round(bbox.height);
|
|
|
|
} else {
|
|
|
|
return false;
|
2009-09-14 00:04:53 +00:00
|
|
|
}
|
2010-01-18 19:14:08 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
if (x != w || y != h) {
|
|
|
|
var handle = svgroot.suspendRedraw(1000);
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!batchCmd) {
|
2014-04-08 03:21:22 +00:00
|
|
|
batchCmd = new svgedit.history.BatchCommand('Change Image Dimensions');
|
2009-09-23 15:29:55 +00:00
|
|
|
}
|
2010-10-27 18:15:28 +00:00
|
|
|
|
2013-02-14 15:19:46 +00:00
|
|
|
x = svgedit.units.convertToNum('width', x);
|
|
|
|
y = svgedit.units.convertToNum('height', y);
|
2009-09-16 14:12:51 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
svgcontent.setAttribute('width', x);
|
|
|
|
svgcontent.setAttribute('height', y);
|
2010-10-27 18:15:28 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
this.contentW = x;
|
|
|
|
this.contentH = y;
|
2014-04-08 03:21:22 +00:00
|
|
|
batchCmd.addSubCommand(new svgedit.history.ChangeElementCommand(svgcontent, {'width':w, 'height':h}));
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
svgcontent.setAttribute('viewBox', [0, 0, x/current_zoom, y/current_zoom].join(' '));
|
|
|
|
batchCmd.addSubCommand(new svgedit.history.ChangeElementCommand(svgcontent, {'viewBox': ['0 0', w, h].join(' ')}));
|
2009-09-23 15:29:55 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
addCommandToHistory(batchCmd);
|
|
|
|
svgroot.unsuspendRedraw(handle);
|
2014-04-08 03:21:22 +00:00
|
|
|
call('changed', [svgcontent]);
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2010-06-28 20:09:34 +00:00
|
|
|
// Function: getOffset
|
|
|
|
// Returns an object with x, y values indicating the svgcontent element's
|
|
|
|
// position in the editor's canvas.
|
2010-06-22 14:52:51 +00:00
|
|
|
this.getOffset = function() {
|
|
|
|
return $(svgcontent).attr(['x', 'y']);
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2010-06-28 20:09:34 +00:00
|
|
|
// Function: setBBoxZoom
|
|
|
|
// Sets the zoom level on the canvas-side based on the given value
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// val - Bounding box object to zoom to or string indicating zoom option
|
|
|
|
// editor_w - Integer with the editor's workarea box's width
|
|
|
|
// editor_h - Integer with the editor's workarea box's height
|
2010-06-22 14:52:51 +00:00
|
|
|
this.setBBoxZoom = function(val, editor_w, editor_h) {
|
2014-02-17 06:48:40 +00:00
|
|
|
var spacer = 0.85;
|
2010-06-22 14:52:51 +00:00
|
|
|
var bb;
|
|
|
|
var calcZoom = function(bb) {
|
2014-02-17 06:48:40 +00:00
|
|
|
if (!bb) {return false;}
|
2010-06-22 14:52:51 +00:00
|
|
|
var w_zoom = Math.round((editor_w / bb.width)*100 * spacer)/100;
|
|
|
|
var h_zoom = Math.round((editor_h / bb.height)*100 * spacer)/100;
|
2013-02-15 16:51:48 +00:00
|
|
|
var zoomlevel = Math.min(w_zoom, h_zoom);
|
2010-06-22 14:52:51 +00:00
|
|
|
canvas.setZoom(zoomlevel);
|
|
|
|
return {'zoom': zoomlevel, 'bbox': bb};
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (typeof val == 'object') {
|
2010-06-22 14:52:51 +00:00
|
|
|
bb = val;
|
2013-02-15 15:51:58 +00:00
|
|
|
if (bb.width == 0 || bb.height == 0) {
|
2014-02-17 06:48:40 +00:00
|
|
|
var newzoom = bb.zoom ? bb.zoom : current_zoom * bb.factor;
|
2010-06-22 14:52:51 +00:00
|
|
|
canvas.setZoom(newzoom);
|
|
|
|
return {'zoom': current_zoom, 'bbox': bb};
|
2009-09-16 14:12:51 +00:00
|
|
|
}
|
2009-09-23 15:29:55 +00:00
|
|
|
return calcZoom(bb);
|
2009-09-16 14:12:51 +00:00
|
|
|
}
|
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
switch (val) {
|
|
|
|
case 'selection':
|
2014-02-17 06:48:40 +00:00
|
|
|
if (!selectedElements[0]) {return;}
|
|
|
|
var sel_elems = $.map(selectedElements, function(n){ if (n) {return n;} });
|
2010-06-30 18:27:36 +00:00
|
|
|
bb = getStrokedBBox(sel_elems);
|
2010-06-22 14:52:51 +00:00
|
|
|
break;
|
|
|
|
case 'canvas':
|
2010-06-30 18:27:36 +00:00
|
|
|
var res = getResolution();
|
2014-02-17 06:48:40 +00:00
|
|
|
spacer = 0.95;
|
2013-02-15 16:51:48 +00:00
|
|
|
bb = {width:res.w, height:res.h , x:0, y:0};
|
2010-06-22 14:52:51 +00:00
|
|
|
break;
|
|
|
|
case 'content':
|
2010-06-30 18:27:36 +00:00
|
|
|
bb = getStrokedBBox();
|
2010-06-22 14:52:51 +00:00
|
|
|
break;
|
|
|
|
case 'layer':
|
2011-01-25 16:15:29 +00:00
|
|
|
bb = getStrokedBBox(getVisibleElements(getCurrentDrawing().getCurrentLayer()));
|
2010-06-22 14:52:51 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
2009-09-09 14:18:24 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
return calcZoom(bb);
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2009-09-09 14:18:24 +00:00
|
|
|
|
2010-06-28 20:09:34 +00:00
|
|
|
// Function: setZoom
|
|
|
|
// Sets the zoom to the given level
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// zoomlevel - Float indicating the zoom level to change to
|
2010-06-22 14:52:51 +00:00
|
|
|
this.setZoom = function(zoomlevel) {
|
2010-06-30 18:27:36 +00:00
|
|
|
var res = getResolution();
|
2014-04-08 03:21:22 +00:00
|
|
|
svgcontent.setAttribute('viewBox', '0 0 ' + res.w/zoomlevel + ' ' + res.h/zoomlevel);
|
2010-06-22 14:52:51 +00:00
|
|
|
current_zoom = zoomlevel;
|
|
|
|
$.each(selectedElements, function(i, elem) {
|
2014-02-17 06:48:40 +00:00
|
|
|
if (!elem) {return;}
|
2010-06-22 14:52:51 +00:00
|
|
|
selectorManager.requestSelector(elem).resize();
|
|
|
|
});
|
|
|
|
pathActions.zoomChange();
|
2014-04-08 03:21:22 +00:00
|
|
|
runExtensions('zoomChanged', zoomlevel);
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2009-06-06 12:25:26 +00:00
|
|
|
|
2010-06-28 20:09:34 +00:00
|
|
|
// Function: getMode
|
|
|
|
// Returns the current editor mode string
|
2010-06-22 14:52:51 +00:00
|
|
|
this.getMode = function() {
|
|
|
|
return current_mode;
|
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2010-06-28 20:09:34 +00:00
|
|
|
// Function: setMode
|
|
|
|
// Sets the editor's mode to the given string
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// name - String with the new mode to change to
|
2010-06-22 14:52:51 +00:00
|
|
|
this.setMode = function(name) {
|
|
|
|
pathActions.clear(true);
|
|
|
|
textActions.clear();
|
|
|
|
cur_properties = (selectedElements[0] && selectedElements[0].nodeName == 'text') ? cur_text : cur_shape;
|
|
|
|
current_mode = name;
|
|
|
|
};
|
2009-06-06 12:25:26 +00:00
|
|
|
|
2010-06-28 20:09:34 +00:00
|
|
|
// Group: Element Styling
|
|
|
|
|
|
|
|
// Function: getColor
|
|
|
|
// Returns the current fill/stroke option
|
|
|
|
this.getColor = function(type) {
|
|
|
|
return cur_properties[type];
|
2010-06-22 14:52:51 +00:00
|
|
|
};
|
2010-02-02 05:48:33 +00:00
|
|
|
|
2010-06-28 20:09:34 +00:00
|
|
|
// Function: setColor
|
|
|
|
// Change the current stroke/fill color/gradient value
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// type - String indicating fill or stroke
|
|
|
|
// val - The value to set the stroke attribute to
|
|
|
|
// preventUndo - Boolean indicating whether or not this should be and undoable option
|
|
|
|
this.setColor = function(type, val, preventUndo) {
|
|
|
|
cur_shape[type] = val;
|
2014-04-08 03:21:22 +00:00
|
|
|
cur_properties[type + '_paint'] = {type:'solidColor'};
|
2010-06-22 14:52:51 +00:00
|
|
|
var elems = [];
|
2014-02-17 06:48:40 +00:00
|
|
|
function addNonG (e) {
|
2014-04-08 03:21:22 +00:00
|
|
|
if (e.nodeName != 'g') {
|
2014-02-17 06:48:40 +00:00
|
|
|
elems.push(e);
|
|
|
|
}
|
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
var i = selectedElements.length;
|
|
|
|
while (i--) {
|
|
|
|
var elem = selectedElements[i];
|
|
|
|
if (elem) {
|
2014-04-08 03:21:22 +00:00
|
|
|
if (elem.tagName == 'g') {
|
2014-02-17 06:48:40 +00:00
|
|
|
svgedit.utilities.walkTree(elem, addNonG);
|
2013-02-15 15:51:58 +00:00
|
|
|
} else {
|
|
|
|
if (type == 'fill') {
|
2014-04-08 03:21:22 +00:00
|
|
|
if (elem.tagName != 'polyline' && elem.tagName != 'line') {
|
2010-06-28 20:09:34 +00:00
|
|
|
elems.push(elem);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
elems.push(elem);
|
|
|
|
}
|
|
|
|
}
|
2010-02-02 05:48:33 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
if (elems.length > 0) {
|
|
|
|
if (!preventUndo) {
|
2010-06-30 18:27:36 +00:00
|
|
|
changeSelectedAttribute(type, val, elems);
|
2014-04-08 03:21:22 +00:00
|
|
|
call('changed', elems);
|
2013-02-15 15:51:58 +00:00
|
|
|
} else {
|
2010-06-30 18:27:36 +00:00
|
|
|
changeSelectedAttributeNoUndo(type, val, elems);
|
2013-02-15 15:51:58 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2010-06-28 20:09:34 +00:00
|
|
|
// Function: setGradient
|
|
|
|
// Apply the current gradient to selected element's fill or stroke
|
|
|
|
//
|
|
|
|
// Parameters
|
|
|
|
// type - String indicating "fill" or "stroke" to apply to an element
|
|
|
|
var setGradient = this.setGradient = function(type) {
|
2014-04-08 03:21:22 +00:00
|
|
|
if (!cur_properties[type + '_paint'] || cur_properties[type + '_paint'].type == 'solidColor') {return;}
|
2010-06-28 20:09:34 +00:00
|
|
|
var grad = canvas[type + 'Grad'];
|
|
|
|
// find out if there is a duplicate gradient already in the defs
|
|
|
|
var duplicate_grad = findDuplicateGradient(grad);
|
2013-02-14 15:19:46 +00:00
|
|
|
var defs = svgedit.utilities.findDefs();
|
2010-06-28 20:09:34 +00:00
|
|
|
// no duplicate found, so import gradient into defs
|
|
|
|
if (!duplicate_grad) {
|
|
|
|
var orig_grad = grad;
|
|
|
|
grad = defs.appendChild( svgdoc.importNode(grad, true) );
|
|
|
|
// get next id and set it on the grad
|
|
|
|
grad.id = getNextId();
|
2013-02-15 15:51:58 +00:00
|
|
|
} else { // use existing gradient
|
2010-06-28 20:09:34 +00:00
|
|
|
grad = duplicate_grad;
|
|
|
|
}
|
2014-04-08 03:21:22 +00:00
|
|
|
canvas.setColor(type, 'url(#' + grad.id + ')');
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2010-06-28 20:09:34 +00:00
|
|
|
// Function: findDuplicateGradient
|
|
|
|
// Check if exact gradient already exists
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// grad - The gradient DOM element to compare to others
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// The existing gradient if found, null if not
|
2010-06-22 14:52:51 +00:00
|
|
|
var findDuplicateGradient = function(grad) {
|
2013-02-14 15:19:46 +00:00
|
|
|
var defs = svgedit.utilities.findDefs();
|
2014-04-08 03:21:22 +00:00
|
|
|
var existing_grads = $(defs).find('linearGradient, radialGradient');
|
2010-06-22 14:52:51 +00:00
|
|
|
var i = existing_grads.length;
|
2013-02-15 16:51:48 +00:00
|
|
|
var rad_attrs = ['r', 'cx', 'cy', 'fx', 'fy'];
|
2010-06-22 14:52:51 +00:00
|
|
|
while (i--) {
|
|
|
|
var og = existing_grads[i];
|
2014-04-08 03:21:22 +00:00
|
|
|
if (grad.tagName == 'linearGradient') {
|
2010-06-22 14:52:51 +00:00
|
|
|
if (grad.getAttribute('x1') != og.getAttribute('x1') ||
|
|
|
|
grad.getAttribute('y1') != og.getAttribute('y1') ||
|
|
|
|
grad.getAttribute('x2') != og.getAttribute('x2') ||
|
|
|
|
grad.getAttribute('y2') != og.getAttribute('y2'))
|
|
|
|
{
|
2009-07-30 22:45:59 +00:00
|
|
|
continue;
|
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
} else {
|
|
|
|
var grad_attrs = $(grad).attr(rad_attrs);
|
|
|
|
var og_attrs = $(og).attr(rad_attrs);
|
|
|
|
|
|
|
|
var diff = false;
|
|
|
|
$.each(rad_attrs, function(i, attr) {
|
2014-02-17 06:48:40 +00:00
|
|
|
if (grad_attrs[attr] != og_attrs[attr]) {diff = true;}
|
2010-06-22 14:52:51 +00:00
|
|
|
});
|
|
|
|
|
2014-02-17 06:48:40 +00:00
|
|
|
if (diff) {continue;}
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// else could be a duplicate, iterate through stops
|
2014-04-08 03:21:22 +00:00
|
|
|
var stops = grad.getElementsByTagNameNS(NS.SVG, 'stop');
|
|
|
|
var ostops = og.getElementsByTagNameNS(NS.SVG, 'stop');
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
if (stops.length != ostops.length) {
|
|
|
|
continue;
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
var j = stops.length;
|
2013-02-15 15:51:58 +00:00
|
|
|
while (j--) {
|
2010-06-22 14:52:51 +00:00
|
|
|
var stop = stops[j];
|
|
|
|
var ostop = ostops[j];
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
if (stop.getAttribute('offset') != ostop.getAttribute('offset') ||
|
|
|
|
stop.getAttribute('stop-opacity') != ostop.getAttribute('stop-opacity') ||
|
|
|
|
stop.getAttribute('stop-color') != ostop.getAttribute('stop-color'))
|
|
|
|
{
|
|
|
|
break;
|
2009-07-30 22:45:59 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
if (j == -1) {
|
|
|
|
return og;
|
|
|
|
}
|
|
|
|
} // for each gradient in defs
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
return null;
|
|
|
|
};
|
2009-09-04 06:30:18 +00:00
|
|
|
|
2010-12-21 19:36:51 +00:00
|
|
|
function reorientGrads(elem, m) {
|
2014-02-17 06:48:40 +00:00
|
|
|
var i;
|
2011-02-24 16:13:26 +00:00
|
|
|
var bb = svgedit.utilities.getBBox(elem);
|
2014-02-17 06:48:40 +00:00
|
|
|
for (i = 0; i < 2; i++) {
|
2010-12-21 19:36:51 +00:00
|
|
|
var type = i === 0 ? 'fill' : 'stroke';
|
|
|
|
var attrVal = elem.getAttribute(type);
|
2013-02-15 15:51:58 +00:00
|
|
|
if (attrVal && attrVal.indexOf('url(') === 0) {
|
2013-02-14 15:19:46 +00:00
|
|
|
var grad = svgedit.utilities.getRefElem(attrVal);
|
2013-02-15 15:51:58 +00:00
|
|
|
if (grad.tagName === 'linearGradient') {
|
2010-12-21 19:36:51 +00:00
|
|
|
var x1 = grad.getAttribute('x1') || 0;
|
|
|
|
var y1 = grad.getAttribute('y1') || 0;
|
|
|
|
var x2 = grad.getAttribute('x2') || 1;
|
|
|
|
var y2 = grad.getAttribute('y2') || 0;
|
|
|
|
|
|
|
|
// Convert to USOU points
|
|
|
|
x1 = (bb.width * x1) + bb.x;
|
|
|
|
y1 = (bb.height * y1) + bb.y;
|
|
|
|
x2 = (bb.width * x2) + bb.x;
|
|
|
|
y2 = (bb.height * y2) + bb.y;
|
|
|
|
|
|
|
|
// Transform those points
|
2013-02-14 15:19:46 +00:00
|
|
|
var pt1 = svgedit.math.transformPoint(x1, y1, m);
|
|
|
|
var pt2 = svgedit.math.transformPoint(x2, y2, m);
|
2010-12-21 19:36:51 +00:00
|
|
|
|
|
|
|
// Convert back to BB points
|
|
|
|
var g_coords = {};
|
|
|
|
|
|
|
|
g_coords.x1 = (pt1.x - bb.x) / bb.width;
|
|
|
|
g_coords.y1 = (pt1.y - bb.y) / bb.height;
|
|
|
|
g_coords.x2 = (pt2.x - bb.x) / bb.width;
|
|
|
|
g_coords.y2 = (pt2.y - bb.y) / bb.height;
|
|
|
|
|
|
|
|
var newgrad = grad.cloneNode(true);
|
|
|
|
$(newgrad).attr(g_coords);
|
|
|
|
|
|
|
|
newgrad.id = getNextId();
|
2013-02-14 15:19:46 +00:00
|
|
|
svgedit.utilities.findDefs().appendChild(newgrad);
|
2010-12-21 19:36:51 +00:00
|
|
|
elem.setAttribute(type, 'url(#' + newgrad.id + ')');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-28 20:09:34 +00:00
|
|
|
// Function: setPaint
|
|
|
|
// Set a color/gradient to a fill/stroke
|
|
|
|
//
|
2013-02-25 13:05:25 +00:00
|
|
|
// Parameters:
|
2010-06-28 20:09:34 +00:00
|
|
|
// type - String with "fill" or "stroke"
|
|
|
|
// paint - The jGraduate paint object to apply
|
|
|
|
this.setPaint = function(type, paint) {
|
2010-06-22 14:52:51 +00:00
|
|
|
// make a copy
|
2010-06-28 20:09:34 +00:00
|
|
|
var p = new $.jGraduate.Paint(paint);
|
2013-02-25 13:05:25 +00:00
|
|
|
this.setPaintOpacity(type, p.alpha / 100, true);
|
2009-09-04 06:30:18 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// now set the current paint object
|
2010-06-28 20:09:34 +00:00
|
|
|
cur_properties[type + '_paint'] = p;
|
2013-02-25 13:05:25 +00:00
|
|
|
switch (p.type) {
|
|
|
|
case 'solidColor':
|
|
|
|
this.setColor(type, p.solidColor != 'none' ? '#' + p.solidColor : 'none');
|
2010-06-28 20:09:34 +00:00
|
|
|
break;
|
2013-02-25 13:05:25 +00:00
|
|
|
case 'linearGradient':
|
|
|
|
case 'radialGradient':
|
2010-06-28 20:09:34 +00:00
|
|
|
canvas[type + 'Grad'] = p[p.type];
|
|
|
|
setGradient(type);
|
|
|
|
break;
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
};
|
2009-07-30 22:45:59 +00:00
|
|
|
|
2013-02-25 13:05:25 +00:00
|
|
|
// alias
|
|
|
|
this.setStrokePaint = function(paint) {
|
|
|
|
this.setPaint('stroke', paint);
|
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2013-02-25 13:05:25 +00:00
|
|
|
this.setFillPaint = function(paint) {
|
|
|
|
this.setPaint('fill', paint);
|
|
|
|
};
|
2010-06-28 20:09:34 +00:00
|
|
|
|
|
|
|
// Function: getStrokeWidth
|
|
|
|
// Returns the current stroke-width value
|
2010-06-22 14:52:51 +00:00
|
|
|
this.getStrokeWidth = function() {
|
|
|
|
return cur_properties.stroke_width;
|
|
|
|
};
|
|
|
|
|
2010-06-28 20:09:34 +00:00
|
|
|
// Function: setStrokeWidth
|
|
|
|
// Sets the stroke width for the current selected elements
|
|
|
|
// When attempting to set a line's width to 0, this changes it to 1 instead
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// val - A Float indicating the new stroke width value
|
2010-06-22 14:52:51 +00:00
|
|
|
this.setStrokeWidth = function(val) {
|
2013-02-15 15:51:58 +00:00
|
|
|
if (val == 0 && ['line', 'path'].indexOf(current_mode) >= 0) {
|
2010-06-22 14:52:51 +00:00
|
|
|
canvas.setStrokeWidth(1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cur_properties.stroke_width = val;
|
2013-02-25 13:05:25 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
var elems = [];
|
2014-02-17 06:48:40 +00:00
|
|
|
function addNonG (e) {
|
|
|
|
if (e.nodeName != 'g') {
|
|
|
|
elems.push(e);
|
|
|
|
}
|
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
var i = selectedElements.length;
|
|
|
|
while (i--) {
|
|
|
|
var elem = selectedElements[i];
|
|
|
|
if (elem) {
|
2014-04-08 03:21:22 +00:00
|
|
|
if (elem.tagName == 'g') {
|
2014-02-17 06:48:40 +00:00
|
|
|
svgedit.utilities.walkTree(elem, addNonG);
|
|
|
|
}
|
|
|
|
else {
|
2010-06-22 14:52:51 +00:00
|
|
|
elems.push(elem);
|
2014-02-17 06:48:40 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
2013-02-25 13:05:25 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
if (elems.length > 0) {
|
2014-04-08 03:21:22 +00:00
|
|
|
changeSelectedAttribute('stroke-width', val, elems);
|
|
|
|
call('changed', selectedElements);
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
};
|
2009-06-06 12:25:26 +00:00
|
|
|
|
2010-06-28 20:09:34 +00:00
|
|
|
// Function: setStrokeAttr
|
|
|
|
// Set the given stroke-related attribute the given value for selected elements
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// attr - String with the attribute name
|
|
|
|
// val - String or number with the attribute value
|
2010-06-22 14:52:51 +00:00
|
|
|
this.setStrokeAttr = function(attr, val) {
|
2013-02-15 16:51:48 +00:00
|
|
|
cur_shape[attr.replace('-', '_')] = val;
|
2010-06-22 14:52:51 +00:00
|
|
|
var elems = [];
|
2014-02-17 06:48:40 +00:00
|
|
|
function addNonG (e) {
|
|
|
|
if (e.nodeName != 'g') {
|
|
|
|
elems.push(e);
|
|
|
|
}
|
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
var i = selectedElements.length;
|
|
|
|
while (i--) {
|
|
|
|
var elem = selectedElements[i];
|
|
|
|
if (elem) {
|
2014-04-08 03:21:22 +00:00
|
|
|
if (elem.tagName == 'g') {
|
|
|
|
svgedit.utilities.walkTree(elem, function(e){if (e.nodeName!='g') {elems.push(e);}});
|
2014-02-17 06:48:40 +00:00
|
|
|
}
|
|
|
|
else {
|
2010-06-22 14:52:51 +00:00
|
|
|
elems.push(elem);
|
2014-02-17 06:48:40 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
2013-02-25 13:05:25 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
if (elems.length > 0) {
|
2010-06-30 18:27:36 +00:00
|
|
|
changeSelectedAttribute(attr, val, elems);
|
2014-04-08 03:21:22 +00:00
|
|
|
call('changed', selectedElements);
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-07-09 19:09:37 +00:00
|
|
|
// Function: getStyle
|
|
|
|
// Returns current style options
|
|
|
|
this.getStyle = function() {
|
|
|
|
return cur_shape;
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-07-09 19:09:37 +00:00
|
|
|
|
2010-06-28 20:09:34 +00:00
|
|
|
// Function: getOpacity
|
|
|
|
// Returns the current opacity
|
2010-06-22 14:52:51 +00:00
|
|
|
this.getOpacity = function() {
|
|
|
|
return cur_shape.opacity;
|
|
|
|
};
|
|
|
|
|
2010-06-28 20:09:34 +00:00
|
|
|
// Function: setOpacity
|
|
|
|
// Sets the given opacity to the current selected elements
|
2010-06-22 14:52:51 +00:00
|
|
|
this.setOpacity = function(val) {
|
|
|
|
cur_shape.opacity = val;
|
2014-04-08 03:21:22 +00:00
|
|
|
changeSelectedAttribute('opacity', val);
|
2010-06-22 14:52:51 +00:00
|
|
|
};
|
2009-06-04 13:43:58 +00:00
|
|
|
|
2010-06-28 20:09:34 +00:00
|
|
|
// Function: getOpacity
|
|
|
|
// Returns the current fill opacity
|
|
|
|
this.getFillOpacity = function() {
|
|
|
|
return cur_shape.fill_opacity;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Function: getStrokeOpacity
|
|
|
|
// Returns the current stroke opacity
|
|
|
|
this.getStrokeOpacity = function() {
|
|
|
|
return cur_shape.stroke_opacity;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Function: setPaintOpacity
|
|
|
|
// Sets the current fill/stroke opacity
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// type - String with "fill" or "stroke"
|
|
|
|
// val - Float with the new opacity value
|
|
|
|
// preventUndo - Boolean indicating whether or not this should be an undoable action
|
|
|
|
this.setPaintOpacity = function(type, val, preventUndo) {
|
|
|
|
cur_shape[type + '_opacity'] = val;
|
2014-02-17 06:48:40 +00:00
|
|
|
if (!preventUndo) {
|
2014-04-08 03:21:22 +00:00
|
|
|
changeSelectedAttribute(type + '-opacity', val);
|
2014-02-17 06:48:40 +00:00
|
|
|
}
|
|
|
|
else {
|
2014-04-08 03:21:22 +00:00
|
|
|
changeSelectedAttributeNoUndo(type + '-opacity', val);
|
2014-02-17 06:48:40 +00:00
|
|
|
}
|
2010-06-28 20:09:34 +00:00
|
|
|
};
|
|
|
|
|
2013-02-19 13:17:27 +00:00
|
|
|
// Function: getPaintOpacity
|
|
|
|
// Gets the current fill/stroke opacity
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// type - String with "fill" or "stroke"
|
|
|
|
this.getPaintOpacity = function(type) {
|
|
|
|
return type === 'fill' ? this.getFillOpacity() : this.getStrokeOpacity();
|
|
|
|
};
|
|
|
|
|
2010-06-29 20:43:44 +00:00
|
|
|
// Function: getBlur
|
|
|
|
// Gets the stdDeviation blur value of the given element
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// elem - The element to check the blur value for
|
2010-06-22 14:52:51 +00:00
|
|
|
this.getBlur = function(elem) {
|
|
|
|
var val = 0;
|
2013-02-19 13:17:27 +00:00
|
|
|
// var elem = selectedElements[0];
|
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (elem) {
|
2010-06-22 14:52:51 +00:00
|
|
|
var filter_url = elem.getAttribute('filter');
|
2013-02-15 15:51:58 +00:00
|
|
|
if (filter_url) {
|
2013-02-14 15:19:46 +00:00
|
|
|
var blur = svgedit.utilities.getElem(elem.id + '_blur');
|
2013-02-15 15:51:58 +00:00
|
|
|
if (blur) {
|
2010-06-22 14:52:51 +00:00
|
|
|
val = blur.firstChild.getAttribute('stdDeviation');
|
2010-04-06 18:40:10 +00:00
|
|
|
}
|
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
return val;
|
|
|
|
};
|
2010-04-06 18:40:10 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
(function() {
|
|
|
|
var cur_command = null;
|
|
|
|
var filter = null;
|
|
|
|
var filterHidden = false;
|
|
|
|
|
2010-06-29 20:43:44 +00:00
|
|
|
// Function: setBlurNoUndo
|
|
|
|
// Sets the stdDeviation blur value on the selected element without being undoable
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// val - The new stdDeviation value
|
2010-06-22 14:52:51 +00:00
|
|
|
canvas.setBlurNoUndo = function(val) {
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!filter) {
|
2010-06-22 14:52:51 +00:00
|
|
|
canvas.setBlur(val);
|
|
|
|
return;
|
2010-04-28 16:34:02 +00:00
|
|
|
}
|
2013-02-15 15:51:58 +00:00
|
|
|
if (val === 0) {
|
2010-06-22 14:52:51 +00:00
|
|
|
// Don't change the StdDev, as that will hide the element.
|
|
|
|
// Instead, just remove the value for "filter"
|
2014-04-08 03:21:22 +00:00
|
|
|
changeSelectedAttributeNoUndo('filter', '');
|
2010-06-22 14:52:51 +00:00
|
|
|
filterHidden = true;
|
|
|
|
} else {
|
2010-10-27 18:58:34 +00:00
|
|
|
var elem = selectedElements[0];
|
2013-02-15 15:51:58 +00:00
|
|
|
if (filterHidden) {
|
2014-04-08 03:21:22 +00:00
|
|
|
changeSelectedAttributeNoUndo('filter', 'url(#' + elem.id + '_blur)');
|
2010-10-27 18:58:34 +00:00
|
|
|
}
|
2013-02-15 15:51:58 +00:00
|
|
|
if (svgedit.browser.isWebkit()) {
|
2010-10-27 18:58:34 +00:00
|
|
|
console.log('e', elem);
|
|
|
|
elem.removeAttribute('filter');
|
|
|
|
elem.setAttribute('filter', 'url(#' + elem.id + '_blur)');
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
2014-04-08 03:21:22 +00:00
|
|
|
changeSelectedAttributeNoUndo('stdDeviation', val, [filter.firstChild]);
|
2010-06-22 14:52:51 +00:00
|
|
|
canvas.setBlurOffsets(filter, val);
|
2010-04-28 16:34:02 +00:00
|
|
|
}
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-04-28 16:34:02 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
function finishChange() {
|
2010-11-12 19:08:29 +00:00
|
|
|
var bCmd = canvas.undoMgr.finishUndoableChange();
|
2010-06-22 14:52:51 +00:00
|
|
|
cur_command.addSubCommand(bCmd);
|
|
|
|
addCommandToHistory(cur_command);
|
|
|
|
cur_command = null;
|
|
|
|
filter = null;
|
|
|
|
}
|
|
|
|
|
2010-06-29 20:43:44 +00:00
|
|
|
// Function: setBlurOffsets
|
|
|
|
// Sets the x, y, with, height values of the filter element in order to
|
|
|
|
// make the blur not be clipped. Removes them if not neeeded
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// filter - The filter DOM element to update
|
|
|
|
// stdDev - The standard deviation value on which to base the offset size
|
2010-06-22 14:52:51 +00:00
|
|
|
canvas.setBlurOffsets = function(filter, stdDev) {
|
2013-02-15 15:51:58 +00:00
|
|
|
if (stdDev > 3) {
|
2010-06-22 14:52:51 +00:00
|
|
|
// TODO: Create algorithm here where size is based on expected blur
|
2013-02-17 04:58:04 +00:00
|
|
|
svgedit.utilities.assignAttributes(filter, {
|
2010-06-22 14:52:51 +00:00
|
|
|
x: '-50%',
|
|
|
|
y: '-50%',
|
|
|
|
width: '200%',
|
2010-10-28 16:47:39 +00:00
|
|
|
height: '200%'
|
2010-06-22 14:52:51 +00:00
|
|
|
}, 100);
|
|
|
|
} else {
|
2010-10-27 18:58:34 +00:00
|
|
|
// Removing these attributes hides text in Chrome (see Issue 579)
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!svgedit.browser.isWebkit()) {
|
2010-10-27 18:58:34 +00:00
|
|
|
filter.removeAttribute('x');
|
|
|
|
filter.removeAttribute('y');
|
|
|
|
filter.removeAttribute('width');
|
|
|
|
filter.removeAttribute('height');
|
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2010-06-29 20:43:44 +00:00
|
|
|
// Function: setBlur
|
|
|
|
// Adds/updates the blur filter to the selected element
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// val - Float with the new stdDeviation blur value
|
|
|
|
// complete - Boolean indicating whether or not the action should be completed (to add to the undo manager)
|
2010-06-22 14:52:51 +00:00
|
|
|
canvas.setBlur = function(val, complete) {
|
2013-02-15 15:51:58 +00:00
|
|
|
if (cur_command) {
|
2010-06-22 14:52:51 +00:00
|
|
|
finishChange();
|
|
|
|
return;
|
2010-05-28 19:17:30 +00:00
|
|
|
}
|
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// Looks for associated blur, creates one if not found
|
|
|
|
var elem = selectedElements[0];
|
|
|
|
var elem_id = elem.id;
|
2013-02-14 15:19:46 +00:00
|
|
|
filter = svgedit.utilities.getElem(elem_id + '_blur');
|
2010-04-28 16:34:02 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
val -= 0;
|
|
|
|
|
2013-02-17 08:21:07 +00:00
|
|
|
var batchCmd = new svgedit.history.BatchCommand();
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
// Blur found!
|
2013-02-15 15:51:58 +00:00
|
|
|
if (filter) {
|
|
|
|
if (val === 0) {
|
2010-06-22 14:52:51 +00:00
|
|
|
filter = null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Not found, so create
|
2014-04-08 03:21:22 +00:00
|
|
|
var newblur = addSvgElementFromJson({ 'element': 'feGaussianBlur',
|
|
|
|
'attr': {
|
|
|
|
'in': 'SourceGraphic',
|
|
|
|
'stdDeviation': val
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
});
|
2010-04-28 16:34:02 +00:00
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
filter = addSvgElementFromJson({ 'element': 'filter',
|
|
|
|
'attr': {
|
|
|
|
'id': elem_id + '_blur'
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
});
|
2010-04-28 16:34:02 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
filter.appendChild(newblur);
|
2013-02-14 15:19:46 +00:00
|
|
|
svgedit.utilities.findDefs().appendChild(filter);
|
2010-04-28 16:34:02 +00:00
|
|
|
|
2013-02-17 08:21:07 +00:00
|
|
|
batchCmd.addSubCommand(new svgedit.history.InsertElementCommand(filter));
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var changes = {filter: elem.getAttribute('filter')};
|
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (val === 0) {
|
2014-04-08 03:21:22 +00:00
|
|
|
elem.removeAttribute('filter');
|
2013-02-17 08:21:07 +00:00
|
|
|
batchCmd.addSubCommand(new svgedit.history.ChangeElementCommand(elem, changes));
|
2010-06-22 14:52:51 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-02-15 15:51:58 +00:00
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
changeSelectedAttribute('filter', 'url(#' + elem_id + '_blur)');
|
2013-02-17 08:21:07 +00:00
|
|
|
batchCmd.addSubCommand(new svgedit.history.ChangeElementCommand(elem, changes));
|
2013-02-15 15:51:58 +00:00
|
|
|
canvas.setBlurOffsets(filter, val);
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
cur_command = batchCmd;
|
2014-04-08 03:21:22 +00:00
|
|
|
canvas.undoMgr.beginUndoableChange('stdDeviation', [filter?filter.firstChild:null]);
|
2013-02-15 15:51:58 +00:00
|
|
|
if (complete) {
|
2010-06-22 14:52:51 +00:00
|
|
|
canvas.setBlurNoUndo(val);
|
|
|
|
finishChange();
|
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2010-06-22 14:52:51 +00:00
|
|
|
}());
|
2009-06-06 12:25:26 +00:00
|
|
|
|
2010-06-29 20:43:44 +00:00
|
|
|
// Function: getBold
|
|
|
|
// Check whether selected element is bold or not
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// Boolean indicating whether or not element is bold
|
2010-06-22 14:52:51 +00:00
|
|
|
this.getBold = function() {
|
|
|
|
// should only have one element selected
|
|
|
|
var selected = selectedElements[0];
|
2014-04-08 03:21:22 +00:00
|
|
|
if (selected != null && selected.tagName == 'text' &&
|
2010-06-22 14:52:51 +00:00
|
|
|
selectedElements[1] == null)
|
|
|
|
{
|
2014-04-08 03:21:22 +00:00
|
|
|
return (selected.getAttribute('font-weight') == 'bold');
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2010-06-29 20:43:44 +00:00
|
|
|
// Function: setBold
|
|
|
|
// Make the selected element bold or normal
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// b - Boolean indicating bold (true) or normal (false)
|
2010-06-22 14:52:51 +00:00
|
|
|
this.setBold = function(b) {
|
|
|
|
var selected = selectedElements[0];
|
2014-04-08 03:21:22 +00:00
|
|
|
if (selected != null && selected.tagName == 'text' &&
|
2010-06-22 14:52:51 +00:00
|
|
|
selectedElements[1] == null)
|
|
|
|
{
|
2014-04-08 03:21:22 +00:00
|
|
|
changeSelectedAttribute('font-weight', b ? 'bold' : 'normal');
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!selectedElements[0].textContent) {
|
2010-07-20 13:35:11 +00:00
|
|
|
textActions.setCursor();
|
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2010-06-29 20:43:44 +00:00
|
|
|
// Function: getItalic
|
|
|
|
// Check whether selected element is italic or not
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// Boolean indicating whether or not element is italic
|
2010-06-22 14:52:51 +00:00
|
|
|
this.getItalic = function() {
|
|
|
|
var selected = selectedElements[0];
|
2014-04-08 03:21:22 +00:00
|
|
|
if (selected != null && selected.tagName == 'text' &&
|
2010-06-22 14:52:51 +00:00
|
|
|
selectedElements[1] == null)
|
|
|
|
{
|
2014-04-08 03:21:22 +00:00
|
|
|
return (selected.getAttribute('font-style') == 'italic');
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2010-06-29 20:43:44 +00:00
|
|
|
// Function: setItalic
|
|
|
|
// Make the selected element italic or normal
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// b - Boolean indicating italic (true) or normal (false)
|
2010-06-22 14:52:51 +00:00
|
|
|
this.setItalic = function(i) {
|
|
|
|
var selected = selectedElements[0];
|
2014-04-08 03:21:22 +00:00
|
|
|
if (selected != null && selected.tagName == 'text' &&
|
2010-06-22 14:52:51 +00:00
|
|
|
selectedElements[1] == null)
|
|
|
|
{
|
2014-04-08 03:21:22 +00:00
|
|
|
changeSelectedAttribute('font-style', i ? 'italic' : 'normal');
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!selectedElements[0].textContent) {
|
2010-07-20 13:35:11 +00:00
|
|
|
textActions.setCursor();
|
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2010-06-29 20:43:44 +00:00
|
|
|
// Function: getFontFamily
|
|
|
|
// Returns the current font family
|
2010-06-22 14:52:51 +00:00
|
|
|
this.getFontFamily = function() {
|
|
|
|
return cur_text.font_family;
|
|
|
|
};
|
2009-06-12 06:45:37 +00:00
|
|
|
|
2010-06-29 20:43:44 +00:00
|
|
|
// Function: setFontFamily
|
|
|
|
// Set the new font family
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// val - String with the new font family
|
2010-06-22 14:52:51 +00:00
|
|
|
this.setFontFamily = function(val) {
|
|
|
|
cur_text.font_family = val;
|
2014-04-08 03:21:22 +00:00
|
|
|
changeSelectedAttribute('font-family', val);
|
2013-02-15 15:51:58 +00:00
|
|
|
if (selectedElements[0] && !selectedElements[0].textContent) {
|
2010-07-20 13:35:11 +00:00
|
|
|
textActions.setCursor();
|
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
};
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2011-03-09 18:55:48 +00:00
|
|
|
|
|
|
|
// Function: setFontColor
|
|
|
|
// Set the new font color
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// val - String with the new font color
|
|
|
|
this.setFontColor = function(val) {
|
|
|
|
cur_text.fill = val;
|
2014-04-08 03:21:22 +00:00
|
|
|
changeSelectedAttribute('fill', val);
|
2011-03-09 18:55:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Function: getFontColor
|
|
|
|
// Returns the current font color
|
2013-02-19 20:27:53 +00:00
|
|
|
this.getFontColor = function() {
|
2011-03-09 18:55:48 +00:00
|
|
|
return cur_text.fill;
|
|
|
|
};
|
|
|
|
|
2010-06-29 20:43:44 +00:00
|
|
|
// Function: getFontSize
|
|
|
|
// Returns the current font size
|
2010-06-22 14:52:51 +00:00
|
|
|
this.getFontSize = function() {
|
|
|
|
return cur_text.font_size;
|
|
|
|
};
|
2009-06-10 03:12:19 +00:00
|
|
|
|
2010-06-29 20:43:44 +00:00
|
|
|
// Function: setFontSize
|
|
|
|
// Applies the given font size to the selected element
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// val - Float with the new font size
|
2010-06-22 14:52:51 +00:00
|
|
|
this.setFontSize = function(val) {
|
|
|
|
cur_text.font_size = val;
|
2014-04-08 03:21:22 +00:00
|
|
|
changeSelectedAttribute('font-size', val);
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!selectedElements[0].textContent) {
|
2010-07-20 13:35:11 +00:00
|
|
|
textActions.setCursor();
|
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
};
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2010-06-29 20:43:44 +00:00
|
|
|
// Function: getText
|
|
|
|
// Returns the current text (textContent) of the selected element
|
2010-06-22 14:52:51 +00:00
|
|
|
this.getText = function() {
|
|
|
|
var selected = selectedElements[0];
|
2014-04-08 03:21:22 +00:00
|
|
|
if (selected == null) { return ''; }
|
2010-06-22 14:52:51 +00:00
|
|
|
return selected.textContent;
|
|
|
|
};
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2010-06-29 20:43:44 +00:00
|
|
|
// Function: setTextContent
|
|
|
|
// Updates the text element with the given string
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// val - String with the new text
|
2010-06-22 14:52:51 +00:00
|
|
|
this.setTextContent = function(val) {
|
2014-04-08 03:21:22 +00:00
|
|
|
changeSelectedAttribute('#text', val);
|
2010-06-22 14:52:51 +00:00
|
|
|
textActions.init(val);
|
|
|
|
textActions.setCursor();
|
|
|
|
};
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2010-06-29 20:43:44 +00:00
|
|
|
// Function: setImageURL
|
|
|
|
// Sets the new image URL for the selected image element. Updates its size if
|
|
|
|
// a new URL is given
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// val - String with the image URL/path
|
2010-06-22 14:52:51 +00:00
|
|
|
this.setImageURL = function(val) {
|
|
|
|
var elem = selectedElements[0];
|
2014-02-17 06:48:40 +00:00
|
|
|
if (!elem) {return;}
|
2010-04-12 15:59:46 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
var attrs = $(elem).attr(['width', 'height']);
|
|
|
|
var setsize = (!attrs.width || !attrs.height);
|
2010-04-09 16:01:09 +00:00
|
|
|
|
2010-08-16 17:53:15 +00:00
|
|
|
var cur_href = getHref(elem);
|
2010-04-09 16:01:09 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// Do nothing if no URL change or size change
|
2013-02-15 15:51:58 +00:00
|
|
|
if (cur_href !== val) {
|
2010-06-22 14:52:51 +00:00
|
|
|
setsize = true;
|
2014-02-17 06:48:40 +00:00
|
|
|
} else if (!setsize) {return;}
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
var batchCmd = new svgedit.history.BatchCommand('Change Image URL');
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2010-08-16 17:53:15 +00:00
|
|
|
setHref(elem, val);
|
2013-02-17 08:21:07 +00:00
|
|
|
batchCmd.addSubCommand(new svgedit.history.ChangeElementCommand(elem, {
|
2014-04-08 03:21:22 +00:00
|
|
|
'#href': cur_href
|
2010-06-22 14:52:51 +00:00
|
|
|
}));
|
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (setsize) {
|
2010-06-22 14:52:51 +00:00
|
|
|
$(new Image()).load(function() {
|
|
|
|
var changes = $(elem).attr(['width', 'height']);
|
|
|
|
|
|
|
|
$(elem).attr({
|
|
|
|
width: this.width,
|
|
|
|
height: this.height
|
|
|
|
});
|
2010-04-09 16:01:09 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
selectorManager.requestSelector(elem).resize();
|
|
|
|
|
2013-02-17 08:21:07 +00:00
|
|
|
batchCmd.addSubCommand(new svgedit.history.ChangeElementCommand(elem, changes));
|
2010-04-09 16:01:09 +00:00
|
|
|
addCommandToHistory(batchCmd);
|
2014-04-08 03:21:22 +00:00
|
|
|
call('changed', [elem]);
|
2013-02-15 16:51:48 +00:00
|
|
|
}).attr('src', val);
|
2010-06-22 14:52:51 +00:00
|
|
|
} else {
|
|
|
|
addCommandToHistory(batchCmd);
|
|
|
|
}
|
|
|
|
};
|
2009-09-04 22:22:40 +00:00
|
|
|
|
2010-12-01 17:54:11 +00:00
|
|
|
// Function: setLinkURL
|
|
|
|
// Sets the new link URL for the selected anchor element.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// val - String with the link URL/path
|
|
|
|
this.setLinkURL = function(val) {
|
|
|
|
var elem = selectedElements[0];
|
2014-02-17 06:48:40 +00:00
|
|
|
if (!elem) {return;}
|
2013-02-15 15:51:58 +00:00
|
|
|
if (elem.tagName !== 'a') {
|
2010-12-01 17:54:11 +00:00
|
|
|
// See if parent is an anchor
|
|
|
|
var parents_a = $(elem).parents('a');
|
2013-02-15 15:51:58 +00:00
|
|
|
if (parents_a.length) {
|
2010-12-01 17:54:11 +00:00
|
|
|
elem = parents_a[0];
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var cur_href = getHref(elem);
|
|
|
|
|
2014-02-17 06:48:40 +00:00
|
|
|
if (cur_href === val) {return;}
|
2010-12-01 17:54:11 +00:00
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
var batchCmd = new svgedit.history.BatchCommand('Change Link URL');
|
2010-12-01 17:54:11 +00:00
|
|
|
|
|
|
|
setHref(elem, val);
|
2013-02-17 08:21:07 +00:00
|
|
|
batchCmd.addSubCommand(new svgedit.history.ChangeElementCommand(elem, {
|
2014-04-08 03:21:22 +00:00
|
|
|
'#href': cur_href
|
2010-12-01 17:54:11 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
addCommandToHistory(batchCmd);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-06-29 20:43:44 +00:00
|
|
|
// Function: setRectRadius
|
|
|
|
// Sets the rx & ry values to the selected rect element to change its corner radius
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// val - The new radius
|
2010-06-22 14:52:51 +00:00
|
|
|
this.setRectRadius = function(val) {
|
|
|
|
var selected = selectedElements[0];
|
2014-04-08 03:21:22 +00:00
|
|
|
if (selected != null && selected.tagName == 'rect') {
|
|
|
|
var r = selected.getAttribute('rx');
|
2010-06-22 14:52:51 +00:00
|
|
|
if (r != val) {
|
2014-04-08 03:21:22 +00:00
|
|
|
selected.setAttribute('rx', val);
|
|
|
|
selected.setAttribute('ry', val);
|
|
|
|
addCommandToHistory(new svgedit.history.ChangeElementCommand(selected, {'rx':r, 'ry':r}, 'Radius'));
|
|
|
|
call('changed', [selected]);
|
2009-06-11 15:18:46 +00:00
|
|
|
}
|
2009-08-28 12:37:52 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
};
|
2009-06-29 14:38:30 +00:00
|
|
|
|
2010-12-01 17:54:11 +00:00
|
|
|
// Function: makeHyperlink
|
|
|
|
// Wraps the selected element(s) in an anchor element or converts group to one
|
|
|
|
this.makeHyperlink = function(url) {
|
|
|
|
canvas.groupSelectedElements('a', url);
|
|
|
|
|
|
|
|
// TODO: If element is a single "g", convert to "a"
|
2013-02-15 15:51:58 +00:00
|
|
|
// if (selectedElements.length > 1 && selectedElements[1]) {
|
2010-12-01 17:54:11 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-12-01 17:54:11 +00:00
|
|
|
|
|
|
|
// Function: removeHyperlink
|
|
|
|
this.removeHyperlink = function() {
|
|
|
|
canvas.ungroupSelectedElement();
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-12-01 17:54:11 +00:00
|
|
|
|
2010-06-30 18:27:36 +00:00
|
|
|
// Group: Element manipulation
|
|
|
|
|
2010-06-29 20:43:44 +00:00
|
|
|
// Function: setSegType
|
|
|
|
// Sets the new segment type to the selected segment(s).
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// new_type - Integer with the new segment type
|
|
|
|
// See http://www.w3.org/TR/SVG/paths.html#InterfaceSVGPathSeg for list
|
2010-06-22 14:52:51 +00:00
|
|
|
this.setSegType = function(new_type) {
|
|
|
|
pathActions.setSegType(new_type);
|
2014-02-17 06:48:40 +00:00
|
|
|
};
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2010-11-15 09:25:49 +00:00
|
|
|
// TODO(codedread): Remove the getBBox argument and split this function into two.
|
2010-06-30 18:27:36 +00:00
|
|
|
// Function: convertToPath
|
|
|
|
// Convert selected element to a path, or get the BBox of an element-as-path
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// elem - The DOM element to be converted
|
|
|
|
// getBBox - Boolean on whether or not to only return the path's BBox
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// If the getBBox flag is true, the resulting path's bounding box object.
|
|
|
|
// Otherwise the resulting path element is returned.
|
|
|
|
this.convertToPath = function(elem, getBBox) {
|
2013-02-15 15:51:58 +00:00
|
|
|
if (elem == null) {
|
2010-06-30 18:27:36 +00:00
|
|
|
var elems = selectedElements;
|
|
|
|
$.each(selectedElements, function(i, elem) {
|
2014-02-17 06:48:40 +00:00
|
|
|
if (elem) {canvas.convertToPath(elem);}
|
2010-06-30 18:27:36 +00:00
|
|
|
});
|
|
|
|
return;
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
2010-06-30 18:27:36 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!getBBox) {
|
2014-04-08 03:21:22 +00:00
|
|
|
var batchCmd = new svgedit.history.BatchCommand('Convert element to Path');
|
2010-06-30 18:27:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var attrs = getBBox?{}:{
|
2014-04-08 03:21:22 +00:00
|
|
|
'fill': cur_shape.fill,
|
|
|
|
'fill-opacity': cur_shape.fill_opacity,
|
|
|
|
'stroke': cur_shape.stroke,
|
|
|
|
'stroke-width': cur_shape.stroke_width,
|
|
|
|
'stroke-dasharray': cur_shape.stroke_dasharray,
|
|
|
|
'stroke-linejoin': cur_shape.stroke_linejoin,
|
|
|
|
'stroke-linecap': cur_shape.stroke_linecap,
|
|
|
|
'stroke-opacity': cur_shape.stroke_opacity,
|
|
|
|
'opacity': cur_shape.opacity,
|
|
|
|
'visibility':'hidden'
|
2010-06-30 18:27:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// any attribute on the element not covered by the above
|
|
|
|
// TODO: make this list global so that we can properly maintain it
|
|
|
|
// TODO: what about @transform, @clip-rule, @fill-rule, etc?
|
|
|
|
$.each(['marker-start', 'marker-end', 'marker-mid', 'filter', 'clip-path'], function() {
|
|
|
|
if (elem.getAttribute(this)) {
|
|
|
|
attrs[this] = elem.getAttribute(this);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var path = addSvgElementFromJson({
|
2014-04-08 03:21:22 +00:00
|
|
|
'element': 'path',
|
|
|
|
'attr': attrs
|
2010-06-30 18:27:36 +00:00
|
|
|
});
|
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
var eltrans = elem.getAttribute('transform');
|
2013-02-15 15:51:58 +00:00
|
|
|
if (eltrans) {
|
2014-04-08 03:21:22 +00:00
|
|
|
path.setAttribute('transform', eltrans);
|
2010-06-30 18:27:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var id = elem.id;
|
|
|
|
var parent = elem.parentNode;
|
2013-02-15 15:51:58 +00:00
|
|
|
if (elem.nextSibling) {
|
2010-06-30 18:27:36 +00:00
|
|
|
parent.insertBefore(path, elem);
|
|
|
|
} else {
|
|
|
|
parent.appendChild(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
var d = '';
|
|
|
|
|
|
|
|
var joinSegs = function(segs) {
|
|
|
|
$.each(segs, function(j, seg) {
|
2014-02-17 06:48:40 +00:00
|
|
|
var i;
|
2010-06-30 18:27:36 +00:00
|
|
|
var l = seg[0], pts = seg[1];
|
|
|
|
d += l;
|
2014-02-17 06:48:40 +00:00
|
|
|
for (i = 0; i < pts.length; i+=2) {
|
2010-06-30 18:27:36 +00:00
|
|
|
d += (pts[i] +','+pts[i+1]) + ' ';
|
|
|
|
}
|
|
|
|
});
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-06-30 18:27:36 +00:00
|
|
|
|
|
|
|
// Possibly the cubed root of 6, but 1.81 works best
|
|
|
|
var num = 1.81;
|
2014-02-17 06:48:40 +00:00
|
|
|
var a, rx;
|
2010-06-30 18:27:36 +00:00
|
|
|
switch (elem.tagName) {
|
|
|
|
case 'ellipse':
|
|
|
|
case 'circle':
|
2014-02-17 06:48:40 +00:00
|
|
|
a = $(elem).attr(['rx', 'ry', 'cx', 'cy']);
|
|
|
|
var cx = a.cx, cy = a.cy;
|
|
|
|
rx = a.rx;
|
|
|
|
ry = a.ry;
|
2013-02-15 15:51:58 +00:00
|
|
|
if (elem.tagName == 'circle') {
|
2010-06-30 18:27:36 +00:00
|
|
|
rx = ry = $(elem).attr('r');
|
|
|
|
}
|
|
|
|
|
|
|
|
joinSegs([
|
|
|
|
['M',[(cx-rx),(cy)]],
|
|
|
|
['C',[(cx-rx),(cy-ry/num), (cx-rx/num),(cy-ry), (cx),(cy-ry)]],
|
|
|
|
['C',[(cx+rx/num),(cy-ry), (cx+rx),(cy-ry/num), (cx+rx),(cy)]],
|
|
|
|
['C',[(cx+rx),(cy+ry/num), (cx+rx/num),(cy+ry), (cx),(cy+ry)]],
|
|
|
|
['C',[(cx-rx/num),(cy+ry), (cx-rx),(cy+ry/num), (cx-rx),(cy)]],
|
|
|
|
['Z',[]]
|
|
|
|
]);
|
|
|
|
break;
|
|
|
|
case 'path':
|
|
|
|
d = elem.getAttribute('d');
|
|
|
|
break;
|
|
|
|
case 'line':
|
2014-04-08 03:21:22 +00:00
|
|
|
a = $(elem).attr(['x1', 'y1', 'x2', 'y2']);
|
|
|
|
d = 'M'+a.x1+','+a.y1+'L'+a.x2+','+a.y2;
|
2010-06-30 18:27:36 +00:00
|
|
|
break;
|
|
|
|
case 'polyline':
|
|
|
|
case 'polygon':
|
2014-04-08 03:21:22 +00:00
|
|
|
d = 'M' + elem.getAttribute('points');
|
2010-06-30 18:27:36 +00:00
|
|
|
break;
|
|
|
|
case 'rect':
|
|
|
|
var r = $(elem).attr(['rx', 'ry']);
|
2014-02-17 06:48:40 +00:00
|
|
|
rx = r.rx;
|
|
|
|
ry = r.ry;
|
2010-06-30 18:27:36 +00:00
|
|
|
var b = elem.getBBox();
|
|
|
|
var x = b.x, y = b.y, w = b.width, h = b.height;
|
2014-02-17 06:48:40 +00:00
|
|
|
num = 4 - num; // Why? Because!
|
2010-06-30 18:27:36 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!rx && !ry) {
|
2010-06-30 18:27:36 +00:00
|
|
|
// Regular rect
|
|
|
|
joinSegs([
|
|
|
|
['M',[x, y]],
|
|
|
|
['L',[x+w, y]],
|
|
|
|
['L',[x+w, y+h]],
|
|
|
|
['L',[x, y+h]],
|
|
|
|
['L',[x, y]],
|
|
|
|
['Z',[]]
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
joinSegs([
|
|
|
|
['M',[x, y+ry]],
|
2013-02-15 16:51:48 +00:00
|
|
|
['C',[x, y+ry/num, x+rx/num, y, x+rx, y]],
|
2010-06-30 18:27:36 +00:00
|
|
|
['L',[x+w-rx, y]],
|
2013-02-15 16:51:48 +00:00
|
|
|
['C',[x+w-rx/num, y, x+w, y+ry/num, x+w, y+ry]],
|
2010-06-30 18:27:36 +00:00
|
|
|
['L',[x+w, y+h-ry]],
|
2013-02-15 16:51:48 +00:00
|
|
|
['C',[x+w, y+h-ry/num, x+w-rx/num, y+h, x+w-rx, y+h]],
|
2010-06-30 18:27:36 +00:00
|
|
|
['L',[x+rx, y+h]],
|
2013-02-15 16:51:48 +00:00
|
|
|
['C',[x+rx/num, y+h, x, y+h-ry/num, x, y+h-ry]],
|
2010-06-30 18:27:36 +00:00
|
|
|
['L',[x, y+ry]],
|
|
|
|
['Z',[]]
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
path.parentNode.removeChild(path);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (d) {
|
2013-02-15 16:51:48 +00:00
|
|
|
path.setAttribute('d', d);
|
2010-06-30 18:27:36 +00:00
|
|
|
}
|
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!getBBox) {
|
2010-06-30 18:27:36 +00:00
|
|
|
// Replace the current element with the converted one
|
|
|
|
|
|
|
|
// Reorient if it has a matrix
|
2013-02-15 15:51:58 +00:00
|
|
|
if (eltrans) {
|
2013-02-14 15:19:46 +00:00
|
|
|
var tlist = svgedit.transformlist.getTransformList(path);
|
2013-02-15 15:51:58 +00:00
|
|
|
if (svgedit.math.hasMatrixTransform(tlist)) {
|
2010-06-30 18:27:36 +00:00
|
|
|
pathActions.resetOrientation(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-13 09:58:51 +00:00
|
|
|
var nextSibling = elem.nextSibling;
|
2013-02-17 08:21:07 +00:00
|
|
|
batchCmd.addSubCommand(new svgedit.history.RemoveElementCommand(elem, nextSibling, parent));
|
|
|
|
batchCmd.addSubCommand(new svgedit.history.InsertElementCommand(path));
|
2010-06-30 18:27:36 +00:00
|
|
|
|
|
|
|
clearSelection();
|
2013-02-15 15:51:58 +00:00
|
|
|
elem.parentNode.removeChild(elem);
|
2010-06-30 18:27:36 +00:00
|
|
|
path.setAttribute('id', id);
|
2014-04-08 03:21:22 +00:00
|
|
|
path.removeAttribute('visibility');
|
2010-06-30 18:27:36 +00:00
|
|
|
addToSelection([path], true);
|
|
|
|
|
|
|
|
addCommandToHistory(batchCmd);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Get the correct BBox of the new path, then discard it
|
|
|
|
pathActions.resetOrientation(path);
|
|
|
|
var bb = false;
|
|
|
|
try {
|
|
|
|
bb = path.getBBox();
|
|
|
|
} catch(e) {
|
|
|
|
// Firefox fails
|
|
|
|
}
|
|
|
|
path.parentNode.removeChild(path);
|
|
|
|
return bb;
|
|
|
|
}
|
2010-11-08 07:23:26 +00:00
|
|
|
};
|
2010-06-30 18:27:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Function: changeSelectedAttributeNoUndo
|
|
|
|
// This function makes the changes to the elements. It does not add the change
|
|
|
|
// to the history stack.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// attr - String with the attribute name
|
|
|
|
// newValue - String or number with the new attribute value
|
|
|
|
// elems - The DOM elements to apply the change to
|
|
|
|
var changeSelectedAttributeNoUndo = function(attr, newValue, elems) {
|
|
|
|
var handle = svgroot.suspendRedraw(1000);
|
2013-02-15 15:51:58 +00:00
|
|
|
if (current_mode == 'pathedit') {
|
2010-06-30 18:27:36 +00:00
|
|
|
// Editing node
|
|
|
|
pathActions.moveNode(attr, newValue);
|
|
|
|
}
|
2014-02-17 06:48:40 +00:00
|
|
|
elems = elems || selectedElements;
|
2010-06-30 18:27:36 +00:00
|
|
|
var i = elems.length;
|
2010-10-05 18:50:21 +00:00
|
|
|
var no_xy_elems = ['g', 'polyline', 'path'];
|
|
|
|
var good_g_attrs = ['transform', 'opacity', 'filter'];
|
|
|
|
|
2010-06-30 18:27:36 +00:00
|
|
|
while (i--) {
|
|
|
|
var elem = elems[i];
|
2014-02-17 06:48:40 +00:00
|
|
|
if (elem == null) {continue;}
|
2010-06-30 18:27:36 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// Set x,y vals on elements that don't have them
|
2013-02-15 15:51:58 +00:00
|
|
|
if ((attr === 'x' || attr === 'y') && no_xy_elems.indexOf(elem.tagName) >= 0) {
|
2010-06-30 18:27:36 +00:00
|
|
|
var bbox = getStrokedBBox([elem]);
|
2010-10-05 18:50:21 +00:00
|
|
|
var diff_x = attr === 'x' ? newValue - bbox.x : 0;
|
|
|
|
var diff_y = attr === 'y' ? newValue - bbox.y : 0;
|
2010-06-22 14:52:51 +00:00
|
|
|
canvas.moveSelectedElements(diff_x*current_zoom, diff_y*current_zoom, true);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-10-05 18:50:21 +00:00
|
|
|
// only allow the transform/opacity/filter attribute to change on <g> elements, slightly hacky
|
2014-01-31 10:40:52 +00:00
|
|
|
// TODO: FIXME: This doesn't seem right. Where's the body of this if statement?
|
2014-04-08 03:21:22 +00:00
|
|
|
if (elem.tagName === 'g' && good_g_attrs.indexOf(attr) >= 0) {}
|
|
|
|
var oldval = attr === '#text' ? elem.textContent : elem.getAttribute(attr);
|
|
|
|
if (oldval == null) {oldval = '';}
|
2010-10-05 18:50:21 +00:00
|
|
|
if (oldval !== String(newValue)) {
|
2014-04-08 03:21:22 +00:00
|
|
|
if (attr == '#text') {
|
2011-02-24 16:13:26 +00:00
|
|
|
var old_w = svgedit.utilities.getBBox(elem).width;
|
2010-06-22 14:52:51 +00:00
|
|
|
elem.textContent = newValue;
|
2011-01-31 20:02:24 +00:00
|
|
|
|
2011-01-31 20:27:05 +00:00
|
|
|
// FF bug occurs on on rotated elements
|
2013-02-15 15:51:58 +00:00
|
|
|
if (/rotate/.test(elem.getAttribute('transform'))) {
|
2011-01-31 20:02:24 +00:00
|
|
|
elem = ffClone(elem);
|
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
// Hoped to solve the issue of moving text with text-anchor="start",
|
|
|
|
// but this doesn't actually fix it. Hopefully on the right track, though. -Fyrd
|
|
|
|
|
2014-01-31 00:39:35 +00:00
|
|
|
// var box=getBBox(elem), left=box.x, top=box.y, width=box.width,
|
|
|
|
// height=box.height, dx = width - old_w, dy=0;
|
|
|
|
// var angle = svgedit.utilities.getRotationAngle(elem, true);
|
|
|
|
// if (angle) {
|
|
|
|
// var r = Math.sqrt( dx*dx + dy*dy );
|
|
|
|
// var theta = Math.atan2(dy,dx) - angle;
|
|
|
|
// dx = r * Math.cos(theta);
|
|
|
|
// dy = r * Math.sin(theta);
|
|
|
|
//
|
|
|
|
// elem.setAttribute('x', elem.getAttribute('x')-dx);
|
|
|
|
// elem.setAttribute('y', elem.getAttribute('y')-dy);
|
|
|
|
// }
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
} else if (attr == '#href') {
|
2010-08-16 17:53:15 +00:00
|
|
|
setHref(elem, newValue);
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
2014-02-17 06:48:40 +00:00
|
|
|
else {elem.setAttribute(attr, newValue);}
|
2013-09-21 22:19:24 +00:00
|
|
|
|
|
|
|
// Go into "select" mode for text changes
|
|
|
|
// NOTE: Important that this happens AFTER elem.setAttribute() or else attributes like
|
|
|
|
// font-size can get reset to their old value, ultimately by svgEditor.updateContextPanel(),
|
|
|
|
// after calling textActions.toSelectMode() below
|
2014-04-08 03:21:22 +00:00
|
|
|
if (current_mode === 'textedit' && attr !== '#text' && elem.textContent.length) {
|
2013-09-21 22:19:24 +00:00
|
|
|
textActions.toSelectMode(elem);
|
|
|
|
}
|
|
|
|
|
2011-02-24 16:13:26 +00:00
|
|
|
// if (i==0)
|
|
|
|
// selectedBBoxes[0] = svgedit.utilities.getBBox(elem);
|
2010-06-22 14:52:51 +00:00
|
|
|
// Use the Firefox ffClone hack for text elements with gradients or
|
|
|
|
// where other text attributes are changed.
|
2013-02-15 15:51:58 +00:00
|
|
|
if (svgedit.browser.isGecko() && elem.nodeName === 'text' && /rotate/.test(elem.getAttribute('transform'))) {
|
2014-02-17 06:48:40 +00:00
|
|
|
if (String(newValue).indexOf('url') === 0 || (['font-size', 'font-family', 'x', 'y'].indexOf(attr) >= 0 && elem.textContent)) {
|
2010-06-22 14:52:51 +00:00
|
|
|
elem = ffClone(elem);
|
2010-02-02 05:48:33 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
// Timeout needed for Opera & Firefox
|
|
|
|
// codedread: it is now possible for this function to be called with elements
|
|
|
|
// that are not in the selectedElements array, we need to only request a
|
|
|
|
// selector if the element is in that array
|
2010-09-29 18:37:51 +00:00
|
|
|
if (selectedElements.indexOf(elem) >= 0) {
|
2010-06-22 14:52:51 +00:00
|
|
|
setTimeout(function() {
|
|
|
|
// Due to element replacement, this element may no longer
|
|
|
|
// be part of the DOM
|
2014-02-17 06:48:40 +00:00
|
|
|
if (!elem.parentNode) {return;}
|
2010-06-22 14:52:51 +00:00
|
|
|
selectorManager.requestSelector(elem).resize();
|
2013-02-15 15:51:58 +00:00
|
|
|
}, 0);
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
// if this element was rotated, and we changed the position of this element
|
|
|
|
// we need to update the rotational transform attribute
|
2013-02-14 15:19:46 +00:00
|
|
|
var angle = svgedit.utilities.getRotationAngle(elem);
|
2014-04-08 03:21:22 +00:00
|
|
|
if (angle != 0 && attr != 'transform') {
|
2013-02-14 15:19:46 +00:00
|
|
|
var tlist = svgedit.transformlist.getTransformList(elem);
|
2010-06-22 14:52:51 +00:00
|
|
|
var n = tlist.numberOfItems;
|
|
|
|
while (n--) {
|
|
|
|
var xform = tlist.getItem(n);
|
|
|
|
if (xform.type == 4) {
|
|
|
|
// remove old rotate
|
|
|
|
tlist.removeItem(n);
|
|
|
|
|
2011-02-24 16:13:26 +00:00
|
|
|
var box = svgedit.utilities.getBBox(elem);
|
2013-02-14 15:19:46 +00:00
|
|
|
var center = svgedit.math.transformPoint(box.x+box.width/2, box.y+box.height/2, svgedit.math.transformListToTransform(tlist).matrix);
|
2010-06-22 14:52:51 +00:00
|
|
|
var cx = center.x,
|
|
|
|
cy = center.y;
|
|
|
|
var newrot = svgroot.createSVGTransform();
|
|
|
|
newrot.setRotate(angle, cx, cy);
|
|
|
|
tlist.insertItemBefore(newrot, n);
|
|
|
|
break;
|
2009-08-23 03:18:59 +00:00
|
|
|
}
|
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
} // if oldValue != newValue
|
|
|
|
} // for each elem
|
|
|
|
svgroot.unsuspendRedraw(handle);
|
|
|
|
};
|
2010-06-21 18:06:21 +00:00
|
|
|
|
2010-06-29 20:43:44 +00:00
|
|
|
// Function: changeSelectedAttribute
|
|
|
|
// Change the given/selected element and add the original value to the history stack
|
2010-06-22 14:52:51 +00:00
|
|
|
// If you want to change all selectedElements, ignore the elems argument.
|
|
|
|
// If you want to change only a subset of selectedElements, then send the
|
|
|
|
// subset to this function in the elems argument.
|
2010-06-29 20:43:44 +00:00
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// attr - String with the attribute name
|
|
|
|
// newValue - String or number with the new attribute value
|
|
|
|
// elems - The DOM elements to apply the change to
|
2010-06-30 18:27:36 +00:00
|
|
|
var changeSelectedAttribute = this.changeSelectedAttribute = function(attr, val, elems) {
|
2014-02-17 06:48:40 +00:00
|
|
|
elems = elems || selectedElements;
|
2010-11-12 19:08:29 +00:00
|
|
|
canvas.undoMgr.beginUndoableChange(attr, elems);
|
2010-06-22 14:52:51 +00:00
|
|
|
var i = elems.length;
|
2009-09-04 05:10:48 +00:00
|
|
|
|
2010-06-30 18:27:36 +00:00
|
|
|
changeSelectedAttributeNoUndo(attr, val, elems);
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2010-11-12 19:08:29 +00:00
|
|
|
var batchCmd = canvas.undoMgr.finishUndoableChange();
|
2010-06-22 14:52:51 +00:00
|
|
|
if (!batchCmd.isEmpty()) {
|
|
|
|
addCommandToHistory(batchCmd);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-06-29 20:43:44 +00:00
|
|
|
// Function: deleteSelectedElements
|
|
|
|
// Removes all selected elements from the DOM and adds the change to the
|
|
|
|
// history stack
|
2010-06-22 14:52:51 +00:00
|
|
|
this.deleteSelectedElements = function() {
|
2014-02-17 06:48:40 +00:00
|
|
|
var i;
|
2014-04-08 03:21:22 +00:00
|
|
|
var batchCmd = new svgedit.history.BatchCommand('Delete Elements');
|
2010-06-22 14:52:51 +00:00
|
|
|
var len = selectedElements.length;
|
|
|
|
var selectedCopy = []; //selectedElements is being deleted
|
2014-02-17 06:48:40 +00:00
|
|
|
for (i = 0; i < len; ++i) {
|
2010-06-22 14:52:51 +00:00
|
|
|
var selected = selectedElements[i];
|
2014-02-17 06:48:40 +00:00
|
|
|
if (selected == null) {break;}
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
var parent = selected.parentNode;
|
|
|
|
var t = selected;
|
2010-12-15 19:42:48 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// this will unselect the element and remove the selectedOutline
|
|
|
|
selectorManager.releaseSelector(t);
|
2010-12-15 19:42:48 +00:00
|
|
|
|
2011-02-23 16:34:59 +00:00
|
|
|
// Remove the path if present.
|
|
|
|
svgedit.path.removePath_(t.id);
|
|
|
|
|
2010-12-15 19:42:48 +00:00
|
|
|
// Get the parent if it's a single-child anchor
|
2013-02-15 15:51:58 +00:00
|
|
|
if (parent.tagName === 'a' && parent.childNodes.length === 1) {
|
2010-12-15 19:42:48 +00:00
|
|
|
t = parent;
|
|
|
|
parent = parent.parentNode;
|
|
|
|
}
|
|
|
|
|
2010-11-13 09:58:51 +00:00
|
|
|
var nextSibling = t.nextSibling;
|
2010-06-22 14:52:51 +00:00
|
|
|
var elem = parent.removeChild(t);
|
2010-11-13 09:58:51 +00:00
|
|
|
selectedCopy.push(selected); //for the copy
|
2010-06-22 14:52:51 +00:00
|
|
|
selectedElements[i] = null;
|
2010-11-13 09:58:51 +00:00
|
|
|
batchCmd.addSubCommand(new RemoveElementCommand(elem, nextSibling, parent));
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
2014-02-17 06:48:40 +00:00
|
|
|
if (!batchCmd.isEmpty()) {addCommandToHistory(batchCmd);}
|
2014-04-08 03:21:22 +00:00
|
|
|
call('changed', selectedCopy);
|
2010-06-30 18:27:36 +00:00
|
|
|
clearSelection();
|
2010-06-22 14:52:51 +00:00
|
|
|
};
|
|
|
|
|
2010-08-17 18:09:50 +00:00
|
|
|
// Function: cutSelectedElements
|
|
|
|
// Removes all selected elements from the DOM and adds the change to the
|
|
|
|
// history stack. Remembers removed elements on the clipboard
|
|
|
|
|
|
|
|
// TODO: Combine similar code with deleteSelectedElements
|
|
|
|
this.cutSelectedElements = function() {
|
2014-02-17 06:48:40 +00:00
|
|
|
var i;
|
2014-04-08 03:21:22 +00:00
|
|
|
var batchCmd = new svgedit.history.BatchCommand('Cut Elements');
|
2010-08-17 18:09:50 +00:00
|
|
|
var len = selectedElements.length;
|
|
|
|
var selectedCopy = []; //selectedElements is being deleted
|
2014-02-17 06:48:40 +00:00
|
|
|
for (i = 0; i < len; ++i) {
|
2010-08-17 18:09:50 +00:00
|
|
|
var selected = selectedElements[i];
|
2014-02-17 06:48:40 +00:00
|
|
|
if (selected == null) {break;}
|
2010-08-17 18:09:50 +00:00
|
|
|
|
|
|
|
var parent = selected.parentNode;
|
|
|
|
var t = selected;
|
2011-02-23 16:34:59 +00:00
|
|
|
|
2010-08-17 18:09:50 +00:00
|
|
|
// this will unselect the element and remove the selectedOutline
|
|
|
|
selectorManager.releaseSelector(t);
|
2011-02-23 16:34:59 +00:00
|
|
|
|
|
|
|
// Remove the path if present.
|
|
|
|
svgedit.path.removePath_(t.id);
|
|
|
|
|
2010-11-13 09:58:51 +00:00
|
|
|
var nextSibling = t.nextSibling;
|
2010-08-17 18:09:50 +00:00
|
|
|
var elem = parent.removeChild(t);
|
2010-11-13 09:58:51 +00:00
|
|
|
selectedCopy.push(selected); //for the copy
|
2010-08-17 18:09:50 +00:00
|
|
|
selectedElements[i] = null;
|
2010-11-13 09:58:51 +00:00
|
|
|
batchCmd.addSubCommand(new RemoveElementCommand(elem, nextSibling, parent));
|
2010-08-17 18:09:50 +00:00
|
|
|
}
|
2014-02-17 06:48:40 +00:00
|
|
|
if (!batchCmd.isEmpty()) {addCommandToHistory(batchCmd);}
|
2014-04-08 03:21:22 +00:00
|
|
|
call('changed', selectedCopy);
|
2010-08-17 18:09:50 +00:00
|
|
|
clearSelection();
|
|
|
|
|
|
|
|
canvas.clipBoard = selectedCopy;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Function: copySelectedElements
|
|
|
|
// Remembers the current selected elements on the clipboard
|
|
|
|
this.copySelectedElements = function() {
|
|
|
|
canvas.clipBoard = $.merge([], selectedElements);
|
|
|
|
};
|
|
|
|
|
2011-01-28 20:11:18 +00:00
|
|
|
this.pasteElements = function(type, x, y) {
|
2010-08-17 18:09:50 +00:00
|
|
|
var cb = canvas.clipBoard;
|
|
|
|
var len = cb.length;
|
2014-02-17 06:48:40 +00:00
|
|
|
if (!len) {return;}
|
2010-08-17 18:09:50 +00:00
|
|
|
|
|
|
|
var pasted = [];
|
2013-02-17 08:21:07 +00:00
|
|
|
var batchCmd = new svgedit.history.BatchCommand('Paste elements');
|
2010-08-17 18:09:50 +00:00
|
|
|
|
|
|
|
// Move elements to lastClickPoint
|
|
|
|
|
|
|
|
while (len--) {
|
|
|
|
var elem = cb[len];
|
2014-02-17 06:48:40 +00:00
|
|
|
if (!elem) {continue;}
|
2010-08-17 18:09:50 +00:00
|
|
|
var copy = copyElem(elem);
|
|
|
|
|
|
|
|
// See if elem with elem ID is in the DOM already
|
2014-02-17 06:48:40 +00:00
|
|
|
if (!svgedit.utilities.getElem(elem.id)) {copy.id = elem.id;}
|
2010-08-17 18:09:50 +00:00
|
|
|
|
|
|
|
pasted.push(copy);
|
2011-01-25 16:15:29 +00:00
|
|
|
(current_group || getCurrentDrawing().getCurrentLayer()).appendChild(copy);
|
2013-02-17 08:21:07 +00:00
|
|
|
batchCmd.addSubCommand(new svgedit.history.InsertElementCommand(copy));
|
2010-08-17 18:09:50 +00:00
|
|
|
}
|
|
|
|
|
2010-09-17 20:33:33 +00:00
|
|
|
selectOnly(pasted);
|
2010-08-17 18:09:50 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (type !== 'in_place') {
|
2011-01-28 20:11:18 +00:00
|
|
|
|
|
|
|
var ctr_x, ctr_y;
|
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!type) {
|
2011-01-28 20:11:18 +00:00
|
|
|
ctr_x = lastClickPoint.x;
|
|
|
|
ctr_y = lastClickPoint.y;
|
2013-02-15 15:51:58 +00:00
|
|
|
} else if (type === 'point') {
|
2011-01-28 20:11:18 +00:00
|
|
|
ctr_x = x;
|
|
|
|
ctr_y = y;
|
|
|
|
}
|
|
|
|
|
2010-08-17 18:09:50 +00:00
|
|
|
var bbox = getStrokedBBox(pasted);
|
2011-01-28 20:11:18 +00:00
|
|
|
var cx = ctr_x - (bbox.x + bbox.width/2),
|
|
|
|
cy = ctr_y - (bbox.y + bbox.height/2),
|
2010-08-17 18:09:50 +00:00
|
|
|
dx = [],
|
|
|
|
dy = [];
|
|
|
|
|
|
|
|
$.each(pasted, function(i, item) {
|
|
|
|
dx.push(cx);
|
|
|
|
dy.push(cy);
|
|
|
|
});
|
|
|
|
|
|
|
|
var cmd = canvas.moveSelectedElements(dx, dy, false);
|
|
|
|
batchCmd.addSubCommand(cmd);
|
|
|
|
}
|
2011-01-28 20:11:18 +00:00
|
|
|
|
2010-08-17 18:09:50 +00:00
|
|
|
addCommandToHistory(batchCmd);
|
2014-04-08 03:21:22 +00:00
|
|
|
call('changed', pasted);
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-08-17 18:09:50 +00:00
|
|
|
|
2010-06-29 20:43:44 +00:00
|
|
|
// Function: groupSelectedElements
|
|
|
|
// Wraps all the selected elements in a group (g) element
|
2010-12-01 17:54:11 +00:00
|
|
|
|
|
|
|
// Parameters:
|
|
|
|
// type - type of element to group into, defaults to <g>
|
2014-02-17 06:48:40 +00:00
|
|
|
this.groupSelectedElements = function(type, urlArg) {
|
|
|
|
if (!type) {type = 'g';}
|
2010-12-01 17:54:11 +00:00
|
|
|
var cmd_str = '';
|
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
switch (type) {
|
2014-04-08 03:21:22 +00:00
|
|
|
case 'a':
|
|
|
|
cmd_str = 'Make hyperlink';
|
2010-12-01 17:54:11 +00:00
|
|
|
var url = '';
|
2013-02-15 15:51:58 +00:00
|
|
|
if (arguments.length > 1) {
|
2014-02-17 06:48:40 +00:00
|
|
|
url = urlArg;
|
2010-12-01 17:54:11 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
type = 'g';
|
2014-04-08 03:21:22 +00:00
|
|
|
cmd_str = 'Group Elements';
|
2010-12-01 17:54:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-02-17 08:21:07 +00:00
|
|
|
var batchCmd = new svgedit.history.BatchCommand(cmd_str);
|
2010-01-08 20:36:39 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// create and insert the group element
|
|
|
|
var g = addSvgElementFromJson({
|
2014-04-08 03:21:22 +00:00
|
|
|
'element': type,
|
|
|
|
'attr': {
|
|
|
|
'id': getNextId()
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
});
|
2013-02-15 15:51:58 +00:00
|
|
|
if (type === 'a') {
|
2010-12-01 17:54:11 +00:00
|
|
|
setHref(g, url);
|
|
|
|
}
|
2013-02-17 08:21:07 +00:00
|
|
|
batchCmd.addSubCommand(new svgedit.history.InsertElementCommand(g));
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
// now move all children into the group
|
|
|
|
var i = selectedElements.length;
|
|
|
|
while (i--) {
|
|
|
|
var elem = selectedElements[i];
|
2014-02-17 06:48:40 +00:00
|
|
|
if (elem == null) {continue;}
|
2010-12-01 17:54:11 +00:00
|
|
|
|
|
|
|
if (elem.parentNode.tagName === 'a' && elem.parentNode.childNodes.length === 1) {
|
|
|
|
elem = elem.parentNode;
|
|
|
|
}
|
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
var oldNextSibling = elem.nextSibling;
|
|
|
|
var oldParent = elem.parentNode;
|
|
|
|
g.appendChild(elem);
|
2013-02-17 08:21:07 +00:00
|
|
|
batchCmd.addSubCommand(new svgedit.history.MoveElementCommand(elem, oldNextSibling, oldParent));
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
2014-02-17 06:48:40 +00:00
|
|
|
if (!batchCmd.isEmpty()) {addCommandToHistory(batchCmd);}
|
2009-09-05 22:09:42 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// update selection
|
2010-09-17 20:33:33 +00:00
|
|
|
selectOnly([g], true);
|
2010-06-22 14:52:51 +00:00
|
|
|
};
|
|
|
|
|
2010-10-20 18:07:30 +00:00
|
|
|
|
|
|
|
// Function: pushGroupProperties
|
|
|
|
// Pushes all appropriate parent group properties down to its children, then
|
|
|
|
// removes them from the group
|
|
|
|
var pushGroupProperties = this.pushGroupProperties = function(g, undoable) {
|
|
|
|
|
|
|
|
var children = g.childNodes;
|
|
|
|
var len = children.length;
|
2014-04-08 03:21:22 +00:00
|
|
|
var xform = g.getAttribute('transform');
|
2010-10-20 18:07:30 +00:00
|
|
|
|
2013-02-14 15:19:46 +00:00
|
|
|
var glist = svgedit.transformlist.getTransformList(g);
|
|
|
|
var m = svgedit.math.transformListToTransform(glist).matrix;
|
2010-10-20 18:07:30 +00:00
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
var batchCmd = new svgedit.history.BatchCommand('Push group properties');
|
2010-10-20 18:07:30 +00:00
|
|
|
|
|
|
|
// TODO: get all fill/stroke properties from the group that we are about to destroy
|
|
|
|
// "fill", "fill-opacity", "fill-rule", "stroke", "stroke-dasharray", "stroke-dashoffset",
|
|
|
|
// "stroke-linecap", "stroke-linejoin", "stroke-miterlimit", "stroke-opacity",
|
|
|
|
// "stroke-width"
|
|
|
|
// and then for each child, if they do not have the attribute (or the value is 'inherit')
|
|
|
|
// then set the child's attribute
|
|
|
|
|
|
|
|
var i = 0;
|
2013-02-14 15:19:46 +00:00
|
|
|
var gangle = svgedit.utilities.getRotationAngle(g);
|
2010-10-20 18:07:30 +00:00
|
|
|
|
|
|
|
var gattrs = $(g).attr(['filter', 'opacity']);
|
2014-02-17 06:48:40 +00:00
|
|
|
var gfilter, gblur, changes;
|
2010-10-20 18:07:30 +00:00
|
|
|
|
2014-02-17 06:48:40 +00:00
|
|
|
for (i = 0; i < len; i++) {
|
2010-10-20 18:07:30 +00:00
|
|
|
var elem = children[i];
|
|
|
|
|
2014-02-17 06:48:40 +00:00
|
|
|
if (elem.nodeType !== 1) {continue;}
|
2011-02-02 21:26:41 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (gattrs.opacity !== null && gattrs.opacity !== 1) {
|
2010-10-20 18:07:30 +00:00
|
|
|
var c_opac = elem.getAttribute('opacity') || 1;
|
|
|
|
var new_opac = Math.round((elem.getAttribute('opacity') || 1) * gattrs.opacity * 100)/100;
|
|
|
|
changeSelectedAttribute('opacity', new_opac, [elem]);
|
|
|
|
}
|
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (gattrs.filter) {
|
2010-10-20 18:07:30 +00:00
|
|
|
var cblur = this.getBlur(elem);
|
|
|
|
var orig_cblur = cblur;
|
2014-02-17 06:48:40 +00:00
|
|
|
if (!gblur) {gblur = this.getBlur(g);}
|
2013-02-15 15:51:58 +00:00
|
|
|
if (cblur) {
|
2010-10-20 18:07:30 +00:00
|
|
|
// Is this formula correct?
|
2014-02-17 06:48:40 +00:00
|
|
|
cblur = Number(gblur) + Number(cblur);
|
2013-02-15 15:51:58 +00:00
|
|
|
} else if (cblur === 0) {
|
2010-10-20 18:07:30 +00:00
|
|
|
cblur = gblur;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If child has no current filter, get group's filter or clone it.
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!orig_cblur) {
|
2010-10-20 18:07:30 +00:00
|
|
|
// Set group's filter to use first child's ID
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!gfilter) {
|
2013-02-14 15:19:46 +00:00
|
|
|
gfilter = svgedit.utilities.getRefElem(gattrs.filter);
|
2010-10-20 18:07:30 +00:00
|
|
|
} else {
|
|
|
|
// Clone the group's filter
|
|
|
|
gfilter = copyElem(gfilter);
|
2013-02-14 15:19:46 +00:00
|
|
|
svgedit.utilities.findDefs().appendChild(gfilter);
|
2010-10-20 18:07:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
2013-02-14 15:19:46 +00:00
|
|
|
gfilter = svgedit.utilities.getRefElem(elem.getAttribute('filter'));
|
2010-10-20 18:07:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Change this in future for different filters
|
|
|
|
var suffix = (gfilter.firstChild.tagName === 'feGaussianBlur')?'blur':'filter';
|
|
|
|
gfilter.id = elem.id + '_' + suffix;
|
|
|
|
changeSelectedAttribute('filter', 'url(#' + gfilter.id + ')', [elem]);
|
|
|
|
|
|
|
|
// Update blur value
|
2013-02-15 15:51:58 +00:00
|
|
|
if (cblur) {
|
2010-10-20 18:07:30 +00:00
|
|
|
changeSelectedAttribute('stdDeviation', cblur, [gfilter.firstChild]);
|
|
|
|
canvas.setBlurOffsets(gfilter, cblur);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 15:19:46 +00:00
|
|
|
var chtlist = svgedit.transformlist.getTransformList(elem);
|
2010-10-20 18:07:30 +00:00
|
|
|
|
|
|
|
// Don't process gradient transforms
|
2014-02-17 06:48:40 +00:00
|
|
|
if (~elem.tagName.indexOf('Gradient')) {chtlist = null;}
|
2010-10-20 18:07:30 +00:00
|
|
|
|
|
|
|
// Hopefully not a problem to add this. Necessary for elements like <desc/>
|
2014-02-17 06:48:40 +00:00
|
|
|
if (!chtlist) {continue;}
|
2010-10-20 18:07:30 +00:00
|
|
|
|
2010-12-21 19:36:51 +00:00
|
|
|
// Apparently <defs> can get get a transformlist, but we don't want it to have one!
|
2014-02-17 06:48:40 +00:00
|
|
|
if (elem.tagName === 'defs') {continue;}
|
2010-12-21 19:36:51 +00:00
|
|
|
|
2010-10-20 18:07:30 +00:00
|
|
|
if (glist.numberOfItems) {
|
|
|
|
// TODO: if the group's transform is just a rotate, we can always transfer the
|
|
|
|
// rotate() down to the children (collapsing consecutive rotates and factoring
|
|
|
|
// out any translates)
|
|
|
|
if (gangle && glist.numberOfItems == 1) {
|
|
|
|
// [Rg] [Rc] [Mc]
|
|
|
|
// we want [Tr] [Rc2] [Mc] where:
|
2014-01-31 00:39:35 +00:00
|
|
|
// - [Rc2] is at the child's current center but has the
|
2014-01-31 10:40:52 +00:00
|
|
|
// sum of the group and child's rotation angles
|
2014-01-31 00:39:35 +00:00
|
|
|
// - [Tr] is the equivalent translation that this child
|
2014-01-31 10:40:52 +00:00
|
|
|
// undergoes if the group wasn't there
|
2010-10-20 18:07:30 +00:00
|
|
|
|
|
|
|
// [Tr] = [Rg] [Rc] [Rc2_inv]
|
|
|
|
|
|
|
|
// get group's rotation matrix (Rg)
|
|
|
|
var rgm = glist.getItem(0).matrix;
|
|
|
|
|
|
|
|
// get child's rotation matrix (Rc)
|
|
|
|
var rcm = svgroot.createSVGMatrix();
|
2013-02-14 15:19:46 +00:00
|
|
|
var cangle = svgedit.utilities.getRotationAngle(elem);
|
2010-10-20 18:07:30 +00:00
|
|
|
if (cangle) {
|
|
|
|
rcm = chtlist.getItem(0).matrix;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get child's old center of rotation
|
2011-02-24 16:13:26 +00:00
|
|
|
var cbox = svgedit.utilities.getBBox(elem);
|
2013-02-14 15:19:46 +00:00
|
|
|
var ceqm = svgedit.math.transformListToTransform(chtlist).matrix;
|
2013-02-15 16:51:48 +00:00
|
|
|
var coldc = svgedit.math.transformPoint(cbox.x+cbox.width/2, cbox.y+cbox.height/2, ceqm);
|
2010-10-20 18:07:30 +00:00
|
|
|
|
|
|
|
// sum group and child's angles
|
|
|
|
var sangle = gangle + cangle;
|
|
|
|
|
|
|
|
// get child's rotation at the old center (Rc2_inv)
|
|
|
|
var r2 = svgroot.createSVGTransform();
|
|
|
|
r2.setRotate(sangle, coldc.x, coldc.y);
|
|
|
|
|
|
|
|
// calculate equivalent translate
|
2013-02-14 15:19:46 +00:00
|
|
|
var trm = svgedit.math.matrixMultiply(rgm, rcm, r2.matrix.inverse());
|
2010-10-20 18:07:30 +00:00
|
|
|
|
|
|
|
// set up tlist
|
|
|
|
if (cangle) {
|
|
|
|
chtlist.removeItem(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sangle) {
|
2013-02-15 15:51:58 +00:00
|
|
|
if (chtlist.numberOfItems) {
|
2010-10-20 18:07:30 +00:00
|
|
|
chtlist.insertItemBefore(r2, 0);
|
|
|
|
} else {
|
|
|
|
chtlist.appendItem(r2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (trm.e || trm.f) {
|
|
|
|
var tr = svgroot.createSVGTransform();
|
|
|
|
tr.setTranslate(trm.e, trm.f);
|
2013-02-15 15:51:58 +00:00
|
|
|
if (chtlist.numberOfItems) {
|
2010-10-20 18:07:30 +00:00
|
|
|
chtlist.insertItemBefore(tr, 0);
|
|
|
|
} else {
|
|
|
|
chtlist.appendItem(tr);
|
|
|
|
}
|
|
|
|
}
|
2013-02-15 15:51:58 +00:00
|
|
|
} else { // more complicated than just a rotate
|
2010-12-21 19:36:51 +00:00
|
|
|
|
2010-10-20 18:07:30 +00:00
|
|
|
// transfer the group's transform down to each child and then
|
2013-02-20 06:29:25 +00:00
|
|
|
// call svgedit.recalculate.recalculateDimensions()
|
2014-04-08 03:21:22 +00:00
|
|
|
var oldxform = elem.getAttribute('transform');
|
2014-02-17 06:48:40 +00:00
|
|
|
changes = {};
|
|
|
|
changes.transform = oldxform || '';
|
2010-10-20 18:07:30 +00:00
|
|
|
|
|
|
|
var newxform = svgroot.createSVGTransform();
|
|
|
|
|
|
|
|
// [ gm ] [ chm ] = [ chm ] [ gm' ]
|
|
|
|
// [ gm' ] = [ chm_inv ] [ gm ] [ chm ]
|
2013-02-14 15:19:46 +00:00
|
|
|
var chm = svgedit.math.transformListToTransform(chtlist).matrix,
|
2010-10-20 18:07:30 +00:00
|
|
|
chm_inv = chm.inverse();
|
2013-02-14 15:19:46 +00:00
|
|
|
var gm = svgedit.math.matrixMultiply( chm_inv, m, chm );
|
2010-10-20 18:07:30 +00:00
|
|
|
newxform.setMatrix(gm);
|
|
|
|
chtlist.appendItem(newxform);
|
|
|
|
}
|
2013-02-20 06:29:25 +00:00
|
|
|
var cmd = svgedit.recalculate.recalculateDimensions(elem);
|
2014-02-17 06:48:40 +00:00
|
|
|
if (cmd) {batchCmd.addSubCommand(cmd);}
|
2010-10-20 18:07:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// remove transform and make it undo-able
|
|
|
|
if (xform) {
|
2014-02-17 06:48:40 +00:00
|
|
|
changes = {};
|
2014-04-08 03:21:22 +00:00
|
|
|
changes.transform = xform;
|
|
|
|
g.setAttribute('transform', '');
|
|
|
|
g.removeAttribute('transform');
|
2013-02-17 08:21:07 +00:00
|
|
|
batchCmd.addSubCommand(new svgedit.history.ChangeElementCommand(g, changes));
|
2010-10-20 18:07:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (undoable && !batchCmd.isEmpty()) {
|
|
|
|
return batchCmd;
|
|
|
|
}
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-10-20 18:07:30 +00:00
|
|
|
|
|
|
|
|
2010-06-29 20:43:44 +00:00
|
|
|
// Function: ungroupSelectedElement
|
|
|
|
// Unwraps all the elements in a selected group (g) element. This requires
|
|
|
|
// significant recalculations to apply group's transforms, etc to its children
|
2010-06-22 14:52:51 +00:00
|
|
|
this.ungroupSelectedElement = function() {
|
|
|
|
var g = selectedElements[0];
|
2013-02-15 15:51:58 +00:00
|
|
|
if ($(g).data('gsvg') || $(g).data('symbol')) {
|
2010-07-29 15:09:49 +00:00
|
|
|
// Is svg, so actually convert to group
|
2010-10-04 16:01:58 +00:00
|
|
|
convertToGroup(g);
|
|
|
|
return;
|
2013-02-15 15:51:58 +00:00
|
|
|
}
|
|
|
|
if (g.tagName === 'use') {
|
2010-10-04 16:01:58 +00:00
|
|
|
// Somehow doesn't have data set, so retrieve
|
2013-02-14 15:19:46 +00:00
|
|
|
var symbol = svgedit.utilities.getElem(getHref(g).substr(1));
|
2011-02-10 18:10:05 +00:00
|
|
|
$(g).data('symbol', symbol).data('ref', symbol);
|
2010-07-29 15:09:49 +00:00
|
|
|
convertToGroup(g);
|
|
|
|
return;
|
|
|
|
}
|
2010-12-01 17:54:11 +00:00
|
|
|
var parents_a = $(g).parents('a');
|
2013-02-15 15:51:58 +00:00
|
|
|
if (parents_a.length) {
|
2010-12-01 17:54:11 +00:00
|
|
|
g = parents_a[0];
|
|
|
|
}
|
2010-07-22 19:10:51 +00:00
|
|
|
|
2010-12-01 17:54:11 +00:00
|
|
|
// Look for parent "a"
|
2014-04-08 03:21:22 +00:00
|
|
|
if (g.tagName === 'g' || g.tagName === 'a') {
|
2010-12-01 17:54:11 +00:00
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
var batchCmd = new svgedit.history.BatchCommand('Ungroup Elements');
|
2010-10-20 18:07:30 +00:00
|
|
|
var cmd = pushGroupProperties(g, true);
|
2014-02-17 06:48:40 +00:00
|
|
|
if (cmd) {batchCmd.addSubCommand(cmd);}
|
2010-10-20 18:07:30 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
var parent = g.parentNode;
|
2010-07-29 15:09:49 +00:00
|
|
|
var anchor = g.nextSibling;
|
2010-06-22 14:52:51 +00:00
|
|
|
var children = new Array(g.childNodes.length);
|
2009-09-05 22:09:42 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
var i = 0;
|
|
|
|
|
|
|
|
while (g.firstChild) {
|
|
|
|
var elem = g.firstChild;
|
2009-09-05 22:09:42 +00:00
|
|
|
var oldNextSibling = elem.nextSibling;
|
|
|
|
var oldParent = elem.parentNode;
|
2010-07-22 19:10:51 +00:00
|
|
|
|
|
|
|
// Remove child title elements
|
2013-02-15 15:51:58 +00:00
|
|
|
if (elem.tagName === 'title') {
|
2010-11-13 09:58:51 +00:00
|
|
|
var nextSibling = elem.nextSibling;
|
2013-02-17 08:21:07 +00:00
|
|
|
batchCmd.addSubCommand(new svgedit.history.RemoveElementCommand(elem, nextSibling, oldParent));
|
2010-07-22 19:10:51 +00:00
|
|
|
oldParent.removeChild(elem);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
children[i++] = elem = parent.insertBefore(elem, anchor);
|
2013-02-17 08:21:07 +00:00
|
|
|
batchCmd.addSubCommand(new svgedit.history.MoveElementCommand(elem, oldNextSibling, oldParent));
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
2010-05-28 19:17:30 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// remove the group from the selection
|
2010-06-30 18:27:36 +00:00
|
|
|
clearSelection();
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
// delete the group element (but make undo-able)
|
2010-11-13 09:58:51 +00:00
|
|
|
var gNextSibling = g.nextSibling;
|
2010-06-22 14:52:51 +00:00
|
|
|
g = parent.removeChild(g);
|
2013-02-17 08:21:07 +00:00
|
|
|
batchCmd.addSubCommand(new svgedit.history.RemoveElementCommand(g, gNextSibling, parent));
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2014-02-17 06:48:40 +00:00
|
|
|
if (!batchCmd.isEmpty()) {addCommandToHistory(batchCmd);}
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
// update selection
|
2010-06-30 18:27:36 +00:00
|
|
|
addToSelection(children);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Function: moveToTopSelectedElement
|
|
|
|
// Repositions the selected element to the bottom in the DOM to appear on top of
|
|
|
|
// other elements
|
|
|
|
this.moveToTopSelectedElement = function() {
|
|
|
|
var selected = selectedElements[0];
|
|
|
|
if (selected != null) {
|
|
|
|
var t = selected;
|
|
|
|
var oldParent = t.parentNode;
|
|
|
|
var oldNextSibling = t.nextSibling;
|
|
|
|
t = t.parentNode.appendChild(t);
|
2011-01-30 16:45:29 +00:00
|
|
|
// If the element actually moved position, add the command and fire the changed
|
|
|
|
// event handler.
|
|
|
|
if (oldNextSibling != t.nextSibling) {
|
2014-04-08 03:21:22 +00:00
|
|
|
addCommandToHistory(new svgedit.history.MoveElementCommand(t, oldNextSibling, oldParent, 'top'));
|
|
|
|
call('changed', [t]);
|
2011-01-30 16:45:29 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-06-30 18:27:36 +00:00
|
|
|
// Function: moveToBottomSelectedElement
|
|
|
|
// Repositions the selected element to the top in the DOM to appear under
|
|
|
|
// other elements
|
|
|
|
this.moveToBottomSelectedElement = function() {
|
|
|
|
var selected = selectedElements[0];
|
|
|
|
if (selected != null) {
|
|
|
|
var t = selected;
|
|
|
|
var oldParent = t.parentNode;
|
|
|
|
var oldNextSibling = t.nextSibling;
|
|
|
|
var firstChild = t.parentNode.firstChild;
|
|
|
|
if (firstChild.tagName == 'title') {
|
|
|
|
firstChild = firstChild.nextSibling;
|
|
|
|
}
|
|
|
|
// This can probably be removed, as the defs should not ever apppear
|
|
|
|
// inside a layer group
|
|
|
|
if (firstChild.tagName == 'defs') {
|
|
|
|
firstChild = firstChild.nextSibling;
|
|
|
|
}
|
|
|
|
t = t.parentNode.insertBefore(t, firstChild);
|
2011-01-30 16:45:29 +00:00
|
|
|
// If the element actually moved position, add the command and fire the changed
|
|
|
|
// event handler.
|
|
|
|
if (oldNextSibling != t.nextSibling) {
|
2014-04-08 03:21:22 +00:00
|
|
|
addCommandToHistory(new svgedit.history.MoveElementCommand(t, oldNextSibling, oldParent, 'bottom'));
|
|
|
|
call('changed', [t]);
|
2011-01-30 16:45:29 +00:00
|
|
|
}
|
2010-06-30 18:27:36 +00:00
|
|
|
}
|
|
|
|
};
|
2010-06-22 18:17:42 +00:00
|
|
|
|
2010-08-20 14:52:55 +00:00
|
|
|
// Function: moveUpDownSelected
|
|
|
|
// Moves the select element up or down the stack, based on the visibly
|
|
|
|
// intersecting elements
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// dir - String that's either 'Up' or 'Down'
|
|
|
|
this.moveUpDownSelected = function(dir) {
|
|
|
|
var selected = selectedElements[0];
|
2014-02-17 06:48:40 +00:00
|
|
|
if (!selected) {return;}
|
2010-08-20 14:52:55 +00:00
|
|
|
|
|
|
|
curBBoxes = [];
|
|
|
|
var closest, found_cur;
|
|
|
|
// jQuery sorts this list
|
|
|
|
var list = $(getIntersectionList(getStrokedBBox([selected]))).toArray();
|
2014-02-17 06:48:40 +00:00
|
|
|
if (dir == 'Down') {list.reverse();}
|
2010-08-20 14:52:55 +00:00
|
|
|
|
|
|
|
$.each(list, function() {
|
2013-02-15 15:51:58 +00:00
|
|
|
if (!found_cur) {
|
|
|
|
if (this == selected) {
|
2010-08-20 14:52:55 +00:00
|
|
|
found_cur = true;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
closest = this;
|
|
|
|
return false;
|
|
|
|
});
|
2014-02-17 06:48:40 +00:00
|
|
|
if (!closest) {return;}
|
2010-08-20 14:52:55 +00:00
|
|
|
|
|
|
|
var t = selected;
|
|
|
|
var oldParent = t.parentNode;
|
|
|
|
var oldNextSibling = t.nextSibling;
|
|
|
|
$(closest)[dir == 'Down'?'before':'after'](t);
|
2011-01-30 16:45:29 +00:00
|
|
|
// If the element actually moved position, add the command and fire the changed
|
|
|
|
// event handler.
|
|
|
|
if (oldNextSibling != t.nextSibling) {
|
2014-04-08 03:21:22 +00:00
|
|
|
addCommandToHistory(new svgedit.history.MoveElementCommand(t, oldNextSibling, oldParent, 'Move ' + dir));
|
|
|
|
call('changed', [t]);
|
2011-01-30 16:45:29 +00:00
|
|
|
}
|
|
|
|
};
|
2010-08-20 14:52:55 +00:00
|
|
|
|
2010-06-30 18:27:36 +00:00
|
|
|
// Function: moveSelectedElements
|
|
|
|
// Moves selected elements on the X/Y axis
|
2010-06-22 18:17:42 +00:00
|
|
|
//
|
2010-06-30 18:27:36 +00:00
|
|
|
// Parameters:
|
|
|
|
// dx - Float with the distance to move on the x-axis
|
|
|
|
// dy - Float with the distance to move on the y-axis
|
|
|
|
// undoable - Boolean indicating whether or not the action should be undoable
|
2010-06-22 18:17:42 +00:00
|
|
|
//
|
|
|
|
// Returns:
|
2010-06-30 18:27:36 +00:00
|
|
|
// Batch command for the move
|
2010-09-21 19:22:52 +00:00
|
|
|
this.moveSelectedElements = function(dx, dy, undoable) {
|
2010-06-30 18:27:36 +00:00
|
|
|
// if undoable is not sent, default to true
|
|
|
|
// if single values, scale them to the zoom
|
|
|
|
if (dx.constructor != Array) {
|
|
|
|
dx /= current_zoom;
|
|
|
|
dy /= current_zoom;
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
2013-02-15 15:51:58 +00:00
|
|
|
undoable = undoable || true;
|
2014-04-08 03:21:22 +00:00
|
|
|
var batchCmd = new svgedit.history.BatchCommand('position');
|
2010-06-22 14:52:51 +00:00
|
|
|
var i = selectedElements.length;
|
|
|
|
while (i--) {
|
|
|
|
var selected = selectedElements[i];
|
|
|
|
if (selected != null) {
|
2011-02-24 16:13:26 +00:00
|
|
|
// if (i==0)
|
|
|
|
// selectedBBoxes[0] = svgedit.utilities.getBBox(selected);
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2011-02-24 16:13:26 +00:00
|
|
|
// var b = {};
|
2013-02-15 15:51:58 +00:00
|
|
|
// for (var j in selectedBBoxes[i]) b[j] = selectedBBoxes[i][j];
|
2011-02-24 16:13:26 +00:00
|
|
|
// selectedBBoxes[i] = b;
|
2010-08-25 19:06:23 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
var xform = svgroot.createSVGTransform();
|
2013-02-14 15:19:46 +00:00
|
|
|
var tlist = svgedit.transformlist.getTransformList(selected);
|
2010-05-28 16:05:36 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// dx and dy could be arrays
|
|
|
|
if (dx.constructor == Array) {
|
2011-02-24 16:13:26 +00:00
|
|
|
// if (i==0) {
|
|
|
|
// selectedBBoxes[0].x += dx[0];
|
|
|
|
// selectedBBoxes[0].y += dy[0];
|
|
|
|
// }
|
2013-02-15 16:51:48 +00:00
|
|
|
xform.setTranslate(dx[i], dy[i]);
|
2010-06-22 14:52:51 +00:00
|
|
|
} else {
|
2011-02-24 16:13:26 +00:00
|
|
|
// if (i==0) {
|
|
|
|
// selectedBBoxes[0].x += dx;
|
|
|
|
// selectedBBoxes[0].y += dy;
|
|
|
|
// }
|
2013-02-15 16:51:48 +00:00
|
|
|
xform.setTranslate(dx, dy);
|
2010-06-30 18:27:36 +00:00
|
|
|
}
|
2010-08-25 19:06:23 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
if (tlist.numberOfItems) {
|
2010-08-25 19:06:23 +00:00
|
|
|
tlist.insertItemBefore(xform, 0);
|
|
|
|
} else {
|
|
|
|
tlist.appendItem(xform);
|
|
|
|
}
|
2010-06-30 18:27:36 +00:00
|
|
|
|
2013-02-20 06:29:25 +00:00
|
|
|
var cmd = svgedit.recalculate.recalculateDimensions(selected);
|
2010-06-30 18:27:36 +00:00
|
|
|
if (cmd) {
|
|
|
|
batchCmd.addSubCommand(cmd);
|
|
|
|
}
|
2010-09-21 19:22:52 +00:00
|
|
|
|
2010-06-30 18:27:36 +00:00
|
|
|
selectorManager.requestSelector(selected).resize();
|
2009-12-09 21:00:35 +00:00
|
|
|
}
|
|
|
|
}
|
2010-06-30 18:27:36 +00:00
|
|
|
if (!batchCmd.isEmpty()) {
|
2014-02-17 06:48:40 +00:00
|
|
|
if (undoable) {
|
2010-06-30 18:27:36 +00:00
|
|
|
addCommandToHistory(batchCmd);
|
2014-02-17 06:48:40 +00:00
|
|
|
}
|
2014-04-08 03:21:22 +00:00
|
|
|
call('changed', selectedElements);
|
2010-06-30 18:27:36 +00:00
|
|
|
return batchCmd;
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-06-30 18:27:36 +00:00
|
|
|
// Function: cloneSelectedElements
|
|
|
|
// Create deep DOM copies (clones) of all selected elements and move them slightly
|
|
|
|
// from their originals
|
2013-02-15 16:51:48 +00:00
|
|
|
this.cloneSelectedElements = function(x, y) {
|
2014-02-17 06:48:40 +00:00
|
|
|
var i, elem;
|
2014-04-08 03:21:22 +00:00
|
|
|
var batchCmd = new svgedit.history.BatchCommand('Clone Elements');
|
2010-06-22 14:52:51 +00:00
|
|
|
// find all the elements selected (stop at first null)
|
|
|
|
var len = selectedElements.length;
|
2013-06-01 08:00:10 +00:00
|
|
|
function sortfunction(a, b){
|
|
|
|
return ($(b).index() - $(a).index()); //causes an array to be sorted numerically and ascending
|
|
|
|
}
|
|
|
|
selectedElements.sort(sortfunction);
|
2014-02-17 06:48:40 +00:00
|
|
|
for (i = 0; i < len; ++i) {
|
|
|
|
elem = selectedElements[i];
|
|
|
|
if (elem == null) {break;}
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
// use slice to quickly get the subset of elements we need
|
2013-02-15 16:51:48 +00:00
|
|
|
var copiedElements = selectedElements.slice(0, i);
|
2010-09-17 20:33:33 +00:00
|
|
|
this.clearSelection(true);
|
2010-06-22 14:52:51 +00:00
|
|
|
// note that we loop in the reverse way because of the way elements are added
|
|
|
|
// to the selectedElements array (top-first)
|
2014-02-17 06:48:40 +00:00
|
|
|
i = copiedElements.length;
|
2010-06-22 14:52:51 +00:00
|
|
|
while (i--) {
|
|
|
|
// clone each element and replace it within copiedElements
|
2014-02-17 06:48:40 +00:00
|
|
|
elem = copiedElements[i] = copyElem(copiedElements[i]);
|
2011-01-25 16:15:29 +00:00
|
|
|
(current_group || getCurrentDrawing().getCurrentLayer()).appendChild(elem);
|
2013-02-17 08:21:07 +00:00
|
|
|
batchCmd.addSubCommand(new svgedit.history.InsertElementCommand(elem));
|
2010-06-22 14:52:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!batchCmd.isEmpty()) {
|
2010-06-30 18:27:36 +00:00
|
|
|
addToSelection(copiedElements.reverse()); // Need to reverse for correct selection-adding
|
2013-02-15 16:51:48 +00:00
|
|
|
this.moveSelectedElements(x, y, false);
|
2010-06-22 14:52:51 +00:00
|
|
|
addCommandToHistory(batchCmd);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-06-30 18:27:36 +00:00
|
|
|
// Function: alignSelectedElements
|
|
|
|
// Aligns selected elements
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// type - String with single character indicating the alignment type
|
|
|
|
// relative_to - String that must be one of the following:
|
|
|
|
// "selected", "largest", "smallest", "page"
|
2010-06-22 14:52:51 +00:00
|
|
|
this.alignSelectedElements = function(type, relative_to) {
|
2014-02-17 06:48:40 +00:00
|
|
|
var i, elem;
|
2010-06-22 14:52:51 +00:00
|
|
|
var bboxes = [], angles = [];
|
|
|
|
var minx = Number.MAX_VALUE, maxx = Number.MIN_VALUE, miny = Number.MAX_VALUE, maxy = Number.MIN_VALUE;
|
|
|
|
var curwidth = Number.MIN_VALUE, curheight = Number.MIN_VALUE;
|
|
|
|
var len = selectedElements.length;
|
2014-02-17 06:48:40 +00:00
|
|
|
if (!len) {return;}
|
|
|
|
for (i = 0; i < len; ++i) {
|
|
|
|
if (selectedElements[i] == null) {break;}
|
|
|
|
elem = selectedElements[i];
|
2010-06-30 18:27:36 +00:00
|
|
|
bboxes[i] = getStrokedBBox([elem]);
|
2010-06-22 14:52:51 +00:00
|
|
|
|
|
|
|
// now bbox is axis-aligned and handles rotation
|
|
|
|
switch (relative_to) {
|
|
|
|
case 'smallest':
|
|
|
|
if ( (type == 'l' || type == 'c' || type == 'r') && (curwidth == Number.MIN_VALUE || curwidth > bboxes[i].width) ||
|
|
|
|
(type == 't' || type == 'm' || type == 'b') && (curheight == Number.MIN_VALUE || curheight > bboxes[i].height) ) {
|
|
|
|
minx = bboxes[i].x;
|
|
|
|
miny = bboxes[i].y;
|
|
|
|
maxx = bboxes[i].x + bboxes[i].width;
|
|
|
|
maxy = bboxes[i].y + bboxes[i].height;
|
|
|
|
curwidth = bboxes[i].width;
|
|
|
|
curheight = bboxes[i].height;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'largest':
|
|
|
|
if ( (type == 'l' || type == 'c' || type == 'r') && (curwidth == Number.MIN_VALUE || curwidth < bboxes[i].width) ||
|
|
|
|
(type == 't' || type == 'm' || type == 'b') && (curheight == Number.MIN_VALUE || curheight < bboxes[i].height) ) {
|
|
|
|
minx = bboxes[i].x;
|
|
|
|
miny = bboxes[i].y;
|
|
|
|
maxx = bboxes[i].x + bboxes[i].width;
|
|
|
|
maxy = bboxes[i].y + bboxes[i].height;
|
|
|
|
curwidth = bboxes[i].width;
|
|
|
|
curheight = bboxes[i].height;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default: // 'selected'
|
2014-02-17 06:48:40 +00:00
|
|
|
if (bboxes[i].x < minx) {minx = bboxes[i].x;}
|
|
|
|
if (bboxes[i].y < miny) {miny = bboxes[i].y;}
|
|
|
|
if (bboxes[i].x + bboxes[i].width > maxx) {maxx = bboxes[i].x + bboxes[i].width;}
|
|
|
|
if (bboxes[i].y + bboxes[i].height > maxy) {maxy = bboxes[i].y + bboxes[i].height;}
|
2010-06-22 14:52:51 +00:00
|
|
|
break;
|
2009-08-17 06:56:55 +00:00
|
|
|
}
|
2010-06-22 14:52:51 +00:00
|
|
|
} // loop for each element to find the bbox and adjust min/max
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
if (relative_to == 'page') {
|
|
|
|
minx = 0;
|
|
|
|
miny = 0;
|
|
|
|
maxx = canvas.contentW;
|
|
|
|
maxy = canvas.contentH;
|
|
|
|
}
|
|
|
|
|
|
|
|
var dx = new Array(len);
|
|
|
|
var dy = new Array(len);
|
2014-02-17 06:48:40 +00:00
|
|
|
for (i = 0; i < len; ++i) {
|
|
|
|
if (selectedElements[i] == null) {break;}
|
|
|
|
elem = selectedElements[i];
|
2010-06-22 14:52:51 +00:00
|
|
|
var bbox = bboxes[i];
|
|
|
|
dx[i] = 0;
|
|
|
|
dy[i] = 0;
|
|
|
|
switch (type) {
|
|
|
|
case 'l': // left (horizontal)
|
|
|
|
dx[i] = minx - bbox.x;
|
|
|
|
break;
|
|
|
|
case 'c': // center (horizontal)
|
|
|
|
dx[i] = (minx+maxx)/2 - (bbox.x + bbox.width/2);
|
|
|
|
break;
|
|
|
|
case 'r': // right (horizontal)
|
|
|
|
dx[i] = maxx - (bbox.x + bbox.width);
|
|
|
|
break;
|
|
|
|
case 't': // top (vertical)
|
|
|
|
dy[i] = miny - bbox.y;
|
|
|
|
break;
|
|
|
|
case 'm': // middle (vertical)
|
|
|
|
dy[i] = (miny+maxy)/2 - (bbox.y + bbox.height/2);
|
|
|
|
break;
|
|
|
|
case 'b': // bottom (vertical)
|
|
|
|
dy[i] = maxy - (bbox.y + bbox.height);
|
|
|
|
break;
|
2010-01-18 20:30:59 +00:00
|
|
|
}
|
|
|
|
}
|
2013-02-15 16:51:48 +00:00
|
|
|
this.moveSelectedElements(dx, dy);
|
2010-06-22 14:52:51 +00:00
|
|
|
};
|
2010-01-18 20:30:59 +00:00
|
|
|
|
2010-06-30 18:27:36 +00:00
|
|
|
// Group: Additional editor tools
|
2009-08-25 07:04:19 +00:00
|
|
|
|
2010-06-30 18:27:36 +00:00
|
|
|
this.contentW = getResolution().w;
|
|
|
|
this.contentH = getResolution().h;
|
2009-08-25 07:04:19 +00:00
|
|
|
|
2010-06-30 18:27:36 +00:00
|
|
|
// Function: updateCanvas
|
|
|
|
// Updates the editor canvas width/height/position after a zoom has occurred
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// w - Float with the new width
|
|
|
|
// h - Float with the new height
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// Object with the following values:
|
|
|
|
// * x - The canvas' new x coordinate
|
|
|
|
// * y - The canvas' new y coordinate
|
|
|
|
// * old_x - The canvas' old x coordinate
|
|
|
|
// * old_y - The canvas' old y coordinate
|
|
|
|
// * d_x - The x position difference
|
|
|
|
// * d_y - The y position difference
|
|
|
|
this.updateCanvas = function(w, h) {
|
2014-04-08 03:21:22 +00:00
|
|
|
svgroot.setAttribute('width', w);
|
|
|
|
svgroot.setAttribute('height', h);
|
2010-06-30 18:27:36 +00:00
|
|
|
var bg = $('#canvasBackground')[0];
|
|
|
|
var old_x = svgcontent.getAttribute('x');
|
|
|
|
var old_y = svgcontent.getAttribute('y');
|
|
|
|
var x = (w/2 - this.contentW*current_zoom/2);
|
|
|
|
var y = (h/2 - this.contentH*current_zoom/2);
|
|
|
|
|
2013-02-17 04:58:04 +00:00
|
|
|
svgedit.utilities.assignAttributes(svgcontent, {
|
2010-06-30 18:27:36 +00:00
|
|
|
width: this.contentW*current_zoom,
|
|
|
|
height: this.contentH*current_zoom,
|
|
|
|
'x': x,
|
|
|
|
'y': y,
|
2014-04-08 03:21:22 +00:00
|
|
|
'viewBox' : '0 0 ' + this.contentW + ' ' + this.contentH
|
2010-06-30 18:27:36 +00:00
|
|
|
});
|
|
|
|
|
2013-02-17 04:58:04 +00:00
|
|
|
svgedit.utilities.assignAttributes(bg, {
|
2010-06-30 18:27:36 +00:00
|
|
|
width: svgcontent.getAttribute('width'),
|
|
|
|
height: svgcontent.getAttribute('height'),
|
|
|
|
x: x,
|
|
|
|
y: y
|
|
|
|
});
|
2011-09-22 23:58:12 +00:00
|
|
|
|
2013-02-14 15:19:46 +00:00
|
|
|
var bg_img = svgedit.utilities.getElem('background_image');
|
2011-09-22 23:58:12 +00:00
|
|
|
if (bg_img) {
|
2013-02-17 04:58:04 +00:00
|
|
|
svgedit.utilities.assignAttributes(bg_img, {
|
2011-09-22 23:58:12 +00:00
|
|
|
'width': '100%',
|
|
|
|
'height': '100%'
|
|
|
|
});
|
|
|
|
}
|
2010-06-30 18:27:36 +00:00
|
|
|
|
2014-04-08 03:21:22 +00:00
|
|
|
selectorManager.selectorParentGroup.setAttribute('transform', 'translate(' + x + ',' + y + ')');
|
|
|
|
runExtensions('canvasUpdated',{new_x:x, new_y:y, old_x:old_x, old_y:old_y, d_x:x - old_x, d_y:y - old_y});
|
2010-06-30 18:27:36 +00:00
|
|
|
return {x:x, y:y, old_x:old_x, old_y:old_y, d_x:x - old_x, d_y:y - old_y};
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2010-06-30 18:27:36 +00:00
|
|
|
// Function: setBackground
|
|
|
|
// Set the background of the editor (NOT the actual document)
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// color - String with fill color to apply
|
|
|
|
// url - URL or path to image to use
|
|
|
|
this.setBackground = function(color, url) {
|
2014-01-31 10:40:52 +00:00
|
|
|
var bg = svgedit.utilities.getElem('canvasBackground');
|
2010-06-30 18:27:36 +00:00
|
|
|
var border = $(bg).find('rect')[0];
|
2013-02-14 15:19:46 +00:00
|
|
|
var bg_img = svgedit.utilities.getElem('background_image');
|
2013-02-15 16:51:48 +00:00
|
|
|
border.setAttribute('fill', color);
|
2013-02-15 15:51:58 +00:00
|
|
|
if (url) {
|
|
|
|
if (!bg_img) {
|
2014-04-08 03:21:22 +00:00
|
|
|
bg_img = svgdoc.createElementNS(NS.SVG, 'image');
|
2013-02-17 04:58:04 +00:00
|
|
|
svgedit.utilities.assignAttributes(bg_img, {
|
2010-06-30 18:27:36 +00:00
|
|
|
'id': 'background_image',
|
|
|
|
'width': '100%',
|
|
|
|
'height': '100%',
|
|
|
|
'preserveAspectRatio': 'xMinYMin',
|
|
|
|
'style':'pointer-events:none'
|
|
|
|
});
|
|
|
|
}
|
2010-08-16 17:53:15 +00:00
|
|
|
setHref(bg_img, url);
|
2010-06-30 18:27:36 +00:00
|
|
|
bg.appendChild(bg_img);
|
2013-02-15 15:51:58 +00:00
|
|
|
} else if (bg_img) {
|
2010-06-30 18:27:36 +00:00
|
|
|
bg_img.parentNode.removeChild(bg_img);
|
|
|
|
}
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2010-06-30 18:27:36 +00:00
|
|
|
// Function: cycleElement
|
|
|
|
// Select the next/previous element within the current layer
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// next - Boolean where true = next and false = previous element
|
|
|
|
this.cycleElement = function(next) {
|
2014-02-17 06:48:40 +00:00
|
|
|
var num;
|
2010-06-30 18:27:36 +00:00
|
|
|
var cur_elem = selectedElements[0];
|
|
|
|
var elem = false;
|
2011-01-25 16:15:29 +00:00
|
|
|
var all_elems = getVisibleElements(current_group || getCurrentDrawing().getCurrentLayer());
|
2014-02-17 06:48:40 +00:00
|
|
|
if (!all_elems.length) {return;}
|
2010-06-30 18:27:36 +00:00
|
|
|
if (cur_elem == null) {
|
2014-02-17 06:48:40 +00:00
|
|
|
num = next?all_elems.length-1:0;
|
2010-06-30 18:27:36 +00:00
|
|
|
elem = all_elems[num];
|
2010-06-22 14:52:51 +00:00
|
|
|
} else {
|
2010-06-30 18:27:36 +00:00
|
|
|
var i = all_elems.length;
|
2013-02-15 15:51:58 +00:00
|
|
|
while (i--) {
|
|
|
|
if (all_elems[i] == cur_elem) {
|
2014-02-17 06:48:40 +00:00
|
|
|
num = next ? i - 1 : i + 1;
|
2013-02-15 15:51:58 +00:00
|
|
|
if (num >= all_elems.length) {
|
2010-06-30 18:27:36 +00:00
|
|
|
num = 0;
|
2013-02-15 15:51:58 +00:00
|
|
|
} else if (num < 0) {
|
2010-06-30 18:27:36 +00:00
|
|
|
num = all_elems.length-1;
|
|
|
|
}
|
|
|
|
elem = all_elems[num];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-09-17 20:33:33 +00:00
|
|
|
selectOnly([elem], true);
|
2014-04-08 03:21:22 +00:00
|
|
|
call('selected', selectedElements);
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-06-22 14:52:51 +00:00
|
|
|
|
2010-06-30 18:27:36 +00:00
|
|
|
this.clear();
|
|
|
|
|
|
|
|
|
|
|
|
// DEPRECATED: getPrivateMethods
|
|
|
|
// Since all methods are/should be public somehow, this function should be removed
|
|
|
|
|
2010-06-22 14:52:51 +00:00
|
|
|
// Being able to access private methods publicly seems wrong somehow,
|
|
|
|
// but currently appears to be the best way to allow testing and provide
|
|
|
|
// access to them to plugins.
|
|
|
|
this.getPrivateMethods = function() {
|
2010-06-30 18:27:36 +00:00
|
|
|
var obj = {
|
2010-06-22 14:52:51 +00:00
|
|
|
addCommandToHistory: addCommandToHistory,
|
2010-06-28 20:09:34 +00:00
|
|
|
setGradient: setGradient,
|
2010-06-22 14:52:51 +00:00
|
|
|
addSvgElementFromJson: addSvgElementFromJson,
|
|
|
|
assignAttributes: assignAttributes,
|
|
|
|
BatchCommand: BatchCommand,
|
|
|
|
call: call,
|
|
|
|
ChangeElementCommand: ChangeElementCommand,
|
|
|
|
copyElem: copyElem,
|
|
|
|
ffClone: ffClone,
|
|
|
|
findDefs: findDefs,
|
|
|
|
findDuplicateGradient: findDuplicateGradient,
|
|
|
|
getElem: getElem,
|
|
|
|
getId: getId,
|
|
|
|
getIntersectionList: getIntersectionList,
|
|
|
|
getMouseTarget: getMouseTarget,
|
|
|
|
getNextId: getNextId,
|
|
|
|
getPathBBox: getPathBBox,
|
|
|
|
getUrlFromAttr: getUrlFromAttr,
|
|
|
|
hasMatrixTransform: hasMatrixTransform,
|
|
|
|
identifyLayers: identifyLayers,
|
|
|
|
InsertElementCommand: InsertElementCommand,
|
2010-11-07 16:46:57 +00:00
|
|
|
isIdentity: svgedit.math.isIdentity,
|
2010-06-22 14:52:51 +00:00
|
|
|
logMatrix: logMatrix,
|
|
|
|
matrixMultiply: matrixMultiply,
|
|
|
|
MoveElementCommand: MoveElementCommand,
|
|
|
|
preventClickDefault: preventClickDefault,
|
|
|
|
recalculateAllSelectedDimensions: recalculateAllSelectedDimensions,
|
|
|
|
recalculateDimensions: recalculateDimensions,
|
|
|
|
remapElement: remapElement,
|
|
|
|
RemoveElementCommand: RemoveElementCommand,
|
|
|
|
removeUnusedDefElems: removeUnusedDefElems,
|
|
|
|
round: round,
|
|
|
|
runExtensions: runExtensions,
|
|
|
|
sanitizeSvg: sanitizeSvg,
|
2010-11-05 15:59:30 +00:00
|
|
|
SVGEditTransformList: svgedit.transformlist.SVGTransformList,
|
2010-06-22 14:52:51 +00:00
|
|
|
toString: toString,
|
2010-11-07 16:46:57 +00:00
|
|
|
transformBox: svgedit.math.transformBox,
|
2010-06-22 14:52:51 +00:00
|
|
|
transformListToTransform: transformListToTransform,
|
|
|
|
transformPoint: transformPoint,
|
2010-11-05 17:00:32 +00:00
|
|
|
walkTree: svgedit.utilities.walkTree
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|
2010-06-30 18:27:36 +00:00
|
|
|
return obj;
|
2010-06-22 14:52:51 +00:00
|
|
|
};
|
2010-03-11 21:07:13 +00:00
|
|
|
|
2013-02-15 15:51:58 +00:00
|
|
|
};
|