2009-06-23 05:17:53 +00:00
|
|
|
if(!window.console) {
|
|
|
|
window.console = new function() {
|
|
|
|
this.log = function(str) {};
|
|
|
|
this.dir = function(str) {};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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-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-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.elems = [];
|
|
|
|
this.text = text || "Batch Command";
|
|
|
|
this.stack = [];
|
|
|
|
|
|
|
|
this.apply = function() {
|
|
|
|
for (var i = 0; i < this.stack.length; ++i) {
|
|
|
|
this.stack[i].apply();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.unapply = function() {
|
|
|
|
for (var i = this.stack.length-1; i >= 0; i--) {
|
|
|
|
this.stack[i].unapply();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.elements = function() { return this.elems; };
|
|
|
|
|
|
|
|
this.addSubCommand = function(cmd) { this.stack[this.stack.length] = cmd; };
|
|
|
|
|
|
|
|
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-06-29 03:58:34 +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-06-29 03:58:34 +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-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-06-29 13:20:39 +00:00
|
|
|
this.selectorGroup.setAttribute("display", "inline");
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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-06-29 03:58:34 +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
|
|
|
|
"style": "pointer-events:none",
|
|
|
|
}
|
|
|
|
}) );
|
2009-06-29 03:58:34 +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,
|
|
|
|
"se":null,
|
|
|
|
};
|
2009-06-29 03:58:34 +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"),
|
|
|
|
// when we are in rotate mode, we will set rx/ry to 3
|
|
|
|
// "rx": 3,
|
|
|
|
// "ry": 3,
|
|
|
|
// 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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.showGrips = function(show) {
|
|
|
|
for (dir in this.selectorGrips) {
|
|
|
|
this.selectorGrips[dir].setAttribute("display", show ? "inline" : "none");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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-07-01 15:34:15 +00:00
|
|
|
var bbox = bbox || this.selectedElement.getBBox();
|
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);
|
|
|
|
selectedBox.x.baseVal.value = l;
|
|
|
|
selectedBox.y.baseVal.value = t;
|
|
|
|
selectedBox.width.baseVal.value = w;
|
|
|
|
selectedBox.height.baseVal.value = h;
|
|
|
|
selectedGrips.nw.x.baseVal.value = l-3;
|
|
|
|
selectedGrips.nw.y.baseVal.value = t-3;
|
|
|
|
selectedGrips.ne.x.baseVal.value = l+w-3;
|
|
|
|
selectedGrips.ne.y.baseVal.value = t-3;
|
|
|
|
selectedGrips.sw.x.baseVal.value = l-3;
|
|
|
|
selectedGrips.sw.y.baseVal.value = t+h-3;
|
|
|
|
selectedGrips.se.x.baseVal.value = l+w-3;
|
|
|
|
selectedGrips.se.y.baseVal.value = t+h-3;
|
|
|
|
selectedGrips.n.x.baseVal.value = l+w/2-3;
|
|
|
|
selectedGrips.n.y.baseVal.value = t-3;
|
|
|
|
selectedGrips.w.x.baseVal.value = l-3;
|
|
|
|
selectedGrips.w.y.baseVal.value = t+h/2-3;
|
|
|
|
selectedGrips.e.x.baseVal.value = l+w-3;
|
|
|
|
selectedGrips.e.y.baseVal.value = t+h/2-3;
|
|
|
|
selectedGrips.s.x.baseVal.value = l+w/2-3;
|
|
|
|
selectedGrips.s.y.baseVal.value = t+h-3;
|
|
|
|
};
|
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-06-30 06:24:41 +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-06-29 03:58:34 +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-06-29 13:20:39 +00:00
|
|
|
// this will hold objects of type Selector (see above)
|
|
|
|
this.selectors = [];
|
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-06-30 14:29:28 +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-07-01 21:59:11 +00:00
|
|
|
console.log("requestSelect() with elem=" + elem + " and N=" + N);
|
2009-06-30 06:24:41 +00:00
|
|
|
|
2009-06-29 13:20:39 +00:00
|
|
|
if (this.selectorParentGroup == null) {
|
2009-06-30 06:24:41 +00:00
|
|
|
initGroup();
|
2009-06-29 03:58:34 +00:00
|
|
|
}
|
2009-06-29 13:20:39 +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-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) { }
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// this keeps the selector groups as the last child in the document
|
|
|
|
this.update = function() {
|
2009-07-01 15:34:15 +00:00
|
|
|
if (this.selectorParentGroup == null) {
|
|
|
|
initGroup();
|
|
|
|
}
|
2009-06-29 13:20:39 +00:00
|
|
|
this.selectorParentGroup = svgroot.appendChild(this.selectorParentGroup);
|
2009-06-29 03:58:34 +00:00
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
|
|
|
|
this.getRubberBandBox = function() {
|
|
|
|
if (this.selectorParentGroup == null) {
|
|
|
|
initGroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
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",
|
|
|
|
"style": "pointer-events:none",
|
|
|
|
}
|
|
|
|
}));
|
2009-06-30 06:24:41 +00:00
|
|
|
}
|
|
|
|
return this.rubberBandBox;
|
|
|
|
}
|
2009-06-29 13:20:39 +00:00
|
|
|
}
|
|
|
|
// **************************************************************************************
|
2009-06-25 08:22:30 +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-06-02 16:40:33 +00:00
|
|
|
var current_fill = "none";
|
|
|
|
var current_stroke = "black";
|
|
|
|
var current_stroke_width = 1;
|
|
|
|
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-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
|
|
|
// this object holds the one-and-only selector (for now)
|
2009-06-29 13:20:39 +00:00
|
|
|
var theSelector = null;
|
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-01 15:34:15 +00:00
|
|
|
// Since the only browser that supports getIntersectionList is Opera, we need to
|
|
|
|
// provide an implementation here. We brute-force it for now.
|
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) {
|
|
|
|
var resultList = null;
|
|
|
|
try {
|
|
|
|
resultList = svgroot.getIntersectionList(rect, null);
|
|
|
|
} catch(e) { }
|
|
|
|
|
|
|
|
if (resultList == null || typeof(resultList.item) != "function")
|
|
|
|
{
|
|
|
|
// TODO: provide brute-force algorithm, looking at every element's bbox for intersection
|
|
|
|
console.log("brute force!");
|
|
|
|
}
|
|
|
|
return resultList;
|
|
|
|
};
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
undoStack[undoStack.length] = cmd;
|
|
|
|
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-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-06-01 21:24:30 +00:00
|
|
|
var assignAttributes = function(node, attrs) {
|
|
|
|
for (i in attrs) {
|
|
|
|
node.setAttributeNS(null, i, attrs[i]);
|
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-06-02 16:40:33 +00:00
|
|
|
// remove unneeded attributes
|
|
|
|
// makes resulting SVG smaller
|
|
|
|
var cleanupElement = function(element) {
|
|
|
|
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');
|
2009-06-11 15:18:46 +00:00
|
|
|
if (element.getAttribute('rx') == '0')
|
|
|
|
element.removeAttribute('rx')
|
|
|
|
if (element.getAttribute('ry') == '0')
|
|
|
|
element.removeAttribute('ry')
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-02 16:40:33 +00:00
|
|
|
|
|
|
|
var addSvgElementFromJson = function(data) {
|
2009-06-09 22:13:13 +00:00
|
|
|
return canvas.updateElementFromJson(data)
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-01 21:24:30 +00:00
|
|
|
|
|
|
|
var svgToString = function(elem, indent) {
|
2009-06-17 03:20:42 +00:00
|
|
|
// TODO: could use the array.join() optimization trick here too
|
2009-06-01 21:24:30 +00:00
|
|
|
var out = "";
|
|
|
|
if (elem) {
|
|
|
|
var attrs = elem.attributes;
|
|
|
|
var attr;
|
|
|
|
var i;
|
|
|
|
var childs = elem.childNodes;
|
2009-06-27 03:42:12 +00:00
|
|
|
for (i=0; i<indent; i++) out += " ";
|
2009-06-01 21:24:30 +00:00
|
|
|
out += "<" + elem.nodeName;
|
|
|
|
for (i=attrs.length-1; i>=0; i--) {
|
|
|
|
attr = attrs.item(i);
|
2009-06-16 02:52:36 +00:00
|
|
|
if (attr.nodeValue != "") {
|
|
|
|
out += " " + attr.nodeName + "=\"" + attr.nodeValue+ "\"";
|
|
|
|
}
|
2009-06-01 21:24:30 +00:00
|
|
|
}
|
|
|
|
if (elem.hasChildNodes()) {
|
2009-06-27 03:42:12 +00:00
|
|
|
out += ">";
|
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;
|
|
|
|
if (child.nodeType == 1) { // element node
|
2009-06-27 03:42:12 +00:00
|
|
|
out += "\n" + svgToString(childs.item(i), indent);
|
2009-06-30 17:50:08 +00:00
|
|
|
} else if (child.nodeType == 3) { // text node
|
2009-06-27 03:42:12 +00:00
|
|
|
bOneLine = true;
|
2009-06-30 17:50:08 +00:00
|
|
|
out += child.nodeValue + "";
|
2009-06-01 21:24:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
indent--;
|
2009-06-29 14:38:30 +00:00
|
|
|
if (!bOneLine) {
|
|
|
|
out += "\n";
|
2009-06-27 03:42:12 +00:00
|
|
|
for (i=0; i<indent; i++) out += " ";
|
|
|
|
}
|
|
|
|
out += "</" + elem.nodeName + ">";
|
2009-06-01 21:24:30 +00:00
|
|
|
} else {
|
2009-06-27 03:42:12 +00:00
|
|
|
out += "/>";
|
2009-06-01 21:24:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return out;
|
2009-06-30 06:24:41 +00:00
|
|
|
}; // end svgToString()
|
2009-06-01 21:24:30 +00:00
|
|
|
|
2009-06-30 06:24:41 +00:00
|
|
|
var recalculateSelectedDimensions = 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
|
|
|
for (var i = 0; i < selectedElements.length; ++i) {
|
|
|
|
var selected = selectedElements[i];
|
2009-07-01 20:05:24 +00:00
|
|
|
if (selected == null) break;
|
2009-07-01 19:54:51 +00:00
|
|
|
var selectedBBox = selectedBBoxes[i];
|
|
|
|
var box = selected.getBBox();
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
2009-06-29 14:38:30 +00:00
|
|
|
|
2009-07-01 19:54:51 +00:00
|
|
|
// after this point, we have some change
|
2009-06-29 14:38:30 +00:00
|
|
|
|
2009-07-01 19:54:51 +00:00
|
|
|
var remapx = function(x) {return ((x-box.x)/box.width)*selectedBBox.width + selectedBBox.x;}
|
|
|
|
var remapy = function(y) {return ((y-box.y)/box.height)*selectedBBox.height + selectedBBox.y;}
|
|
|
|
var scalew = function(w) {return w*selectedBBox.width/box.width;}
|
|
|
|
var scaleh = function(h) {return h*selectedBBox.height/box.height;}
|
2009-06-29 14:38:30 +00:00
|
|
|
|
2009-07-01 19:54:51 +00:00
|
|
|
var changes = {};
|
2009-06-23 07:48:15 +00:00
|
|
|
|
2009-07-01 19:54:51 +00:00
|
|
|
selected.removeAttribute("transform");
|
|
|
|
switch (selected.tagName)
|
|
|
|
{
|
2009-06-23 05:17:53 +00:00
|
|
|
case "path":
|
|
|
|
// extract the x,y from the path, adjust it and write back the new path
|
2009-06-25 02:54:07 +00:00
|
|
|
// but first, save the old path
|
|
|
|
changes["d"] = selected.getAttribute("d");
|
2009-06-23 05:17:53 +00:00
|
|
|
var M = selected.pathSegList.getItem(0);
|
|
|
|
var curx = M.x, cury = M.y;
|
|
|
|
var newd = "M" + remapx(curx) + "," + remapy(cury);
|
|
|
|
for (var i = 1; i < selected.pathSegList.numberOfItems; ++i) {
|
|
|
|
var l = selected.pathSegList.getItem(i);
|
|
|
|
var x = l.x, y = l.y;
|
|
|
|
// webkit browsers normalize things and this becomes an absolute
|
|
|
|
// line segment! we need to turn this back into a rel line segment
|
|
|
|
// see https://bugs.webkit.org/show_bug.cgi?id=26487
|
|
|
|
if (l.pathSegType == 4) {
|
|
|
|
x -= curx;
|
|
|
|
y -= cury;
|
|
|
|
curx += x;
|
|
|
|
cury += y;
|
|
|
|
}
|
|
|
|
// we only need to scale the relative coordinates (no need to translate)
|
|
|
|
newd += " l" + scalew(x) + "," + scaleh(y);
|
|
|
|
}
|
|
|
|
selected.setAttributeNS(null, "d", newd);
|
|
|
|
break;
|
|
|
|
case "line":
|
2009-06-25 02:54:07 +00:00
|
|
|
changes["x1"] = selected.x1.baseVal.value;
|
|
|
|
changes["y1"] = selected.y1.baseVal.value;
|
|
|
|
changes["x2"] = selected.x2.baseVal.value;
|
|
|
|
changes["y2"] = selected.y2.baseVal.value;
|
2009-06-23 05:17:53 +00:00
|
|
|
selected.x1.baseVal.value = remapx(selected.x1.baseVal.value);
|
|
|
|
selected.y1.baseVal.value = remapy(selected.y1.baseVal.value);
|
|
|
|
selected.x2.baseVal.value = remapx(selected.x2.baseVal.value);
|
|
|
|
selected.y2.baseVal.value = remapy(selected.y2.baseVal.value);
|
|
|
|
break;
|
|
|
|
case "circle":
|
2009-06-25 02:54:07 +00:00
|
|
|
changes["cx"] = selected.cx.baseVal.value;
|
|
|
|
changes["cy"] = selected.cy.baseVal.value;
|
2009-06-29 14:38:30 +00:00
|
|
|
changes["r"] = selected.r.baseVal.value;
|
2009-06-23 05:17:53 +00:00
|
|
|
selected.cx.baseVal.value = remapx(selected.cx.baseVal.value);
|
|
|
|
selected.cy.baseVal.value = remapy(selected.cy.baseVal.value);
|
|
|
|
// take the minimum of the new selected box's dimensions for the new circle radius
|
|
|
|
selected.r.baseVal.value = Math.min(selectedBBox.width/2,selectedBBox.height/2);
|
|
|
|
break;
|
|
|
|
case "ellipse":
|
2009-06-25 02:54:07 +00:00
|
|
|
changes["cx"] = selected.cx.baseVal.value;
|
|
|
|
changes["cy"] = selected.cy.baseVal.value;
|
|
|
|
changes["rx"] = selected.rx.baseVal.value;
|
|
|
|
changes["ry"] = selected.ry.baseVal.value;
|
2009-06-23 05:17:53 +00:00
|
|
|
selected.cx.baseVal.value = remapx(selected.cx.baseVal.value);
|
|
|
|
selected.cy.baseVal.value = remapy(selected.cy.baseVal.value);
|
|
|
|
selected.rx.baseVal.value = scalew(selected.rx.baseVal.value);
|
|
|
|
selected.ry.baseVal.value = scaleh(selected.ry.baseVal.value);
|
|
|
|
break;
|
|
|
|
case "text":
|
|
|
|
// cannot use x.baseVal.value here because x is a SVGLengthList
|
2009-06-25 02:54:07 +00:00
|
|
|
changes["x"] = selected.getAttribute("x");
|
|
|
|
changes["y"] = selected.getAttribute("y");
|
2009-06-23 05:17:53 +00:00
|
|
|
selected.setAttribute("x", remapx(selected.getAttribute("x")));
|
|
|
|
selected.setAttribute("y", remapy(selected.getAttribute("y")));
|
|
|
|
break;
|
|
|
|
case "rect":
|
2009-06-25 02:54:07 +00:00
|
|
|
changes["x"] = selected.x.baseVal.value;
|
|
|
|
changes["y"] = selected.y.baseVal.value;
|
|
|
|
changes["width"] = selected.width.baseVal.value;
|
|
|
|
changes["height"] = selected.height.baseVal.value;
|
2009-06-23 05:17:53 +00:00
|
|
|
selected.x.baseVal.value = remapx(selected.x.baseVal.value);
|
|
|
|
selected.y.baseVal.value = remapy(selected.y.baseVal.value);
|
|
|
|
selected.width.baseVal.value = scalew(selected.width.baseVal.value);
|
|
|
|
selected.height.baseVal.value = scaleh(selected.height.baseVal.value);
|
|
|
|
break;
|
|
|
|
default: // rect
|
|
|
|
console.log("Unknown shape type: " + selected.tagName);
|
|
|
|
break;
|
2009-07-01 19:54:51 +00:00
|
|
|
}
|
|
|
|
if (changes) {
|
2009-07-01 21:28:22 +00:00
|
|
|
batchCmd.addSubCommand(new ChangeElementCommand(selected, changes, text));
|
2009-07-01 19:54:51 +00:00
|
|
|
}
|
2009-07-01 21:28:22 +00:00
|
|
|
} // for each selected element
|
|
|
|
|
|
|
|
if (!batchCmd.isEmpty()) addCommandToHistory(batchCmd);
|
2009-07-01 19:54:51 +00:00
|
|
|
call("changed", selectedElements);
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-23 05:17:53 +00:00
|
|
|
|
|
|
|
var recalculateSelectedOutline = function() {
|
2009-07-01 19:54:51 +00:00
|
|
|
for (var i = 0; i < selectedElements.length; ++i) {
|
|
|
|
var selected = selectedElements[i];
|
2009-07-01 20:05:24 +00:00
|
|
|
if (selected == null) break;
|
2009-07-01 19:54:51 +00:00
|
|
|
var selectedBBox = selectedBBoxes[i]
|
|
|
|
var theSelector = selectorManager.requestSelector(selected);
|
|
|
|
if (selected != null && theSelector != null) {
|
|
|
|
theSelector.resize(selectedBBox);
|
|
|
|
}
|
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; }
|
|
|
|
|
|
|
|
for (var i = 0; i < selectedElements.length; ++i) {
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
this.addToSelection = function(elemsToAdd) {
|
|
|
|
if (elemsToAdd.length == 0) { return; }
|
2009-07-01 21:59:11 +00:00
|
|
|
|
|
|
|
console.log("addToSelection()");
|
|
|
|
console.log(elemsToAdd);
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// now add each element consecutively
|
|
|
|
for (var i = 0; i < elemsToAdd.length; ++i) {
|
|
|
|
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;
|
|
|
|
selectedBBoxes[j++] = elem.getBBox();
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
this.removeFromSelection = function(elemsToRemove) {
|
|
|
|
if (selectedElements[0] == null) { return; }
|
|
|
|
if (elemsToRemove.length == 0) { return; }
|
|
|
|
|
|
|
|
// find every element and remove it from our array copy
|
|
|
|
var newSelectedItems = new Array(selectedElements.length);
|
|
|
|
var j = 0;
|
|
|
|
for (var i = 0; i < selectedElements.length; ++i) {
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
// else the user is going to manipulate the selected elements
|
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-06-30 06:24:41 +00:00
|
|
|
rubberBox.x.baseVal.value = start_x;
|
|
|
|
rubberBox.y.baseVal.value = start_y;
|
|
|
|
rubberBox.width.baseVal.value = 0;
|
|
|
|
rubberBox.height.baseVal.value = 0;
|
|
|
|
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;
|
|
|
|
d_attr = "M" + x + "," + y + " ";
|
2009-06-03 08:41:25 +00:00
|
|
|
addSvgElementFromJson({
|
|
|
|
"element": "path",
|
|
|
|
"attr": {
|
|
|
|
"d": d_attr,
|
2009-06-05 12:33:02 +00:00
|
|
|
"id": getId(),
|
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,
|
|
|
|
"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-06-06 13:21:45 +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
|
|
|
"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-06-05 12:33:02 +00:00
|
|
|
"id": getId(),
|
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-06-05 12:33:02 +00:00
|
|
|
"id": getId(),
|
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-06-05 13:20:04 +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
|
|
|
"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,
|
|
|
|
"id": getId(),
|
|
|
|
"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,
|
|
|
|
"font-family": current_font_family,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
newText.textContent = "text";
|
|
|
|
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-06-29 14:38:30 +00:00
|
|
|
// in mouseMove 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-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-06-23 05:17:53 +00:00
|
|
|
var ts = "translate(" + dx + "," + dy + ")";
|
2009-07-01 19:54:51 +00:00
|
|
|
for (var i = 0; i < selectedElements.length; ++i) {
|
|
|
|
var selected = selectedElements[i];
|
|
|
|
if (selected == null) break;
|
|
|
|
selectedBBoxes[i] = selected.getBBox();
|
|
|
|
selectedBBoxes[i].x += dx;
|
|
|
|
selectedBBoxes[i].y += dy;
|
|
|
|
selected.setAttribute("transform", ts);
|
|
|
|
}
|
2009-06-23 07:48:15 +00:00
|
|
|
recalculateSelectedOutline();
|
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-07-01 19:28:08 +00:00
|
|
|
rubberBox.x.baseVal.value = Math.min(start_x,x);
|
|
|
|
rubberBox.y.baseVal.value = Math.min(start_y,y);
|
|
|
|
rubberBox.width.baseVal.value = Math.abs(x-start_x);
|
|
|
|
rubberBox.height.baseVal.value = Math.abs(y-start_y);
|
|
|
|
|
2009-06-30 06:24:41 +00:00
|
|
|
// evt.target is the element that the mouse pointer is passing over
|
|
|
|
// TODO: add new targets to the selectedElements array, create a
|
|
|
|
// selector for it, etc
|
2009-07-01 21:59:11 +00:00
|
|
|
var nodeName = evt.target.nodeName.toLowerCase();
|
2009-06-30 14:29:28 +00:00
|
|
|
if (nodeName != "div" && nodeName != "svg") {
|
2009-07-01 19:28:08 +00:00
|
|
|
canvas.addToSelection([evt.target]);
|
2009-06-30 14:29:28 +00:00
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
break;
|
2009-06-23 05:17:53 +00:00
|
|
|
case "resize":
|
|
|
|
// we track the resize bounding box and translate/scale the selected element
|
|
|
|
// while the mouse is down, when mouse goes up, we use this to recalculate
|
|
|
|
// the shape's coordinates
|
2009-06-23 07:48:15 +00:00
|
|
|
var box=selected.getBBox(), 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
|
|
|
|
|
|
|
ts = "translate(" + (left+tx) + "," + (top+ty) + ") scale(" + (sx) + "," + (sy) +
|
2009-06-23 05:17:53 +00:00
|
|
|
") translate(" + (-left) + "," + (-top) + ")";
|
|
|
|
selected.setAttribute("transform", ts);
|
|
|
|
recalculateSelectedOutline();
|
2009-06-23 07:48:15 +00:00
|
|
|
break;
|
2009-06-10 03:12:19 +00:00
|
|
|
case "text":
|
|
|
|
shape.setAttribute("x", x);
|
|
|
|
shape.setAttribute("y", y);
|
|
|
|
break;
|
2009-06-02 16:40:33 +00:00
|
|
|
case "line":
|
|
|
|
shape.setAttributeNS(null, "x2", x);
|
|
|
|
shape.setAttributeNS(null, "y2", y);
|
|
|
|
break;
|
|
|
|
case "square":
|
|
|
|
var size = Math.max( Math.abs(x - start_x), Math.abs(y - start_y) );
|
|
|
|
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-06-02 16:40:33 +00:00
|
|
|
break;
|
|
|
|
case "rect":
|
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-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");
|
|
|
|
shape.setAttributeNS(null, "rx", Math.abs(x - cx) );
|
|
|
|
shape.setAttributeNS(null, "ry", Math.abs(y - cy) );
|
|
|
|
break;
|
|
|
|
case "fhellipse":
|
|
|
|
case "fhrect":
|
|
|
|
freehand_min_x = Math.min(x, freehand_min_x);
|
|
|
|
freehand_max_x = Math.max(x, freehand_max_x);
|
|
|
|
freehand_min_y = Math.min(y, freehand_min_y);
|
|
|
|
freehand_max_y = Math.max(y, freehand_max_y);
|
2009-06-03 08:41:25 +00:00
|
|
|
// break; missing on purpose
|
2009-06-02 16:40:33 +00:00
|
|
|
case "path":
|
2009-06-08 02:17:07 +00:00
|
|
|
var dx = x - start_x;
|
|
|
|
var dy = y - start_y;
|
|
|
|
start_x = x;
|
|
|
|
start_y = y;
|
|
|
|
d_attr += "l" + dx + "," + dy + " ";
|
2009-06-02 16:40:33 +00:00
|
|
|
shape.setAttributeNS(null, "d", d_attr);
|
|
|
|
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-06-23 18:06:53 +00:00
|
|
|
// fire changed event
|
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-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
|
|
|
|
// this is done in recalculateSelectedDimensions()
|
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;
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
selectorManager.requestSelector(selected).showGrips(selected.tagName != "text");
|
|
|
|
}
|
2009-06-23 05:17:53 +00:00
|
|
|
recalculateSelectedDimensions();
|
2009-06-18 05:08:47 +00:00
|
|
|
recalculateSelectedOutline();
|
2009-06-08 02:17:07 +00:00
|
|
|
// we return immediately from select so that the obj_num is not incremented
|
|
|
|
return;
|
2009-07-01 19:28:08 +00:00
|
|
|
}
|
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-06-02 16:40:33 +00:00
|
|
|
}
|
2009-06-06 17:53:38 +00:00
|
|
|
d_attr = null;
|
|
|
|
obj_num++;
|
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-06-27 03:42:12 +00:00
|
|
|
// see http://jwatt.org/svg/authoring/#doctype-declaration
|
|
|
|
// str += "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n";
|
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-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-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-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-06-25 08:22:30 +00:00
|
|
|
svgroot.setAttribute("width", x);
|
|
|
|
svgroot.setAttribute("height", y);
|
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-06-02 16:40:33 +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;
|
|
|
|
this.changeSelectedAttribute("fill", 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.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-06-04 13:43:58 +00:00
|
|
|
|
2009-06-09 22:13:13 +00:00
|
|
|
this.updateElementFromJson = function(data) {
|
2009-06-12 11:43:20 +00:00
|
|
|
var shape = svgdoc.getElementById(data.attr.id);
|
2009-06-16 02:39:12 +00:00
|
|
|
// if shape is a path but we need to create a rect/ellipse, then remove the path
|
2009-06-16 18:51:35 +00:00
|
|
|
if (shape && data.element != shape.tagName) {
|
2009-06-16 02:39:12 +00:00
|
|
|
svgroot.removeChild(shape);
|
|
|
|
shape = null;
|
|
|
|
}
|
|
|
|
if (!shape) {
|
|
|
|
shape = svgdoc.createElementNS(svgns, data.element);
|
|
|
|
svgroot.appendChild(shape);
|
|
|
|
}
|
2009-06-09 22:13:13 +00:00
|
|
|
assignAttributes(shape, data.attr);
|
|
|
|
cleanupElement(shape);
|
|
|
|
return shape;
|
2009-06-30 06:24:41 +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-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-06-24 04:33:09 +00:00
|
|
|
this.changeSelectedAttribute = function(attr, val) {
|
2009-07-01 21:28:22 +00:00
|
|
|
var batchCmd = new BatchCommand("Change " + attr);
|
2009-07-01 20:05:24 +00:00
|
|
|
for (var i = 0; i < selectedElements.length; ++i) {
|
|
|
|
var selected = selectedElements[i];
|
|
|
|
if (selected == null) break;
|
|
|
|
|
2009-06-25 02:54:07 +00:00
|
|
|
var oldval = (attr == "#text" ? selected.textContent : selected.getAttribute(attr));
|
|
|
|
if (oldval != val) {
|
|
|
|
if (attr == "#text") selected.textContent = val;
|
|
|
|
else selected.setAttribute(attr, val);
|
2009-07-01 20:05:24 +00:00
|
|
|
selectedBBoxes[i] = selected.getBBox();
|
2009-06-25 02:54:07 +00:00
|
|
|
recalculateSelectedOutline();
|
|
|
|
var changes = {};
|
|
|
|
changes[attr] = oldval;
|
2009-07-01 21:28:22 +00:00
|
|
|
batchCmd.addSubCommand(new ChangeElementCommand(selected, changes, attr));
|
2009-06-25 02:54:07 +00:00
|
|
|
}
|
2009-06-24 04:33:09 +00:00
|
|
|
}
|
2009-07-01 21:28:22 +00:00
|
|
|
if (!batchCmd.isEmpty()) addCommandToHistory(batchCmd);
|
2009-07-01 20:05:24 +00:00
|
|
|
call("changed", selectedElements);
|
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-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-01 20:22:29 +00:00
|
|
|
for (var i = 0; i < selectedElements.length; ++i) {
|
|
|
|
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;
|
|
|
|
// TODO: batch all element deletions up into a batch command
|
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-06-25 02:54:07 +00:00
|
|
|
t = t.parentNode.insertBefore(t, t.parentNode.firstChild);
|
|
|
|
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-06-23 21:18:23 +00:00
|
|
|
this.moveSelectedElement = function(dx,dy) {
|
2009-06-29 22:22:13 +00:00
|
|
|
var selected = selectedElements[0];
|
2009-06-23 21:18:23 +00:00
|
|
|
if (selected != null) {
|
2009-07-01 19:28:08 +00:00
|
|
|
selectedBBoxes[0] = selected.getBBox();
|
|
|
|
selectedBBoxes[0].x += dx;
|
|
|
|
selectedBBoxes[0].y += dy;
|
2009-06-29 14:38:30 +00:00
|
|
|
|
2009-06-23 21:18:23 +00:00
|
|
|
recalculateSelectedDimensions();
|
|
|
|
recalculateSelectedOutline();
|
|
|
|
}
|
2009-06-30 06:24:41 +00:00
|
|
|
};
|
2009-06-17 20:08:53 +00:00
|
|
|
|
2009-06-25 15:35:28 +00:00
|
|
|
this.getUndoStackSize = function() { return undoStackPointer; }
|
2009-06-25 02:54:07 +00:00
|
|
|
this.getRedoStackSize = function() { return undoStack.length - undoStackPointer; }
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
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-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-06-08 09:08:45 +00:00
|
|
|
}
|
2009-06-16 15:47:38 +00:00
|
|
|
|
2009-06-08 08:09:15 +00:00
|
|
|
}
|