annotate core/misc/tableselect.js @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 1fec387a4317
children
rev   line source
Chris@0 1 /**
Chris@0 2 * DO NOT EDIT THIS FILE.
Chris@0 3 * See the following change record for more information,
Chris@0 4 * https://www.drupal.org/node/2815083
Chris@0 5 * @preserve
Chris@0 6 **/
Chris@0 7
Chris@0 8 (function ($, Drupal) {
Chris@0 9 Drupal.behaviors.tableSelect = {
Chris@0 10 attach: function attach(context, settings) {
Chris@0 11 $(context).find('th.select-all').closest('table').once('table-select').each(Drupal.tableSelect);
Chris@0 12 }
Chris@0 13 };
Chris@0 14
Chris@0 15 Drupal.tableSelect = function () {
Chris@0 16 if ($(this).find('td input[type="checkbox"]').length === 0) {
Chris@0 17 return;
Chris@0 18 }
Chris@0 19
Chris@0 20 var table = this;
Chris@0 21 var checkboxes = void 0;
Chris@0 22 var lastChecked = void 0;
Chris@0 23 var $table = $(table);
Chris@0 24 var strings = {
Chris@0 25 selectAll: Drupal.t('Select all rows in this table'),
Chris@0 26 selectNone: Drupal.t('Deselect all rows in this table')
Chris@0 27 };
Chris@0 28 var updateSelectAll = function updateSelectAll(state) {
Chris@0 29 $table.prev('table.sticky-header').addBack().find('th.select-all input[type="checkbox"]').each(function () {
Chris@0 30 var $checkbox = $(this);
Chris@0 31 var stateChanged = $checkbox.prop('checked') !== state;
Chris@0 32
Chris@0 33 $checkbox.attr('title', state ? strings.selectNone : strings.selectAll);
Chris@0 34
Chris@0 35 if (stateChanged) {
Chris@0 36 $checkbox.prop('checked', state).trigger('change');
Chris@0 37 }
Chris@0 38 });
Chris@0 39 };
Chris@0 40
Chris@0 41 $table.find('th.select-all').prepend($('<input type="checkbox" class="form-checkbox" />').attr('title', strings.selectAll)).on('click', function (event) {
Chris@0 42 if ($(event.target).is('input[type="checkbox"]')) {
Chris@0 43 checkboxes.each(function () {
Chris@0 44 var $checkbox = $(this);
Chris@0 45 var stateChanged = $checkbox.prop('checked') !== event.target.checked;
Chris@0 46
Chris@0 47 if (stateChanged) {
Chris@0 48 $checkbox.prop('checked', event.target.checked).trigger('change');
Chris@0 49 }
Chris@0 50
Chris@0 51 $checkbox.closest('tr').toggleClass('selected', this.checked);
Chris@0 52 });
Chris@0 53
Chris@0 54 updateSelectAll(event.target.checked);
Chris@0 55 }
Chris@0 56 });
Chris@0 57
Chris@0 58 checkboxes = $table.find('td input[type="checkbox"]:enabled').on('click', function (e) {
Chris@0 59 $(this).closest('tr').toggleClass('selected', this.checked);
Chris@0 60
Chris@0 61 if (e.shiftKey && lastChecked && lastChecked !== e.target) {
Chris@0 62 Drupal.tableSelectRange($(e.target).closest('tr')[0], $(lastChecked).closest('tr')[0], e.target.checked);
Chris@0 63 }
Chris@0 64
Chris@0 65 updateSelectAll(checkboxes.length === checkboxes.filter(':checked').length);
Chris@0 66
Chris@0 67 lastChecked = e.target;
Chris@0 68 });
Chris@0 69
Chris@0 70 updateSelectAll(checkboxes.length === checkboxes.filter(':checked').length);
Chris@0 71 };
Chris@0 72
Chris@0 73 Drupal.tableSelectRange = function (from, to, state) {
Chris@0 74 var mode = from.rowIndex > to.rowIndex ? 'previousSibling' : 'nextSibling';
Chris@0 75
Chris@0 76 for (var i = from[mode]; i; i = i[mode]) {
Chris@14 77 var $i = $(i);
Chris@0 78
Chris@0 79 if (i.nodeType !== 1) {
Chris@0 80 continue;
Chris@0 81 }
Chris@0 82
Chris@0 83 $i.toggleClass('selected', state);
Chris@0 84 $i.find('input[type="checkbox"]').prop('checked', state);
Chris@0 85
Chris@0 86 if (to.nodeType) {
Chris@0 87 if (i === to) {
Chris@0 88 break;
Chris@0 89 }
Chris@0 90 } else if ($.filter(to, [i]).r.length) {
Chris@0 91 break;
Chris@0 92 }
Chris@0 93 }
Chris@0 94 };
Chris@0 95 })(jQuery, Drupal);