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