123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- (function ($, Drupal, window) {
-
- Drupal.behaviors.tableResponsive = {
- attach(context, settings) {
- const $tables = $(context).find('table.responsive-enabled').once('tableresponsive');
- if ($tables.length) {
- const il = $tables.length;
- for (let i = 0; i < il; i++) {
- TableResponsive.tables.push(new TableResponsive($tables[i]));
- }
- }
- },
- };
-
- function TableResponsive(table) {
- this.table = table;
- this.$table = $(table);
- this.showText = Drupal.t('Show all columns');
- this.hideText = Drupal.t('Hide lower priority columns');
-
-
- this.$headers = this.$table.find('th');
-
- this.$link = $('<button type="button" class="link tableresponsive-toggle"></button>')
- .attr('title', Drupal.t('Show table cells that were hidden to make the table fit within a small screen.'))
- .on('click', $.proxy(this, 'eventhandlerToggleColumns'));
- this.$table.before($('<div class="tableresponsive-toggle-columns"></div>').append(this.$link));
-
- $(window)
- .on('resize.tableresponsive', $.proxy(this, 'eventhandlerEvaluateColumnVisibility'))
- .trigger('resize.tableresponsive');
- }
-
- $.extend(TableResponsive, {
-
- tables: [],
- });
-
- $.extend(TableResponsive.prototype, {
-
- eventhandlerEvaluateColumnVisibility(e) {
- const pegged = parseInt(this.$link.data('pegged'), 10);
- const hiddenLength = this.$headers.filter('.priority-medium:hidden, .priority-low:hidden').length;
-
-
- if (hiddenLength > 0) {
- this.$link.show().text(this.showText);
- }
-
-
-
- if (!pegged && hiddenLength === 0) {
- this.$link.hide().text(this.hideText);
- }
- },
-
- eventhandlerToggleColumns(e) {
- e.preventDefault();
- const self = this;
- const $hiddenHeaders = this.$headers.filter('.priority-medium:hidden, .priority-low:hidden');
- this.$revealedCells = this.$revealedCells || $();
-
- if ($hiddenHeaders.length > 0) {
- $hiddenHeaders.each(function (index, element) {
- const $header = $(this);
- const position = $header.prevAll('th').length;
- self.$table.find('tbody tr').each(function () {
- const $cells = $(this).find('td').eq(position);
- $cells.show();
-
- self.$revealedCells = $().add(self.$revealedCells).add($cells);
- });
- $header.show();
-
- self.$revealedCells = $().add(self.$revealedCells).add($header);
- });
- this.$link.text(this.hideText).data('pegged', 1);
- }
-
- else {
- this.$revealedCells.hide();
-
-
- this.$revealedCells.each(function (index, element) {
- const $cell = $(this);
- const properties = $cell.attr('style').split(';');
- const newProps = [];
-
-
-
-
- const match = /^display\s*\:\s*none$/;
- for (let i = 0; i < properties.length; i++) {
- const prop = properties[i];
- prop.trim();
-
- const isDisplayNone = match.exec(prop);
- if (isDisplayNone) {
- continue;
- }
- newProps.push(prop);
- }
-
- $cell.attr('style', newProps.join(';'));
- });
- this.$link.text(this.showText).data('pegged', 0);
-
- $(window).trigger('resize.tableresponsive');
- }
- },
- });
-
- Drupal.TableResponsive = TableResponsive;
- }(jQuery, Drupal, window));
|