jrhicks-static-generators 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. data/CHANGELOG +3 -0
  2. data/LICENSE +21 -0
  3. data/Manifest +32 -0
  4. data/README.md +27 -0
  5. data/Rakefile +15 -0
  6. data/TODO +4 -0
  7. data/USAGE +17 -0
  8. data/lib/static_generators.rb +3 -0
  9. data/rails_generators/static_app/USAGE +0 -0
  10. data/rails_generators/static_app/static_app_generator.rb +26 -0
  11. data/rails_generators/static_app/templates/application_helper.rb +11 -0
  12. data/rails_generators/static_app/templates/layout.html.erb +0 -0
  13. data/rails_generators/static_app/templates/layout_helper.rb +9 -0
  14. data/rails_generators/static_app/templates/sorttable.js +568 -0
  15. data/rails_generators/static_app/templates/static_style.css +90 -0
  16. data/rails_generators/static_gen_specs/USAGE +0 -0
  17. data/rails_generators/static_gen_specs/static_gen_specs_generator.rb +343 -0
  18. data/rails_generators/static_gen_specs/templates/generator_specs.rb +60 -0
  19. data/rails_generators/static_gen_specs/templates/model_gen_specs.rb +145 -0
  20. data/rails_generators/static_scaffold/USAGE +29 -0
  21. data/rails_generators/static_scaffold/static_scaffold_generator.rb +150 -0
  22. data/rails_generators/static_scaffold/templates/controller.rb +85 -0
  23. data/rails_generators/static_scaffold/templates/functional_test.rb +45 -0
  24. data/rails_generators/static_scaffold/templates/helper.rb +2 -0
  25. data/rails_generators/static_scaffold/templates/helper_test.rb +4 -0
  26. data/rails_generators/static_scaffold/templates/layout.html.erb +18 -0
  27. data/rails_generators/static_scaffold/templates/model.rb +109 -0
  28. data/rails_generators/static_scaffold/templates/view_edit.html.erb +19 -0
  29. data/rails_generators/static_scaffold/templates/view_index.html.erb +26 -0
  30. data/rails_generators/static_scaffold/templates/view_new.html.erb +18 -0
  31. data/rails_generators/static_scaffold/templates/view_show.html.erb +13 -0
  32. data/static-generators.gemspec +31 -0
  33. data/tasks/deployment.rake +9 -0
  34. metadata +95 -0
