UNPKG

1.58 kBJavaScriptView Raw
1(function (global, factory) {
2 if (typeof define === "function" && define.amd) {
3 define(["exports"], factory);
4 } else if (typeof exports !== "undefined") {
5 factory(exports);
6 } else {
7 var mod = {
8 exports: {}
9 };
10 factory(mod.exports);
11 global.svgToggleClass = mod.exports;
12 }
13})(this, function (_exports) {
14 "use strict";
15
16 Object.defineProperty(_exports, "__esModule", {
17 value: true
18 });
19 _exports.default = void 0;
20
21 function _defineProperty(obj, key, value) {
22 if (key in obj) {
23 Object.defineProperty(obj, key, {
24 value: value,
25 enumerable: true,
26 configurable: true,
27 writable: true
28 });
29 } else {
30 obj[key] = value;
31 }
32
33 return obj;
34 }
35 /**
36 * Copyright IBM Corp. 2016, 2018
37 *
38 * This source code is licensed under the Apache-2.0 license found in the
39 * LICENSE file in the root directory of this source tree.
40 */
41
42
43 function svgToggleClass(svg, name, forceAdd) {
44 var list = svg.getAttribute('class').trim().split(/\s+/);
45 var uniqueList = Object.keys(list.reduce(function (o, item) {
46 return Object.assign(o, _defineProperty({}, item, 1));
47 }, {}));
48 var index = uniqueList.indexOf(name);
49 var found = index >= 0;
50 var add = forceAdd === undefined ? !found : forceAdd;
51
52 if (found === !add) {
53 if (add) {
54 uniqueList.push(name);
55 } else {
56 uniqueList.splice(index, 1);
57 }
58
59 svg.setAttribute('class', uniqueList.join(' '));
60 }
61 }
62
63 var _default = svgToggleClass;
64 _exports.default = _default;
65});
\No newline at end of file