Requires

Provides

List.js

Trait that makes it simple to work with a list of item (and select one of them)

License:
Public domain (http://unlicense.org).
  1. 23
  2. 24
  3. 25
  4. 26
  5. 27
  6. 28
  7. 29
  8. 30
  9. 31
  10. 32
  11. 33
  12. 34
  13. 35
  14. 36
  15. 37
  16. 38
  17. 39
  18. 40
  19. 41
  20. 42
  21. 43
  22. 44
  23. 45
  24. 46
  25. 47
  26. 48
  27. 49
  28. 50
  29. 51
  30. 52
  31. 53
  32. 54
  33. 55
  34. 56
  35. 57
  36. 58
  37. 59
  38. 60
  39. 61
  40. 62
  41. 63
  42. 64
  43. 65
  44. 66
  45. 67
  46. 68
  47. 69
  48. 70
  49. 71
  50. 72
  51. 73
  52. 74
  53. 75
  54. 76
  55. 77
  56. 78
  57. 79
  58. 80
  59. 81
  60. 82
  61. 83
  62. 84
  63. 85
  64. 86
  65. 87
  66. 88
  67. 89
  68. 90
  69. 91
  70. 92
  71. 93
  72. 94
  73. 95
  74. 96
  75. 97
  76. 98
  77. 99
  78. 100
  79. 101
  80. 102
  81. 103
  82. 104
  83. 105
  84. 106
  85. 107
  86. 108
  87. 109
  88. 110
  89. 111
  90. 112
  91. 113
  92. 114
  93. 115
  94. 116
  95. 117
  96. 118
  97. 119
  98. 120
  99. 121
  100. 122
  101. 123
  102. 124
  103. 125
  104. 126
  105. 127
  106. 128
  107. 129
  108. 130
  109. 131
  110. 132
  111. 133
  112. 134
  113. 135
  114. 136
  115. 137
  116. 138
  117. 139
  118. 140
  119. 141
  120. 142
  121. 143
  122. 144
  123. 145
  124. 146
  125. 147
  126. 148
  127. 149
  128. 150
  129. 151
  130. 152
  131. 153
  132. 154
  133. 155
  134. 156
  135. 157
  136. 158
  137. 159
  138. 160
  139. 161
  140. 162
  141. 163
  142. 164
  143. 165
  144. 166
  145. 167
  146. 168
  147. 169
  148. 170
  149. 171
  150. 172
  151. 173
  152. 174
  153. 175
  154. 176
  155. 177
  156. 178
  157. 179
  158. 180
  159. 181
  160. 182
  161. 183
  162. 184
  163. 185
  164. 186
  165. 187
  166. 188
  167. 189
  168. 190
  169. 191
  170. 192
  171. 193
  172. 194
  173. 195
  174. 196
  175. 197
  176. 198
  177. 199
  178. 200
  179. 201
  180. 202
  181. 203
  182. 204
  183. 205
  184. 206
  185. 207
  186. 208
  187. 209
  188. 210
  189. 211
  190. 212
  191. 213
  192. 214
  193. 215
  194. 216
  195. 217
  196. 218
  197. 219
  198. 220
  199. 221
  200. 222
  201. 223
  202. 224
  203. 225
  204. 226
  205. 227
  206. 228
  207. 229
  208. 230
  209. 231
  210. 232
  211. 233
  212. 234
  213. 235
  214. 236
  215. 237
  216. 238
  217. 239
  218. 240
LSD.Trait.List = new Class({ options: { list: { endless: true, force: false, multiple: false, unselect: null }, proxies: { container: { condition: function(widget) { return !!widget.setList } } }, shortcuts: { previous: 'previous', next: 'next' }, events: { attach: function() { var items = this.list.length ? this.list : this.options.list.items; if (items) this.setItems(items); } }, has: { many: { items: { selector: ':item', events: { select: function() { this.listWidget.selectItem(this) }, unselect: function() { this.listWidget.unselectItem(this); }, dispose: function() { this.listWidget.unselectItem(this); } }, alias: 'listWidget', states: { add: Array.fast('selected') } } } }, pseudos: Array.fast('list') }, initialize: function() { this.widgets = []; this.list = []; this.parent.apply(this, arguments) }, selectItem: function(item) { if (!(item = this.getItem(item)) && this.options.list.force) return false; var unselect = (this.options.list.unselect !== null) ? this.options.list.unselect : !this.options.list.multiple; var selected = this.selectedItem; if (unselect && (selected != item) && selected && selected.unselect) this.unselectItem(selected); this.setSelectedItem.apply(this, arguments); this.fireEvent('set', [item, this.getItemIndex(item)]); item.select(); return item; }, unselectItem: function(item) { if (!(item = this.getItem(item)) || !this.isItemSelected(item)) return false; if (item.unselect) item.unselect(); this.unsetSelectedItem.apply(this, arguments); this.fireEvent('unset', [item, this.getItemIndex(item)]); delete item; }, setSelectedItem: function(item, type) { var property = (type || 'selected') + 'Item'; if (this.options.list.multiple) { property += 's'; if (!this[property]) this[property] = []; this[property].push(item); } else this[property] = item }, unsetSelectedItem: function(item, type) { var property = (type || 'selected') + 'Item'; if (this.options.list.multiple) { property += 's'; if (this[property]) this[property].erase(item); } else delete this[property] }, getSelectedItem: function() { return this.selectedItem || (this.selectedItems ? this.selectedItems.getLast() : null); }, getSelectedItems: function(type) { if (this.selectedItems) return Array.prototype.slice.call(this.selectedItems, 0); return this.selectedItem ? [this.selectedItem] : []; }, isItemSelected: function(item) { return this.selectedItems ? this.selectedItems.indexOf(item) > -1 : (this.selectedItem == item) }, buildItem: function(value) { if (this.options.layout.item) return this.buildLayout(this.options.layout.item); return new Element('div', { 'class': 'lsd option', 'html': value.toString(), 'events': { click: function() { this.selectItem(value); }.bind(this) } }); }, getItem: function(item) { return (item && !item.select) ? this.findItemByValue(item) : item; }, setItems: function(items) { this.list = []; this.widgets = []; items.each(this.addItem.bind(this)); if (this.options.list.force) this.selectItem(items[0]); return this; }, addItem: function(item) { if (item.setList) var data = item.getValue ? item.getValue() : item.value || $uid(item), widget = item, item = data; if (!this.list.contains(item)) { this.list.push(item); if (widget) { widget.listWidget = this; this.widgets.push(widget); } return true; } return false; }, makeItems: function() { var item, i = this.widgets.length; while (item = this.list[i++]) this.makeItem(item); }, makeItem: function(item) { var widget = this.buildItem.apply(this, arguments); widget.item = widget.value = item; if (widget.setContent) widget.setContent(item) else widget.set('html', item.toString()); return widget; }, getItems: function() { return this.list; }, hasItems: function() { return this.getItems() && (this.getItems().length > 0) }, getItemIndex: function(item) { return this.getItems().indexOf(item || this.selectedItem); }, findItemByValue: function(value) { for (var i = 0, j = this.widgets.length; i < j; i++) { if (this.widgets[i].value == value) return this.widgets[i]; } return null; }, getItemValue: function(item) { for (var i = 0, j = this.widgets.length; i < j; i++) { if (this.widgets[i] == item) return this.list[i]; } return null; }, getActiveItem: function() { var active = (this.chosenItem || this.selectedItem); return active ? active.value : null; }, next: function(e) { this.makeItems(); var next = this.getItems()[this.getItemIndex(this.getActiveItem()) + 1]; if (!next && this.options.list.endless) next = this.getItems()[0]; if (this.selectItem(next, true, !!e)) { if (e && e.stop) e.stop(); return !!this.fireEvent('next', [next]); } return false; }, previous: function(e) { this.makeItems(); var previous = this.getItems()[this.getItemIndex(this.getActiveItem()) - 1]; if (!previous && this.options.list.endless) previous = this.getItems().getLast(); if (this.selectItem(previous, true)) { if (e && e.stop) e.stop(); return !!this.fireEvent('previous', [previous]); } return false; }, sort: function(sort) { return this.getItems().sort(sort) }, filter: function(filter) { return this.getItems().filter(filter) } });