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.

80 lines
1.9 KiB

4 years ago
  1. 'use strict';
  2. exports.type = 'perItem';
  3. exports.active = false;
  4. exports.description = 'removes arbitrary elements by ID or className (disabled by default)';
  5. exports.params = {
  6. id: [],
  7. class: []
  8. };
  9. /**
  10. * Remove arbitrary SVG elements by ID or className.
  11. *
  12. * @param id
  13. * examples:
  14. *
  15. * > single: remove element with ID of `elementID`
  16. * ---
  17. * removeElementsByAttr:
  18. * id: 'elementID'
  19. *
  20. * > list: remove multiple elements by ID
  21. * ---
  22. * removeElementsByAttr:
  23. * id:
  24. * - 'elementID'
  25. * - 'anotherID'
  26. *
  27. * @param class
  28. * examples:
  29. *
  30. * > single: remove all elements with class of `elementClass`
  31. * ---
  32. * removeElementsByAttr:
  33. * class: 'elementClass'
  34. *
  35. * > list: remove all elements with class of `elementClass` or `anotherClass`
  36. * ---
  37. * removeElementsByAttr:
  38. * class:
  39. * - 'elementClass'
  40. * - 'anotherClass'
  41. *
  42. * @param {Object} item current iteration item
  43. * @param {Object} params plugin params
  44. * @return {Boolean} if false, item will be filtered out
  45. *
  46. * @author Eli Dupuis (@elidupuis)
  47. */
  48. exports.fn = function(item, params) {
  49. var elemId, elemClass;
  50. // wrap params in an array if not already
  51. ['id', 'class'].forEach(function(key) {
  52. if (!Array.isArray(params[key])) {
  53. params[key] = [ params[key] ];
  54. }
  55. });
  56. // abort if current item is no an element
  57. if (!item.isElem()) {
  58. return;
  59. }
  60. // remove element if it's `id` matches configured `id` params
  61. elemId = item.attr('id');
  62. if (elemId) {
  63. return params.id.indexOf(elemId.value) === -1;
  64. }
  65. // remove element if it's `class` contains any of the configured `class` params
  66. elemClass = item.attr('class');
  67. if (elemClass) {
  68. var hasClassRegex = new RegExp(params.class.join('|'));
  69. return !hasClassRegex.test(elemClass.value);
  70. }
  71. };