root/trunk/checkallbutton.js

Revision 69, 1.7 kB (checked in by mischa, 3 years ago)

stop using Try.these

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2  This is a class which you can attach to a checkbox element.
3  When that element is clicked, it will toggle every checkbox
4  with a specified classname to be the same as the checkbox that was
5  clicked.
6
7  $Id$
8*/
9
10CheckallButton = new Class(Object, {
11
12  // opts:
13  //  class => what class all of the checkboxes have
14  //  button => the "check all" button element
15  //  parent => [optional] only check boxes that are children of this element
16  init: function (opts) {
17    CheckallButton.superClass.init.apply(arguments);
18
19    this.button = opts["button"];
20    this.className = opts["class"];
21    this.parent = opts["parent"];
22    this.attachEvents();
23  },
24
25  attachEvents: function () {
26    if (!this.button || !this.className)
27      return;
28
29    DOM.addEventListener(this.button, "click", this.buttonClicked.bindEventListener(this));
30  },
31
32  buttonClicked: function (e) {
33    if (!this.button || !this.className)
34      return;
35
36    var parent = this.parent;
37    if (!parent)
38      parent = document;
39
40    var viewObjects = parent.getElementsByTagName("*");
41    var boxes = DOM.filterElementsByClassName(viewObjects, this.className) || [];
42
43    var checkallBox = this.button;
44
45    for (var i = 0; i < boxes.length; i++) {
46      var box = boxes[i];
47
48      if (!box)
49        continue;
50
51      if (box.checked == checkallBox.checked) continue;
52
53      // send a "clicked" event to the checkbox
54      try {
55          // w3c
56          var evt = document.createEvent("MouseEvents");
57          evt.initMouseEvent("click", true, false, window,
58                             0, 0, 0, 0, 0, false, false, false, false, 0, null);
59          box.dispatchEvent(evt);
60      } catch (e) {
61          try {
62              // ie
63              box.click();
64          } catch (e2) { }
65      }
66    }
67  }
68});
Note: See TracBrowser for help on using the browser.