- Linting (ESLint): Finish extensions and most files in editor/; unfinished: editor/svg-editor.js, editor/svgcanvas.js
- Linting (ESLint): Fix ignore file paths - History `elem` fix
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*globals $, svgedit*/
|
||||
/*jslint vars: true, eqeq: true*/
|
||||
/* eslint-disable no-var, eqeqeq */
|
||||
/* globals $, svgedit */
|
||||
/**
|
||||
* SVGTransformList
|
||||
*
|
||||
@@ -12,7 +12,8 @@
|
||||
// Dependencies:
|
||||
// 1) browser.js
|
||||
|
||||
(function() {'use strict';
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
if (!svgedit.transformlist) {
|
||||
svgedit.transformlist = {};
|
||||
@@ -21,48 +22,49 @@ if (!svgedit.transformlist) {
|
||||
var svgroot = document.createElementNS(svgedit.NS.SVG, 'svg');
|
||||
|
||||
// Helper function.
|
||||
function transformToString(xform) {
|
||||
function transformToString (xform) {
|
||||
var m = xform.matrix,
|
||||
text = '';
|
||||
switch(xform.type) {
|
||||
case 1: // MATRIX
|
||||
text = 'matrix(' + [m.a, m.b, m.c, m.d, m.e, m.f].join(',') + ')';
|
||||
break;
|
||||
case 2: // TRANSLATE
|
||||
text = 'translate(' + m.e + ',' + m.f + ')';
|
||||
break;
|
||||
case 3: // SCALE
|
||||
if (m.a == m.d) {text = 'scale(' + m.a + ')';}
|
||||
else {text = 'scale(' + m.a + ',' + m.d + ')';}
|
||||
break;
|
||||
case 4: // ROTATE
|
||||
var cx = 0, cy = 0;
|
||||
// this prevents divide by zero
|
||||
if (xform.angle != 0) {
|
||||
var K = 1 - m.a;
|
||||
cy = ( K * m.f + m.b*m.e ) / ( K*K + m.b*m.b );
|
||||
cx = ( m.e - m.b * cy ) / K;
|
||||
}
|
||||
text = 'rotate(' + xform.angle + ' ' + cx + ',' + cy + ')';
|
||||
break;
|
||||
switch (xform.type) {
|
||||
case 1: // MATRIX
|
||||
text = 'matrix(' + [m.a, m.b, m.c, m.d, m.e, m.f].join(',') + ')';
|
||||
break;
|
||||
case 2: // TRANSLATE
|
||||
text = 'translate(' + m.e + ',' + m.f + ')';
|
||||
break;
|
||||
case 3: // SCALE
|
||||
if (m.a == m.d) {
|
||||
text = 'scale(' + m.a + ')';
|
||||
} else {
|
||||
text = 'scale(' + m.a + ',' + m.d + ')';
|
||||
}
|
||||
break;
|
||||
case 4: // ROTATE
|
||||
var cx = 0, cy = 0;
|
||||
// this prevents divide by zero
|
||||
if (xform.angle != 0) {
|
||||
var K = 1 - m.a;
|
||||
cy = (K * m.f + m.b * m.e) / (K * K + m.b * m.b);
|
||||
cx = (m.e - m.b * cy) / K;
|
||||
}
|
||||
text = 'rotate(' + xform.angle + ' ' + cx + ',' + cy + ')';
|
||||
break;
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Map of SVGTransformList objects.
|
||||
*/
|
||||
var listMap_ = {};
|
||||
|
||||
|
||||
// **************************************************************************************
|
||||
// SVGTransformList implementation for Webkit
|
||||
// SVGTransformList implementation for Webkit
|
||||
// These methods do not currently raise any exceptions.
|
||||
// These methods also do not check that transforms are being inserted. This is basically
|
||||
// implementing as much of SVGTransformList that we need to get the job done.
|
||||
//
|
||||
// interface SVGEditTransformList {
|
||||
// interface SVGEditTransformList {
|
||||
// attribute unsigned long numberOfItems;
|
||||
// void clear ( )
|
||||
// SVGTransform initialize ( in SVGTransform newItem )
|
||||
@@ -75,13 +77,13 @@ var listMap_ = {};
|
||||
// NOT IMPLEMENTED: SVGTransform consolidate ( );
|
||||
// }
|
||||
// **************************************************************************************
|
||||
svgedit.transformlist.SVGTransformList = function(elem) {
|
||||
svgedit.transformlist.SVGTransformList = function (elem) {
|
||||
this._elem = elem || null;
|
||||
this._xforms = [];
|
||||
// TODO: how do we capture the undo-ability in the changed transform list?
|
||||
this._update = function() {
|
||||
this._update = function () {
|
||||
var tstr = '';
|
||||
var concatMatrix = svgroot.createSVGMatrix();
|
||||
/* var concatMatrix = */ svgroot.createSVGMatrix();
|
||||
var i;
|
||||
for (i = 0; i < this.numberOfItems; ++i) {
|
||||
var xform = this._list.getItem(i);
|
||||
@@ -90,10 +92,10 @@ svgedit.transformlist.SVGTransformList = function(elem) {
|
||||
this._elem.setAttribute('transform', tstr);
|
||||
};
|
||||
this._list = this;
|
||||
this._init = function() {
|
||||
this._init = function () {
|
||||
// Transform attribute parser
|
||||
var str = this._elem.getAttribute('transform');
|
||||
if (!str) {return;}
|
||||
if (!str) { return; }
|
||||
|
||||
// TODO: Add skew support in future
|
||||
var re = /\s*((scale|matrix|rotate|translate)\s*\(.*?\))\s*,?\s*/;
|
||||
@@ -105,20 +107,20 @@ svgedit.transformlist.SVGTransformList = function(elem) {
|
||||
var x = m[1];
|
||||
var bits = x.split(/\s*\(/);
|
||||
var name = bits[0];
|
||||
var val_bits = bits[1].match(/\s*(.*?)\s*\)/);
|
||||
val_bits[1] = val_bits[1].replace(/(\d)-/g, '$1 -');
|
||||
var val_arr = val_bits[1].split(/[, ]+/);
|
||||
var valBits = bits[1].match(/\s*(.*?)\s*\)/);
|
||||
valBits[1] = valBits[1].replace(/(\d)-/g, '$1 -');
|
||||
var valArr = valBits[1].split(/[, ]+/);
|
||||
var letters = 'abcdef'.split('');
|
||||
var mtx = svgroot.createSVGMatrix();
|
||||
$.each(val_arr, function(i, item) {
|
||||
val_arr[i] = parseFloat(item);
|
||||
$.each(valArr, function (i, item) {
|
||||
valArr[i] = parseFloat(item);
|
||||
if (name == 'matrix') {
|
||||
mtx[letters[i]] = val_arr[i];
|
||||
mtx[letters[i]] = valArr[i];
|
||||
}
|
||||
});
|
||||
var xform = svgroot.createSVGTransform();
|
||||
var fname = 'set' + name.charAt(0).toUpperCase() + name.slice(1);
|
||||
var values = name == 'matrix' ? [mtx] : val_arr;
|
||||
var values = name == 'matrix' ? [mtx] : valArr;
|
||||
|
||||
if (name == 'scale' && values.length == 1) {
|
||||
values.push(values[0]);
|
||||
@@ -132,7 +134,7 @@ svgedit.transformlist.SVGTransformList = function(elem) {
|
||||
}
|
||||
}
|
||||
};
|
||||
this._removeFromOtherLists = function(item) {
|
||||
this._removeFromOtherLists = function (item) {
|
||||
if (item) {
|
||||
// Check if this transform is already in a transformlist, and
|
||||
// remove it if so.
|
||||
@@ -156,25 +158,27 @@ svgedit.transformlist.SVGTransformList = function(elem) {
|
||||
};
|
||||
|
||||
this.numberOfItems = 0;
|
||||
this.clear = function() {
|
||||
this.clear = function () {
|
||||
this.numberOfItems = 0;
|
||||
this._xforms = [];
|
||||
};
|
||||
|
||||
this.initialize = function(newItem) {
|
||||
this.initialize = function (newItem) {
|
||||
this.numberOfItems = 1;
|
||||
this._removeFromOtherLists(newItem);
|
||||
this._xforms = [newItem];
|
||||
};
|
||||
|
||||
this.getItem = function(index) {
|
||||
this.getItem = function (index) {
|
||||
if (index < this.numberOfItems && index >= 0) {
|
||||
return this._xforms[index];
|
||||
}
|
||||
throw {code: 1}; // DOMException with code=INDEX_SIZE_ERR
|
||||
var err = new Error('DOMException with code=INDEX_SIZE_ERR');
|
||||
err.code = 1;
|
||||
throw err;
|
||||
};
|
||||
|
||||
this.insertItemBefore = function(newItem, index) {
|
||||
this.insertItemBefore = function (newItem, index) {
|
||||
var retValue = null;
|
||||
if (index >= 0) {
|
||||
if (index < this.numberOfItems) {
|
||||
@@ -187,22 +191,21 @@ svgedit.transformlist.SVGTransformList = function(elem) {
|
||||
}
|
||||
newxforms[i] = newItem;
|
||||
var j;
|
||||
for (j = i+1; i < this.numberOfItems; ++j, ++i) {
|
||||
for (j = i + 1; i < this.numberOfItems; ++j, ++i) {
|
||||
newxforms[j] = this._xforms[i];
|
||||
}
|
||||
this.numberOfItems++;
|
||||
this._xforms = newxforms;
|
||||
retValue = newItem;
|
||||
this._list._update();
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
retValue = this._list.appendItem(newItem);
|
||||
}
|
||||
}
|
||||
return retValue;
|
||||
};
|
||||
|
||||
this.replaceItem = function(newItem, index) {
|
||||
this.replaceItem = function (newItem, index) {
|
||||
var retValue = null;
|
||||
if (index < this.numberOfItems && index >= 0) {
|
||||
this._removeFromOtherLists(newItem);
|
||||
@@ -213,7 +216,7 @@ svgedit.transformlist.SVGTransformList = function(elem) {
|
||||
return retValue;
|
||||
};
|
||||
|
||||
this.removeItem = function(index) {
|
||||
this.removeItem = function (index) {
|
||||
if (index < this.numberOfItems && index >= 0) {
|
||||
var retValue = this._xforms[index];
|
||||
var newxforms = new Array(this.numberOfItems - 1);
|
||||
@@ -221,18 +224,20 @@ svgedit.transformlist.SVGTransformList = function(elem) {
|
||||
for (i = 0; i < index; ++i) {
|
||||
newxforms[i] = this._xforms[i];
|
||||
}
|
||||
for (j = i; j < this.numberOfItems-1; ++j, ++i) {
|
||||
newxforms[j] = this._xforms[i+1];
|
||||
for (j = i; j < this.numberOfItems - 1; ++j, ++i) {
|
||||
newxforms[j] = this._xforms[i + 1];
|
||||
}
|
||||
this.numberOfItems--;
|
||||
this._xforms = newxforms;
|
||||
this._list._update();
|
||||
return retValue;
|
||||
}
|
||||
throw {code: 1}; // DOMException with code=INDEX_SIZE_ERR
|
||||
var err = new Error('DOMException with code=INDEX_SIZE_ERR');
|
||||
err.code = 1;
|
||||
throw err;
|
||||
};
|
||||
|
||||
this.appendItem = function(newItem) {
|
||||
this.appendItem = function (newItem) {
|
||||
this._removeFromOtherLists(newItem);
|
||||
this._xforms.push(newItem);
|
||||
this.numberOfItems++;
|
||||
@@ -241,8 +246,7 @@ svgedit.transformlist.SVGTransformList = function(elem) {
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
svgedit.transformlist.resetListMap = function() {
|
||||
svgedit.transformlist.resetListMap = function () {
|
||||
listMap_ = {};
|
||||
};
|
||||
|
||||
@@ -251,7 +255,7 @@ svgedit.transformlist.resetListMap = function() {
|
||||
* Parameters:
|
||||
* elem - a DOM Element
|
||||
*/
|
||||
svgedit.transformlist.removeElementFromListMap = function(elem) {
|
||||
svgedit.transformlist.removeElementFromListMap = function (elem) {
|
||||
if (elem.id && listMap_[elem.id]) {
|
||||
delete listMap_[elem.id];
|
||||
}
|
||||
@@ -262,7 +266,7 @@ svgedit.transformlist.removeElementFromListMap = function(elem) {
|
||||
//
|
||||
// Parameters:
|
||||
// elem - DOM element to get a transformlist from
|
||||
svgedit.transformlist.getTransformList = function(elem) {
|
||||
svgedit.transformlist.getTransformList = function (elem) {
|
||||
if (!svgedit.browser.supportsNativeTransformLists()) {
|
||||
var id = elem.id || 'temp';
|
||||
var t = listMap_[id];
|
||||
@@ -285,5 +289,4 @@ svgedit.transformlist.getTransformList = function(elem) {
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
}());
|
||||
}());
|
||||
|
||||
Reference in New Issue
Block a user