UNPKG

2.07 kBJavaScriptView Raw
1'use strict';
2
3exports.type = 'full';
4
5exports.active = false;
6
7exports.description = 'adds attributes to an outer <svg> element';
8
9var ENOCLS = `Error in plugin "addAttributesToSVGElement": absent parameters.
10It should have a list of "attributes" or one "attribute".
11Config example:
12
13plugins:
14- addAttributesToSVGElement:
15 attribute: "mySvg"
16
17plugins:
18- addAttributesToSVGElement:
19 attributes: ["mySvg", "size-big"]
20
21plugins:
22- addAttributesToSVGElement:
23 attributes:
24 - focusable: false
25 - data-image: icon`;
26
27/**
28 * Add attributes to an outer <svg> element. Example config:
29 *
30 * plugins:
31 * - addAttributesToSVGElement:
32 * attribute: 'data-icon'
33 *
34 * plugins:
35 * - addAttributesToSVGElement:
36 * attributes: ['data-icon', 'data-disabled']
37 *
38 * plugins:
39 * - addAttributesToSVGElement:
40 * attributes:
41 * - focusable: false
42 * - data-image: icon
43 *
44 * @author April Arcus
45 */
46exports.fn = function(data, params) {
47 if (!params || !(Array.isArray(params.attributes) || params.attribute)) {
48 console.error(ENOCLS);
49 return data;
50 }
51
52 var attributes = params.attributes || [ params.attribute ],
53 svg = data.content[0];
54
55 if (svg.isElem('svg')) {
56 attributes.forEach(function (attribute) {
57 if (typeof attribute === 'string') {
58 if (!svg.hasAttr(attribute)) {
59 svg.addAttr({
60 name: attribute,
61 prefix: '',
62 local: attribute
63 });
64 }
65 } else if (typeof attribute === 'object') {
66 Object.keys(attribute).forEach(function (key) {
67 if (!svg.hasAttr(key)) {
68 svg.addAttr({
69 name: key,
70 value: attribute[key],
71 prefix: '',
72 local: key
73 });
74 }
75 });
76 }
77 });
78 }
79
80 return data;
81
82};