diff --git a/editor/units.js b/editor/units.js index d9ec0de2..019908bf 100644 --- a/editor/units.js +++ b/editor/units.js @@ -18,10 +18,10 @@ if (!svgedit.units) { svgedit.units = {}; } -var w_attrs = ['x', 'x1', 'cx', 'rx', 'width']; -var h_attrs = ['y', 'y1', 'cy', 'ry', 'height']; -var unit_attrs = $.merge(['r','radius'], w_attrs); - +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, 'em': 3, @@ -34,15 +34,13 @@ var unitNumMap = { 'pc': 10 }; -$.merge(unit_attrs, h_attrs); - // Container of elements. var elementContainer_; /** * Stores mapping of unit type to user coordinates. */ -var typeMap_ = {px: 1}; +var typeMap_ = {}; /** * ElementContainer interface @@ -78,14 +76,17 @@ svgedit.units.init = function(elementContainer) { document.body.removeChild(svg); var inch = bb.x; - typeMap_['em'] = bb.width; - typeMap_['ex'] = bb.height; - typeMap_['in'] = inch; - typeMap_['cm'] = inch / 2.54; - typeMap_['mm'] = inch / 25.4; - typeMap_['pt'] = inch / 72; - typeMap_['pc'] = inch / 6; - typeMap_['%'] = 0; + typeMap_ = { + 'em': bb.width, + 'ex': bb.height, + 'in': inch, + 'cm': inch / 2.54, + 'mm': inch / 25.4, + 'pt': inch / 72, + 'pc': inch / 6, + 'px': 1, + '%': 0 + }; }; // Group: Unit conversion functions @@ -99,7 +100,7 @@ svgedit.units.getTypeMap = function() { // Function: svgedit.units.shortFloat // Rounds a given value to a float with number of digits defined in save_options // -// Parameters: +// Parameters: // val - The value as a String, Number or Array of two numbers to be rounded // // Returns: @@ -107,10 +108,11 @@ svgedit.units.getTypeMap = function() { // with comma-seperated floats svgedit.units.shortFloat = function(val) { var digits = elementContainer_.getRoundDigits(); - if(!isNaN(val)) { + if (!isNaN(val)) { // Note that + converts to Number return +((+val).toFixed(digits)); - } else if($.isArray(val)) { + } + if ($.isArray(val)) { return svgedit.units.shortFloat(val[0]) + ',' + svgedit.units.shortFloat(val[1]); } return parseFloat(val).toFixed(digits) - 0; @@ -123,45 +125,45 @@ svgedit.units.convertUnit = function(val, unit) { // baseVal.convertToSpecifiedUnits(unitNumMap[unit]); // var val = baseVal.valueInSpecifiedUnits; // baseVal.convertToSpecifiedUnits(1); - return svgedit.unit.shortFloat(val / typeMap_[unit]); + return svgedit.units.shortFloat(val / typeMap_[unit]); }; // Function: svgedit.units.setUnitAttr // Sets an element's attribute based on the unit in its current value. // -// Parameters: +// Parameters: // 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)) { + if (!isNaN(val)) { // New value is a number, so check currently used unit - var old_val = elem.getAttribute(attr); - +// var old_val = elem.getAttribute(attr); + // Enable this for alternate mode -// if(old_val !== null && (isNaN(old_val) || elementContainer_.getBaseUnit() !== 'px')) { +// 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) === '%') { +// if (old_val.substr(-1) === '%') { // var res = getResolution(); // unit = '%'; // val *= 100; -// if(w_attrs.indexOf(attr) >= 0) { +// if (wAttrs.indexOf(attr) >= 0) { // val = val / res.w; -// } else if(h_attrs.indexOf(attr) >= 0) { +// } 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') { +// if (elementContainer_.getBaseUnit() !== 'px') { // unit = elementContainer_.getBaseUnit(); // } else { // unit = old_val.substr(-2); // } // val = val / typeMap_[unit]; // } -// +// // val += unit; // } } @@ -188,13 +190,14 @@ svgedit.units.convertAttrs = function(element) { var elName = element.tagName; var unit = elementContainer_.getBaseUnit(); var attrs = attrsToConvert[elName]; - if(!attrs) return; - var len = attrs.length - for(var i = 0; i < len; i++) { + if (!attrs) return; + + var len = attrs.length; + for (var i = 0; i < len; i++) { var attr = attrs[i]; var cur = element.getAttribute(attr); - if(cur) { - if(!isNaN(cur)) { + if (cur) { + if (!isNaN(cur)) { element.setAttribute(attr, (cur / typeMap_[unit]) + unit); } else { // Convert existing? @@ -212,17 +215,17 @@ svgedit.units.convertAttrs = function(element) { // val - String with the attribute value to convert svgedit.units.convertToNum = function(attr, val) { // Return a number if that's what it already is - if(!isNaN(val)) return val-0; - - if(val.substr(-1) === '%') { + if (!isNaN(val)) return val-0; + + if (val.substr(-1) === '%') { // Deal with percentage, depends on attribute var num = val.substr(0, val.length-1)/100; var width = elementContainer_.getWidth(); var height = elementContainer_.getHeight(); - - if(w_attrs.indexOf(attr) >= 0) { + + if (wAttrs.indexOf(attr) >= 0) { return num * width; - } else if(h_attrs.indexOf(attr) >= 0) { + } else if (hAttrs.indexOf(attr) >= 0) { return num * height; } else { return num * Math.sqrt((width*width) + (height*height))/Math.sqrt(2); @@ -238,22 +241,22 @@ svgedit.units.convertToNum = function(attr, val) { // Function: svgedit.units.isValidUnit // Check if an attribute's value is in a valid format // -// Parameters: +// 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) { var valid = false; - if(unit_attrs.indexOf(attr) >= 0) { + if (unitAttrs.indexOf(attr) >= 0) { // True if it's just a number - if(!isNaN(val)) { + if (!isNaN(val)) { valid = true; } else { // Not a number, check if it has a valid unit val = val.toLowerCase(); $.each(typeMap_, function(unit) { - if(valid) return; + 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") { @@ -273,9 +276,8 @@ svgedit.units.isValidUnit = function(attr, val, selectedElement) { } else { valid = true; } - + return valid; }; - })(); \ No newline at end of file diff --git a/test/units_test.html b/test/units_test.html index 116f20e8..48055211 100644 --- a/test/units_test.html +++ b/test/units_test.html @@ -1,29 +1,29 @@
- - - - - + + + - - -