comparison core/misc/tableresponsive.js @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
1 /**
2 * DO NOT EDIT THIS FILE.
3 * See the following change record for more information,
4 * https://www.drupal.org/node/2815083
5 * @preserve
6 **/
7
8 (function ($, Drupal, window) {
9 Drupal.behaviors.tableResponsive = {
10 attach: function attach(context, settings) {
11 var $tables = $(context).find('table.responsive-enabled').once('tableresponsive');
12 if ($tables.length) {
13 var il = $tables.length;
14 for (var i = 0; i < il; i++) {
15 TableResponsive.tables.push(new TableResponsive($tables[i]));
16 }
17 }
18 }
19 };
20
21 function TableResponsive(table) {
22 this.table = table;
23 this.$table = $(table);
24 this.showText = Drupal.t('Show all columns');
25 this.hideText = Drupal.t('Hide lower priority columns');
26
27 this.$headers = this.$table.find('th');
28
29 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'));
30
31 this.$table.before($('<div class="tableresponsive-toggle-columns"></div>').append(this.$link));
32
33 $(window).on('resize.tableresponsive', $.proxy(this, 'eventhandlerEvaluateColumnVisibility')).trigger('resize.tableresponsive');
34 }
35
36 $.extend(TableResponsive, {
37 tables: []
38 });
39
40 $.extend(TableResponsive.prototype, {
41 eventhandlerEvaluateColumnVisibility: function eventhandlerEvaluateColumnVisibility(e) {
42 var pegged = parseInt(this.$link.data('pegged'), 10);
43 var hiddenLength = this.$headers.filter('.priority-medium:hidden, .priority-low:hidden').length;
44
45 if (hiddenLength > 0) {
46 this.$link.show().text(this.showText);
47 }
48
49 if (!pegged && hiddenLength === 0) {
50 this.$link.hide().text(this.hideText);
51 }
52 },
53 eventhandlerToggleColumns: function eventhandlerToggleColumns(e) {
54 e.preventDefault();
55 var self = this;
56 var $hiddenHeaders = this.$headers.filter('.priority-medium:hidden, .priority-low:hidden');
57 this.$revealedCells = this.$revealedCells || $();
58
59 if ($hiddenHeaders.length > 0) {
60 $hiddenHeaders.each(function (index, element) {
61 var $header = $(this);
62 var position = $header.prevAll('th').length;
63 self.$table.find('tbody tr').each(function () {
64 var $cells = $(this).find('td').eq(position);
65 $cells.show();
66
67 self.$revealedCells = $().add(self.$revealedCells).add($cells);
68 });
69 $header.show();
70
71 self.$revealedCells = $().add(self.$revealedCells).add($header);
72 });
73 this.$link.text(this.hideText).data('pegged', 1);
74 } else {
75 this.$revealedCells.hide();
76
77 this.$revealedCells.each(function (index, element) {
78 var $cell = $(this);
79 var properties = $cell.attr('style').split(';');
80 var newProps = [];
81
82 var match = /^display\s*:\s*none$/;
83 for (var i = 0; i < properties.length; i++) {
84 var prop = properties[i];
85 prop.trim();
86
87 var isDisplayNone = match.exec(prop);
88 if (isDisplayNone) {
89 continue;
90 }
91 newProps.push(prop);
92 }
93
94 $cell.attr('style', newProps.join(';'));
95 });
96 this.$link.text(this.showText).data('pegged', 0);
97
98 $(window).trigger('resize.tableresponsive');
99 }
100 }
101 });
102
103 Drupal.TableResponsive = TableResponsive;
104 })(jQuery, Drupal, window);