buildframework/helium/doc/api/properties-table.js
author Jon Chatten
Fri, 12 Nov 2010 14:49:36 +0000
changeset 674 37ee82a83d43
parent 587 85df38eb4012
permissions -rw-r--r--
sbs version 2.15.3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     1
/*
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     2
  SortTable
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     3
  version 2
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     4
  7th April 2007
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     5
  Stuart Langridge, http://www.kryogenix.org/code/browser/sorttable/
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     6
  
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     7
  Instructions:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     8
  Download this file
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     9
  Add <script src="sorttable.js"></script> to your HTML
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    10
  Add class="sortable" to any table you'd like to make sortable
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    11
  Click on the headers to sort
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    12
  
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    13
  Thanks to many, many people for contributions and suggestions.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    14
  Licenced as X11: http://www.kryogenix.org/code/browser/licence.html
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    15
  This basically means: do what you want with it.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    16
*/
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    17
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    18
 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    19
var stIsIE = /*@cc_on!@*/false;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    20
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    21
sorttable = {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    22
  init: function() {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    23
    // quit if this function has already been called
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    24
    if (arguments.callee.done) return;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    25
    // flag this function so we don't do the same thing twice
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    26
    arguments.callee.done = true;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    27
    // kill the timer
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    28
    if (_timer) clearInterval(_timer);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    29
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    30
    if (!document.createElement || !document.getElementsByTagName) return;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    31
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    32
    sorttable.DATE_RE = /^(\d\d?)[\/\.-](\d\d?)[\/\.-]((\d\d)?\d\d)$/;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    33
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    34
    forEach(document.getElementsByTagName('table'), function(table) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    35
      if (table.className.search(/\bsortable\b/) != -1) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    36
        sorttable.makeSortable(table);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    37
      }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    38
    });
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    39
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    40
  },
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    41
  
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    42
  makeSortable: function(table) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    43
    if (table.getElementsByTagName('thead').length == 0) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    44
      // table doesn't have a tHead. Since it should have, create one and
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    45
      // put the first table row in it.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    46
      the = document.createElement('thead');
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    47
      the.appendChild(table.rows[0]);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    48
      table.insertBefore(the,table.firstChild);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    49
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    50
    // Safari doesn't support table.tHead, sigh
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    51
    if (table.tHead == null) table.tHead = table.getElementsByTagName('thead')[0];
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    52
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    53
    if (table.tHead.rows.length != 1) return; // can't cope with two header rows
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    54
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    55
    // Sorttable v1 put rows with a class of "sortbottom" at the bottom (as
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    56
    // "total" rows, for example). This is B&R, since what you're supposed
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    57
    // to do is put them in a tfoot. So, if there are sortbottom rows,
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    58
    // for backwards compatibility, move them to tfoot (creating it if needed).
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    59
    sortbottomrows = [];
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    60
    for (var i=0; i<table.rows.length; i++) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    61
      if (table.rows[i].className.search(/\bsortbottom\b/) != -1) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    62
        sortbottomrows[sortbottomrows.length] = table.rows[i];
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    63
      }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    64
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    65
    if (sortbottomrows) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    66
      if (table.tFoot == null) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    67
        // table doesn't have a tfoot. Create one.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    68
        tfo = document.createElement('tfoot');
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    69
        table.appendChild(tfo);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    70
      }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    71
      for (var i=0; i<sortbottomrows.length; i++) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    72
        tfo.appendChild(sortbottomrows[i]);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    73
      }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    74
      delete sortbottomrows;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    75
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    76
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    77
    // work through each column and calculate its type
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    78
    headrow = table.tHead.rows[0].cells;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    79
    for (var i=0; i<headrow.length; i++) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    80
      // manually override the type with a sorttable_type attribute
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    81
      if (!headrow[i].className.match(/\bsorttable_nosort\b/)) { // skip this col
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    82
        mtch = headrow[i].className.match(/\bsorttable_([a-z0-9]+)\b/);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    83
        if (mtch) { override = mtch[1]; }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    84
          if (mtch && typeof sorttable["sort_"+override] == 'function') {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    85
            headrow[i].sorttable_sortfunction = sorttable["sort_"+override];
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    86
          } else {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    87
            headrow[i].sorttable_sortfunction = sorttable.guessType(table,i);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    88
          }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    89
          // make it clickable to sort
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    90
          headrow[i].sorttable_columnindex = i;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    91
          headrow[i].sorttable_tbody = table.tBodies[0];
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    92
          dean_addEvent(headrow[i],"click", function(e) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    93
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    94
          if (this.className.search(/\bsorttable_sorted\b/) != -1) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    95
            // if we're already sorted by this column, just 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    96
            // reverse the table, which is quicker
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    97
            sorttable.reverse(this.sorttable_tbody);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    98
            this.className = this.className.replace('sorttable_sorted',
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    99
                                                    'sorttable_sorted_reverse');
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   100
            this.removeChild(document.getElementById('sorttable_sortfwdind'));
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   101
            sortrevind = document.createElement('span');
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   102
            sortrevind.id = "sorttable_sortrevind";
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   103
            sortrevind.innerHTML = stIsIE ? '&nbsp<font face="webdings">5</font>' : '&nbsp;&#x25B4;';
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   104
            this.appendChild(sortrevind);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   105
            return;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   106
          }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   107
          if (this.className.search(/\bsorttable_sorted_reverse\b/) != -1) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   108
            // if we're already sorted by this column in reverse, just 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   109
            // re-reverse the table, which is quicker
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   110
            sorttable.reverse(this.sorttable_tbody);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   111
            this.className = this.className.replace('sorttable_sorted_reverse',
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   112
                                                    'sorttable_sorted');
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   113
            this.removeChild(document.getElementById('sorttable_sortrevind'));
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   114
            sortfwdind = document.createElement('span');
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   115
            sortfwdind.id = "sorttable_sortfwdind";
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   116
            sortfwdind.innerHTML = stIsIE ? '&nbsp<font face="webdings">6</font>' : '&nbsp;&#x25BE;';
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   117
            this.appendChild(sortfwdind);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   118
            return;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   119
          }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   120
          
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   121
          // remove sorttable_sorted classes
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   122
          theadrow = this.parentNode;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   123
          forEach(theadrow.childNodes, function(cell) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   124
            if (cell.nodeType == 1) { // an element
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   125
              cell.className = cell.className.replace('sorttable_sorted_reverse','');
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   126
              cell.className = cell.className.replace('sorttable_sorted','');
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   127
            }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   128
          });
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   129
          sortfwdind = document.getElementById('sorttable_sortfwdind');
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   130
          if (sortfwdind) { sortfwdind.parentNode.removeChild(sortfwdind); }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   131
          sortrevind = document.getElementById('sorttable_sortrevind');
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   132
          if (sortrevind) { sortrevind.parentNode.removeChild(sortrevind); }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   133
          
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   134
          this.className += ' sorttable_sorted';
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   135
          sortfwdind = document.createElement('span');
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   136
          sortfwdind.id = "sorttable_sortfwdind";
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   137
          sortfwdind.innerHTML = stIsIE ? '&nbsp<font face="webdings">6</font>' : '&nbsp;&#x25BE;';
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   138
          this.appendChild(sortfwdind);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   139
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   140
            // build an array to sort. This is a Schwartzian transform thing,
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   141
            // i.e., we "decorate" each row with the actual sort key,
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   142
            // sort based on the sort keys, and then put the rows back in order
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   143
            // which is a lot faster because you only do getInnerText once per row
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   144
            row_array = [];
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   145
            col = this.sorttable_columnindex;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   146
            rows = this.sorttable_tbody.rows;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   147
            for (var j=0; j<rows.length; j++) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   148
              row_array[row_array.length] = [sorttable.getInnerText(rows[j].cells[col]), rows[j]];
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   149
            }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   150
            /* If you want a stable sort, uncomment the following line */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   151
            //sorttable.shaker_sort(row_array, this.sorttable_sortfunction);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   152
            /* and comment out this one */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   153
            row_array.sort(this.sorttable_sortfunction);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   154
            
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   155
            tb = this.sorttable_tbody;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   156
            for (var j=0; j<row_array.length; j++) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   157
              tb.appendChild(row_array[j][1]);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   158
            }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   159
            
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   160
            delete row_array;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   161
          });
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   162
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   163
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   164
  },
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   165
  
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   166
  guessType: function(table, column) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   167
    // guess the type of a column based on its first non-blank row
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   168
    sortfn = sorttable.sort_alpha;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   169
    for (var i=0; i<table.tBodies[0].rows.length; i++) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   170
      text = sorttable.getInnerText(table.tBodies[0].rows[i].cells[column]);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   171
      if (text != '') {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   172
        if (text.match(/^-?[£$¤]?[\d,.]+%?$/)) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   173
          return sorttable.sort_numeric;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   174
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   175
        // check for a date: dd/mm/yyyy or dd/mm/yy 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   176
        // can have / or . or - as separator
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   177
        // can be mm/dd as well
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   178
        possdate = text.match(sorttable.DATE_RE)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   179
        if (possdate) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   180
          // looks like a date
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   181
          first = parseInt(possdate[1]);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   182
          second = parseInt(possdate[2]);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   183
          if (first > 12) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   184
            // definitely dd/mm
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   185
            return sorttable.sort_ddmm;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   186
          } else if (second > 12) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   187
            return sorttable.sort_mmdd;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   188
          } else {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   189
            // looks like a date, but we can't tell which, so assume
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   190
            // that it's dd/mm (English imperialism!) and keep looking
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   191
            sortfn = sorttable.sort_ddmm;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   192
          }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   193
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   194
      }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   195
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   196
    return sortfn;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   197
  },
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   198
  
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   199
  getInnerText: function(node) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   200
    // gets the text we want to use for sorting for a cell.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   201
    // strips leading and trailing whitespace.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   202
    // this is *not* a generic getInnerText function; it's special to sorttable.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   203
    // for example, you can override the cell text with a customkey attribute.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   204
    // it also gets .value for <input> fields.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   205
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   206
    hasInputs = (typeof node.getElementsByTagName == 'function') &&
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   207
                 node.getElementsByTagName('input').length;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   208
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   209
    if (node.getAttribute("sorttable_customkey") != null) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   210
      return node.getAttribute("sorttable_customkey");
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   211
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   212
    else if (typeof node.textContent != 'undefined' && !hasInputs) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   213
      return node.textContent.replace(/^\s+|\s+$/g, '');
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   214
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   215
    else if (typeof node.innerText != 'undefined' && !hasInputs) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   216
      return node.innerText.replace(/^\s+|\s+$/g, '');
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   217
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   218
    else if (typeof node.text != 'undefined' && !hasInputs) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   219
      return node.text.replace(/^\s+|\s+$/g, '');
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   220
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   221
    else {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   222
      switch (node.nodeType) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   223
        case 3:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   224
          if (node.nodeName.toLowerCase() == 'input') {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   225
            return node.value.replace(/^\s+|\s+$/g, '');
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   226
          }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   227
        case 4:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   228
          return node.nodeValue.replace(/^\s+|\s+$/g, '');
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   229
          break;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   230
        case 1:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   231
        case 11:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   232
          var innerText = '';
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   233
          for (var i = 0; i < node.childNodes.length; i++) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   234
            innerText += sorttable.getInnerText(node.childNodes[i]);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   235
          }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   236
          return innerText.replace(/^\s+|\s+$/g, '');
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   237
          break;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   238
        default:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   239
          return '';
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   240
      }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   241
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   242
  },
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   243
  
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   244
  reverse: function(tbody) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   245
    // reverse the rows in a tbody
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   246
    newrows = [];
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   247
    for (var i=0; i<tbody.rows.length; i++) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   248
      newrows[newrows.length] = tbody.rows[i];
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   249
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   250
    for (var i=newrows.length-1; i>=0; i--) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   251
       tbody.appendChild(newrows[i]);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   252
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   253
    delete newrows;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   254
  },
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   255
  
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   256
  /* sort functions
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   257
     each sort function takes two parameters, a and b
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   258
     you are comparing a[0] and b[0] */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   259
  sort_numeric: function(a,b) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   260
    aa = parseFloat(a[0].replace(/[^0-9.-]/g,''));
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   261
    if (isNaN(aa)) aa = 0;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   262
    bb = parseFloat(b[0].replace(/[^0-9.-]/g,'')); 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   263
    if (isNaN(bb)) bb = 0;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   264
    return aa-bb;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   265
  },
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   266
  sort_alpha: function(a,b) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   267
    if (a[0]==b[0]) return 0;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   268
    if (a[0]<b[0]) return -1;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   269
    return 1;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   270
  },
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   271
  sort_ddmm: function(a,b) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   272
    mtch = a[0].match(sorttable.DATE_RE);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   273
    y = mtch[3]; m = mtch[2]; d = mtch[1];
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   274
    if (m.length == 1) m = '0'+m;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   275
    if (d.length == 1) d = '0'+d;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   276
    dt1 = y+m+d;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   277
    mtch = b[0].match(sorttable.DATE_RE);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   278
    y = mtch[3]; m = mtch[2]; d = mtch[1];
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   279
    if (m.length == 1) m = '0'+m;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   280
    if (d.length == 1) d = '0'+d;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   281
    dt2 = y+m+d;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   282
    if (dt1==dt2) return 0;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   283
    if (dt1<dt2) return -1;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   284
    return 1;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   285
  },
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   286
  sort_mmdd: function(a,b) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   287
    mtch = a[0].match(sorttable.DATE_RE);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   288
    y = mtch[3]; d = mtch[2]; m = mtch[1];
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   289
    if (m.length == 1) m = '0'+m;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   290
    if (d.length == 1) d = '0'+d;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   291
    dt1 = y+m+d;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   292
    mtch = b[0].match(sorttable.DATE_RE);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   293
    y = mtch[3]; d = mtch[2]; m = mtch[1];
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   294
    if (m.length == 1) m = '0'+m;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   295
    if (d.length == 1) d = '0'+d;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   296
    dt2 = y+m+d;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   297
    if (dt1==dt2) return 0;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   298
    if (dt1<dt2) return -1;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   299
    return 1;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   300
  },
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   301
  
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   302
  shaker_sort: function(list, comp_func) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   303
    // A stable sort function to allow multi-level sorting of data
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   304
    // see: http://en.wikipedia.org/wiki/Cocktail_sort
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   305
    // thanks to Joseph Nahmias
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   306
    var b = 0;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   307
    var t = list.length - 1;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   308
    var swap = true;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   309
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   310
    while(swap) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   311
        swap = false;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   312
        for(var i = b; i < t; ++i) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   313
            if ( comp_func(list[i], list[i+1]) > 0 ) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   314
                var q = list[i]; list[i] = list[i+1]; list[i+1] = q;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   315
                swap = true;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   316
            }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   317
        } // for
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   318
        t--;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   319
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   320
        if (!swap) break;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   321
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   322
        for(var i = t; i > b; --i) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   323
            if ( comp_func(list[i], list[i-1]) < 0 ) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   324
                var q = list[i]; list[i] = list[i-1]; list[i-1] = q;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   325
                swap = true;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   326
            }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   327
        } // for
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   328
        b++;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   329
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   330
    } // while(swap)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   331
  }  
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   332
}
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   333
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   334
/* ******************************************************************
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   335
   Supporting functions: bundled here to avoid depending on a library
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   336
   ****************************************************************** */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   337
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   338
// Dean Edwards/Matthias Miller/John Resig
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   339
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   340
/* for Mozilla/Opera9 */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   341
if (document.addEventListener) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   342
    document.addEventListener("DOMContentLoaded", sorttable.init, false);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   343
}
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   344
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   345
/* for Internet Explorer */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   346
/*@cc_on @*/
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   347
/*@if (@_win32)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   348
    document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   349
    var script = document.getElementById("__ie_onload");
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   350
    script.onreadystatechange = function() {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   351
        if (this.readyState == "complete") {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   352
            sorttable.init(); // call the onload handler
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   353
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   354
    };
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   355
/*@end @*/
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   356
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   357
/* for Safari */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   358
if (/WebKit/i.test(navigator.userAgent)) { // sniff
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   359
    var _timer = setInterval(function() {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   360
        if (/loaded|complete/.test(document.readyState)) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   361
            sorttable.init(); // call the onload handler
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   362
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   363
    }, 10);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   364
}
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   365
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   366
/* for other browsers */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   367
window.onload = sorttable.init;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   368
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   369
// written by Dean Edwards, 2005
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   370
// with input from Tino Zijdel, Matthias Miller, Diego Perini
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   371
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   372
// http://dean.edwards.name/weblog/2005/10/add-event/
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   373
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   374
function dean_addEvent(element, type, handler) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   375
    if (element.addEventListener) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   376
        element.addEventListener(type, handler, false);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   377
    } else {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   378
        // assign each event handler a unique ID
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   379
        if (!handler.$$guid) handler.$$guid = dean_addEvent.guid++;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   380
        // create a hash table of event types for the element
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   381
        if (!element.events) element.events = {};
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   382
        // create a hash table of event handlers for each element/event pair
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   383
        var handlers = element.events[type];
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   384
        if (!handlers) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   385
            handlers = element.events[type] = {};
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   386
            // store the existing event handler (if there is one)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   387
            if (element["on" + type]) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   388
                handlers[0] = element["on" + type];
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   389
            }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   390
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   391
        // store the event handler in the hash table
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   392
        handlers[handler.$$guid] = handler;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   393
        // assign a global event handler to do all the work
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   394
        element["on" + type] = handleEvent;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   395
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   396
};
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   397
// a counter used to create unique IDs
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   398
dean_addEvent.guid = 1;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   399
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   400
function removeEvent(element, type, handler) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   401
    if (element.removeEventListener) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   402
        element.removeEventListener(type, handler, false);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   403
    } else {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   404
        // delete the event handler from the hash table
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   405
        if (element.events && element.events[type]) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   406
            delete element.events[type][handler.$$guid];
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   407
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   408
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   409
};
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   410
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   411
function handleEvent(event) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   412
    var returnValue = true;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   413
    // grab the event object (IE uses a global event object)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   414
    event = event || fixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   415
    // get a reference to the hash table of event handlers
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   416
    var handlers = this.events[event.type];
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   417
    // execute each event handler
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   418
    for (var i in handlers) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   419
        this.$$handleEvent = handlers[i];
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   420
        if (this.$$handleEvent(event) === false) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   421
            returnValue = false;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   422
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   423
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   424
    return returnValue;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   425
};
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   426
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   427
function fixEvent(event) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   428
    // add W3C standard event methods
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   429
    event.preventDefault = fixEvent.preventDefault;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   430
    event.stopPropagation = fixEvent.stopPropagation;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   431
    return event;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   432
};
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   433
fixEvent.preventDefault = function() {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   434
    this.returnValue = false;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   435
};
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   436
fixEvent.stopPropagation = function() {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   437
  this.cancelBubble = true;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   438
}
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   439
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   440
// Dean's forEach: http://dean.edwards.name/base/forEach.js
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   441
/*
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   442
    forEach, version 1.0
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   443
    Copyright 2006, Dean Edwards
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   444
    License: http://www.opensource.org/licenses/mit-license.php
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   445
*/
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   446
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   447
// array-like enumeration
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   448
if (!Array.forEach) { // mozilla already supports this
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   449
    Array.forEach = function(array, block, context) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   450
        for (var i = 0; i < array.length; i++) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   451
            block.call(context, array[i], i, array);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   452
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   453
    };
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   454
}
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   455
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   456
// generic enumeration
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   457
Function.prototype.forEach = function(object, block, context) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   458
    for (var key in object) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   459
        if (typeof this.prototype[key] == "undefined") {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   460
            block.call(context, object[key], key, object);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   461
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   462
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   463
};
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   464
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   465
// character enumeration
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   466
String.forEach = function(string, block, context) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   467
    Array.forEach(string.split(""), function(chr, index) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   468
        block.call(context, chr, index, string);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   469
    });
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   470
};
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   471
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   472
// globally resolve forEach enumeration
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   473
var forEach = function(object, block, context) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   474
    if (object) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   475
        var resolve = Object; // default
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   476
        if (object instanceof Function) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   477
            // functions have a "length" property
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   478
            resolve = Function;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   479
        } else if (object.forEach instanceof Function) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   480
            // the object implements a custom forEach method so use that
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   481
            object.forEach(block, context);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   482
            return;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   483
        } else if (typeof object == "string") {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   484
            // the object is a string
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   485
            resolve = String;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   486
        } else if (typeof object.length == "number") {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   487
            // the object is array-like
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   488
            resolve = Array;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   489
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   490
        resolve.forEach(object, block, context);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   491
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   492
};