You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

addAttributesToSVGElement.js 2.0KB

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