@@ -0,0 +1,3 @@
1
+ 0.2 (June, 2009)
2
+
3
+ * painfully embarrassing
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2009 Jeffrey Hicks
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
@@ -0,0 +1,32 @@
1
+ CHANGELOG
2
+ lib/static_generators.rb
3
+ LICENSE
4
+ rails_generators/static_app/static_app_generator.rb
5
+ rails_generators/static_app/templates/application_helper.rb
6
+ rails_generators/static_app/templates/layout.html.erb
7
+ rails_generators/static_app/templates/layout_helper.rb
8
+ rails_generators/static_app/templates/sorttable.js
9
+ rails_generators/static_app/templates/static_style.css
10
+ rails_generators/static_app/USAGE
11
+ rails_generators/static_gen_specs/static_gen_specs_generator.rb
12
+ rails_generators/static_gen_specs/templates/generator_specs.rb
13
+ rails_generators/static_gen_specs/templates/model_gen_specs.rb
14
+ rails_generators/static_gen_specs/USAGE
15
+ rails_generators/static_scaffold/static_scaffold_generator.rb
16
+ rails_generators/static_scaffold/templates/controller.rb
17
+ rails_generators/static_scaffold/templates/functional_test.rb
18
+ rails_generators/static_scaffold/templates/helper.rb
19
+ rails_generators/static_scaffold/templates/helper_test.rb
20
+ rails_generators/static_scaffold/templates/layout.html.erb
21
+ rails_generators/static_scaffold/templates/model.rb
22
+ rails_generators/static_scaffold/templates/view_edit.html.erb
23
+ rails_generators/static_scaffold/templates/view_index.html.erb
24
+ rails_generators/static_scaffold/templates/view_new.html.erb
25
+ rails_generators/static_scaffold/templates/view_show.html.erb
26
+ rails_generators/static_scaffold/USAGE
27
+ Rakefile
28
+ README.md
29
+ tasks/deployment.rake
30
+ TODO
31
+ USAGE
32
+ Manifest
@@ -0,0 +1,27 @@
1
+ ## Static Scaffolds (Not really ready yet)
2
+
3
+ Generate attractive interfaces that are easy to edit.
4
+
5
+ ## Installation
6
+
7
+ gem sources -a http://gems.github.com
8
+ gem install jrhicks-static-generators
9
+
10
+ ## Usage (Roadmap)
11
+
12
+ ruby script/generate static_app
13
+ ruby script/generate static_gen_specs MyModel
14
+ ruby script/generate static_scaffold MyModel
15
+
16
+ ## Gem Development
17
+
18
+ gem install echoe
19
+ rake manifest
20
+ rake install
21
+ rake build
22
+ rake publish
23
+
24
+ ## Author
25
+
26
+ Jeffrey Hicks
27
+
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('static-generators', '0.1.6') do |p|
6
+ p.project = "staticgenerators"
7
+ p.description = "Generate attractive interfaces that are easy to edit."
8
+ p.url = "http://github.com/jrhicks/static-scaffolds"
9
+ p.author = 'Jeffrey Hicks'
10
+ p.email = "jrhicks (at) gmail (dot) com"
11
+ p.ignore_pattern = ["script/*", "rails_templates/*","Manifest",".gitignor"]
12
+ p.development_dependencies = []
13
+ end
14
+
15
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
data/TODO ADDED
@@ -0,0 +1,4 @@
1
+ Paperclip
2
+ - out with filecolumn and in with paperclip
3
+ - couchdb ??
4
+
data/USAGE ADDED
@@ -0,0 +1,17 @@
1
+ NAME
2
+ static scaffolds - generate attractive interfaces that are easy to edit
3
+
4
+ SYNOPSIS
5
+ static_app
6
+ static_gen_specs
7
+ static_scaffold
8
+
9
+ DESCRIPTION
10
+ Generate static_app once at the beginning of a project. Generate static_gen_specs for each model. The generated gen_specs
11
+ will contain best guesses, but you can edit them before your proceed to generating the scaffold. Last generate the static_scaffold.
12
+
13
+
14
+ EXAMPLE
15
+ ./script/generate static_app
16
+ ./script/generate static_gen_specs MyModel
17
+ ./script/generate generator_scaffold MyModel
@@ -0,0 +1,3 @@
1
+ module StaticGenerators
2
+ # check in rails_generators
3
+ end
File without changes
@@ -0,0 +1,26 @@
1
+ class StaticAppGenerator < Rails::Generator::Base
2
+ def initialize(runtime_args, runtime_options = {})
3
+ super
4
+ @name = 'application'
5
+ end
6
+
7
+ def manifest
8
+ record do |m|
9
+ m.directory 'app/views/layouts'
10
+ m.directory 'public/stylesheets'
11
+ m.directory 'app/helpers'
12
+ m.template "layout.html.erb", "app/views/layouts/application.html.erb"
13
+ m.file "static_style.css", "public/stylesheets/static_style.css"
14
+ m.file "sorttable.js", "public/javascripts/sorttable.js"
15
+ m.file "application_helper.rb", "app/helpers/application_helper.rb"
16
+ m.file "layout_helper.rb", "app/helpers/layout_helper.rb"
17
+ end
18
+ end
19
+
20
+ protected
21
+
22
+ def banner
23
+ "Creates layout, stylesheet, and helper files designed to be compatible with other static_scaffolds"
24
+ end
25
+
26
+ end
@@ -0,0 +1,11 @@
1
+ module ApplicationHelper
2
+
3
+ def data_table_start(name='zebra')
4
+ "<table class='sortable' id='#{name}'>"
5
+ end
6
+
7
+ def data_table_end(name='zebra')
8
+ "</table> <script>stripe('#{name}','#fff', '#edf3fe');</script>"
9
+ end
10
+
11
+ end
@@ -0,0 +1,9 @@
1
+ # These helper methods can be called in your template to set variables to be used in the layout
2
+ # This module should be included in all views globally,
3
+ # to do so you may need to add this line to your ApplicationController
4
+ # helper :layout
5
+ module LayoutHelper
6
+ def title(page_title)
7
+ @content_for_title = page_title.to_s
8
+ end
9
+ end
@@ -0,0 +1,568 @@
1
+ /*
2
+ SortTable
3
+ version 2
4
+ 7th April 2007
5
+ Stuart Langridge, http://www.kryogenix.org/code/browser/sorttable/
6
+
7
+ Instructions:
8
+ Download this file
9
+ Add <script src="sorttable.js"></script> to your HTML
10
+ Add class="sortable" to any table you'd like to make sortable
11
+ Click on the headers to sort
12
+
13
+ Thanks to many, many people for contributions and suggestions.
14
+ Licenced as X11: http://www.kryogenix.org/code/browser/licence.html
15
+ This basically means: do what you want with it.
16
+ */
17
+
18
+
19
+ var stIsIE = /*@cc_on!@*/false;
20
+
21
+ sorttable = {
22
+ init: function() {
23
+ // quit if this function has already been called
24
+ if (arguments.callee.done) return;
25
+ // flag this function so we don't do the same thing twice
26
+ arguments.callee.done = true;
27
+ // kill the timer
28
+ if (_timer) clearInterval(_timer);
29
+
30
+ if (!document.createElement || !document.getElementsByTagName) return;
31
+
32
+ sorttable.DATE_RE = /^(\d\d?)[\/\.-](\d\d?)[\/\.-]((\d\d)?\d\d)$/;
33
+
34
+ forEach(document.getElementsByTagName('table'), function(table) {
35
+ if (table.className.search(/\bsortable\b/) != -1) {
36
+ sorttable.makeSortable(table);
37
+ }
38
+ });
39
+
40
+ },
41
+
42
+ makeSortable: function(table) {
43
+ if (table.getElementsByTagName('thead').length == 0) {
44
+ // table doesn't have a tHead. Since it should have, create one and
45
+ // put the first table row in it.
46
+ the = document.createElement('thead');
47
+ the.appendChild(table.rows[0]);
48
+ table.insertBefore(the,table.firstChild);
49
+ }
50
+ // Safari doesn't support table.tHead, sigh
51
+ if (table.tHead == null) table.tHead = table.getElementsByTagName('thead')[0];
52
+
53
+ if (table.tHead.rows.length != 1) return; // can't cope with two header rows
54
+
55
+ // Sorttable v1 put rows with a class of "sortbottom" at the bottom (as
56
+ // "total" rows, for example). This is B&R, since what you're supposed
57
+ // to do is put them in a tfoot. So, if there are sortbottom rows,
58
+ // for backwards compatibility, move them to tfoot (creating it if needed).
59
+ sortbottomrows = [];
60
+ for (var i=0; i<table.rows.length; i++) {
61
+ if (table.rows[i].className.search(/\bsortbottom\b/) != -1) {
62
+ sortbottomrows[sortbottomrows.length] = table.rows[i];
63
+ }
64
+ }
65
+ if (sortbottomrows) {
66
+ if (table.tFoot == null) {
67
+ // table doesn't have a tfoot. Create one.
68
+ tfo = document.createElement('tfoot');
69
+ table.appendChild(tfo);
70
+ }
71
+ for (var i=0; i<sortbottomrows.length; i++) {
72
+ tfo.appendChild(sortbottomrows[i]);
73
+ }
74
+ delete sortbottomrows;
75
+ }
76
+
77
+ // work through each column and calculate its type
78
+ headrow = table.tHead.rows[0].cells;
79
+ for (var i=0; i<headrow.length; i++) {
80
+ // manually override the type with a sorttable_type attribute
81
+ if (!headrow[i].className.match(/\bsorttable_nosort\b/)) { // skip this col
82
+ mtch = headrow[i].className.match(/\bsorttable_([a-z0-9]+)\b/);
83
+ if (mtch) { override = mtch[1]; }
84
+ if (mtch && typeof sorttable["sort_"+override] == 'function') {
85
+ headrow[i].sorttable_sortfunction = sorttable["sort_"+override];
86
+ } else {
87
+ headrow[i].sorttable_sortfunction = sorttable.guessType(table,i);
88
+ }
89
+ // make it clickable to sort
90
+ headrow[i].sorttable_columnindex = i;
91
+ headrow[i].sorttable_tbody = table.tBodies[0];
92
+ dean_addEvent(headrow[i],"click", function(e) {
93
+
94
+ if (this.className.search(/\bsorttable_sorted\b/) != -1) {
95
+ // if we're already sorted by this column, just
96
+ // reverse the table, which is quicker
97
+ sorttable.reverse(this.sorttable_tbody);
98
+ this.className = this.className.replace('sorttable_sorted',
99
+ 'sorttable_sorted_reverse');
100
+ this.removeChild(document.getElementById('sorttable_sortfwdind'));
101
+ sortrevind = document.createElement('span');
102
+ sortrevind.id = "sorttable_sortrevind";
103
+ sortrevind.innerHTML = stIsIE ? '&nbsp<font face="webdings">5</font>' : '&nbsp;&#x25B4;';
104
+ this.appendChild(sortrevind);
105
+ return;
106
+ }
107
+ if (this.className.search(/\bsorttable_sorted_reverse\b/) != -1) {
108
+ // if we're already sorted by this column in reverse, just
109
+ // re-reverse the table, which is quicker
110
+ sorttable.reverse(this.sorttable_tbody);
111
+ this.className = this.className.replace('sorttable_sorted_reverse',
112
+ 'sorttable_sorted');
113
+ this.removeChild(document.getElementById('sorttable_sortrevind'));
114
+ sortfwdind = document.createElement('span');
115
+ sortfwdind.id = "sorttable_sortfwdind";
116
+ sortfwdind.innerHTML = stIsIE ? '&nbsp<font face="webdings">6</font>' : '&nbsp;&#x25BE;';
117
+ this.appendChild(sortfwdind);
118
+ return;
119
+ }
120
+
121
+ // remove sorttable_sorted classes
122
+ theadrow = this.parentNode;
123
+ forEach(theadrow.childNodes, function(cell) {
124
+ if (cell.nodeType == 1) { // an element
125
+ cell.className = cell.className.replace('sorttable_sorted_reverse','');
126
+ cell.className = cell.className.replace('sorttable_sorted','');
127
+ }
128
+ });
129
+ sortfwdind = document.getElementById('sorttable_sortfwdind');
130
+ if (sortfwdind) { sortfwdind.parentNode.removeChild(sortfwdind); }
131
+ sortrevind = document.getElementById('sorttable_sortrevind');
132
+ if (sortrevind) { sortrevind.parentNode.removeChild(sortrevind); }
133
+
134
+ this.className += ' sorttable_sorted';
135
+ sortfwdind = document.createElement('span');
136
+ sortfwdind.id = "sorttable_sortfwdind";
137
+ sortfwdind.innerHTML = stIsIE ? '&nbsp<font face="webdings">6</font>' : '&nbsp;&#x25BE;';
138
+ this.appendChild(sortfwdind);
139
+
140
+ // build an array to sort. This is a Schwartzian transform thing,
141
+ // i.e., we "decorate" each row with the actual sort key,
142
+ // sort based on the sort keys, and then put the rows back in order
143
+ // which is a lot faster because you only do getInnerText once per row
144
+ row_array = [];
145
+ col = this.sorttable_columnindex;
146
+ rows = this.sorttable_tbody.rows;
147
+ for (var j=0; j<rows.length; j++) {
148
+ row_array[row_array.length] = [sorttable.getInnerText(rows[j].cells[col]), rows[j]];
149
+ }
150
+ /* If you want a stable sort, uncomment the following line */
151
+ //sorttable.shaker_sort(row_array, this.sorttable_sortfunction);
152
+ /* and comment out this one */
153
+ row_array.sort(this.sorttable_sortfunction);
154
+
155
+ tb = this.sorttable_tbody;
156
+ for (var j=0; j<row_array.length; j++) {
157
+ tb.appendChild(row_array[j][1]);
158
+ }
159
+
160
+ delete row_array;
161
+ });
162
+ }
163
+ }
164
+ },
165
+
166
+ guessType: function(table, column) {
167
+ // guess the type of a column based on its first non-blank row
168
+ sortfn = sorttable.sort_alpha;
169
+ for (var i=0; i<table.tBodies[0].rows.length; i++) {
170
+ text = sorttable.getInnerText(table.tBodies[0].rows[i].cells[column]);
171
+ if (text != '') {
172
+ if (text.match(/^-?[�$�]?[\d,.]+%?$/)) {
173
+ return sorttable.sort_numeric;
174
+ }
175
+ // check for a date: dd/mm/yyyy or dd/mm/yy
176
+ // can have / or . or - as separator
177
+ // can be mm/dd as well
178
+ possdate = text.match(sorttable.DATE_RE)
179
+ if (possdate) {
180
+ // looks like a date
181
+ first = parseInt(possdate[1]);
182
+ second = parseInt(possdate[2]);
183
+ if (first > 12) {
184
+ // definitely dd/mm
185
+ return sorttable.sort_ddmm;
186
+ } else if (second > 12) {
187
+ return sorttable.sort_mmdd;
188
+ } else {
189
+ // looks like a date, but we can't tell which, so assume
190
+ // that it's dd/mm (English imperialism!) and keep looking
191
+ sortfn = sorttable.sort_ddmm;
192
+ }
193
+ }
194
+ }
195
+ }
196
+ return sortfn;
197
+ },
198
+
199
+ getInnerText: function(node) {
200
+ // gets the text we want to use for sorting for a cell.
201
+ // strips leading and trailing whitespace.
202
+ // this is *not* a generic getInnerText function; it's special to sorttable.
203
+ // for example, you can override the cell text with a customkey attribute.
204
+ // it also gets .value for <input> fields.
205
+
206
+ hasInputs = (typeof node.getElementsByTagName == 'function') &&
207
+ node.getElementsByTagName('input').length;
208
+
209
+ if (node.getAttribute("sorttable_customkey") != null) {
210
+ return node.getAttribute("sorttable_customkey");
211
+ }
212
+ else if (typeof node.textContent != 'undefined' && !hasInputs) {
213
+ return node.textContent.replace(/^\s+|\s+$/g, '');
214
+ }
215
+ else if (typeof node.innerText != 'undefined' && !hasInputs) {
216
+ return node.innerText.replace(/^\s+|\s+$/g, '');
217
+ }
218
+ else if (typeof node.text != 'undefined' && !hasInputs) {
219
+ return node.text.replace(/^\s+|\s+$/g, '');
220
+ }
221
+ else {
222
+ switch (node.nodeType) {
223
+ case 3:
224
+ if (node.nodeName.toLowerCase() == 'input') {
225
+ return node.value.replace(/^\s+|\s+$/g, '');
226
+ }
227
+ case 4:
228
+ return node.nodeValue.replace(/^\s+|\s+$/g, '');
229
+ break;
230
+ case 1:
231
+ case 11:
232
+ var innerText = '';
233
+ for (var i = 0; i < node.childNodes.length; i++) {
234
+ innerText += sorttable.getInnerText(node.childNodes[i]);
235
+ }
236
+ return innerText.replace(/^\s+|\s+$/g, '');
237
+ break;
238
+ default:
239
+ return '';
240
+ }
241
+ }
242
+ },
243
+
244
+ reverse: function(tbody) {
245
+ // reverse the rows in a tbody
246
+ newrows = [];
247
+ for (var i=0; i<tbody.rows.length; i++) {
248
+ newrows[newrows.length] = tbody.rows[i];
249
+ }
250
+ for (var i=newrows.length-1; i>=0; i--) {
251
+ tbody.appendChild(newrows[i]);
252
+ }
253
+ delete newrows;
254
+ },
255
+
256
+ /* sort functions
257
+ each sort function takes two parameters, a and b
258
+ you are comparing a[0] and b[0] */
259
+ sort_numeric: function(a,b) {
260
+ aa = parseFloat(a[0].replace(/[^0-9.-]/g,''));
261
+ if (isNaN(aa)) aa = 0;
262
+ bb = parseFloat(b[0].replace(/[^0-9.-]/g,''));
263
+ if (isNaN(bb)) bb = 0;
264
+ return aa-bb;
265
+ },
266
+ sort_alpha: function(a,b) {
267
+ if (a[0]==b[0]) return 0;
268
+ if (a[0]<b[0]) return -1;
269
+ return 1;
270
+ },
271
+ sort_ddmm: function(a,b) {
272
+ mtch = a[0].match(sorttable.DATE_RE);
273
+ y = mtch[3]; m = mtch[2]; d = mtch[1];
274
+ if (m.length == 1) m = '0'+m;
275
+ if (d.length == 1) d = '0'+d;
276
+ dt1 = y+m+d;
277
+ mtch = b[0].match(sorttable.DATE_RE);
278
+ y = mtch[3]; m = mtch[2]; d = mtch[1];
279
+ if (m.length == 1) m = '0'+m;
280
+ if (d.length == 1) d = '0'+d;
281
+ dt2 = y+m+d;
282
+ if (dt1==dt2) return 0;
283
+ if (dt1<dt2) return -1;
284
+ return 1;
285
+ },
286
+ sort_mmdd: function(a,b) {
287
+ mtch = a[0].match(sorttable.DATE_RE);
288
+ y = mtch[3]; d = mtch[2]; m = mtch[1];
289
+ if (m.length == 1) m = '0'+m;
290
+ if (d.length == 1) d = '0'+d;
291
+ dt1 = y+m+d;
292
+ mtch = b[0].match(sorttable.DATE_RE);
293
+ y = mtch[3]; d = mtch[2]; m = mtch[1];
294
+ if (m.length == 1) m = '0'+m;
295
+ if (d.length == 1) d = '0'+d;
296
+ dt2 = y+m+d;
297
+ if (dt1==dt2) return 0;
298
+ if (dt1<dt2) return -1;
299
+ return 1;
300
+ },
301
+
302
+ shaker_sort: function(list, comp_func) {
303
+ // A stable sort function to allow multi-level sorting of data
304
+ // see: http://en.wikipedia.org/wiki/Cocktail_sort
305
+ // thanks to Joseph Nahmias
306
+ var b = 0;
307
+ var t = list.length - 1;
308
+ var swap = true;
309
+
310
+ while(swap) {
311
+ swap = false;
312
+ for(var i = b; i < t; ++i) {
313
+ if ( comp_func(list[i], list[i+1]) > 0 ) {
314
+ var q = list[i]; list[i] = list[i+1]; list[i+1] = q;
315
+ swap = true;
316
+ }
317
+ } // for
318
+ t--;
319
+
320
+ if (!swap) break;
321
+
322
+ for(var i = t; i > b; --i) {
323
+ if ( comp_func(list[i], list[i-1]) < 0 ) {
324
+ var q = list[i]; list[i] = list[i-1]; list[i-1] = q;
325
+ swap = true;
326
+ }
327
+ } // for
328
+ b++;
329
+
330
+ } // while(swap)
331
+ }
332
+ }
333
+
334
+ /* ******************************************************************
335
+ Supporting functions: bundled here to avoid depending on a library
336
+ ****************************************************************** */
337
+
338
+ // Dean Edwards/Matthias Miller/John Resig
339
+
340
+ /* for Mozilla/Opera9 */
341
+ if (document.addEventListener) {
342
+ document.addEventListener("DOMContentLoaded", sorttable.init, false);
343
+ }
344
+
345
+ /* for Internet Explorer */
346
+ /*@cc_on @*/
347
+ /*@if (@_win32)
348
+ document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
349
+ var script = document.getElementById("__ie_onload");
350
+ script.onreadystatechange = function() {
351
+ if (this.readyState == "complete") {
352
+ sorttable.init(); // call the onload handler
353
+ }
354
+ };
355
+ /*@end @*/
356
+
357
+ /* for Safari */
358
+ if (/WebKit/i.test(navigator.userAgent)) { // sniff
359
+ var _timer = setInterval(function() {
360
+ if (/loaded|complete/.test(document.readyState)) {
361
+ sorttable.init(); // call the onload handler
362
+ }
363
+ }, 10);
364
+ }
365
+
366
+ /* for other browsers */
367
+ window.onload = sorttable.init;
368
+
369
+ // written by Dean Edwards, 2005
370
+ // with input from Tino Zijdel, Matthias Miller, Diego Perini
371
+
372
+ // http://dean.edwards.name/weblog/2005/10/add-event/
373
+
374
+ function dean_addEvent(element, type, handler) {
375
+ if (element.addEventListener) {
376
+ element.addEventListener(type, handler, false);
377
+ } else {
378
+ // assign each event handler a unique ID
379
+ if (!handler.$$guid) handler.$$guid = dean_addEvent.guid++;
380
+ // create a hash table of event types for the element
381
+ if (!element.events) element.events = {};
382
+ // create a hash table of event handlers for each element/event pair
383
+ var handlers = element.events[type];
384
+ if (!handlers) {
385
+ handlers = element.events[type] = {};
386
+ // store the existing event handler (if there is one)
387
+ if (element["on" + type]) {
388
+ handlers[0] = element["on" + type];
389
+ }
390
+ }
391
+ // store the event handler in the hash table
392
+ handlers[handler.$$guid] = handler;
393
+ // assign a global event handler to do all the work
394
+ element["on" + type] = handleEvent;
395
+ }
396
+ };
397
+ // a counter used to create unique IDs
398
+ dean_addEvent.guid = 1;
399
+
400
+ function removeEvent(element, type, handler) {
401
+ if (element.removeEventListener) {
402
+ element.removeEventListener(type, handler, false);
403
+ } else {
404
+ // delete the event handler from the hash table
405
+ if (element.events && element.events[type]) {
406
+ delete element.events[type][handler.$$guid];
407
+ }
408
+ }
409
+ };
410
+
411
+ function handleEvent(event) {
412
+ var returnValue = true;
413
+ // grab the event object (IE uses a global event object)
414
+ event = event || fixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event);
415
+ // get a reference to the hash table of event handlers
416
+ var handlers = this.events[event.type];
417
+ // execute each event handler
418
+ for (var i in handlers) {
419
+ this.$$handleEvent = handlers[i];
420
+ if (this.$$handleEvent(event) === false) {
421
+ returnValue = false;
422
+ }
423
+ }
424
+ return returnValue;
425
+ };
426
+
427
+ function fixEvent(event) {
428
+ // add W3C standard event methods
429
+ event.preventDefault = fixEvent.preventDefault;
430
+ event.stopPropagation = fixEvent.stopPropagation;
431
+ return event;
432
+ };
433
+ fixEvent.preventDefault = function() {
434
+ this.returnValue = false;
435
+ };
436
+ fixEvent.stopPropagation = function() {
437
+ this.cancelBubble = true;
438
+ }
439
+
440
+ // Dean's forEach: http://dean.edwards.name/base/forEach.js
441
+ /*
442
+ forEach, version 1.0
443
+ Copyright 2006, Dean Edwards
444
+ License: http://www.opensource.org/licenses/mit-license.php
445
+ */
446
+
447
+ // array-like enumeration
448
+ if (!Array.forEach) { // mozilla already supports this
449
+ Array.forEach = function(array, block, context) {
450
+ for (var i = 0; i < array.length; i++) {
451
+ block.call(context, array[i], i, array);
452
+ }
453
+ };
454
+ }
455
+
456
+ // generic enumeration
457
+ Function.prototype.forEach = function(object, block, context) {
458
+ for (var key in object) {
459
+ if (typeof this.prototype[key] == "undefined") {
460
+ block.call(context, object[key], key, object);
461
+ }
462
+ }
463
+ };
464
+
465
+ // character enumeration
466
+ String.forEach = function(string, block, context) {
467
+ Array.forEach(string.split(""), function(chr, index) {
468
+ block.call(context, chr, index, string);
469
+ });
470
+ };
471
+
472
+ // globally resolve forEach enumeration
473
+ var forEach = function(object, block, context) {
474
+ if (object) {
475
+ var resolve = Object; // default
476
+ if (object instanceof Function) {
477
+ // functions have a "length" property
478
+ resolve = Function;
479
+ } else if (object.forEach instanceof Function) {
480
+ // the object implements a custom forEach method so use that
481
+ object.forEach(block, context);
482
+ return;
483
+ } else if (typeof object == "string") {
484
+ // the object is a string
485
+ resolve = String;
486
+ } else if (typeof object.length == "number") {
487
+ // the object is array-like
488
+ resolve = Array;
489
+ }
490
+ resolve.forEach(object, block, context);
491
+ }
492
+ };
493
+
494
+ /*
495
+ Some stuff from A-List-Apart that goes well with sortable tables
496
+ */
497
+
498
+
499
+
500
+ // this function is need to work around
501
+ // a bug in IE related to element attributes
502
+ function hasClass(obj) {
503
+ var result = false;
504
+ if (obj.getAttributeNode("class") != null) {
505
+ result = obj.getAttributeNode("class").value;
506
+ }
507
+ return result;
508
+ }
509
+
510
+ function stripe(id) {
511
+
512
+ // the flag we'll use to keep track of
513
+ // whether the current row is odd or even
514
+ var even = false;
515
+
516
+ // if arguments are provided to specify the colours
517
+ // of the even & odd rows, then use the them;
518
+ // otherwise use the following defaults:
519
+ var evenColor = arguments[1] ? arguments[1] : "#fff";
520
+ var oddColor = arguments[2] ? arguments[2] : "#eee";
521
+
522
+ // obtain a reference to the desired table
523
+ // if no such table exists, abort
524
+ var table = document.getElementById(id);
525
+ if (! table) { return; }
526
+
527
+ // by definition, tables can have more than one tbody
528
+ // element, so we'll have to get the list of child
529
+ // &lt;tbody&gt;s
530
+ var tbodies = table.getElementsByTagName("tbody");
531
+
532
+ // and iterate through them...
533
+ for (var h = 0; h < tbodies.length; h++) {
534
+
535
+ // find all the &lt;tr&gt; elements...
536
+ var trs = tbodies[h].getElementsByTagName("tr");
537
+
538
+ // ... and iterate through them
539
+ for (var i = 0; i < trs.length; i++) {
540
+
541
+ // avoid rows that have a class attribute
542
+ // or backgroundColor style
543
+ if (!hasClass(trs[i]) && ! trs[i].style.backgroundColor) {
544
+
545
+ // get all the cells in this row...
546
+ var tds = trs[i].getElementsByTagName("td");
547
+
548
+ // and iterate through them...
549
+ for (var j = 0; j < tds.length; j++) {
550
+
551
+ var mytd = tds[j];
552
+
553
+ // avoid cells that have a class attribute
554
+ // or backgroundColor style
555
+ if (! hasClass(mytd) && ! mytd.style.backgroundColor) {
556
+
557
+ mytd.style.backgroundColor = even ? evenColor : oddColor;
558
+
559
+ }
560
+ }
561
+ }
562
+ // flip from odd to even, or vice-versa
563
+ even = ! even;
564
+ }
565
+ }
566
+ }
567
+
568
+