Projet

Général

Profil

0001-js-make-table-widgets-reponsive-41734.patch

Thomas Jund, 12 octobre 2020 17:23

Télécharger (2,54 ko)

Voir les différences:

Subject: [PATCH] js: make table widgets reponsive (#41734)

 wcs/qommon/static/js/qommon.forms.js | 52 ++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)
wcs/qommon/static/js/qommon.forms.js
75 75
  return weight;
76 76
}
77 77

  
78
/* Make table widget responsive
79
 *    new Responsive_table_widget(table)
80
 */
81
const Responsive_table_widget = function (table) {
82
    'use strict';
83
    this.table = table;
84
    this.col_headers = table.querySelectorAll('thead th');
85
    this.col_headers_text = [];
86
    this.body_rows = table.querySelectorAll('tbody tr');
87
    this.parent = table.parentElement;
88
    this.init();
89
};
90
Responsive_table_widget.prototype.storeHeaders = function () {
91
    'use strict';
92
    let _self = this;
93
    $(this.col_headers).each(function (idx, header) {
94
        _self.col_headers_text.push(header.innerText);
95
    });
96
    $(this.body_rows).each(function (idx, tr) {
97
        $(tr.querySelectorAll('td')).each( function (id, td) {
98
            td.dataset.colHeader = _self.col_headers_text[id];
99
        });
100
    });
101
};
102
Responsive_table_widget.prototype.fit = function () {
103
    'use strict';
104
    this.table.style.width = "auto";
105
    if (this.parent.clientWidth < this.table.clientWidth)
106
        this.table.style.width = "100%";
107
};
108
Responsive_table_widget.prototype.init = function () {
109
    'use strict';
110
    let _self = this;
111
    this.table.classList.add('responsive-tableWidget');
112
    this.storeHeaders();
113
    this.fit();
114
    // debounce resize event
115
    let callback;
116
    window.addEventListener("resize", function () {
117
        clearTimeout(callback);
118
        callback = setTimeout( function () {
119
            _self.fit.call(_self)
120
        }, 200);
121
    });
122
};
123

  
78 124
$(function() {
79 125
  var autosave_timeout_id = null;
80 126
  if ($('form[data-has-draft]').length == 1) {
......
372 418
      $(elem).select2('data', {id: $(elem).data('value'), text: $(elem).data('initial-display-value')});
373 419
    }
374 420
  });
421

  
422
  /* Make table widgets responsive */
423
  $('.TableWidget, .SingleSelectTableWidget, .TableListRowsWidget').each(function (i, elem) {
424
    const table = elem.querySelector('table');
425
    new Responsive_table_widget(table);
426
  });
375 427
});
376
-