diff options
author | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2018-11-09 11:17:18 +0100 |
---|---|---|
committer | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2018-11-12 12:59:25 +0100 |
commit | 7a5ae8b897548eef9efbf900b42936f24f911cea (patch) | |
tree | 9e1df6d92ba7e9266669f77e9ba374c61b975776 | |
parent | d3f8d83551799db061a558537bc89dcbfd522c21 (diff) | |
download | svg.js-7a5ae8b897548eef9efbf900b42936f24f911cea.tar.gz svg.js-7a5ae8b897548eef9efbf900b42936f24f911cea.zip |
adds `List` which does bring back `SVG.Set` in an elegant way (#645)
-rw-r--r-- | dist/svg.js | 155 | ||||
-rw-r--r-- | src/main.js | 20 | ||||
-rw-r--r-- | src/types/List.js | 38 | ||||
-rw-r--r-- | src/types/set.js | 18 | ||||
-rw-r--r-- | src/utils/adopter.js | 4 | ||||
-rw-r--r-- | src/utils/methods.js | 10 |
6 files changed, 168 insertions, 77 deletions
diff --git a/dist/svg.js b/dist/svg.js index 8cd3575..670b1fd 100644 --- a/dist/svg.js +++ b/dist/svg.js @@ -6,7 +6,7 @@ * @copyright Wout Fierens <wout@mick-wout.com> * @license MIT * -* BUILT: Mon Nov 12 2018 09:31:46 GMT+0100 (GMT+01:00) +* BUILT: Mon Nov 12 2018 12:58:46 GMT+0100 (GMT+01:00) */; var SVG = (function () { 'use strict'; @@ -216,6 +216,64 @@ var SVG = (function () { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } + var methods = {}; + var names = []; + function registerMethods(name, m) { + if (Array.isArray(name)) { + var _iteratorNormalCompletion = true; + var _didIteratorError = false; + var _iteratorError = undefined; + + try { + for (var _iterator = name[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { + var _name = _step.value; + registerMethods(_name, m); + } + } catch (err) { + _didIteratorError = true; + _iteratorError = err; + } finally { + try { + if (!_iteratorNormalCompletion && _iterator.return != null) { + _iterator.return(); + } + } finally { + if (_didIteratorError) { + throw _iteratorError; + } + } + } + + return; + } + + if (_typeof(name) === 'object') { + var _arr = Object.entries(name); + + for (var _i = 0; _i < _arr.length; _i++) { + var _arr$_i = _slicedToArray(_arr[_i], 2), + _name2 = _arr$_i[0], + _m = _arr$_i[1]; + + registerMethods(_name2, _m); + } + + return; + } + + addMethodNames(Object.keys(m)); + methods[name] = Object.assign(methods[name] || {}, m); + } + function getMethodsFor(name) { + return methods[name] || {}; + } + function getMethodNames() { + return _toConsumableArray(new Set(names)); + } + function addMethodNames(_names) { + names.push.apply(names, _toConsumableArray(_names)); + } + // Map function function map(array, block) { var i; @@ -398,6 +456,7 @@ var SVG = (function () { var asRoot = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; elements[name] = element; if (asRoot) elements[root] = element; + addMethodNames(Object.keys(element.prototype)); return element; } function getClass(name) { @@ -462,56 +521,6 @@ var SVG = (function () { }; } - var methods = {}; - function registerMethods(name, m) { - if (Array.isArray(name)) { - var _iteratorNormalCompletion = true; - var _didIteratorError = false; - var _iteratorError = undefined; - - try { - for (var _iterator = name[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { - var _name = _step.value; - registerMethods(_name, m); - } - } catch (err) { - _didIteratorError = true; - _iteratorError = err; - } finally { - try { - if (!_iteratorNormalCompletion && _iterator.return != null) { - _iterator.return(); - } - } finally { - if (_didIteratorError) { - throw _iteratorError; - } - } - } - - return; - } - - if (_typeof(name) === 'object') { - var _arr = Object.entries(name); - - for (var _i = 0; _i < _arr.length; _i++) { - var _arr$_i = _slicedToArray(_arr[_i], 2), - _name2 = _arr$_i[0], - _m = _arr$_i[1]; - - registerMethods(_name2, _m); - } - - return; - } - - methods[name] = Object.assign(methods[name] || {}, m); - } - function getMethodsFor(name) { - return methods[name] || {}; - } - function siblings() { return this.parent().children(); } // Get the curent position siblings @@ -5369,6 +5378,48 @@ var SVG = (function () { transform: transform }); + var List = subClassArray('List', Array, function (arr) { + this.length = 0; + this.push.apply(this, _toConsumableArray(arr)); + }); + extend(List, { + each: function each(cbOrName) { + for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { + args[_key - 1] = arguments[_key]; + } + + if (typeof cbOrName === 'function') { + this.forEach(function (el) { + cbOrName.call(el, el); + }); + } else { + this.forEach(function (el) { + el[cbOrName].apply(el, args); + }); + } + + return this; + }, + toArray: function toArray() { + return Array.prototype.concat.apply([], this); + } + }); + + List.extend = function (methods) { + methods = methods.reduce(function (obj, name) { + obj[name] = function () { + for (var _len2 = arguments.length, attrs = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { + attrs[_key2] = arguments[_key2]; + } + + return this.each.apply(this, [name].concat(attrs)); + }; + + return obj; + }, {}); + extend(List, methods); + }; + var Shape = /*#__PURE__*/ function (_Element) { @@ -6968,6 +7019,7 @@ var SVG = (function () { extend(Shape, getMethodsFor('Shape')); // extend(Element, getConstructor('Memory')) extend(Container, getMethodsFor('Container')); + List.extend(getMethodNames()); registerMorphableType([SVGNumber, Color, Box, Matrix, SVGArray, PointArray, PathArray]); makeMorphable(); @@ -7000,6 +7052,7 @@ var SVG = (function () { PathArray: PathArray, Point: Point, PointArray: PointArray, + List: List, Bare: Bare, Circle: Circle, ClipPath: ClipPath, diff --git a/src/main.js b/src/main.js index 1961604..a7deaaa 100644 --- a/src/main.js +++ b/src/main.js @@ -7,15 +7,9 @@ import './modules/optional/memory.js' import './modules/optional/sugar.js' import './modules/optional/transform.js' -import Morphable, { - NonMorphable, - ObjectBag, - TransformBag, - makeMorphable, - registerMorphableType -} from './types/Morphable.js' +import List from './types/List.js' import { extend } from './utils/adopter.js' -import { getMethodsFor } from './utils/methods.js' +import { getMethodNames, getMethodsFor } from './utils/methods.js' import Box from './types/Box.js' import Circle from './elements/Circle.js' import Color from './types/Color.js' @@ -31,6 +25,13 @@ import Image from './elements/Image.js' import Line from './elements/Line.js' import Marker from './elements/Marker.js' import Matrix from './types/Matrix.js' +import Morphable, { + NonMorphable, + ObjectBag, + TransformBag, + makeMorphable, + registerMorphableType +} from './types/Morphable.js' import Path from './elements/Path.js' import PathArray from './types/PathArray.js' import Pattern from './elements/Pattern.js' @@ -80,6 +81,7 @@ export { default as SVGNumber } from './types/SVGNumber.js' export { default as PathArray } from './types/PathArray.js' export { default as Point } from './types/Point.js' export { default as PointArray } from './types/PointArray.js' +export { default as List } from './types/List.js' /* Elements */ export { default as Bare } from './elements/Bare.js' @@ -152,6 +154,8 @@ extend(Shape, getMethodsFor('Shape')) // extend(Element, getConstructor('Memory')) extend(Container, getMethodsFor('Container')) +List.extend(getMethodNames()) + registerMorphableType([ SVGNumber, Color, diff --git a/src/types/List.js b/src/types/List.js new file mode 100644 index 0000000..f3c0e1e --- /dev/null +++ b/src/types/List.js @@ -0,0 +1,38 @@ +import { extend } from '../utils/adopter.js' +import { subClassArray } from './ArrayPolyfill.js' + +const List = subClassArray('List', Array, function (arr) { + this.length = 0 + this.push(...arr) +}) + +export default List + +extend(List, { + each (cbOrName, ...args) { + if (typeof cbOrName === 'function') { + this.forEach((el) => { cbOrName.call(el, el) }) + } else { + this.forEach((el) => { + el[cbOrName](...args) + }) + } + + return this + }, + + toArray () { + return Array.prototype.concat.apply([], this) + } +}) + +List.extend = function (methods) { + methods = methods.reduce((obj, name) => { + obj[name] = function (...attrs) { + return this.each(name, ...attrs) + } + return obj + }, {}) + + extend(List, methods) +} diff --git a/src/types/set.js b/src/types/set.js deleted file mode 100644 index c755c2c..0000000 --- a/src/types/set.js +++ /dev/null @@ -1,18 +0,0 @@ -/* eslint no-unused-vars: "off" */ -class SVGSet extends Set { - // constructor (arr) { - // super(arr) - // } - - each (cbOrName, ...args) { - if (typeof cbOrName === 'function') { - this.forEach((el) => { cbOrName.call(el, el) }) - } else { - this.forEach((el) => { - el[cbOrName](...args) - }) - } - - return this - } -} diff --git a/src/utils/adopter.js b/src/utils/adopter.js index 5de4038..80bfd8a 100644 --- a/src/utils/adopter.js +++ b/src/utils/adopter.js @@ -1,3 +1,4 @@ +import { addMethodNames } from './methods.js' import { capitalize } from './utils.js' import { ns } from '../modules/core/namespaces.js' import { globals } from '../utils/window.js' @@ -73,6 +74,9 @@ export function adopt (node) { export function register (element, name = element.name, asRoot = false) { elements[name] = element if (asRoot) elements[root] = element + + addMethodNames(Object.keys(element.prototype)) + return element } diff --git a/src/utils/methods.js b/src/utils/methods.js index 2373445..70f9329 100644 --- a/src/utils/methods.js +++ b/src/utils/methods.js @@ -1,5 +1,6 @@ const methods = {} const constructors = {} +const names = [] export function registerMethods (name, m) { if (Array.isArray(name)) { @@ -16,6 +17,7 @@ export function registerMethods (name, m) { return } + addMethodNames(Object.keys(m)) methods[name] = Object.assign(methods[name] || {}, m) } @@ -23,6 +25,14 @@ export function getMethodsFor (name) { return methods[name] || {} } +export function getMethodNames () { + return [...new Set(names)] +} + +export function addMethodNames (_names) { + names.push(..._names) +} + export function registerConstructor (name, setup) { constructors[name] = setup } |