aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/adopter.js
diff options
context:
space:
mode:
authorSaivan <savian@me.com>2018-11-25 16:21:53 +1300
committerSaivan <savian@me.com>2018-11-25 16:21:53 +1300
commit62de7d0a1b994b69032a759b796b486e6bc382e3 (patch)
tree112b19f2903b4dc5b4cf61ebef0d021c6ca2f14d /src/utils/adopter.js
parent2b37d7ba5b4267b39c86f9aba5fb14a1b376e846 (diff)
downloadsvg.js-62de7d0a1b994b69032a759b796b486e6bc382e3.tar.gz
svg.js-62de7d0a1b994b69032a759b796b486e6bc382e3.zip
Changed the esLint rules to avoid silly ternary operators, and to let code breathe!
This commit modifies some of the eslint rules, to allow our code to be a little bit more readable. This came about because we had a particularly pesky problem, where the code was indenting ternary operators. This fixes that, and makes it easy to add new rules to eslint as we please in the future. Changes ======= - Rebuilt the library with new eslint rules - Changed the eslintrc file to a yaml file by default
Diffstat (limited to 'src/utils/adopter.js')
-rw-r--r--src/utils/adopter.js156
1 files changed, 105 insertions, 51 deletions
diff --git a/src/utils/adopter.js b/src/utils/adopter.js
index 80bfd8a..6e526e3 100644
--- a/src/utils/adopter.js
+++ b/src/utils/adopter.js
@@ -5,136 +5,190 @@ import { globals } from '../utils/window.js'
import Base from '../types/Base.js'
const elements = {}
-export const root = Symbol('root')
+export const root = Symbol( 'root' )
// Method for element creation
-export function makeNode (name) {
+export function makeNode ( name ) {
+
// create element
- return globals.document.createElementNS(ns, name)
+ return globals.document.createElementNS( ns, name )
+
}
-export function makeInstance (element) {
- if (element instanceof Base) return element
+export function makeInstance ( element ) {
+
+ if ( element instanceof Base ) return element
+
+ if ( typeof element === 'object' ) {
+
+ return adopt( element )
- if (typeof element === 'object') {
- return adopt(element)
}
- if (element == null) {
+ if ( element == null ) {
+
return new elements[root]()
+
}
- if (typeof element === 'string' && element.charAt(0) !== '<') {
- return adopt(globals.document.querySelector(element))
+ if ( typeof element === 'string' && element.charAt( 0 ) !== '<' ) {
+
+ return adopt( globals.document.querySelector( element ) )
+
}
- var node = makeNode('svg')
+ var node = makeNode( 'svg' )
node.innerHTML = element
// We can use firstChild here because we know,
// that the first char is < and thus an element
- element = adopt(node.firstChild)
+ element = adopt( node.firstChild )
return element
+
}
-export function nodeOrNew (name, node) {
- return node instanceof globals.window.Node ? node : makeNode(name)
+export function nodeOrNew ( name, node ) {
+
+ return node instanceof globals.window.Node ? node : makeNode( name )
+
}
// Adopt existing svg elements
-export function adopt (node) {
+export function adopt ( node ) {
+
// check for presence of node
- if (!node) return null
+ if ( !node ) return null
// make sure a node isn't already adopted
- if (node.instance instanceof Base) return node.instance
+ if ( node.instance instanceof Base ) return node.instance
+
+ if ( !( node instanceof globals.window.SVGElement ) ) {
+
+ return new elements.HtmlNode( node )
- if (!(node instanceof globals.window.SVGElement)) {
- return new elements.HtmlNode(node)
}
// initialize variables
var element
// adopt with element-specific settings
- if (node.nodeName === 'svg') {
- element = new elements[root](node)
- } else if (node.nodeName === 'linearGradient' || node.nodeName === 'radialGradient') {
- element = new elements.Gradient(node)
- } else if (elements[capitalize(node.nodeName)]) {
- element = new elements[capitalize(node.nodeName)](node)
+ if ( node.nodeName === 'svg' ) {
+
+ element = new elements[root]( node )
+
+ } else if ( node.nodeName === 'linearGradient' || node.nodeName === 'radialGradient' ) {
+
+ element = new elements.Gradient( node )
+
+ } else if ( elements[capitalize( node.nodeName )] ) {
+
+ element = new elements[capitalize( node.nodeName )]( node )
+
} else {
- element = new elements.Bare(node)
+
+ element = new elements.Bare( node )
+
}
return element
+
}
-export function register (element, name = element.name, asRoot = false) {
+export function register ( element, name = element.name, asRoot = false ) {
+
elements[name] = element
- if (asRoot) elements[root] = element
+ if ( asRoot ) elements[root] = element
- addMethodNames(Object.keys(element.prototype))
+ addMethodNames( Object.keys( element.prototype ) )
return element
+
}
-export function getClass (name) {
+export function getClass ( name ) {
+
return elements[name]
+
}
// Element id sequence
let did = 1000
// Get next named element id
-export function eid (name) {
- return 'Svgjs' + capitalize(name) + (did++)
+export function eid ( name ) {
+
+ return 'Svgjs' + capitalize( name ) + ( did++ )
+
}
// Deep new id assignment
-export function assignNewId (node) {
+export function assignNewId ( node ) {
+
// do the same for SVG child nodes as well
- for (var i = node.children.length - 1; i >= 0; i--) {
- assignNewId(node.children[i])
+ for ( var i = node.children.length - 1; i >= 0; i-- ) {
+
+ assignNewId( node.children[i] )
+
}
- if (node.id) {
- return adopt(node).id(eid(node.nodeName))
+ if ( node.id ) {
+
+ return adopt( node ).id( eid( node.nodeName ) )
+
}
- return adopt(node)
+ return adopt( node )
+
}
// Method for extending objects
-export function extend (modules, methods, attrCheck) {
+export function extend ( modules, methods, attrCheck ) {
+
var key, i
- modules = Array.isArray(modules) ? modules : [modules]
+ modules = Array.isArray( modules ) ? modules : [ modules ]
+
+ for ( i = modules.length - 1; i >= 0; i-- ) {
+
+ for ( key in methods ) {
- for (i = modules.length - 1; i >= 0; i--) {
- for (key in methods) {
let method = methods[key]
- if (attrCheck) {
- method = wrapWithAttrCheck(methods[key])
+ if ( attrCheck ) {
+
+ method = wrapWithAttrCheck( methods[key] )
+
}
modules[i].prototype[key] = method
+
}
+
}
+
}
-export function extendWithAttrCheck (...args) {
- extend(...args, true)
+export function extendWithAttrCheck ( ...args ) {
+
+ extend( ...args, true )
+
}
-export function wrapWithAttrCheck (fn) {
- return function (...args) {
+export function wrapWithAttrCheck ( fn ) {
+
+ return function ( ...args ) {
+
let o = args[args.length - 1]
- if (o && o.constructor === Object && !(o instanceof Array)) {
- return fn.apply(this, args.slice(0, -1)).attr(o)
+ if ( o && o.constructor === Object && !( o instanceof Array ) ) {
+
+ return fn.apply( this, args.slice( 0, -1 ) ).attr( o )
+
} else {
- return fn.apply(this, args)
+
+ return fn.apply( this, args )
+
}
+
}
+
}