aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/adopter.js
diff options
context:
space:
mode:
authorUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2018-11-08 16:06:39 +0100
committerUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2018-11-08 16:06:39 +0100
commit8c81fb7c2e6e9842570d27a84b1a1c600c82c16b (patch)
treeb5de85d617f5761db7cdfdce70d8a0f7cc059aba /src/utils/adopter.js
parentf7f6c4b801172ce119d4ea9a650c543670474784 (diff)
downloadsvg.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/utils/adopter.js')
-rw-r--r--src/utils/adopter.js26
1 files changed, 23 insertions, 3 deletions
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)
}
}
}