- 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:
121
editor/units.js
121
editor/units.js
@@ -1,5 +1,5 @@
|
||||
/*globals $, svgedit*/
|
||||
/*jslint vars: true, eqeq: true*/
|
||||
/* eslint-disable no-var */
|
||||
/* globals $, svgedit */
|
||||
/**
|
||||
* Package: svgedit.units
|
||||
*
|
||||
@@ -12,7 +12,8 @@
|
||||
// Dependencies:
|
||||
// 1) jQuery
|
||||
|
||||
(function() {'use strict';
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
if (!svgedit.units) {
|
||||
svgedit.units = {};
|
||||
@@ -23,8 +24,9 @@ var wAttrs = ['x', 'x1', 'cx', 'rx', 'width'];
|
||||
var hAttrs = ['y', 'y1', 'cy', 'ry', 'height'];
|
||||
var unitAttrs = ['r', 'radius'].concat(wAttrs, hAttrs);
|
||||
// unused
|
||||
/*
|
||||
var unitNumMap = {
|
||||
'%': 2,
|
||||
'%': 2,
|
||||
'em': 3,
|
||||
'ex': 4,
|
||||
'px': 5,
|
||||
@@ -34,7 +36,7 @@ var unitNumMap = {
|
||||
'pt': 9,
|
||||
'pc': 10
|
||||
};
|
||||
|
||||
*/
|
||||
// Container of elements.
|
||||
var elementContainer_;
|
||||
|
||||
@@ -60,7 +62,7 @@ var typeMap_ = {};
|
||||
* Parameters:
|
||||
* elementContainer - an object implementing the ElementContainer interface.
|
||||
*/
|
||||
svgedit.units.init = function(elementContainer) {
|
||||
svgedit.units.init = function (elementContainer) {
|
||||
elementContainer_ = elementContainer;
|
||||
|
||||
// Get correct em/ex values by creating a temporary SVG.
|
||||
@@ -92,7 +94,7 @@ svgedit.units.init = function(elementContainer) {
|
||||
|
||||
// Function: svgedit.units.getTypeMap
|
||||
// Returns the unit object with values for each unit
|
||||
svgedit.units.getTypeMap = function() {
|
||||
svgedit.units.getTypeMap = function () {
|
||||
return typeMap_;
|
||||
};
|
||||
|
||||
@@ -105,7 +107,7 @@ svgedit.units.getTypeMap = function() {
|
||||
// Returns:
|
||||
// If a string/number was given, returns a Float. If an array, return a string
|
||||
// with comma-seperated floats
|
||||
svgedit.units.shortFloat = function(val) {
|
||||
svgedit.units.shortFloat = function (val) {
|
||||
var digits = elementContainer_.getRoundDigits();
|
||||
if (!isNaN(val)) {
|
||||
// Note that + converts to Number
|
||||
@@ -119,11 +121,11 @@ svgedit.units.shortFloat = function(val) {
|
||||
|
||||
// Function: svgedit.units.convertUnit
|
||||
// Converts the number to given unit or baseUnit
|
||||
svgedit.units.convertUnit = function(val, unit) {
|
||||
svgedit.units.convertUnit = function (val, unit) {
|
||||
unit = unit || elementContainer_.getBaseUnit();
|
||||
// baseVal.convertToSpecifiedUnits(unitNumMap[unit]);
|
||||
// var val = baseVal.valueInSpecifiedUnits;
|
||||
// baseVal.convertToSpecifiedUnits(1);
|
||||
// baseVal.convertToSpecifiedUnits(unitNumMap[unit]);
|
||||
// var val = baseVal.valueInSpecifiedUnits;
|
||||
// baseVal.convertToSpecifiedUnits(1);
|
||||
return svgedit.units.shortFloat(val / typeMap_[unit]);
|
||||
};
|
||||
|
||||
@@ -134,38 +136,38 @@ svgedit.units.convertUnit = function(val, unit) {
|
||||
// elem - DOM element to be changed
|
||||
// attr - String with the name of the attribute associated with the value
|
||||
// val - String with the attribute value to convert
|
||||
svgedit.units.setUnitAttr = function(elem, attr, val) {
|
||||
// if (!isNaN(val)) {
|
||||
// New value is a number, so check currently used unit
|
||||
// var old_val = elem.getAttribute(attr);
|
||||
svgedit.units.setUnitAttr = function (elem, attr, val) {
|
||||
// if (!isNaN(val)) {
|
||||
// New value is a number, so check currently used unit
|
||||
// var old_val = elem.getAttribute(attr);
|
||||
|
||||
// Enable this for alternate mode
|
||||
// if (old_val !== null && (isNaN(old_val) || elementContainer_.getBaseUnit() !== 'px')) {
|
||||
// // Old value was a number, so get unit, then convert
|
||||
// var unit;
|
||||
// if (old_val.substr(-1) === '%') {
|
||||
// var res = getResolution();
|
||||
// unit = '%';
|
||||
// val *= 100;
|
||||
// if (wAttrs.indexOf(attr) >= 0) {
|
||||
// val = val / res.w;
|
||||
// } else if (hAttrs.indexOf(attr) >= 0) {
|
||||
// val = val / res.h;
|
||||
// } else {
|
||||
// return val / Math.sqrt((res.w*res.w) + (res.h*res.h))/Math.sqrt(2);
|
||||
// }
|
||||
// } else {
|
||||
// if (elementContainer_.getBaseUnit() !== 'px') {
|
||||
// unit = elementContainer_.getBaseUnit();
|
||||
// } else {
|
||||
// unit = old_val.substr(-2);
|
||||
// }
|
||||
// val = val / typeMap_[unit];
|
||||
// }
|
||||
//
|
||||
// val += unit;
|
||||
// }
|
||||
// }
|
||||
// Enable this for alternate mode
|
||||
// if (old_val !== null && (isNaN(old_val) || elementContainer_.getBaseUnit() !== 'px')) {
|
||||
// // Old value was a number, so get unit, then convert
|
||||
// var unit;
|
||||
// if (old_val.substr(-1) === '%') {
|
||||
// var res = getResolution();
|
||||
// unit = '%';
|
||||
// val *= 100;
|
||||
// if (wAttrs.indexOf(attr) >= 0) {
|
||||
// val = val / res.w;
|
||||
// } else if (hAttrs.indexOf(attr) >= 0) {
|
||||
// val = val / res.h;
|
||||
// } else {
|
||||
// return val / Math.sqrt((res.w*res.w) + (res.h*res.h))/Math.sqrt(2);
|
||||
// }
|
||||
// } else {
|
||||
// if (elementContainer_.getBaseUnit() !== 'px') {
|
||||
// unit = elementContainer_.getBaseUnit();
|
||||
// } else {
|
||||
// unit = old_val.substr(-2);
|
||||
// }
|
||||
// val = val / typeMap_[unit];
|
||||
// }
|
||||
//
|
||||
// val += unit;
|
||||
// }
|
||||
// }
|
||||
elem.setAttribute(attr, val);
|
||||
};
|
||||
|
||||
@@ -185,11 +187,11 @@ var attrsToConvert = {
|
||||
//
|
||||
// Parameters:
|
||||
// element - a DOM element whose attributes should be converted
|
||||
svgedit.units.convertAttrs = function(element) {
|
||||
svgedit.units.convertAttrs = function (element) {
|
||||
var elName = element.tagName;
|
||||
var unit = elementContainer_.getBaseUnit();
|
||||
var attrs = attrsToConvert[elName];
|
||||
if (!attrs) {return;}
|
||||
if (!attrs) { return; }
|
||||
|
||||
var len = attrs.length;
|
||||
var i;
|
||||
@@ -201,26 +203,26 @@ svgedit.units.convertAttrs = function(element) {
|
||||
element.setAttribute(attr, (cur / typeMap_[unit]) + unit);
|
||||
}
|
||||
// else {
|
||||
// Convert existing?
|
||||
// Convert existing?
|
||||
// }
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Function: svgedit.units.convertToNum
|
||||
// Converts given values to numbers. Attributes must be supplied in
|
||||
// Converts given values to numbers. Attributes must be supplied in
|
||||
// case a percentage is given
|
||||
//
|
||||
// Parameters:
|
||||
// attr - String with the name of the attribute associated with the value
|
||||
// val - String with the attribute value to convert
|
||||
svgedit.units.convertToNum = function(attr, val) {
|
||||
svgedit.units.convertToNum = function (attr, val) {
|
||||
// Return a number if that's what it already is
|
||||
if (!isNaN(val)) {return val-0;}
|
||||
if (!isNaN(val)) { return val - 0; }
|
||||
var num;
|
||||
if (val.substr(-1) === '%') {
|
||||
// Deal with percentage, depends on attribute
|
||||
num = val.substr(0, val.length-1)/100;
|
||||
num = val.substr(0, val.length - 1) / 100;
|
||||
var width = elementContainer_.getWidth();
|
||||
var height = elementContainer_.getHeight();
|
||||
|
||||
@@ -230,10 +232,10 @@ svgedit.units.convertToNum = function(attr, val) {
|
||||
if (hAttrs.indexOf(attr) >= 0) {
|
||||
return num * height;
|
||||
}
|
||||
return num * Math.sqrt((width*width) + (height*height))/Math.sqrt(2);
|
||||
return num * Math.sqrt((width * width) + (height * height)) / Math.sqrt(2);
|
||||
}
|
||||
var unit = val.substr(-2);
|
||||
num = val.substr(0, val.length-2);
|
||||
num = val.substr(0, val.length - 2);
|
||||
// Note that this multiplication turns the string into a number
|
||||
return num * typeMap_[unit];
|
||||
};
|
||||
@@ -244,7 +246,7 @@ svgedit.units.convertToNum = function(attr, val) {
|
||||
// Parameters:
|
||||
// attr - String with the name of the attribute associated with the value
|
||||
// val - String with the attribute value to check
|
||||
svgedit.units.isValidUnit = function(attr, val, selectedElement) {
|
||||
svgedit.units.isValidUnit = function (attr, val, selectedElement) {
|
||||
var valid = false;
|
||||
if (unitAttrs.indexOf(attr) >= 0) {
|
||||
// True if it's just a number
|
||||
@@ -253,13 +255,13 @@ svgedit.units.isValidUnit = function(attr, val, selectedElement) {
|
||||
} else {
|
||||
// Not a number, check if it has a valid unit
|
||||
val = val.toLowerCase();
|
||||
$.each(typeMap_, function(unit) {
|
||||
if (valid) {return;}
|
||||
$.each(typeMap_, function (unit) {
|
||||
if (valid) { return; }
|
||||
var re = new RegExp('^-?[\\d\\.]+' + unit + '$');
|
||||
if (re.test(val)) {valid = true;}
|
||||
if (re.test(val)) { valid = true; }
|
||||
});
|
||||
}
|
||||
} else if (attr == 'id') {
|
||||
} else if (attr === 'id') {
|
||||
// if we're trying to change the id, make sure it's not already present in the doc
|
||||
// and the id value is valid.
|
||||
|
||||
@@ -271,12 +273,11 @@ svgedit.units.isValidUnit = function(attr, val, selectedElement) {
|
||||
try {
|
||||
var elem = elementContainer_.getElement(val);
|
||||
result = (elem == null || elem === selectedElement);
|
||||
} catch(e) {}
|
||||
} catch (e) {}
|
||||
return result;
|
||||
}
|
||||
valid = true;
|
||||
|
||||
return valid;
|
||||
};
|
||||
|
||||
}());
|
||||
}());
|
||||
|
||||
Reference in New Issue
Block a user