radiant-page_list_view-extension 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,926 @@
1
+ /*
2
+ *
3
+ * Copyright (c) 2007 Andrew Tetlaw & Millstream Web Software
4
+ * http://www.millstream.com.au/view/code/tablekit/
5
+ * Version: 1.2.1 2007-03-11
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person
8
+ * obtaining a copy of this software and associated documentation
9
+ * files (the "Software"), to deal in the Software without
10
+ * restriction, including without limitation the rights to use, copy,
11
+ * modify, merge, publish, distribute, sublicense, and/or sell copies
12
+ * of the Software, and to permit persons to whom the Software is
13
+ * furnished to do so, subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be
16
+ * included in all copies or substantial portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
+ * SOFTWARE.
26
+ * *
27
+ */
28
+
29
+ /*
30
+ === Changes by Sean Cribbs, March 5, 2008 ===
31
+ * Changed window load observer to Prototype standard dom:loaded
32
+ * Applied patches from mailing list to allow reloading after Ajax calls
33
+ === Changes by Sean Cribbs, August 12, 2008 ===
34
+ * Added cookie storage for remembering which column was sorted and the direction.
35
+ * Added date-us-civil sortable matcher.
36
+ */
37
+
38
+ // Use the TableKit class constructure if you'd prefer to init your tables as JS objects
39
+ var TableKit = Class.create();
40
+
41
+ TableKit.prototype = {
42
+ initialize : function(elm, options) {
43
+ var table = $(elm);
44
+ if(table.tagName !== "TABLE") {
45
+ return;
46
+ }
47
+ TableKit.register(table,Object.extend(TableKit.options,options || {}));
48
+ this.id = table.id;
49
+ var op = TableKit.option('sortable resizable editable', this.id);
50
+ if(op.sortable) {
51
+ TableKit.Sortable.init(table);
52
+ }
53
+ if(op.resizable) {
54
+ TableKit.Resizable.init(table);
55
+ }
56
+ if(op.editable) {
57
+ TableKit.Editable.init(table);
58
+ }
59
+ },
60
+ sort : function(column, order) {
61
+ TableKit.Sortable.sort(this.id, column, order);
62
+ },
63
+ resizeColumn : function(column, w) {
64
+ TableKit.Resizable.resize(this.id, column, w);
65
+ },
66
+ editCell : function(row, column) {
67
+ TableKit.Editable.editCell(this.id, row, column);
68
+ }
69
+ };
70
+
71
+ Object.extend(TableKit, {
72
+ getBodyRows : function(table) {
73
+ table = $(table);
74
+ var id = table.id;
75
+ if(!TableKit.rows[id]) {
76
+ TableKit.rows[id] = (table.tHead && table.tHead.rows.length > 0) ? $A(table.tBodies[0].rows) : $A(table.rows).without(table.rows[0]);
77
+ }
78
+ return TableKit.rows[id];
79
+ },
80
+ getHeaderCells : function(table, cell) {
81
+ if(!table) { table = $(cell).up('table'); }
82
+ var id = table.id;
83
+ if(!TableKit.heads[id]) {
84
+ TableKit.heads[id] = $A((table.tHead && table.tHead.rows.length > 0) ? table.tHead.rows[table.tHead.rows.length-1].cells : table.rows[0].cells);
85
+ }
86
+ return TableKit.heads[id];
87
+ },
88
+ getCellIndex : function(cell) {
89
+ return $A(cell.parentNode.cells).indexOf(cell);
90
+ },
91
+ getRowIndex : function(row) {
92
+ return $A(row.parentNode.rows).indexOf(row);
93
+ },
94
+ getCellText : function(cell, refresh) {
95
+ if(!cell) { return ""; }
96
+ TableKit.registerCell(cell);
97
+ var data = TableKit.cells[cell.id];
98
+ if(refresh || data.refresh || !data.textContent) {
99
+ data.textContent = cell.textContent ? cell.textContent : cell.innerText;
100
+ data.refresh = false;
101
+ }
102
+ return data.textContent;
103
+ },
104
+ deregister: function(){
105
+ TableKit.tables={};
106
+ TableKit.rows={};
107
+ TableKit.cells={};
108
+ TableKit.heads={};
109
+ TableKit._opcache={};
110
+ TableKit._tblcount = 0;
111
+ TableKit._cellcount = 0;
112
+ },
113
+ register : function(table, options) {
114
+ if(!table.id) {
115
+ TableKit._tblcount += 1;
116
+ table.id = "tablekit-table-" + TableKit._tblcount;
117
+ }
118
+ var id = table.id;
119
+ TableKit.tables[id] = TableKit.tables[id] ? Object.extend(TableKit.tables[id], options || {}) : Object.extend({sortable:false,resizable:false,editable:false}, options || {});
120
+ },
121
+ registerCell : function(cell) {
122
+ if(!cell.id) {
123
+ TableKit._cellcount += 1;
124
+ cell.id = "tablekit-cell-" + TableKit._cellcount;
125
+ }
126
+ if(!TableKit.cells[cell.id]) {
127
+ TableKit.cells[cell.id] = {textContent : '', htmlContent : '', active : false};
128
+ }
129
+ },
130
+ isSortable : function(table) {
131
+ return TableKit.tables[table.id] ? TableKit.tables[table.id].sortable : false;
132
+ },
133
+ isResizable : function(table) {
134
+ return TableKit.tables[table.id] ? TableKit.tables[table.id].resizable : false;
135
+ },
136
+ isEditable : function(table) {
137
+ return TableKit.tables[table.id] ? TableKit.tables[table.id].editable : false;
138
+ },
139
+ setup : function(o) {
140
+ Object.extend(TableKit.options, o || {} );
141
+ },
142
+ option : function(s, id, o1, o2) {
143
+ o1 = o1 || TableKit.options;
144
+ o2 = o2 || (id ? (TableKit.tables[id] ? TableKit.tables[id] : {}) : {});
145
+ var key = id + s;
146
+ if(!TableKit._opcache[key]){
147
+ TableKit._opcache[key] = $A($w(s)).inject([],function(a,v){
148
+ a.push(a[v] = o2[v] || o1[v]);
149
+ return a;
150
+ });
151
+ }
152
+ return TableKit._opcache[key];
153
+ },
154
+ e : function(event) {
155
+ return event || window.event;
156
+ },
157
+ tables : {},
158
+ _opcache : {},
159
+ cells : {},
160
+ rows : {},
161
+ heads : {},
162
+ options : {
163
+ autoLoad : true,
164
+ stripe : true,
165
+ sortable : true,
166
+ resizable : true,
167
+ editable : true,
168
+ rowEvenClass : 'roweven',
169
+ rowOddClass : 'rowodd',
170
+ sortableSelector : ['table.sortable'],
171
+ columnClass : 'sortcol',
172
+ descendingClass : 'sortdesc',
173
+ ascendingClass : 'sortasc',
174
+ noSortClass : 'nosort',
175
+ sortFirstAscendingClass : 'sortfirstasc',
176
+ sortFirstDecendingClass : 'sortfirstdesc',
177
+ resizableSelector : ['table.resizable'],
178
+ minWidth : 10,
179
+ showHandle : true,
180
+ resizeOnHandleClass : 'resize-handle-active',
181
+ editableSelector : ['table.editable'],
182
+ formClassName : 'editable-cell-form',
183
+ noEditClass : 'noedit',
184
+ editAjaxURI : '/',
185
+ editAjaxOptions : {},
186
+ reloadAfterAjax: true
187
+ },
188
+ _tblcount : 0,
189
+ _cellcount : 0,
190
+ load : function() {
191
+ if(TableKit.options.autoLoad) {
192
+ TableKit._autoload();
193
+ }
194
+ if (TableKit.options.reloadAfterAjax) {
195
+ Ajax.Responders.register({
196
+ onComplete : function() {
197
+ clearTimeout(TableKit.reloadTimeout);
198
+ TableKit.reloadTimeout = setTimeout(TableKit._autoload, 10);
199
+ }
200
+ });
201
+ }
202
+ },
203
+ _autoload: function() {
204
+ TableKit.deregister();
205
+ if(TableKit.options.sortable) {
206
+ $A(TableKit.options.sortableSelector).each(function(s){
207
+ $$(s).each(function(t) {
208
+ TableKit.Sortable.init(t);
209
+ });
210
+ });
211
+ }
212
+ if(TableKit.options.resizable) {
213
+ $A(TableKit.options.resizableSelector).each(function(s){
214
+ $$(s).each(function(t) {
215
+ TableKit.Resizable.init(t);
216
+ });
217
+ });
218
+ }
219
+ if(TableKit.options.editable) {
220
+ $A(TableKit.options.editableSelector).each(function(s){
221
+ $$(s).each(function(t) {
222
+ TableKit.Editable.init(t);
223
+ });
224
+ });
225
+ }
226
+ }
227
+ });
228
+
229
+ TableKit.Rows = {
230
+ stripe : function(table) {
231
+ var rows = TableKit.getBodyRows(table);
232
+ rows.each(function(r,i) {
233
+ TableKit.Rows.addStripeClass(table,r,i);
234
+ });
235
+ },
236
+ addStripeClass : function(t,r,i) {
237
+ t = t || r.up('table');
238
+ var op = TableKit.option('rowEvenClass rowOddClass', t.id);
239
+ var css = ((i+1)%2 === 0 ? op[0] : op[1]);
240
+ // using prototype's assClassName/RemoveClassName was not efficient for large tables, hence:
241
+ var cn = r.className.split(/\s+/);
242
+ var newCn = [];
243
+ for(var x = 0, l = cn.length; x < l; x += 1) {
244
+ if(cn[x] !== op[0] && cn[x] !== op[1]) { newCn.push(cn[x]); }
245
+ }
246
+ newCn.push(css);
247
+ r.className = newCn.join(" ");
248
+ }
249
+ };
250
+
251
+ TableKit.Sortable = {
252
+ init : function(elm, options){
253
+ var table = $(elm);
254
+ if(table.tagName !== "TABLE") {
255
+ return;
256
+ }
257
+ TableKit.register(table,Object.extend(options || {},{sortable:true}));
258
+ var sortFirst;
259
+ var cells = TableKit.getHeaderCells(table);
260
+ var op = TableKit.option('noSortClass columnClass sortFirstAscendingClass sortFirstDecendingClass', table.id);
261
+ cells.each(function(c){
262
+ c = $(c);
263
+ if(!c.hasClassName(op.noSortClass)) {
264
+ Event.observe(c, 'mousedown', TableKit.Sortable._sort);
265
+ c.addClassName(op.columnClass);
266
+ if(c.hasClassName(op.sortFirstAscendingClass) || c.hasClassName(op.sortFirstDecendingClass)) {
267
+ sortFirst = c;
268
+ }
269
+ }
270
+ });
271
+
272
+ if(sortFirst) {
273
+ if(sortFirst.hasClassName(op.sortFirstAscendingClass)) {
274
+ TableKit.Sortable.sort(table, sortFirst, 1);
275
+ } else {
276
+ TableKit.Sortable.sort(table, sortFirst, -1);
277
+ }
278
+ } else { // just add row stripe classes
279
+ TableKit.Rows.stripe(table);
280
+ }
281
+ },
282
+ reload : function(table) {
283
+ table = $(table);
284
+ var cells = TableKit.getHeaderCells(table);
285
+ var op = TableKit.option('noSortClass columnClass', table.id);
286
+ cells.each(function(c){
287
+ c = $(c);
288
+ if(!c.hasClassName(op.noSortClass)) {
289
+ Event.stopObserving(c, 'mousedown', TableKit.Sortable._sort);
290
+ c.removeClassName(op.columnClass);
291
+ }
292
+ });
293
+ TableKit.Sortable.init(table);
294
+ },
295
+ _sort : function(e) {
296
+ if(TableKit.Resizable._onHandle) {return;}
297
+ e = TableKit.e(e);
298
+ Event.stop(e);
299
+ var cell = Event.element(e);
300
+ while(!(cell.tagName && cell.tagName.match(/td|th/gi))) {
301
+ cell = cell.parentNode;
302
+ }
303
+ TableKit.Sortable.sort(null, cell);
304
+ },
305
+ sort : function(table, index, order) {
306
+ var cell;
307
+ if(typeof index === 'number') {
308
+ if(!table || (table.tagName && table.tagName !== "TABLE")) {
309
+ return;
310
+ }
311
+ table = $(table);
312
+ index = Math.min(table.rows[0].cells.length, index);
313
+ index = Math.max(1, index);
314
+ index -= 1;
315
+ cell = (table.tHead && table.tHead.rows.length > 0) ? $(table.tHead.rows[table.tHead.rows.length-1].cells[index]) : $(table.rows[0].cells[index]);
316
+ } else {
317
+ cell = $(index);
318
+ table = table ? $(table) : cell.up('table');
319
+ index = TableKit.getCellIndex(cell);
320
+ }
321
+ var op = TableKit.option('noSortClass descendingClass ascendingClass', table.id);
322
+
323
+ if(cell.hasClassName(op.noSortClass)) {return;}
324
+
325
+ order = order ? order : (cell.hasClassName(op.descendingClass) ? 1 : -1);
326
+ var rows = TableKit.getBodyRows(table);
327
+
328
+ if(cell.hasClassName(op.ascendingClass) || cell.hasClassName(op.descendingClass)) {
329
+ rows.reverse(); // if it was already sorted we just need to reverse it.
330
+ } else {
331
+ var datatype = TableKit.Sortable.getDataType(cell,index,table);
332
+ var tkst = TableKit.Sortable.types;
333
+ rows.sort(function(a,b) {
334
+ return order * tkst[datatype].compare(TableKit.getCellText(a.cells[index]),TableKit.getCellText(b.cells[index]));
335
+ });
336
+ }
337
+ var tb = table.tBodies[0];
338
+ var tkr = TableKit.Rows;
339
+ rows.each(function(r,i) {
340
+ tb.appendChild(r);
341
+ tkr.addStripeClass(table,r,i);
342
+ });
343
+ var hcells = TableKit.getHeaderCells(null, cell);
344
+ $A(hcells).each(function(c,i){
345
+ c = $(c);
346
+ c.removeClassName(op.ascendingClass);
347
+ c.removeClassName(op.descendingClass);
348
+ if(index === i) {
349
+ if(order === 1) {
350
+ c.removeClassName(op.descendingClass);
351
+ c.addClassName(op.ascendingClass);
352
+ } else {
353
+ c.removeClassName(op.ascendingClass);
354
+ c.addClassName(op.descendingClass);
355
+ }
356
+ }
357
+ });
358
+ TableKit.Sortable.rememberOrder(table, cell, order);
359
+ },
360
+ types : {},
361
+ detectors : [],
362
+ addSortType : function() {
363
+ $A(arguments).each(function(o){
364
+ TableKit.Sortable.types[o.name] = o;
365
+ });
366
+ },
367
+ getDataType : function(cell,index,table) {
368
+ cell = $(cell);
369
+ index = (index || index === 0) ? index : TableKit.getCellIndex(cell);
370
+
371
+ var colcache = TableKit.Sortable._coltypecache;
372
+ var cache = colcache[table.id] ? colcache[table.id] : (colcache[table.id] = {});
373
+
374
+ if(!cache[index]) {
375
+ var t = '';
376
+ // first look for a data type id on the heading row cell
377
+ if(cell.id && TableKit.Sortable.types[cell.id]) {
378
+ t = cell.id;
379
+ }
380
+ t = cell.classNames().detect(function(n){ // then look for a data type classname on the heading row cell
381
+ return (TableKit.Sortable.types[n]) ? true : false;
382
+ });
383
+ if(!t) {
384
+ var rows = TableKit.getBodyRows(table);
385
+ cell = rows[0].cells[index]; // grab same index cell from body row to try and match data type
386
+ t = TableKit.Sortable.detectors.detect(
387
+ function(d){
388
+ return TableKit.Sortable.types[d].detect(TableKit.getCellText(cell));
389
+ });
390
+ }
391
+ cache[index] = t;
392
+ }
393
+ return cache[index];
394
+ },
395
+ _coltypecache : {},
396
+ rememberOrder: function(table, cell, order){
397
+ var index = $(cell).previousSiblings().length;
398
+ Cookies.set('table_kit_order', encodeURIComponent([index, order].toJSON()));
399
+ }
400
+ };
401
+
402
+ TableKit.Sortable.detectors = $A($w('date-iso date date-eu date-au time currency datasize number casesensitivetext text')); // setting it here because Safari complained when I did it above...
403
+
404
+ TableKit.Sortable.Type = Class.create();
405
+ TableKit.Sortable.Type.prototype = {
406
+ initialize : function(name, options){
407
+ this.name = name;
408
+ options = Object.extend({
409
+ normal : function(v){
410
+ return v;
411
+ },
412
+ pattern : /.*/
413
+ }, options || {});
414
+ this.normal = options.normal;
415
+ this.pattern = options.pattern;
416
+ if(options.compare) {
417
+ this.compare = options.compare;
418
+ }
419
+ if(options.detect) {
420
+ this.detect = options.detect;
421
+ }
422
+ },
423
+ compare : function(a,b){
424
+ return TableKit.Sortable.Type.compare(this.normal(a), this.normal(b));
425
+ },
426
+ detect : function(v){
427
+ return this.pattern.test(v);
428
+ }
429
+ };
430
+
431
+ TableKit.Sortable.Type.compare = function(a,b) {
432
+ return a < b ? -1 : a === b ? 0 : 1;
433
+ };
434
+
435
+ TableKit.Sortable.addSortType(
436
+ new TableKit.Sortable.Type('number', {
437
+ pattern : /^[-+]?[\d]*\.?[\d]+(?:[eE][-+]?[\d]+)?/,
438
+ normal : function(v) {
439
+ // This will grab the first thing that looks like a number from a string, so you can use it to order a column of various srings containing numbers.
440
+ v = parseFloat(v.replace(/^.*?([-+]?[\d]*\.?[\d]+(?:[eE][-+]?[\d]+)?).*$/,"$1"));
441
+ return isNaN(v) ? 0 : v;
442
+ }}),
443
+ new TableKit.Sortable.Type('text',{
444
+ normal : function(v) {
445
+ return v ? v.toLowerCase() : '';
446
+ }}),
447
+ new TableKit.Sortable.Type('casesensitivetext',{pattern : /^[A-Z]+$/}),
448
+ new TableKit.Sortable.Type('datasize',{
449
+ pattern : /^[-+]?[\d]*\.?[\d]+(?:[eE][-+]?[\d]+)?\s?[k|m|g|t]b$/i,
450
+ normal : function(v) {
451
+ var r = v.match(/^([-+]?[\d]*\.?[\d]+([eE][-+]?[\d]+)?)\s?([k|m|g|t]?b)?/i);
452
+ var b = r[1] ? Number(r[1]).valueOf() : 0;
453
+ var m = r[3] ? r[3].substr(0,1).toLowerCase() : '';
454
+ var result = b;
455
+ switch(m) {
456
+ case 'k':
457
+ result = b * 1024;
458
+ break;
459
+ case 'm':
460
+ result = b * 1024 * 1024;
461
+ break;
462
+ case 'g':
463
+ result = b * 1024 * 1024 * 1024;
464
+ break;
465
+ case 't':
466
+ result = b * 1024 * 1024 * 1024 * 1024;
467
+ break;
468
+ }
469
+ return result;
470
+ }}),
471
+ new TableKit.Sortable.Type('date-au',{
472
+ pattern : /^\d{2}\/\d{2}\/\d{4}\s?(?:\d{1,2}\:\d{2}(?:\:\d{2})?\s?[a|p]?m?)?/i,
473
+ normal : function(v) {
474
+ if(!this.pattern.test(v)) {return 0;}
475
+ var r = v.match(/^(\d{2})\/(\d{2})\/(\d{4})\s?(?:(\d{1,2})\:(\d{2})(?:\:(\d{2}))?\s?([a|p]?m?))?/i);
476
+ var yr_num = r[3];
477
+ var mo_num = parseInt(r[2],10)-1;
478
+ var day_num = r[1];
479
+ var hr_num = r[4] ? r[4] : 0;
480
+ if(r[7] && r[7].toLowerCase().indexOf('p') !== -1) {
481
+ hr_num = parseInt(r[4],10) + 12;
482
+ }
483
+ var min_num = r[5] ? r[5] : 0;
484
+ var sec_num = r[6] ? r[6] : 0;
485
+ return new Date(yr_num, mo_num, day_num, hr_num, min_num, sec_num, 0).valueOf();
486
+ }}),
487
+ new TableKit.Sortable.Type('date-us',{
488
+ pattern : /^\d{2}\/\d{2}\/\d{4}\s?(?:\d{1,2}\:\d{2}(?:\:\d{2})?\s?[a|p]?m?)?/i,
489
+ normal : function(v) {
490
+ if(!this.pattern.test(v)) {return 0;}
491
+ var r = v.match(/^(\d{2})\/(\d{2})\/(\d{4})\s?(?:(\d{1,2})\:(\d{2})(?:\:(\d{2}))?\s?([a|p]?m?))?/i);
492
+ var yr_num = r[3];
493
+ var mo_num = parseInt(r[1],10)-1;
494
+ var day_num = r[2];
495
+ var hr_num = r[4] ? r[4] : 0;
496
+ if(r[7] && r[7].toLowerCase().indexOf('p') !== -1) {
497
+ hr_num = parseInt(r[4],10) + 12;
498
+ }
499
+ var min_num = r[5] ? r[5] : 0;
500
+ var sec_num = r[6] ? r[6] : 0;
501
+ return new Date(yr_num, mo_num, day_num, hr_num, min_num, sec_num, 0).valueOf();
502
+ }}),
503
+ new TableKit.Sortable.Type('date-eu',{
504
+ pattern : /^\d{2}-\d{2}-\d{4}/i,
505
+ normal : function(v) {
506
+ if(!this.pattern.test(v)) {return 0;}
507
+ var r = v.match(/^(\d{2})-(\d{2})-(\d{4})/);
508
+ var yr_num = r[3];
509
+ var mo_num = parseInt(r[2],10)-1;
510
+ var day_num = r[1];
511
+ return new Date(yr_num, mo_num, day_num).valueOf();
512
+ }}),
513
+ new TableKit.Sortable.Type('date-iso',{
514
+ pattern : /[\d]{4}-[\d]{2}-[\d]{2}(?:T[\d]{2}\:[\d]{2}(?:\:[\d]{2}(?:\.[\d]+)?)?(Z|([-+][\d]{2}:[\d]{2})?)?)?/, // 2005-03-26T19:51:34Z
515
+ normal : function(v) {
516
+ if(!this.pattern.test(v)) {return 0;}
517
+ var d = v.match(/([\d]{4})(-([\d]{2})(-([\d]{2})(T([\d]{2}):([\d]{2})(:([\d]{2})(\.([\d]+))?)?(Z|(([-+])([\d]{2}):([\d]{2})))?)?)?)?/);
518
+ var offset = 0;
519
+ var date = new Date(d[1], 0, 1);
520
+ if (d[3]) { date.setMonth(d[3] - 1) ;}
521
+ if (d[5]) { date.setDate(d[5]); }
522
+ if (d[7]) { date.setHours(d[7]); }
523
+ if (d[8]) { date.setMinutes(d[8]); }
524
+ if (d[10]) { date.setSeconds(d[10]); }
525
+ if (d[12]) { date.setMilliseconds(Number("0." + d[12]) * 1000); }
526
+ if (d[14]) {
527
+ offset = (Number(d[16]) * 60) + Number(d[17]);
528
+ offset *= ((d[15] === '-') ? 1 : -1);
529
+ }
530
+ offset -= date.getTimezoneOffset();
531
+ if(offset !== 0) {
532
+ var time = (Number(date) + (offset * 60 * 1000));
533
+ date.setTime(Number(time));
534
+ }
535
+ return date.valueOf();
536
+ }}),
537
+ new TableKit.Sortable.Type('date',{
538
+ pattern: /^(?:sun|mon|tue|wed|thu|fri|sat)\,\s\d{1,2}\s(?:jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\s\d{4}(?:\s\d{2}\:\d{2}(?:\:\d{2})?(?:\sGMT(?:[+-]\d{4})?)?)?/i, //Mon, 18 Dec 1995 17:28:35 GMT
539
+ compare : function(a,b) { // must be standard javascript date format
540
+ if(a && b) {
541
+ return TableKit.Sortable.Type.compare(new Date(a),new Date(b));
542
+ } else {
543
+ return TableKit.Sortable.Type.compare(a ? 1 : 0, b ? 1 : 0);
544
+ }
545
+ }}),
546
+ new TableKit.Sortable.Type('time',{
547
+ pattern : /^\d{1,2}\:\d{2}(?:\:\d{2})?(?:\s[a|p]m)?$/i,
548
+ compare : function(a,b) {
549
+ var d = new Date();
550
+ var ds = d.getMonth() + "/" + d.getDate() + "/" + d.getFullYear() + " ";
551
+ return TableKit.Sortable.Type.compare(new Date(ds + a),new Date(ds + b));
552
+ }}),
553
+ new TableKit.Sortable.Type('currency',{
554
+ pattern : /^[$����]/, // dollar,pound,yen,euro,generic currency symbol
555
+ normal : function(v) {
556
+ return v ? parseFloat(v.replace(/[^-\d\.]/g,'')) : 0;
557
+ }})
558
+ );
559
+
560
+ TableKit.Resizable = {
561
+ init : function(elm, options){
562
+ var table = $(elm);
563
+ if(table.tagName !== "TABLE") {return;}
564
+ TableKit.register(table,Object.extend(options || {},{resizable:true}));
565
+ var cells = TableKit.getHeaderCells(table);
566
+ cells.each(function(c){
567
+ c = $(c);
568
+ Event.observe(c, 'mouseover', TableKit.Resizable.initDetect);
569
+ Event.observe(c, 'mouseout', TableKit.Resizable.killDetect);
570
+ });
571
+ },
572
+ resize : function(table, index, w) {
573
+ var cell;
574
+ if(typeof index === 'number') {
575
+ if(!table || (table.tagName && table.tagName !== "TABLE")) {return;}
576
+ table = $(table);
577
+ index = Math.min(table.rows[0].cells.length, index);
578
+ index = Math.max(1, index);
579
+ index -= 1;
580
+ cell = (table.tHead && table.tHead.rows.length > 0) ? $(table.tHead.rows[table.tHead.rows.length-1].cells[index]) : $(table.rows[0].cells[index]);
581
+ } else {
582
+ cell = $(index);
583
+ table = table ? $(table) : cell.up('table');
584
+ index = TableKit.getCellIndex(cell);
585
+ }
586
+ var pad = parseInt(cell.getStyle('paddingLeft'),10) + parseInt(cell.getStyle('paddingRight'),10);
587
+ w = Math.max(w-pad, TableKit.option('minWidth', table.id)[0]);
588
+
589
+ cell.setStyle({'width' : w + 'px'});
590
+ },
591
+ initDetect : function(e) {
592
+ e = TableKit.e(e);
593
+ var cell = Event.element(e);
594
+ Event.observe(cell, 'mousemove', TableKit.Resizable.detectHandle);
595
+ Event.observe(cell, 'mousedown', TableKit.Resizable.startResize);
596
+ },
597
+ detectHandle : function(e) {
598
+ e = TableKit.e(e);
599
+ var cell = Event.element(e);
600
+ if(TableKit.Resizable.pointerPos(cell,Event.pointerX(e),Event.pointerY(e))){
601
+ cell.addClassName(TableKit.option('resizeOnHandleClass', cell.up('table').id)[0]);
602
+ TableKit.Resizable._onHandle = true;
603
+ } else {
604
+ cell.removeClassName(TableKit.option('resizeOnHandleClass', cell.up('table').id)[0]);
605
+ TableKit.Resizable._onHandle = false;
606
+ }
607
+ },
608
+ killDetect : function(e) {
609
+ e = TableKit.e(e);
610
+ TableKit.Resizable._onHandle = false;
611
+ var cell = Event.element(e);
612
+ Event.stopObserving(cell, 'mousemove', TableKit.Resizable.detectHandle);
613
+ Event.stopObserving(cell, 'mousedown', TableKit.Resizable.startResize);
614
+ cell.removeClassName(TableKit.option('resizeOnHandleClass', cell.up('table').id)[0]);
615
+ },
616
+ startResize : function(e) {
617
+ e = TableKit.e(e);
618
+ if(!TableKit.Resizable._onHandle) {return;}
619
+ var cell = Event.element(e);
620
+ Event.stopObserving(cell, 'mousemove', TableKit.Resizable.detectHandle);
621
+ Event.stopObserving(cell, 'mousedown', TableKit.Resizable.startResize);
622
+ Event.stopObserving(cell, 'mouseout', TableKit.Resizable.killDetect);
623
+ TableKit.Resizable._cell = cell;
624
+ var table = cell.up('table');
625
+ TableKit.Resizable._tbl = table;
626
+ if(TableKit.option('showHandle', table.id)[0]) {
627
+ TableKit.Resizable._handle = $(document.createElement('div')).addClassName('resize-handle').setStyle({
628
+ 'top' : Position.cumulativeOffset(cell)[1] + 'px',
629
+ 'left' : Event.pointerX(e) + 'px',
630
+ 'height' : table.getDimensions().height + 'px'
631
+ });
632
+ document.body.appendChild(TableKit.Resizable._handle);
633
+ }
634
+ Event.observe(document, 'mousemove', TableKit.Resizable.drag);
635
+ Event.observe(document, 'mouseup', TableKit.Resizable.endResize);
636
+ Event.stop(e);
637
+ },
638
+ endResize : function(e) {
639
+ e = TableKit.e(e);
640
+ var cell = TableKit.Resizable._cell;
641
+ TableKit.Resizable.resize(null, cell, (Event.pointerX(e) - Position.cumulativeOffset(cell)[0]));
642
+ Event.stopObserving(document, 'mousemove', TableKit.Resizable.drag);
643
+ Event.stopObserving(document, 'mouseup', TableKit.Resizable.endResize);
644
+ if(TableKit.option('showHandle', TableKit.Resizable._tbl.id)[0]) {
645
+ $$('div.resize-handle').each(function(elm){
646
+ document.body.removeChild(elm);
647
+ });
648
+ }
649
+ Event.observe(cell, 'mouseout', TableKit.Resizable.killDetect);
650
+ TableKit.Resizable._tbl = TableKit.Resizable._handle = TableKit.Resizable._cell = null;
651
+ Event.stop(e);
652
+ },
653
+ drag : function(e) {
654
+ e = TableKit.e(e);
655
+ if(TableKit.Resizable._handle === null) {
656
+ try {
657
+ TableKit.Resizable.resize(TableKit.Resizable._tbl, TableKit.Resizable._cell, (Event.pointerX(e) - Position.cumulativeOffset(TableKit.Resizable._cell)[0]));
658
+ } catch(e) {}
659
+ } else {
660
+ TableKit.Resizable._handle.setStyle({'left' : Event.pointerX(e) + 'px'});
661
+ }
662
+ return false;
663
+ },
664
+ pointerPos : function(element, x, y) {
665
+ var offset = Position.cumulativeOffset(element);
666
+ return (y >= offset[1] &&
667
+ y < offset[1] + element.offsetHeight &&
668
+ x >= offset[0] + element.offsetWidth - 5 &&
669
+ x < offset[0] + element.offsetWidth);
670
+ },
671
+ _onHandle : false,
672
+ _cell : null,
673
+ _tbl : null,
674
+ _handle : null
675
+ };
676
+
677
+
678
+ TableKit.Editable = {
679
+ init : function(elm, options){
680
+ var table = $(elm);
681
+ if(table.tagName !== "TABLE") {return;}
682
+ TableKit.register(table,Object.extend(options || {},{editable:true}));
683
+ Event.observe(table.tBodies[0], 'click', TableKit.Editable._editCell);
684
+ },
685
+ _editCell : function(e) {
686
+ e = TableKit.e(e);
687
+ var cell = Event.findElement(e,'td');
688
+ TableKit.Editable.editCell(null, cell);
689
+ },
690
+ editCell : function(table, index, cindex) {
691
+ var cell, row;
692
+ if(typeof index === 'number') {
693
+ if(!table || (table.tagName && table.tagName !== "TABLE")) {return;}
694
+ table = $(table);
695
+ index = Math.min(table.tBodies[0].rows.length, index);
696
+ index = Math.max(1, index);
697
+ index -= 1;
698
+ cindex = Math.min(table.rows[0].cells.length, cindex);
699
+ cindex = Math.max(1, cindex);
700
+ cindex -= 1;
701
+ row = $(table.tBodies[0].rows[index]);
702
+ cell = $(row.cells[cindex]);
703
+ } else {
704
+ cell = $(index);
705
+ table = (table && table.tagName && table.tagName !== "TABLE") ? $(table) : cell.up('table');
706
+ row = cell.up('tr');
707
+ }
708
+ var op = TableKit.option('noEditClass', table.id);
709
+ if(cell.hasClassName(op.noEditClass)) {return;}
710
+
711
+ var head = $(TableKit.getHeaderCells(table, cell)[TableKit.getCellIndex(cell)]);
712
+ if(head.hasClassName(op.noEditClass)) {return;}
713
+
714
+ TableKit.registerCell(cell);
715
+ var data = TableKit.cells[cell.id];
716
+ if(data.active) {return;}
717
+ data.htmlContent = cell.innerHTML;
718
+ var ftype = TableKit.Editable.types['text-input'];
719
+ if(head.id && TableKit.Editable.types[head.id]) {
720
+ ftype = TableKit.Editable.types[head.id];
721
+ } else {
722
+ var n = head.classNames().detect(function(n){
723
+ return (TableKit.Editable.types[n]) ? true : false;
724
+ });
725
+ ftype = n ? TableKit.Editable.types[n] : ftype;
726
+ }
727
+ ftype.edit(cell);
728
+ data.active = true;
729
+ },
730
+ types : {},
731
+ addCellEditor : function(o) {
732
+ if(o && o.name) { TableKit.Editable.types[o.name] = o; }
733
+ }
734
+ };
735
+
736
+ TableKit.Editable.CellEditor = Class.create();
737
+ TableKit.Editable.CellEditor.prototype = {
738
+ initialize : function(name, options){
739
+ this.name = name;
740
+ this.options = Object.extend({
741
+ element : 'input',
742
+ attributes : {name : 'value', type : 'text'},
743
+ selectOptions : [],
744
+ showSubmit : true,
745
+ submitText : 'OK',
746
+ showCancel : true,
747
+ cancelText : 'Cancel',
748
+ ajaxURI : null,
749
+ ajaxOptions : null
750
+ }, options || {});
751
+ },
752
+ edit : function(cell) {
753
+ cell = $(cell);
754
+ var op = this.options;
755
+ var table = cell.up('table');
756
+
757
+ var form = $(document.createElement("form"));
758
+ form.id = cell.id + '-form';
759
+ form.addClassName(TableKit.option('formClassName', table.id)[0]);
760
+ form.onsubmit = this._submit.bindAsEventListener(this);
761
+
762
+ var field = document.createElement(op.element);
763
+ $H(op.attributes).each(function(v){
764
+ field[v.key] = v.value;
765
+ });
766
+ switch(op.element) {
767
+ case 'input':
768
+ case 'textarea':
769
+ field.value = TableKit.getCellText(cell);
770
+ break;
771
+
772
+ case 'select':
773
+ var txt = TableKit.getCellText(cell);
774
+ $A(op.selectOptions).each(function(v){
775
+ field.options[field.options.length] = new Option(v[0], v[1]);
776
+ if(txt === v[1]) {
777
+ field.options[field.options.length-1].selected = 'selected';
778
+ }
779
+ });
780
+ break;
781
+ }
782
+ form.appendChild(field);
783
+ if(op.element === 'textarea') {
784
+ form.appendChild(document.createElement("br"));
785
+ }
786
+ if(op.showSubmit) {
787
+ var okButton = document.createElement("input");
788
+ okButton.type = "submit";
789
+ okButton.value = op.submitText;
790
+ okButton.className = 'editor_ok_button';
791
+ form.appendChild(okButton);
792
+ }
793
+ if(op.showCancel) {
794
+ var cancelLink = document.createElement("a");
795
+ cancelLink.href = "#";
796
+ cancelLink.appendChild(document.createTextNode(op.cancelText));
797
+ cancelLink.onclick = this._cancel.bindAsEventListener(this);
798
+ cancelLink.className = 'editor_cancel';
799
+ form.appendChild(cancelLink);
800
+ }
801
+ cell.innerHTML = '';
802
+ cell.appendChild(form);
803
+ },
804
+ _submit : function(e) {
805
+ var cell = Event.findElement(e,'td');
806
+ var form = Event.findElement(e,'form');
807
+ Event.stop(e);
808
+ this.submit(cell,form);
809
+ },
810
+ submit : function(cell, form) {
811
+ var op = this.options;
812
+ form = form ? form : cell.down('form');
813
+ var head = $(TableKit.getHeaderCells(null, cell)[TableKit.getCellIndex(cell)]);
814
+ var row = cell.up('tr');
815
+ var table = cell.up('table');
816
+ var s = '&row=' + (TableKit.getRowIndex(row)+1) + '&cell=' + (TableKit.getCellIndex(cell)+1) + '&id=' + row.id + '&field=' + head.id + '&' + Form.serialize(form);
817
+ this.ajax = new Ajax.Updater(cell, op.ajaxURI || TableKit.option('editAjaxURI', table.id)[0], Object.extend(op.ajaxOptions || TableKit.option('editAjaxOptions', table.id)[0], {
818
+ postBody : s,
819
+ onComplete : function() {
820
+ var data = TableKit.cells[cell.id];
821
+ data.active = false;
822
+ data.refresh = true; // mark cell cache for refreshing, in case cell contents has changed and sorting is applied
823
+ }
824
+ }));
825
+ },
826
+ _cancel : function(e) {
827
+ var cell = Event.findElement(e,'td');
828
+ Event.stop(e);
829
+ this.cancel(cell);
830
+ },
831
+ cancel : function(cell) {
832
+ this.ajax = null;
833
+ var data = TableKit.cells[cell.id];
834
+ cell.innerHTML = data.htmlContent;
835
+ data.htmlContent = '';
836
+ data.active = false;
837
+ },
838
+ ajax : null
839
+ };
840
+
841
+ TableKit.Editable.textInput = function(n,attributes) {
842
+ TableKit.Editable.addCellEditor(new TableKit.Editable.CellEditor(n, {
843
+ element : 'input',
844
+ attributes : Object.extend({name : 'value', type : 'text'}, attributes||{})
845
+ }));
846
+ };
847
+ TableKit.Editable.textInput('text-input');
848
+
849
+ TableKit.Editable.multiLineInput = function(n,attributes) {
850
+ TableKit.Editable.addCellEditor(new TableKit.Editable.CellEditor(n, {
851
+ element : 'textarea',
852
+ attributes : Object.extend({name : 'value', rows : '5', cols : '20'}, attributes||{})
853
+ }));
854
+ };
855
+ TableKit.Editable.multiLineInput('multi-line-input');
856
+
857
+ TableKit.Editable.selectInput = function(n,attributes,selectOptions) {
858
+ TableKit.Editable.addCellEditor(new TableKit.Editable.CellEditor(n, {
859
+ element : 'select',
860
+ attributes : Object.extend({name : 'value'}, attributes||{}),
861
+ 'selectOptions' : selectOptions
862
+ }));
863
+ };
864
+
865
+ // Based on http://www.quirksmode.org/js/cookies.html
866
+ var Cookies = {
867
+ set: function(name, value, days){
868
+ if (days) {
869
+ var date = new Date();
870
+ date.setTime(date.getTime()+(days*24*60*60*1000));
871
+ var expires = "; expires="+date.toGMTString();
872
+ }
873
+ else var expires = "";
874
+ document.cookie = name+"="+value+expires+"; path=/";
875
+ },
876
+ read: function(name) {
877
+ var nameEQ = name + "=";
878
+ var ca = document.cookie.split(';');
879
+ for(var i=0;i < ca.length;i++) {
880
+ var c = ca[i];
881
+ while (c.charAt(0)==' ') c = c.substring(1,c.length);
882
+ if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
883
+ }
884
+ return null;
885
+ },
886
+ erase: function(name) {
887
+ Cookies.set(name,"",-1);
888
+ }
889
+ };
890
+
891
+ // SDC ADDED
892
+ TableKit.Sortable.addSortType(
893
+ new TableKit.Sortable.Type('date-us-civil', {
894
+ pattern: /(\d{2})\/(\d{2})\/(\d{4})\s+at\s+(\d{2}):(\d{2})\s+(am|pm)/i,
895
+ normal: function(v){
896
+ var value = v.stripTags().strip();
897
+ if(!this.pattern.test(value)){ return 0; }
898
+ var pieces = value.match(this.pattern);
899
+ var year = pieces[3];
900
+ var month = parseInt(pieces[1],10)-1;
901
+ var day = pieces[2];
902
+ var hour = parseInt(pieces[4],10);
903
+ if(pieces[6].toLowerCase() == 'pm') { hour += 12; }
904
+ var minute = pieces[5];
905
+ var date = new Date(year, month, day, hour, minute, 0, 0);
906
+ return date.valueOf();
907
+ }
908
+ })
909
+ );
910
+
911
+ TableKit.Sortable.detectors.push('date-us-civil');
912
+
913
+ /*
914
+ TableKit.Bench = {
915
+ bench : [],
916
+ start : function(){
917
+ TableKit.Bench.bench[0] = new Date().getTime();
918
+ },
919
+ end : function(s){
920
+ TableKit.Bench.bench[1] = new Date().getTime();
921
+ alert(s + ' ' + ((TableKit.Bench.bench[1]-TableKit.Bench.bench[0])/1000)+' seconds.') //console.log(s + ' ' + ((TableKit.Bench.bench[1]-TableKit.Bench.bench[0])/1000)+' seconds.')
922
+ TableKit.Bench.bench = [];
923
+ }
924
+ } */
925
+
926
+ document.observe('dom:loaded', TableKit.load);