|
| 1 | +/* ShiftCheckbox jQuery plugin |
| 2 | + * |
| 3 | + * Copyright (C) 2011-2012 James Nylen |
| 4 | + * |
| 5 | + * Released under MIT license |
| 6 | + * For details see: |
| 7 | + * https://github.com/nylen/shiftcheckbox |
| 8 | + * |
| 9 | + * Requires jQuery v1.7 or higher. |
| 10 | + */ |
| 11 | + |
| 12 | +(function($) { |
| 13 | + var ns = '.shiftcheckbox'; |
| 14 | + |
| 15 | + $.fn.shiftcheckbox = function(opts) { |
| 16 | + opts = $.extend({ |
| 17 | + checkboxSelector : null, |
| 18 | + selectAll : null, |
| 19 | + onChange : null, |
| 20 | + ignoreClick : null |
| 21 | + }, opts); |
| 22 | + |
| 23 | + if (typeof opts.onChange != 'function') { |
| 24 | + opts.onChange = function(checked) { }; |
| 25 | + } |
| 26 | + |
| 27 | + $.fn.scb_changeChecked = function(opts, checked) { |
| 28 | + this.prop('checked', checked); |
| 29 | + opts.onChange.call(this, checked); |
| 30 | + return this; |
| 31 | + } |
| 32 | + |
| 33 | + var $containers, |
| 34 | + $checkboxes, |
| 35 | + $containersSelectAll, |
| 36 | + $checkboxesSelectAll, |
| 37 | + $otherSelectAll, |
| 38 | + $containersAll, |
| 39 | + $checkboxesAll; |
| 40 | + |
| 41 | + if (opts.selectAll) { |
| 42 | + // We need to set up a "select all" control |
| 43 | + $containersSelectAll = $(opts.selectAll); |
| 44 | + if ($containersSelectAll && !$containersSelectAll.length) { |
| 45 | + $containersSelectAll = false; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + if ($containersSelectAll) { |
| 50 | + $checkboxesSelectAll = $containersSelectAll |
| 51 | + .filter(':checkbox') |
| 52 | + .add($containersSelectAll.find(':checkbox')); |
| 53 | + |
| 54 | + $containersSelectAll = $containersSelectAll.not(':checkbox'); |
| 55 | + $otherSelectAll = $containersSelectAll.filter(function() { |
| 56 | + return !$(this).find($checkboxesSelectAll).length; |
| 57 | + }); |
| 58 | + $containersSelectAll = $containersSelectAll.filter(function() { |
| 59 | + return !!$(this).find($checkboxesSelectAll).length; |
| 60 | + }).each(function() { |
| 61 | + $(this).data('childCheckbox', $(this).find($checkboxesSelectAll)[0]); |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + if (opts.checkboxSelector) { |
| 66 | + |
| 67 | + // checkboxSelector means that the elements we need to attach handlers to |
| 68 | + // ($containers) are not actually checkboxes but contain them instead |
| 69 | + |
| 70 | + $containersAll = this.filter(function() { |
| 71 | + return !!$(this).find(opts.checkboxSelector).filter(':checkbox').length; |
| 72 | + }).each(function() { |
| 73 | + $(this).data('childCheckbox', $(this).find(opts.checkboxSelector).filter(':checkbox')[0]); |
| 74 | + }).add($containersSelectAll); |
| 75 | + |
| 76 | + $checkboxesAll = $containersAll.map(function() { |
| 77 | + return $(this).data('childCheckbox'); |
| 78 | + }); |
| 79 | + |
| 80 | + } else { |
| 81 | + |
| 82 | + $checkboxesAll = this.filter(':checkbox'); |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + if ($checkboxesSelectAll && !$checkboxesSelectAll.length) { |
| 87 | + $checkboxesSelectAll = false; |
| 88 | + } else { |
| 89 | + $checkboxesAll = $checkboxesAll.add($checkboxesSelectAll); |
| 90 | + } |
| 91 | + |
| 92 | + if ($otherSelectAll && !$otherSelectAll.length) { |
| 93 | + $otherSelectAll = false; |
| 94 | + } |
| 95 | + |
| 96 | + if ($containersAll) { |
| 97 | + $containers = $containersAll.not($containersSelectAll); |
| 98 | + } |
| 99 | + $checkboxes = $checkboxesAll.not($checkboxesSelectAll); |
| 100 | + |
| 101 | + if (!$checkboxes.length) { |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + var lastIndex = -1; |
| 106 | + |
| 107 | + var checkboxClicked = function(e) { |
| 108 | + var checked = !!$(this).prop('checked'); |
| 109 | + |
| 110 | + var curIndex = $checkboxes.index(this); |
| 111 | + if (curIndex < 0) { |
| 112 | + if ($checkboxesSelectAll.filter(this).length) { |
| 113 | + $checkboxesAll.scb_changeChecked(opts, checked); |
| 114 | + } |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + if (e.shiftKey && lastIndex != -1) { |
| 119 | + var di = (curIndex > lastIndex ? 1 : -1); |
| 120 | + for (var i = lastIndex; i != curIndex; i += di) { |
| 121 | + $checkboxes.eq(i).scb_changeChecked(opts, checked); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + if ($checkboxesSelectAll) { |
| 126 | + if (checked && !$checkboxes.not(':checked').length) { |
| 127 | + $checkboxesSelectAll.scb_changeChecked(opts, true); |
| 128 | + } else if (!checked) { |
| 129 | + $checkboxesSelectAll.scb_changeChecked(opts, false); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + lastIndex = curIndex; |
| 134 | + }; |
| 135 | + |
| 136 | + if ($checkboxesSelectAll) { |
| 137 | + $checkboxesSelectAll |
| 138 | + .prop('checked', !$checkboxes.not(':checked').length) |
| 139 | + .filter(function() { |
| 140 | + return !$containersAll.find(this).length; |
| 141 | + }).on('click' + ns, checkboxClicked); |
| 142 | + } |
| 143 | + |
| 144 | + if ($otherSelectAll) { |
| 145 | + $otherSelectAll.on('click' + ns, function() { |
| 146 | + var checked; |
| 147 | + if ($checkboxesSelectAll) { |
| 148 | + checked = !!$checkboxesSelectAll.eq(0).prop('checked'); |
| 149 | + } else { |
| 150 | + checked = !!$checkboxes.eq(0).prop('checked'); |
| 151 | + } |
| 152 | + $checkboxesAll.scb_changeChecked(opts, !checked); |
| 153 | + }); |
| 154 | + } |
| 155 | + |
| 156 | + if (opts.checkboxSelector) { |
| 157 | + $containersAll.on('click' + ns, function(e) { |
| 158 | + if ($(e.target).closest(opts.ignoreClick).length) { |
| 159 | + return; |
| 160 | + } |
| 161 | + var $checkbox = $($(this).data('childCheckbox')); |
| 162 | + $checkbox.not(e.target).each(function() { |
| 163 | + var checked = !$checkbox.prop('checked'); |
| 164 | + $(this).scb_changeChecked(opts, checked); |
| 165 | + }); |
| 166 | + |
| 167 | + $checkbox[0].focus(); |
| 168 | + checkboxClicked.call($checkbox, e); |
| 169 | + |
| 170 | + // If the user clicked on a label inside the row that points to the |
| 171 | + // current row's checkbox, cancel the event. |
| 172 | + var $label = $(e.target).closest('label'); |
| 173 | + var labelFor = $label.attr('for'); |
| 174 | + if (labelFor && labelFor == $checkbox.attr('id')) { |
| 175 | + if ($label.find($checkbox).length) { |
| 176 | + // Special case: The label contains the checkbox. |
| 177 | + if ($checkbox[0] != e.target) { |
| 178 | + return false; |
| 179 | + } |
| 180 | + } else { |
| 181 | + return false; |
| 182 | + } |
| 183 | + } |
| 184 | + }).on('mousedown' + ns, function(e) { |
| 185 | + if (e.shiftKey) { |
| 186 | + // Prevent selecting text by Shift+click |
| 187 | + return false; |
| 188 | + } |
| 189 | + }); |
| 190 | + } else { |
| 191 | + $checkboxes.on('click' + ns, checkboxClicked); |
| 192 | + } |
| 193 | + |
| 194 | + return this; |
| 195 | + }; |
| 196 | +})(jQuery); |
0 commit comments