1
0
Fork 0
freesewing/packages/core/src/attributes.js

100 lines
2.3 KiB
JavaScript
Raw Normal View History

function Attributes() {
2019-08-03 15:03:33 +02:00
this.list = {}
2018-07-23 20:14:32 +02:00
}
2018-07-23 20:14:32 +02:00
/** Adds an attribute */
Attributes.prototype.add = function(name, value) {
2019-08-03 15:03:33 +02:00
if (typeof this.list[name] === 'undefined') {
this.list[name] = []
2018-07-23 20:14:32 +02:00
}
2019-08-03 15:03:33 +02:00
this.list[name].push(value)
2018-07-14 16:04:39 +00:00
2019-08-03 15:03:33 +02:00
return this
}
2018-07-23 20:14:32 +02:00
/** Sets an attribute, overwriting existing value */
Attributes.prototype.set = function(name, value) {
2019-08-03 15:03:33 +02:00
this.list[name] = [value]
2019-08-03 15:03:33 +02:00
return this
}
2018-08-16 11:53:32 +02:00
/** Removes an attribute */
Attributes.prototype.remove = function(name) {
2019-08-03 15:03:33 +02:00
delete this.list[name]
2018-08-16 11:53:32 +02:00
2019-08-03 15:03:33 +02:00
return this
}
2018-08-16 11:53:32 +02:00
2018-07-23 20:14:32 +02:00
/** Retrieves an attribute */
Attributes.prototype.get = function(name) {
2019-08-03 15:03:33 +02:00
if (typeof this.list[name] === 'undefined') return false
else return this.list[name].join(' ')
}
2018-07-23 20:14:32 +02:00
/** Retrieves an attribute as array*/
Attributes.prototype.getAsArray = function(name) {
2019-08-03 15:03:33 +02:00
if (typeof this.list[name] === 'undefined') return false
else return this.list[name]
}
2018-07-23 20:14:32 +02:00
/** Returns SVG code for attributes */
Attributes.prototype.render = function() {
2019-08-03 15:03:33 +02:00
let svg = ''
2018-07-23 20:14:32 +02:00
for (let key in this.list) {
2019-08-03 15:03:33 +02:00
svg += ` ${key}="${this.list[key].join(' ')}"`
2018-07-23 20:14:32 +02:00
}
2019-08-03 15:03:33 +02:00
return svg
}
2018-07-23 20:14:32 +02:00
2018-09-06 12:03:18 +02:00
/** Returns CSS code for attributes */
Attributes.prototype.renderAsCss = function() {
2019-08-03 15:03:33 +02:00
let css = ''
2018-09-06 12:03:18 +02:00
for (let key in this.list) {
2019-08-03 15:03:33 +02:00
css += ` ${key}:${this.list[key].join(' ')};`
2018-09-06 12:03:18 +02:00
}
2019-08-03 15:03:33 +02:00
return css
}
2018-09-06 12:03:18 +02:00
2018-07-23 20:14:32 +02:00
/** Returns SVG code for attributes with a fiven prefix
* typically used for data-text*/
2019-08-03 15:03:33 +02:00
Attributes.prototype.renderIfPrefixIs = function(prefix = '') {
let svg = ''
let prefixLen = prefix.length
2018-07-23 20:14:32 +02:00
for (let key in this.list) {
if (key.substr(0, prefixLen) === prefix) {
2019-08-03 15:03:33 +02:00
svg += ` ${key.substr(prefixLen)}="${this.list[key].join(' ')}"`
2018-07-23 20:14:32 +02:00
}
}
2019-08-03 15:03:33 +02:00
return svg
}
2018-07-23 11:12:06 +00:00
2019-05-05 17:06:22 +02:00
/** Returns a props object for attributes with a fiven prefix
* typically used for data-text*/
2019-08-03 15:03:33 +02:00
Attributes.prototype.asPropsIfPrefixIs = function(prefix = '') {
let props = {}
let prefixLen = prefix.length
2019-05-05 17:06:22 +02:00
for (let key in this.list) {
if (key.substr(0, prefixLen) === prefix) {
2019-08-03 15:03:33 +02:00
let propKey = key.substr(prefixLen)
if (propKey === 'class') propKey = 'className'
props[propKey] = this.get(key)
2019-05-05 17:06:22 +02:00
}
}
2019-08-03 15:03:33 +02:00
return props
}
2019-05-05 17:06:22 +02:00
2018-08-03 14:20:28 +02:00
/** Returns a deep copy of this */
Attributes.prototype.clone = function() {
2019-08-03 15:03:33 +02:00
let clone = new Attributes()
clone.list = JSON.parse(JSON.stringify(this.list))
2018-08-03 14:20:28 +02:00
2019-08-03 15:03:33 +02:00
return clone
}
2018-08-03 14:20:28 +02:00
2019-08-03 15:03:33 +02:00
export default Attributes