2009-08-21 15:53:36 +00:00
|
|
|
/*
|
|
|
|
TODOs for Rotator:
|
|
|
|
|
2009-08-23 03:18:59 +00:00
|
|
|
- resize with negative width/height causes problems with selector (and upon release, kablooie)
|
|
|
|
- fix resize when mouseup
|
2009-08-21 15:53:36 +00:00
|
|
|
- show the proper resize cursor based on the rotation
|
|
|
|
- add a rotator line/handle to the selector group
|
|
|
|
- respond to mouse down on the rotator handle to start 'rotate' mode
|
|
|
|
- respond to mouse move in rotate mode to change the rotation of the element
|
|
|
|
- upon mouse up in rotate mode go back to select mode
|
|
|
|
|
|
|
|
*/
|
2009-06-23 05:17:53 +00:00
|
|
|
if(!window.console) {
|
|
|
|
window.console = new function() {
|
|
|
|
this.log = function(str) {};
|
|
|
|
this.dir = function(str) {};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
// this defines which elements and attributes that we support
|
2009-08-14 22:50:34 +00:00
|
|
|
// TODO: add <g> elements to this
|
|
|
|
// TODO: add <a> elements to this
|
|
|
|
// TODO: add xmlns:xlink attr to <svg> element
|
2009-07-10 05:04:06 +00:00
|
|
|
var svgWhiteList = {
|
2009-08-16 06:02:26 +00:00
|
|
|
"circle": ["cx", "cy", "fill", "fill-opacity", "id", "r", "stroke", "stroke-dasharray", "stroke-opacity", "stroke-width", "transform"],
|
2009-07-14 12:52:39 +00:00
|
|
|
"defs": [],
|
2009-08-16 06:02:26 +00:00
|
|
|
"ellipse": ["cx", "cy", "fill", "fill-opacity", "id", "rx", "ry", "stroke", "stroke-dasharray", "stroke-opacity", "stroke-width", "transform"],
|
|
|
|
"line": ["fill", "fill-opacity", "id", "stroke", "stroke-dasharray", "stroke-linecap", "stroke-opacity", "stroke-width", "transform", "x1", "x2", "y1", "y2"],
|
2009-08-14 22:36:54 +00:00
|
|
|
"linearGradient": ["id", "gradientTransform", "gradientUnits", "spreadMethod", "x1", "x2", "y1", "y2"],
|
2009-08-16 06:02:26 +00:00
|
|
|
"path": ["d", "fill", "fill-opacity", "id", "stroke", "stroke-dasharray", "stroke-linecap", "stroke-linejoin", "stroke-opacity", "stroke-width", "transform"],
|
|
|
|
"polygon": ["id", "fill", "fill-opacity", "points", "stroke", "stroke-dasharray", "stroke-linecap", "stroke-linejoin", "stroke-opacity", "stroke-width", "transform"],
|
|
|
|
"polyline": ["id", "points", "stroke", "stroke-dasharray", "stroke-linecap", "stroke-linejoin", "stroke-opacity", "stroke-width", "transform"],
|
2009-08-14 22:36:54 +00:00
|
|
|
"radialGradient": ["id", "cx", "cy", "fx", "fy", "gradientTransform", "gradientUnits", "r", "spreadMethod"],
|
2009-08-26 13:03:02 +00:00
|
|
|
"rect": ["fill", "fill-opacity", "height", "id", "rx", "ry", "stroke", "stroke-dasharray", "stroke-linecap", "stroke-linejoin", "stroke-opacity", "stroke-width", "transform", "width", "x", "y"],
|
2009-07-30 22:45:59 +00:00
|
|
|
"stop": ["id", "offset", "stop-color", "stop-opacity"],
|
2009-08-14 22:36:54 +00:00
|
|
|
"svg": ["id", "height", "transform", "width", "xmlns"],
|
2009-08-16 06:02:26 +00:00
|
|
|
"text": ["fill", "fill-opacity", "font-family", "font-size", "font-style", "font-weight", "id", "stroke", "stroke-dasharray", "stroke-linecap", "stroke-linejoin", "stroke-opacity", "stroke-width", "transform", "x", "y"],
|
2009-08-17 06:56:55 +00:00
|
|
|
};
|
2009-07-10 05:04:06 +00:00
|
|
|
|
2009-06-25 02:54:07 +00:00
|
|
|
// These command objects are used for the Undo/Redo stack
|
|
|
|
// attrs contains the values that the attributes had before the change
|
|
|
|
function ChangeElementCommand(elem, attrs, text) {
|
|
|
|
this.elem = elem;
|
|
|
|
this.text = text ? ("Change " + elem.tagName + " " + text) : ("Change " + elem.tagName);
|
|
|
|
this.newValues = {};
|
|
|
|
this.oldValues = attrs;
|
|
|
|
for (attr in attrs) {
|
|
|
|
if (attr == "#text") this.newValues[attr] = elem.textContent;
|
|
|
|
else this.newValues[attr] = elem.getAttribute(attr);
|
|
|
|
}
|
2009-06-29 14:38:30 +00:00
|
|
|
|
2009-06-25 02:54:07 +00:00
|
|
|
this.apply = function() {
|
|
|
|
for( attr in this.newValues ) {
|
|
|
|
if (this.newValues[attr]) {
|
|
|
|
if (attr == "#text") this.elem.textContent = this.newValues[attr];
|
|
|
|
else this.elem.setAttribute(attr, this.newValues[attr]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (attr != "#text") this.elem.textContent = "";
|
|
|
|
else this.elem.removeAttribute(attr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
2009-06-29 14:38:30 +00:00
|
|
|
|
2009-06-25 02:54:07 +00:00
|
|
|
this.unapply = function() {
|
|
|
|
for( attr in this.oldValues ) {
|
|
|
|
if (this.oldValues[attr]) {
|
|
|
|
if (attr == "#text") this.elem.textContent = this.oldValues[attr];
|
|
|
|
else this.elem.setAttribute(attr, this.oldValues[attr]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (attr == "#text") this.elem.textContent = "";
|
|
|
|
else this.elem.removeAttribute(attr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-01 21:28:22 +00:00
|
|
|
this.elements = function() { return [this.elem]; }
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function InsertElementCommand(elem, text) {
|
|
|
|
this.elem = elem;
|
|
|
|
this.text = text || ("Create " + elem.tagName);
|
|
|
|
this.parent = elem.parentNode;
|
|
|
|
|
|
|
|
this.apply = function() { this.elem = this.parent.insertBefore(this.elem, this.elem.nextSibling); };
|
2009-06-29 14:38:30 +00:00
|
|
|
|
2009-06-25 02:54:07 +00:00
|
|
|
this.unapply = function() {
|
|
|
|
this.parent = this.elem.parentNode;
|
|
|
|
this.elem = this.elem.parentNode.removeChild(this.elem);
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-01 21:28:22 +00:00
|
|
|
this.elements = function() { return [this.elem]; };
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function RemoveElementCommand(elem, parent, text) {
|
|
|
|
this.elem = elem;
|
|
|
|
this.text = text || ("Delete " + elem.tagName);
|
|
|
|
this.parent = parent;
|
|
|
|
|
|
|
|
this.apply = function() {
|
|
|
|
this.parent = this.elem.parentNode;
|
2009-06-29 14:38:30 +00:00
|
|
|
this.elem = this.parent.removeChild(this.elem);
|
2009-06-25 02:54:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.unapply = function() { this.elem = this.parent.insertBefore(this.elem, this.elem.nextSibling); };
|
2009-07-01 21:28:22 +00:00
|
|
|
|
|
|
|
this.elements = function() { return [this.elem]; };
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function MoveElementCommand(elem, oldNextSibling, oldParent, text) {
|
|
|
|
this.elem = elem;
|
|
|
|
this.text = text ? ("Move " + elem.tagName + " to " + text) : ("Move " + elem.tagName + "top/bottom");
|
|
|
|
this.oldNextSibling = oldNextSibling;
|
|
|
|
this.oldParent = oldParent;
|
|
|
|
this.newNextSibling = elem.nextSibling;
|
|
|
|
this.newParent = elem.parentNode;
|
2009-06-29 14:38:30 +00:00
|
|
|
|
|
|
|
this.apply = function() {
|
2009-06-25 02:54:07 +00:00
|
|
|
this.elem = this.newParent.insertBefore(this.elem, this.newNextSibling);
|
|
|
|
};
|
2009-06-29 14:38:30 +00:00
|
|
|
|
|
|
|
this.unapply = function() {
|
2009-06-25 02:54:07 +00:00
|
|
|
this.elem = this.oldParent.insertBefore(this.elem, this.oldNextSibling);
|
|
|
|
};
|
2009-07-01 21:28:22 +00:00
|
|
|
|
|
|
|
this.elements = function() { return [this.elem]; };
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
|
|
|
|
2009-06-29 03:58:34 +00:00
|
|
|
// TODO: create a 'typing' command object that tracks changes in text
|
|
|
|
// if a new Typing command is created and the top command on the stack is also a Typing
|
|
|
|
// and they both affect the same element, then collapse the two commands into one
|
|
|
|
|
2009-07-01 21:28:22 +00:00
|
|
|
// this command object acts an arbitrary number of subcommands
|
|
|
|
function BatchCommand(text) {
|
|
|
|
this.text = text || "Batch Command";
|
|
|
|
this.stack = [];
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-01 21:28:22 +00:00
|
|
|
this.apply = function() {
|
2009-07-08 00:03:08 +00:00
|
|
|
var len = this.stack.length;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
2009-07-01 21:28:22 +00:00
|
|
|
this.stack[i].apply();
|
|
|
|
}
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-01 21:28:22 +00:00
|
|
|
this.unapply = function() {
|
|
|
|
for (var i = this.stack.length-1; i >= 0; i--) {
|
|
|
|
this.stack[i].unapply();
|
|
|
|
}
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
this.elements = function() {
|
|
|
|
// iterate through all our subcommands and find all the elements we are changing
|
|
|
|
var elems = [];
|
|
|
|
var cmd = this.stack.length;
|
|
|
|
while (cmd--) {
|
|
|
|
var thisElems = this.stack[cmd].elements();
|
|
|
|
var elem = thisElems.length;
|
|
|
|
while (elem--) {
|
|
|
|
if (elems.indexOf(thisElems[elem]) == -1) elems.push(thisElems[elem]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return elems;
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-02 04:50:24 +00:00
|
|
|
this.addSubCommand = function(cmd) { this.stack.push(cmd); };
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-01 21:28:22 +00:00
|
|
|
this.isEmpty = function() { return this.stack.length == 0; };
|
|
|
|
}
|
2009-06-29 03:58:34 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
function SvgCanvas(c)
|
|
|
|
{
|
|
|
|
// private members
|
|
|
|
|
|
|
|
// **************************************************************************************
|
|
|
|
// FIXME: what's the right way to make this 'class' private to the SelectorManager?
|
|
|
|
function Selector(id, elem) {
|
|
|
|
// this is the selector's unique number
|
|
|
|
this.id = id;
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// this holds a reference to the element for which this selector is being used
|
|
|
|
this.selectedElement = elem;
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// this is a flag used internally to track whether the selector is being used or not
|
2009-06-29 03:58:34 +00:00
|
|
|
this.locked = true;
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// this function is used to reset the id and element that the selector is attached to
|
|
|
|
this.reset = function(e) {
|
|
|
|
this.locked = true;
|
|
|
|
this.selectedElement = e;
|
2009-07-01 15:34:15 +00:00
|
|
|
this.resize();
|
2009-08-14 18:35:48 +00:00
|
|
|
selectorManager.update();
|
2009-06-29 13:20:39 +00:00
|
|
|
this.selectorGroup.setAttribute("display", "inline");
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// this holds a reference to the <g> element that holds all visual elements of the selector
|
|
|
|
this.selectorGroup = addSvgElementFromJson({ "element": "g",
|
|
|
|
"attr": {"id": ("selectorGroup"+this.id)}
|
|
|
|
});
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// this holds a reference to <rect> element
|
|
|
|
this.selectorRect = this.selectorGroup.appendChild( addSvgElementFromJson({
|
|
|
|
"element": "rect",
|
|
|
|
"attr": {
|
|
|
|
"id": ("selectedBox"+this.id),
|
|
|
|
"fill": "none",
|
|
|
|
"stroke": "blue",
|
|
|
|
"stroke-width": "1",
|
|
|
|
"stroke-dasharray": "5,5",
|
|
|
|
"width": 1,
|
|
|
|
"height": 1,
|
|
|
|
// need to specify this so that the rect is not selectable
|
2009-08-04 03:14:23 +00:00
|
|
|
"style": "pointer-events:none"
|
2009-06-29 13:20:39 +00:00
|
|
|
}
|
|
|
|
}) );
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// this holds a reference to the grip elements for this selector
|
|
|
|
this.selectorGrips = { "nw":null,
|
|
|
|
"n":null,
|
|
|
|
"ne":null,
|
|
|
|
"e":null,
|
2009-08-26 13:59:25 +00:00
|
|
|
"se":null,
|
2009-06-29 13:20:39 +00:00
|
|
|
"s":null,
|
2009-08-26 13:59:25 +00:00
|
|
|
"sw":null,
|
|
|
|
"w":null
|
2009-06-29 13:20:39 +00:00
|
|
|
};
|
2009-08-26 19:09:13 +00:00
|
|
|
this.rotateGripConnector = this.selectorGroup.appendChild( addSvgElementFromJson({
|
|
|
|
"element": "line",
|
|
|
|
"attr": {
|
|
|
|
"id": ("selectorGrip_rotate_connector_" + this.id),
|
|
|
|
"stroke": "blue",
|
|
|
|
"stroke-width": "1",
|
|
|
|
}
|
|
|
|
}) );
|
|
|
|
this.rotateGrip = this.selectorGroup.appendChild( addSvgElementFromJson({
|
|
|
|
"element": "circle",
|
|
|
|
"attr": {
|
|
|
|
"id": ("selectorGrip_rotate_" + this.id),
|
|
|
|
"fill": "lime",
|
|
|
|
"r": 4,
|
|
|
|
"stroke": "blue",
|
|
|
|
"stroke-width": 2
|
|
|
|
}
|
|
|
|
}) );
|
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// add the corner grips
|
|
|
|
for (dir in this.selectorGrips) {
|
2009-07-01 21:59:11 +00:00
|
|
|
this.selectorGrips[dir] = this.selectorGroup.appendChild(
|
|
|
|
addSvgElementFromJson({
|
|
|
|
"element": "rect",
|
|
|
|
"attr": {
|
|
|
|
"id": ("selectorGrip_" + dir + "_" + this.id),
|
|
|
|
"fill": "blue",
|
|
|
|
"width": 6,
|
|
|
|
"height": 6,
|
|
|
|
"style": ("cursor:" + dir + "-resize"),
|
|
|
|
// This expands the mouse-able area of the grips making them
|
|
|
|
// easier to grab with the mouse.
|
|
|
|
// This works in Opera and WebKit, but does not work in Firefox
|
|
|
|
// see https://bugzilla.mozilla.org/show_bug.cgi?id=500174
|
|
|
|
"stroke-width": 2,
|
|
|
|
"pointer-events":"all",
|
|
|
|
"display":"none"
|
|
|
|
}
|
|
|
|
}) );
|
2009-06-29 13:20:39 +00:00
|
|
|
$('#'+this.selectorGrips[dir].id).mousedown( function() {
|
|
|
|
current_mode = "resize";
|
2009-07-01 19:28:08 +00:00
|
|
|
current_resize_mode = this.id.substr(13,this.id.indexOf("_",13)-13);
|
2009-06-29 13:20:39 +00:00
|
|
|
});
|
2009-08-26 19:45:28 +00:00
|
|
|
$('#selectorGrip_rotate_'+id).mousedown( function() {
|
|
|
|
current_mode = "rotate";
|
|
|
|
});
|
2009-06-29 13:20:39 +00:00
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
this.showGrips = function(show) {
|
2009-07-30 22:45:59 +00:00
|
|
|
// TODO: use suspendRedraw() here
|
2009-08-26 19:09:13 +00:00
|
|
|
var bShow = show ? "inline" : "none";
|
2009-06-29 13:20:39 +00:00
|
|
|
for (dir in this.selectorGrips) {
|
2009-08-26 19:09:13 +00:00
|
|
|
this.selectorGrips[dir].setAttribute("display", bShow);
|
2009-06-29 13:20:39 +00:00
|
|
|
}
|
2009-08-26 19:09:13 +00:00
|
|
|
this.rotateGrip.setAttribute("display", bShow);
|
|
|
|
this.rotateGripConnector.setAttribute("display", bShow);
|
2009-06-29 13:20:39 +00:00
|
|
|
};
|
2009-08-26 13:59:25 +00:00
|
|
|
|
|
|
|
// Updates cursors for corner grips on rotation so arrows point the right way
|
|
|
|
this.updateGripCursors = function(angle) {
|
|
|
|
var dir_arr = [];
|
|
|
|
var steps = Math.round(angle / 45);
|
|
|
|
for (dir in this.selectorGrips) {
|
|
|
|
dir_arr.push(dir);
|
|
|
|
}
|
|
|
|
while(steps > 0) {
|
|
|
|
dir_arr.push(dir_arr.shift());
|
|
|
|
steps--;
|
|
|
|
}
|
|
|
|
var i = 0;
|
|
|
|
for (dir in this.selectorGrips) {
|
|
|
|
this.selectorGrips[dir].setAttribute('style', ("cursor:" + dir_arr[i] + "-resize"));
|
|
|
|
i++;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2009-08-21 15:53:36 +00:00
|
|
|
this.resize = function(cur_bbox) {
|
2009-06-29 13:20:39 +00:00
|
|
|
var selectedBox = this.selectorRect;
|
|
|
|
var selectedGrips = this.selectorGrips;
|
|
|
|
var selected = this.selectedElement;
|
|
|
|
var sw = parseInt(selected.getAttribute("stroke-width"));
|
|
|
|
var offset = 1;
|
|
|
|
if (!isNaN(sw)) {
|
|
|
|
offset += sw/2;
|
|
|
|
}
|
|
|
|
if (selected.tagName == "text") {
|
|
|
|
offset += 2;
|
|
|
|
}
|
2009-08-21 15:53:36 +00:00
|
|
|
var bbox = cur_bbox || canvas.getBBox(this.selectedElement);
|
2009-06-29 13:20:39 +00:00
|
|
|
var l=bbox.x-offset, t=bbox.y-offset, w=bbox.width+(offset<<1), h=bbox.height+(offset<<1);
|
2009-08-24 19:19:27 +00:00
|
|
|
var sr_handle = svgroot.suspendRedraw(100);
|
2009-08-24 18:39:22 +00:00
|
|
|
assignAttributes(selectedBox, {
|
|
|
|
'x': l,
|
|
|
|
'y': t,
|
|
|
|
'width': w,
|
|
|
|
'height': h
|
|
|
|
});
|
2009-08-24 19:19:27 +00:00
|
|
|
|
2009-08-24 18:39:22 +00:00
|
|
|
var gripCoords = {
|
|
|
|
nw: [l-3, t-3],
|
|
|
|
ne: [l+w-3, t-3],
|
|
|
|
sw: [l-3, t+h-3],
|
|
|
|
se: [l+w-3, t+h-3],
|
|
|
|
n: [l+w/2-3, t-3],
|
|
|
|
w: [l-3, t+h/2-3],
|
|
|
|
e: [l+w-3, t+h/2-3],
|
|
|
|
s: [l+w/2-3, t+h-3]
|
|
|
|
};
|
|
|
|
$.each(gripCoords, function(dir, coords) {
|
|
|
|
assignAttributes(selectedGrips[dir], {
|
|
|
|
x: coords[0], y: coords[1]
|
|
|
|
});
|
|
|
|
});
|
2009-08-21 15:53:36 +00:00
|
|
|
|
2009-08-26 19:09:13 +00:00
|
|
|
assignAttributes(this.rotateGripConnector, { x1: l+w/2, y1: t-20, x2: l+w/2, y2: t });
|
|
|
|
assignAttributes(this.rotateGrip, { cx: l+w/2, cy: t-20 });
|
|
|
|
|
2009-08-21 15:53:36 +00:00
|
|
|
// empty out the transform attribute
|
|
|
|
this.selectorGroup.setAttribute("transform", "");
|
|
|
|
this.selectorGroup.removeAttribute("transform");
|
|
|
|
|
|
|
|
// align selector group with element coordinate axes
|
|
|
|
var transform = this.selectedElement.getAttribute("transform");
|
2009-08-26 21:50:35 +00:00
|
|
|
// TODO: fix Issue 129 here (and other places). For some reason, WebKit sometimes
|
|
|
|
// returns matrix(a,b,c,d,e,f) instead of the things that we've set
|
|
|
|
// (like rotate(a,cx,cy) or translate(tx,ty))
|
|
|
|
// I haven't been able to create a reduced test case yet though
|
2009-08-21 15:53:36 +00:00
|
|
|
if (transform && transform != "") {
|
|
|
|
// this.selectorGroup.setAttribute("transform", transform);
|
|
|
|
var rotind = transform.indexOf("rotate(");
|
|
|
|
if (rotind != -1) {
|
|
|
|
var rotstr = transform.substr(rotind, transform.indexOf(')',rotind)+1);
|
|
|
|
this.selectorGroup.setAttribute("transform", rotstr);
|
|
|
|
}
|
|
|
|
}
|
2009-08-24 19:19:27 +00:00
|
|
|
svgroot.unsuspendRedraw(sr_handle);
|
2009-06-29 13:20:39 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-01 15:34:15 +00:00
|
|
|
// now initialize the selector
|
|
|
|
this.reset(elem);
|
2009-06-29 13:20:39 +00:00
|
|
|
};
|
2009-06-01 17:35:22 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
function SelectorManager() {
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// this will hold the <g> element that contains all selector rects/grips
|
|
|
|
this.selectorParentGroup = null;
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-30 06:24:41 +00:00
|
|
|
// this is a special rect that is used for multi-select
|
|
|
|
this.rubberBandBox = null;
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// this will hold objects of type Selector (see above)
|
|
|
|
this.selectors = [];
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-30 06:37:39 +00:00
|
|
|
// this holds a map of SVG elements to their Selector object
|
|
|
|
this.selectorMap = {};
|
2009-06-30 06:24:41 +00:00
|
|
|
|
|
|
|
// local reference to this object
|
|
|
|
var mgr = this;
|
|
|
|
// private function
|
|
|
|
var initGroup = function() {
|
|
|
|
mgr.selectorParentGroup = addSvgElementFromJson({
|
|
|
|
"element": "g",
|
|
|
|
"attr": {"id": "selectorParentGroup"}
|
2009-08-17 06:56:55 +00:00
|
|
|
});
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
this.requestSelector = function(elem) {
|
2009-07-01 15:34:15 +00:00
|
|
|
if (elem == null) return null;
|
2009-06-29 13:20:39 +00:00
|
|
|
var N = this.selectors.length;
|
2009-06-30 06:24:41 +00:00
|
|
|
|
2009-06-30 14:29:28 +00:00
|
|
|
// if we've already acquired one for this element, return it
|
2009-07-01 21:59:11 +00:00
|
|
|
if (typeof(this.selectorMap[elem.id]) == "object") {
|
|
|
|
this.selectorMap[elem.id].locked = true;
|
|
|
|
return this.selectorMap[elem.id];
|
2009-06-30 14:29:28 +00:00
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
for (var i = 0; i < N; ++i) {
|
|
|
|
if (this.selectors[i] && !this.selectors[i].locked) {
|
|
|
|
this.selectors[i].locked = true;
|
2009-07-01 15:34:15 +00:00
|
|
|
this.selectors[i].reset(elem);
|
2009-07-01 21:59:11 +00:00
|
|
|
this.selectorMap[elem.id] = this.selectors[i];
|
2009-06-29 13:20:39 +00:00
|
|
|
return this.selectors[i];
|
2009-06-29 03:58:34 +00:00
|
|
|
}
|
|
|
|
}
|
2009-06-29 13:20:39 +00:00
|
|
|
// if we reached here, no available selectors were found, we create one
|
|
|
|
this.selectors[N] = new Selector(N, elem);
|
|
|
|
this.selectorParentGroup.appendChild(this.selectors[N].selectorGroup);
|
2009-07-01 21:59:11 +00:00
|
|
|
this.selectorMap[elem.id] = this.selectors[N];
|
2009-06-29 13:20:39 +00:00
|
|
|
return this.selectors[N];
|
|
|
|
};
|
2009-07-01 15:34:15 +00:00
|
|
|
this.releaseSelector = function(elem) {
|
|
|
|
if (elem == null) return;
|
2009-06-29 13:20:39 +00:00
|
|
|
var N = this.selectors.length;
|
2009-07-01 21:59:11 +00:00
|
|
|
var sel = this.selectorMap[elem.id];
|
2009-06-29 13:20:39 +00:00
|
|
|
for (var i = 0; i < N; ++i) {
|
|
|
|
if (this.selectors[i] && this.selectors[i] == sel) {
|
|
|
|
if (sel.locked == false) {
|
|
|
|
console.log("WARNING! selector was released but was already unlocked");
|
|
|
|
}
|
2009-07-01 21:59:11 +00:00
|
|
|
delete this.selectorMap[elem.id];
|
2009-06-29 13:20:39 +00:00
|
|
|
sel.locked = false;
|
|
|
|
sel.selectedElement = null;
|
2009-07-01 19:28:08 +00:00
|
|
|
sel.showGrips(false);
|
2009-06-29 13:20:39 +00:00
|
|
|
|
|
|
|
// remove from DOM and store reference in JS but only if it exists in the DOM
|
|
|
|
try {
|
|
|
|
sel.selectorGroup.setAttribute("display", "none");
|
|
|
|
} catch(e) { }
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
// this keeps the selector groups as the last child in the document
|
|
|
|
this.update = function() {
|
|
|
|
this.selectorParentGroup = svgroot.appendChild(this.selectorParentGroup);
|
2009-08-07 05:44:55 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-30 06:24:41 +00:00
|
|
|
this.getRubberBandBox = function() {
|
|
|
|
if (this.rubberBandBox == null) {
|
2009-06-30 17:50:08 +00:00
|
|
|
this.rubberBandBox = this.selectorParentGroup.appendChild(
|
2009-07-01 19:28:08 +00:00
|
|
|
addSvgElementFromJson({ "element": "rect",
|
|
|
|
"attr": {
|
|
|
|
"id": "selectorRubberBand",
|
|
|
|
"fill": "blue",
|
|
|
|
"fill-opacity": 0.15,
|
|
|
|
"stroke": "blue",
|
|
|
|
"stroke-width": 0.5,
|
|
|
|
"display": "none",
|
2009-08-04 03:14:23 +00:00
|
|
|
"style": "pointer-events:none"
|
2009-07-01 19:28:08 +00:00
|
|
|
}
|
|
|
|
}));
|
2009-06-30 06:24:41 +00:00
|
|
|
}
|
|
|
|
return this.rubberBandBox;
|
2009-08-07 05:44:55 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-07 05:44:55 +00:00
|
|
|
initGroup();
|
2009-06-29 13:20:39 +00:00
|
|
|
}
|
|
|
|
// **************************************************************************************
|
2009-06-25 08:22:30 +00:00
|
|
|
|
2009-08-07 05:44:55 +00:00
|
|
|
var addSvgElementFromJson = function(data) {
|
|
|
|
return canvas.updateElementFromJson(data)
|
|
|
|
};
|
|
|
|
|
2009-08-24 19:19:27 +00:00
|
|
|
var assignAttributes = function(node, attrs, suspendLength) {
|
|
|
|
if(!suspendLength) suspendLength = 0;
|
|
|
|
var handle = svgroot.suspendRedraw(suspendLength);
|
|
|
|
|
2009-08-07 05:44:55 +00:00
|
|
|
for (i in attrs) {
|
|
|
|
node.setAttributeNS(null, i, attrs[i]);
|
|
|
|
}
|
2009-08-24 19:19:27 +00:00
|
|
|
|
2009-08-07 05:44:55 +00:00
|
|
|
svgroot.unsuspendRedraw(handle);
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-07 05:44:55 +00:00
|
|
|
// remove unneeded attributes
|
|
|
|
// makes resulting SVG smaller
|
|
|
|
var cleanupElement = function(element) {
|
|
|
|
var handle = svgroot.suspendRedraw(60);
|
|
|
|
if (element.getAttribute('fill-opacity') == '1')
|
|
|
|
element.removeAttribute('fill-opacity');
|
|
|
|
if (element.getAttribute('opacity') == '1')
|
|
|
|
element.removeAttribute('opacity');
|
|
|
|
if (element.getAttribute('stroke') == 'none')
|
|
|
|
element.removeAttribute('stroke');
|
|
|
|
if (element.getAttribute('stroke-dasharray') == 'none')
|
|
|
|
element.removeAttribute('stroke-dasharray');
|
|
|
|
if (element.getAttribute('stroke-opacity') == '1')
|
|
|
|
element.removeAttribute('stroke-opacity');
|
|
|
|
if (element.getAttribute('stroke-width') == '1')
|
|
|
|
element.removeAttribute('stroke-width');
|
|
|
|
if (element.getAttribute('rx') == '0')
|
|
|
|
element.removeAttribute('rx')
|
|
|
|
if (element.getAttribute('ry') == '0')
|
|
|
|
element.removeAttribute('ry')
|
|
|
|
svgroot.unsuspendRedraw(handle);
|
2009-08-17 06:56:55 +00:00
|
|
|
};
|
2009-08-07 05:44:55 +00:00
|
|
|
|
|
|
|
this.updateElementFromJson = function(data) {
|
|
|
|
var shape = svgdoc.getElementById(data.attr.id);
|
|
|
|
// if shape is a path but we need to create a rect/ellipse, then remove the path
|
|
|
|
if (shape && data.element != shape.tagName) {
|
|
|
|
svgroot.removeChild(shape);
|
|
|
|
shape = null;
|
|
|
|
}
|
|
|
|
if (!shape) {
|
|
|
|
shape = svgdoc.createElementNS(svgns, data.element);
|
|
|
|
svgroot.appendChild(shape);
|
|
|
|
}
|
2009-08-24 19:19:27 +00:00
|
|
|
assignAttributes(shape, data.attr, 100);
|
2009-08-07 05:44:55 +00:00
|
|
|
cleanupElement(shape);
|
|
|
|
return shape;
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-09 22:13:13 +00:00
|
|
|
var canvas = this;
|
2009-06-07 00:17:21 +00:00
|
|
|
var container = c;
|
2009-06-01 21:24:30 +00:00
|
|
|
var svgns = "http://www.w3.org/2000/svg";
|
2009-06-07 00:17:21 +00:00
|
|
|
|
2009-06-12 06:45:37 +00:00
|
|
|
var idprefix = "svg_";
|
2009-06-07 00:17:21 +00:00
|
|
|
var svgdoc = c.ownerDocument;
|
|
|
|
var svgroot = svgdoc.createElementNS(svgns, "svg");
|
2009-06-07 10:27:55 +00:00
|
|
|
svgroot.setAttribute("width", 640);
|
|
|
|
svgroot.setAttribute("height", 480);
|
2009-06-08 15:05:12 +00:00
|
|
|
svgroot.setAttribute("id", "svgroot");
|
2009-06-08 08:09:15 +00:00
|
|
|
svgroot.setAttribute("xmlns", svgns);
|
2009-06-07 00:17:21 +00:00
|
|
|
container.appendChild(svgroot);
|
|
|
|
|
2009-06-02 16:40:33 +00:00
|
|
|
var d_attr = null;
|
|
|
|
var started = false;
|
2009-06-01 21:24:30 +00:00
|
|
|
var obj_num = 1;
|
2009-06-02 16:40:33 +00:00
|
|
|
var start_x = null;
|
|
|
|
var start_y = null;
|
2009-06-27 03:18:50 +00:00
|
|
|
var current_mode = "select";
|
2009-06-23 05:17:53 +00:00
|
|
|
var current_resize_mode = "none";
|
2009-08-07 05:44:55 +00:00
|
|
|
var current_fill = "#FF0000";
|
2009-08-01 05:03:51 +00:00
|
|
|
var current_stroke = "#000000";
|
2009-07-30 22:45:59 +00:00
|
|
|
var current_stroke_paint = null;
|
|
|
|
var current_fill_paint = null;
|
2009-08-07 05:44:55 +00:00
|
|
|
var current_stroke_width = 5;
|
2009-06-02 16:40:33 +00:00
|
|
|
var current_stroke_style = "none";
|
|
|
|
var current_opacity = 1;
|
2009-06-04 13:43:58 +00:00
|
|
|
var current_stroke_opacity = 1;
|
|
|
|
var current_fill_opacity = 1;
|
2009-08-20 13:50:41 +00:00
|
|
|
var current_text_fill = "#000000";
|
|
|
|
var current_text_stroke_width = 0;
|
2009-06-10 03:12:19 +00:00
|
|
|
var current_font_size = "12pt";
|
|
|
|
var current_font_family = "serif";
|
2009-06-01 21:24:30 +00:00
|
|
|
var freehand_min_x = null;
|
|
|
|
var freehand_max_x = null;
|
|
|
|
var freehand_min_y = null;
|
|
|
|
var freehand_max_y = null;
|
2009-08-08 14:03:14 +00:00
|
|
|
var current_poly = null;
|
2009-08-07 05:44:55 +00:00
|
|
|
var current_poly_pts = [];
|
2009-08-08 22:17:59 +00:00
|
|
|
var current_poly_pt_drag = -1;
|
2009-06-29 22:22:13 +00:00
|
|
|
// this will hold all the currently selected elements
|
|
|
|
// default size of 1 until it needs to grow bigger
|
|
|
|
var selectedElements = new Array(1);
|
2009-06-30 06:24:41 +00:00
|
|
|
// this holds the selected's bbox
|
2009-07-01 19:28:08 +00:00
|
|
|
var selectedBBoxes = new Array(1);
|
2009-06-30 06:24:41 +00:00
|
|
|
// this object manages selectors for us
|
2009-06-29 13:20:39 +00:00
|
|
|
var selectorManager = new SelectorManager();
|
2009-06-30 06:24:41 +00:00
|
|
|
var rubberBox = null;
|
2009-06-06 18:34:42 +00:00
|
|
|
var events = {};
|
2009-06-25 02:54:07 +00:00
|
|
|
var undoStackPointer = 0;
|
|
|
|
var undoStack = [];
|
2009-06-29 14:38:30 +00:00
|
|
|
|
2009-08-26 19:59:52 +00:00
|
|
|
var curBBoxes = [];
|
|
|
|
|
2009-07-02 04:50:24 +00:00
|
|
|
// This method sends back an array or a NodeList full of elements that
|
|
|
|
// intersect the multi-select rubber-band-box.
|
|
|
|
//
|
|
|
|
// Since the only browser that supports the SVG DOM getIntersectionList is Opera,
|
|
|
|
// we need to provide an implementation here. We brute-force it for now.
|
|
|
|
//
|
|
|
|
// Reference:
|
2009-07-01 19:28:08 +00:00
|
|
|
// 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
|
2009-07-01 15:34:15 +00:00
|
|
|
var getIntersectionList = function(rect) {
|
2009-07-02 04:50:24 +00:00
|
|
|
if (rubberBox == null) { return null; }
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-26 19:59:52 +00:00
|
|
|
if(!curBBoxes.length) {
|
|
|
|
// Cache all bboxes
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-02 04:50:24 +00:00
|
|
|
var nodes = svgroot.childNodes;
|
2009-07-03 02:49:42 +00:00
|
|
|
var i = svgroot.childNodes.length;
|
2009-08-26 19:59:52 +00:00
|
|
|
|
2009-07-03 02:49:42 +00:00
|
|
|
while (i--) {
|
2009-07-30 22:45:59 +00:00
|
|
|
// need to do this since the defs has no bbox and causes an exception
|
|
|
|
// to be thrown in Mozilla
|
|
|
|
try {
|
2009-08-26 21:50:35 +00:00
|
|
|
var box = canvas.getBBox(nodes[i]);
|
|
|
|
if (nodes[i].id != "selectorParentGroup" && box) {
|
|
|
|
curBBoxes.push({'elem':nodes[i], 'bbox':box});
|
2009-07-30 22:45:59 +00:00
|
|
|
}
|
|
|
|
} catch(e) {
|
|
|
|
// do nothing, this element did not have a bbox
|
2009-07-02 04:50:24 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-01 15:34:15 +00:00
|
|
|
}
|
2009-08-26 19:59:52 +00:00
|
|
|
|
|
|
|
var resultList = null;
|
|
|
|
try {
|
|
|
|
resultList = svgroot.getIntersectionList(rect, null);
|
|
|
|
} catch(e) { }
|
|
|
|
|
|
|
|
if (resultList == null || typeof(resultList.item) != "function") {
|
|
|
|
resultList = [];
|
|
|
|
|
|
|
|
var rubberBBox = rubberBox.getBBox();
|
|
|
|
var i = curBBoxes.length;
|
|
|
|
while (i--) {
|
|
|
|
if (Utils.rectsIntersect(rubberBBox, curBBoxes[i].bbox)) {
|
|
|
|
resultList.push(curBBoxes[i].elem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-02 04:50:24 +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
|
2009-07-01 15:34:15 +00:00
|
|
|
return resultList;
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 14:38:30 +00:00
|
|
|
// FIXME: we MUST compress consecutive text changes to the same element
|
2009-06-25 02:54:07 +00:00
|
|
|
// (right now each keystroke is saved as a separate command that includes the
|
|
|
|
// entire text contents of the text element)
|
|
|
|
// TODO: consider limiting the history that we store here (need to do some slicing)
|
2009-06-30 06:24:41 +00:00
|
|
|
var addCommandToHistory = function(cmd) {
|
2009-06-25 02:54:07 +00:00
|
|
|
// if our stack pointer is not at the end, then we have to remove
|
|
|
|
// all commands after the pointer and insert the new command
|
|
|
|
if (undoStackPointer < undoStack.length && undoStack.length > 0) {
|
|
|
|
undoStack = undoStack.splice(0, undoStackPointer);
|
|
|
|
}
|
2009-07-02 04:50:24 +00:00
|
|
|
undoStack.push(cmd);
|
2009-06-25 02:54:07 +00:00
|
|
|
undoStackPointer = undoStack.length;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-25 02:54:07 +00:00
|
|
|
|
2009-06-01 21:24:30 +00:00
|
|
|
// private functions
|
2009-06-05 12:33:02 +00:00
|
|
|
var getId = function() {
|
2009-06-25 08:22:30 +00:00
|
|
|
if (events["getid"]) return call("getid", obj_num);
|
|
|
|
return idprefix + obj_num;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-11 13:15:16 +00:00
|
|
|
var getNextId = function() {
|
|
|
|
// ensure the ID does not exist
|
|
|
|
var id = getId();
|
|
|
|
while (svgdoc.getElementById(id)) {
|
|
|
|
obj_num++;
|
|
|
|
id = getId();
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
return id;
|
2009-07-11 13:15:16 +00:00
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-06-06 18:34:42 +00:00
|
|
|
var call = function(event, arg) {
|
|
|
|
if (events[event]) {
|
|
|
|
return events[event](this,arg);
|
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 18:34:42 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
// this function sanitizes the input node and its children
|
|
|
|
// this function only keeps what is allowed from our whitelist defined above
|
|
|
|
var sanitizeSvg = function(node) {
|
|
|
|
// we only care about element nodes
|
2009-07-13 02:57:11 +00:00
|
|
|
// automatically return for all comment, etc nodes
|
|
|
|
// for text, we do a whitespace trim
|
|
|
|
if (node.nodeType == 3) {
|
|
|
|
node.nodeValue = node.nodeValue.replace(/^\s+|\s+$/g, "");
|
|
|
|
}
|
2009-07-10 05:04:06 +00:00
|
|
|
if (node.nodeType != 1) return;
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
var doc = node.ownerDocument;
|
|
|
|
var parent = node.parentNode;
|
|
|
|
// can parent ever be null here? I think the root node's parent is the document...
|
|
|
|
if (!doc || !parent) return;
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
var allowedAttrs = svgWhiteList[node.nodeName];
|
|
|
|
// if this element is allowed
|
|
|
|
if (allowedAttrs != undefined) {
|
|
|
|
var i = node.attributes.length;
|
|
|
|
while (i--) {
|
|
|
|
// if the attribute is not in our whitelist, then remove it
|
|
|
|
// could use jQuery's inArray(), but I don't know if that's any better
|
|
|
|
var attrName = node.attributes.item(i).nodeName;
|
|
|
|
if (allowedAttrs.indexOf(attrName) == -1) {
|
|
|
|
// TODO: do I need to call setAttribute(..., "") here for Fx2?
|
|
|
|
node.removeAttribute(attrName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// recurse to children
|
|
|
|
i = node.childNodes.length;
|
|
|
|
while (i--) { sanitizeSvg(node.childNodes.item(i)); }
|
|
|
|
}
|
|
|
|
// else, remove this element
|
|
|
|
else {
|
|
|
|
// remove all children from this node and insert them before this node
|
|
|
|
var children = [];
|
|
|
|
while (node.hasChildNodes()) {
|
|
|
|
children.push(parent.insertBefore(node.firstChild, node));
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
// remove this node from the document altogether
|
|
|
|
parent.removeChild(node);
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
// call sanitizeSvg on each of those children
|
|
|
|
var i = children.length;
|
|
|
|
while (i--) { sanitizeSvg(children[i]); }
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
}
|
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-08-19 17:08:05 +00:00
|
|
|
var removeUnusedGrads = function() {
|
|
|
|
var defs = svgroot.getElementsByTagNameNS(svgns, "defs");
|
2009-08-25 12:40:58 +00:00
|
|
|
if(!defs || !defs.length) return;
|
2009-08-19 17:08:05 +00:00
|
|
|
|
|
|
|
var all_els = svgroot.getElementsByTagNameNS(svgns, '*');
|
|
|
|
var grad_uses = [];
|
|
|
|
|
|
|
|
$.each(all_els, function(i, el) {
|
|
|
|
var fill = el.getAttribute('fill');
|
|
|
|
if(fill && fill.indexOf('url(#') == 0) {
|
|
|
|
//found gradient
|
|
|
|
grad_uses.push(fill);
|
|
|
|
}
|
|
|
|
|
|
|
|
var stroke = el.getAttribute('stroke');
|
|
|
|
if(stroke && stroke.indexOf('url(#') == 0) {
|
|
|
|
//found gradient
|
|
|
|
grad_uses.push(stroke);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var lgrads = svgroot.getElementsByTagNameNS(svgns, "linearGradient");
|
|
|
|
var grad_ids = [];
|
|
|
|
|
|
|
|
var i = lgrads.length;
|
|
|
|
while (i--) {
|
|
|
|
var grad = lgrads[i];
|
|
|
|
var id = grad.getAttribute('id');
|
|
|
|
var url_id = 'url(#' + id + ')';
|
|
|
|
if($.inArray(url_id, grad_uses) == -1) {
|
|
|
|
// Not found, so remove
|
|
|
|
grad.parentNode.removeChild(grad);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove defs if empty
|
|
|
|
var i = defs.length;
|
|
|
|
while (i--) {
|
|
|
|
var def = defs[i];
|
|
|
|
if(!def.getElementsByTagNameNS(svgns,'*').length) {
|
|
|
|
def.parentNode.removeChild(def);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-01 21:24:30 +00:00
|
|
|
var svgToString = function(elem, indent) {
|
2009-07-10 05:04:06 +00:00
|
|
|
var out = new Array();
|
2009-06-01 21:24:30 +00:00
|
|
|
if (elem) {
|
|
|
|
var attrs = elem.attributes;
|
|
|
|
var attr;
|
|
|
|
var i;
|
|
|
|
var childs = elem.childNodes;
|
2009-07-10 05:04:06 +00:00
|
|
|
for (i=0; i<indent; i++) out.push(" ");
|
|
|
|
out.push("<"); out.push(elem.nodeName);
|
2009-06-01 21:24:30 +00:00
|
|
|
for (i=attrs.length-1; i>=0; i--) {
|
|
|
|
attr = attrs.item(i);
|
2009-06-16 02:52:36 +00:00
|
|
|
if (attr.nodeValue != "") {
|
2009-07-10 05:04:06 +00:00
|
|
|
out.push(" "); out.push(attr.nodeName); out.push("=\"");
|
|
|
|
out.push(attr.nodeValue); out.push("\"");
|
2009-06-16 02:52:36 +00:00
|
|
|
}
|
2009-06-01 21:24:30 +00:00
|
|
|
}
|
|
|
|
if (elem.hasChildNodes()) {
|
2009-07-10 05:04:06 +00:00
|
|
|
out.push(">");
|
2009-06-01 21:24:30 +00:00
|
|
|
indent++;
|
2009-06-27 03:42:12 +00:00
|
|
|
var bOneLine = false;
|
2009-06-01 21:24:30 +00:00
|
|
|
for (i=0; i<childs.length; i++)
|
|
|
|
{
|
2009-06-30 17:50:08 +00:00
|
|
|
var child = childs.item(i);
|
|
|
|
if (child.id == "selectorParentGroup") continue;
|
2009-07-13 03:23:58 +00:00
|
|
|
switch(child.nodeType) {
|
|
|
|
case 1: // element node
|
2009-07-10 05:04:06 +00:00
|
|
|
out.push("\n");
|
|
|
|
out.push(svgToString(childs.item(i), indent));
|
2009-07-13 03:23:58 +00:00
|
|
|
break;
|
|
|
|
case 3: // text node
|
2009-07-13 02:57:11 +00:00
|
|
|
var str = child.nodeValue.replace(/^\s+|\s+$/g, "");
|
|
|
|
if (str != "") {
|
|
|
|
bOneLine = true;
|
|
|
|
out.push(str + "");
|
2009-07-13 00:17:15 +00:00
|
|
|
}
|
2009-07-13 03:23:58 +00:00
|
|
|
break;
|
|
|
|
case 8: // comment
|
|
|
|
out.push("\n");
|
|
|
|
out.push(new Array(indent+1).join(" "));
|
|
|
|
out.push("<!--");
|
|
|
|
out.push(child.data);
|
|
|
|
out.push("-->");
|
|
|
|
break;
|
|
|
|
} // switch on node type
|
2009-06-01 21:24:30 +00:00
|
|
|
}
|
|
|
|
indent--;
|
2009-06-29 14:38:30 +00:00
|
|
|
if (!bOneLine) {
|
2009-07-10 05:04:06 +00:00
|
|
|
out.push("\n");
|
2009-07-30 22:45:59 +00:00
|
|
|
for (i=0; i<indent; i++) out.push(" ");
|
2009-06-27 03:42:12 +00:00
|
|
|
}
|
2009-07-10 05:04:06 +00:00
|
|
|
out.push("</"); out.push(elem.nodeName); out.push(">");
|
2009-06-01 21:24:30 +00:00
|
|
|
} else {
|
2009-07-10 05:04:06 +00:00
|
|
|
out.push("/>");
|
2009-06-01 21:24:30 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-10 05:04:06 +00:00
|
|
|
return out.join('');
|
2009-06-30 06:24:41 +00:00
|
|
|
}; // end svgToString()
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-07-08 00:03:08 +00:00
|
|
|
var recalculateAllSelectedDimensions = function() {
|
2009-07-01 21:28:22 +00:00
|
|
|
var text = (current_resize_mode == "none" ? "position" : "size");
|
|
|
|
var batchCmd = new BatchCommand(text);
|
2009-07-01 19:54:51 +00:00
|
|
|
|
2009-07-08 00:03:08 +00:00
|
|
|
var i = selectedElements.length;
|
|
|
|
while(i--) {
|
|
|
|
var cmd = recalculateSelectedDimensions(i);
|
|
|
|
if (cmd) {
|
|
|
|
batchCmd.addSubCommand(cmd);
|
2009-07-01 19:54:51 +00:00
|
|
|
}
|
2009-07-08 00:03:08 +00:00
|
|
|
}
|
2009-06-29 14:38:30 +00:00
|
|
|
|
2009-07-08 00:03:08 +00:00
|
|
|
if (!batchCmd.isEmpty()) {
|
|
|
|
addCommandToHistory(batchCmd);
|
|
|
|
call("changed", selectedElements);
|
|
|
|
}
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-15 04:37:08 +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',
|
|
|
|
'l', 'l', 'l', 'l', // TODO: be less lazy below and map them to h and v
|
|
|
|
's', 's', 't', 't' ];
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-16 14:56:22 +00:00
|
|
|
// this function returns the command which resulted from the selected change
|
2009-07-08 00:03:08 +00:00
|
|
|
var recalculateSelectedDimensions = function(i) {
|
|
|
|
var selected = selectedElements[i];
|
|
|
|
if (selected == null) return null;
|
|
|
|
var selectedBBox = selectedBBoxes[i];
|
2009-08-16 15:07:00 +00:00
|
|
|
var box = canvas.getBBox(selected);
|
2009-07-08 00:03:08 +00:00
|
|
|
|
|
|
|
// if we have not moved/resized, then immediately leave
|
|
|
|
if (box.x == selectedBBox.x && box.y == selectedBBox.y &&
|
|
|
|
box.width == selectedBBox.width && box.height == selectedBBox.height) {
|
|
|
|
return null;
|
|
|
|
}
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2009-07-08 00:03:08 +00:00
|
|
|
// after this point, we have some change to this element
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-26 05:08:34 +00:00
|
|
|
var remap = function(x,y) {
|
|
|
|
return {
|
|
|
|
'x':parseInt(((x-box.x)/box.width)*selectedBBox.width + selectedBBox.x),
|
|
|
|
'y':parseInt(((y-box.y)/box.height)*selectedBBox.height + selectedBBox.y)
|
|
|
|
};
|
|
|
|
};
|
2009-08-15 04:37:08 +00:00
|
|
|
var scalew = function(w) {return parseInt(w*selectedBBox.width/box.width);}
|
|
|
|
var scaleh = function(h) {return parseInt(h*selectedBBox.height/box.height);}
|
2009-07-08 00:03:08 +00:00
|
|
|
|
|
|
|
var changes = {};
|
|
|
|
|
2009-08-16 14:56:22 +00:00
|
|
|
// if there was a rotation transform, re-set it, otherwise empty out the transform attribute
|
|
|
|
// This fixes Firefox 2- behavior - which does not reset values when the attribute has
|
|
|
|
// been removed, see https://bugzilla.mozilla.org/show_bug.cgi?id=320622
|
|
|
|
var angle = canvas.getRotationAngle(selected);
|
2009-08-18 16:59:01 +00:00
|
|
|
var pointGripContainer = document.getElementById("polypointgrip_container");
|
2009-08-16 19:18:22 +00:00
|
|
|
if (angle) {
|
2009-08-25 12:40:58 +00:00
|
|
|
var xform = selected.getAttribute('transform');
|
|
|
|
var matched_numbers = xform.substr(xform.indexOf('rotate(')).match(/([\d\.\-\+]+)/g);
|
2009-08-26 05:08:34 +00:00
|
|
|
// this is our old center upon which we have rotated the shape
|
2009-08-25 12:40:58 +00:00
|
|
|
var tr_x = parseFloat(matched_numbers[1]),
|
|
|
|
tr_y = parseFloat(matched_numbers[2]);
|
2009-08-26 05:08:34 +00:00
|
|
|
var cx = null, cy = null;
|
2009-08-25 12:40:58 +00:00
|
|
|
|
2009-08-26 05:08:34 +00:00
|
|
|
// if this was a resize, find the new cx,cy
|
|
|
|
if (xform.indexOf("scale(") != -1) {
|
|
|
|
var alpha = angle * Math.PI / 180.0;
|
|
|
|
|
|
|
|
// rotate new opposite corners of bbox by angle at old center
|
|
|
|
var dx = selectedBBox.x - tr_x,
|
|
|
|
dy = selectedBBox.y - tr_y,
|
|
|
|
r = Math.sqrt(dx*dx + dy*dy),
|
|
|
|
theta = Math.atan2(dy,dx) + alpha;
|
|
|
|
var left = r * Math.cos(theta) + tr_x,
|
|
|
|
top = r * Math.sin(theta) + tr_y;
|
|
|
|
|
|
|
|
dx += selectedBBox.width;
|
|
|
|
dy += selectedBBox.height;
|
|
|
|
r = Math.sqrt(dx*dx + dy*dy);
|
|
|
|
theta = Math.atan2(dy,dx) + alpha;
|
|
|
|
var right = r * Math.cos(theta) + tr_x,
|
|
|
|
bottom = r * Math.sin(theta) + tr_y;
|
|
|
|
|
|
|
|
// now find mid-point of line between top-left and bottom-right to find new center
|
|
|
|
cx = parseInt(left + (right-left)/2);
|
|
|
|
cy = parseInt(top + (bottom-top)/2);
|
|
|
|
|
|
|
|
// now that we know the center and the axis-aligned width/height, calculate the x,y
|
|
|
|
selectedBBox.x = parseInt(cx - selectedBBox.width/2),
|
|
|
|
selectedBBox.y = parseInt(cy - selectedBBox.height/2);
|
2009-08-25 12:40:58 +00:00
|
|
|
}
|
2009-08-26 05:08:34 +00:00
|
|
|
// if it was not a resize, then it was a translation only
|
|
|
|
else {
|
|
|
|
var tx = selectedBBox.x - box.x,
|
|
|
|
ty = selectedBBox.y - box.y;
|
|
|
|
cx = tr_x + tx;
|
|
|
|
cy = tr_y + ty;
|
2009-08-25 12:40:58 +00:00
|
|
|
}
|
|
|
|
|
2009-08-18 16:59:01 +00:00
|
|
|
var rotate = ["rotate(", angle, " ", cx, ",", cy, ")"].join('');
|
|
|
|
selected.setAttribute("transform", rotate);
|
2009-08-18 19:05:42 +00:00
|
|
|
if(pointGripContainer) {
|
|
|
|
pointGripContainer.setAttribute("transform", rotate);
|
|
|
|
}
|
2009-08-16 14:56:22 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
selected.setAttribute("transform", "");
|
|
|
|
selected.removeAttribute("transform");
|
2009-08-18 19:05:42 +00:00
|
|
|
if(pointGripContainer) {
|
|
|
|
pointGripContainer.setAttribute("transform", "");
|
|
|
|
pointGripContainer.removeAttribute("transform");
|
|
|
|
}
|
2009-08-16 14:56:22 +00:00
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-08 00:03:08 +00:00
|
|
|
switch (selected.tagName)
|
|
|
|
{
|
2009-08-20 05:33:34 +00:00
|
|
|
// NOTE: at the moment, there's no way to create an actual polygon element except by
|
|
|
|
// editing source or importing from somewhere else but we'll cover it here anyway
|
|
|
|
// polygon is handled just like polyline
|
2009-08-15 04:37:08 +00:00
|
|
|
case "polygon":
|
2009-08-07 17:19:32 +00:00
|
|
|
case "polyline":
|
2009-08-20 05:33:34 +00:00
|
|
|
// extract the points from the polygon/polyline, adjust it and write back the new points
|
|
|
|
// but first, save the old points
|
2009-08-07 17:19:32 +00:00
|
|
|
changes["points"] = selected.getAttribute("points");
|
|
|
|
var list = selected.points;
|
|
|
|
var len = list.numberOfItems;
|
|
|
|
var newpoints = "";
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
var pt = list.getItem(i);
|
2009-08-26 05:08:34 +00:00
|
|
|
pt = remap(pt.x,pt.y);
|
|
|
|
newpoints += pt.x + "," + pt.y + " ";
|
2009-08-07 17:19:32 +00:00
|
|
|
}
|
|
|
|
selected.setAttributeNS(null, "points", newpoints);
|
|
|
|
break;
|
2009-07-08 00:03:08 +00:00
|
|
|
case "path":
|
|
|
|
// extract the x,y from the path, adjust it and write back the new path
|
|
|
|
// but first, save the old path
|
|
|
|
changes["d"] = selected.getAttribute("d");
|
|
|
|
var M = selected.pathSegList.getItem(0);
|
|
|
|
var curx = M.x, cury = M.y;
|
2009-08-26 05:08:34 +00:00
|
|
|
var pt = remap(curx,cury);
|
|
|
|
var newd = "M" + pt.x + "," + pt.y;
|
2009-07-08 00:03:08 +00:00
|
|
|
var segList = selected.pathSegList;
|
|
|
|
var len = segList.numberOfItems;
|
2009-08-15 04:40:33 +00:00
|
|
|
// for all path segments in the path, we first turn them into relative path segments,
|
|
|
|
// then we remap the coordinates from the resize
|
2009-07-08 00:03:08 +00:00
|
|
|
for (var i = 1; i < len; ++i) {
|
2009-08-15 04:37:08 +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;
|
|
|
|
switch (type) {
|
|
|
|
case 1: // z,Z closepath (Z/z)
|
|
|
|
newd += "z";
|
|
|
|
continue;
|
2009-08-16 06:02:26 +00:00
|
|
|
// turn this into a relative segment then fall through
|
2009-08-15 04:37:08 +00:00
|
|
|
case 2: // absolute move (M)
|
|
|
|
case 4: // absolute line (L)
|
|
|
|
case 12: // absolute horizontal line (H)
|
|
|
|
case 14: // absolute vertical line (V)
|
|
|
|
case 18: // absolute smooth quad (T)
|
|
|
|
x -= curx;
|
|
|
|
y -= cury;
|
|
|
|
case 3: // relative move (m)
|
|
|
|
case 5: // relative line (l)
|
|
|
|
case 13: // relative horizontal line (h)
|
|
|
|
case 15: // relative vertical line (v)
|
|
|
|
case 19: // relative smooth quad (t)
|
|
|
|
curx += x;
|
|
|
|
cury += y;
|
2009-08-15 10:53:56 +00:00
|
|
|
newd += [" ", pathMap[type], scalew(x), ",", scaleh(y)].join('');
|
2009-08-15 04:37:08 +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)
|
|
|
|
curx += x;
|
|
|
|
cury += y;
|
|
|
|
newd += [" c", scalew(x1), ",", scaleh(y1), " ", scalew(x2), ",", scaleh(y2),
|
2009-08-15 10:53:56 +00:00
|
|
|
" ", scalew(x), ",", scaleh(y)].join('');
|
2009-08-15 04:37:08 +00:00
|
|
|
break;
|
|
|
|
case 8: // absolute quad (Q)
|
|
|
|
x -= curx; x1 -= curx;
|
|
|
|
y -= cury; y1 -= cury;
|
|
|
|
case 9: // relative quad (q)
|
|
|
|
curx += x;
|
|
|
|
cury += y;
|
2009-08-15 10:53:56 +00:00
|
|
|
newd += [" q", scalew(x1), ",", scaleh(y1), " ", scalew(x), ",", scaleh(y)].join('');
|
2009-08-15 04:37:08 +00:00
|
|
|
break;
|
|
|
|
case 10: // absolute elliptical arc (A)
|
|
|
|
x -= curx;
|
|
|
|
y -= cury;
|
|
|
|
case 11: // relative elliptical arc (a)
|
|
|
|
curx += x;
|
|
|
|
cury += y;
|
|
|
|
newd += [ "a", scalew(seg.r1), ",", scaleh(seg.r2), " ", seg.angle, " ",
|
|
|
|
(seg.largeArcFlag ? 1 : 0), " ", (seg.sweepFlag ? 1 : 0), " ",
|
2009-08-15 10:53:56 +00:00
|
|
|
scalew(x), ",", scaleh(y) ].join('')
|
2009-08-15 04:37:08 +00:00
|
|
|
break;
|
|
|
|
case 16: // absolute smooth cubic (S)
|
|
|
|
x -= curx; x2 -= curx;
|
|
|
|
y -= cury; y2 -= cury;
|
|
|
|
case 17: // relative smooth cubic (s)
|
|
|
|
curx += x;
|
|
|
|
cury += y;
|
2009-08-15 10:53:56 +00:00
|
|
|
newd += [" s", scalew(x2), ",", scaleh(y2), " ", scalew(x), ",", scaleh(y)].join('');
|
2009-08-15 04:37:08 +00:00
|
|
|
break;
|
|
|
|
} // switch on path segment type
|
|
|
|
} // for each segment
|
2009-07-08 00:03:08 +00:00
|
|
|
selected.setAttributeNS(null, "d", newd);
|
|
|
|
break;
|
|
|
|
case "line":
|
2009-08-16 06:02:26 +00:00
|
|
|
changes["x1"] = selected.getAttribute("x1");
|
|
|
|
changes["y1"] = selected.getAttribute("y1");
|
|
|
|
changes["x2"] = selected.getAttribute("x2");
|
|
|
|
changes["y2"] = selected.getAttribute("y2");
|
2009-08-26 05:08:34 +00:00
|
|
|
var pt1 = remap(changes["x1"],changes["y1"]),
|
|
|
|
pt2 = remap(changes["x2"],changes["y2"]);
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(selected, {
|
2009-08-26 05:08:34 +00:00
|
|
|
'x1': pt1.x,
|
|
|
|
'y1': pt1.y,
|
|
|
|
'x2': pt2.x,
|
|
|
|
'y2': pt2.y,
|
2009-08-24 20:00:48 +00:00
|
|
|
}, 1000);
|
2009-07-08 00:03:08 +00:00
|
|
|
break;
|
|
|
|
case "circle":
|
2009-08-16 06:02:26 +00:00
|
|
|
changes["cx"] = selected.getAttribute("cx");
|
|
|
|
changes["cy"] = selected.getAttribute("cy");
|
|
|
|
changes["r"] = selected.getAttribute("r");
|
2009-08-26 05:08:34 +00:00
|
|
|
var pt = remap(changes["cx"], changes["cy"]);
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(selected, {
|
2009-08-26 05:08:34 +00:00
|
|
|
'cx': pt.x,
|
|
|
|
'cy': pt.y,
|
2009-08-24 20:00:48 +00:00
|
|
|
|
|
|
|
// take the minimum of the new selected box's dimensions for the new circle radius
|
|
|
|
'r': Math.min(selectedBBox.width/2,selectedBBox.height/2)
|
|
|
|
}, 1000);
|
2009-07-08 00:03:08 +00:00
|
|
|
break;
|
|
|
|
case "ellipse":
|
2009-08-16 06:02:26 +00:00
|
|
|
changes["cx"] = selected.getAttribute("cx");
|
|
|
|
changes["cy"] = selected.getAttribute("cy");
|
|
|
|
changes["rx"] = selected.getAttribute("rx");
|
|
|
|
changes["ry"] = selected.getAttribute("ry");
|
2009-08-26 05:08:34 +00:00
|
|
|
var pt = remap(changes["cx"], changes["cy"]);
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(selected, {
|
2009-08-26 05:08:34 +00:00
|
|
|
'cx': pt.x,
|
|
|
|
'cy': pt.y,
|
2009-08-24 20:00:48 +00:00
|
|
|
'rx': scalew(changes["rx"]),
|
|
|
|
'ry': scaleh(changes["ry"])
|
|
|
|
}, 1000);
|
2009-07-08 00:03:08 +00:00
|
|
|
break;
|
|
|
|
case "text":
|
|
|
|
changes["x"] = selected.getAttribute("x");
|
|
|
|
changes["y"] = selected.getAttribute("y");
|
2009-08-26 05:08:34 +00:00
|
|
|
var pt = remap(changes["x"], changes["y"]);
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(selected, {
|
2009-08-26 05:08:34 +00:00
|
|
|
'x': pt.x,
|
|
|
|
'y': pt.y
|
2009-08-24 20:00:48 +00:00
|
|
|
}, 1000);
|
2009-07-08 00:03:08 +00:00
|
|
|
break;
|
|
|
|
case "rect":
|
2009-08-16 06:02:26 +00:00
|
|
|
changes["x"] = selected.getAttribute("x");
|
|
|
|
changes["y"] = selected.getAttribute("y");
|
|
|
|
changes["width"] = selected.getAttribute("width");
|
|
|
|
changes["height"] = selected.getAttribute("height");
|
2009-08-26 05:08:34 +00:00
|
|
|
var pt = remap(changes["x"], changes["y"]);
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(selected, {
|
2009-08-26 05:08:34 +00:00
|
|
|
'x': pt.x,
|
|
|
|
'y': pt.y,
|
2009-08-24 20:00:48 +00:00
|
|
|
'width': scalew(changes["width"]),
|
|
|
|
'height': scaleh(changes["height"])
|
|
|
|
}, 1000);
|
2009-07-08 00:03:08 +00:00
|
|
|
break;
|
|
|
|
default: // rect
|
|
|
|
console.log("Unknown shape type: " + selected.tagName);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (changes) {
|
|
|
|
return new ChangeElementCommand(selected, changes);
|
2009-06-23 07:48:15 +00:00
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-23 05:17:53 +00:00
|
|
|
|
2009-06-02 12:52:44 +00:00
|
|
|
// public events
|
2009-07-01 15:34:15 +00:00
|
|
|
|
|
|
|
this.clearSelection = function() {
|
|
|
|
if (selectedElements[0] == null) { return; }
|
2009-07-03 02:49:42 +00:00
|
|
|
var len = selectedElements.length;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
2009-07-01 15:34:15 +00:00
|
|
|
var elem = selectedElements[i];
|
|
|
|
if (elem == null) break;
|
|
|
|
selectorManager.releaseSelector(elem);
|
|
|
|
selectedElements[i] = null;
|
2009-07-01 19:28:08 +00:00
|
|
|
selectedBBoxes[i] = null;
|
2009-07-01 15:34:15 +00:00
|
|
|
}
|
2009-07-01 19:28:08 +00:00
|
|
|
call("selected", selectedElements);
|
2009-07-01 15:34:15 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-21 13:39:23 +00:00
|
|
|
this.addToSelection = function(elemsToAdd, showGrips) {
|
2009-07-01 15:34:15 +00:00
|
|
|
if (elemsToAdd.length == 0) { return; }
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-01 15:34:15 +00:00
|
|
|
// find the first null in our selectedElements array
|
|
|
|
var j = 0;
|
|
|
|
while (j < selectedElements.length) {
|
|
|
|
if (selectedElements[j] == null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++j;
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-01 15:34:15 +00:00
|
|
|
// now add each element consecutively
|
2009-07-03 02:49:42 +00:00
|
|
|
var i = elemsToAdd.length;
|
|
|
|
while (i--) {
|
2009-07-01 15:34:15 +00:00
|
|
|
var elem = elemsToAdd[i];
|
2009-07-01 19:28:08 +00:00
|
|
|
// we ignore any selectors
|
|
|
|
if (elem.id.substr(0,13) == "selectorGrip_") continue;
|
2009-07-01 15:34:15 +00:00
|
|
|
// if it's not already there, add it
|
|
|
|
if (selectedElements.indexOf(elem) == -1) {
|
2009-07-01 19:28:08 +00:00
|
|
|
selectedElements[j] = elem;
|
2009-08-16 15:07:00 +00:00
|
|
|
selectedBBoxes[j++] = this.getBBox(elem);
|
2009-07-01 15:34:15 +00:00
|
|
|
selectorManager.requestSelector(elem);
|
2009-07-01 19:28:08 +00:00
|
|
|
call("selected", selectedElements);
|
2009-07-01 15:34:15 +00:00
|
|
|
}
|
|
|
|
}
|
2009-08-21 13:39:23 +00:00
|
|
|
|
|
|
|
if(showGrips) {
|
|
|
|
selectorManager.requestSelector(selectedElements[0]).showGrips(elem.tagName != "text");
|
|
|
|
}
|
2009-07-01 15:34:15 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-01 15:34:15 +00:00
|
|
|
//
|
|
|
|
this.removeFromSelection = function(elemsToRemove) {
|
|
|
|
if (selectedElements[0] == null) { return; }
|
|
|
|
if (elemsToRemove.length == 0) { return; }
|
|
|
|
|
2009-08-17 06:56:55 +00:00
|
|
|
// find every element and remove it from our array copy
|
2009-07-01 15:34:15 +00:00
|
|
|
var newSelectedItems = new Array(selectedElements.length);
|
|
|
|
var j = 0;
|
2009-07-03 02:49:42 +00:00
|
|
|
var len = selectedElements.length;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
2009-07-01 15:34:15 +00:00
|
|
|
var elem = selectedElements[i];
|
|
|
|
if (elem) {
|
|
|
|
// keep the item
|
|
|
|
if (elemsToRemove.indexOf(elem) == -1) {
|
|
|
|
newSelectedItems[j++] = elem;
|
|
|
|
}
|
|
|
|
else { // remove the item and its selector
|
|
|
|
selectorManager.releaseSelector(elem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// the copy becomes the master now
|
|
|
|
selectedElements = newSelectedItems;
|
|
|
|
};
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2009-06-25 02:54:07 +00:00
|
|
|
// in mouseDown :
|
|
|
|
// - when we are in a create mode, the element is added to the canvas
|
2009-07-01 19:28:08 +00:00
|
|
|
// but the action is not recorded until mousing up
|
2009-06-29 14:38:30 +00:00
|
|
|
// - when we are in select mode, select the element, remember the position
|
2009-06-25 02:54:07 +00:00
|
|
|
// and do nothing else
|
2009-06-07 00:17:21 +00:00
|
|
|
var mouseDown = function(evt)
|
2009-06-01 21:24:30 +00:00
|
|
|
{
|
2009-06-29 15:08:40 +00:00
|
|
|
var x = evt.pageX - container.parentNode.offsetLeft + container.parentNode.scrollLeft;
|
|
|
|
var y = evt.pageY - container.parentNode.offsetTop + container.parentNode.scrollTop;
|
2009-08-19 17:08:05 +00:00
|
|
|
|
|
|
|
if($.inArray(current_mode, ['select', 'resize']) == -1) {
|
|
|
|
addGradient();
|
|
|
|
}
|
2009-08-24 17:40:41 +00:00
|
|
|
start_x = x;
|
|
|
|
start_y = y;
|
2009-08-19 17:08:05 +00:00
|
|
|
|
2009-06-03 08:41:25 +00:00
|
|
|
switch (current_mode) {
|
|
|
|
case "select":
|
2009-06-08 02:17:07 +00:00
|
|
|
started = true;
|
2009-06-25 02:54:07 +00:00
|
|
|
current_resize_mode = "none";
|
2009-06-07 05:07:23 +00:00
|
|
|
var t = evt.target;
|
2009-06-18 04:20:03 +00:00
|
|
|
// WebKit returns <div> when the canvas is clicked, Firefox/Opera return <svg>
|
2009-07-01 21:59:11 +00:00
|
|
|
var nodeName = t.nodeName.toLowerCase();
|
|
|
|
if (nodeName != "div" && nodeName != "svg") {
|
2009-07-01 19:54:51 +00:00
|
|
|
// if this element is not yet selected, clear selection and select it
|
|
|
|
if (selectedElements.indexOf(t) == -1) {
|
|
|
|
canvas.clearSelection();
|
|
|
|
canvas.addToSelection([t]);
|
2009-08-08 14:09:30 +00:00
|
|
|
current_poly = null;
|
2009-07-01 19:54:51 +00:00
|
|
|
}
|
2009-08-08 14:03:14 +00:00
|
|
|
// else if it's a poly, go into polyedit mode in mouseup
|
2009-06-30 06:24:41 +00:00
|
|
|
}
|
|
|
|
else {
|
2009-07-01 19:28:08 +00:00
|
|
|
canvas.clearSelection();
|
2009-06-30 06:24:41 +00:00
|
|
|
current_mode = "multiselect";
|
2009-07-01 19:28:08 +00:00
|
|
|
if (rubberBox == null) {
|
|
|
|
rubberBox = selectorManager.getRubberBandBox();
|
|
|
|
}
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(rubberBox, {
|
|
|
|
'x': start_x,
|
|
|
|
'y': start_y,
|
|
|
|
'width': 0,
|
|
|
|
'height': 0,
|
|
|
|
'display': 'inline'
|
|
|
|
}, 100);
|
2009-06-07 05:07:23 +00:00
|
|
|
}
|
2009-06-03 08:41:25 +00:00
|
|
|
break;
|
2009-06-23 05:17:53 +00:00
|
|
|
case "resize":
|
|
|
|
started = true;
|
|
|
|
start_x = x;
|
|
|
|
start_y = y;
|
|
|
|
break;
|
2009-06-03 08:41:25 +00:00
|
|
|
case "fhellipse":
|
|
|
|
case "fhrect":
|
|
|
|
case "path":
|
|
|
|
started = true;
|
2009-06-08 02:17:07 +00:00
|
|
|
start_x = x;
|
|
|
|
start_y = y;
|
2009-08-07 17:19:32 +00:00
|
|
|
d_attr = x + "," + y + " ";
|
2009-06-03 08:41:25 +00:00
|
|
|
addSvgElementFromJson({
|
2009-08-07 17:19:32 +00:00
|
|
|
"element": "polyline",
|
2009-06-03 08:41:25 +00:00
|
|
|
"attr": {
|
2009-08-07 17:19:32 +00:00
|
|
|
"points": d_attr,
|
2009-07-11 13:15:16 +00:00
|
|
|
"id": getNextId(),
|
2009-06-03 08:41:25 +00:00
|
|
|
"fill": "none",
|
|
|
|
"stroke": current_stroke,
|
|
|
|
"stroke-width": current_stroke_width,
|
|
|
|
"stroke-dasharray": current_stroke_style,
|
2009-06-04 13:43:58 +00:00
|
|
|
"stroke-opacity": current_stroke_opacity,
|
2009-08-16 06:02:26 +00:00
|
|
|
"stroke-linecap": "round",
|
|
|
|
"stroke-linejoin": "round",
|
2009-06-04 13:43:58 +00:00
|
|
|
"opacity": current_opacity / 2
|
2009-06-03 08:41:25 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
freehand_min_x = x;
|
|
|
|
freehand_max_x = x;
|
|
|
|
freehand_min_y = y;
|
|
|
|
freehand_max_y = y;
|
|
|
|
break;
|
|
|
|
case "square":
|
2009-06-25 02:54:07 +00:00
|
|
|
// FIXME: once we create the rect, we lose information that this was a square
|
2009-08-21 12:21:43 +00:00
|
|
|
// (for resizing purposes this could be important)
|
2009-06-03 08:41:25 +00:00
|
|
|
case "rect":
|
|
|
|
started = true;
|
|
|
|
start_x = x;
|
|
|
|
start_y = y;
|
|
|
|
addSvgElementFromJson({
|
|
|
|
"element": "rect",
|
|
|
|
"attr": {
|
|
|
|
"x": x,
|
|
|
|
"y": y,
|
|
|
|
"width": 0,
|
|
|
|
"height": 0,
|
2009-07-11 13:15:16 +00:00
|
|
|
"id": getNextId(),
|
2009-06-03 08:41:25 +00:00
|
|
|
"fill": current_fill,
|
|
|
|
"stroke": current_stroke,
|
|
|
|
"stroke-width": current_stroke_width,
|
|
|
|
"stroke-dasharray": current_stroke_style,
|
2009-06-04 13:43:58 +00:00
|
|
|
"stroke-opacity": current_stroke_opacity,
|
|
|
|
"fill-opacity": current_fill_opacity,
|
|
|
|
"opacity": current_opacity / 2
|
2009-06-03 08:41:25 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case "line":
|
|
|
|
started = true;
|
|
|
|
addSvgElementFromJson({
|
|
|
|
"element": "line",
|
|
|
|
"attr": {
|
|
|
|
"x1": x,
|
|
|
|
"y1": y,
|
|
|
|
"x2": x,
|
|
|
|
"y2": y,
|
2009-07-11 13:15:16 +00:00
|
|
|
"id": getNextId(),
|
2009-06-03 08:41:25 +00:00
|
|
|
"stroke": current_stroke,
|
|
|
|
"stroke-width": current_stroke_width,
|
|
|
|
"stroke-dasharray": current_stroke_style,
|
2009-06-04 13:43:58 +00:00
|
|
|
"stroke-opacity": current_stroke_opacity,
|
2009-06-17 17:36:00 +00:00
|
|
|
"fill": "none",
|
2009-06-04 13:43:58 +00:00
|
|
|
"opacity": current_opacity / 2
|
2009-06-03 08:41:25 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case "circle":
|
2009-06-03 15:00:48 +00:00
|
|
|
started = true;
|
|
|
|
addSvgElementFromJson({
|
|
|
|
"element": "circle",
|
|
|
|
"attr": {
|
|
|
|
"cx": x,
|
|
|
|
"cy": y,
|
|
|
|
"r": 0,
|
2009-07-11 13:15:16 +00:00
|
|
|
"id": getNextId(),
|
2009-06-03 15:00:48 +00:00
|
|
|
"fill": current_fill,
|
|
|
|
"stroke": current_stroke,
|
|
|
|
"stroke-width": current_stroke_width,
|
|
|
|
"stroke-dasharray": current_stroke_style,
|
2009-06-04 13:43:58 +00:00
|
|
|
"stroke-opacity": current_stroke_opacity,
|
|
|
|
"fill-opacity": current_fill_opacity,
|
|
|
|
"opacity": current_opacity / 2
|
2009-06-03 15:00:48 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
2009-06-03 08:41:25 +00:00
|
|
|
case "ellipse":
|
|
|
|
started = true;
|
|
|
|
addSvgElementFromJson({
|
|
|
|
"element": "ellipse",
|
|
|
|
"attr": {
|
|
|
|
"cx": x,
|
|
|
|
"cy": y,
|
|
|
|
"rx": 0,
|
|
|
|
"ry": 0,
|
2009-07-11 13:15:16 +00:00
|
|
|
"id": getNextId(),
|
2009-06-03 08:41:25 +00:00
|
|
|
"fill": current_fill,
|
|
|
|
"stroke": current_stroke,
|
|
|
|
"stroke-width": current_stroke_width,
|
|
|
|
"stroke-dasharray": current_stroke_style,
|
2009-06-04 13:43:58 +00:00
|
|
|
"stroke-opacity": current_stroke_opacity,
|
|
|
|
"fill-opacity": current_fill_opacity,
|
|
|
|
"opacity": current_opacity / 2
|
2009-06-03 08:41:25 +00:00
|
|
|
}
|
|
|
|
});
|
2009-06-03 15:00:48 +00:00
|
|
|
break;
|
2009-06-10 03:12:19 +00:00
|
|
|
case "text":
|
|
|
|
started = true;
|
|
|
|
var newText = addSvgElementFromJson({
|
|
|
|
"element": "text",
|
|
|
|
"attr": {
|
|
|
|
"x": x,
|
|
|
|
"y": y,
|
2009-07-11 13:15:16 +00:00
|
|
|
"id": getNextId(),
|
2009-08-20 13:50:41 +00:00
|
|
|
"fill": current_text_fill,
|
2009-06-10 03:12:19 +00:00
|
|
|
"stroke": current_stroke,
|
2009-08-20 13:50:41 +00:00
|
|
|
"stroke-width": current_text_stroke_width,
|
2009-06-10 03:12:19 +00:00
|
|
|
"stroke-dasharray": current_stroke_style,
|
|
|
|
"stroke-opacity": current_stroke_opacity,
|
|
|
|
"fill-opacity": current_fill_opacity,
|
2009-06-11 15:18:46 +00:00
|
|
|
// fix for bug where text elements were always 50% opacity
|
|
|
|
"opacity": current_opacity,
|
2009-06-10 03:12:19 +00:00
|
|
|
"font-size": current_font_size,
|
2009-08-04 03:14:23 +00:00
|
|
|
"font-family": current_font_family
|
2009-06-10 03:12:19 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
newText.textContent = "text";
|
|
|
|
break;
|
2009-08-07 05:44:55 +00:00
|
|
|
case "poly":
|
|
|
|
started = true;
|
|
|
|
break;
|
2009-08-08 14:03:14 +00:00
|
|
|
case "polyedit":
|
|
|
|
started = true;
|
2009-08-08 22:17:59 +00:00
|
|
|
var id = evt.target.id;
|
|
|
|
if (id.substr(0,14) == "polypointgrip_") {
|
|
|
|
current_poly_pt_drag = parseInt(id.substr(14));
|
|
|
|
}
|
2009-08-24 17:40:41 +00:00
|
|
|
|
|
|
|
if(current_poly_pt_drag == -1) {
|
|
|
|
canvas.clearSelection();
|
|
|
|
canvas.setMode("multiselect");
|
|
|
|
if (rubberBox == null) {
|
|
|
|
rubberBox = selectorManager.getRubberBandBox();
|
|
|
|
}
|
2009-08-24 18:39:22 +00:00
|
|
|
assignAttributes(rubberBox, {
|
|
|
|
'x': start_x,
|
|
|
|
'y': start_y,
|
|
|
|
'width': 0,
|
|
|
|
'height': 0,
|
|
|
|
'display': 'inline'
|
2009-08-24 19:19:27 +00:00
|
|
|
}, 100);
|
2009-08-24 17:40:41 +00:00
|
|
|
}
|
|
|
|
|
2009-08-26 19:45:28 +00:00
|
|
|
break;
|
|
|
|
case "rotate":
|
|
|
|
started = true;
|
2009-08-08 14:03:14 +00:00
|
|
|
break;
|
2009-08-07 05:44:55 +00:00
|
|
|
default:
|
|
|
|
console.log("Unknown mode in mousedown: " + current_mode);
|
|
|
|
break;
|
2009-06-01 21:24:30 +00:00
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-08-17 03:11:30 +00:00
|
|
|
// in this function we do not record any state changes yet (but we do update
|
2009-06-25 02:54:07 +00:00
|
|
|
// any elements that are still being created, moved or resized on the canvas)
|
2009-07-10 05:04:06 +00:00
|
|
|
// TODO: svgcanvas should just retain a reference to the image being dragged instead
|
|
|
|
// of the getId() and getElementById() funkiness - this will help us customize the ids
|
|
|
|
// a little bit for squares and polys
|
2009-06-07 00:17:21 +00:00
|
|
|
var mouseMove = function(evt)
|
2009-06-01 21:24:30 +00:00
|
|
|
{
|
2009-06-02 16:40:33 +00:00
|
|
|
if (!started) return;
|
2009-06-29 22:22:13 +00:00
|
|
|
var selected = selectedElements[0];
|
2009-06-29 15:08:40 +00:00
|
|
|
var x = evt.pageX - container.parentNode.offsetLeft + container.parentNode.scrollLeft;
|
|
|
|
var y = evt.pageY - container.parentNode.offsetTop + container.parentNode.scrollTop;
|
2009-06-05 12:33:02 +00:00
|
|
|
var shape = svgdoc.getElementById(getId());
|
2009-06-02 16:40:33 +00:00
|
|
|
switch (current_mode)
|
2009-06-01 21:24:30 +00:00
|
|
|
{
|
2009-06-08 02:17:07 +00:00
|
|
|
case "select":
|
|
|
|
// we temporarily use a translate on the element being dragged
|
2009-07-01 19:28:08 +00:00
|
|
|
// this transform is removed upon mousing up and the element is
|
|
|
|
// relocated to the new location
|
2009-07-01 19:54:51 +00:00
|
|
|
if (selectedElements[0] != null) {
|
2009-06-08 02:17:07 +00:00
|
|
|
var dx = x - start_x;
|
|
|
|
var dy = y - start_y;
|
2009-08-21 15:53:36 +00:00
|
|
|
|
2009-08-08 14:03:14 +00:00
|
|
|
if (dx != 0 || dy != 0) {
|
|
|
|
var ts = ["translate(",dx,",",dy,")"].join('');
|
|
|
|
var len = selectedElements.length;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
var selected = selectedElements[i];
|
|
|
|
if (selected == null) break;
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-16 15:07:00 +00:00
|
|
|
var box = canvas.getBBox(selected);
|
2009-08-21 15:53:36 +00:00
|
|
|
selectedBBoxes[i].x = box.x + dx;
|
|
|
|
selectedBBoxes[i].y = box.y + dy;
|
2009-08-16 14:56:22 +00:00
|
|
|
var angle = canvas.getRotationAngle(selected);
|
2009-08-16 19:18:22 +00:00
|
|
|
if (angle) {
|
2009-08-24 15:30:11 +00:00
|
|
|
var xform = selected.getAttribute('transform');
|
|
|
|
var matched_numbers = xform.substr(xform.indexOf('rotate(')).match(/([\d\.\-\+]+)/g);
|
2009-08-26 05:08:34 +00:00
|
|
|
var cx = matched_numbers[1],
|
|
|
|
cy = matched_numbers[2];
|
2009-08-16 14:56:22 +00:00
|
|
|
ts += [" rotate(", angle, " ", cx, ",", cy, ")"].join('');
|
2009-08-21 15:53:36 +00:00
|
|
|
|
|
|
|
var r = Math.sqrt( dx*dx + dy*dy );
|
|
|
|
var theta = Math.atan2(dy,dx) - angle * Math.PI / 180.0;
|
|
|
|
dx = r * Math.cos(theta);
|
|
|
|
dy = r * Math.sin(theta);
|
2009-08-16 14:56:22 +00:00
|
|
|
}
|
|
|
|
selected.setAttribute("transform", ts);
|
2009-08-21 15:53:36 +00:00
|
|
|
// update our internal bbox that we're tracking while dragging
|
2009-08-08 14:03:14 +00:00
|
|
|
box.x += dx; box.y += dy;
|
|
|
|
selectorManager.requestSelector(selected).resize(box);
|
|
|
|
}
|
2009-07-01 19:54:51 +00:00
|
|
|
}
|
2009-06-08 02:17:07 +00:00
|
|
|
}
|
2009-06-10 03:12:19 +00:00
|
|
|
break;
|
2009-06-30 06:24:41 +00:00
|
|
|
case "multiselect":
|
2009-08-24 19:19:27 +00:00
|
|
|
assignAttributes(rubberBox, {
|
|
|
|
'x': Math.min(start_x,x),
|
2009-08-24 19:20:52 +00:00
|
|
|
'y': Math.min(start_y,y),
|
2009-08-24 19:19:27 +00:00
|
|
|
'width': Math.abs(x-start_x),
|
|
|
|
'height': Math.abs(y-start_y)
|
|
|
|
},100);
|
2009-07-02 04:50:24 +00:00
|
|
|
|
|
|
|
// this code will probably be faster than using getIntersectionList(), but
|
|
|
|
// not as accurate (only grabs an element if the mouse happens to pass over
|
|
|
|
// its bbox and elements would never be released from selection)
|
|
|
|
// var nodeName = evt.target.nodeName.toLowerCase();
|
|
|
|
// if (nodeName != "div" && nodeName != "svg") {
|
|
|
|
// canvas.addToSelection([evt.target]);
|
|
|
|
// }
|
2009-07-03 02:49:42 +00:00
|
|
|
|
|
|
|
// clear out selection and set it to the new list
|
2009-07-02 04:50:24 +00:00
|
|
|
canvas.clearSelection();
|
|
|
|
canvas.addToSelection(getIntersectionList());
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-03 02:49:42 +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
|
|
|
|
var elemsToRemove = [];
|
|
|
|
var newList = getIntersectionList();
|
|
|
|
var len = selectedElements.length;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
var ind = newList.indexOf(selectedElements[i]);
|
|
|
|
if (ind == -1) {
|
|
|
|
elemsToRemove.push(selectedElements[i]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
newList[ind] = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (elemsToRemove.length > 0)
|
|
|
|
canvas.removeFromSelection(elemsToRemove);
|
|
|
|
*/
|
2009-06-30 06:24:41 +00:00
|
|
|
break;
|
2009-06-23 05:17:53 +00:00
|
|
|
case "resize":
|
|
|
|
// 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
|
2009-08-16 15:07:00 +00:00
|
|
|
var box=canvas.getBBox(selected), left=box.x, top=box.y, width=box.width,
|
2009-06-23 05:17:53 +00:00
|
|
|
height=box.height, dx=(x-start_x), dy=(y-start_y);
|
2009-08-22 19:08:01 +00:00
|
|
|
|
2009-08-21 15:53:36 +00:00
|
|
|
// if rotated, adjust the dx,dy values
|
2009-08-22 19:08:01 +00:00
|
|
|
var angle = canvas.getRotationAngle(selected);
|
2009-08-21 15:53:36 +00:00
|
|
|
if (angle) {
|
|
|
|
var r = Math.sqrt( dx*dx + dy*dy );
|
2009-08-22 19:08:01 +00:00
|
|
|
var theta = Math.atan2(dy,dx) - angle * Math.PI / 180.0;
|
2009-08-21 15:53:36 +00:00
|
|
|
dx = r * Math.cos(theta);
|
|
|
|
dy = r * Math.sin(theta);
|
|
|
|
}
|
|
|
|
|
2009-08-22 19:08:01 +00:00
|
|
|
// if not stretching in y direction, set dy to 0
|
|
|
|
// if not stretching in x direction, set dx to 0
|
|
|
|
if(current_resize_mode.indexOf("n")==-1 && current_resize_mode.indexOf("s")==-1) {
|
|
|
|
dy = 0;
|
|
|
|
}
|
|
|
|
if(current_resize_mode.indexOf("e")==-1 && current_resize_mode.indexOf("w")==-1) {
|
|
|
|
dx = 0;
|
|
|
|
}
|
|
|
|
|
2009-06-23 05:17:53 +00:00
|
|
|
var ts = null;
|
2009-08-22 19:08:01 +00:00
|
|
|
var tx = 0, ty = 0;
|
|
|
|
var sy = (height+dy)/height, sx = (width+dx)/width;
|
2009-06-23 05:17:53 +00:00
|
|
|
if(current_resize_mode.indexOf("n") != -1) {
|
|
|
|
sy = (height-dy)/height;
|
2009-08-22 19:08:01 +00:00
|
|
|
ty = height;
|
2009-06-23 05:17:53 +00:00
|
|
|
}
|
2009-08-22 19:08:01 +00:00
|
|
|
if(current_resize_mode.indexOf("w") != -1) {
|
2009-06-23 05:17:53 +00:00
|
|
|
sx = (width-dx)/width;
|
2009-08-22 19:08:01 +00:00
|
|
|
tx = width;
|
2009-06-23 05:17:53 +00:00
|
|
|
}
|
2009-08-22 19:08:01 +00:00
|
|
|
|
|
|
|
var selectedBBox = selectedBBoxes[0];
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2009-08-22 19:08:01 +00:00
|
|
|
// find the rotation transform and prepend it
|
|
|
|
var ts = [" translate(", (left+tx), ",", (top+ty), ") scale(", sx, ",", sy,
|
|
|
|
") translate(", -(left+tx), ",", -(top+ty), ")"].join('');
|
|
|
|
if (angle) {
|
2009-08-24 15:30:11 +00:00
|
|
|
var xform = selected.getAttribute('transform');
|
|
|
|
var matched_numbers = xform.substr(xform.indexOf('rotate(')).match(/([\d\.\-\+]+)/g);
|
2009-08-26 05:08:34 +00:00
|
|
|
var cx = matched_numbers[1],
|
|
|
|
cy = matched_numbers[2];
|
2009-08-22 19:08:01 +00:00
|
|
|
ts = ["rotate(", angle, " ", cx, ",", cy, ")", ts].join('')
|
|
|
|
}
|
|
|
|
selected.setAttribute("transform", ts);
|
|
|
|
|
|
|
|
if (tx) {
|
|
|
|
selectedBBox.x = left+dx;
|
|
|
|
}
|
|
|
|
if (ty) {
|
|
|
|
selectedBBox.y = top+dy;
|
|
|
|
}
|
2009-08-26 05:08:34 +00:00
|
|
|
selectedBBox.width = parseInt(width*sx);
|
|
|
|
selectedBBox.height = parseInt(height*sy);
|
2009-06-23 05:46:47 +00:00
|
|
|
// normalize selectedBBox
|
|
|
|
if (selectedBBox.width < 0) {
|
|
|
|
selectedBBox.x += selectedBBox.width;
|
2009-08-22 19:08:01 +00:00
|
|
|
selectedBBox.width *= -1;//-selectedBBox.width;
|
2009-06-23 05:46:47 +00:00
|
|
|
}
|
|
|
|
if (selectedBBox.height < 0) {
|
|
|
|
selectedBBox.y += selectedBBox.height;
|
2009-08-22 19:08:01 +00:00
|
|
|
selectedBBox.height *= -1;//-selectedBBox.height;
|
2009-08-16 14:56:22 +00:00
|
|
|
}
|
2009-07-08 00:03:08 +00:00
|
|
|
selectorManager.requestSelector(selected).resize(selectedBBox);
|
2009-06-23 07:48:15 +00:00
|
|
|
break;
|
2009-06-10 03:12:19 +00:00
|
|
|
case "text":
|
2009-08-24 19:19:27 +00:00
|
|
|
assignAttributes(shape,{
|
|
|
|
'x': x,
|
|
|
|
'y': y
|
|
|
|
},1000);
|
2009-06-10 03:12:19 +00:00
|
|
|
break;
|
2009-06-02 16:40:33 +00:00
|
|
|
case "line":
|
2009-07-30 22:45:59 +00:00
|
|
|
var handle = svgroot.suspendRedraw(1000);
|
2009-06-02 16:40:33 +00:00
|
|
|
shape.setAttributeNS(null, "x2", x);
|
|
|
|
shape.setAttributeNS(null, "y2", y);
|
2009-08-17 06:56:55 +00:00
|
|
|
svgroot.unsuspendRedraw(handle);
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "square":
|
|
|
|
var size = Math.max( Math.abs(x - start_x), Math.abs(y - start_y) );
|
2009-08-24 19:19:27 +00:00
|
|
|
assignAttributes(shape,{
|
|
|
|
'width': size,
|
|
|
|
'height': size,
|
|
|
|
'x': start_x < x ? start_x : start_x - size,
|
|
|
|
'y': start_y < y ? start_y : start_y - size
|
|
|
|
},1000);
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "rect":
|
2009-08-24 19:19:27 +00:00
|
|
|
assignAttributes(shape,{
|
|
|
|
'width': Math.abs(x-start_x),
|
|
|
|
'height': Math.abs(y-start_y),
|
|
|
|
'x': Math.min(start_x,x),
|
|
|
|
'y': Math.min(start_y,y)
|
|
|
|
},1000);
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "circle":
|
|
|
|
var cx = shape.getAttributeNS(null, "cx");
|
|
|
|
var cy = shape.getAttributeNS(null, "cy");
|
|
|
|
var rad = Math.sqrt( (x-cx)*(x-cx) + (y-cy)*(y-cy) );
|
2009-06-03 15:00:48 +00:00
|
|
|
shape.setAttributeNS(null, "r", rad);
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "ellipse":
|
|
|
|
var cx = shape.getAttributeNS(null, "cx");
|
|
|
|
var cy = shape.getAttributeNS(null, "cy");
|
2009-07-30 22:45:59 +00:00
|
|
|
var handle = svgroot.suspendRedraw(1000);
|
2009-06-02 16:40:33 +00:00
|
|
|
shape.setAttributeNS(null, "rx", Math.abs(x - cx) );
|
|
|
|
shape.setAttributeNS(null, "ry", Math.abs(y - cy) );
|
2009-08-17 06:56:55 +00:00
|
|
|
svgroot.unsuspendRedraw(handle);
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "fhellipse":
|
|
|
|
case "fhrect":
|
|
|
|
freehand_min_x = Math.min(x, freehand_min_x);
|
|
|
|
freehand_max_x = Math.max(x, freehand_max_x);
|
|
|
|
freehand_min_y = Math.min(y, freehand_min_y);
|
|
|
|
freehand_max_y = Math.max(y, freehand_max_y);
|
2009-06-03 08:41:25 +00:00
|
|
|
// break; missing on purpose
|
2009-06-02 16:40:33 +00:00
|
|
|
case "path":
|
2009-06-08 02:17:07 +00:00
|
|
|
start_x = x;
|
|
|
|
start_y = y;
|
2009-08-07 17:19:32 +00:00
|
|
|
d_attr += + x + "," + y + " ";
|
|
|
|
shape.setAttributeNS(null, "points", d_attr);
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
2009-08-07 17:19:32 +00:00
|
|
|
// update poly stretch line coordinates
|
2009-08-07 05:44:55 +00:00
|
|
|
case "poly":
|
|
|
|
var line = document.getElementById("poly_stretch_line");
|
|
|
|
if (line) {
|
|
|
|
line.setAttribute("x2", x);
|
|
|
|
line.setAttribute("y2", y);
|
|
|
|
}
|
|
|
|
break;
|
2009-08-08 22:17:59 +00:00
|
|
|
case "polyedit":
|
|
|
|
// if we are dragging a point, let's move it
|
|
|
|
if (current_poly_pt_drag != -1 && current_poly) {
|
|
|
|
var i = current_poly_pt_drag * 2;
|
2009-08-19 03:42:27 +00:00
|
|
|
|
|
|
|
// if the image is rotated, then we must modify the x,y mouse coordinates
|
|
|
|
// and rotate them into the shape's rotated coordinate system
|
|
|
|
|
|
|
|
// FIXME: the problem is that the element's rotation is controlled by
|
|
|
|
// two things: an angle and a rotation point (the center of the element).
|
|
|
|
// If the element's bbox is changed, its center changes. In this case,
|
|
|
|
// we keep the rotation center where it is (parse it out from the transform
|
2009-08-21 12:21:43 +00:00
|
|
|
// attribute), and move the poly point appropriately. This looks good while
|
2009-08-19 03:42:27 +00:00
|
|
|
// dragging, but looks funny when you subsequently rotate the element again.
|
2009-08-21 12:21:43 +00:00
|
|
|
var angle = canvas.getRotationAngle(current_poly) * Math.PI / 180.0;
|
2009-08-19 03:42:27 +00:00
|
|
|
if (angle) {
|
|
|
|
// extract the shape's (potentially) old 'center' from the transform attribute
|
|
|
|
var matched_numbers = current_poly.getAttribute('transform').match(/([\d\.\-\+]+)/g);
|
|
|
|
var cx = parseFloat(matched_numbers[1]),
|
|
|
|
cy = parseFloat(matched_numbers[2]);
|
|
|
|
var dx = x - cx, dy = y - cy;
|
|
|
|
var r = Math.sqrt( dx*dx + dy*dy );
|
|
|
|
var theta = Math.atan2(dy,dx) - angle;
|
|
|
|
x = cx + r * Math.cos(theta);
|
|
|
|
y = cy + r * Math.sin(theta);
|
|
|
|
}
|
|
|
|
|
2009-08-08 22:17:59 +00:00
|
|
|
current_poly_pts[i] = x;
|
|
|
|
current_poly_pts[i+1] = y;
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-08 22:17:59 +00:00
|
|
|
// reset the path's d attribute using current_poly_pts
|
|
|
|
var oldd = current_poly.getAttribute("d");
|
|
|
|
var closedPath = (oldd[oldd.length-1] == 'z' || oldd[oldd.length-1] == 'Z');
|
|
|
|
var len = current_poly_pts.length/2;
|
|
|
|
var arr = new Array(len+1);
|
|
|
|
var curx = current_poly_pts[0],
|
|
|
|
cury = current_poly_pts[1];
|
|
|
|
arr[0] = ["M", curx, ",", cury].join('');
|
|
|
|
for (var j = 1; j < len; ++j) {
|
|
|
|
var px = current_poly_pts[j*2], py = current_poly_pts[j*2+1];
|
2009-08-19 03:42:27 +00:00
|
|
|
arr[j] = ["l", parseInt(px-curx), ",", parseInt(py-cury)].join('');
|
2009-08-08 22:17:59 +00:00
|
|
|
curx = px;
|
|
|
|
cury = py;
|
|
|
|
}
|
|
|
|
if (closedPath) {
|
|
|
|
arr[len] = "z";
|
|
|
|
}
|
|
|
|
current_poly.setAttribute("d", arr.join(' '));
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-08 22:17:59 +00:00
|
|
|
// move the point grip
|
|
|
|
var grip = document.getElementById("polypointgrip_" + current_poly_pt_drag);
|
|
|
|
if (grip) {
|
|
|
|
grip.setAttribute("cx", x);
|
|
|
|
grip.setAttribute("cy", y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2009-08-26 19:45:28 +00:00
|
|
|
case "rotate":
|
2009-08-27 01:03:08 +00:00
|
|
|
var box = canvas.getBBox(selected),cx = box.x + box.width/2, cy = box.y + box.height/2;
|
2009-08-27 01:05:59 +00:00
|
|
|
canvas.setRotationAngle(parseInt(((Math.atan2(cy-y,cx-x) * (180/Math.PI))-90) % 360));
|
2009-08-26 19:45:28 +00:00
|
|
|
break;
|
2009-08-07 05:44:55 +00:00
|
|
|
default:
|
|
|
|
break;
|
2009-06-01 21:24:30 +00:00
|
|
|
}
|
2009-06-28 04:10:03 +00:00
|
|
|
// TODO: should we fire the change event here? I'm thinking only fire
|
2009-06-29 14:38:30 +00:00
|
|
|
// this event when the user mouses up. That's when the action (create,
|
2009-06-28 04:10:03 +00:00
|
|
|
// move, resize, draw) has finished
|
2009-07-08 00:03:08 +00:00
|
|
|
// Only question is whether in Wave Gadget mode whether we want to see the
|
|
|
|
// person live-dragging the element around (for instance)
|
2009-06-29 14:38:30 +00:00
|
|
|
// call("changed", selected);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2009-08-08 14:03:14 +00:00
|
|
|
var removeAllPointGripsFromPoly = function() {
|
|
|
|
// loop through and hide all pointgrips
|
|
|
|
var i = current_poly_pts.length/2;
|
|
|
|
while(i--) {
|
|
|
|
document.getElementById("polypointgrip_"+i).setAttribute("display", "none");
|
|
|
|
}
|
2009-08-14 15:50:11 +00:00
|
|
|
document.getElementById("poly_stretch_line").setAttribute("display", "none");
|
2009-08-08 14:03:14 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-08 03:36:01 +00:00
|
|
|
var addAllPointGripsToPoly = function() {
|
2009-08-08 14:03:14 +00:00
|
|
|
// loop through and hide all pointgrips
|
|
|
|
var i = current_poly_pts.length;
|
|
|
|
while(i) {
|
|
|
|
i -= 2;
|
|
|
|
var grip = document.getElementById("polypointgrip_"+i/2);
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(grip, {
|
|
|
|
'cx': current_poly_pts[i],
|
|
|
|
'cy': current_poly_pts[i+1],
|
|
|
|
'display': 'inline'
|
|
|
|
});
|
2009-08-08 14:03:14 +00:00
|
|
|
}
|
2009-08-08 03:36:01 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-08 03:36:01 +00:00
|
|
|
var addPointGripToPoly = function(x,y) {
|
|
|
|
// create the container of all the point grips
|
|
|
|
var pointGripContainer = document.getElementById("polypointgrip_container");
|
|
|
|
if (!pointGripContainer) {
|
|
|
|
var parent = document.getElementById("selectorParentGroup");
|
|
|
|
pointGripContainer = parent.appendChild(document.createElementNS(svgns, "g"));
|
|
|
|
pointGripContainer.id = "polypointgrip_container";
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-08 03:36:01 +00:00
|
|
|
// get index of this point
|
|
|
|
var index = current_poly_pts.length/2 - 1;
|
|
|
|
|
|
|
|
var pointGrip = document.getElementById("polypointgrip_"+index);
|
|
|
|
// create it
|
|
|
|
if (!pointGrip) {
|
|
|
|
pointGrip = document.createElementNS(svgns, "circle");
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(pointGrip, {
|
|
|
|
'id': "polypointgrip_" + index,
|
|
|
|
'display': "none",
|
|
|
|
'r': 4,
|
|
|
|
'fill': "#0F0",
|
|
|
|
'stroke': "#00F",
|
|
|
|
'stroke-width': 2,
|
|
|
|
'cursor': 'move',
|
|
|
|
"pointer-events": "all"
|
|
|
|
});
|
2009-08-08 03:36:01 +00:00
|
|
|
pointGrip = pointGripContainer.appendChild(pointGrip);
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-08 03:36:01 +00:00
|
|
|
var grip = $('#polypointgrip_'+index);
|
2009-08-08 14:03:14 +00:00
|
|
|
grip.mouseover( function() { this.setAttribute("stroke", "#F00"); } );
|
2009-08-08 03:36:01 +00:00
|
|
|
grip.mouseout( function() {this.setAttribute("stroke", "#00F"); } );
|
|
|
|
}
|
|
|
|
|
|
|
|
// set up the point grip element and display it
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(pointGrip, {
|
|
|
|
'cx': x,
|
|
|
|
'cy': y,
|
|
|
|
'display': "inline",
|
|
|
|
});
|
2009-08-08 03:36:01 +00:00
|
|
|
};
|
|
|
|
|
2009-06-25 02:54:07 +00:00
|
|
|
// - in create mode, the element's opacity is set properly, we create an InsertElementCommand
|
|
|
|
// and store it on the Undo stack
|
|
|
|
// - in move/resize mode, the element's attributes which were affected by the move/resize are
|
|
|
|
// identified, a ChangeElementCommand is created and stored on the stack for those attrs
|
2009-07-08 00:03:08 +00:00
|
|
|
// this is done in when we recalculate the selected dimensions()
|
2009-06-08 02:17:07 +00:00
|
|
|
var mouseUp = function(evt)
|
2009-06-01 21:24:30 +00:00
|
|
|
{
|
2009-06-02 16:40:33 +00:00
|
|
|
if (!started) return;
|
|
|
|
|
2009-08-08 14:03:14 +00:00
|
|
|
var x = evt.pageX - container.parentNode.offsetLeft + container.parentNode.scrollLeft;
|
|
|
|
var y = evt.pageY - container.parentNode.offsetTop + container.parentNode.scrollTop;
|
|
|
|
|
2009-06-02 16:40:33 +00:00
|
|
|
started = false;
|
2009-06-05 12:33:02 +00:00
|
|
|
var element = svgdoc.getElementById(getId());
|
2009-06-06 17:53:38 +00:00
|
|
|
var keep = false;
|
2009-06-02 16:40:33 +00:00
|
|
|
switch (current_mode)
|
2009-06-01 21:24:30 +00:00
|
|
|
{
|
2009-07-01 19:28:08 +00:00
|
|
|
// intentionally fall-through to select here
|
2009-06-23 05:17:53 +00:00
|
|
|
case "resize":
|
2009-07-01 19:28:08 +00:00
|
|
|
case "multiselect":
|
|
|
|
if (rubberBox != null) {
|
|
|
|
rubberBox.setAttribute("display", "none");
|
2009-08-26 19:59:52 +00:00
|
|
|
curBBoxes = [];
|
2009-07-01 19:28:08 +00:00
|
|
|
}
|
2009-06-23 05:17:53 +00:00
|
|
|
current_mode = "select";
|
2009-06-02 16:40:33 +00:00
|
|
|
case "select":
|
2009-07-01 19:28:08 +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];
|
|
|
|
current_fill = selected.getAttribute("fill");
|
|
|
|
current_fill_opacity = selected.getAttribute("fill-opacity");
|
|
|
|
current_stroke = selected.getAttribute("stroke");
|
|
|
|
current_stroke_opacity = selected.getAttribute("stroke-opacity");
|
|
|
|
current_stroke_width = selected.getAttribute("stroke-width");
|
|
|
|
current_stroke_style = selected.getAttribute("stroke-dasharray");
|
|
|
|
if (selected.tagName == "text") {
|
|
|
|
current_font_size = selected.getAttribute("font-size");
|
|
|
|
current_font_family = selected.getAttribute("font-family");
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-01 19:28:08 +00:00
|
|
|
selectorManager.requestSelector(selected).showGrips(selected.tagName != "text");
|
|
|
|
}
|
2009-08-08 14:03:14 +00:00
|
|
|
// if it was being dragged/resized
|
|
|
|
if (x != start_x || y != start_y) {
|
|
|
|
recalculateAllSelectedDimensions();
|
|
|
|
var len = selectedElements.length;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
if (selectedElements[i] == null) break;
|
|
|
|
selectorManager.requestSelector(selectedElements[i]).resize(selectedBBoxes[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// no change in position/size, so maybe we should move to polyedit
|
|
|
|
else {
|
|
|
|
// TODO: this causes a poly that was just going to be selected to go straight to polyedit
|
|
|
|
if (selectedElements[0].nodeName == "path" && selectedElements[1] == null) {
|
|
|
|
var t = evt.target;
|
|
|
|
if (current_poly == t) {
|
|
|
|
current_mode = "polyedit";
|
|
|
|
|
|
|
|
// recalculate current_poly_pts
|
|
|
|
current_poly_pts = [];
|
|
|
|
var segList = t.pathSegList;
|
|
|
|
var curx = segList.getItem(0).x, cury = segList.getItem(0).y;
|
|
|
|
current_poly_pts.push(curx);
|
|
|
|
current_poly_pts.push(cury);
|
|
|
|
var len = segList.numberOfItems;
|
|
|
|
for (var i = 1; i < len; ++i) {
|
|
|
|
var l = segList.getItem(i);
|
|
|
|
var x = l.x, y = l.y;
|
|
|
|
// polys can now be closed, skip Z segments
|
|
|
|
if (l.pathSegType == 1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
var type = l.pathSegType;
|
|
|
|
// current_poly_pts just holds the absolute coords
|
|
|
|
if (type == 4) {
|
|
|
|
curx = x;
|
|
|
|
cury = y;
|
|
|
|
} // type 4 (abs line)
|
|
|
|
else if (type == 5) {
|
|
|
|
curx += x;
|
|
|
|
cury += y;
|
|
|
|
} // type 5 (rel line)
|
|
|
|
current_poly_pts.push(curx);
|
|
|
|
current_poly_pts.push(cury);
|
|
|
|
} // for each segment
|
|
|
|
canvas.clearSelection();
|
|
|
|
addAllPointGripsToPoly();
|
|
|
|
} // going into polyedit mode
|
|
|
|
else {
|
|
|
|
current_poly = t;
|
|
|
|
}
|
|
|
|
} // no change in mouse position
|
2009-07-08 00:03:08 +00:00
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
}
|
2009-07-13 04:12:25 +00:00
|
|
|
// we return immediately from select so that the obj_num is not incremented
|
|
|
|
return;
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "path":
|
2009-06-06 17:53:38 +00:00
|
|
|
keep = true;
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "line":
|
2009-06-06 17:53:38 +00:00
|
|
|
keep = (element.getAttribute('x1') != element.getAttribute('x2') ||
|
2009-06-07 10:27:55 +00:00
|
|
|
element.getAttribute('y1') == element.getAttribute('y2'));
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "square":
|
|
|
|
case "rect":
|
2009-06-06 17:53:38 +00:00
|
|
|
keep = (element.getAttribute('width') != 0 ||
|
2009-06-07 10:27:55 +00:00
|
|
|
element.getAttribute('height') != 0);
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "circle":
|
2009-06-06 17:53:38 +00:00
|
|
|
keep = (element.getAttribute('r') != 0);
|
2009-06-03 15:00:48 +00:00
|
|
|
break;
|
2009-06-02 16:40:33 +00:00
|
|
|
case "ellipse":
|
2009-06-06 17:53:38 +00:00
|
|
|
keep = (element.getAttribute('rx') != 0 ||
|
2009-06-07 10:27:55 +00:00
|
|
|
element.getAttribute('ry') != 0);
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "fhellipse":
|
2009-06-03 08:41:25 +00:00
|
|
|
if ((freehand_max_x - freehand_min_x) > 0 &&
|
|
|
|
(freehand_max_y - freehand_min_y) > 0) {
|
2009-06-28 04:10:03 +00:00
|
|
|
element = addSvgElementFromJson({
|
2009-06-03 08:41:25 +00:00
|
|
|
"element": "ellipse",
|
|
|
|
"attr": {
|
|
|
|
"cx": (freehand_min_x + freehand_max_x) / 2,
|
|
|
|
"cy": (freehand_min_y + freehand_max_y) / 2,
|
|
|
|
"rx": (freehand_max_x - freehand_min_x) / 2,
|
|
|
|
"ry": (freehand_max_y - freehand_min_y) / 2,
|
2009-06-05 12:33:02 +00:00
|
|
|
"id": getId(),
|
2009-06-03 08:41:25 +00:00
|
|
|
"fill": current_fill,
|
|
|
|
"stroke": current_stroke,
|
|
|
|
"stroke-width": current_stroke_width,
|
|
|
|
"stroke-dasharray": current_stroke_style,
|
2009-06-04 13:43:58 +00:00
|
|
|
"opacity": current_opacity,
|
|
|
|
"stroke-opacity": current_stroke_opacity,
|
|
|
|
"fill-opacity": current_fill_opacity
|
2009-06-03 08:41:25 +00:00
|
|
|
}
|
2009-06-28 04:10:03 +00:00
|
|
|
});
|
2009-07-01 19:54:51 +00:00
|
|
|
call("changed",[element]);
|
2009-06-16 02:39:12 +00:00
|
|
|
keep = true;
|
2009-06-03 08:41:25 +00:00
|
|
|
}
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "fhrect":
|
2009-06-03 08:41:25 +00:00
|
|
|
if ((freehand_max_x - freehand_min_x) > 0 &&
|
|
|
|
(freehand_max_y - freehand_min_y) > 0) {
|
2009-06-28 04:10:03 +00:00
|
|
|
element = addSvgElementFromJson({
|
2009-06-03 08:41:25 +00:00
|
|
|
"element": "rect",
|
|
|
|
"attr": {
|
|
|
|
"x": freehand_min_x,
|
|
|
|
"y": freehand_min_y,
|
|
|
|
"width": (freehand_max_x - freehand_min_x),
|
|
|
|
"height": (freehand_max_y - freehand_min_y),
|
2009-06-05 12:33:02 +00:00
|
|
|
"id": getId(),
|
2009-06-03 08:41:25 +00:00
|
|
|
"fill": current_fill,
|
|
|
|
"stroke": current_stroke,
|
|
|
|
"stroke-width": current_stroke_width,
|
|
|
|
"stroke-dasharray": current_stroke_style,
|
2009-06-04 13:43:58 +00:00
|
|
|
"opacity": current_opacity,
|
|
|
|
"stroke-opacity": current_stroke_opacity,
|
|
|
|
"fill-opacity": current_fill_opacity
|
2009-06-03 08:41:25 +00:00
|
|
|
}
|
2009-06-28 04:10:03 +00:00
|
|
|
});
|
2009-07-01 19:54:51 +00:00
|
|
|
call("changed",[element]);
|
2009-06-16 02:39:12 +00:00
|
|
|
keep = true;
|
2009-06-03 08:41:25 +00:00
|
|
|
}
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
2009-06-10 03:12:19 +00:00
|
|
|
case "text":
|
|
|
|
keep = true;
|
2009-07-01 19:28:08 +00:00
|
|
|
canvas.clearSelection();
|
2009-06-10 03:12:19 +00:00
|
|
|
break;
|
2009-08-17 06:56:55 +00:00
|
|
|
case "poly":
|
2009-08-07 05:44:55 +00:00
|
|
|
// set element to null here so that it is not removed nor finalized
|
|
|
|
element = null;
|
|
|
|
// continue to be set to true so that mouseMove happens
|
|
|
|
started = true;
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-07 05:44:55 +00:00
|
|
|
var stretchy = document.getElementById("poly_stretch_line");
|
|
|
|
if (!stretchy) {
|
|
|
|
stretchy = document.createElementNS(svgns, "line");
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(stretchy, {
|
|
|
|
'id': "poly_stretch_line",
|
|
|
|
'stroke': "blue",
|
|
|
|
'stroke-width': "0.5"
|
|
|
|
});
|
2009-08-07 05:44:55 +00:00
|
|
|
stretchy = document.getElementById("selectorParentGroup").appendChild(stretchy);
|
|
|
|
}
|
|
|
|
stretchy.setAttribute("display", "inline");
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-07 05:44:55 +00:00
|
|
|
// if pts array is empty, create path element with M at current point
|
|
|
|
if (current_poly_pts.length == 0) {
|
|
|
|
current_poly_pts.push(x);
|
|
|
|
current_poly_pts.push(y);
|
|
|
|
d_attr = "M" + x + "," + y + " ";
|
|
|
|
addSvgElementFromJson({
|
|
|
|
"element": "path",
|
|
|
|
"attr": {
|
|
|
|
"d": d_attr,
|
|
|
|
"id": getNextId(),
|
|
|
|
"fill": current_fill,
|
|
|
|
"fill-opacity": current_fill_opacity,
|
|
|
|
"stroke": current_stroke,
|
|
|
|
"stroke-width": current_stroke_width,
|
|
|
|
"stroke-dasharray": current_stroke_style,
|
|
|
|
"stroke-opacity": current_stroke_opacity,
|
|
|
|
"opacity": current_opacity / 2
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// set stretchy line to first point
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(stretchy, {
|
|
|
|
'x1': x,
|
|
|
|
'y1': y,
|
|
|
|
'x2': x,
|
|
|
|
'y2': y
|
|
|
|
});
|
2009-08-08 03:36:01 +00:00
|
|
|
addPointGripToPoly(x,y);
|
2009-08-07 05:44:55 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// determine if we clicked on an existing point
|
|
|
|
var i = current_poly_pts.length;
|
|
|
|
var FUZZ = 6;
|
|
|
|
var clickOnPoint = false;
|
|
|
|
while(i) {
|
|
|
|
i -= 2;
|
|
|
|
var px = current_poly_pts[i], py = current_poly_pts[i+1];
|
|
|
|
// found a matching point
|
|
|
|
if ( x >= (px-FUZZ) && x <= (px+FUZZ) && y >= (py-FUZZ) && y <= (py+FUZZ) ) {
|
|
|
|
clickOnPoint = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get poly element that we are in the process of creating
|
|
|
|
var poly = svgdoc.getElementById(getId());
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-07 05:44:55 +00:00
|
|
|
// if we clicked on an existing point, then we are done this poly, commit it
|
|
|
|
// (i,i+1) are the x,y that were clicked on
|
|
|
|
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 poly open
|
|
|
|
// otherwise, close the poly
|
|
|
|
if (i == 0 && current_poly_pts.length >= 6) {
|
|
|
|
poly.setAttribute("d", d_attr + "z");
|
|
|
|
}
|
|
|
|
|
2009-08-08 14:03:14 +00:00
|
|
|
removeAllPointGripsFromPoly();
|
2009-08-08 03:36:01 +00:00
|
|
|
|
2009-08-07 05:44:55 +00:00
|
|
|
// this will signal to commit the poly
|
|
|
|
element = poly;
|
|
|
|
current_poly_pts = [];
|
|
|
|
started = false;
|
|
|
|
}
|
|
|
|
// else, create a new point, append to pts array, update path element
|
|
|
|
else {
|
|
|
|
var len = current_poly_pts.length;
|
|
|
|
var lastx = current_poly_pts[len-2], lasty = current_poly_pts[len-1];
|
|
|
|
// we store absolute values in our poly points array for easy checking above
|
|
|
|
current_poly_pts.push(x);
|
|
|
|
current_poly_pts.push(y);
|
|
|
|
// but we store relative coordinates in the d string of the poly for easy
|
|
|
|
// translation around the canvas in move mode
|
2009-08-07 05:55:11 +00:00
|
|
|
d_attr += "l" + parseInt(x-lastx) + "," + parseInt(y-lasty) + " ";
|
2009-08-07 05:44:55 +00:00
|
|
|
poly.setAttribute("d", d_attr);
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-07 05:44:55 +00:00
|
|
|
// set stretchy line to latest point
|
2009-08-24 20:00:48 +00:00
|
|
|
assignAttributes(stretchy, {
|
|
|
|
'x1': x,
|
|
|
|
'y1': y,
|
|
|
|
'x2': x,
|
|
|
|
'y2': y
|
|
|
|
});
|
2009-08-17 06:56:55 +00:00
|
|
|
addPointGripToPoly(x,y);
|
2009-08-07 05:44:55 +00:00
|
|
|
}
|
|
|
|
keep = true;
|
|
|
|
}
|
|
|
|
break;
|
2009-08-08 14:03:14 +00:00
|
|
|
case "polyedit":
|
|
|
|
keep = true;
|
|
|
|
element = null;
|
2009-08-08 22:17:59 +00:00
|
|
|
// if we were dragging a poly point, stop it now
|
|
|
|
if (current_poly_pt_drag != -1) {
|
|
|
|
current_poly_pt_drag = -1;
|
|
|
|
}
|
|
|
|
// else, move back to select mode
|
|
|
|
else {
|
|
|
|
current_mode = "select";
|
|
|
|
removeAllPointGripsFromPoly();
|
2009-08-21 12:21:43 +00:00
|
|
|
canvas.clearSelection();
|
2009-08-08 22:17:59 +00:00
|
|
|
canvas.addToSelection([evt.target]);
|
|
|
|
}
|
2009-08-08 14:03:14 +00:00
|
|
|
break;
|
2009-08-26 19:45:28 +00:00
|
|
|
case "rotate":
|
|
|
|
keep = true;
|
|
|
|
element = null;
|
|
|
|
current_mode = "select";
|
|
|
|
break;
|
2009-08-07 05:44:55 +00:00
|
|
|
default:
|
|
|
|
console.log("Unknown mode in mouseup: " + current_mode);
|
|
|
|
break;
|
2009-06-02 16:40:33 +00:00
|
|
|
}
|
2009-06-18 05:08:47 +00:00
|
|
|
if (!keep && element != null) {
|
2009-06-10 03:12:19 +00:00
|
|
|
element.parentNode.removeChild(element);
|
|
|
|
element = null;
|
|
|
|
} else if (element != null) {
|
2009-08-21 13:39:23 +00:00
|
|
|
canvas.addedNew = true;
|
2009-06-10 03:12:19 +00:00
|
|
|
element.setAttribute("opacity", current_opacity);
|
|
|
|
cleanupElement(element);
|
2009-06-30 17:50:08 +00:00
|
|
|
selectorManager.update();
|
2009-08-21 17:02:51 +00:00
|
|
|
canvas.addToSelection([element], true);
|
2009-06-25 02:54:07 +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()
|
|
|
|
addCommandToHistory(new InsertElementCommand(element));
|
2009-07-01 19:54:51 +00:00
|
|
|
call("changed",[element]);
|
2009-06-01 21:24:30 +00:00
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
|
|
|
// public functions
|
|
|
|
|
2009-08-25 16:35:57 +00:00
|
|
|
this.open = function(str) {
|
|
|
|
// Nothing by default, handled by optional widget/extention
|
|
|
|
call("opened", str);
|
2009-08-19 03:42:27 +00:00
|
|
|
};
|
|
|
|
|
2009-06-06 23:54:53 +00:00
|
|
|
this.save = function() {
|
2009-06-07 05:07:23 +00:00
|
|
|
// remove the selected outline before serializing
|
2009-07-01 15:34:15 +00:00
|
|
|
this.clearSelection();
|
2009-08-19 17:08:05 +00:00
|
|
|
|
|
|
|
// remove unused gradients
|
|
|
|
removeUnusedGrads();
|
|
|
|
|
2009-06-16 02:52:36 +00:00
|
|
|
var str = "<?xml version=\"1.0\" standalone=\"no\"?>\n";
|
2009-07-08 00:03:08 +00:00
|
|
|
// no need for doctype, see http://jwatt.org/svg/authoring/#doctype-declaration
|
2009-06-01 21:24:30 +00:00
|
|
|
str += svgToString(svgroot, 0);
|
2009-08-17 21:55:47 +00:00
|
|
|
call("saved", str);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
this.getSvgString = function() {
|
2009-08-19 17:08:05 +00:00
|
|
|
removeUnusedGrads();
|
2009-07-10 05:04:06 +00:00
|
|
|
return svgToString(svgroot, 0);
|
|
|
|
};
|
|
|
|
|
|
|
|
// this function returns false if the set was unsuccessful, true otherwise
|
2009-07-11 13:15:16 +00:00
|
|
|
// TODO: should this function keep throwing the exception?
|
2009-08-16 02:48:00 +00:00
|
|
|
// TODO: after parsing in the new text, do we need to synchronize getId()?
|
2009-07-10 05:04:06 +00:00
|
|
|
this.setSvgString = function(xmlString) {
|
|
|
|
try {
|
|
|
|
// convert string into XML document
|
|
|
|
var newDoc = Utils.text2xml(xmlString);
|
|
|
|
|
|
|
|
// run it through our sanitizer to remove anything we do not support
|
|
|
|
sanitizeSvg(newDoc.documentElement);
|
|
|
|
|
|
|
|
var batchCmd = new BatchCommand("Change Source");
|
|
|
|
|
2009-08-16 02:48:00 +00:00
|
|
|
// save our old selectorParentGroup
|
|
|
|
selectorManager.selectorParentGroup = svgroot.removeChild(selectorManager.selectorParentGroup);
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
// remove old root
|
|
|
|
var oldroot = container.removeChild(svgroot);
|
|
|
|
batchCmd.addSubCommand(new RemoveElementCommand(oldroot, container));
|
|
|
|
|
|
|
|
// set new root
|
|
|
|
svgroot = container.appendChild(svgdoc.importNode(newDoc.documentElement, true));
|
|
|
|
batchCmd.addSubCommand(new InsertElementCommand(svgroot));
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-16 02:48:00 +00:00
|
|
|
// add back in parentSelectorGroup
|
|
|
|
svgroot.appendChild(selectorManager.selectorParentGroup);
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
addCommandToHistory(batchCmd);
|
|
|
|
call("changed", [svgroot]);
|
|
|
|
} catch(e) {
|
|
|
|
console.log(e);
|
|
|
|
return false;
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
return true;
|
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
|
|
|
this.clear = function() {
|
|
|
|
var nodes = svgroot.childNodes;
|
|
|
|
var len = svgroot.childNodes.length;
|
|
|
|
var i = 0;
|
2009-07-01 15:34:15 +00:00
|
|
|
this.clearSelection();
|
2009-06-01 21:24:30 +00:00
|
|
|
for(var rep = 0; rep < len; rep++){
|
2009-06-02 16:40:33 +00:00
|
|
|
if (nodes[i].nodeType == 1) { // element node
|
2009-06-01 21:24:30 +00:00
|
|
|
nodes[i].parentNode.removeChild(nodes[i]);
|
|
|
|
} else {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
2009-07-02 03:54:11 +00:00
|
|
|
// clear the undo stack
|
|
|
|
resetUndoStack();
|
2009-06-12 12:20:48 +00:00
|
|
|
call("cleared");
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
this.getResolution = function() {
|
|
|
|
return [svgroot.getAttribute("width"), svgroot.getAttribute("height")];
|
|
|
|
};
|
2009-06-25 08:22:30 +00:00
|
|
|
this.setResolution = function(x, y) {
|
2009-06-26 14:31:03 +00:00
|
|
|
var w = svgroot.getAttribute("width"),
|
|
|
|
h = svgroot.getAttribute("height");
|
2009-07-30 22:45:59 +00:00
|
|
|
|
2009-08-17 06:56:55 +00:00
|
|
|
var handle = svgroot.suspendRedraw(1000);
|
2009-08-20 15:59:29 +00:00
|
|
|
|
|
|
|
if(!x) {
|
|
|
|
canvas.clearSelection();
|
|
|
|
|
|
|
|
// Get bounding box
|
|
|
|
var bbox = svgroot.getBBox();
|
|
|
|
|
|
|
|
if(bbox) {
|
|
|
|
x = bbox.x + bbox.width;
|
|
|
|
y = bbox.y + bbox.height;
|
|
|
|
} else {
|
|
|
|
alert('No content to fit to');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-25 08:22:30 +00:00
|
|
|
svgroot.setAttribute("width", x);
|
|
|
|
svgroot.setAttribute("height", y);
|
2009-08-20 15:59:29 +00:00
|
|
|
|
2009-08-17 06:56:55 +00:00
|
|
|
svgroot.unsuspendRedraw(handle);
|
2009-06-26 14:31:03 +00:00
|
|
|
addCommandToHistory(new ChangeElementCommand(svgroot, {"width":w,"height":h}, "resolution"));
|
2009-07-01 19:54:51 +00:00
|
|
|
call("changed", [svgroot]);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-25 08:22:30 +00:00
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getMode = function() {
|
|
|
|
return current_mode;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 12:25:26 +00:00
|
|
|
|
2009-06-01 21:24:30 +00:00
|
|
|
this.setMode = function(name) {
|
2009-08-07 05:44:55 +00:00
|
|
|
// toss out half-drawn poly
|
|
|
|
if (current_mode == "poly" && current_poly_pts.length > 0) {
|
2009-08-14 15:50:11 +00:00
|
|
|
var elem = svgdoc.getElementById(getId());
|
|
|
|
elem.parentNode.removeChild(elem);
|
|
|
|
removeAllPointGripsFromPoly();
|
|
|
|
canvas.clearSelection();
|
|
|
|
started = false;
|
2009-08-08 22:17:59 +00:00
|
|
|
current_poly = null;
|
2009-08-07 05:44:55 +00:00
|
|
|
current_poly_pts = [];
|
|
|
|
}
|
2009-08-17 03:57:43 +00:00
|
|
|
else if (current_mode == "polyedit") {
|
|
|
|
removeAllPointGripsFromPoly();
|
|
|
|
current_poly = null;
|
|
|
|
current_poly_pts = [];
|
|
|
|
}
|
2009-08-14 15:50:11 +00:00
|
|
|
current_mode = name;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getStrokeColor = function() {
|
|
|
|
return current_stroke;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 12:25:26 +00:00
|
|
|
|
2009-06-25 02:54:07 +00:00
|
|
|
this.setStrokeColor = function(val) {
|
|
|
|
current_stroke = val;
|
|
|
|
this.changeSelectedAttribute("stroke", val);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getFillColor = function() {
|
|
|
|
return current_fill;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 12:25:26 +00:00
|
|
|
|
2009-06-25 02:54:07 +00:00
|
|
|
this.setFillColor = function(val) {
|
2009-08-21 13:39:23 +00:00
|
|
|
if(selectedElements[0] != null && selectedElements[0].nodeName == 'text') {
|
2009-08-20 13:50:41 +00:00
|
|
|
current_text_fill = val;
|
|
|
|
} else {
|
|
|
|
current_fill = val;
|
|
|
|
}
|
|
|
|
|
2009-07-08 04:40:45 +00:00
|
|
|
// take out any path/line elements when setting fill
|
|
|
|
var elems = [];
|
|
|
|
var i = selectedElements.length;
|
|
|
|
while(i--) {
|
|
|
|
var elem = selectedElements[i];
|
2009-08-07 17:19:32 +00:00
|
|
|
if (elem && elem.tagName != "polyline" && elem.tagName != "line") {
|
2009-07-08 04:40:45 +00:00
|
|
|
elems.push(elem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (elems.length > 0)
|
|
|
|
this.changeSelectedAttribute("fill", val, elems);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-07-30 22:45:59 +00:00
|
|
|
var findDefs = function() {
|
|
|
|
var defs = svgroot.getElementsByTagNameNS(svgns, "defs");
|
|
|
|
if (defs.length > 0) {
|
|
|
|
defs = defs[0];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
defs = svgroot.insertBefore( svgdoc.createElementNS(svgns, "defs" ), svgroot.firstChild);
|
|
|
|
}
|
|
|
|
return defs;
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-19 17:08:05 +00:00
|
|
|
var addGradient = function() {
|
|
|
|
$.each(['stroke','fill'],function(i,type) {
|
|
|
|
|
|
|
|
if(type == 'stroke' && (!current_stroke_paint || current_stroke_paint.type == "solidColor")) return;
|
|
|
|
if(type == 'fill' && (!current_fill_paint || current_fill_paint.type == "solidColor")) return;
|
|
|
|
|
|
|
|
var grad = canvas[type + 'Grad'];
|
|
|
|
|
|
|
|
// find out if there is a duplicate gradient already in the defs
|
|
|
|
var duplicate_grad = findDuplicateGradient(grad);
|
|
|
|
var defs = findDefs();
|
|
|
|
|
|
|
|
// no duplicate found, so import gradient into defs
|
|
|
|
if (!duplicate_grad) {
|
|
|
|
grad = defs.appendChild( svgdoc.importNode(grad, true) );
|
|
|
|
|
|
|
|
// get next id and set it on the grad
|
|
|
|
grad.id = getNextId();
|
|
|
|
}
|
|
|
|
else { // use existing gradient
|
|
|
|
grad = duplicate_grad;
|
|
|
|
}
|
|
|
|
var functype = type=='fill'?'Fill':'Stroke';
|
|
|
|
|
|
|
|
canvas['set'+ functype +'Color']("url(#" + grad.id + ")");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2009-07-30 22:45:59 +00:00
|
|
|
var findDuplicateGradient = function(grad) {
|
|
|
|
var defs = findDefs();
|
|
|
|
var existing_grads = defs.getElementsByTagNameNS(svgns, "linearGradient");
|
|
|
|
var i = existing_grads.length;
|
|
|
|
while (i--) {
|
|
|
|
var og = existing_grads.item(i);
|
|
|
|
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'))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-30 22:45:59 +00:00
|
|
|
// else could be a duplicate, iterate through stops
|
|
|
|
var stops = grad.getElementsByTagNameNS(svgns, "stop");
|
|
|
|
var ostops = og.getElementsByTagNameNS(svgns, "stop");
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-30 22:45:59 +00:00
|
|
|
if (stops.length != ostops.length) {
|
|
|
|
continue;
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-30 22:45:59 +00:00
|
|
|
var j = stops.length;
|
|
|
|
while(j--) {
|
|
|
|
var stop = stops.item(j);
|
|
|
|
var ostop = ostops.item(j);
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-30 22:45:59 +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-08-17 06:56:55 +00:00
|
|
|
|
2009-07-30 22:45:59 +00:00
|
|
|
if (j == -1) {
|
|
|
|
return og;
|
|
|
|
}
|
|
|
|
} // for each gradient in defs
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-30 22:45:59 +00:00
|
|
|
return null;
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-19 17:08:05 +00:00
|
|
|
this.setStrokePaint = function(p, addGrad) {
|
2009-08-01 05:03:51 +00:00
|
|
|
current_stroke_paint = new $.jGraduate.Paint(p);
|
|
|
|
if (current_stroke_paint.type == "solidColor") {
|
|
|
|
this.setStrokeColor("#"+current_stroke_paint.solidColor);
|
2009-07-30 22:45:59 +00:00
|
|
|
}
|
2009-08-01 05:03:51 +00:00
|
|
|
else if(current_stroke_paint.type == "linearGradient") {
|
2009-08-19 17:08:05 +00:00
|
|
|
canvas.strokeGrad = current_stroke_paint.linearGradient;
|
|
|
|
if(addGrad) addGradient();
|
2009-07-30 22:45:59 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// console.log("none!");
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
this.setStrokeOpacity(current_stroke_paint.alpha/100);
|
2009-07-30 22:45:59 +00:00
|
|
|
};
|
|
|
|
|
2009-08-19 17:08:05 +00:00
|
|
|
this.setFillPaint = function(p, addGrad) {
|
2009-08-01 05:03:51 +00:00
|
|
|
// copy the incoming paint object
|
|
|
|
current_fill_paint = new $.jGraduate.Paint(p);
|
|
|
|
if (current_fill_paint.type == "solidColor") {
|
|
|
|
this.setFillColor("#"+current_fill_paint.solidColor);
|
2009-07-30 22:45:59 +00:00
|
|
|
}
|
2009-08-01 05:03:51 +00:00
|
|
|
else if(current_fill_paint.type == "linearGradient") {
|
2009-08-19 17:08:05 +00:00
|
|
|
canvas.fillGrad = current_fill_paint.linearGradient;
|
|
|
|
if(addGrad) addGradient();
|
2009-07-30 22:45:59 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// console.log("none!");
|
|
|
|
}
|
2009-08-01 05:03:51 +00:00
|
|
|
this.setFillOpacity(current_fill_paint.alpha/100);
|
2009-07-30 22:45:59 +00:00
|
|
|
};
|
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getStrokeWidth = function() {
|
|
|
|
return current_stroke_width;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 12:25:26 +00:00
|
|
|
|
2009-06-01 22:01:07 +00:00
|
|
|
this.setStrokeWidth = function(val) {
|
2009-08-21 13:39:23 +00:00
|
|
|
if(selectedElements[0] != null && selectedElements[0].nodeName == 'text') {
|
2009-08-20 13:50:41 +00:00
|
|
|
current_text_stroke_width = val;
|
|
|
|
} else {
|
|
|
|
current_stroke_width = val;
|
|
|
|
}
|
2009-06-25 02:54:07 +00:00
|
|
|
this.changeSelectedAttribute("stroke-width", val);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getStrokeStyle = function() {
|
|
|
|
return current_stroke_style;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 12:25:26 +00:00
|
|
|
|
2009-06-01 22:01:07 +00:00
|
|
|
this.setStrokeStyle = function(val) {
|
2009-06-02 16:40:33 +00:00
|
|
|
current_stroke_style = val;
|
2009-06-25 02:54:07 +00:00
|
|
|
this.changeSelectedAttribute("stroke-dasharray", val);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-01 22:01:07 +00:00
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getOpacity = function() {
|
|
|
|
return current_opacity;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 12:25:26 +00:00
|
|
|
|
2009-06-04 13:43:58 +00:00
|
|
|
this.setOpacity = function(val) {
|
|
|
|
current_opacity = val;
|
2009-06-25 02:54:07 +00:00
|
|
|
this.changeSelectedAttribute("opacity", val);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-04 13:43:58 +00:00
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getFillOpacity = function() {
|
|
|
|
return current_fill_opacity;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 12:25:26 +00:00
|
|
|
|
2009-06-04 13:43:58 +00:00
|
|
|
this.setFillOpacity = function(val) {
|
|
|
|
current_fill_opacity = val;
|
2009-06-25 02:54:07 +00:00
|
|
|
this.changeSelectedAttribute("fill-opacity", val);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-04 13:43:58 +00:00
|
|
|
|
2009-06-06 12:25:26 +00:00
|
|
|
this.getStrokeOpacity = function() {
|
|
|
|
return current_stroke_opacity;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 12:25:26 +00:00
|
|
|
|
2009-06-04 13:43:58 +00:00
|
|
|
this.setStrokeOpacity = function(val) {
|
|
|
|
current_stroke_opacity = val;
|
2009-06-25 02:54:07 +00:00
|
|
|
this.changeSelectedAttribute("stroke-opacity", val);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-16 15:07:00 +00:00
|
|
|
this.getBBox = function(elem) {
|
|
|
|
var selected = elem || selectedElements[0];
|
2009-08-23 16:39:45 +00:00
|
|
|
|
2009-08-24 12:55:22 +00:00
|
|
|
if(elem.nodeName == 'text' && selected.textContent == '') {
|
2009-08-21 20:12:01 +00:00
|
|
|
selected.textContent = 'a'; // Some character needed for the selector to use.
|
|
|
|
var ret = selected.getBBox();
|
|
|
|
selected.textContent = '';
|
|
|
|
} else {
|
|
|
|
var ret = selected.getBBox();
|
|
|
|
}
|
|
|
|
|
2009-08-19 04:49:50 +00:00
|
|
|
// get the bounding box from the DOM (which is in that element's coordinate system)
|
2009-08-21 20:12:01 +00:00
|
|
|
return ret;
|
2009-08-16 15:07:00 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-16 14:56:22 +00:00
|
|
|
this.getRotationAngle = function(elem) {
|
|
|
|
var selected = elem || selectedElements[0];
|
|
|
|
// find the rotation transform (if any) and set it
|
|
|
|
var tlist = selected.transform.baseVal;
|
|
|
|
var t = tlist.numberOfItems;
|
|
|
|
var foundRot = false;
|
|
|
|
while (t--) {
|
|
|
|
var xform = tlist.getItem(t);
|
|
|
|
if (xform.type == 4) {
|
|
|
|
return xform.angle;
|
|
|
|
}
|
|
|
|
}
|
2009-08-16 19:18:22 +00:00
|
|
|
return 0;
|
2009-08-16 14:56:22 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-16 02:48:00 +00:00
|
|
|
this.setRotationAngle = function(val) {
|
2009-08-16 06:02:26 +00:00
|
|
|
var elem = selectedElements[0];
|
2009-08-20 05:33:34 +00:00
|
|
|
// we use the actual element's bbox (not the calculated one) since the
|
|
|
|
// calculated bbox's center can change depending on the angle
|
|
|
|
var bbox = elem.getBBox(); //this.getBBox(elem);
|
2009-08-18 19:05:42 +00:00
|
|
|
var rotate = "rotate(" + val + " " +
|
2009-08-16 06:02:26 +00:00
|
|
|
(bbox.x+bbox.width/2) + "," +
|
2009-08-18 19:05:42 +00:00
|
|
|
(bbox.y+bbox.height/2) + ")";
|
|
|
|
this.changeSelectedAttribute("transform", rotate);
|
2009-08-18 16:59:01 +00:00
|
|
|
var pointGripContainer = document.getElementById("polypointgrip_container");
|
2009-08-18 19:05:42 +00:00
|
|
|
if(pointGripContainer) {
|
|
|
|
pointGripContainer.setAttribute("transform", rotate);
|
|
|
|
}
|
2009-08-26 13:59:25 +00:00
|
|
|
selectorManager.requestSelector(selectedElements[0]).updateGripCursors(val);
|
2009-08-16 02:48:00 +00:00
|
|
|
};
|
2009-06-04 13:43:58 +00:00
|
|
|
|
2009-06-09 22:13:13 +00:00
|
|
|
this.each = function(cb) {
|
|
|
|
$(svgroot).children().each(cb);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-09 22:13:13 +00:00
|
|
|
|
2009-06-06 18:34:42 +00:00
|
|
|
this.bind = function(event, f) {
|
|
|
|
events[event] = f;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-06 18:34:42 +00:00
|
|
|
|
2009-06-12 06:45:37 +00:00
|
|
|
this.setIdPrefix = function(p) {
|
|
|
|
idprefix = p;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-06 14:57:13 +00:00
|
|
|
this.getBold = function() {
|
|
|
|
// should only have one element selected
|
|
|
|
var selected = selectedElements[0];
|
|
|
|
if (selected != null && selected.tagName == "text" &&
|
|
|
|
selectedElements[1] == null)
|
|
|
|
{
|
|
|
|
return (selected.getAttribute("font-weight") == "bold");
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-06 14:57:13 +00:00
|
|
|
this.setBold = function(b) {
|
|
|
|
var selected = selectedElements[0];
|
|
|
|
if (selected != null && selected.tagName == "text" &&
|
|
|
|
selectedElements[1] == null)
|
|
|
|
{
|
2009-07-06 16:21:12 +00:00
|
|
|
this.changeSelectedAttribute("font-weight", b ? "bold" : "normal");
|
2009-07-06 14:57:13 +00:00
|
|
|
}
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-06 14:57:13 +00:00
|
|
|
this.getItalic = function() {
|
|
|
|
var selected = selectedElements[0];
|
|
|
|
if (selected != null && selected.tagName == "text" &&
|
|
|
|
selectedElements[1] == null)
|
|
|
|
{
|
|
|
|
return (selected.getAttribute("font-style") == "italic");
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-06 14:57:13 +00:00
|
|
|
this.setItalic = function(i) {
|
|
|
|
var selected = selectedElements[0];
|
|
|
|
if (selected != null && selected.tagName == "text" &&
|
|
|
|
selectedElements[1] == null)
|
|
|
|
{
|
2009-07-06 15:03:56 +00:00
|
|
|
this.changeSelectedAttribute("font-style", i ? "italic" : "normal");
|
2009-07-06 14:57:13 +00:00
|
|
|
}
|
|
|
|
};
|
2009-06-12 06:45:37 +00:00
|
|
|
|
2009-06-10 03:12:19 +00:00
|
|
|
this.getFontFamily = function() {
|
|
|
|
return current_font_family;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2009-06-10 03:12:19 +00:00
|
|
|
this.setFontFamily = function(val) {
|
|
|
|
current_font_family = val;
|
2009-06-29 14:38:30 +00:00
|
|
|
this.changeSelectedAttribute("font-family", val);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-10 03:12:19 +00:00
|
|
|
|
|
|
|
this.getFontSize = function() {
|
|
|
|
return current_font_size;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2009-06-10 03:12:19 +00:00
|
|
|
this.setFontSize = function(val) {
|
|
|
|
current_font_size = val;
|
2009-06-25 02:54:07 +00:00
|
|
|
this.changeSelectedAttribute("font-size", val);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2009-06-10 03:12:19 +00:00
|
|
|
this.getText = function() {
|
2009-06-29 22:22:13 +00:00
|
|
|
var selected = selectedElements[0];
|
2009-06-10 03:12:19 +00:00
|
|
|
if (selected == null) { return ""; }
|
|
|
|
return selected.textContent;
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2009-06-10 03:12:19 +00:00
|
|
|
this.setTextContent = function(val) {
|
2009-06-25 02:54:07 +00:00
|
|
|
this.changeSelectedAttribute("#text", val);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2009-06-11 15:18:46 +00:00
|
|
|
this.setRectRadius = function(val) {
|
2009-06-29 22:22:13 +00:00
|
|
|
var selected = selectedElements[0];
|
2009-06-11 15:18:46 +00:00
|
|
|
if (selected != null && selected.tagName == "rect") {
|
2009-06-25 02:54:07 +00:00
|
|
|
var r = selected.getAttribute("rx");
|
|
|
|
if (r != val) {
|
|
|
|
selected.setAttribute("rx", val);
|
|
|
|
selected.setAttribute("ry", val);
|
|
|
|
addCommandToHistory(new ChangeElementCommand(selected, {"rx":r, "ry":r}, "Radius"));
|
2009-07-01 19:54:51 +00:00
|
|
|
call("changed", [selected]);
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
2009-06-11 15:18:46 +00:00
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-29 14:38:30 +00:00
|
|
|
|
2009-07-08 04:40:45 +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.
|
|
|
|
this.changeSelectedAttribute = function(attr, val, elems) {
|
|
|
|
var elems = elems || selectedElements;
|
2009-07-01 21:28:22 +00:00
|
|
|
var batchCmd = new BatchCommand("Change " + attr);
|
2009-07-08 04:40:45 +00:00
|
|
|
var i = elems.length;
|
2009-07-30 22:45:59 +00:00
|
|
|
var handle = svgroot.suspendRedraw(1000);
|
2009-07-08 04:40:45 +00:00
|
|
|
while(i--) {
|
|
|
|
var elem = elems[i];
|
|
|
|
if (elem == null) continue;
|
2009-08-21 20:12:01 +00:00
|
|
|
|
2009-07-08 04:40:45 +00:00
|
|
|
var oldval = (attr == "#text" ? elem.textContent : elem.getAttribute(attr));
|
2009-06-25 02:54:07 +00:00
|
|
|
if (oldval != val) {
|
2009-07-08 04:40:45 +00:00
|
|
|
if (attr == "#text") elem.textContent = val;
|
|
|
|
else elem.setAttribute(attr, val);
|
2009-08-16 15:07:00 +00:00
|
|
|
selectedBBoxes[i] = this.getBBox(elem);
|
2009-08-13 15:42:41 +00:00
|
|
|
// Timeout needed for Opera & Firefox
|
|
|
|
setTimeout(function() {
|
|
|
|
selectorManager.requestSelector(elem).resize(selectedBBoxes[i]);
|
|
|
|
},0);
|
2009-06-25 02:54:07 +00:00
|
|
|
var changes = {};
|
|
|
|
changes[attr] = oldval;
|
2009-08-23 03:18:59 +00:00
|
|
|
|
|
|
|
// if this element was rotated, and we changed the position of this element
|
|
|
|
// we need to update the rotational transform attribute and store it as part
|
|
|
|
// of our changeset
|
|
|
|
var angle = canvas.getRotationAngle(elem);
|
|
|
|
if (angle) {
|
|
|
|
var cx = selectedBBoxes[i].x + selectedBBoxes[i].width/2,
|
|
|
|
cy = selectedBBoxes[i].y + selectedBBoxes[i].height/2;
|
|
|
|
var rotate = ["rotate(", angle, " ", cx, ",", cy, ")"].join('');
|
|
|
|
if (rotate != elem.getAttribute("transform")) {
|
|
|
|
elem.setAttribute("transform", rotate);
|
|
|
|
changes['transform'] = rotate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-08 04:40:45 +00:00
|
|
|
batchCmd.addSubCommand(new ChangeElementCommand(elem, changes, attr));
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
2009-06-24 04:33:09 +00:00
|
|
|
}
|
2009-07-30 22:45:59 +00:00
|
|
|
svgroot.unsuspendRedraw(handle);
|
2009-07-08 04:40:45 +00:00
|
|
|
if (!batchCmd.isEmpty()) {
|
|
|
|
addCommandToHistory(batchCmd);
|
|
|
|
call("changed", elems);
|
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2009-06-07 00:17:21 +00:00
|
|
|
$(container).mouseup(mouseUp);
|
|
|
|
$(container).mousedown(mouseDown);
|
|
|
|
$(container).mousemove(mouseMove);
|
2009-06-01 17:35:22 +00:00
|
|
|
|
2009-07-01 20:22:29 +00:00
|
|
|
this.deleteSelectedElements = function() {
|
2009-07-01 21:28:22 +00:00
|
|
|
var batchCmd = new BatchCommand("Delete Elements");
|
2009-07-03 02:49:42 +00:00
|
|
|
var len = selectedElements.length;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
2009-07-01 20:22:29 +00:00
|
|
|
var selected = selectedElements[i];
|
|
|
|
if (selected == null) break;
|
|
|
|
|
2009-06-25 02:54:07 +00:00
|
|
|
var parent = selected.parentNode;
|
2009-06-10 04:44:59 +00:00
|
|
|
var t = selected;
|
2009-07-01 20:22:29 +00:00
|
|
|
// this will unselect the element and remove the selectedOutline
|
|
|
|
selectorManager.releaseSelector(t);
|
2009-06-25 02:54:07 +00:00
|
|
|
var elem = parent.removeChild(t);
|
2009-07-01 20:22:29 +00:00
|
|
|
selectedElements[i] = null;
|
2009-07-01 21:28:22 +00:00
|
|
|
batchCmd.addSubCommand(new RemoveElementCommand(elem, parent));
|
2009-06-10 04:44:59 +00:00
|
|
|
}
|
2009-07-01 21:28:22 +00:00
|
|
|
if (!batchCmd.isEmpty()) addCommandToHistory(batchCmd);
|
2009-07-01 20:22:29 +00:00
|
|
|
call("selected", selectedElements);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-10 04:44:59 +00:00
|
|
|
|
2009-06-17 20:08:53 +00:00
|
|
|
this.moveToTopSelectedElement = function() {
|
2009-06-29 22:22:13 +00:00
|
|
|
var selected = selectedElements[0];
|
2009-06-17 20:08:53 +00:00
|
|
|
if (selected != null) {
|
|
|
|
var t = selected;
|
2009-06-25 02:54:07 +00:00
|
|
|
var oldParent = t.parentNode;
|
|
|
|
var oldNextSibling = t.nextSibling;
|
2009-06-29 13:20:39 +00:00
|
|
|
if (oldNextSibling == selectorManager.selectorParentGroup) oldNextSibling = null;
|
2009-06-25 02:54:07 +00:00
|
|
|
t = t.parentNode.appendChild(t);
|
|
|
|
addCommandToHistory(new MoveElementCommand(t, oldNextSibling, oldParent, "top"));
|
2009-06-17 20:08:53 +00:00
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-17 20:08:53 +00:00
|
|
|
|
|
|
|
this.moveToBottomSelectedElement = function() {
|
2009-06-29 22:22:13 +00:00
|
|
|
var selected = selectedElements[0];
|
2009-06-17 20:08:53 +00:00
|
|
|
if (selected != null) {
|
|
|
|
var t = selected;
|
2009-06-25 02:54:07 +00:00
|
|
|
var oldParent = t.parentNode;
|
|
|
|
var oldNextSibling = t.nextSibling;
|
2009-06-29 13:20:39 +00:00
|
|
|
if (oldNextSibling == selectorManager.selectorParentGroup) oldNextSibling = null;
|
2009-07-30 22:45:59 +00:00
|
|
|
var firstChild = t.parentNode.firstChild;
|
|
|
|
if (firstChild.tagName == 'defs') {
|
|
|
|
firstChild = firstChild.nextSibling;
|
|
|
|
}
|
|
|
|
t = t.parentNode.insertBefore(t, firstChild);
|
2009-06-25 02:54:07 +00:00
|
|
|
addCommandToHistory(new MoveElementCommand(t, oldNextSibling, oldParent, "bottom"));
|
2009-06-17 20:08:53 +00:00
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-29 14:38:30 +00:00
|
|
|
|
2009-07-13 13:51:00 +00:00
|
|
|
this.moveSelectedElements = function(dx,dy,undoable) {
|
|
|
|
// if undoable is not sent, default to true
|
2009-08-08 03:36:01 +00:00
|
|
|
var undoable = undoable || true;
|
2009-07-08 00:03:08 +00:00
|
|
|
var batchCmd = new BatchCommand("position");
|
|
|
|
var i = selectedElements.length;
|
|
|
|
while (i--) {
|
|
|
|
var selected = selectedElements[i];
|
|
|
|
if (selected != null) {
|
2009-08-16 15:07:00 +00:00
|
|
|
selectedBBoxes[i] = this.getBBox(selected);
|
2009-08-23 17:16:27 +00:00
|
|
|
// dx and dy could be arrays
|
|
|
|
if (dx.constructor == Array) {
|
|
|
|
selectedBBoxes[i].x += dx[i];
|
|
|
|
} else {
|
|
|
|
selectedBBoxes[i].x += dx;
|
|
|
|
}
|
|
|
|
if (dy.constructor == Array) {
|
|
|
|
selectedBBoxes[i].y += dy[i];
|
|
|
|
} else {
|
|
|
|
selectedBBoxes[i].y += dy;
|
|
|
|
}
|
2009-07-08 00:03:08 +00:00
|
|
|
var cmd = recalculateSelectedDimensions(i);
|
|
|
|
if (cmd) {
|
|
|
|
batchCmd.addSubCommand(cmd);
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
selectorManager.requestSelector(selected).resize(selectedBBoxes[i]);
|
2009-07-08 00:03:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!batchCmd.isEmpty()) {
|
2009-07-13 13:51:00 +00:00
|
|
|
if (undoable)
|
|
|
|
addCommandToHistory(batchCmd);
|
2009-07-08 00:03:08 +00:00
|
|
|
call("changed", selectedElements);
|
2009-06-23 21:18:23 +00:00
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-17 20:08:53 +00:00
|
|
|
|
2009-08-21 13:39:23 +00:00
|
|
|
this.cycleElement = function(next) {
|
|
|
|
var cur_elem = selectedElements[0];
|
|
|
|
var elem = false;
|
|
|
|
if (cur_elem == null) {
|
|
|
|
if(next) {
|
|
|
|
var elem = svgroot.firstChild;
|
|
|
|
if (elem.tagName == 'defs') {
|
|
|
|
elem = elem.nextSibling;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var elem = svgroot.lastChild;
|
|
|
|
var id = elem.getAttribute('id');
|
|
|
|
if(!id || id.indexOf('svg_') != 0){
|
|
|
|
elem = elem.previousSibling;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var type = next?'next':'previous';
|
|
|
|
var elem = cur_elem[type + 'Sibling'];
|
|
|
|
if(!elem) return;
|
|
|
|
|
|
|
|
var id = elem.getAttribute('id');
|
|
|
|
if(!id || id.indexOf('svg_') != 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
canvas.clearSelection();
|
|
|
|
canvas.addToSelection([elem], true);
|
|
|
|
call("selected", selectedElements);
|
|
|
|
}
|
|
|
|
|
2009-07-02 03:54:11 +00:00
|
|
|
var resetUndoStack = function() {
|
|
|
|
undoStack = [];
|
|
|
|
undoStackPointer = 0;
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
|
|
|
this.getUndoStackSize = function() { return undoStackPointer; };
|
2009-07-06 18:54:38 +00:00
|
|
|
this.getRedoStackSize = function() { return undoStack.length - undoStackPointer; };
|
|
|
|
|
|
|
|
this.getNextUndoCommandText = function() {
|
|
|
|
if (undoStackPointer > 0)
|
|
|
|
return undoStack[undoStackPointer-1].text;
|
|
|
|
return "";
|
|
|
|
};
|
|
|
|
this.getNextRedoCommandText = function() {
|
|
|
|
if (undoStackPointer < undoStack.length)
|
|
|
|
return undoStack[undoStackPointer].text;
|
|
|
|
return "";
|
|
|
|
};
|
2009-06-29 14:38:30 +00:00
|
|
|
|
2009-06-25 02:54:07 +00:00
|
|
|
this.undo = function() {
|
2009-06-29 14:38:30 +00:00
|
|
|
if (undoStackPointer > 0) {
|
2009-07-01 15:34:15 +00:00
|
|
|
this.clearSelection();
|
2009-06-25 15:35:28 +00:00
|
|
|
var cmd = undoStack[--undoStackPointer];
|
|
|
|
cmd.unapply();
|
2009-07-01 21:28:22 +00:00
|
|
|
call("changed", cmd.elements());
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
2009-07-06 18:54:38 +00:00
|
|
|
};
|
2009-06-25 02:54:07 +00:00
|
|
|
this.redo = function() {
|
|
|
|
if (undoStackPointer < undoStack.length && undoStack.length > 0) {
|
2009-07-01 15:34:15 +00:00
|
|
|
this.clearSelection();
|
2009-06-25 15:35:28 +00:00
|
|
|
var cmd = undoStack[undoStackPointer++];
|
|
|
|
cmd.apply();
|
2009-07-01 21:28:22 +00:00
|
|
|
call("changed", cmd.elements());
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-13 13:51:00 +00:00
|
|
|
// this creates deep DOM copies (clones) of all selected elements
|
2009-08-06 11:43:19 +00:00
|
|
|
this.cloneSelectedElements = function() {
|
|
|
|
var batchCmd = new BatchCommand("Clone Elements");
|
2009-08-07 05:44:55 +00:00
|
|
|
var copiedElements = [];
|
2009-07-13 13:51:00 +00:00
|
|
|
var len = selectedElements.length;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
if (selectedElements[i] == null) break;
|
|
|
|
copiedElements.push(selectedElements[i].cloneNode(true));
|
|
|
|
}
|
|
|
|
this.clearSelection();
|
|
|
|
var len = copiedElements.length;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
var elem = copiedElements[i];
|
2009-08-25 12:40:58 +00:00
|
|
|
elem.removeAttribute("id");
|
2009-07-13 13:51:00 +00:00
|
|
|
elem.id = getNextId();
|
|
|
|
svgroot.appendChild(elem);
|
|
|
|
batchCmd.addSubCommand(new InsertElementCommand(elem));
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-13 13:51:00 +00:00
|
|
|
if (!batchCmd.isEmpty()) {
|
|
|
|
this.addToSelection(copiedElements);
|
|
|
|
this.moveSelectedElements(20,20,false);
|
|
|
|
addCommandToHistory(batchCmd);
|
|
|
|
call("selected", selectedElements);
|
2009-08-17 06:56:55 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// aligns selected elements (type is a char - see switch below for explanation)
|
2009-08-25 07:04:19 +00:00
|
|
|
// relative_to can be "selected", "largest", "smallest", "page"
|
|
|
|
this.alignSelectedElements = function(type, relative_to) {
|
2009-08-24 15:30:11 +00:00
|
|
|
var bboxes = [], angles = [];
|
2009-08-23 17:16:27 +00:00
|
|
|
var minx = Number.MAX_VALUE, maxx = Number.MIN_VALUE, miny = Number.MAX_VALUE, maxy = Number.MIN_VALUE;
|
2009-08-25 07:04:19 +00:00
|
|
|
var curwidth = Number.MIN_VALUE, curheight = Number.MIN_VALUE;
|
2009-08-17 06:56:55 +00:00
|
|
|
var len = selectedElements.length;
|
|
|
|
if (!len) return;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
if (selectedElements[i] == null) break;
|
|
|
|
var elem = selectedElements[i];
|
2009-08-24 15:30:11 +00:00
|
|
|
bboxes[i] = this.getBBox(elem);
|
|
|
|
// TODO: could make the following code block as part of getBBox() and add a parameter
|
|
|
|
// to that function
|
|
|
|
// if element is rotated, get angle and rotate the 4 corners of the bbox and get
|
|
|
|
// the new axis-aligned bbox
|
|
|
|
angles[i] = this.getRotationAngle(elem) * Math.PI / 180.0;
|
|
|
|
if (angles[i]) {
|
|
|
|
var rminx = Number.MAX_VALUE, rminy = Number.MAX_VALUE,
|
|
|
|
rmaxx = Number.MIN_VALUE, rmaxy = Number.MIN_VALUE;
|
|
|
|
var cx = bboxes[i].x + bboxes[i].width/2,
|
|
|
|
cy = bboxes[i].y + bboxes[i].height/2;
|
|
|
|
var pts = [ [bboxes[i].x - cx, bboxes[i].y - cy],
|
|
|
|
[bboxes[i].x + bboxes[i].width - cx, bboxes[i].y - cy],
|
2009-08-25 07:04:19 +00:00
|
|
|
[bboxes[i].x + bboxes[i].width - cx, bboxes[i].y + bboxes[i].height - cy],
|
2009-08-24 15:30:11 +00:00
|
|
|
[bboxes[i].x - cx, bboxes[i].y + bboxes[i].height - cy] ];
|
|
|
|
var j = 4;
|
|
|
|
while (j--) {
|
2009-08-25 07:04:19 +00:00
|
|
|
var x = pts[j][0],
|
2009-08-24 15:30:11 +00:00
|
|
|
y = pts[j][1],
|
|
|
|
r = Math.sqrt( x*x + y*y );
|
|
|
|
var theta = Math.atan2(y,x) + angles[i];
|
2009-08-26 05:08:34 +00:00
|
|
|
x = parseInt(r * Math.cos(theta) + cx);
|
|
|
|
y = parseInt(r * Math.sin(theta) + cy);
|
2009-08-24 15:30:11 +00:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bboxes[i].x = rminx;
|
|
|
|
bboxes[i].y = rminy;
|
|
|
|
bboxes[i].width = rmaxx - rminx;
|
|
|
|
bboxes[i].height = rmaxy - rminy;
|
|
|
|
}
|
|
|
|
|
|
|
|
// now bbox is axis-aligned and handles rotation
|
2009-08-25 07:04:19 +00:00
|
|
|
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'
|
|
|
|
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;
|
|
|
|
break;
|
|
|
|
}
|
2009-08-24 15:30:11 +00:00
|
|
|
} // loop for each element to find the bbox and adjust min/max
|
2009-08-25 07:04:19 +00:00
|
|
|
|
|
|
|
if (relative_to == 'page') {
|
|
|
|
minx = 0;
|
|
|
|
miny = 0;
|
|
|
|
maxx = svgroot.getAttribute('width');
|
|
|
|
maxy = svgroot.getAttribute('height');
|
|
|
|
}
|
|
|
|
|
2009-08-23 17:16:27 +00:00
|
|
|
var dx = new Array(len);
|
|
|
|
var dy = new Array(len);
|
2009-08-17 06:56:55 +00:00
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
if (selectedElements[i] == null) break;
|
|
|
|
var elem = selectedElements[i];
|
2009-08-24 15:30:11 +00:00
|
|
|
var bbox = bboxes[i];
|
2009-08-23 17:16:27 +00:00
|
|
|
dx[i] = 0;
|
|
|
|
dy[i] = 0;
|
|
|
|
switch (type) {
|
|
|
|
case 'l': // left (horizontal)
|
|
|
|
dx[i] = minx - bbox.x;
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
2009-08-23 17:16:27 +00:00
|
|
|
case 'c': // center (horizontal)
|
|
|
|
dx[i] = (minx+maxx)/2 - (bbox.x + bbox.width/2);
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
2009-08-23 17:16:27 +00:00
|
|
|
case 'r': // right (horizontal)
|
|
|
|
dx[i] = maxx - (bbox.x + bbox.width);
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
2009-08-23 17:16:27 +00:00
|
|
|
case 't': // top (vertical)
|
|
|
|
dy[i] = miny - bbox.y;
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
2009-08-23 17:16:27 +00:00
|
|
|
case 'm': // middle (vertical)
|
|
|
|
dy[i] = (miny+maxy)/2 - (bbox.y + bbox.height/2);
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
2009-08-23 17:16:27 +00:00
|
|
|
case 'b': // bottom (vertical)
|
|
|
|
dy[i] = maxy - (bbox.y + bbox.height);
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-08-23 17:16:27 +00:00
|
|
|
this.moveSelectedElements(dx,dy);
|
2009-07-13 13:51:00 +00:00
|
|
|
};
|
2009-06-25 02:54:07 +00:00
|
|
|
|
2009-06-01 17:35:22 +00:00
|
|
|
}
|
2009-06-08 08:09:15 +00:00
|
|
|
|
|
|
|
// Static class for various utility functions
|
|
|
|
|
2009-06-08 09:08:45 +00:00
|
|
|
var Utils = {
|
2009-06-08 08:09:15 +00:00
|
|
|
|
|
|
|
// This code was written by Tyler Akins and has been placed in the
|
|
|
|
// public domain. It would be nice if you left this header intact.
|
|
|
|
// Base64 code from Tyler Akins -- http://rumkin.com
|
|
|
|
|
2009-06-23 07:48:15 +00:00
|
|
|
// schiller: Removed string concatenation in favour of Array.join() optimization,
|
2009-06-17 03:20:42 +00:00
|
|
|
// also precalculate the size of the array needed.
|
2009-06-16 18:51:35 +00:00
|
|
|
|
2009-06-12 13:27:42 +00:00
|
|
|
"_keyStr" : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
|
2009-06-08 08:09:15 +00:00
|
|
|
|
2009-06-12 13:27:42 +00:00
|
|
|
"encode64" : function(input) {
|
2009-06-17 03:20:42 +00:00
|
|
|
// base64 strings are 4/3 larger than the original string
|
2009-06-17 19:00:23 +00:00
|
|
|
var output = new Array( Math.floor( (input.length + 2) / 3 ) * 4 );
|
2009-06-08 09:08:45 +00:00
|
|
|
var chr1, chr2, chr3;
|
|
|
|
var enc1, enc2, enc3, enc4;
|
2009-06-17 03:20:42 +00:00
|
|
|
var i = 0, p = 0;
|
2009-06-08 08:09:15 +00:00
|
|
|
|
2009-06-08 09:08:45 +00:00
|
|
|
do {
|
|
|
|
chr1 = input.charCodeAt(i++);
|
|
|
|
chr2 = input.charCodeAt(i++);
|
|
|
|
chr3 = input.charCodeAt(i++);
|
2009-06-08 08:09:15 +00:00
|
|
|
|
2009-06-08 09:08:45 +00:00
|
|
|
enc1 = chr1 >> 2;
|
|
|
|
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
|
|
|
|
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
|
|
|
|
enc4 = chr3 & 63;
|
2009-06-08 08:09:15 +00:00
|
|
|
|
2009-06-08 09:08:45 +00:00
|
|
|
if (isNaN(chr2)) {
|
|
|
|
enc3 = enc4 = 64;
|
|
|
|
} else if (isNaN(chr3)) {
|
|
|
|
enc4 = 64;
|
|
|
|
}
|
2009-06-08 08:09:15 +00:00
|
|
|
|
2009-06-17 03:20:42 +00:00
|
|
|
output[p++] = this._keyStr.charAt(enc1);
|
|
|
|
output[p++] = this._keyStr.charAt(enc2);
|
|
|
|
output[p++] = this._keyStr.charAt(enc3);
|
|
|
|
output[p++] = this._keyStr.charAt(enc4);
|
2009-06-08 09:08:45 +00:00
|
|
|
} while (i < input.length);
|
2009-06-08 08:09:15 +00:00
|
|
|
|
2009-06-16 18:51:35 +00:00
|
|
|
return output.join('');
|
2009-07-02 04:50:24 +00:00
|
|
|
},
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-02 04:50:24 +00:00
|
|
|
"rectsIntersect": function(r1, r2) {
|
|
|
|
return r2.x < (r1.x+r1.width) &&
|
|
|
|
(r2.x+r2.width) > r1.x &&
|
|
|
|
r2.y < (r1.y+r1.height) &&
|
|
|
|
(r2.y+r2.height) > r1.y;
|
2009-07-10 05:04:06 +00:00
|
|
|
},
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-10 05:04:06 +00:00
|
|
|
// found this function http://groups.google.com/group/jquery-dev/browse_thread/thread/c6d11387c580a77f
|
|
|
|
"text2xml": function(sXML) {
|
2009-08-17 06:56:55 +00:00
|
|
|
// NOTE: I'd like to use jQuery for this, but jQuery makes all tags uppercase
|
|
|
|
//return $(xml)[0];
|
|
|
|
var out;
|
|
|
|
try{
|
|
|
|
var dXML = ($.browser.msie)?new ActiveXObject("Microsoft.XMLDOM"):new DOMParser();
|
|
|
|
dXML.async = false;
|
|
|
|
} catch(e){
|
|
|
|
throw new Error("XML Parser could not be instantiated");
|
|
|
|
};
|
|
|
|
try{
|
|
|
|
if($.browser.msie) out = (dXML.loadXML(sXML))?dXML:false;
|
|
|
|
else out = dXML.parseFromString(sXML, "text/xml");
|
|
|
|
}
|
|
|
|
catch(e){ throw new Error("Error parsing XML string"); };
|
|
|
|
return out;
|
|
|
|
}
|
2009-07-02 04:50:24 +00:00
|
|
|
};
|