diff options
Diffstat (limited to 'src/modules/optional')
-rw-r--r-- | src/modules/optional/arrange.js | 24 | ||||
-rw-r--r-- | src/modules/optional/class.js | 27 | ||||
-rw-r--r-- | src/modules/optional/css.js | 24 | ||||
-rw-r--r-- | src/modules/optional/data.js | 19 | ||||
-rw-r--r-- | src/modules/optional/memory.js | 6 | ||||
-rw-r--r-- | src/modules/optional/sugar.js | 55 | ||||
-rw-r--r-- | src/modules/optional/transform.js | 32 |
7 files changed, 118 insertions, 69 deletions
diff --git a/src/modules/optional/arrange.js b/src/modules/optional/arrange.js index 9aaeef1..292cd79 100644 --- a/src/modules/optional/arrange.js +++ b/src/modules/optional/arrange.js @@ -2,27 +2,27 @@ import { makeInstance } from '../../utils/adopter.js' import { registerMethods } from '../../utils/methods.js' // Get all siblings, including myself -export function siblings () { +export function siblings() { return this.parent().children() } // Get the current position siblings -export function position () { +export function position() { return this.parent().index(this) } // Get the next element (will return null if there is none) -export function next () { +export function next() { return this.siblings()[this.position() + 1] } // Get the next element (will return null if there is none) -export function prev () { +export function prev() { return this.siblings()[this.position() - 1] } // Send given element one step forward -export function forward () { +export function forward() { const i = this.position() const p = this.parent() @@ -33,7 +33,7 @@ export function forward () { } // Send given element one step backward -export function backward () { +export function backward() { const i = this.position() const p = this.parent() @@ -43,7 +43,7 @@ export function backward () { } // Send given element all the way to the front -export function front () { +export function front() { const p = this.parent() // Move node forward @@ -53,7 +53,7 @@ export function front () { } // Send given element all the way to the back -export function back () { +export function back() { const p = this.parent() // Move node back @@ -63,7 +63,7 @@ export function back () { } // Inserts a given element before the targeted element -export function before (element) { +export function before(element) { element = makeInstance(element) element.remove() @@ -75,7 +75,7 @@ export function before (element) { } // Inserts a given element after the targeted element -export function after (element) { +export function after(element) { element = makeInstance(element) element.remove() @@ -86,13 +86,13 @@ export function after (element) { return this } -export function insertBefore (element) { +export function insertBefore(element) { element = makeInstance(element) element.before(this) return this } -export function insertAfter (element) { +export function insertAfter(element) { element = makeInstance(element) element.after(this) return this diff --git a/src/modules/optional/class.js b/src/modules/optional/class.js index 4e544be..3141644 100644 --- a/src/modules/optional/class.js +++ b/src/modules/optional/class.js @@ -2,18 +2,18 @@ import { delimiter } from '../core/regex.js' import { registerMethods } from '../../utils/methods.js' // Return array of classes on the node -export function classes () { +export function classes() { const attr = this.attr('class') return attr == null ? [] : attr.trim().split(delimiter) } // Return true if class exists on the node, false otherwise -export function hasClass (name) { +export function hasClass(name) { return this.classes().indexOf(name) !== -1 } // Add class to the node -export function addClass (name) { +export function addClass(name) { if (!this.hasClass(name)) { const array = this.classes() array.push(name) @@ -24,21 +24,30 @@ export function addClass (name) { } // Remove class from the node -export function removeClass (name) { +export function removeClass(name) { if (this.hasClass(name)) { - this.attr('class', this.classes().filter(function (c) { - return c !== name - }).join(' ')) + this.attr( + 'class', + this.classes() + .filter(function (c) { + return c !== name + }) + .join(' ') + ) } return this } // Toggle the presence of a class on the node -export function toggleClass (name) { +export function toggleClass(name) { return this.hasClass(name) ? this.removeClass(name) : this.addClass(name) } registerMethods('Dom', { - classes, hasClass, addClass, removeClass, toggleClass + classes, + hasClass, + addClass, + removeClass, + toggleClass }) diff --git a/src/modules/optional/css.js b/src/modules/optional/css.js index ee93869..92f8c21 100644 --- a/src/modules/optional/css.js +++ b/src/modules/optional/css.js @@ -3,11 +3,12 @@ import { isBlank } from '../core/regex.js' import { registerMethods } from '../../utils/methods.js' // Dynamic style generator -export function css (style, val) { +export function css(style, val) { const ret = {} if (arguments.length === 0) { // get full style as object - this.node.style.cssText.split(/\s*;\s*/) + this.node.style.cssText + .split(/\s*;\s*/) .filter(function (el) { return !!el.length }) @@ -37,36 +38,39 @@ export function css (style, val) { if (typeof style === 'object') { for (const name in style) { // set empty string if null/undefined/'' was given - this.node.style[camelCase(name)] - = (style[name] == null || isBlank.test(style[name])) ? '' : style[name] + this.node.style[camelCase(name)] = + style[name] == null || isBlank.test(style[name]) ? '' : style[name] } } } // set style for property if (arguments.length === 2) { - this.node.style[camelCase(style)] - = (val == null || isBlank.test(val)) ? '' : val + this.node.style[camelCase(style)] = + val == null || isBlank.test(val) ? '' : val } return this } // Show element -export function show () { +export function show() { return this.css('display', '') } // Hide element -export function hide () { +export function hide() { return this.css('display', 'none') } // Is element visible? -export function visible () { +export function visible() { return this.css('display') !== 'none' } registerMethods('Dom', { - css, show, hide, visible + css, + show, + hide, + visible }) diff --git a/src/modules/optional/data.js b/src/modules/optional/data.js index 00bb8ea..9655048 100644 --- a/src/modules/optional/data.js +++ b/src/modules/optional/data.js @@ -2,10 +2,18 @@ import { registerMethods } from '../../utils/methods.js' import { filter, map } from '../../utils/utils.js' // Store data values on svg nodes -export function data (a, v, r) { +export function data(a, v, r) { if (a == null) { // get an object of attributes - return this.data(map(filter(this.node.attributes, (el) => el.nodeName.indexOf('data-') === 0), (el) => el.nodeName.slice(5))) + return this.data( + map( + filter( + this.node.attributes, + (el) => el.nodeName.indexOf('data-') === 0 + ), + (el) => el.nodeName.slice(5) + ) + ) } else if (a instanceof Array) { const data = {} for (const key of a) { @@ -23,12 +31,13 @@ export function data (a, v, r) { return this.attr('data-' + a) } } else { - this.attr('data-' + a, + this.attr( + 'data-' + a, v === null ? null : r === true || typeof v === 'string' || typeof v === 'number' - ? v - : JSON.stringify(v) + ? v + : JSON.stringify(v) ) } diff --git a/src/modules/optional/memory.js b/src/modules/optional/memory.js index 459dcf1..31058c3 100644 --- a/src/modules/optional/memory.js +++ b/src/modules/optional/memory.js @@ -1,7 +1,7 @@ import { registerMethods } from '../../utils/methods.js' // Remember arbitrary data -export function remember (k, v) { +export function remember(k, v) { // remember every item in an object individually if (typeof arguments[0] === 'object') { for (const key in k) { @@ -19,7 +19,7 @@ export function remember (k, v) { } // Erase a given memory -export function forget () { +export function forget() { if (arguments.length === 0) { this._memory = {} } else { @@ -33,7 +33,7 @@ export function forget () { // This triggers creation of a new hidden class which is not performant // However, this function is not rarely used so it will not happen frequently // Return local memory object -export function memory () { +export function memory() { return (this._memory = this._memory || {}) } diff --git a/src/modules/optional/sugar.js b/src/modules/optional/sugar.js index d4c3da5..aa5fb82 100644 --- a/src/modules/optional/sugar.js +++ b/src/modules/optional/sugar.js @@ -7,15 +7,24 @@ import SVGNumber from '../../types/SVGNumber.js' // Define list of available attributes for stroke and fill const sugar = { - stroke: [ 'color', 'width', 'opacity', 'linecap', 'linejoin', 'miterlimit', 'dasharray', 'dashoffset' ], - fill: [ 'color', 'opacity', 'rule' ], + 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) { const extension = {} let i @@ -23,7 +32,12 @@ const sugar = { if (typeof o === 'undefined') { return this.attr(m) } - if (typeof o === 'string' || o instanceof Color || Color.isRgb(o) || (o instanceof Element)) { + if ( + typeof o === 'string' || + o instanceof Color || + Color.isRgb(o) || + o instanceof Element + ) { this.attr(m, o) } else { // set all attributes from sugar.fill and sugar.stroke list @@ -37,10 +51,10 @@ const sugar = { return this } - registerMethods([ 'Element', 'Runner' ], extension) + registerMethods(['Element', 'Runner'], extension) }) -registerMethods([ 'Element', 'Runner' ], { +registerMethods(['Element', 'Runner'], { // Let the user set the matrix directly matrix: function (mat, b, c, d, e, f) { // Act as a getter @@ -61,7 +75,7 @@ registerMethods([ 'Element', 'Runner' ], { 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, y], ox: cx, oy: cy }, true) }, shear: function (lam, cx, cy) { @@ -72,17 +86,17 @@ registerMethods([ 'Element', 'Runner' ], { 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, y], ox: cx, oy: cy }, true) }, // Map translate to transform translate: function (x, y) { - return this.transform({ translate: [ x, y ] }, true) + return this.transform({ translate: [x, y] }, true) }, // Map relative translations to transform relative: function (x, y) { - return this.transform({ relative: [ x, y ] }, true) + return this.transform({ relative: [x, y] }, true) }, // Map flip to transform @@ -122,7 +136,7 @@ registerMethods('Path', { } }) -registerMethods([ 'Element', 'Runner' ], { +registerMethods(['Element', 'Runner'], { // Set font font: function (a, v) { if (typeof a === 'object') { @@ -133,15 +147,21 @@ registerMethods([ 'Element', 'Runner' ], { return a === 'leading' ? this.leading(v) : a === 'anchor' - ? 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('text-anchor', v) + : a === 'size' || + a === 'family' || + a === 'weight' || + a === 'stretch' || + a === 'variant' || + a === 'style' + ? this.attr('font-' + a, v) + : this.attr(a, v) } }) // Add events to elements -const methods = [ 'click', +const methods = [ + 'click', 'dblclick', 'mousedown', 'mouseup', @@ -154,7 +174,8 @@ 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) { diff --git a/src/modules/optional/transform.js b/src/modules/optional/transform.js index d8e7381..7f950b3 100644 --- a/src/modules/optional/transform.js +++ b/src/modules/optional/transform.js @@ -4,22 +4,24 @@ import { registerMethods } from '../../utils/methods.js' import Matrix from '../../types/Matrix.js' // Reset all transformations -export function untransform () { +export function untransform() { return this.attr('transform', null) } // merge the whole transformation chain into one matrix and returns it -export function matrixify () { +export function matrixify() { const matrix = (this.attr('transform') || '') // split transformations - .split(transforms).slice(0, -1).map(function (str) { + .split(transforms) + .slice(0, -1) + .map(function (str) { // generate key => value pairs const kv = str.trim().split('(') - return [ kv[0], - kv[1].split(delimiter) - .map(function (str) { - return parseFloat(str) - }) + return [ + kv[0], + kv[1].split(delimiter).map(function (str) { + return parseFloat(str) + }) ] }) .reverse() @@ -35,7 +37,7 @@ export function matrixify () { } // add an element to another parent without changing the visual representation on the screen -export function toParent (parent, i) { +export function toParent(parent, i) { if (this === parent) return this const ctm = this.screenCTM() const pCtm = parent.screenCTM().inverse() @@ -46,12 +48,12 @@ export function toParent (parent, i) { } // same as above with parent equals root-svg -export function toRoot (i) { +export function toRoot(i) { return this.toParent(this.root(), i) } // Add transformations -export function transform (o, relative) { +export function transform(o, relative) { // Act as a getter if no object was passed if (o == null || typeof o === 'string') { const decomposed = new Matrix(this).decompose() @@ -64,11 +66,15 @@ export function transform (o, relative) { } // The user can pass a boolean, an Element or an Matrix or nothing - const cleanRelative = relative === true ? this : (relative || false) + const cleanRelative = relative === true ? this : relative || false const result = new Matrix(cleanRelative).transform(o) return this.attr('transform', result) } registerMethods('Element', { - untransform, matrixify, toParent, toRoot, transform + untransform, + matrixify, + toParent, + toRoot, + transform }) |