diff options
author | Saivan <savian@me.com> | 2018-11-25 16:21:53 +1300 |
---|---|---|
committer | Saivan <savian@me.com> | 2018-11-25 16:21:53 +1300 |
commit | 62de7d0a1b994b69032a759b796b486e6bc382e3 (patch) | |
tree | 112b19f2903b4dc5b4cf61ebef0d021c6ca2f14d /src/modules/optional/sugar.js | |
parent | 2b37d7ba5b4267b39c86f9aba5fb14a1b376e846 (diff) | |
download | svg.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/modules/optional/sugar.js')
-rw-r--r-- | src/modules/optional/sugar.js | 240 |
1 files changed, 153 insertions, 87 deletions
diff --git a/src/modules/optional/sugar.js b/src/modules/optional/sugar.js index 6001631..4b6e6f3 100644 --- a/src/modules/optional/sugar.js +++ b/src/modules/optional/sugar.js @@ -8,169 +8,227 @@ import SVGNumber from '../../types/SVGNumber.js' // Define list of available attributes for stroke and fill var sugar = { - stroke: ['color', 'width', 'opacity', 'linecap', 'linejoin', 'miterlimit', 'dasharray', 'dashoffset'], - fill: ['color', 'opacity', 'rule'], - prefix: function (t, a) { + stroke: [ 'color', 'width', 'opacity', 'linecap', 'linejoin', 'miterlimit', 'dasharray', 'dashoffset' ], + fill: [ 'color', 'opacity', 'rule' ], + prefix: function ( t, a ) { + return a === 'color' ? t : t + '-' + a + } } // Add sugar for fill and stroke -;['fill', 'stroke'].forEach(function (m) { +;[ 'fill', 'stroke' ].forEach( function ( m ) { + var extension = {} var i - extension[m] = function (o) { - if (typeof o === 'undefined') { - return this.attr(m) + extension[m] = function ( o ) { + + if ( typeof o === 'undefined' ) { + + return this.attr( m ) + } - if (typeof o === 'string' || Color.isRgb(o) || (o instanceof Element)) { - this.attr(m, o) + if ( typeof o === 'string' || Color.isRgb( o ) || ( o instanceof Element ) ) { + + this.attr( m, o ) + } else { + // set all attributes from sugar.fill and sugar.stroke list - for (i = sugar[m].length - 1; i >= 0; i--) { - if (o[sugar[m][i]] != null) { - this.attr(sugar.prefix(m, sugar[m][i]), o[sugar[m][i]]) + for ( i = sugar[m].length - 1; i >= 0; i-- ) { + + if ( o[sugar[m][i]] != null ) { + + this.attr( sugar.prefix( m, sugar[m][i] ), o[sugar[m][i]] ) + } + } + } return this + } - registerMethods(['Shape', 'Runner'], extension) -}) + registerMethods( [ 'Shape', 'Runner' ], extension ) -registerMethods(['Element', 'Runner'], { +} ) + +registerMethods( [ 'Element', 'Runner' ], { // Let the user set the matrix directly - matrix: function (mat, b, c, d, e, f) { + matrix: function ( mat, b, c, d, e, f ) { + // Act as a getter - if (mat == null) { - return new Matrix(this) + if ( mat == null ) { + + return new Matrix( this ) + } // Act as a setter, the user can pass a matrix or a set of numbers - return this.attr('transform', new Matrix(mat, b, c, d, e, f)) + return this.attr( 'transform', new Matrix( mat, b, c, d, e, f ) ) + }, // Map rotation to transform - rotate: function (angle, cx, cy) { - return this.transform({ rotate: angle, ox: cx, oy: cy }, true) + rotate: function ( angle, cx, cy ) { + + return this.transform( { rotate: angle, ox: cx, oy: cy }, true ) + }, // Map skew to transform - skew: function (x, y, cx, cy) { + skew: function ( x, y, cx, cy ) { + return arguments.length === 1 || arguments.length === 3 - ? this.transform({ skew: x, ox: y, oy: cx }, true) - : this.transform({ skew: [x, y], ox: cx, oy: cy }, true) + ? this.transform( { skew: x, ox: y, oy: cx }, true ) + : this.transform( { skew: [ x, y ], ox: cx, oy: cy }, true ) + }, - shear: function (lam, cx, cy) { - return this.transform({ shear: lam, ox: cx, oy: cy }, true) + shear: function ( lam, cx, cy ) { + + return this.transform( { shear: lam, ox: cx, oy: cy }, true ) + }, // Map scale to transform - scale: function (x, y, cx, cy) { + scale: function ( x, y, cx, cy ) { + return arguments.length === 1 || arguments.length === 3 - ? this.transform({ scale: x, ox: y, oy: cx }, true) - : this.transform({ scale: [x, y], ox: cx, oy: cy }, true) + ? this.transform( { scale: x, ox: y, oy: cx }, true ) + : this.transform( { scale: [ x, y ], ox: cx, oy: cy }, true ) + }, // Map translate to transform - translate: function (x, y) { - return this.transform({ translate: [x, y] }, true) + translate: function ( x, y ) { + + return this.transform( { translate: [ x, y ] }, true ) + }, // Map relative translations to transform - relative: function (x, y) { - return this.transform({ relative: [x, y] }, true) + relative: function ( x, y ) { + + return this.transform( { relative: [ x, y ] }, true ) + }, // Map flip to transform - flip: function (direction, around) { + flip: function ( direction, around ) { + var directionString = typeof direction === 'string' ? direction - : isFinite(direction) ? 'both' - : 'both' - var origin = (direction === 'both' && isFinite(around)) ? [around, around] - : (direction === 'x') ? [around, 0] - : (direction === 'y') ? [0, around] - : isFinite(direction) ? [direction, direction] - : [0, 0] - this.transform({ flip: directionString, origin: origin }, true) + : isFinite( direction ) ? 'both' + : 'both' + var origin = ( direction === 'both' && isFinite( around ) ) ? [ around, around ] + : ( direction === 'x' ) ? [ around, 0 ] + : ( direction === 'y' ) ? [ 0, around ] + : isFinite( direction ) ? [ direction, direction ] + : [ 0, 0 ] + this.transform( { flip: directionString, origin: origin }, true ) + }, // Opacity - opacity: function (value) { - return this.attr('opacity', value) + opacity: function ( value ) { + + return this.attr( 'opacity', value ) + }, // Relative move over x and y axes - dmove: function (x, y) { - return this.dx(x).dy(y) + dmove: function ( x, y ) { + + return this.dx( x ).dy( y ) + } -}) +} ) -registerMethods('Element', { +registerMethods( 'Element', { // Relative move over x axis - dx: function (x) { - return this.x(new SVGNumber(x).plus(this.x())) + dx: function ( x ) { + + return this.x( new SVGNumber( x ).plus( this.x() ) ) + }, // Relative move over y axis - dy: function (y) { - return this.y(new SVGNumber(y).plus(this.y())) + dy: function ( y ) { + + return this.y( new SVGNumber( y ).plus( this.y() ) ) + } -}) +} ) -registerMethods('radius', { +registerMethods( 'radius', { // Add x and y radius - radius: function (x, y) { - var type = (this._element || this).type + radius: function ( x, y ) { + + var type = ( this._element || this ).type return type === 'radialGradient' || type === 'radialGradient' - ? this.attr('r', new SVGNumber(x)) - : this.rx(x).ry(y == null ? x : y) + ? this.attr( 'r', new SVGNumber( x ) ) + : this.rx( x ).ry( y == null ? x : y ) + } -}) +} ) -registerMethods('Path', { +registerMethods( 'Path', { // Get path length length: function () { + return this.node.getTotalLength() + }, // Get point at length - pointAt: function (length) { - return new Point(this.node.getPointAtLength(length)) + pointAt: function ( length ) { + + return new Point( this.node.getPointAtLength( length ) ) + } -}) +} ) -registerMethods(['Element', 'Runner'], { +registerMethods( [ 'Element', 'Runner' ], { // Set font - font: function (a, v) { - if (typeof a === 'object') { - for (v in a) this.font(v, a[v]) + font: function ( a, v ) { + + if ( typeof a === 'object' ) { + + for ( v in a ) this.font( v, a[v] ) + } return a === 'leading' - ? this.leading(v) + ? this.leading( v ) : a === 'anchor' - ? this.attr('text-anchor', v) + ? this.attr( 'text-anchor', v ) : a === 'size' || a === 'family' || a === 'weight' || a === 'stretch' || a === 'variant' || a === 'style' - ? this.attr('font-' + a, v) - : this.attr(a, v) + ? this.attr( 'font-' + a, v ) + : this.attr( a, v ) + } -}) +} ) + +registerMethods( 'Text', { + ax ( x ) { + + return this.attr( 'x', x ) -registerMethods('Text', { - ax (x) { - return this.attr('x', x) }, - ay (y) { - return this.attr('y', y) + ay ( y ) { + + return this.attr( 'y', y ) + }, - amove (x, y) { - return this.ax(x).ay(y) + amove ( x, y ) { + + return this.ax( x ).ay( y ) + } -}) +} ) // Add events to elements const methods = [ 'click', @@ -186,19 +244,27 @@ const methods = [ 'click', 'touchmove', 'touchleave', 'touchend', - 'touchcancel' ].reduce(function (last, event) { + 'touchcancel' ].reduce( function ( last, event ) { + // add event to Element - const fn = function (f) { - if (f === null) { - off(this, event) + const fn = function ( f ) { + + if ( f === null ) { + + off( this, event ) + } else { - on(this, event, f) + + on( this, event, f ) + } return this + } last[event] = fn return last -}, {}) -registerMethods('Element', methods) +}, {} ) + +registerMethods( 'Element', methods ) |