tablefilter 0.7.13 → 0.7.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,6 +5,15 @@ import {addEvt, cancelEvt, stopEvt, targetEvt, removeEvt} from '../event';
5
5
  import {INPUT, NONE, CHECKLIST, MULTIPLE} from '../const';
6
6
  import {root} from '../root';
7
7
  import {defaultsStr, defaultsBool, defaultsArr, defaultsFn} from '../settings';
8
+ import {sanitizeHtml} from '../sanitize';
9
+
10
+ // Focusable trigger controls; non-focusable `image_html` markup is wrapped
11
+ // in a button. Matches FOCUSABLE in components/tf-popup-filter.js.
12
+ const FOCUSABLE_TRIGGER = 'button:not([disabled]), a[href], ' +
13
+ 'input:not([disabled]), select:not([disabled]), ' +
14
+ 'textarea:not([disabled]), [tabindex]:not([tabindex="-1"])';
15
+
16
+ const FILTER_TRIGGER_TEXT = 'Filter';
8
17
 
9
18
  /**
10
19
  * Pop-up filter component
@@ -136,6 +145,41 @@ export class PopupFilter extends Feature {
136
145
  * @private
137
146
  */
138
147
  this.activeFilterIdx = -1;
148
+
149
+ /**
150
+ * `<tf-popup-filter>` hosts, keyed by column index
151
+ * @type {Array}
152
+ * @private
153
+ */
154
+ this.hosts = [];
155
+
156
+ /**
157
+ * Bound element `open-change` handlers, keyed by column index
158
+ * @type {Array}
159
+ * @private
160
+ */
161
+ this.ceEvt = [];
162
+
163
+ /**
164
+ * Whether the bridge is applying an open state to an element
165
+ * @type {Boolean}
166
+ * @private
167
+ */
168
+ this.syncingCEOpen = false;
169
+
170
+ /**
171
+ * Stable handlers for emitter and document subscriptions
172
+ * @private
173
+ */
174
+ this.boundHandlers = {
175
+ mouseup: (event) => this.onMouseup(event),
176
+ containerClick: (event) => stopEvt(event),
177
+ beforeFiltering: () => this.setIconsState(),
178
+ afterFiltering: () => this.closeAll(),
179
+ cellProcessed: (tf, cellIndex) => this.changeState(cellIndex, true),
180
+ filtersRowInserted: () => this.buildIcons(),
181
+ beforeFilterInit: (tf, colIndex) => this.build(colIndex)
182
+ };
139
183
  }
140
184
 
141
185
  /**
@@ -217,14 +261,19 @@ export class PopupFilter extends Feature {
217
261
  this.buildIcons();
218
262
  }
219
263
 
264
+ // TODO(#965): remove the legacy render path once the webpack
265
+ // pipeline is retired.
266
+
220
267
  // subscribe to events
221
- this.emitter.on(['before-filtering'], () => this.setIconsState());
222
- this.emitter.on(['after-filtering'], () => this.closeAll());
223
- this.emitter.on(['cell-processed'],
224
- (tf, cellIndex) => this.changeState(cellIndex, true));
225
- this.emitter.on(['filters-row-inserted'], () => this.buildIcons());
268
+ this.emitter.on(['before-filtering'],
269
+ this.boundHandlers.beforeFiltering);
270
+ this.emitter.on(['after-filtering'],
271
+ this.boundHandlers.afterFiltering);
272
+ this.emitter.on(['cell-processed'], this.boundHandlers.cellProcessed);
273
+ this.emitter.on(['filters-row-inserted'],
274
+ this.boundHandlers.filtersRowInserted);
226
275
  this.emitter.on(['before-filter-init'],
227
- (tf, colIndex) => this.build(colIndex));
276
+ this.boundHandlers.beforeFilterInit);
228
277
 
229
278
  /** @inherited */
230
279
  this.initialized = true;
