diff options
author | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2018-11-08 16:06:39 +0100 |
---|---|---|
committer | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2018-11-08 16:06:39 +0100 |
commit | 8c81fb7c2e6e9842570d27a84b1a1c600c82c16b (patch) | |
tree | b5de85d617f5761db7cdfdce70d8a0f7cc059aba | |
parent | f7f6c4b801172ce119d4ea9a650c543670474784 (diff) | |
download | svg.js-8c81fb7c2e6e9842570d27a84b1a1c600c82c16b.tar.gz svg.js-8c81fb7c2e6e9842570d27a84b1a1c600c82c16b.zip |
added possibility to pass attribues into a constructor like: `new SVG.Rect({width:100})`
32 files changed, 171 insertions, 97 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 250b7f4..c88389e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ The document follows the conventions described in [“Keep a CHANGELOG”](http: - added `Dom` which is a baseclass to get dom abilities - added `round()` which lets you round attribues from a node - added `ax(), ay(), amove()` to change texts x and y values directly (#787) +- added possibility to pass attribues into a constructor like: `new SVG.Rect({width:100})` ### Removed - removed `SVG.Array.split()` function diff --git a/dist/svg.js b/dist/svg.js index bdf49a1..91c140b 100644 --- a/dist/svg.js +++ b/dist/svg.js @@ -6,7 +6,7 @@ * @copyright Wout Fierens <wout@mick-wout.com> * @license MIT * -* BUILT: Thu Nov 08 2018 14:12:10 GMT+0100 (GMT+01:00) +* BUILT: Thu Nov 08 2018 16:05:07 GMT+0100 (GMT+01:00) */; var SVG = (function () { 'use strict'; @@ -354,7 +354,7 @@ var SVG = (function () { return element; } function nodeOrNew(name, node) { - return node || makeNode(name); + return node instanceof window.Node ? node : makeNode(name); } // Adopt existing svg elements function adopt(node) { @@ -412,16 +412,44 @@ var SVG = (function () { return adopt(node); } // Method for extending objects - function extend(modules, methods) { + function extend(modules, methods, attrCheck) { var key, i; modules = Array.isArray(modules) ? modules : [modules]; for (i = modules.length - 1; i >= 0; i--) { for (key in methods) { - modules[i].prototype[key] = methods[key]; + var method = methods[key]; + + if (attrCheck) { + method = wrapWithAttrCheck(methods[key]); + } + + modules[i].prototype[key] = method; } } } + function extendWithAttrCheck() { + for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { + args[_key] = arguments[_key]; + } + + extend.apply(void 0, args.concat([true])); + } + function wrapWithAttrCheck(fn) { + return function () { + for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { + args[_key2] = arguments[_key2]; + } + + var o = args[args.length - 1]; + + if (o && !o.prototype && !(o instanceof Array) && _typeof(o) === 'object') { + return fn.apply(this, args.slice(0, -1)).attr(o); + } else { + return fn.apply(this, args); + } + }; + } var methods = {}; function registerMethods(name, m) { @@ -1422,7 +1450,7 @@ var SVG = (function () { function (_EventTarget) { _inherits(Dom, _EventTarget); - function Dom(node) { + function Dom(node, attrs) { var _this2; _classCallCheck(this, Dom); @@ -1430,6 +1458,11 @@ var SVG = (function () { _this2 = _possibleConstructorReturn(this, _getPrototypeOf(Dom).call(this, node)); _this2.node = node; _this2.type = node.nodeName; + + if (attrs && node !== attrs) { + _this2.attr(attrs); + } + return _this2; } // Add given element at a position @@ -1734,12 +1767,12 @@ var SVG = (function () { function (_Dom) { _inherits(Element, _Dom); - function Element(node) { + function Element(node, attrs) { var _this; _classCallCheck(this, Element); - _this = _possibleConstructorReturn(this, _getPrototypeOf(Element).call(this, node)); // initialize data object + _this = _possibleConstructorReturn(this, _getPrototypeOf(Element).call(this, node, attrs)); // initialize data object _this.dom = {}; // create circular reference @@ -1927,7 +1960,7 @@ var SVG = (function () { function Defs(node) { _classCallCheck(this, Defs); - return _possibleConstructorReturn(this, _getPrototypeOf(Defs).call(this, nodeOrNew('defs', node), Defs)); + return _possibleConstructorReturn(this, _getPrototypeOf(Defs).call(this, nodeOrNew('defs', node), node)); } _createClass(Defs, [{ @@ -1956,7 +1989,7 @@ var SVG = (function () { _classCallCheck(this, Doc); - _this = _possibleConstructorReturn(this, _getPrototypeOf(Doc).call(this, nodeOrNew('svg', node), Doc)); + _this = _possibleConstructorReturn(this, _getPrototypeOf(Doc).call(this, nodeOrNew('svg', node), node)); _this.namespace(); @@ -2051,32 +2084,37 @@ var SVG = (function () { /*#__PURE__*/ function () { // Initialize - function Point(x, y, base) { + function Point() { _classCallCheck(this, Point); - var source; - base = base || { - x: 0, - y: 0 // ensure source as object + this.init.apply(this, arguments); + } - }; - source = Array.isArray(x) ? { - x: x[0], - y: x[1] - } : _typeof(x) === 'object' ? { - x: x.x, - y: x.y - } : { - x: x, - y: y // merge source + _createClass(Point, [{ + key: "init", + value: function init(x, y) { + var source; + var base = { + x: 0, + y: 0 // ensure source as object - }; - this.x = source.x == null ? base.x : source.x; - this.y = source.y == null ? base.y : source.y; - } // Clone point + }; + source = Array.isArray(x) ? { + x: x[0], + y: x[1] + } : _typeof(x) === 'object' ? { + x: x.x, + y: x.y + } : { + x: x, + y: y // merge source + }; + this.x = source.x == null ? base.x : source.x; + this.y = source.y == null ? base.y : source.y; + } // Clone point - _createClass(Point, [{ + }, { key: "clone", value: function clone() { return new Point(this); @@ -2102,6 +2140,11 @@ var SVG = (function () { return new Point(x, y); } + }, { + key: "toArray", + value: function toArray() { + return [this.x, this.y]; + } }]); return Point; @@ -5322,7 +5365,7 @@ var SVG = (function () { function Circle(node) { _classCallCheck(this, Circle); - return _possibleConstructorReturn(this, _getPrototypeOf(Circle).call(this, nodeOrNew('circle', node), Circle)); + return _possibleConstructorReturn(this, _getPrototypeOf(Circle).call(this, nodeOrNew('circle', node), node)); } _createClass(Circle, [{ @@ -5377,7 +5420,7 @@ var SVG = (function () { function Ellipse(node) { _classCallCheck(this, Ellipse); - return _possibleConstructorReturn(this, _getPrototypeOf(Ellipse).call(this, nodeOrNew('ellipse', node), Ellipse)); + return _possibleConstructorReturn(this, _getPrototypeOf(Ellipse).call(this, nodeOrNew('ellipse', node), node)); } _createClass(Ellipse, [{ @@ -5407,7 +5450,7 @@ var SVG = (function () { function Stop(node) { _classCallCheck(this, Stop); - return _possibleConstructorReturn(this, _getPrototypeOf(Stop).call(this, nodeOrNew('stop', node), Stop)); + return _possibleConstructorReturn(this, _getPrototypeOf(Stop).call(this, nodeOrNew('stop', node), node)); } // add color stops @@ -5452,10 +5495,10 @@ var SVG = (function () { function (_Container) { _inherits(Gradient, _Container); - function Gradient(type) { + function Gradient(type, attrs) { _classCallCheck(this, Gradient); - return _possibleConstructorReturn(this, _getPrototypeOf(Gradient).call(this, nodeOrNew(type + 'Gradient', typeof type === 'string' ? null : type), Gradient)); + return _possibleConstructorReturn(this, _getPrototypeOf(Gradient).call(this, nodeOrNew(type + 'Gradient', typeof type === 'string' ? null : type), attrs)); } // Add a color stop @@ -5536,7 +5579,7 @@ var SVG = (function () { function Pattern(node) { _classCallCheck(this, Pattern); - return _possibleConstructorReturn(this, _getPrototypeOf(Pattern).call(this, nodeOrNew('pattern', node), Pattern)); + return _possibleConstructorReturn(this, _getPrototypeOf(Pattern).call(this, nodeOrNew('pattern', node), node)); } // Return the fill id @@ -5614,7 +5657,7 @@ var SVG = (function () { function Image(node) { _classCallCheck(this, Image); - return _possibleConstructorReturn(this, _getPrototypeOf(Image).call(this, nodeOrNew('image', node), Image)); + return _possibleConstructorReturn(this, _getPrototypeOf(Image).call(this, nodeOrNew('image', node), node)); } // (re)load image @@ -5824,7 +5867,7 @@ var SVG = (function () { function Line(node) { _classCallCheck(this, Line); - return _possibleConstructorReturn(this, _getPrototypeOf(Line).call(this, nodeOrNew('line', node), Line)); + return _possibleConstructorReturn(this, _getPrototypeOf(Line).call(this, nodeOrNew('line', node), node)); } // Get array @@ -5895,7 +5938,7 @@ var SVG = (function () { function Marker(node) { _classCallCheck(this, Marker); - return _possibleConstructorReturn(this, _getPrototypeOf(Marker).call(this, nodeOrNew('marker', node), Marker)); + return _possibleConstructorReturn(this, _getPrototypeOf(Marker).call(this, nodeOrNew('marker', node), node)); } // Set width of element @@ -5977,7 +6020,7 @@ var SVG = (function () { function Path(node) { _classCallCheck(this, Path); - return _possibleConstructorReturn(this, _getPrototypeOf(Path).call(this, nodeOrNew('path', node), Path)); + return _possibleConstructorReturn(this, _getPrototypeOf(Path).call(this, nodeOrNew('path', node), node)); } // Get array @@ -6097,7 +6140,7 @@ var SVG = (function () { function Polygon(node) { _classCallCheck(this, Polygon); - return _possibleConstructorReturn(this, _getPrototypeOf(Polygon).call(this, nodeOrNew('polygon', node), Polygon)); + return _possibleConstructorReturn(this, _getPrototypeOf(Polygon).call(this, nodeOrNew('polygon', node), node)); } return Polygon; @@ -6124,7 +6167,7 @@ var SVG = (function () { function Polyline(node) { _classCallCheck(this, Polyline); - return _possibleConstructorReturn(this, _getPrototypeOf(Polyline).call(this, nodeOrNew('polyline', node), Polyline)); + return _possibleConstructorReturn(this, _getPrototypeOf(Polyline).call(this, nodeOrNew('polyline', node), node)); } return Polyline; @@ -6151,7 +6194,7 @@ var SVG = (function () { function Rect(node) { _classCallCheck(this, Rect); - return _possibleConstructorReturn(this, _getPrototypeOf(Rect).call(this, nodeOrNew('rect', node), Rect)); + return _possibleConstructorReturn(this, _getPrototypeOf(Rect).call(this, nodeOrNew('rect', node), node)); } return Rect; @@ -6202,7 +6245,7 @@ var SVG = (function () { _classCallCheck(this, Text); - _this = _possibleConstructorReturn(this, _getPrototypeOf(Text).call(this, nodeOrNew('text', node), Text)); + _this = _possibleConstructorReturn(this, _getPrototypeOf(Text).call(this, nodeOrNew('text', node), node)); _this.dom.leading = new SVGNumber(1.3); // store leading value for rebuilding _this._rebuild = true; // enable automatic updating of dy values @@ -6388,7 +6431,7 @@ var SVG = (function () { function Tspan(node) { _classCallCheck(this, Tspan); - return _possibleConstructorReturn(this, _getPrototypeOf(Tspan).call(this, nodeOrNew('tspan', node), Tspan)); + return _possibleConstructorReturn(this, _getPrototypeOf(Tspan).call(this, nodeOrNew('tspan', node), node)); } // Set text content @@ -6449,10 +6492,10 @@ var SVG = (function () { function (_Container) { _inherits(Bare, _Container); - function Bare(node) { + function Bare(node, attrs) { _classCallCheck(this, Bare); - return _possibleConstructorReturn(this, _getPrototypeOf(Bare).call(this, nodeOrNew(node, typeof node === 'string' ? null : node), Bare)); + return _possibleConstructorReturn(this, _getPrototypeOf(Bare).call(this, nodeOrNew(node, typeof node === 'string' ? null : node), attrs)); } _createClass(Bare, [{ @@ -6487,7 +6530,7 @@ var SVG = (function () { function ClipPath(node) { _classCallCheck(this, ClipPath); - return _possibleConstructorReturn(this, _getPrototypeOf(ClipPath).call(this, nodeOrNew('clipPath', node), ClipPath)); + return _possibleConstructorReturn(this, _getPrototypeOf(ClipPath).call(this, nodeOrNew('clipPath', node), node)); } // Unclip all clipped elements and remove itself @@ -6544,7 +6587,7 @@ var SVG = (function () { function G(node) { _classCallCheck(this, G); - return _possibleConstructorReturn(this, _getPrototypeOf(G).call(this, nodeOrNew('g', node), G)); + return _possibleConstructorReturn(this, _getPrototypeOf(G).call(this, nodeOrNew('g', node), node)); } return G; @@ -6564,10 +6607,10 @@ var SVG = (function () { function (_Dom) { _inherits(HtmlNode, _Dom); - function HtmlNode(node) { + function HtmlNode() { _classCallCheck(this, HtmlNode); - return _possibleConstructorReturn(this, _getPrototypeOf(HtmlNode).call(this, node, HtmlNode)); + return _possibleConstructorReturn(this, _getPrototypeOf(HtmlNode).apply(this, arguments)); } return HtmlNode; @@ -6582,7 +6625,7 @@ var SVG = (function () { function A(node) { _classCallCheck(this, A); - return _possibleConstructorReturn(this, _getPrototypeOf(A).call(this, nodeOrNew('a', node), A)); + return _possibleConstructorReturn(this, _getPrototypeOf(A).call(this, nodeOrNew('a', node), node)); } // Link url @@ -6634,7 +6677,7 @@ var SVG = (function () { function Mask(node) { _classCallCheck(this, Mask); - return _possibleConstructorReturn(this, _getPrototypeOf(Mask).call(this, nodeOrNew('mask', node), Mask)); + return _possibleConstructorReturn(this, _getPrototypeOf(Mask).call(this, nodeOrNew('mask', node), node)); } // Unmask all masked elements and remove itself @@ -6703,7 +6746,7 @@ var SVG = (function () { function Style(node) { _classCallCheck(this, Style); - return _possibleConstructorReturn(this, _getPrototypeOf(Style).call(this, nodeOrNew('style', node), Style)); + return _possibleConstructorReturn(this, _getPrototypeOf(Style).call(this, nodeOrNew('style', node), node)); } _createClass(Style, [{ @@ -6748,7 +6791,7 @@ var SVG = (function () { function _Symbol(node) { _classCallCheck(this, _Symbol); - return _possibleConstructorReturn(this, _getPrototypeOf(_Symbol).call(this, nodeOrNew('symbol', node), _Symbol)); + return _possibleConstructorReturn(this, _getPrototypeOf(_Symbol).call(this, nodeOrNew('symbol', node), node)); } return _Symbol; @@ -6771,7 +6814,7 @@ var SVG = (function () { function TextPath(node) { _classCallCheck(this, TextPath); - return _possibleConstructorReturn(this, _getPrototypeOf(TextPath).call(this, nodeOrNew('textPath', node), TextPath)); + return _possibleConstructorReturn(this, _getPrototypeOf(TextPath).call(this, nodeOrNew('textPath', node), node)); } // return the array of the path track element @@ -6857,7 +6900,7 @@ var SVG = (function () { function Use(node) { _classCallCheck(this, Use); - return _possibleConstructorReturn(this, _getPrototypeOf(Use).call(this, nodeOrNew('use', node), Use)); + return _possibleConstructorReturn(this, _getPrototypeOf(Use).call(this, nodeOrNew('use', node), node)); } // Use element as a reference @@ -6981,7 +7024,9 @@ var SVG = (function () { getClass: getClass, eid: eid, assignNewId: assignNewId, - extend: extend + extend: extend, + extendWithAttrCheck: extendWithAttrCheck, + wrapWithAttrCheck: wrapWithAttrCheck }); function SVG(element) { diff --git a/src/elements/A.js b/src/elements/A.js index 68da597..a0d7311 100644 --- a/src/elements/A.js +++ b/src/elements/A.js @@ -5,7 +5,7 @@ import Container from './Container.js' export default class A extends Container { constructor (node) { - super(nodeOrNew('a', node), A) + super(nodeOrNew('a', node), node) } // Link url diff --git a/src/elements/Bare.js b/src/elements/Bare.js index 43fc075..6264ae2 100644 --- a/src/elements/Bare.js +++ b/src/elements/Bare.js @@ -3,8 +3,8 @@ import { registerMethods } from '../utils/methods.js' import Container from './Container.js' export default class Bare extends Container { - constructor (node) { - super(nodeOrNew(node, typeof node === 'string' ? null : node), Bare) + constructor (node, attrs) { + super(nodeOrNew(node, typeof node === 'string' ? null : node), attrs) } words (text) { diff --git a/src/elements/Circle.js b/src/elements/Circle.js index 52aaa3d..2f0d7f2 100644 --- a/src/elements/Circle.js +++ b/src/elements/Circle.js @@ -6,7 +6,7 @@ import Shape from './Shape.js' export default class Circle extends Shape { constructor (node) { - super(nodeOrNew('circle', node), Circle) + super(nodeOrNew('circle', node), node) } radius (r) { diff --git a/src/elements/ClipPath.js b/src/elements/ClipPath.js index 2828d6e..5164086 100644 --- a/src/elements/ClipPath.js +++ b/src/elements/ClipPath.js @@ -5,7 +5,7 @@ import baseFind from '../modules/core/selector.js' export default class ClipPath extends Container { constructor (node) { - super(nodeOrNew('clipPath', node), ClipPath) + super(nodeOrNew('clipPath', node), node) } // Unclip all clipped elements and remove itself diff --git a/src/elements/Defs.js b/src/elements/Defs.js index 58932cb..2826611 100644 --- a/src/elements/Defs.js +++ b/src/elements/Defs.js @@ -3,7 +3,7 @@ import Container from './Container.js' export default class Defs extends Container { constructor (node) { - super(nodeOrNew('defs', node), Defs) + super(nodeOrNew('defs', node), node) } flatten () { return this } diff --git a/src/elements/Doc.js b/src/elements/Doc.js index 8d450ce..2132491 100644 --- a/src/elements/Doc.js +++ b/src/elements/Doc.js @@ -6,7 +6,7 @@ import Defs from './Defs.js' export default class Doc extends Container { constructor (node) { - super(nodeOrNew('svg', node), Doc) + super(nodeOrNew('svg', node), node) this.namespace() } diff --git a/src/elements/Dom.js b/src/elements/Dom.js index 75f3e94..899da77 100644 --- a/src/elements/Dom.js +++ b/src/elements/Dom.js @@ -11,10 +11,14 @@ import EventTarget from '../types/EventTarget.js' import attr from '../modules/core/attr.js' export default class Dom extends EventTarget { - constructor (node) { + constructor (node, attrs) { super(node) this.node = node this.type = node.nodeName + + if (attrs && node !== attrs) { + this.attr(attrs) + } } // Add given element at a position diff --git a/src/elements/Element.js b/src/elements/Element.js index a38b2ac..fe513e9 100644 --- a/src/elements/Element.js +++ b/src/elements/Element.js @@ -7,8 +7,8 @@ import SVGNumber from '../types/SVGNumber.js' const Doc = getClass(root) export default class Element extends Dom { - constructor (node) { - super(node) + constructor (node, attrs) { + super(node, attrs) // initialize data object this.dom = {} diff --git a/src/elements/Ellipse.js b/src/elements/Ellipse.js index b0ee4bf..4ba8771 100644 --- a/src/elements/Ellipse.js +++ b/src/elements/Ellipse.js @@ -7,7 +7,7 @@ import * as circled from '../modules/core/circled.js' export default class Ellipse extends Shape { constructor (node) { - super(nodeOrNew('ellipse', node), Ellipse) + super(nodeOrNew('ellipse', node), node) } size (width, height) { diff --git a/src/elements/G.js b/src/elements/G.js index 00803c0..5eeb65a 100644 --- a/src/elements/G.js +++ b/src/elements/G.js @@ -4,7 +4,7 @@ import Container from './Container.js' export default class G extends Container { constructor (node) { - super(nodeOrNew('g', node), G) + super(nodeOrNew('g', node), node) } } diff --git a/src/elements/Gradient.js b/src/elements/Gradient.js index cf8aeaa..cfa2950 100644 --- a/src/elements/Gradient.js +++ b/src/elements/Gradient.js @@ -7,10 +7,10 @@ import baseFind from '../modules/core/selector.js' import * as gradiented from '../modules/core/gradiented.js' export default class Gradient extends Container { - constructor (type) { + constructor (type, attrs) { super( nodeOrNew(type + 'Gradient', typeof type === 'string' ? null : type), - Gradient + attrs ) } diff --git a/src/elements/HtmlNode.js b/src/elements/HtmlNode.js index 59152d3..009b122 100644 --- a/src/elements/HtmlNode.js +++ b/src/elements/HtmlNode.js @@ -1,10 +1,6 @@ import { register } from '../utils/adopter.js' import Dom from './Dom.js' -export default class HtmlNode extends Dom { - constructor (node) { - super(node, HtmlNode) - } -} +export default class HtmlNode extends Dom {} register(HtmlNode) diff --git a/src/elements/Image.js b/src/elements/Image.js index ec9459f..469e10a 100644 --- a/src/elements/Image.js +++ b/src/elements/Image.js @@ -9,7 +9,7 @@ import Shape from './Shape.js' export default class Image extends Shape { constructor (node) { - super(nodeOrNew('image', node), Image) + super(nodeOrNew('image', node), node) } // (re)load image diff --git a/src/elements/Line.js b/src/elements/Line.js index b9bc4e8..ba15135 100644 --- a/src/elements/Line.js +++ b/src/elements/Line.js @@ -8,7 +8,7 @@ import * as pointed from '../modules/core/pointed.js' export default class Line extends Shape { // Initialize node constructor (node) { - super(nodeOrNew('line', node), Line) + super(nodeOrNew('line', node), node) } // Get array diff --git a/src/elements/Marker.js b/src/elements/Marker.js index 2b0541b..7e78e7f 100644 --- a/src/elements/Marker.js +++ b/src/elements/Marker.js @@ -5,7 +5,7 @@ import Container from './Container.js' export default class Marker extends Container { // Initialize node constructor (node) { - super(nodeOrNew('marker', node), Marker) + super(nodeOrNew('marker', node), node) } // Set width of element diff --git a/src/elements/Mask.js b/src/elements/Mask.js index 1ed5a8b..89eb97f 100644 --- a/src/elements/Mask.js +++ b/src/elements/Mask.js @@ -6,7 +6,7 @@ import baseFind from '../modules/core/selector.js' export default class Mask extends Container { // Initialize node constructor (node) { - super(nodeOrNew('mask', node), Mask) + super(nodeOrNew('mask', node), node) } // Unmask all masked elements and remove itself diff --git a/src/elements/Path.js b/src/elements/Path.js index 71be8a1..c8a4de4 100644 --- a/src/elements/Path.js +++ b/src/elements/Path.js @@ -8,7 +8,7 @@ import baseFind from '../modules/core/selector.js' export default class Path extends Shape { // Initialize node constructor (node) { - super(nodeOrNew('path', node), Path) + super(nodeOrNew('path', node), node) } // Get array diff --git a/src/elements/Pattern.js b/src/elements/Pattern.js index 9111837..4a1eee0 100644 --- a/src/elements/Pattern.js +++ b/src/elements/Pattern.js @@ -7,7 +7,7 @@ import baseFind from '../modules/core/selector.js' export default class Pattern extends Container { // Initialize node constructor (node) { - super(nodeOrNew('pattern', node), Pattern) + super(nodeOrNew('pattern', node), node) } // Return the fill id diff --git a/src/elements/Polygon.js b/src/elements/Polygon.js index 6097977..a7bf592 100644 --- a/src/elements/Polygon.js +++ b/src/elements/Polygon.js @@ -8,7 +8,7 @@ import * as poly from '../modules/core/poly.js' export default class Polygon extends Shape { // Initialize node constructor (node) { - super(nodeOrNew('polygon', node), Polygon) + super(nodeOrNew('polygon', node), node) } } diff --git a/src/elements/Polyline.js b/src/elements/Polyline.js index b2cb15b..079da52 100644 --- a/src/elements/Polyline.js +++ b/src/elements/Polyline.js @@ -8,7 +8,7 @@ import * as poly from '../modules/core/poly.js' export default class Polyline extends Shape { // Initialize node constructor (node) { - super(nodeOrNew('polyline', node), Polyline) + super(nodeOrNew('polyline', node), node) } } diff --git a/src/elements/Rect.js b/src/elements/Rect.js index fa66fc3..7433993 100644 --- a/src/elements/Rect.js +++ b/src/elements/Rect.js @@ -6,7 +6,7 @@ import Shape from './Shape.js' export default class Rect extends Shape { // Initialize node constructor (node) { - super(nodeOrNew('rect', node), Rect) + super(nodeOrNew('rect', node), node) } } diff --git a/src/elements/Stop.js b/src/elements/Stop.js index bf919e8..9a5acaa 100644 --- a/src/elements/Stop.js +++ b/src/elements/Stop.js @@ -4,7 +4,7 @@ import SVGNumber from '../types/SVGNumber.js' export default class Stop extends Element { constructor (node) { - super(nodeOrNew('stop', node), Stop) + super(nodeOrNew('stop', node), node) } // add color stops diff --git a/src/elements/Style.js b/src/elements/Style.js index 5c25893..6ac84f4 100644 --- a/src/elements/Style.js +++ b/src/elements/Style.js @@ -20,7 +20,7 @@ function cssRule (selector, rule) { export default class Style extends Element { constructor (node) { - super(nodeOrNew('style', node), Style) + super(nodeOrNew('style', node), node) } words (w) { diff --git a/src/elements/Symbol.js b/src/elements/Symbol.js index 183f449..cdb5f85 100644 --- a/src/elements/Symbol.js +++ b/src/elements/Symbol.js @@ -5,7 +5,7 @@ import Container from './Container.js' export default class Symbol extends Container { // Initialize node constructor (node) { - super(nodeOrNew('symbol', node), Symbol) + super(nodeOrNew('symbol', node), node) } } diff --git a/src/elements/Text.js b/src/elements/Text.js index f221004..bd27428 100644 --- a/src/elements/Text.js +++ b/src/elements/Text.js @@ -8,7 +8,7 @@ import * as textable from '../modules/core/textable.js' export default class Text extends Shape { // Initialize node constructor (node) { - super(nodeOrNew('text', node), Text) + super(nodeOrNew('text', node), node) this.dom.leading = new SVGNumber(1.3) // store leading value for rebuilding this._rebuild = true // enable automatic updating of dy values diff --git a/src/elements/TextPath.js b/src/elements/TextPath.js index 480eca2..cddeda8 100644 --- a/src/elements/TextPath.js +++ b/src/elements/TextPath.js @@ -9,7 +9,7 @@ import Text from './Text.js' export default class TextPath extends Text { // Initialize node constructor (node) { - super(nodeOrNew('textPath', node), TextPath) + super(nodeOrNew('textPath', node), node) } // return the array of the path track element diff --git a/src/elements/Tspan.js b/src/elements/Tspan.js index 69815d4..9745b95 100644 --- a/src/elements/Tspan.js +++ b/src/elements/Tspan.js @@ -6,7 +6,7 @@ import * as textable from '../modules/core/textable.js' export default class Tspan extends Text { // Initialize node constructor (node) { - super(nodeOrNew('tspan', node), Tspan) + super(nodeOrNew('tspan', node), node) } // Set text content diff --git a/src/elements/Use.js b/src/elements/Use.js index 43a4e9b..5808988 100644 --- a/src/elements/Use.js +++ b/src/elements/Use.js @@ -5,7 +5,7 @@ import Shape from './Shape.js' export default class Use extends Shape { constructor (node) { - super(nodeOrNew('use', node), Use) + super(nodeOrNew('use', node), node) } // Use element as a reference diff --git a/src/types/Point.js b/src/types/Point.js index 0adcd90..685240b 100644 --- a/src/types/Point.js +++ b/src/types/Point.js @@ -3,9 +3,13 @@ import parser from '../modules/core/parser.js' export default class Point { // Initialize - constructor (x, y, base) { + constructor (...args) { + this.init(...args) + } + + init (x, y) { let source - base = base || { x: 0, y: 0 } + let base = { x: 0, y: 0 } // ensure source as object source = Array.isArray(x) ? { x: x[0], y: x[1] } @@ -42,6 +46,10 @@ export default class Point { // Return the required point return new Point(x, y) } + + toArray () { + return [this.x, this.y] + } } registerMethods({ diff --git a/src/utils/adopter.js b/src/utils/adopter.js index 8017359..6880399 100644 --- a/src/utils/adopter.js +++ b/src/utils/adopter.js @@ -37,7 +37,7 @@ export function makeInstance (element) { } export function nodeOrNew (name, node) { - return node || makeNode(name) + return node instanceof window.Node ? node : makeNode(name) } // Adopt existing svg elements @@ -102,14 +102,34 @@ export function assignNewId (node) { } // Method for extending objects -export function extend (modules, methods) { +export function extend (modules, methods, attrCheck) { var key, i modules = Array.isArray(modules) ? modules : [modules] for (i = modules.length - 1; i >= 0; i--) { for (key in methods) { - modules[i].prototype[key] = methods[key] + let method = methods[key] + if (attrCheck) { + method = wrapWithAttrCheck(methods[key]) + } + modules[i].prototype[key] = method + } + } +} + +export function extendWithAttrCheck (...args) { + extend(...args, true) +} + +export function wrapWithAttrCheck (fn) { + return function (...args) { + let o = args[args.length - 1] + + if (o && !o.prototype && !(o instanceof Array) && typeof o === 'object') { + return fn.apply(this, args.slice(0, -1)).attr(o) + } else { + return fn.apply(this, args) } } } |