2018-05-16 00:53:27 +00:00
|
|
|
/* eslint-disable no-var */
|
|
|
|
/* globals $, jQuery */
|
2013-02-20 15:34:42 +00:00
|
|
|
/**
|
|
|
|
* jQuery module to work with SVG.
|
|
|
|
*
|
|
|
|
* Licensed under the MIT License
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Dependencies:
|
|
|
|
// 1) jquery
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
(function () {
|
|
|
|
'use strict';
|
2013-02-20 15:34:42 +00:00
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
// This fixes $(...).attr() to work as expected with SVG elements.
|
|
|
|
// Does not currently use *AttributeNS() since we rarely need that.
|
2013-02-20 15:34:42 +00:00
|
|
|
|
2018-05-18 04:40:50 +00:00
|
|
|
// See https://api.jquery.com/attr/ for basic documentation of .attr()
|
2013-02-20 15:34:42 +00:00
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
// Additional functionality:
|
|
|
|
// - When getting attributes, a string that's a number is return as type number.
|
|
|
|
// - If an array is supplied as first parameter, multiple values are returned
|
|
|
|
// as an object with values for each given attributes
|
2013-02-20 15:34:42 +00:00
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
var proxied = jQuery.fn.attr,
|
2018-05-18 06:23:36 +00:00
|
|
|
// TODO use NS.SVG instead
|
|
|
|
svgns = 'http://www.w3.org/2000/svg';
|
2018-05-16 00:53:27 +00:00
|
|
|
jQuery.fn.attr = function (key, value) {
|
2018-05-18 06:23:36 +00:00
|
|
|
var i, attr;
|
|
|
|
var len = this.length;
|
|
|
|
if (!len) { return proxied.apply(this, arguments); }
|
|
|
|
for (i = 0; i < len; ++i) {
|
|
|
|
var elem = this[i];
|
|
|
|
// set/get SVG attribute
|
|
|
|
if (elem.namespaceURI === svgns) {
|
|
|
|
// Setting attribute
|
|
|
|
if (value !== undefined) {
|
|
|
|
elem.setAttribute(key, value);
|
|
|
|
} else if ($.isArray(key)) {
|
|
|
|
// Getting attributes from array
|
|
|
|
var j = key.length, obj = {};
|
2013-02-20 15:34:42 +00:00
|
|
|
|
2018-05-18 06:23:36 +00:00
|
|
|
while (j--) {
|
|
|
|
var aname = key[j];
|
|
|
|
attr = elem.getAttribute(aname);
|
|
|
|
// This returns a number when appropriate
|
|
|
|
if (attr || attr === '0') {
|
|
|
|
attr = isNaN(attr) ? attr : (attr - 0);
|
|
|
|
}
|
|
|
|
obj[aname] = attr;
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
if (typeof key === 'object') {
|
|
|
|
// Setting attributes form object
|
|
|
|
var v;
|
|
|
|
for (v in key) {
|
|
|
|
elem.setAttribute(v, key[v]);
|
|
|
|
}
|
|
|
|
// Getting attribute
|
|
|
|
} else {
|
|
|
|
attr = elem.getAttribute(key);
|
|
|
|
if (attr || attr === '0') {
|
|
|
|
attr = isNaN(attr) ? attr : (attr - 0);
|
|
|
|
}
|
|
|
|
return attr;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return proxied.apply(this, arguments);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this;
|
2018-05-16 00:53:27 +00:00
|
|
|
};
|
2013-02-20 15:34:42 +00:00
|
|
|
}());
|