2018-07-23 11:44:34 +00:00
|
|
|
function attributes(init = false) {
|
|
|
|
if (init) {
|
2018-07-21 18:53:03 +02:00
|
|
|
for (let key in init) {
|
|
|
|
let val = init[key];
|
|
|
|
this.add(key, val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-14 16:04:39 +00:00
|
|
|
/** Adds an attribute */
|
2018-07-23 11:44:34 +00:00
|
|
|
this.prototype.add = function(name, value) {
|
|
|
|
if (typeof this.list[name] === "undefined") {
|
2018-07-19 09:29:25 +00:00
|
|
|
this.list[name] = [];
|
|
|
|
}
|
|
|
|
this.list[name].push(value);
|
2018-07-14 16:04:39 +00:00
|
|
|
|
|
|
|
return this;
|
2018-07-23 11:44:34 +00:00
|
|
|
};
|
2018-07-14 16:04:39 +00:00
|
|
|
|
2018-07-19 09:29:25 +00:00
|
|
|
/** Retrieves an attribute */
|
2018-07-23 11:44:34 +00:00
|
|
|
this.prototype.get = function(name) {
|
|
|
|
if (typeof this.list[name] === "undefined") return false;
|
|
|
|
else return this.list[name].join(" ");
|
|
|
|
};
|
2018-07-19 09:29:25 +00:00
|
|
|
|
2018-07-14 16:04:39 +00:00
|
|
|
/** Returns SVG code for attributes */
|
2018-07-23 11:44:34 +00:00
|
|
|
this.prototype.render = function() {
|
|
|
|
let svg = "";
|
2018-07-19 09:29:25 +00:00
|
|
|
for (let key in this.list) {
|
2018-07-23 11:44:34 +00:00
|
|
|
svg += ` ${key}="${this.list[key].join(" ")}"`;
|
2018-07-14 16:04:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return svg;
|
2018-07-23 11:44:34 +00:00
|
|
|
};
|
2018-07-21 18:53:03 +02:00
|
|
|
|
|
|
|
/** Returns SVG code for attributes with a fiven prefix
|
|
|
|
* typically used for data-text*/
|
2018-07-23 11:44:34 +00:00
|
|
|
this.prototype.renderIfPrefixIs = function(prefix = "") {
|
|
|
|
let svg = "";
|
2018-07-21 18:53:03 +02:00
|
|
|
let prefixLen = prefix.length;
|
|
|
|
for (let key in this.list) {
|
2018-07-23 11:44:34 +00:00
|
|
|
if (key.substr(0, prefixLen) === prefix) {
|
|
|
|
svg += ` ${key.substr(prefixLen)}="${this.list[key].join(" ")}"`;
|
2018-07-21 18:53:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return svg;
|
2018-07-23 11:44:34 +00:00
|
|
|
};
|
2018-07-23 17:35:06 +00:00
|
|
|
|
|
|
|
return this;
|
2018-07-14 16:04:39 +00:00
|
|
|
}
|
2018-07-23 11:12:06 +00:00
|
|
|
|
|
|
|
export default attributes;
|