addClassesToSVGElement.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. 'use strict';
  2. exports.type = 'full';
  3. exports.active = false;
  4. exports.description = 'adds classnames to an outer <svg> element';
  5. var ENOCLS = `Error in plugin "addClassesToSVGElement": absent parameters.
  6. It should have a list of classes in "classNames" or one "className".
  7. Config example:
  8. plugins:
  9. - addClassesToSVGElement:
  10. className: "mySvg"
  11. plugins:
  12. - addClassesToSVGElement:
  13. classNames: ["mySvg", "size-big"]
  14. `;
  15. /**
  16. * Add classnames to an outer <svg> element. Example config:
  17. *
  18. * plugins:
  19. * - addClassesToSVGElement:
  20. * className: 'mySvg'
  21. *
  22. * plugins:
  23. * - addClassesToSVGElement:
  24. * classNames: ['mySvg', 'size-big']
  25. *
  26. * @author April Arcus
  27. */
  28. exports.fn = function(data, params) {
  29. if (!params || !(Array.isArray(params.classNames) && params.classNames.some(String) || params.className)) {
  30. console.error(ENOCLS);
  31. return data;
  32. }
  33. var classNames = params.classNames || [ params.className ],
  34. svg = data.content[0];
  35. if (svg.isElem('svg')) {
  36. svg.class.add.apply(svg.class, classNames);
  37. }
  38. return data;
  39. };