| 1 | /** |
| 2 | * JQuery shiftcheckbox plugin |
| 3 | * |
| 4 | * shiftcheckbox provides a simpler and faster way to select/unselect multiple checkboxes within a given range with just two clicks. |
| 5 | * Inspired from GMail checkbox functionality |
| 6 | * |
| 7 | * Just call $('.<class-name>').shiftcheckbox() in $(document).ready |
| 8 | * |
| 9 | * @name shiftcheckbox |
| 10 | * @type jquery |
| 11 | * @cat Plugin/Form |
| 12 | * @return JQuery |
| 13 | * |
| 14 | * @URL http://www.sanisoft.com/blog/2009/07/02/jquery-shiftcheckbox-plugin |
| 15 | * |
| 16 | * Copyright (c) 2009 Aditya Mooley <adityamooley@sanisoft.com> |
| 17 | * Dual licensed under the MIT (MIT-LICENSE.txt) and GPL (GPL-LICENSE.txt) licenses |
| 18 | */ |
| 19 | |
| 20 | (function ($) { |
| 21 | $.fn.shiftcheckbox = function() |
| 22 | { |
| 23 | var prevChecked = null; |
| 24 | |
| 25 | selectorStr = this; |
| 26 | |
| 27 | $(selectorStr).bind("click", handleClick); |
| 28 | }; |
| 29 | |
| 30 | function handleClick(event) |
| 31 | { |
| 32 | var val = this.value; |
| 33 | var checkStatus = this.checked; |
| 34 | //get the checkbox number which the user has checked |
| 35 | |
| 36 | //check whether user has pressed shift |
| 37 | if (event.shiftKey) { |
| 38 | if (prevChecked != 'null') { |
| 39 | //get the current checkbox number |
| 40 | var ind = 0, found = 0, currentChecked; |
| 41 | currentChecked = getSelected(val); |
| 42 | |
| 43 | ind = 0; |
| 44 | if (currentChecked < prevChecked) { |
| 45 | $(selectorStr).each(function(i) { |
| 46 | if (ind >= currentChecked && ind <= prevChecked) { |
| 47 | this.checked = checkStatus; |
| 48 | } |
| 49 | ind++; |
| 50 | }); |
| 51 | } else { |
| 52 | $(selectorStr).each(function(i) { |
| 53 | if (ind >= prevChecked && ind <= currentChecked) { |
| 54 | this.checked = checkStatus; |
| 55 | } |
| 56 | ind++; |
| 57 | }); |
| 58 | } |
| 59 | |
| 60 | prevChecked = currentChecked; |
| 61 | } |
| 62 | } else { |
| 63 | if (checkStatus) { |
| 64 | prevChecked = getSelected(val); |
| 65 | } |
| 66 | } |
| 67 | }; |
| 68 | |
| 69 | function getSelected(val) |
| 70 | { |
| 71 | var ind = 0, found = 0, checkedIndex; |
| 72 | |
| 73 | $(selectorStr).each(function(i) { |
| 74 | if (val == this.value && found != 1) { |
| 75 | checkedIndex = ind; |
| 76 | found = 1; |
| 77 | } |
| 78 | ind++; |
| 79 | }); |
| 80 | |
| 81 | return checkedIndex; |
| 82 | }; |
| 83 | })(jQuery); |
| 84 | No newline at end of file |