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 /src | |
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})`
Diffstat (limited to 'src')
-rw-r--r-- | src/elements/A.js | 2 | ||||
-rw-r--r-- | src/elements/Bare.js | 4 | ||||
-rw-r--r-- | src/elements/Circle.js | 2 | ||||
-rw-r--r-- | src/elements/ClipPath.js | 2 | ||||
-rw-r--r-- | src/elements/Defs.js | 2 | ||||
-rw-r--r-- | src/elements/Doc.js | 2 | ||||
-rw-r--r-- | src/elements/Dom.js | 6 | ||||
-rw-r--r-- | src/elements/Element.js | 4 | ||||
-rw-r--r-- | src/elements/Ellipse.js | 2 | ||||
-rw-r--r-- | src/elements/G.js | 2 | ||||
-rw-r--r-- | src/elements/Gradient.js | 4 | ||||
-rw-r--r-- | src/elements/HtmlNode.js | 6 | ||||
-rw-r--r-- | src/elements/Image.js | 2 | ||||
-rw-r--r-- | src/elements/Line.js | 2 | ||||
-rw-r--r-- | src/elements/Marker.js | 2 | ||||
-rw-r--r-- | src/elements/Mask.js | 2 | ||||
-rw-r--r-- | src/elements/Path.js | 2 | ||||
-rw-r--r-- | src/elements/Pattern.js | 2 | ||||
-rw-r--r-- | src/elements/Polygon.js | 2 | ||||
-rw-r--r-- | src/elements/Polyline.js | 2 | ||||
-rw-r--r-- | src/elements/Rect.js | 2 | ||||
-rw-r--r-- | src/elements/Stop.js | 2 | ||||
-rw-r--r-- | src/elements/Style.js | 2 | ||||
-rw-r--r-- | src/elements/Symbol.js | 2 | ||||
-rw-r--r-- | src/elements/Text.js | 2 | ||||
-rw-r--r-- | src/elements/TextPath.js | 2 | ||||
-rw-r--r-- | src/elements/Tspan.js | 2 | ||||
-rw-r--r-- | src/elements/Use.js | 2 | ||||
-rw-r--r-- | src/types/Point.js | 12 | ||||
-rw-r--r-- | src/utils/adopter.js | 26 |
30 files changed, 68 insertions, 40 deletions
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) } } } |