Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add experimental Object.isNodeList, CheckboxRange for multiple selection

  • Loading branch information...
commit 430b5dab60ce35179becba2cc36a37a6d3a636eb 1 parent 1f8ffe3
kangax authored
Showing with 33 additions and 1 deletion.
  1. +26 −0 checkbox_range.js
  2. +7 −1 object.extensions.js
View
26 checkbox_range.js
@@ -0,0 +1,26 @@
+/**
+ * Adds a multi-select behavior to a group of checkboxes (via a shift key modifier)
+ *
+ * new CheckboxRange('.myCheckbox');
+ * new CheckboxRange(); // defaults to all "input[type=checkbox]" within a document
+ *
+ **/
+var CheckboxRange = Class.create({
+ initialize: function(elements) {
+ this.elements = elements ? $$(elements) : $$('input[type=checkbox]');
+ this.initObservers();
+ },
+ initObservers: function() {
+ document.observe('click', function(e, el) {
+ if (!(el = e.findElement('input[type="checkbox"]'))) return;
+ if (e.shiftKey && this.lastChecked) {
+ var currentIndex = this.elements.indexOf(el),
+ lastIndex = this.elements.indexOf(this.lastChecked);
+ for (var i=Math.min(currentIndex, lastIndex); i<Math.max(currentIndex, lastIndex); i++) {
+ this.elements[i].checked = true;
+ }
+ }
+ if (el.checked) this.lastChecked = el;
+ }.bind(this))
+ }
+})
View
8 object.extensions.js
@@ -109,4 +109,10 @@ Object.methodize = function(object) {
m[name] = object[name].methodize();
return m;
});
-};
+};
+
+Object.isNodeList = function(object) {
+ return !!object &&
+ typeof object.length != 'undefined' &&
+ typeof object.item != 'undefined'
+}
Please sign in to comment.
Something went wrong with that request. Please try again.