2018-08-11 19:17:39 +02:00
|
|
|
function Attributes() {
|
2018-07-23 20:14:32 +02:00
|
|
|
this.list = {};
|
|
|
|
}
|
2018-07-21 18:53:03 +02:00
|
|
|
|
2018-07-23 20:14:32 +02:00
|
|
|
/** Adds an attribute */
|
2018-08-05 18:19:48 +02:00
|
|
|
Attributes.prototype.add = function(name, value) {
|
2018-07-23 20:14:32 +02:00
|
|
|
if (typeof this.list[name] === "undefined") {
|
|
|
|
this.list[name] = [];
|
|
|
|
}
|
|
|
|
this.list[name].push(value);
|
2018-07-14 16:04:39 +00:00
|
|
|
|
2018-07-23 20:14:32 +02:00
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2018-08-05 15:52:37 +02:00
|
|
|
/** Sets an attribute, overwriting existing value */
|
2018-08-05 18:19:48 +02:00
|
|
|
Attributes.prototype.set = function(name, value) {
|
2018-08-05 15:52:37 +02:00
|
|
|
this.list[name] = [value];
|
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2018-08-16 11:53:32 +02:00
|
|
|
/** Removes an attribute */
|
|
|
|
Attributes.prototype.remove = function(name) {
|
|
|
|
delete this.list[name];
|
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2018-07-23 20:14:32 +02:00
|
|
|
/** Retrieves an attribute */
|
2018-08-05 18:19:48 +02:00
|
|
|
Attributes.prototype.get = function(name) {
|
2018-07-23 20:14:32 +02:00
|
|
|
if (typeof this.list[name] === "undefined") return false;
|
|
|
|
else return this.list[name].join(" ");
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Returns SVG code for attributes */
|
2018-08-05 18:19:48 +02:00
|
|
|
Attributes.prototype.render = function() {
|
2018-07-23 20:14:32 +02:00
|
|
|
let svg = "";
|
|
|
|
for (let key in this.list) {
|
|
|
|
svg += ` ${key}="${this.list[key].join(" ")}"`;
|
|
|
|
}
|
2018-07-21 18:53:03 +02:00
|
|
|
|
2018-07-23 20:14:32 +02:00
|
|
|
return svg;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Returns SVG code for attributes with a fiven prefix
|
|
|
|
* typically used for data-text*/
|
2018-08-05 18:19:48 +02:00
|
|
|
Attributes.prototype.renderIfPrefixIs = function(prefix = "") {
|
2018-07-23 20:14:32 +02:00
|
|
|
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(" ")}"`;
|
|
|
|
}
|
|
|
|
}
|
2018-07-23 17:35:06 +00:00
|
|
|
|
2018-07-23 20:14:32 +02:00
|
|
|
return svg;
|
|
|
|
};
|
2018-07-23 11:12:06 +00:00
|
|
|
|
2018-08-03 14:20:28 +02:00
|
|
|
/** Returns a deep copy of this */
|
2018-08-05 18:19:48 +02:00
|
|
|
Attributes.prototype.clone = function() {
|
|
|
|
let clone = new Attributes();
|
2018-08-03 14:20:28 +02:00
|
|
|
clone.list = JSON.parse(JSON.stringify(this.list));
|
|
|
|
|
|
|
|
return clone;
|
|
|
|
};
|
|
|
|
|
2018-08-05 18:19:48 +02:00
|
|
|
export default Attributes;
|