2010-11-05 17:00:32 +00:00
|
|
|
/**
|
|
|
|
* Package: svgedit.units
|
|
|
|
*
|
2012-09-16 18:53:27 +00:00
|
|
|
* Licensed under the MIT License
|
2010-11-05 17:00:32 +00:00
|
|
|
*
|
|
|
|
* Copyright(c) 2010 Alexis Deveria
|
|
|
|
* Copyright(c) 2010 Jeff Schiller
|
|
|
|
*/
|
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
import {NS} from './svgedit.js';
|
2010-11-05 17:00:32 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
const wAttrs = ['x', 'x1', 'cx', 'rx', 'width'];
|
|
|
|
const hAttrs = ['y', 'y1', 'cy', 'ry', 'height'];
|
|
|
|
const unitAttrs = ['r', 'radius', ...wAttrs, ...hAttrs];
|
2013-02-14 12:18:13 +00:00
|
|
|
// unused
|
2018-05-16 00:53:27 +00:00
|
|
|
/*
|
2018-05-18 03:25:45 +00:00
|
|
|
const unitNumMap = {
|
2018-05-18 04:02:30 +00:00
|
|
|
'%': 2,
|
- Breaking change: Rename config file to `svgedit-config-iife.js` (or for the module version, `svgedit-config-es.js`);
also expect one directory higher; incorporates #207 (@iuyiuy)
- Breaking change: Separate `extIconsPath` from `extPath` (not copying over icons)
- Breaking change: Don't reference `custom.css` in HTML; can instead be referenced in JavaScript through
the config file (provided in `svgedit-config-sample-iife.js`/`svgedit-config-sample-es.js` as `svgedit-custom.css` for
better namespacing); incorporates #207 (@iuyiuy)
- Breaking change: Remove minified jgraduate/spinbtn files (minified within Rollup routine)
- Fix: Zoom when scrolled; incorporates #169 (@AndrolGenhald), adapting for conventions; also allow avoidance when shift key pressed
- Fix: Update Atom feed reference in HTML
- Fixes related to recent commits: Some path and method name fixes needed, function order, missing methods, variable scope declaration, no need for DOMContentLoaded listeners in modules, switch back to non-default export, avoid trimming nullish, deal with mock tests, fix `math.matrixMultiply`, use jquery-svg where needed for array/SVG attributes; add babel-polyfill and defer script to imagelib; other misc. fixes
- Enhancement: Move config-sample.js out of `editor` directory
- Enhancement: For `callback`-style extensions, also provide config object; add following
to that object: buildCanvgCallback, canvg, decode64, encode64, executeAfterLoads, getTypeMap, isChrome, ieIE, NS, text2xml
- Enhancement: Complete ES6 modules work (extensions, locales, tests), along with Babel;
make Node build routine for converting modular source to non-modular,
use `loadStylesheets` for modular stylehsheet defining (but parallel loading);
- Enhancement: Add `stylesheets` config for modular but parallel stylesheet loading with `@default` option for simple inclusion/exclusion of defaults (if not going with default).
- Refactoring: Clean up `svg-editor.html`: consistent indents; avoid extra lbs, avoid long lines
- Refactoring: Avoid embedded API adding inline JavaScript listener
- Refactoring: Move layers and context code to `draw.js`
- Refactoring: Move `pathActions` from `svgcanvas.js` (though preserve aliases to these methods on `canvas`) and `convertPath` from `svgutils.js` to `path.js`
- Refactoring: Move `getStrokedBBox` from `svgcanvas.js` (while keeping an alias) to `svgutils.js` (as `getStrokedBBoxDefaultVisible` to avoid conflict with existing)
- Docs: Remove "dependencies" comments in code except where summarizing role of jQuery or a non-obvious dependency
- Refactoring/Linting: Enfore `no-extra-semi` and `quote-props` rules
- Refactoring: Further avoidance of quotes on properties (as possible)
- Refactoring: Use `class` in place of functions where intended as classes
- Refactoring: Consistency and granularity in extensions imports
- Testing: Update QUnit to 2.6.1 (node_modules) and Sinon to 5.0.8 (and add sinon-test at 2.1.3) and enforce eslint-plugin-qunit linting rules; update custom extensions
- Testing: Add node-static for automating (and accessing out-of-directory contents)
- Testing: Avoid HTML attributes for styling
- Testing: Add npm `test` script
- Testing: Comment out unused jQuery SVG test
- Testing: Add test1 and svgutils_performance_test to all tests page
- Testing: Due apparently to Path having not been a formal class, the test was calling it without `new`; refactored now with sufficient mock data to take into account it is a class
- npm: Update devDeps
- npm: Add html modules and config build to test script
2018-05-22 10:03:16 +00:00
|
|
|
em: 3,
|
|
|
|
ex: 4,
|
|
|
|
px: 5,
|
|
|
|
cm: 6,
|
|
|
|
mm: 7,
|
|
|
|
in: 8,
|
|
|
|
pt: 9,
|
|
|
|
pc: 10
|
2010-11-05 17:00:32 +00:00
|
|
|
};
|
2018-05-16 00:53:27 +00:00
|
|
|
*/
|
2010-11-07 05:20:03 +00:00
|
|
|
// Container of elements.
|
2018-05-18 03:25:45 +00:00
|
|
|
let elementContainer_;
|
2010-11-06 00:45:21 +00:00
|
|
|
|
2010-11-05 17:00:32 +00:00
|
|
|
/**
|
|
|
|
* Stores mapping of unit type to user coordinates.
|
|
|
|
*/
|
2018-05-18 03:25:45 +00:00
|
|
|
let typeMap_ = {};
|
2010-11-07 05:20:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ElementContainer interface
|
|
|
|
*
|
2018-05-18 03:25:45 +00:00
|
|
|
* function getBaseUnit() - Returns a string of the base unit type of the container ('em')
|
|
|
|
* function getElement() - Returns an element in the container given an id
|
|
|
|
* function getHeight() - Returns the container's height
|
|
|
|
* function getWidth() - Returns the container's width
|
|
|
|
* function getRoundDigits() - Returns the number of digits number should be rounded to
|
2010-11-07 05:20:03 +00:00
|
|
|
*/
|
2010-11-05 17:00:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes this module.
|
2010-11-06 00:45:21 +00:00
|
|
|
*
|
2018-05-18 03:25:45 +00:00
|
|
|
* @param elementContainer - An object implementing the ElementContainer interface.
|
2010-11-05 17:00:32 +00:00
|
|
|
*/
|
2018-05-18 03:25:45 +00:00
|
|
|
export const init = function (elementContainer) {
|
2018-05-18 04:02:30 +00:00
|
|
|
elementContainer_ = elementContainer;
|
2010-11-06 00:45:21 +00:00
|
|
|
|
2018-05-18 04:02:30 +00:00
|
|
|
// Get correct em/ex values by creating a temporary SVG.
|
2018-05-18 03:25:45 +00:00
|
|
|
const svg = document.createElementNS(NS.SVG, 'svg');
|
2018-05-18 04:02:30 +00:00
|
|
|
document.body.appendChild(svg);
|
2018-05-18 03:25:45 +00:00
|
|
|
const rect = document.createElementNS(NS.SVG, 'rect');
|
2018-05-18 04:02:30 +00:00
|
|
|
rect.setAttribute('width', '1em');
|
|
|
|
rect.setAttribute('height', '1ex');
|
|
|
|
rect.setAttribute('x', '1in');
|
|
|
|
svg.appendChild(rect);
|
2018-05-18 03:25:45 +00:00
|
|
|
const bb = rect.getBBox();
|
2018-05-18 04:02:30 +00:00
|
|
|
document.body.removeChild(svg);
|
2010-11-05 17:00:32 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
const inch = bb.x;
|
2018-05-18 04:02:30 +00:00
|
|
|
typeMap_ = {
|
- Breaking change: Rename config file to `svgedit-config-iife.js` (or for the module version, `svgedit-config-es.js`);
also expect one directory higher; incorporates #207 (@iuyiuy)
- Breaking change: Separate `extIconsPath` from `extPath` (not copying over icons)
- Breaking change: Don't reference `custom.css` in HTML; can instead be referenced in JavaScript through
the config file (provided in `svgedit-config-sample-iife.js`/`svgedit-config-sample-es.js` as `svgedit-custom.css` for
better namespacing); incorporates #207 (@iuyiuy)
- Breaking change: Remove minified jgraduate/spinbtn files (minified within Rollup routine)
- Fix: Zoom when scrolled; incorporates #169 (@AndrolGenhald), adapting for conventions; also allow avoidance when shift key pressed
- Fix: Update Atom feed reference in HTML
- Fixes related to recent commits: Some path and method name fixes needed, function order, missing methods, variable scope declaration, no need for DOMContentLoaded listeners in modules, switch back to non-default export, avoid trimming nullish, deal with mock tests, fix `math.matrixMultiply`, use jquery-svg where needed for array/SVG attributes; add babel-polyfill and defer script to imagelib; other misc. fixes
- Enhancement: Move config-sample.js out of `editor` directory
- Enhancement: For `callback`-style extensions, also provide config object; add following
to that object: buildCanvgCallback, canvg, decode64, encode64, executeAfterLoads, getTypeMap, isChrome, ieIE, NS, text2xml
- Enhancement: Complete ES6 modules work (extensions, locales, tests), along with Babel;
make Node build routine for converting modular source to non-modular,
use `loadStylesheets` for modular stylehsheet defining (but parallel loading);
- Enhancement: Add `stylesheets` config for modular but parallel stylesheet loading with `@default` option for simple inclusion/exclusion of defaults (if not going with default).
- Refactoring: Clean up `svg-editor.html`: consistent indents; avoid extra lbs, avoid long lines
- Refactoring: Avoid embedded API adding inline JavaScript listener
- Refactoring: Move layers and context code to `draw.js`
- Refactoring: Move `pathActions` from `svgcanvas.js` (though preserve aliases to these methods on `canvas`) and `convertPath` from `svgutils.js` to `path.js`
- Refactoring: Move `getStrokedBBox` from `svgcanvas.js` (while keeping an alias) to `svgutils.js` (as `getStrokedBBoxDefaultVisible` to avoid conflict with existing)
- Docs: Remove "dependencies" comments in code except where summarizing role of jQuery or a non-obvious dependency
- Refactoring/Linting: Enfore `no-extra-semi` and `quote-props` rules
- Refactoring: Further avoidance of quotes on properties (as possible)
- Refactoring: Use `class` in place of functions where intended as classes
- Refactoring: Consistency and granularity in extensions imports
- Testing: Update QUnit to 2.6.1 (node_modules) and Sinon to 5.0.8 (and add sinon-test at 2.1.3) and enforce eslint-plugin-qunit linting rules; update custom extensions
- Testing: Add node-static for automating (and accessing out-of-directory contents)
- Testing: Avoid HTML attributes for styling
- Testing: Add npm `test` script
- Testing: Comment out unused jQuery SVG test
- Testing: Add test1 and svgutils_performance_test to all tests page
- Testing: Due apparently to Path having not been a formal class, the test was calling it without `new`; refactored now with sufficient mock data to take into account it is a class
- npm: Update devDeps
- npm: Add html modules and config build to test script
2018-05-22 10:03:16 +00:00
|
|
|
em: bb.width,
|
|
|
|
ex: bb.height,
|
|
|
|
in: inch,
|
|
|
|
cm: inch / 2.54,
|
|
|
|
mm: inch / 25.4,
|
|
|
|
pt: inch / 72,
|
|
|
|
pc: inch / 6,
|
|
|
|
px: 1,
|
2018-05-18 04:02:30 +00:00
|
|
|
'%': 0
|
|
|
|
};
|
2010-11-06 00:45:21 +00:00
|
|
|
};
|
|
|
|
|
2018-05-29 12:31:24 +00:00
|
|
|
/**
|
|
|
|
* Group: Unit conversion functions
|
|
|
|
*/
|
2010-11-07 16:46:57 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
/**
|
|
|
|
* @returns The unit object with values for each unit
|
|
|
|
*/
|
|
|
|
export const getTypeMap = function () {
|
2018-05-18 04:02:30 +00:00
|
|
|
return typeMap_;
|
2010-11-06 00:45:21 +00:00
|
|
|
};
|
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
/**
|
|
|
|
* Rounds a given value to a float with number of digits defined in save_options
|
|
|
|
*
|
|
|
|
* @param val - The value as a String, Number or Array of two numbers to be rounded
|
|
|
|
*
|
|
|
|
* @returns
|
|
|
|
* If a string/number was given, returns a Float. If an array, return a string
|
|
|
|
* with comma-separated floats
|
|
|
|
*/
|
|
|
|
export const shortFloat = function (val) {
|
|
|
|
const digits = elementContainer_.getRoundDigits();
|
2018-05-18 04:02:30 +00:00
|
|
|
if (!isNaN(val)) {
|
|
|
|
// Note that + converts to Number
|
|
|
|
return +((+val).toFixed(digits));
|
|
|
|
}
|
2018-05-18 03:25:45 +00:00
|
|
|
if (Array.isArray(val)) {
|
|
|
|
return shortFloat(val[0]) + ',' + shortFloat(val[1]);
|
2018-05-18 04:02:30 +00:00
|
|
|
}
|
|
|
|
return parseFloat(val).toFixed(digits) - 0;
|
2011-02-02 17:28:43 +00:00
|
|
|
};
|
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
/**
|
|
|
|
* Converts the number to given unit or baseUnit
|
|
|
|
* @returns {number}
|
|
|
|
*/
|
|
|
|
export const convertUnit = function (val, unit) {
|
2018-05-18 04:02:30 +00:00
|
|
|
unit = unit || elementContainer_.getBaseUnit();
|
|
|
|
// baseVal.convertToSpecifiedUnits(unitNumMap[unit]);
|
2018-05-18 03:25:45 +00:00
|
|
|
// const val = baseVal.valueInSpecifiedUnits;
|
2018-05-18 04:02:30 +00:00
|
|
|
// baseVal.convertToSpecifiedUnits(1);
|
2018-05-18 03:25:45 +00:00
|
|
|
return shortFloat(val / typeMap_[unit]);
|
2010-11-06 00:45:21 +00:00
|
|
|
};
|
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
/**
|
|
|
|
* Sets an element's attribute based on the unit in its current value.
|
|
|
|
*
|
|
|
|
* @param elem - DOM element to be changed
|
|
|
|
* @param attr - String with the name of the attribute associated with the value
|
|
|
|
* @param val - String with the attribute value to convert
|
|
|
|
*/
|
|
|
|
export const setUnitAttr = function (elem, attr, val) {
|
2018-05-18 06:41:43 +00:00
|
|
|
// if (!isNaN(val)) {
|
2018-05-18 04:02:30 +00:00
|
|
|
// New value is a number, so check currently used unit
|
2018-05-18 03:25:45 +00:00
|
|
|
// const oldVal = elem.getAttribute(attr);
|
2013-02-14 12:18:13 +00:00
|
|
|
|
2018-05-18 04:02:30 +00:00
|
|
|
// Enable this for alternate mode
|
2018-05-18 03:25:45 +00:00
|
|
|
// if (oldVal !== null && (isNaN(oldVal) || elementContainer_.getBaseUnit() !== 'px')) {
|
2018-05-18 06:41:43 +00:00
|
|
|
// // Old value was a number, so get unit, then convert
|
2018-05-18 03:25:45 +00:00
|
|
|
// let unit;
|
|
|
|
// if (oldVal.substr(-1) === '%') {
|
|
|
|
// const res = getResolution();
|
2018-05-18 06:41:43 +00:00
|
|
|
// unit = '%';
|
|
|
|
// val *= 100;
|
2018-05-18 03:25:45 +00:00
|
|
|
// if (wAttrs.includes(attr)) {
|
2018-05-18 06:41:43 +00:00
|
|
|
// val = val / res.w;
|
2018-05-18 03:25:45 +00:00
|
|
|
// } else if (hAttrs.includes(attr)) {
|
2018-05-18 06:41:43 +00:00
|
|
|
// 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 {
|
2018-05-18 03:25:45 +00:00
|
|
|
// unit = oldVal.substr(-2);
|
2018-05-18 06:41:43 +00:00
|
|
|
// }
|
|
|
|
// val = val / typeMap_[unit];
|
|
|
|
// }
|
2018-05-18 04:02:30 +00:00
|
|
|
//
|
|
|
|
// val += unit;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
elem.setAttribute(attr, val);
|
2010-11-05 17:00:32 +00:00
|
|
|
};
|
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
const attrsToConvert = {
|
- Breaking change: Rename config file to `svgedit-config-iife.js` (or for the module version, `svgedit-config-es.js`);
also expect one directory higher; incorporates #207 (@iuyiuy)
- Breaking change: Separate `extIconsPath` from `extPath` (not copying over icons)
- Breaking change: Don't reference `custom.css` in HTML; can instead be referenced in JavaScript through
the config file (provided in `svgedit-config-sample-iife.js`/`svgedit-config-sample-es.js` as `svgedit-custom.css` for
better namespacing); incorporates #207 (@iuyiuy)
- Breaking change: Remove minified jgraduate/spinbtn files (minified within Rollup routine)
- Fix: Zoom when scrolled; incorporates #169 (@AndrolGenhald), adapting for conventions; also allow avoidance when shift key pressed
- Fix: Update Atom feed reference in HTML
- Fixes related to recent commits: Some path and method name fixes needed, function order, missing methods, variable scope declaration, no need for DOMContentLoaded listeners in modules, switch back to non-default export, avoid trimming nullish, deal with mock tests, fix `math.matrixMultiply`, use jquery-svg where needed for array/SVG attributes; add babel-polyfill and defer script to imagelib; other misc. fixes
- Enhancement: Move config-sample.js out of `editor` directory
- Enhancement: For `callback`-style extensions, also provide config object; add following
to that object: buildCanvgCallback, canvg, decode64, encode64, executeAfterLoads, getTypeMap, isChrome, ieIE, NS, text2xml
- Enhancement: Complete ES6 modules work (extensions, locales, tests), along with Babel;
make Node build routine for converting modular source to non-modular,
use `loadStylesheets` for modular stylehsheet defining (but parallel loading);
- Enhancement: Add `stylesheets` config for modular but parallel stylesheet loading with `@default` option for simple inclusion/exclusion of defaults (if not going with default).
- Refactoring: Clean up `svg-editor.html`: consistent indents; avoid extra lbs, avoid long lines
- Refactoring: Avoid embedded API adding inline JavaScript listener
- Refactoring: Move layers and context code to `draw.js`
- Refactoring: Move `pathActions` from `svgcanvas.js` (though preserve aliases to these methods on `canvas`) and `convertPath` from `svgutils.js` to `path.js`
- Refactoring: Move `getStrokedBBox` from `svgcanvas.js` (while keeping an alias) to `svgutils.js` (as `getStrokedBBoxDefaultVisible` to avoid conflict with existing)
- Docs: Remove "dependencies" comments in code except where summarizing role of jQuery or a non-obvious dependency
- Refactoring/Linting: Enfore `no-extra-semi` and `quote-props` rules
- Refactoring: Further avoidance of quotes on properties (as possible)
- Refactoring: Use `class` in place of functions where intended as classes
- Refactoring: Consistency and granularity in extensions imports
- Testing: Update QUnit to 2.6.1 (node_modules) and Sinon to 5.0.8 (and add sinon-test at 2.1.3) and enforce eslint-plugin-qunit linting rules; update custom extensions
- Testing: Add node-static for automating (and accessing out-of-directory contents)
- Testing: Avoid HTML attributes for styling
- Testing: Add npm `test` script
- Testing: Comment out unused jQuery SVG test
- Testing: Add test1 and svgutils_performance_test to all tests page
- Testing: Due apparently to Path having not been a formal class, the test was calling it without `new`; refactored now with sufficient mock data to take into account it is a class
- npm: Update devDeps
- npm: Add html modules and config build to test script
2018-05-22 10:03:16 +00:00
|
|
|
line: ['x1', 'x2', 'y1', 'y2'],
|
|
|
|
circle: ['cx', 'cy', 'r'],
|
|
|
|
ellipse: ['cx', 'cy', 'rx', 'ry'],
|
|
|
|
foreignObject: ['x', 'y', 'width', 'height'],
|
|
|
|
rect: ['x', 'y', 'width', 'height'],
|
|
|
|
image: ['x', 'y', 'width', 'height'],
|
|
|
|
use: ['x', 'y', 'width', 'height'],
|
|
|
|
text: ['x', 'y']
|
2010-11-06 14:37:00 +00:00
|
|
|
};
|
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
/**
|
|
|
|
* Converts all applicable attributes to the configured baseUnit
|
|
|
|
* @param element - A DOM element whose attributes should be converted
|
|
|
|
*/
|
|
|
|
export const convertAttrs = function (element) {
|
|
|
|
const elName = element.tagName;
|
|
|
|
const unit = elementContainer_.getBaseUnit();
|
|
|
|
const attrs = attrsToConvert[elName];
|
2018-05-18 04:02:30 +00:00
|
|
|
if (!attrs) { return; }
|
2013-02-14 12:18:13 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
const len = attrs.length;
|
|
|
|
for (let i = 0; i < len; i++) {
|
|
|
|
const attr = attrs[i];
|
|
|
|
const cur = element.getAttribute(attr);
|
2018-05-18 04:02:30 +00:00
|
|
|
if (cur) {
|
|
|
|
if (!isNaN(cur)) {
|
|
|
|
element.setAttribute(attr, (cur / typeMap_[unit]) + unit);
|
|
|
|
}
|
|
|
|
// else {
|
|
|
|
// Convert existing?
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
}
|
2010-11-06 14:37:00 +00:00
|
|
|
};
|
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
/**
|
|
|
|
* Converts given values to numbers. Attributes must be supplied in
|
|
|
|
* case a percentage is given
|
|
|
|
*
|
|
|
|
* @param attr - String with the name of the attribute associated with the value
|
|
|
|
* @param val - String with the attribute value to convert
|
|
|
|
*/
|
|
|
|
export const convertToNum = function (attr, val) {
|
2018-05-18 04:02:30 +00:00
|
|
|
// Return a number if that's what it already is
|
|
|
|
if (!isNaN(val)) { return val - 0; }
|
|
|
|
if (val.substr(-1) === '%') {
|
|
|
|
// Deal with percentage, depends on attribute
|
2018-05-18 03:25:45 +00:00
|
|
|
const num = val.substr(0, val.length - 1) / 100;
|
|
|
|
const width = elementContainer_.getWidth();
|
|
|
|
const height = elementContainer_.getHeight();
|
2013-02-14 12:18:13 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
if (wAttrs.includes(attr)) {
|
2018-05-18 04:02:30 +00:00
|
|
|
return num * width;
|
|
|
|
}
|
2018-05-18 03:25:45 +00:00
|
|
|
if (hAttrs.includes(attr)) {
|
2018-05-18 04:02:30 +00:00
|
|
|
return num * height;
|
|
|
|
}
|
|
|
|
return num * Math.sqrt((width * width) + (height * height)) / Math.sqrt(2);
|
|
|
|
}
|
2018-05-18 03:25:45 +00:00
|
|
|
const unit = val.substr(-2);
|
|
|
|
const num = val.substr(0, val.length - 2);
|
2018-05-18 04:02:30 +00:00
|
|
|
// Note that this multiplication turns the string into a number
|
|
|
|
return num * typeMap_[unit];
|
2010-11-07 05:20:03 +00:00
|
|
|
};
|
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
/**
|
|
|
|
* Check if an attribute's value is in a valid format
|
|
|
|
* @param attr - String with the name of the attribute associated with the value
|
|
|
|
* @param val - String with the attribute value to check
|
|
|
|
*/
|
|
|
|
export const isValidUnit = function (attr, val, selectedElement) {
|
|
|
|
let valid = false;
|
|
|
|
if (unitAttrs.includes(attr)) {
|
2018-05-18 04:02:30 +00:00
|
|
|
// True if it's just a number
|
|
|
|
if (!isNaN(val)) {
|
|
|
|
valid = true;
|
|
|
|
} else {
|
|
|
|
// Not a number, check if it has a valid unit
|
|
|
|
val = val.toLowerCase();
|
2018-05-18 03:25:45 +00:00
|
|
|
Object.keys(typeMap_).forEach((unit) => {
|
2018-05-18 04:02:30 +00:00
|
|
|
if (valid) { return; }
|
2018-05-18 03:25:45 +00:00
|
|
|
const re = new RegExp('^-?[\\d\\.]+' + unit + '$');
|
2018-05-18 04:02:30 +00:00
|
|
|
if (re.test(val)) { valid = true; }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} 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.
|
2010-11-07 05:20:03 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
let result = false;
|
2018-05-18 04:02:30 +00:00
|
|
|
// because getElem() can throw an exception in the case of an invalid id
|
2018-05-18 04:40:50 +00:00
|
|
|
// (according to https://www.w3.org/TR/xml-id/ IDs must be a NCName)
|
2018-05-18 04:02:30 +00:00
|
|
|
// we wrap it in an exception and only return true if the ID was valid and
|
|
|
|
// not already present
|
|
|
|
try {
|
2018-05-18 03:25:45 +00:00
|
|
|
const elem = elementContainer_.getElement(val);
|
2018-05-18 04:02:30 +00:00
|
|
|
result = (elem == null || elem === selectedElement);
|
|
|
|
} catch (e) {}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
valid = true;
|
2013-02-14 12:18:13 +00:00
|
|
|
|
2018-05-18 04:02:30 +00:00
|
|
|
return valid;
|
2010-11-07 05:20:03 +00:00
|
|
|
};
|