chore(core): Refactor v3 code
This commit is contained in:
parent
f882a26408
commit
200cebf582
27 changed files with 3961 additions and 2633 deletions
|
@ -1,8 +1,30 @@
|
|||
//////////////////////////////////////////////
|
||||
// CONSTRUCTOR //
|
||||
//////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* Constructor for Attributes
|
||||
*
|
||||
* @constructor
|
||||
* @return {Attributes} this - The Attributes instance
|
||||
*/
|
||||
export function Attributes() {
|
||||
this.list = {}
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
/** Adds an attribute */
|
||||
//////////////////////////////////////////////
|
||||
// PUBLIC METHODS //
|
||||
//////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* Add an attribute
|
||||
*
|
||||
* @param {string} name - Name of the attribute to add
|
||||
* @param {string} value - Value of the attribute to add
|
||||
* @return {Attributes} this - The Attributes instance
|
||||
*/
|
||||
Attributes.prototype.add = function (name, value) {
|
||||
if (typeof this.list[name] === 'undefined') {
|
||||
this.list[name] = []
|
||||
|
@ -12,75 +34,12 @@ Attributes.prototype.add = function (name, value) {
|
|||
return this
|
||||
}
|
||||
|
||||
/** Sets an attribute, overwriting existing value */
|
||||
Attributes.prototype.set = function (name, value) {
|
||||
this.list[name] = [value]
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
/** Sets an attribute, but only if it's not currently set */
|
||||
Attributes.prototype.setIfUnset = function (name, value) {
|
||||
if (typeof this.list[name] === 'undefined') this.list[name] = [value]
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
/** Removes an attribute */
|
||||
Attributes.prototype.remove = function (name) {
|
||||
delete this.list[name]
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
/** Retrieves an attribute */
|
||||
Attributes.prototype.get = function (name) {
|
||||
if (typeof this.list[name] === 'undefined') return false
|
||||
else return this.list[name].join(' ')
|
||||
}
|
||||
|
||||
/** Retrieves an attribute as array*/
|
||||
Attributes.prototype.getAsArray = function (name) {
|
||||
if (typeof this.list[name] === 'undefined') return false
|
||||
else return this.list[name]
|
||||
}
|
||||
|
||||
/** Returns SVG code for attributes */
|
||||
Attributes.prototype.render = function () {
|
||||
let svg = ''
|
||||
for (let key in this.list) {
|
||||
svg += ` ${key}="${this.list[key].join(' ')}"`
|
||||
}
|
||||
|
||||
return svg
|
||||
}
|
||||
|
||||
/** Returns CSS code for attributes */
|
||||
Attributes.prototype.renderAsCss = function () {
|
||||
let css = ''
|
||||
for (let key in this.list) {
|
||||
css += ` ${key}:${this.list[key].join(' ')};`
|
||||
}
|
||||
|
||||
return css
|
||||
}
|
||||
|
||||
/** Returns SVG code for attributes with a fiven prefix
|
||||
* typically used for data-text*/
|
||||
Attributes.prototype.renderIfPrefixIs = function (prefix = '') {
|
||||
let svg = ''
|
||||
let prefixLen = prefix.length
|
||||
for (let key in this.list) {
|
||||
if (key.substr(0, prefixLen) === prefix) {
|
||||
svg += ` ${key.substr(prefixLen)}="${this.list[key].join(' ')}"`
|
||||
}
|
||||
}
|
||||
|
||||
return svg
|
||||
}
|
||||
|
||||
/** Returns a props object for attributes with a fiven prefix
|
||||
* typically used for data-text*/
|
||||
/**
|
||||
* Return a props object for attributes with a fiven prefix (typically used for data-text)
|
||||
*
|
||||
* @param {string} prefix - The prefix to filter attributes on
|
||||
* @return {object} props - The attributes as props
|
||||
*/
|
||||
Attributes.prototype.asPropsIfPrefixIs = function (prefix = '') {
|
||||
let props = {}
|
||||
let prefixLen = prefix.length
|
||||
|
@ -95,10 +54,120 @@ Attributes.prototype.asPropsIfPrefixIs = function (prefix = '') {
|
|||
return props
|
||||
}
|
||||
|
||||
/** Returns a deep copy of this */
|
||||
/**
|
||||
* Return a deep copy of this
|
||||
*
|
||||
* @return {object} this - The Attributes instance
|
||||
*/
|
||||
Attributes.prototype.clone = function () {
|
||||
let clone = new Attributes()
|
||||
clone.list = JSON.parse(JSON.stringify(this.list))
|
||||
|
||||
return clone
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve an attribute
|
||||
*
|
||||
* @param {string} name - Name of the attribute to get
|
||||
* @return value - The value under name
|
||||
*/
|
||||
Attributes.prototype.get = function (name) {
|
||||
if (typeof this.list[name] === 'undefined') return false
|
||||
else return this.list[name].join(' ')
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve an attribute as array
|
||||
*
|
||||
* @param {string} name - Name of the attribute to set
|
||||
* @return {object} this - The Attributes instance
|
||||
*/
|
||||
Attributes.prototype.getAsArray = function (name) {
|
||||
if (typeof this.list[name] === 'undefined') return false
|
||||
else return this.list[name]
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove an attribute
|
||||
*
|
||||
* @param {string} name - Name of the attribute to set
|
||||
* @return {object} this - The Attributes instance
|
||||
*/
|
||||
Attributes.prototype.remove = function (name) {
|
||||
delete this.list[name]
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Return SVG code for attributes
|
||||
*
|
||||
* @return {string} svg - The SVG code
|
||||
*/
|
||||
Attributes.prototype.render = function () {
|
||||
let svg = ''
|
||||
for (let key in this.list) {
|
||||
svg += ` ${key}="${this.list[key].join(' ')}"`
|
||||
}
|
||||
|
||||
return svg
|
||||
}
|
||||
|
||||
/**
|
||||
* Return CSS code for attributes
|
||||
*
|
||||
* @return {string} css - The CSS code
|
||||
*/
|
||||
Attributes.prototype.renderAsCss = function () {
|
||||
let css = ''
|
||||
for (let key in this.list) {
|
||||
css += ` ${key}:${this.list[key].join(' ')};`
|
||||
}
|
||||
|
||||
return css
|
||||
}
|
||||
|
||||
/**
|
||||
* Return SVG code for attributes with a fiven prefix (typically used for data-text)
|
||||
*
|
||||
* @param {string} prefix - The prefix to filter attributes on
|
||||
* @return {string} svg - The SVG code
|
||||
*/
|
||||
Attributes.prototype.renderIfPrefixIs = function (prefix = '') {
|
||||
let svg = ''
|
||||
let prefixLen = prefix.length
|
||||
for (let key in this.list) {
|
||||
if (key.substr(0, prefixLen) === prefix) {
|
||||
svg += ` ${key.substr(prefixLen)}="${this.list[key].join(' ')}"`
|
||||
}
|
||||
}
|
||||
|
||||
return svg
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an attribute, overwriting existing value
|
||||
*
|
||||
* @param {string} name - Name of the attribute to set
|
||||
* @param {string} value - Value of the attribute to set
|
||||
* @return {Attributes} this - The Attributes instance
|
||||
*/
|
||||
Attributes.prototype.set = function (name, value) {
|
||||
this.list[name] = [value]
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets an attribute, but only if it's not currently set
|
||||
*
|
||||
* @param {string} name - Name of the attribute to set
|
||||
* @param {string} value - Value of the attribute to set
|
||||
* @return {Attributes} this - The Attributes instance
|
||||
*/
|
||||
Attributes.prototype.setIfUnset = function (name, value) {
|
||||
if (typeof this.list[name] === 'undefined') this.list[name] = [value]
|
||||
|
||||
return this
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue