|
| 1 | +/** |
| 2 | + * source by `component-classes` |
| 3 | + * https://github.com/component/classes.git |
| 4 | + */ |
| 5 | + |
| 6 | +import { indexOf } from 'lodash-es'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Whitespace regexp. |
| 10 | + */ |
| 11 | +const re = /\s+/; |
| 12 | + |
| 13 | +export class ClassList { |
| 14 | + el: Element; |
| 15 | + list: DOMTokenList; |
| 16 | + |
| 17 | + constructor(el: Element) { |
| 18 | + if (!el || !el.nodeType) { |
| 19 | + throw new Error('A DOM element reference is required'); |
| 20 | + } |
| 21 | + this.el = el; |
| 22 | + this.list = el.classList; |
| 23 | + } |
| 24 | + |
| 25 | + array() { |
| 26 | + const className = this.el.getAttribute('class') || ''; |
| 27 | + const str = className.replace(/^\s+|\s+$/g, ''); |
| 28 | + const arr = str.split(re); |
| 29 | + if ('' === arr[0]) arr.shift(); |
| 30 | + return arr; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Add class `name` if not already present. |
| 35 | + * |
| 36 | + * @param {String} name |
| 37 | + * @return {ClassList} |
| 38 | + * @api public |
| 39 | + */ |
| 40 | + add(name: string): ClassList { |
| 41 | + // classList |
| 42 | + if (this.list) { |
| 43 | + this.list.add(name); |
| 44 | + return this; |
| 45 | + } |
| 46 | + |
| 47 | + // fallback |
| 48 | + const arr = this.array(); |
| 49 | + const i = indexOf(arr, name); |
| 50 | + if (!~i) arr.push(name); |
| 51 | + this.el.className = arr.join(' '); |
| 52 | + return this; |
| 53 | + } |
| 54 | + /** |
| 55 | + * Remove class `name` when present, or |
| 56 | + * pass a regular expression to remove |
| 57 | + * any which match. |
| 58 | + * |
| 59 | + * @param {String|RegExp} name |
| 60 | + * @return {ClassList} |
| 61 | + * @api public |
| 62 | + */ |
| 63 | + remove(name: string | RegExp): ClassList { |
| 64 | + if ('[object RegExp]' === toString.call(name)) { |
| 65 | + return this._removeMatching(name as RegExp); |
| 66 | + } |
| 67 | + |
| 68 | + // classList |
| 69 | + if (this.list) { |
| 70 | + this.list.remove(name as string); |
| 71 | + return this; |
| 72 | + } |
| 73 | + |
| 74 | + // fallback |
| 75 | + const arr = this.array(); |
| 76 | + const i = indexOf(arr, name); |
| 77 | + if (~i) arr.splice(i, 1); |
| 78 | + this.el.className = arr.join(' '); |
| 79 | + return this; |
| 80 | + } |
| 81 | + /** |
| 82 | + * Remove all classes matching `re`. |
| 83 | + * |
| 84 | + * @param {RegExp} re |
| 85 | + * @return {ClassList} |
| 86 | + * @api private |
| 87 | + */ |
| 88 | + _removeMatching(re: RegExp): ClassList { |
| 89 | + const arr = this.array(); |
| 90 | + for (let i = 0; i < arr.length; i++) { |
| 91 | + if (re.test(arr[i])) { |
| 92 | + this.remove(arr[i]); |
| 93 | + } |
| 94 | + } |
| 95 | + return this; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Toggle class `name`, can force state via `force`. |
| 100 | + * |
| 101 | + * For browsers that support classList, but do not support `force` yet, |
| 102 | + * the mistake will be detected and corrected. |
| 103 | + * |
| 104 | + * @param {String} name |
| 105 | + * @param {Boolean} force |
| 106 | + * @return {ClassList} |
| 107 | + * @api public |
| 108 | + */ |
| 109 | + toggle(name: string, force: boolean): ClassList { |
| 110 | + // classList |
| 111 | + if (this.list) { |
| 112 | + if ('undefined' !== typeof force) { |
| 113 | + if (force !== this.list.toggle(name, force)) { |
| 114 | + this.list.toggle(name); // toggle again to correct |
| 115 | + } |
| 116 | + } else { |
| 117 | + this.list.toggle(name); |
| 118 | + } |
| 119 | + return this; |
| 120 | + } |
| 121 | + |
| 122 | + // fallback |
| 123 | + if ('undefined' !== typeof force) { |
| 124 | + if (!force) { |
| 125 | + this.remove(name); |
| 126 | + } else { |
| 127 | + this.add(name); |
| 128 | + } |
| 129 | + } else { |
| 130 | + if (this.has(name)) { |
| 131 | + this.remove(name); |
| 132 | + } else { |
| 133 | + this.add(name); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + return this; |
| 138 | + } |
| 139 | + /** |
| 140 | + * Check if class `name` is present. |
| 141 | + * |
| 142 | + * @param {String} name |
| 143 | + * @api public |
| 144 | + */ |
| 145 | + has(name: string) { |
| 146 | + return this.list ? this.list.contains(name) : !!~indexOf(this.array(), name); |
| 147 | + } |
| 148 | + /** |
| 149 | + * Check if class `name` is present. |
| 150 | + * |
| 151 | + * @param {String} name |
| 152 | + * @api public |
| 153 | + */ |
| 154 | + contains(name: string) { |
| 155 | + return this.has(name); |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +/** |
| 160 | + * Wrap `el` in a `ClassList`. |
| 161 | + * |
| 162 | + * @param {Element} el |
| 163 | + * @return {ClassList} |
| 164 | + * @api public |
| 165 | + */ |
| 166 | +export default function(el: Element): ClassList { |
| 167 | + return new ClassList(el); |
| 168 | +} |
0 commit comments