1 | 'use strict';
|
2 |
|
3 | exports.type = 'full';
|
4 |
|
5 | exports.active = false;
|
6 |
|
7 | exports.description = 'adds attributes to an outer <svg> element';
|
8 |
|
9 | var ENOCLS = `Error in plugin "addAttributesToSVGElement": absent parameters.
|
10 | It should have a list of "attributes" or one "attribute".
|
11 | Config example:
|
12 |
|
13 | plugins:
|
14 | - addAttributesToSVGElement:
|
15 | attribute: "mySvg"
|
16 |
|
17 | plugins:
|
18 | - addAttributesToSVGElement:
|
19 | attributes: ["mySvg", "size-big"]
|
20 |
|
21 | plugins:
|
22 | - addAttributesToSVGElement:
|
23 | attributes:
|
24 | - focusable: false
|
25 | - data-image: icon`;
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | exports.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 | };
|