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 /src | |
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)
Diffstat (limited to 'src')
-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 |
5 files changed, 64 insertions, 26 deletions
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 } |