From 8c81fb7c2e6e9842570d27a84b1a1c600c82c16b Mon Sep 17 00:00:00 2001 From: Ulrich-Matthias Schäfer Date: Thu, 8 Nov 2018 16:06:39 +0100 Subject: added possibility to pass attribues into a constructor like: `new SVG.Rect({width:100})` --- src/utils/adopter.js | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'src/utils') 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) } } } -- cgit v1.2.3