summaryrefslogtreecommitdiffstats
path: root/src/utils
diff options
context:
space:
mode:
authorSaivan <savian@me.com>2018-11-26 00:17:41 +1300
committerSaivan <savian@me.com>2018-11-26 00:17:41 +1300
commit617aa12304541cf1d80b2bf5567ac633958c38de (patch)
treec1082b4573625f93d18e82e6d5a0c4a40782993f /src/utils
parent599fda2f11c88b2c18d0cd0b57d4adeca20db2eb (diff)
downloadsvg.js-617aa12304541cf1d80b2bf5567ac633958c38de.tar.gz
svg.js-617aa12304541cf1d80b2bf5567ac633958c38de.zip
Reverted some of the lint rules after chatting with fuzzy
This commit reverts some of the rules we added on the linter, it changed a lot of code again... but these won't happen too often. Changes ======= - Modified the linter again
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/adopter.js156
-rw-r--r--src/utils/methods.js50
-rw-r--r--src/utils/utils.js94
-rw-r--r--src/utils/window.js4
4 files changed, 95 insertions, 209 deletions
diff --git a/src/utils/adopter.js b/src/utils/adopter.js
index 6e526e3..ec6b2e5 100644
--- a/src/utils/adopter.js
+++ b/src/utils/adopter.js
@@ -5,190 +5,136 @@ 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
-
- if ( typeof element === 'object' ) {
-
- return adopt( element )
+export function makeInstance (element) {
+ if (element instanceof Base) return 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 instanceof globals.window.SVGElement ) ) {
-
- return new elements.HtmlNode( node )
+ if (node.instance instanceof Base) return node.instance
+ 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 ]
-
- for ( i = modules.length - 1; i >= 0; i-- ) {
-
- for ( key in methods ) {
+ modules = Array.isArray(modules) ? modules : [ modules ]
+ 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)
}
-
}
-
}
diff --git a/src/utils/methods.js b/src/utils/methods.js
index 4973d13..527e7b7 100644
--- a/src/utils/methods.js
+++ b/src/utils/methods.js
@@ -2,61 +2,41 @@ const methods = {}
const constructors = {}
const names = []
-export function registerMethods ( name, m ) {
-
- if ( Array.isArray( name ) ) {
-
- for ( let _name of name ) {
-
- registerMethods( _name, m )
-
+export function registerMethods (name, m) {
+ if (Array.isArray(name)) {
+ for (let _name of name) {
+ registerMethods(_name, m)
}
return
-
}
- if ( typeof name === 'object' ) {
-
- for ( let [ _name, _m ] of Object.entries( name ) ) {
-
- registerMethods( _name, _m )
-
+ if (typeof name === 'object') {
+ for (let [ _name, _m ] of Object.entries(name)) {
+ registerMethods(_name, _m)
}
return
-
}
- addMethodNames( Object.keys( m ) )
- methods[name] = Object.assign( methods[name] || {}, m )
-
+ addMethodNames(Object.keys(m))
+ methods[name] = Object.assign(methods[name] || {}, m)
}
-export function getMethodsFor ( name ) {
-
+export function getMethodsFor (name) {
return methods[name] || {}
-
}
export function getMethodNames () {
-
- return [ ...new Set( names ) ]
-
+ return [ ...new Set(names) ]
}
-export function addMethodNames ( _names ) {
-
- names.push( ..._names )
-
+export function addMethodNames (_names) {
+ names.push(..._names)
}
-export function registerConstructor ( name, setup ) {
-
+export function registerConstructor (name, setup) {
constructors[name] = setup
-
}
-export function getConstructor ( name ) {
-
+export function getConstructor (name) {
return constructors[name] ? { setup: constructors[name], name } : {}
-
}
diff --git a/src/utils/utils.js b/src/utils/utils.js
index 01cd49f..3bac0de 100644
--- a/src/utils/utils.js
+++ b/src/utils/utils.js
@@ -1,143 +1,105 @@
// Map function
-export function map ( array, block ) {
-
+export function map (array, block) {
var i
var il = array.length
var result = []
- for ( i = 0; i < il; i++ ) {
-
- result.push( block( array[i] ) )
-
+ for (i = 0; i < il; i++) {
+ result.push(block(array[i]))
}
return result
-
}
// Filter function
-export function filter ( array, block ) {
-
+export function filter (array, block) {
var i
var il = array.length
var result = []
- for ( i = 0; i < il; i++ ) {
-
- if ( block( array[i] ) ) {
-
- result.push( array[i] )
-
+ for (i = 0; i < il; i++) {
+ if (block(array[i])) {
+ result.push(array[i])
}
-
}
return result
-
}
// Degrees to radians
-export function radians ( d ) {
-
+export function radians (d) {
return d % 360 * Math.PI / 180
-
}
// Radians to degrees
-export function degrees ( r ) {
-
+export function degrees (r) {
return r * 180 / Math.PI % 360
-
}
// Convert dash-separated-string to camelCase
-export function camelCase ( s ) {
-
- return s.toLowerCase().replace( /-(.)/g, function ( m, g ) {
-
+export function camelCase (s) {
+ return s.toLowerCase().replace(/-(.)/g, function (m, g) {
return g.toUpperCase()
-
- } )
-
+ })
}
// Convert camel cased string to string seperated
-export function unCamelCase ( s ) {
-
- return s.replace( /([A-Z])/g, function ( m, g ) {
-
+export function unCamelCase (s) {
+ return s.replace(/([A-Z])/g, function (m, g) {
return '-' + g.toLowerCase()
-
- } )
-
+ })
}
// Capitalize first letter of a string
-export function capitalize ( s ) {
-
- return s.charAt( 0 ).toUpperCase() + s.slice( 1 )
-
+export function capitalize (s) {
+ return s.charAt(0).toUpperCase() + s.slice(1)
}
// Calculate proportional width and height values when necessary
-export function proportionalSize ( element, width, height ) {
-
- if ( width == null || height == null ) {
-
+export function proportionalSize (element, width, height) {
+ if (width == null || height == null) {
var box = element.bbox()
- if ( width == null ) {
-
+ if (width == null) {
width = box.width / box.height * height
-
- } else if ( height == null ) {
-
+ } else if (height == null) {
height = box.height / box.width * width
-
}
-
}
return {
width: width,
height: height
}
-
}
-export function getOrigin ( o, element ) {
-
+export function getOrigin (o, element) {
// Allow origin or around as the names
let origin = o.origin // o.around == null ? o.origin : o.around
let ox, oy
// Allow the user to pass a string to rotate around a given point
- if ( typeof origin === 'string' || origin == null ) {
-
+ if (typeof origin === 'string' || origin == null) {
// Get the bounding box of the element with no transformations applied
- const string = ( origin || 'center' ).toLowerCase().trim()
+ const string = (origin || 'center').toLowerCase().trim()
const { height, width, x, y } = element.bbox()
// Calculate the transformed x and y coordinates
- let bx = string.includes( 'left' ) ? x
- : string.includes( 'right' ) ? x + width
+ let bx = string.includes('left') ? x
+ : string.includes('right') ? x + width
: x + width / 2
- let by = string.includes( 'top' ) ? y
- : string.includes( 'bottom' ) ? y + height
+ let by = string.includes('top') ? y
+ : string.includes('bottom') ? y + height
: y + height / 2
// Set the bounds eg : "bottom-left", "Top right", "middle" etc...
ox = o.ox != null ? o.ox : bx
oy = o.oy != null ? o.oy : by
-
} else {
-
ox = origin[0]
oy = origin[1]
-
}
// Return the origin as it is if it wasn't a string
return [ ox, oy ]
-
}
diff --git a/src/utils/window.js b/src/utils/window.js
index 55f0bb6..9e51339 100644
--- a/src/utils/window.js
+++ b/src/utils/window.js
@@ -3,9 +3,7 @@ export const globals = {
document: typeof document === 'undefined' ? null : document
}
-export function registerWindow ( win = null, doc = null ) {
-
+export function registerWindow (win = null, doc = null) {
globals.window = win
globals.document = doc
-
}