Requires

Provides

HtmlTable.Select.js

Builds a stripy, sortable table with methods to add rows. Rows can be selected with the mouse or keyboard navigation.

License:
MIT-style license
Authors:
Harald Kirschner, Aaron Newton
  1. 30
  2. 31
  3. 32
HtmlTable = Class.refactor(HtmlTable, { options: {

onRowFocus: function(){}, onRowUnfocus: function(){},

  1. 35
  2. 36
  3. 37
  4. 38
  5. 39
  6. 40
  7. 41
  8. 42
  9. 43
  10. 44
  11. 45
  12. 46
  13. 47
  14. 48
  15. 49
  16. 50
  17. 51
  18. 52
  19. 53
  20. 54
  21. 55
  22. 56
  23. 57
  24. 58
  25. 59
  26. 60
  27. 61
  28. 62
  29. 63
  30. 64
  31. 65
  32. 66
  33. 67
  34. 68
  35. 69
  36. 70
  37. 71
  38. 72
  39. 73
  40. 74
  41. 75
  42. 76
  43. 77
  44. 78
  45. 79
  46. 80
  47. 81
  48. 82
  49. 83
  50. 84
useKeyboard: true, classRowSelected: 'table-tr-selected', classRowHovered: 'table-tr-hovered', classSelectable: 'table-selectable', shiftForMultiSelect: true, allowMultiSelect: true, selectable: false }, initialize: function(){ this.previous.apply(this, arguments); if (this.occluded) return this.occluded; this._selectedRows = new Elements(); this._bound = { mouseleave: this._mouseleave.bind(this), clickRow: this._clickRow.bind(this) }; if (this.options.selectable) this.enableSelect(); }, enableSelect: function(){ this._selectEnabled = true; this._attachSelects(); this.element.addClass(this.options.classSelectable); }, disableSelect: function(){ this._selectEnabled = false; this._attachSelects(false); this.element.removeClass(this.options.classSelectable); }, push: function(){ var ret = this.previous.apply(this, arguments); this._updateSelects(); return ret; }, isSelected: function(row){ return this._selectedRows.contains(row); }, toggleRow: function(row){ return this[(this.isSelected(row) ? 'de' : '') + 'selectRow'](row); }, selectRow: function(row, _nocheck){

private variable _nocheck: boolean whether or not to confirm the row is in the table body added here for optimization when selecting ranges

  1. 87
  2. 88
  3. 89
  4. 90
  5. 91
  6. 92
  7. 93
  8. 94
  9. 95
  10. 96
  11. 97
  12. 98
  13. 99
  14. 100
  15. 101
  16. 102
  17. 103
  18. 104
  19. 105
  20. 106
  21. 107
  22. 108
  23. 109
  24. 110
  25. 111
  26. 112
  27. 113
  28. 114
  29. 115
  30. 116
  31. 117
  32. 118
  33. 119
  34. 120
  35. 121
  36. 122
  37. 123
  38. 124
  39. 125
  40. 126
  41. 127
  42. 128
  43. 129
  44. 130
  45. 131
  46. 132
  47. 133
  48. 134
  49. 135
  50. 136
  51. 137
  52. 138
if (!_nocheck && !this.body.getChildren().contains(row)) return; if (!this.options.allowMultiSelect) this.selectNone(); if (!this.isSelected(row)) { this._selectedRows.push(row); row.addClass(this.options.classRowSelected); this.fireEvent('rowFocus', [row, this._selectedRows]); } this._focused = row; document.clearSelection(); return this; }, deselectRow: function(row, _nocheck){ if (!_nocheck && !this.body.getChildren().contains(row)) return; this._selectedRows = new Elements(Array.from(this._selectedRows).erase(row)); row.removeClass(this.options.classRowSelected); this.fireEvent('rowUnfocus', [row, this._selectedRows]); return this; }, selectAll: function(selectNone){ if (!selectNone && !this.options.allowMultiSelect) return; this.selectRange(0, this.body.rows.length, selectNone); return this; }, selectNone: function(){ return this.selectAll(true); }, selectRange: function(startRow, endRow, _deselect){ if (!this.options.allowMultiSelect) return; var method = _deselect ? 'deselectRow' : 'selectRow', rows = Array.clone(this.body.rows); if (typeOf(startRow) == 'element') startRow = rows.indexOf(startRow); if (typeOf(endRow) == 'element') endRow = rows.indexOf(endRow); endRow = endRow < rows.length - 1 ? endRow : rows.length - 1; for(var i = startRow; i <= endRow; i++) this[method](rows[i], true); return this; }, deselectRange: function(startRow, endRow){ this.selectRange(startRow, endRow, true); },

Private methods:

  1. 144
  2. 145
  3. 146
  4. 147
  5. 148
  6. 149
  7. 150
  8. 151
  9. 152
  10. 153
  11. 154
  12. 155
  13. 156
  14. 157
  15. 158
  16. 159
  17. 160
  18. 161
  19. 162
  20. 163
  21. 164
  22. 165
  23. 166
  24. 167
  25. 168
  26. 169
  27. 170
  28. 171
  29. 172
  30. 173
  31. 174
  32. 175
  33. 176
  34. 177
  35. 178
  36. 179
  37. 180
  38. 181
  39. 182
  40. 183
  41. 184
  42. 185
  43. 186
  44. 187
  45. 188
  46. 189
  47. 190
  48. 191
  49. 192
  50. 193
  51. 194
  52. 195
  53. 196
  54. 197
  55. 198
  56. 199
  57. 200
  58. 201
  59. 202
  60. 203
  61. 204
  62. 205
  63. 206
  64. 207
  65. 208
  66. 209
  67. 210
  68. 211
  69. 212
  70. 213
  71. 214
  72. 215
  73. 216
  74. 217
  75. 218
  76. 219
  77. 220
  78. 221
  79. 222
  80. 223
  81. 224
  82. 225
  83. 226
  84. 227
  85. 228
  86. 229
  87. 230
  88. 231
  89. 232
  90. 233
  91. 234
  92. 235
  93. 236
  94. 237
  95. 238
  96. 239
  97. 240
  98. 241
  99. 242
  100. 243
  101. 244
  102. 245
  103. 246
  104. 247
  105. 248
  106. 249
  107. 250
  108. 251
  109. 252
  110. 253
  111. 254
  112. 255
  113. 256
  114. 257
  115. 258
  116. 259
  117. 260
  118. 261
  119. 262
  120. 263
  121. 264
  122. 265
  123. 266
  124. 267
  125. 268
  126. 269
  127. 270
  128. 271
  129. 272
  130. 273
  131. 274
  132. 275
  133. 276
  134. 277
  135. 278
  136. 279
  137. 280
  138. 281
  139. 282
  140. 283
  141. 284
  142. 285
  143. 286
  144. 287
  145. 288
  146. 289
  147. 290
  148. 291
  149. 292
_enterRow: function(row){ if (this._hovered) this._hovered = this._leaveRow(this._hovered); this._hovered = row.addClass(this.options.classRowHovered); }, _leaveRow: function(row){ row.removeClass(this.options.classRowHovered); }, _updateSelects: function(){ Array.each(this.body.rows, function(row){ var binders = row.retrieve('binders'); if ((binders && this._selectEnabled) || (!binders && !this._selectEnabled)) return; if (!binders){ binders = { mouseenter: this._enterRow.bind(this, [row]), mouseleave: this._leaveRow.bind(this, [row]) }; row.store('binders', binders).addEvents(binders); } else { row.removeEvents(binders); } }, this); }, _shiftFocus: function(offset, event){ if (!this._focused) return this.selectRow(this.body.rows[0], event); var to = this._getRowByOffset(offset); if (to === null || this._focused == this.body.rows[to]) return this; this.toggleRow(this.body.rows[to], event); }, _clickRow: function(event, row){ var selecting = (event.shift || event.meta || event.control) && this.options.shiftForMultiSelect; if (!selecting && !(event.rightClick && this.isSelected(row) && this.options.allowMultiSelect)) this.selectNone(); if (event.rightClick) this.selectRow(row); else this.toggleRow(row); if (event.shift) { this.selectRange(this._rangeStart || this.body.rows[0], row, this._rangeStart ? !this.isSelected(row) : true); } this._rangeStart = row; }, _getRowByOffset: function(offset){ if (!this._focused) return 0; var rows = Array.clone(this.body.rows), index = rows.indexOf(this._focused) + offset; if (index < 0) index = null; if (index >= rows.length) index = null; return index; }, _attachSelects: function(attach){ attach = attach != null ? attach : true; var method = attach ? 'addEvents' : 'removeEvents'; this.element[method]({ mouseleave: this._bound.mouseleave }); this.body[method]({ 'click:relay(tr)': this._bound.clickRow, 'contextmenu:relay(tr)': this._bound.clickRow }); if (this.options.useKeyboard || this.keyboard){ if (!this.keyboard) { var timer, held; var move = function(offset){ var mover = function(e){ clearTimeout(timer); e.preventDefault(); var to = this.body.rows[this._getRowByOffset(offset)]; if (e.shift && to && this.isSelected(to)) { this.deselectRow(this._focused); this._focused = to; } else { if (to && (!this.options.allowMultiSelect || !e.shift)) { this.selectNone(); } this._shiftFocus(offset, e); } if (held) { timer = mover.delay(100, this, e); } else { timer = (function(){ held = true; mover(e); }).delay(400); } }.bind(this); return mover; }.bind(this); var clear = function(){ clearTimeout(timer); held = false; }; this.keyboard = new Keyboard({ events: { 'keydown:shift+up': move(-1), 'keydown:shift+down': move(1), 'keyup:shift+up': clear, 'keyup:shift+down': clear, 'keyup:up': clear, 'keyup:down': clear }, active: true }); var shiftHint = ''; if (this.options.allowMultiSelect && this.options.shiftForMultiSelect && this.options.useKeyboard) { shiftHint = " (Shift multi-selects)."; } this.keyboard.addShortcuts({ 'Select Previous Row': { keys: 'up', shortcut: 'up arrow', handler: move(-1), description: 'Select the previous row in the table.' + shiftHint }, 'Select Next Row': { keys: 'down', shortcut: 'down arrow', handler: move(1), description: 'Select the next row in the table.' + shiftHint } }); } this.keyboard[attach ? 'activate' : 'deactivate'](); } this._updateSelects(); }, _mouseleave: function(){ if (this._hovered) this._leaveRow(this._hovered); } });