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-16 06:02:26 +00:00
|
|
|
"rect": ["fill", "fill-opacity", "height", "id", "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,
|
|
|
|
"w":null,
|
|
|
|
"e":null,
|
|
|
|
"sw":null,
|
|
|
|
"s":null,
|
2009-08-04 03:14:23 +00:00
|
|
|
"se":null
|
2009-06-29 13:20:39 +00:00
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
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-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-06-29 13:20:39 +00:00
|
|
|
for (dir in this.selectorGrips) {
|
|
|
|
this.selectorGrips[dir].setAttribute("display", show ? "inline" : "none");
|
|
|
|
}
|
|
|
|
};
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
this.resize = function(bbox) {
|
|
|
|
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-16 15:07:00 +00:00
|
|
|
var bbox = 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-07-30 22:45:59 +00:00
|
|
|
// TODO: use suspendRedraw() here
|
2009-08-16 06:02:26 +00:00
|
|
|
selectedBox.setAttribute("x", l);
|
|
|
|
selectedBox.setAttribute("y", t);
|
|
|
|
selectedBox.setAttribute("width", w);
|
|
|
|
selectedBox.setAttribute("height", h);
|
|
|
|
selectedGrips.nw.setAttribute("x", l-3);
|
|
|
|
selectedGrips.nw.setAttribute("y", t-3);
|
|
|
|
selectedGrips.ne.setAttribute("x", l+w-3);
|
|
|
|
selectedGrips.ne.setAttribute("y", t-3);
|
|
|
|
selectedGrips.sw.setAttribute("x", l-3);
|
|
|
|
selectedGrips.sw.setAttribute("y", t+h-3);
|
|
|
|
selectedGrips.se.setAttribute("x", l+w-3);
|
|
|
|
selectedGrips.se.setAttribute("y", t+h-3);
|
|
|
|
selectedGrips.n.setAttribute("x", l+w/2-3);
|
|
|
|
selectedGrips.n.setAttribute("y", t-3);
|
|
|
|
selectedGrips.w.setAttribute("x", l-3);
|
|
|
|
selectedGrips.w.setAttribute("y", t+h/2-3);
|
|
|
|
selectedGrips.e.setAttribute("x", l+w-3);
|
|
|
|
selectedGrips.e.setAttribute("y", t+h/2-3);
|
|
|
|
selectedGrips.s.setAttribute("x", l+w/2-3);
|
|
|
|
selectedGrips.s.setAttribute("y", t+h-3);
|
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)
|
|
|
|
};
|
|
|
|
|
|
|
|
var assignAttributes = function(node, attrs) {
|
|
|
|
var handle = svgroot.suspendRedraw(60);
|
|
|
|
for (i in attrs) {
|
|
|
|
node.setAttributeNS(null, i, attrs[i]);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
assignAttributes(shape, data.attr);
|
|
|
|
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-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-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-07-01 15:34:15 +00:00
|
|
|
var resultList = null;
|
|
|
|
try {
|
|
|
|
resultList = svgroot.getIntersectionList(rect, null);
|
|
|
|
} catch(e) { }
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-01 15:34:15 +00:00
|
|
|
if (resultList == null || typeof(resultList.item) != "function")
|
|
|
|
{
|
2009-07-02 04:50:24 +00:00
|
|
|
resultList = [];
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-02 04:50:24 +00:00
|
|
|
var rubberBBox = rubberBox.getBBox();
|
|
|
|
var nodes = svgroot.childNodes;
|
2009-07-03 02:49:42 +00:00
|
|
|
var i = svgroot.childNodes.length;
|
|
|
|
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 {
|
|
|
|
if (nodes[i].id != "selectorParentGroup" &&
|
2009-08-16 15:07:00 +00:00
|
|
|
Utils.rectsIntersect(rubberBBox, canvas.getBBox(nodes[i])))
|
2009-07-30 22:45:59 +00:00
|
|
|
{
|
|
|
|
resultList.push(nodes[i]);
|
|
|
|
}
|
|
|
|
} 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-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
|
|
|
|
|
|
|
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-15 04:37:08 +00:00
|
|
|
var remapx = function(x) {return parseInt(((x-box.x)/box.width)*selectedBBox.width + selectedBBox.x);}
|
|
|
|
var remapy = function(y) {return parseInt(((y-box.y)/box.height)*selectedBBox.height + selectedBBox.y);}
|
|
|
|
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-16 19:18:22 +00:00
|
|
|
if (angle) {
|
2009-08-16 14:56:22 +00:00
|
|
|
var cx = remapx(box.x + box.width/2),
|
|
|
|
cy = remapy(box.y + box.height/2);
|
|
|
|
selected.setAttribute("transform", ["rotate(", angle, " ", cx, ",", cy, ")"].join(''));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
selected.setAttribute("transform", "");
|
|
|
|
selected.removeAttribute("transform");
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-08 00:03:08 +00:00
|
|
|
switch (selected.tagName)
|
|
|
|
{
|
2009-08-15 04:37:08 +00:00
|
|
|
// NOTE: there's no way to create an actual polygon element except by editing source
|
|
|
|
// or importing from somewhere else
|
|
|
|
case "polygon":
|
|
|
|
// polygon is handled identically as polyline
|
2009-08-07 17:19:32 +00:00
|
|
|
case "polyline":
|
|
|
|
// extract the x,y from the path, adjust it and write back the new path
|
|
|
|
// but first, save the old path
|
|
|
|
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);
|
|
|
|
var x = remapx(pt.x), y = remapy(pt.y);
|
|
|
|
// we only need to scale the relative coordinates (no need to translate)
|
|
|
|
newpoints += x + "," + y + " ";
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
var newd = "M" + remapx(curx) + "," + remapy(cury);
|
|
|
|
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-17 06:56:55 +00:00
|
|
|
var handle = svgroot.suspendRedraw(1000);
|
2009-08-16 06:02:26 +00:00
|
|
|
selected.setAttribute("x1", remapx(changes["x1"]));
|
|
|
|
selected.setAttribute("y1", remapy(changes["y1"]));
|
|
|
|
selected.setAttribute("x2", remapx(changes["x2"]));
|
|
|
|
selected.setAttribute("y2", remapy(changes["y2"]));
|
2009-08-17 06:56:55 +00:00
|
|
|
svgroot.unsuspendRedraw(handle);
|
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-07-30 22:45:59 +00:00
|
|
|
var handle = svgroot.suspendRedraw(1000);
|
2009-08-16 06:02:26 +00:00
|
|
|
selected.setAttribute("cx", remapx(changes["cx"]));
|
|
|
|
selected.setAttribute("cy", remapy(changes["cy"]));
|
2009-07-08 00:03:08 +00:00
|
|
|
// take the minimum of the new selected box's dimensions for the new circle radius
|
2009-08-16 06:02:26 +00:00
|
|
|
selected.setAttribute("r", Math.min(selectedBBox.width/2,selectedBBox.height/2));
|
2009-08-17 06:56:55 +00:00
|
|
|
svgroot.unsuspendRedraw(handle);
|
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-07-30 22:45:59 +00:00
|
|
|
var handle = svgroot.suspendRedraw(1000);
|
2009-08-16 06:02:26 +00:00
|
|
|
selected.setAttribute("cx", remapx(changes["cx"]));
|
|
|
|
selected.setAttribute("cy", remapy(changes["cy"]));
|
|
|
|
selected.setAttribute("rx", scalew(changes["rx"]));
|
|
|
|
selected.setAttribute("ry", scaleh(changes["ry"]));
|
2009-08-17 06:56:55 +00:00
|
|
|
svgroot.unsuspendRedraw(handle);
|
2009-07-08 00:03:08 +00:00
|
|
|
break;
|
|
|
|
case "text":
|
|
|
|
changes["x"] = selected.getAttribute("x");
|
|
|
|
changes["y"] = selected.getAttribute("y");
|
2009-07-30 22:45:59 +00:00
|
|
|
var handle = svgroot.suspendRedraw(1000);
|
2009-08-16 06:02:26 +00:00
|
|
|
selected.setAttribute("x", remapx(changes["x"]));
|
|
|
|
selected.setAttribute("y", remapy(changes["y"]));
|
|
|
|
svgroot.unsuspendRedraw(handle);
|
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-07-30 22:45:59 +00:00
|
|
|
var handle = svgroot.suspendRedraw(1000);
|
2009-08-16 06:02:26 +00:00
|
|
|
selected.setAttribute("x", remapx(changes["x"]));
|
|
|
|
selected.setAttribute("y", remapy(changes["y"]));
|
|
|
|
selected.setAttribute("width", scalew(changes["width"]));
|
|
|
|
selected.setAttribute("height", scaleh(changes["height"]));
|
2009-08-17 06:56:55 +00:00
|
|
|
svgroot.unsuspendRedraw(handle);
|
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-07-01 15:34:15 +00:00
|
|
|
this.addToSelection = function(elemsToAdd) {
|
|
|
|
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-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-06-03 08:41:25 +00:00
|
|
|
switch (current_mode) {
|
|
|
|
case "select":
|
2009-06-08 02:17:07 +00:00
|
|
|
started = true;
|
|
|
|
start_x = x;
|
|
|
|
start_y = y;
|
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-16 06:02:26 +00:00
|
|
|
rubberBox.setAttribute("x", start_x);
|
|
|
|
rubberBox.setAttribute("y", start_y);
|
|
|
|
rubberBox.setAttribute("width", 0);
|
|
|
|
rubberBox.setAttribute("height", 0);
|
2009-06-30 06:24:41 +00:00
|
|
|
rubberBox.setAttribute("display", "inline");
|
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-06-23 05:17:53 +00:00
|
|
|
// (for resizing purposes this is 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-06-10 03:12:19 +00:00
|
|
|
"fill": current_fill,
|
|
|
|
"stroke": current_stroke,
|
|
|
|
"stroke-width": current_stroke_width,
|
|
|
|
"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-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-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-16 14:56:22 +00:00
|
|
|
var angle = canvas.getRotationAngle(selected);
|
2009-08-16 19:18:22 +00:00
|
|
|
if (angle) {
|
2009-08-16 14:56:22 +00:00
|
|
|
var cx = box.x + box.width/2,
|
|
|
|
cy = box.y + box.height/2;
|
|
|
|
ts += [" rotate(", angle, " ", cx, ",", cy, ")"].join('');
|
|
|
|
}
|
|
|
|
selected.setAttribute("transform", ts);
|
2009-08-08 14:03:14 +00:00
|
|
|
box.x += dx; box.y += dy;
|
|
|
|
selectorManager.requestSelector(selected).resize(box);
|
|
|
|
selectedBBoxes[i] = 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-16 06:02:26 +00:00
|
|
|
rubberBox.setAttribute("x", Math.min(start_x,x));
|
|
|
|
rubberBox.setAttribute("y", Math.min(start_y,y));
|
|
|
|
rubberBox.setAttribute("width", Math.abs(x-start_x));
|
|
|
|
rubberBox.setAttribute("height", Math.abs(y-start_y));
|
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":
|
2009-08-16 19:18:22 +00:00
|
|
|
// TODO: update this to handle rotated elements
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-06-23 05:17:53 +00:00
|
|
|
// we track the resize bounding box and translate/scale the selected element
|
|
|
|
// while the mouse is down, when mouse goes up, we use this to recalculate
|
|
|
|
// the shape's coordinates
|
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);
|
|
|
|
var tx=0, ty=0, sx=1, sy=1;
|
|
|
|
var ts = null;
|
|
|
|
if(current_resize_mode.indexOf("n") != -1) {
|
|
|
|
ty = dy;
|
|
|
|
sy = (height-dy)/height;
|
|
|
|
}
|
|
|
|
else if(current_resize_mode.indexOf("s") != -1) {
|
|
|
|
sy = (height+dy)/height;
|
|
|
|
}
|
|
|
|
if(current_resize_mode.indexOf("e") != -1) {
|
|
|
|
sx = (width+dx)/width;
|
|
|
|
}
|
|
|
|
else if(current_resize_mode.indexOf("w") != -1) {
|
|
|
|
tx = dx;
|
|
|
|
sx = (width-dx)/width;
|
|
|
|
}
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2009-07-01 19:28:08 +00:00
|
|
|
var selectedBBox = selectedBBoxes[0];
|
2009-06-23 07:48:15 +00:00
|
|
|
selectedBBox.x = left+tx;
|
2009-06-23 05:17:53 +00:00
|
|
|
selectedBBox.y = top+ty;
|
|
|
|
selectedBBox.width = width*sx;
|
|
|
|
selectedBBox.height = height*sy;
|
2009-06-23 05:46:47 +00:00
|
|
|
// normalize selectedBBox
|
|
|
|
if (selectedBBox.width < 0) {
|
|
|
|
selectedBBox.x += selectedBBox.width;
|
|
|
|
selectedBBox.width = -selectedBBox.width;
|
|
|
|
}
|
|
|
|
if (selectedBBox.height < 0) {
|
|
|
|
selectedBBox.y += selectedBBox.height;
|
|
|
|
selectedBBox.height = -selectedBBox.height;
|
|
|
|
}
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2009-08-16 14:56:22 +00:00
|
|
|
// find the rotation transform and prepend it
|
2009-08-17 03:11:30 +00:00
|
|
|
var ts = [" translate(", (left+tx), ",", (top+ty), ") scale(", sx, ",", sy,
|
2009-08-16 14:56:22 +00:00
|
|
|
") translate(", -left, ",", -top, ")"].join('');
|
|
|
|
var angle = canvas.getRotationAngle(selected);
|
2009-08-16 19:18:22 +00:00
|
|
|
if (angle) {
|
2009-08-16 14:56:22 +00:00
|
|
|
var cx = selectedBBox.x + selectedBBox.width/2,
|
|
|
|
cy = selectedBBox.y + selectedBBox.height/2;
|
2009-08-17 03:11:30 +00:00
|
|
|
ts = ["rotate(", angle, " ", cx, ",", cy, ")", ts].join('')
|
2009-08-16 14:56:22 +00:00
|
|
|
}
|
|
|
|
selected.setAttribute("transform", ts);
|
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-07-30 22:45:59 +00:00
|
|
|
var handle = svgroot.suspendRedraw(1000);
|
2009-06-10 03:12:19 +00:00
|
|
|
shape.setAttribute("x", x);
|
|
|
|
shape.setAttribute("y", y);
|
2009-08-17 06:56:55 +00:00
|
|
|
svgroot.unsuspendRedraw(handle);
|
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-07-30 22:45:59 +00:00
|
|
|
var handle = svgroot.suspendRedraw(1000);
|
2009-06-02 16:40:33 +00:00
|
|
|
shape.setAttributeNS(null, "width", size);
|
|
|
|
shape.setAttributeNS(null, "height", size);
|
2009-06-06 17:06:44 +00:00
|
|
|
shape.setAttributeNS(null, "x", start_x < x ? start_x : start_x - size);
|
|
|
|
shape.setAttributeNS(null, "y", start_y < y ? start_y : start_y - size);
|
2009-08-17 06:56:55 +00:00
|
|
|
svgroot.unsuspendRedraw(handle);
|
2009-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "rect":
|
2009-07-30 22:45:59 +00:00
|
|
|
var handle = svgroot.suspendRedraw(1000);
|
2009-06-06 17:06:44 +00:00
|
|
|
shape.setAttributeNS(null, "x", Math.min(start_x,x));
|
|
|
|
shape.setAttributeNS(null, "y", Math.min(start_y,y));
|
|
|
|
shape.setAttributeNS(null, "width", Math.abs(x-start_x));
|
|
|
|
shape.setAttributeNS(null, "height", Math.abs(y-start_y));
|
2009-08-17 06:56:55 +00:00
|
|
|
svgroot.unsuspendRedraw(handle);
|
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-08-07 17:19:32 +00:00
|
|
|
// var dx = x - start_x;
|
|
|
|
// var dy = y - start_y;
|
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;
|
|
|
|
var dx = x - current_poly_pts[i],
|
|
|
|
dy = y - current_poly_pts[i+1];
|
|
|
|
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];
|
|
|
|
arr[j] = ["l", (px-curx), ",", (py-cury)].join('');
|
|
|
|
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-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);
|
|
|
|
grip.setAttribute("cx", current_poly_pts[i]);
|
|
|
|
grip.setAttribute("cy", current_poly_pts[i+1]);
|
|
|
|
grip.setAttribute("display", "inline");
|
|
|
|
}
|
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");
|
|
|
|
pointGrip.id = "polypointgrip_" + index;
|
|
|
|
pointGrip.setAttribute("display", "none");
|
|
|
|
pointGrip.setAttribute("r", 4);
|
|
|
|
pointGrip.setAttribute("fill", "#0F0");
|
|
|
|
pointGrip.setAttribute("stroke", "#00F");
|
|
|
|
pointGrip.setAttribute("stroke-width", 2);
|
|
|
|
pointGrip.setAttribute("cursor", "move");
|
|
|
|
pointGrip.setAttribute("pointer-events", "all");
|
|
|
|
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
|
|
|
|
pointGrip.setAttribute("cx", x);
|
|
|
|
pointGrip.setAttribute("cy", y);
|
|
|
|
pointGrip.setAttribute("display", "inline");
|
|
|
|
};
|
|
|
|
|
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-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();
|
|
|
|
canvas.addToSelection([element]);
|
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");
|
|
|
|
stretchy.id = "poly_stretch_line";
|
|
|
|
stretchy.setAttribute("stroke-width", "0.5");
|
|
|
|
stretchy.setAttribute("stroke", "blue");
|
|
|
|
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
|
|
|
|
stretchy.setAttribute("x1", x);
|
|
|
|
stretchy.setAttribute("y1", y);
|
|
|
|
stretchy.setAttribute("x2", x);
|
|
|
|
stretchy.setAttribute("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
|
|
|
|
stretchy.setAttribute("x1", x);
|
|
|
|
stretchy.setAttribute("y1", y);
|
|
|
|
stretchy.setAttribute("x2", x);
|
|
|
|
stretchy.setAttribute("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();
|
|
|
|
canvas.addToSelection([evt.target]);
|
|
|
|
}
|
2009-08-08 14:03:14 +00:00
|
|
|
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) {
|
|
|
|
element.setAttribute("opacity", current_opacity);
|
|
|
|
cleanupElement(element);
|
2009-06-30 17:50:08 +00:00
|
|
|
selectorManager.update();
|
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-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-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-06-06 23:54:53 +00:00
|
|
|
this.saveHandler(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() {
|
|
|
|
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-06-25 08:22:30 +00:00
|
|
|
svgroot.setAttribute("width", x);
|
|
|
|
svgroot.setAttribute("height", y);
|
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) {
|
|
|
|
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-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-07-30 22:45:59 +00:00
|
|
|
this.setStrokePaint = function(p) {
|
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-07-30 22:45:59 +00:00
|
|
|
// find out if there is a duplicate gradient already in the defs
|
2009-08-01 05:03:51 +00:00
|
|
|
var grad = current_stroke_paint.linearGradient;
|
2009-07-30 22:45:59 +00:00
|
|
|
var duplicate_grad = findDuplicateGradient(grad);
|
|
|
|
var defs = findDefs();
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-30 22:45:59 +00:00
|
|
|
// no duplicate found, so import gradient into defs
|
|
|
|
if (!duplicate_grad) {
|
|
|
|
grad = defs.appendChild( svgdoc.importNode(grad, true) );
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-30 22:45:59 +00:00
|
|
|
// get next id and set it on the grad
|
|
|
|
grad.id = getNextId();
|
|
|
|
}
|
|
|
|
else { // use existing gradient
|
|
|
|
grad = duplicate_grad;
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-30 22:45:59 +00:00
|
|
|
this.setStrokeColor("url(#" + grad.id + ")");
|
|
|
|
}
|
|
|
|
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-01 05:03:51 +00:00
|
|
|
// TODO: rework this so that we are not append elements into the SVG at this stage
|
|
|
|
// This should only be done at the actual creation stage or when we change a selected
|
|
|
|
// element's fill paint - at that point, batch up the creation of the gradient element
|
|
|
|
// with the creation/change
|
2009-07-30 22:45:59 +00:00
|
|
|
this.setFillPaint = function(p) {
|
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-07-30 22:45:59 +00:00
|
|
|
// find out if there is a duplicate gradient already in the defs
|
2009-08-01 05:03:51 +00:00
|
|
|
var grad = current_fill_paint.linearGradient;
|
2009-07-30 22:45:59 +00:00
|
|
|
var duplicate_grad = findDuplicateGradient(grad);
|
|
|
|
var defs = findDefs();
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-30 22:45:59 +00:00
|
|
|
// no duplicate found, so import gradient into defs
|
|
|
|
if (!duplicate_grad) {
|
|
|
|
grad = defs.appendChild( svgdoc.importNode(grad, true) );
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-30 22:45:59 +00:00
|
|
|
// get next id and set it on the grad
|
|
|
|
grad.id = getNextId();
|
|
|
|
}
|
|
|
|
else { // use existing gradient
|
|
|
|
grad = duplicate_grad;
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-07-30 22:45:59 +00:00
|
|
|
this.setFillColor("url(#" + grad.id + ")");
|
|
|
|
}
|
|
|
|
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-06-02 16:40:33 +00:00
|
|
|
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-16 18:00:56 +00:00
|
|
|
var bbox = selected.getBBox();
|
|
|
|
|
|
|
|
// determine the bounding box if rotated (NOTE: this won't be
|
|
|
|
// the tightest possible bounding box, for it will do)
|
|
|
|
var angle = this.getRotationAngle(selected) * Math.PI / 180.0;
|
2009-08-16 19:18:22 +00:00
|
|
|
if (angle) {
|
2009-08-16 18:00:56 +00:00
|
|
|
var w2 = bbox.width/2, h2 = bbox.height/2,
|
|
|
|
cx = bbox.x + w2, cy = bbox.y + h2;
|
|
|
|
var pts = [ [-w2,-h2], [w2,-h2],
|
|
|
|
[w2,h2], [-w2,h2] ];
|
|
|
|
var r = Math.sqrt( w2*w2 + h2*h2 );
|
|
|
|
var i = 4;
|
|
|
|
var MINX = Number.MAX_VALUE, MINY = Number.MAX_VALUE,
|
|
|
|
MAXX = Number.MIN_VALUE, MAXY = Number.MIN_VALUE;
|
|
|
|
while (i--) {
|
|
|
|
var theta = angle + Math.atan2(pts[i][1],pts[i][0]);
|
|
|
|
var x = r * Math.cos(theta),
|
|
|
|
y = r * Math.sin(theta);
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-16 18:00:56 +00:00
|
|
|
if (MINX > x) { MINX = x; }
|
|
|
|
if (MINY > y) { MINY = y; }
|
|
|
|
if (MAXX < x) { MAXX = x; }
|
|
|
|
if (MAXY < y) { MAXY = y; }
|
|
|
|
}
|
|
|
|
bbox.x = cx + parseInt(MINX);
|
|
|
|
bbox.y = cy + parseInt(MINY);
|
|
|
|
bbox.width = parseInt(MAXX-MINX);
|
|
|
|
bbox.height = parseInt(MAXY-MINY);
|
|
|
|
}
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-16 18:00:56 +00:00
|
|
|
return bbox;
|
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-16 15:07:00 +00:00
|
|
|
var bbox = this.getBBox(elem);
|
2009-08-17 06:56:55 +00:00
|
|
|
|
2009-08-16 06:02:26 +00:00
|
|
|
this.changeSelectedAttribute("transform", "rotate(" + val + " " +
|
|
|
|
(bbox.x+bbox.width/2) + "," +
|
|
|
|
(bbox.y+bbox.height/2) + ")");
|
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-17 06:56:55 +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-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-08-13 15:42:41 +00:00
|
|
|
// TODO: Unfortunately Mozilla does not handle internal references to gradients
|
|
|
|
// inside a data: URL document. This means that any elements filled/stroked
|
|
|
|
// with a gradient will appear black in Firefox, etc. See bug 308590
|
|
|
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=308590
|
2009-06-06 23:54:53 +00:00
|
|
|
this.saveHandler = function(svg) {
|
2009-06-08 08:09:15 +00:00
|
|
|
window.open("data:image/svg+xml;base64," + Utils.encode64(svg));
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-07 10:27:55 +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-07-08 00:03:08 +00:00
|
|
|
selectedBBoxes[i].x += dx;
|
|
|
|
selectedBBoxes[i].y += dy;
|
|
|
|
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-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];
|
|
|
|
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)
|
|
|
|
this.alignSelectedElements = function(type) {
|
|
|
|
var minx = 100000, maxx = -100000, miny = 100000, maxy = -100000;
|
|
|
|
var len = selectedElements.length;
|
|
|
|
if (!len) return;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
if (selectedElements[i] == null) break;
|
|
|
|
var elem = selectedElements[i];
|
|
|
|
switch (elem.tagName) {
|
|
|
|
case 'circle':
|
2009-08-17 07:05:45 +00:00
|
|
|
if (parseInt(elem.getAttribute('cx')) - parseInt(elem.getAttribute('r')) < minx) minx = parseInt(elem.getAttribute('cx')) - parseInt(elem.getAttribute('r'));
|
|
|
|
if (parseInt(elem.getAttribute('cx')) + parseInt(elem.getAttribute('r')) > maxx) maxx = parseInt(elem.getAttribute('cx')) + parseInt(elem.getAttribute('r'));
|
|
|
|
if (parseInt(elem.getAttribute('cy')) - parseInt(elem.getAttribute('r')) < miny) miny = parseInt(elem.getAttribute('cy')) - parseInt(elem.getAttribute('r'));
|
|
|
|
if (parseInt(elem.getAttribute('cy')) + parseInt(elem.getAttribute('r')) > maxy) maxy = parseInt(elem.getAttribute('cy')) + parseInt(elem.getAttribute('r'));
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
|
|
|
case 'ellipse':
|
2009-08-17 07:05:45 +00:00
|
|
|
if (parseInt(elem.getAttribute('cx')) - parseInt(elem.getAttribute('rx')) < minx) minx = parseInt(elem.getAttribute('cx')) - parseInt(elem.getAttribute('rx'));
|
|
|
|
if (parseInt(elem.getAttribute('cx')) + parseInt(elem.getAttribute('rx')) > maxx) maxx = parseInt(elem.getAttribute('cx')) + parseInt(elem.getAttribute('rx'));
|
|
|
|
if (parseInt(elem.getAttribute('cy')) - parseInt(elem.getAttribute('ry')) < miny) miny = parseInt(elem.getAttribute('cy')) - parseInt(elem.getAttribute('ry'));
|
|
|
|
if (parseInt(elem.getAttribute('cy')) + parseInt(elem.getAttribute('ry')) > maxy) maxy = parseInt(elem.getAttribute('cy')) + parseInt(elem.getAttribute('ry'));
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
|
|
|
case 'rect':
|
2009-08-17 07:05:45 +00:00
|
|
|
if (parseInt(elem.getAttribute('x')) < minx) minx = parseInt(elem.getAttribute('x'));
|
|
|
|
if (parseInt(elem.getAttribute('x')) + parseInt(elem.getAttribute('width')) > maxx) maxx = parseInt(elem.getAttribute('x')) + parseInt(elem.getAttribute('width'));
|
|
|
|
if (parseInt(elem.getAttribute('y')) < minx) miny = parseInt(elem.getAttribute('y'));
|
|
|
|
if (parseInt(elem.getAttribute('y')) + parseInt(elem.getAttribute('height')) > maxx) maxy = parseInt(elem.getAttribute('y')) + parseInt(elem.getAttribute('height'));
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
|
|
|
case 'line':
|
2009-08-17 07:05:45 +00:00
|
|
|
if (parseInt(elem.getAttribute('x1')) < minx) minx = parseInt(elem.getAttribute('x1'));
|
|
|
|
if (parseInt(elem.getAttribute('x2')) < minx) minx = parseInt(elem.getAttribute('x2'));
|
|
|
|
if (parseInt(elem.getAttribute('x1')) > maxx) maxx = parseInt(elem.getAttribute('x1'));
|
|
|
|
if (parseInt(elem.getAttribute('x2')) > maxx) maxx = parseInt(elem.getAttribute('x2'));
|
|
|
|
if (parseInt(elem.getAttribute('y1')) < miny) miny = parseInt(elem.getAttribute('y1'));
|
|
|
|
if (parseInt(elem.getAttribute('y2')) < miny) miny = parseInt(elem.getAttribute('y2'));
|
|
|
|
if (parseInt(elem.getAttribute('y1')) > maxy) maxy = parseInt(elem.getAttribute('y1'));
|
|
|
|
if (parseInt(elem.getAttribute('y2')) > maxy) maxy = parseInt(elem.getAttribute('y2'));
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
|
|
|
case 'path':
|
|
|
|
// TODO: implement
|
|
|
|
break;
|
|
|
|
case 'polygon':
|
|
|
|
// TODO: implement
|
|
|
|
break;
|
|
|
|
case 'polyline':
|
|
|
|
// TODO: implement
|
|
|
|
break;
|
|
|
|
case 'text':
|
|
|
|
// TODO: how to handle text?
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var len = selectedElements.length;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
if (selectedElements[i] == null) break;
|
|
|
|
var elem = selectedElements[i];
|
|
|
|
switch (elem.tagName) {
|
|
|
|
case 'circle':
|
|
|
|
switch (type) {
|
|
|
|
case 'l': // left (horizontal)
|
2009-08-17 07:05:45 +00:00
|
|
|
elem.setAttribute('cx', minx+parseInt(elem.getAttribute('r')));
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
|
|
|
case 'c': // center (horizontal)
|
|
|
|
elem.setAttribute('cx', (minx+maxx)/2);
|
|
|
|
break;
|
|
|
|
case 'r': // right (horizontal)
|
2009-08-17 07:05:45 +00:00
|
|
|
elem.setAttribute('cx', maxx-parseInt(elem.getAttribute('r')));
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
|
|
|
case 't': // top (vertical)
|
2009-08-17 07:05:45 +00:00
|
|
|
elem.setAttribute('cy', miny+parseInt(elem.getAttribute('r')));
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
|
|
|
case 'm': // middle (vertical)
|
|
|
|
elem.setAttribute('cy', (miny+maxy)/2);
|
|
|
|
break;
|
|
|
|
case 'b': // bottom (vertical)
|
2009-08-17 07:05:45 +00:00
|
|
|
elem.setAttribute('cy', maxy-parseInt(elem.getAttribute('r')));
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'ellipse':
|
|
|
|
switch (type) {
|
|
|
|
case 'l': // left (horizontal)
|
2009-08-17 07:05:45 +00:00
|
|
|
elem.setAttribute('cx', minx+parseInt(elem.getAttribute('rx')));
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
|
|
|
case 'c': // center (horizontal)
|
|
|
|
elem.setAttribute('cx', (minx+maxx)/2);
|
|
|
|
break;
|
|
|
|
case 'r': // right (horizontal)
|
2009-08-17 07:05:45 +00:00
|
|
|
elem.setAttribute('cx', maxx-parseInt(elem.getAttribute('rx')));
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
|
|
|
case 't': // top (vertical)
|
2009-08-17 07:05:45 +00:00
|
|
|
elem.setAttribute('cy', miny+parseInt(elem.getAttribute('ry')));
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
|
|
|
case 'm': // middle (vertical)
|
|
|
|
elem.setAttribute('cy', (miny+maxy)/2);
|
|
|
|
break;
|
|
|
|
case 'b': // bottom (vertical)
|
2009-08-17 07:05:45 +00:00
|
|
|
elem.setAttribute('cy', maxy-parseInt(elem.getAttribute('ry')));
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'rect':
|
|
|
|
switch (type) {
|
|
|
|
case 'l': // left (horizontal)
|
|
|
|
elem.setAttribute('x', minx);
|
|
|
|
break;
|
|
|
|
case 'c': // center (horizontal)
|
2009-08-17 07:05:45 +00:00
|
|
|
elem.setAttribute('x', (minx+maxx-parseInt(elem.getAttribute('width')))/2);
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
|
|
|
case 'r': // right (horizontal)
|
2009-08-17 07:05:45 +00:00
|
|
|
elem.setAttribute('x', maxx-parseInt(elem.getAttribute('width')));
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
|
|
|
case 't': // top (vertical)
|
|
|
|
elem.setAttribute('y', miny);
|
|
|
|
break;
|
|
|
|
case 'm': // middle (vertical)
|
2009-08-17 07:05:45 +00:00
|
|
|
elem.setAttribute('y', (miny+maxy-parseInt(elem.getAttribute('height')))/2);
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
|
|
|
case 'b': // bottom (vertical)
|
2009-08-17 07:08:16 +00:00
|
|
|
elem.setAttribute('y', maxy-parseInt(elem.getAttribute('height')));
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'line':
|
|
|
|
switch (type) {
|
|
|
|
case 'l': // left (horizontal)
|
2009-08-17 07:05:45 +00:00
|
|
|
var d = Math.min(parseInt(elem.getAttribute('x1')), parseInt(elem.getAttribute('x2'))) - minx;
|
|
|
|
elem.setAttribute('x1', parseInt(elem.getAttribute('x1')) - d);
|
|
|
|
elem.setAttribute('x2', parseInt(elem.getAttribute('x2')) - d);
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
|
|
|
case 'c': // center (horizontal)
|
2009-08-17 07:05:45 +00:00
|
|
|
var d = Math.abs(parseInt(elem.getAttribute('x1')) - parseInt(elem.getAttribute('x2')));
|
2009-08-17 06:56:55 +00:00
|
|
|
elem.setAttribute('x1', (minx+maxx-d)/2 );
|
|
|
|
elem.setAttribute('x2', (minx+maxx+d)/2 );
|
|
|
|
break;
|
|
|
|
case 'r': // right (horizontal)
|
2009-08-17 07:05:45 +00:00
|
|
|
var d = Math.max(parseInt(elem.getAttribute('x1')), parseInt(elem.getAttribute('x2'))) - maxx;
|
|
|
|
elem.setAttribute('x1', parseInt(elem.getAttribute('x1')) - d);
|
|
|
|
elem.setAttribute('x2', parseInt(elem.getAttribute('x2')) - d);
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
|
|
|
case 't': // top (vertical)
|
2009-08-17 07:05:45 +00:00
|
|
|
var d = Math.min(parseInt(elem.getAttribute('y1')), parseInt(elem.getAttribute('y2'))) - miny;
|
|
|
|
elem.setAttribute('y1', parseInt(elem.getAttribute('y1'))-d);
|
|
|
|
elem.setAttribute('y2', parseInt(elem.getAttribute('y2'))-d);
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
|
|
|
case 'm': // middle (vertical)
|
2009-08-17 07:05:45 +00:00
|
|
|
var d = Math.abs(parseInt(elem.getAttribute('y1')) - parseInt(elem.getAttribute('y2')));
|
2009-08-17 06:56:55 +00:00
|
|
|
elem.setAttribute('y1', (miny+maxy-d)/2 );
|
|
|
|
elem.setAttribute('y2', (miny+maxy+d)/2 );
|
|
|
|
break;
|
|
|
|
case 'b': // bottom (vertical)
|
2009-08-17 07:05:45 +00:00
|
|
|
var d = Math.max(parseInt(elem.getAttribute('y1')), parseInt(elem.getAttribute('y2'))) - maxy;
|
|
|
|
elem.setAttribute('y1', parseInt(elem.getAttribute('y1')) - d);
|
|
|
|
elem.setAttribute('y2', parseInt(elem.getAttribute('y2')) - d);
|
2009-08-17 06:56:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'path':
|
|
|
|
// TODO: implement
|
|
|
|
break;
|
|
|
|
case 'polygon':
|
|
|
|
// TODO: implement
|
|
|
|
break;
|
|
|
|
case 'polyline':
|
|
|
|
// TODO: implement
|
|
|
|
break;
|
|
|
|
case 'text':
|
|
|
|
// TODO: how to handle text?
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
};
|