aboutsummaryrefslogtreecommitdiffstats
path: root/src/types/Box.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/types/Box.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/types/Box.js')
-rw-r--r--src/types/Box.js152
1 files changed, 99 insertions, 53 deletions
diff --git a/src/types/Box.js b/src/types/Box.js
index e55f114..2fcb923 100644
--- a/src/types/Box.js
+++ b/src/types/Box.js
@@ -4,33 +4,45 @@ import { globals } from '../utils/window.js'
import Point from './Point.js'
import parser from '../modules/core/parser.js'
-function isNulledBox (box) {
+function isNulledBox ( box ) {
+
return !box.w && !box.h && !box.x && !box.y
+
}
-function domContains (node) {
- return (globals.document.documentElement.contains || function (node) {
+function domContains ( node ) {
+
+ return ( globals.document.documentElement.contains || function ( node ) {
+
// This is IE - it does not support contains() for top-level SVGs
- while (node.parentNode) {
+ while ( node.parentNode ) {
+
node = node.parentNode
+
}
return node === document
- }).call(globals.document.documentElement, node)
+
+ } ).call( globals.document.documentElement, node )
+
}
export default class Box {
- constructor (...args) {
- this.init(...args)
+
+ constructor ( ...args ) {
+
+ this.init( ...args )
+
}
- init (source) {
- var base = [0, 0, 0, 0]
- source = typeof source === 'string' ? source.split(delimiter).map(parseFloat)
- : Array.isArray(source) ? source
- : typeof source === 'object' ? [source.left != null ? source.left
- : source.x, source.top != null ? source.top : source.y, source.width, source.height]
- : arguments.length === 4 ? [].slice.call(arguments)
- : base
+ init ( source ) {
+
+ var base = [ 0, 0, 0, 0 ]
+ source = typeof source === 'string' ? source.split( delimiter ).map( parseFloat )
+ : Array.isArray( source ) ? source
+ : typeof source === 'object' ? [ source.left != null ? source.left
+ : source.x, source.top != null ? source.top : source.y, source.width, source.height ]
+ : arguments.length === 4 ? [].slice.call( arguments )
+ : base
this.x = source[0] || 0
this.y = source[1] || 0
@@ -44,105 +56,139 @@ export default class Box {
this.cy = this.y + this.h / 2
return this
+
}
// Merge rect box with another, return a new instance
- merge (box) {
- let x = Math.min(this.x, box.x)
- let y = Math.min(this.y, box.y)
- let width = Math.max(this.x + this.width, box.x + box.width) - x
- let height = Math.max(this.y + this.height, box.y + box.height) - y
+ merge ( box ) {
+
+ let x = Math.min( this.x, box.x )
+ let y = Math.min( this.y, box.y )
+ let width = Math.max( this.x + this.width, box.x + box.width ) - x
+ let height = Math.max( this.y + this.height, box.y + box.height ) - y
+
+ return new Box( x, y, width, height )
- return new Box(x, y, width, height)
}
- transform (m) {
+ transform ( m ) {
+
let xMin = Infinity
let xMax = -Infinity
let yMin = Infinity
let yMax = -Infinity
let pts = [
- new Point(this.x, this.y),
- new Point(this.x2, this.y),
- new Point(this.x, this.y2),
- new Point(this.x2, this.y2)
+ new Point( this.x, this.y ),
+ new Point( this.x2, this.y ),
+ new Point( this.x, this.y2 ),
+ new Point( this.x2, this.y2 )
]
- pts.forEach(function (p) {
- p = p.transform(m)
- xMin = Math.min(xMin, p.x)
- xMax = Math.max(xMax, p.x)
- yMin = Math.min(yMin, p.y)
- yMax = Math.max(yMax, p.y)
- })
+ pts.forEach( function ( p ) {
+
+ p = p.transform( m )
+ xMin = Math.min( xMin, p.x )
+ xMax = Math.max( xMax, p.x )
+ yMin = Math.min( yMin, p.y )
+ yMax = Math.max( yMax, p.y )
+
+ } )
return new Box(
xMin, yMin,
xMax - xMin,
yMax - yMin
)
+
}
addOffset () {
+
// offset by window scroll position, because getBoundingClientRect changes when window is scrolled
this.x += globals.window.pageXOffset
this.y += globals.window.pageYOffset
return this
+
}
toString () {
+
return this.x + ' ' + this.y + ' ' + this.width + ' ' + this.height
+
}
toArray () {
- return [this.x, this.y, this.width, this.height]
+
+ return [ this.x, this.y, this.width, this.height ]
+
}
isNulled () {
- return isNulledBox(this)
+
+ return isNulledBox( this )
+
}
+
}
-function getBox (cb) {
+function getBox ( cb ) {
+
let box
try {
- box = cb(this.node)
- if (isNulledBox(box) && !domContains(this.node)) {
- throw new Error('Element not in the dom')
+ box = cb( this.node )
+
+ if ( isNulledBox( box ) && !domContains( this.node ) ) {
+
+ throw new Error( 'Element not in the dom' )
+
}
- } catch (e) {
+
+ } catch ( e ) {
+
try {
- let clone = this.clone().addTo(parser().svg).show()
- box = cb(clone.node)
+
+ let clone = this.clone().addTo( parser().svg ).show()
+ box = cb( clone.node )
clone.remove()
- } catch (e) {
- throw new Error('Getting a bounding box of element "' + this.node.nodeName + '" is not possible')
+
+ } catch ( e ) {
+
+ throw new Error( 'Getting a bounding box of element "' + this.node.nodeName + '" is not possible' )
+
}
+
}
return box
+
}
export function bbox () {
- return new Box(getBox.call(this, (node) => node.getBBox()))
+
+ return new Box( getBox.call( this, ( node ) => node.getBBox() ) )
+
}
-export function rbox (el) {
- let box = new Box(getBox.call(this, (node) => node.getBoundingClientRect()))
- if (el) return box.transform(el.screenCTM().inverse())
+export function rbox ( el ) {
+
+ let box = new Box( getBox.call( this, ( node ) => node.getBoundingClientRect() ) )
+ if ( el ) return box.transform( el.screenCTM().inverse() )
return box.addOffset()
+
}
-registerMethods({
+registerMethods( {
viewbox: {
- viewbox (x, y, width, height) {
+ viewbox ( x, y, width, height ) {
+
// act as getter
- if (x == null) return new Box(this.attr('viewBox'))
+ if ( x == null ) return new Box( this.attr( 'viewBox' ) )
// act as setter
- return this.attr('viewBox', new Box(x, y, width, height))
+ return this.attr( 'viewBox', new Box( x, y, width, height ) )
+
}
}
-})
+} )