@@ -236,15 +285,50 @@ export class PopupFilter extends Feature {
236
285
  reset() {
237
286
  this.enable();
238
287
  this.init();
239
- this.buildIcons();
288
+ if (!this.tf.gridLayout) {
289
+ this.buildIcons();
290
+ }
240
291
  this.buildAll();
241
292
  }
242
293
 
294
+ /**
295
+ * Whether the feature renders through `<tf-popup-filter>`.
296
+ * A definition is accepted when it exposes the bridge methods and the
297
+ * shadow parts the bridge projects into.
298
+ * @returns {Boolean} True when the bridge renders through the element
299
+ * @private
300
+ */
301
+ canUseCE() {
302
+ const PopupFilterElement = typeof customElements === 'undefined' ?
303
+ null : customElements.get('tf-popup-filter');
304
+ if (!PopupFilterElement ||
305
+ typeof PopupFilterElement.prototype.setOpen !== 'function' ||
306
+ typeof PopupFilterElement.prototype.setPopupLabel !== 'function' ||
307
+ typeof PopupFilterElement.prototype.setLabel !== 'function' ||
308
+ typeof PopupFilterElement.prototype.setText !== 'function') {
309
+ return false;
310
+ }
311
+
312
+ let probe;
313
+ try {
314
+ probe = document.createElement('tf-popup-filter');
315
+ } catch (e) {
316
+ return false;
317
+ }
318
+ const shadow = probe.shadowRoot;
319
+ return Boolean(shadow &&
320
+ shadow.querySelector('slot[name="trigger"]') &&
321
+ shadow.querySelector('[part="trigger"]') &&
322
+ shadow.querySelector('[part="popup"]') &&
323
+ shadow.querySelector('slot[part="content-slot"]'));
324
+ }
325
+
243
326
  /**
244
327
  * Build all filters icons
245
328
  */
246
329
  buildIcons() {
247
330
  let tf = this.tf;
331
+ const useCE = this.canUseCE();
248
332
 
249
333
  // TODO: Because of the filters row generation, headers row index needs
250
334
  // adjusting: prevent useless row generation
@@ -252,19 +336,160 @@ export class PopupFilter extends Feature {
252
336
 
253
337
  tf.eachCol(
254
338
  (i) => {
255
- let icon = createElm('span', ['ci', i]);
256
- icon.innerHTML = this.iconHtml;
257
- let header = tf.getHeaderElement(i);
258
- header.appendChild(icon);
259
- addEvt(icon, 'click', (evt) => this.onClick(evt));
260
- this.fltSpans[i] = icon;
261
- this.fltIcons[i] = icon.firstChild;
339
+ if (useCE) {
340
+ this.buildIconCE(i);
341
+ } else {
342
+ this.buildIconLegacy(i);
343
+ }
262
344
  },
263
345
  // continue condition function
264
346
  (i) => tf.getFilterType(i) === NONE
265
347
  );
266
348
  }
267
349
 
350
+ /**
351
+ * Build a filter trigger using <tf-popup-filter>
352
+ * @param {Number} colIndex Column index
353
+ * @private
354
+ */
355
+ buildIconCE(colIndex) {
356
+ const existing = this.hosts[colIndex];
357
+ if (existing && existing.parentNode) {
358
+ return;
359
+ }
360
+
361
+ const host = document.createElement('tf-popup-filter');
362
+ host.className = this.placeholderCssClass;
363
+ host.setText(FILTER_TRIGGER_TEXT);
364
+ host.setLabel(FILTER_TRIGGER_TEXT);
365
+
366
+ const label = this.appendTrigger(host);
367
+ host.setPopupLabel(label || FILTER_TRIGGER_TEXT);
368
+
369
+ const handler = (event) => this.onCEOpenChange(colIndex, event);
370
+ this.ceEvt[colIndex] = handler;
371
+ addEvt(host, 'open-change', handler);
372
+
373
+ this.tf.getHeaderElement(colIndex).appendChild(host);
374
+ this.hosts[colIndex] = host;
375
+ this.fltIcons[colIndex] =
376
+ host.querySelector('[slot="trigger"] img[src]');
377
+ }
378
+
379
+ /**
380
+ * Append the sanitised `image_html` to the trigger slot
381
+ * @param {DOMElement} host `<tf-popup-filter>` host
382
+ * @returns {String} Accessible name of the projected trigger
383
+ * @private
384
+ */
385
+ appendTrigger(host) {
386
+ const wrapper = createElm('span');
387
+ wrapper.innerHTML = sanitizeHtml(this.iconHtml);
388
+ const nodes = [];
389
+ while (wrapper.firstChild) {
390
+ nodes.push(wrapper.removeChild(wrapper.firstChild));
391
+ }
392
+ if (!nodes.length) {
393
+ return '';
394
+ }
395
+
396
+ const focusable = nodes.some((node) => node.nodeType === 1 &&
397
+ (node.matches(FOCUSABLE_TRIGGER) ||
398
+ node.querySelector(FOCUSABLE_TRIGGER)));
399
+ if (!focusable) {
400
+ const button = createElm('button');
401
+ button.type = 'button';
402
+ button.slot = 'trigger';
403
+ nodes.forEach((node) => button.appendChild(node));
404
+ host.appendChild(button);
405
+ } else {
406
+ nodes.forEach((node) => {
407
+ if (node.nodeType === 1) {
408
+ node.slot = 'trigger';
409
+ host.appendChild(node);
410
+ } else if (node.nodeType === 3 && node.textContent.trim()) {
411
+ const text = createElm('span');
412
+ text.slot = 'trigger';
413
+ text.appendChild(node);
414
+ host.appendChild(text);
415
+ }
416
+ });
417
+ }
418
+ const label = this.triggerName(host);
419
+ if (!label) {
420
+ this.applyTriggerLabel(host);
421
+ }
422
+ return label;
423
+ }
424
+
425
+ /**
426
+ * Name the first slotted trigger without an accessible name
427
+ * @param {DOMElement} host `<tf-popup-filter>` host
428
+ * @private
429
+ */
430
+ applyTriggerLabel(host) {
431
+ const triggers = host.querySelectorAll('[slot="trigger"]');
432
+ for (let i = 0; i < triggers.length; i++) {
433
+ const el = triggers[i];
434
+ const target = el.matches(FOCUSABLE_TRIGGER) ?
435
+ el : el.querySelector(FOCUSABLE_TRIGGER);
436
+ if (target && !target.hasAttribute('aria-label')) {
437
+ target.setAttribute('aria-label', FILTER_TRIGGER_TEXT);
438
+ return;
439
+ }
440
+ }
441
+ }
442
+
443
+ /**
444
+ * Read the accessible name of the first slotted trigger
445
+ * @param {DOMElement} host `<tf-popup-filter>` host
446
+ * @returns {String} Trigger name, or an empty string
447
+ * @private
448
+ */
449
+ triggerName(host) {
450
+ const triggers = host.querySelectorAll('[slot="trigger"]');
451
+ for (let i = 0; i < triggers.length; i++) {
452
+ const el = triggers[i];
453
+ const label = (el.getAttribute('aria-label') || '').trim();
454
+ if (label) {
455
+ return label;
456
+ }
457
+ const text = (el.textContent || '').trim();
458
+ if (text) {
459
+ return text;
460
+ }
461
+ const image = el.querySelector('img[alt]') ||
462
+ (el.matches('img[alt]') ? el : null);
463
+ const alt = image ?
464
+ (image.getAttribute('alt') || '').trim() : '';
465
+ if (alt) {
466
+ return alt;
467
+ }
468
+ }
469
+ return '';
470
+ }
471
+
472
+ /**
473
+ * Append the legacy icon to the header
474
+ * @param {Number} colIndex Column index
475
+ * @private
476
+ */
477
+ buildIconLegacy(colIndex) {
478
+ const existing = this.fltSpans[colIndex];
479
+ if (existing && existing.parentNode) {
480
+ return;
481
+ }
482
+
483
+ let tf = this.tf;
484
+ let icon = createElm('span', ['ci', colIndex]);
485
+ icon.innerHTML = sanitizeHtml(this.iconHtml);
486
+ let header = tf.getHeaderElement(colIndex);
487
+ header.appendChild(icon);
488
+ addEvt(icon, 'click', (evt) => this.onClick(evt));
489
+ this.fltSpans[colIndex] = icon;
490
+ this.fltIcons[colIndex] = icon.firstChild;
491
+ }
492
+
268
493
  /**
269
494
  * Build all pop-up filters elements
270
495
  */
@@ -282,15 +507,23 @@ export class PopupFilter extends Feature {
282
507
  build(colIndex, div) {
283
508
  let tf = this.tf;
284
509
  let contId = `${this.prfxDiv}${tf.id}_${colIndex}`;
285
- let placeholder = createElm('div', ['class', this.placeholderCssClass]);
286
510
  let cont = div ||
287
511
  createElm('div', ['id', contId], ['class', this.containerCssClass]);
288
512
  tf.externalFltIds[colIndex] = cont.id;
513
+ addEvt(cont, 'click', this.boundHandlers.containerClick);
514
+
515
+ const host = this.hosts[colIndex];
516
+ if (host) {
517
+ host.appendChild(cont);
518
+ this.fltElms[colIndex] = cont;
519
+ return;
520
+ }
521
+
522
+ let placeholder = createElm('div', ['class', this.placeholderCssClass]);
289
523
  placeholder.appendChild(cont);
290
524
 
291
525
  let header = tf.getHeaderElement(colIndex);
292
526
  header.insertBefore(placeholder, header.firstChild);
293
- addEvt(cont, 'click', (evt) => stopEvt(evt));
294
527
  this.fltElms[colIndex] = cont;
295
528
  }
296
529
 
@@ -312,18 +545,32 @@ export class PopupFilter extends Feature {
312
545
  */
313
546
  open(colIndex) {
314
547
  let tf = this.tf,
315
- container = this.fltElms[colIndex];
548
+ container = this.fltElms[colIndex],
549
+ host = this.hosts[colIndex];
316
550
 
551
+ if (!container) {
552
+ return;
553
+ }
317
554
  this.onBeforeOpen(this, container, colIndex);
318
555
 
319
556
  container.style.display = 'block';
557
+ if (host) {
558
+ this.setCEOpen(host, true);
559
+ if (this.adjustToContainer) {
560
+ let header = tf.getHeaderElement(colIndex);
561
+ host.style.width = parseInt(header.clientWidth * 0.95, 10) +
562
+ 'px';
563
+ }
564
+ }
320
565
  this.activeFilterIdx = colIndex;
321
- addEvt(root, 'mouseup', (evt) => this.onMouseup(evt));
322
566
 
323
- if (tf.getFilterType(colIndex) === INPUT) {
324
- let flt = tf.getFilterElement(colIndex);
325
- if (flt) {
326
- flt.focus();
567
+ if (!host) {
568
+ addEvt(root, 'mouseup', this.boundHandlers.mouseup);
569
+ if (tf.getFilterType(colIndex) === INPUT) {
570
+ let flt = tf.getFilterElement(colIndex);
571
+ if (flt) {
572
+ flt.focus();
573
+ }
327
574
  }
328
575
  }
329
576
 
@@ -335,25 +582,69 @@ export class PopupFilter extends Feature {
335
582
  * @param {Number} colIndex Column index
336
583
  */
337
584
  close(colIndex) {
338
- let container = this.fltElms[colIndex];
585
+ let container = this.fltElms[colIndex],
586
+ host = this.hosts[colIndex];
339
587
 
588
+ if (!container) {
589
+ return;
590
+ }
340
591
  this.onBeforeClose(this, container, colIndex);
341
592
 
593
+ if (host) {
594
+ this.setCEOpen(host, false);
595
+ }
342
596
  container.style.display = NONE;
343
597
  if (this.activeFilterIdx === colIndex) {
344
598
  this.activeFilterIdx = -1;
345
599
  }
346
- removeEvt(root, 'mouseup', (evt) => this.onMouseup(evt));
600
+ if (!host) {
601
+ removeEvt(root, 'mouseup', this.boundHandlers.mouseup);
602
+ }
347
603
 
348
604
  this.onAfterClose(this, container, colIndex);
349
605
  }
350
606
 
607
+ /**
608
+ * Apply an open state to `<tf-popup-filter>` without replaying the
609
+ * bridge callbacks for its `open-change` event
610
+ * @param {DOMElement} host `<tf-popup-filter>` host
611
+ * @param {Boolean} open Whether the popup is open
612
+ * @private
613
+ */
614
+ setCEOpen(host, open) {
615
+ this.syncingCEOpen = true;
616
+ host.setOpen(open);
617
+ this.syncingCEOpen = false;
618
+ }
619
+
620
+ /**
621
+ * Drive the bridge from `<tf-popup-filter>` state changes
622
+ * @param {Number} colIndex Column index
623
+ * @param {CustomEvent} event Element `open-change` event
624
+ * @private
625
+ */
626
+ onCEOpenChange(colIndex, event) {
627
+ if (this.syncingCEOpen || !event.detail) {
628
+ return;
629
+ }
630
+ if (event.detail.open) {
631
+ this.closeAll(colIndex);
632
+ this.open(colIndex);
633
+ } else {
634
+ this.close(colIndex);
635
+ }
636
+ }
637
+
351
638
  /**
352
639
  * Check if popup filter for specified column is open
353
640
  * @param {Number} colIndex Column index
354
641
  * @returns {Boolean}
355
642
  */
356
643
  isOpen(colIndex) {
644
+ const host = this.hosts[colIndex];
645
+ if (host) {
646
+ return host.hasAttribute('open');
647
+ }
357
648
  return this.fltElms[colIndex].style.display === 'block';
358
649
  }
359
650
 
@@ -415,23 +706,36 @@ export class PopupFilter extends Feature {
415
706
  this.filtersCache = [];
416
707
  for (let i = 0; i < this.fltElms.length; i++) {
417
708
  let container = this.fltElms[i],
418
- placeholder = container.parentNode,
709
+ host = this.hosts[i],
710
+ placeholder = container && container.parentNode,
419
711
  icon = this.fltSpans[i],
420
712
  iconImg = this.fltIcons[i];
421
713
  if (container) {
422
- removeElm(container);
714
+ removeEvt(container, 'click',
715
+ this.boundHandlers.containerClick);
716
+ if (container.parentNode) {
717
+ removeElm(container);
718
+ }
423
719
  this.filtersCache[i] = container;
424
720
  }
425
721
  container = null;
426
- if (placeholder) {
722
+ if (placeholder && placeholder !== host && placeholder.parentNode) {
427
723
  removeElm(placeholder);
428
724
  }
429
725
  placeholder = null;
430
- if (icon) {
726
+ if (host) {
727
+ if (this.ceEvt[i]) {
728
+ removeEvt(host, 'open-change', this.ceEvt[i]);
729
+ }
730
+ if (host.parentNode) {
731
+ removeElm(host);
732
+ }
733
+ }
734
+ if (icon && icon.parentNode) {
431
735
  removeElm(icon);
432
736
  }
433
737
  icon = null;
434
- if (iconImg) {
738
+ if (iconImg && iconImg.parentNode) {
435
739
  removeElm(iconImg);
436
740
  }
437
741
  iconImg = null;
@@ -439,18 +743,25 @@ export class PopupFilter extends Feature {
439
743
  this.fltElms = [];
440
744
  this.fltSpans = [];
441
745
  this.fltIcons = [];
746
+ this.hosts = [];
747
+ this.ceEvt = [];
748
+ this.syncingCEOpen = false;
749
+ this.activeFilterIdx = -1;
750
+ removeEvt(root, 'mouseup', this.boundHandlers.mouseup);
442
751
 
443
752
  // TODO: expose an API to handle external filter IDs
444
753
  this.tf.externalFltIds = [];
445
754
 
446
755
  // unsubscribe to events
447
- this.emitter.off(['before-filtering'], () => this.setIconsState());
448
- this.emitter.off(['after-filtering'], () => this.closeAll());
449
- this.emitter.off(['cell-processed'],
450
- (tf, cellIndex) => this.changeState(cellIndex, true));
451
- this.emitter.off(['filters-row-inserted'], () => this.buildIcons());
756
+ this.emitter.off(['before-filtering'],
757
+ this.boundHandlers.beforeFiltering);
758
+ this.emitter.off(['after-filtering'],
759
+ this.boundHandlers.afterFiltering);
760
+ this.emitter.off(['cell-processed'], this.boundHandlers.cellProcessed);
761
+ this.emitter.off(['filters-row-inserted'],
762
+ this.boundHandlers.filtersRowInserted);
452
763
  this.emitter.off(['before-filter-init'],
453
- (tf, colIndex) => this.build(colIndex));
764
+ this.boundHandlers.beforeFilterInit);
454
765
 
455
766
  this.initialized = false;
456
767
  }