clockface 1.0.0.beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +210 -0
  4. data/Rakefile +22 -0
  5. data/app/assets/config/clockface_manifest.js +2 -0
  6. data/app/assets/images/clockface/clockface.svg +34 -0
  7. data/app/assets/javascripts/clockface/application.js +17 -0
  8. data/app/assets/javascripts/clockface/flash.js +7 -0
  9. data/app/assets/javascripts/clockface/sorttable.js +494 -0
  10. data/app/assets/stylesheets/clockface/application.scss +80 -0
  11. data/app/assets/stylesheets/clockface/application/_fonts.scss +2 -0
  12. data/app/assets/stylesheets/clockface/application/colors.scss +8 -0
  13. data/app/assets/stylesheets/clockface/application/flash.scss +6 -0
  14. data/app/assets/stylesheets/clockface/application/footer.scss +37 -0
  15. data/app/assets/stylesheets/clockface/application/nav.scss +51 -0
  16. data/app/assets/stylesheets/clockface/events/delete.scss +45 -0
  17. data/app/assets/stylesheets/clockface/events/event_form.scss +62 -0
  18. data/app/assets/stylesheets/clockface/events/index.scss +56 -0
  19. data/app/assets/stylesheets/clockface/tasks/delete.scss +29 -0
  20. data/app/assets/stylesheets/clockface/tasks/index.scss +47 -0
  21. data/app/assets/stylesheets/clockface/tasks/task_form.scss +20 -0
  22. data/app/controllers/clockface/application_controller.rb +20 -0
  23. data/app/controllers/clockface/events_controller.rb +151 -0
  24. data/app/controllers/clockface/root_controller.rb +7 -0
  25. data/app/controllers/clockface/tasks_controller.rb +137 -0
  26. data/app/events/clockface/application_job.rb +4 -0
  27. data/app/helpers/clockface/application_helper.rb +4 -0
  28. data/app/helpers/clockface/config_helper.rb +32 -0
  29. data/app/helpers/clockface/events_helper.rb +37 -0
  30. data/app/helpers/clockface/logging_helper.rb +12 -0
  31. data/app/mailers/clockface/application_mailer.rb +6 -0
  32. data/app/models/clockface/application_record.rb +7 -0
  33. data/app/models/clockface/event.rb +179 -0
  34. data/app/models/clockface/task.rb +12 -0
  35. data/app/presenters/clockface/events_presenter.rb +48 -0
  36. data/app/services/clockface/event_validation_interactor.rb +35 -0
  37. data/app/services/clockface/task_validation_interactor.rb +25 -0
  38. data/app/views/clockface/application/_flash.html.erb +25 -0
  39. data/app/views/clockface/application/_footer.html.erb +15 -0
  40. data/app/views/clockface/application/_nav.html.erb +19 -0
  41. data/app/views/clockface/events/_event_form.html.erb +130 -0
  42. data/app/views/clockface/events/delete.html.erb +124 -0
  43. data/app/views/clockface/events/edit.html.erb +14 -0
  44. data/app/views/clockface/events/index.html.erb +108 -0
  45. data/app/views/clockface/events/new.html.erb +14 -0
  46. data/app/views/clockface/tasks/_task_form.html.erb +57 -0
  47. data/app/views/clockface/tasks/delete.html.erb +83 -0
  48. data/app/views/clockface/tasks/edit.html.erb +14 -0
  49. data/app/views/clockface/tasks/index.html.erb +70 -0
  50. data/app/views/clockface/tasks/new.html.erb +14 -0
  51. data/app/views/layouts/clockface/application.html.erb +27 -0
  52. data/config/locales/en.yml +158 -0
  53. data/config/routes.rb +15 -0
  54. data/db/migrate/20170528230549_create_clockface_tasks.rb +10 -0
  55. data/db/migrate/20170528234810_create_clockface_events.rb +20 -0
  56. data/lib/clockface.rb +135 -0
  57. data/lib/clockface/engine.rb +79 -0
  58. data/lib/clockface/version.rb +3 -0
  59. data/lib/clockwork/database_events/synchronizer.rb +73 -0
  60. data/lib/tasks/clockface_tasks.rake +4 -0
  61. metadata +199 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bf5aca788ece95fd88581328494f6e939e5062c7
4
+ data.tar.gz: 3a3eb9ee32ac3836669f894d77b963186474ff8b
5
+ SHA512:
6
+ metadata.gz: 5e12ad767b216e35779e7dbcbb2ec7f07a0dbc22c4deb8383fcdd1a9d30da6164807e7353fa8fc607bd3ce7384d56e7aea7085b5f53d51eb4d2c173c75eb62f7
7
+ data.tar.gz: 462ad552339d748a366ad714e4394467a14ecb3b9d04421f0a643be2f6efa0eeca0c6f05b5b65da90c0716776d818fc1a6e72a2e467d1c2e5f0ed7aec0e53388
@@ -0,0 +1,20 @@
1
+ Copyright 2017 Abhishek Chandrasekhar
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.
@@ -0,0 +1,210 @@
1
+ # Clockface
2
+
3
+ [![Build Status](https://gitlab.com/abhchand/clockface/badges/master/build.svg)](https://gitlab.com/abhchand/clockface/pipelines)
4
+
5
+ A lightweight UI for the [Clockwork gem](https://github.com/Rykian/clockwork) to easily schedule and manage background jobs. It's built as a [Rails Engine](http://guides.rubyonrails.org/engines.html) to extend the functionality of your Rails Application
6
+
7
+ ![Clockface](https://gitlab.com/abhchand/clockface/raw/master/meta/screenshot.png)
8
+
9
+ Clockface serves as a **complete UI wrapper** on top of clockwork -
10
+
11
+ - It includes and configures clockwork directly so you only have to worry about configuring one thing - Clockface
12
+ - It doesn't add any new functionality on top of clockwork. It simply adds a UI for management and execution
13
+
14
+ ##### Multi Tenancy
15
+
16
+ Clockface also supports schema-based multi-tenancy!
17
+
18
+ See the [Multi Tenancy section](#multi_tenancy) below.
19
+
20
+ ### Have a Question?
21
+
22
+ Find us on StackOverflow! Just [ask a question](https://stackoverflow.com/questions/ask) and include the [`clockface`](https://stackoverflow.com/questions/tagged/clockface) tag.
23
+
24
+
25
+ ## Quickstart (In 3 Easy Steps)
26
+
27
+ #### A. Add Clockface
28
+
29
+ Add the Clockface gem. Remove the `clockwork` gem if you're already using it - Clockface takes care of including and invoking it.
30
+
31
+ ```ruby
32
+ gem "clockface"
33
+ ```
34
+
35
+ ```diff
36
+ - gem "clockwork"
37
+ ```
38
+
39
+ Clockface uses DB tables to store your scheduled events, so you'll need to create them in your application
40
+
41
+ ```ruby
42
+ rake clockface:install:migrations
43
+ rake db:migrate
44
+ ```
45
+
46
+ #### B. Configure Clockface
47
+
48
+ Mount the Clockface engine in your `routes.rb` files
49
+
50
+ ```ruby
51
+ mount Clockface::Engine => "/clockface"
52
+ ```
53
+
54
+ Create an initializer under `config/initializers/clockface.rb` and configure Clockface options. For more options - including multi tenancy configuration options - see [Configuration Options](#configuration_options) below
55
+
56
+ ```ruby
57
+ Clockface::Engine.configure do |app|
58
+ app.config.clockface.time_zone = "Pacific Time (US & Canada)"
59
+ end
60
+ ```
61
+
62
+ #### C. Define `clock.rb`
63
+
64
+ Create a `clock.rb` file in your application's root, or replace your existing `clock.rb` file if you're already using the `clockwork` gem
65
+
66
+ ```ruby
67
+ # /clock.rb
68
+ require_relative "./config/boot"
69
+ require_relative "./config/environment"
70
+
71
+ require "clockface"
72
+
73
+ Clockface.sync_database_events(every: 10.seconds) do |event|
74
+ # An Event is a scheduled instance of a particular Task.
75
+ #
76
+ # You will define new Tasks and Events in the UI
77
+ # The `Event` DB record will be yielded to your application here
78
+ #
79
+ # You're free to do anything you like with this yielded record. Specifically,
80
+ # the `command` field exists to store any relevant job execution information.
81
+ #
82
+ # For example: we might use the `command` field to store the class of the
83
+ # job we wish to schedule with Sidekiq.
84
+ #
85
+ # > command: "{\"class\":\"MyHardWorker\"}"
86
+
87
+ klass = JSON.parse(event.command)["class"]
88
+ klass.constantize.perform_async
89
+ end
90
+ ```
91
+
92
+ That's it! Clockface is now accessible in your application under the mounted route (e.g. `/clockface`)
93
+
94
+
95
+ ## <a name="configuration_options"></a>Configuration Options
96
+
97
+ ```ruby
98
+ # Specify a timezone for display purposes. Because humans don't work in UTC.
99
+ # default: `Rails.application.config.time_zone` (your application time zone)
100
+ app.config.clockface.time_zone = "Pacific Time (US & Canada)"
101
+
102
+ # Specify a logger for Clockface to use
103
+ # default: `Rails.logger` (your application's logger)
104
+ app.config.clockface.logger = [Rails.logger, Logger.new(Rails.root.join("log", "clockface.log"))]
105
+
106
+ #
107
+ # (Multi Tenant Options)
108
+ #
109
+
110
+ # You can use any gem library to manage your multi tenant schemas.
111
+ # The `apartment` gem is quite popular, so the examples below reference configuration using that gem
112
+
113
+ # Tell clockface what your tenant/schema names are
114
+ # default: []
115
+ app.config.clockface.tenant_list = %w[tenant1 tenant2]
116
+
117
+ # Tell Clockface how to get the current tenant/schema context
118
+ # A callable proc that returns the current schema context
119
+ # default: nil (must be specified by you)
120
+ app.config.clockface.current_tenant_proc = proc { Apartment::Tenant.current }
121
+
122
+ # Tell Clockface how to execute commands within the context of some tenant/schema
123
+ # A callable proc that takes arguments for tenant name, another proc to
124
+ # execute, and arguments for the proc to be executed
125
+ # default: nil (mst be specified by you)
126
+ app.config.clockface.execute_in_tenant_proc =
127
+ proc do |tenant_name, some_proc, proc_args|
128
+ Apartment::Tenant.switch(tenant_name) { some_proc.call(*proc_args) }
129
+ end
130
+ ```
131
+
132
+ ## Running Locally
133
+
134
+ Clone, build, install, and seed the local database with the inbuilt script.
135
+
136
+ ```
137
+ git clone https://gitlab.com/abhchand/clockface
138
+ cd clockface/
139
+ ```
140
+
141
+ Seed the database with the in-built script
142
+
143
+ ```
144
+ bin/setup
145
+ ```
146
+
147
+ Run the application with
148
+
149
+ ```
150
+ bundle exec rails server
151
+ ```
152
+
153
+ You can now visit the app at [http://localhost:3000/clockface](http://localhost:3000/clockface)
154
+
155
+ #### Running in Multi-Tenant mode
156
+
157
+ By default the app runs as a single tenant application. The app can also be run in multi tenant mode locally to test or develop any multi tenant features
158
+
159
+ Clone, build, install, and seed the local database with the inbuilt script.
160
+
161
+ ```
162
+ git clone https://gitlab.com/abhchand/clockface
163
+ cd clockface/
164
+ ```
165
+
166
+ Seed the database with the in-built script
167
+
168
+ ```
169
+ bin/setup-multi-tenant
170
+ ```
171
+
172
+ Run the application with
173
+
174
+ ```
175
+ bundle exec rails server -b lvh.me
176
+ ```
177
+
178
+
179
+ Note:
180
+ 1. By default the above process seeds two tenants - "earth" and "mars" - that run on different subdomains
181
+
182
+ 2. Since `localhost` does not support subdomains, we use `lvh.me` (a loopback domain) when running locally
183
+
184
+
185
+ You can now visit the "earth" tenant at [http://earth.lvh.me:3000/clockface](http://earth.lvh.me:3000/clockface)
186
+
187
+
188
+ #### Running Background Jobs
189
+
190
+
191
+ The above `rails server` commands only start the web server, which **does not start the job processing queue or run any scheduled events**.
192
+
193
+ To actually run any scheduled events you'll need to start the Sidekiq server (which the dummy app uses for job scheduling) and the Clock process.
194
+
195
+ The [foreman gem](https://github.com/ddollar/foreman) can be used to easily start all processes at once (as defined in the [Procfile](./spec/dummy/Procfile))
196
+
197
+ ```
198
+ bundle exec foreman start -f spec/dummy/Procfile
199
+ ```
200
+
201
+ ## Contributing
202
+
203
+ All are welcome to contribute.
204
+
205
+ If you're a newbie or consider yourself inexperienced, don't hesitate to contribute 🙂. That's how you learn!
206
+
207
+ > **NOTE**: This project only takes contributions on [Gitlab](https://gitlab.com/abhchand/clockface).
208
+
209
+ 1. Open a Gitlab issue for this project [here](https://gitlab.com/abhchand/clockface/issues/new). Please include a description fo the changes / fixes you'd like to make.
210
+ 2. If any project owner approves the idea, please open a new pull request agains the `master` branch.
@@ -0,0 +1,22 @@
1
+ begin
2
+ require "bundler/setup"
3
+ rescue LoadError
4
+ puts "You must `gem install bundler` and `bundle install` to run rake tasks"
5
+ end
6
+
7
+ require "rdoc/task"
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = "rdoc"
11
+ rdoc.title = "Clockface"
12
+ rdoc.options << "--line-numbers"
13
+ rdoc.rdoc_files.include("README.md")
14
+ rdoc.rdoc_files.include("lib/**/*.rb")
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("spec/dummy/Rakefile", __dir__)
18
+ load "rails/tasks/engine.rake"
19
+
20
+ load "rails/tasks/statistics.rake"
21
+
22
+ require "bundler/gem_tasks"
@@ -0,0 +1,2 @@
1
+ //= link_directory ../javascripts/clockface .js
2
+ //= link_directory ../stylesheets/clockface .css
@@ -0,0 +1,34 @@
1
+ <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2
+ <svg width="128px" height="128px" viewBox="0 0 128 128" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
3
+ <!-- Generator: Sketch 43.2 (39069) - http://www.bohemiancoding.com/sketch -->
4
+ <title>Group</title>
5
+ <desc>Created with Sketch.</desc>
6
+ <defs></defs>
7
+ <g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
8
+ <g id="Group" transform="translate(-8.000000, -8.000000)">
9
+ <circle id="face" fill="#BD0000" cx="72" cy="72" r="64"></circle>
10
+ <circle id="base" stroke="#FF1919" fill="#BDBDCF" cx="72" cy="72" r="56"></circle>
11
+ <g id="numbers3" transform="translate(72.000000, 72.000000) rotate(60.000000) translate(-72.000000, -72.000000) translate(20.000000, 20.000000)" fill="#417505">
12
+ <circle id="clock-12" cx="52" cy="2" r="2"></circle>
13
+ <circle id="clock-06" cx="52" cy="102" r="2"></circle>
14
+ <circle id="clock-03" cx="102" cy="52" r="2"></circle>
15
+ <circle id="clock-09" cx="2" cy="52" r="2"></circle>
16
+ </g>
17
+ <g id="numbers2" transform="translate(72.000000, 72.000000) rotate(30.000000) translate(-72.000000, -72.000000) translate(20.000000, 20.000000)" fill="#417505">
18
+ <circle id="clock-12" cx="52" cy="2" r="2"></circle>
19
+ <circle id="clock-06" cx="52" cy="102" r="2"></circle>
20
+ <circle id="clock-03" cx="102" cy="52" r="2"></circle>
21
+ <circle id="clock-09" cx="2" cy="52" r="2"></circle>
22
+ </g>
23
+ <g id="numbers1" transform="translate(20.000000, 20.000000)" fill="#417505">
24
+ <circle id="clock-12" cx="52" cy="2" r="2"></circle>
25
+ <circle id="clock-06" cx="52" cy="102" r="2"></circle>
26
+ <circle id="clock-03" cx="102" cy="52" r="2"></circle>
27
+ <circle id="clock-09" cx="2" cy="52" r="2"></circle>
28
+ </g>
29
+ <rect id="second" fill="#E0E0E0" transform="translate(88.043485, 56.243467) rotate(45.000000) translate(-88.043485, -56.243467) " x="87.5434853" y="33.743467" width="1" height="45"></rect>
30
+ <rect id="minute" fill="#000000" x="71.5" y="27" width="1" height="45"></rect>
31
+ <rect id="hour" fill="#000000" transform="translate(55.000000, 72.000000) rotate(270.000000) translate(-55.000000, -72.000000) " x="54.5" y="54.5" width="1" height="35"></rect>
32
+ </g>
33
+ </g>
34
+ </svg>
@@ -0,0 +1,17 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file. JavaScript code in this file should be added after the last require_* statement.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require ./flash
14
+ //= require ./sorttable
15
+ //= require_tree .
16
+
17
+ window.onload = flashClose;
@@ -0,0 +1,7 @@
1
+ function flashClose() {
2
+ document.querySelectorAll(".flash .close").forEach(function(e) {
3
+ e.addEventListener("click", function(){
4
+ this.parentElement.classList.remove("active");
5
+ })
6
+ })
7
+ }
@@ -0,0 +1,494 @@
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", sorttable.innerSortFunction = 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
+ if (!node) return "";
207
+
208
+ hasInputs = (typeof node.getElementsByTagName == 'function') &&
209
+ node.getElementsByTagName('input').length;
210
+
211
+ if (node.getAttribute("sorttable_customkey") != null) {
212
+ return node.getAttribute("sorttable_customkey");
213
+ }
214
+ else if (typeof node.textContent != 'undefined' && !hasInputs) {
215
+ return node.textContent.replace(/^\s+|\s+$/g, '');
216
+ }
217
+ else if (typeof node.innerText != 'undefined' && !hasInputs) {
218
+ return node.innerText.replace(/^\s+|\s+$/g, '');
219
+ }
220
+ else if (typeof node.text != 'undefined' && !hasInputs) {
221
+ return node.text.replace(/^\s+|\s+$/g, '');
222
+ }
223
+ else {
224
+ switch (node.nodeType) {
225
+ case 3:
226
+ if (node.nodeName.toLowerCase() == 'input') {
227
+ return node.value.replace(/^\s+|\s+$/g, '');
228
+ }
229
+ case 4:
230
+ return node.nodeValue.replace(/^\s+|\s+$/g, '');
231
+ break;
232
+ case 1:
233
+ case 11:
234
+ var innerText = '';
235
+ for (var i = 0; i < node.childNodes.length; i++) {
236
+ innerText += sorttable.getInnerText(node.childNodes[i]);
237
+ }
238
+ return innerText.replace(/^\s+|\s+$/g, '');
239
+ break;
240
+ default:
241
+ return '';
242
+ }
243
+ }
244
+ },
245
+
246
+ reverse: function(tbody) {
247
+ // reverse the rows in a tbody
248
+ newrows = [];
249
+ for (var i=0; i<tbody.rows.length; i++) {
250
+ newrows[newrows.length] = tbody.rows[i];
251
+ }
252
+ for (var i=newrows.length-1; i>=0; i--) {
253
+ tbody.appendChild(newrows[i]);
254
+ }
255
+ delete newrows;
256
+ },
257
+
258
+ /* sort functions
259
+ each sort function takes two parameters, a and b
260
+ you are comparing a[0] and b[0] */
261
+ sort_numeric: function(a,b) {
262
+ aa = parseFloat(a[0].replace(/[^0-9.-]/g,''));
263
+ if (isNaN(aa)) aa = 0;
264
+ bb = parseFloat(b[0].replace(/[^0-9.-]/g,''));
265
+ if (isNaN(bb)) bb = 0;
266
+ return aa-bb;
267
+ },
268
+ sort_alpha: function(a,b) {
269
+ if (a[0]==b[0]) return 0;
270
+ if (a[0]<b[0]) return -1;
271
+ return 1;
272
+ },
273
+ sort_ddmm: function(a,b) {
274
+ mtch = a[0].match(sorttable.DATE_RE);
275
+ y = mtch[3]; m = mtch[2]; d = mtch[1];
276
+ if (m.length == 1) m = '0'+m;
277
+ if (d.length == 1) d = '0'+d;
278
+ dt1 = y+m+d;
279
+ mtch = b[0].match(sorttable.DATE_RE);
280
+ y = mtch[3]; m = mtch[2]; d = mtch[1];
281
+ if (m.length == 1) m = '0'+m;
282
+ if (d.length == 1) d = '0'+d;
283
+ dt2 = y+m+d;
284
+ if (dt1==dt2) return 0;
285
+ if (dt1<dt2) return -1;
286
+ return 1;
287
+ },
288
+ sort_mmdd: function(a,b) {
289
+ mtch = a[0].match(sorttable.DATE_RE);
290
+ y = mtch[3]; d = mtch[2]; m = mtch[1];
291
+ if (m.length == 1) m = '0'+m;
292
+ if (d.length == 1) d = '0'+d;
293
+ dt1 = y+m+d;
294
+ mtch = b[0].match(sorttable.DATE_RE);
295
+ y = mtch[3]; d = mtch[2]; m = mtch[1];
296
+ if (m.length == 1) m = '0'+m;
297
+ if (d.length == 1) d = '0'+d;
298
+ dt2 = y+m+d;
299
+ if (dt1==dt2) return 0;
300
+ if (dt1<dt2) return -1;
301
+ return 1;
302
+ },
303
+
304
+ shaker_sort: function(list, comp_func) {
305
+ // A stable sort function to allow multi-level sorting of data
306
+ // see: http://en.wikipedia.org/wiki/Cocktail_sort
307
+ // thanks to Joseph Nahmias
308
+ var b = 0;
309
+ var t = list.length - 1;
310
+ var swap = true;
311
+
312
+ while(swap) {
313
+ swap = false;
314
+ for(var i = b; i < t; ++i) {
315
+ if ( comp_func(list[i], list[i+1]) > 0 ) {
316
+ var q = list[i]; list[i] = list[i+1]; list[i+1] = q;
317
+ swap = true;
318
+ }
319
+ } // for
320
+ t--;
321
+
322
+ if (!swap) break;
323
+
324
+ for(var i = t; i > b; --i) {
325
+ if ( comp_func(list[i], list[i-1]) < 0 ) {
326
+ var q = list[i]; list[i] = list[i-1]; list[i-1] = q;
327
+ swap = true;
328
+ }
329
+ } // for
330
+ b++;
331
+
332
+ } // while(swap)
333
+ }
334
+ }
335
+
336
+ /* ******************************************************************
337
+ Supporting functions: bundled here to avoid depending on a library
338
+ ****************************************************************** */
339
+
340
+ // Dean Edwards/Matthias Miller/John Resig
341
+
342
+ /* for Mozilla/Opera9 */
343
+ if (document.addEventListener) {
344
+ document.addEventListener("DOMContentLoaded", sorttable.init, false);
345
+ }
346
+
347
+ /* for Internet Explorer */
348
+ /*@cc_on @*/
349
+ /*@if (@_win32)
350
+ document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
351
+ var script = document.getElementById("__ie_onload");
352
+ script.onreadystatechange = function() {
353
+ if (this.readyState == "complete") {
354
+ sorttable.init(); // call the onload handler
355
+ }
356
+ };
357
+ /*@end @*/
358
+
359
+ /* for Safari */
360
+ if (/WebKit/i.test(navigator.userAgent)) { // sniff
361
+ var _timer = setInterval(function() {
362
+ if (/loaded|complete/.test(document.readyState)) {
363
+ sorttable.init(); // call the onload handler
364
+ }
365
+ }, 10);
366
+ }
367
+
368
+ /* for other browsers */
369
+ window.onload = sorttable.init;
370
+
371
+ // written by Dean Edwards, 2005
372
+ // with input from Tino Zijdel, Matthias Miller, Diego Perini
373
+
374
+ // http://dean.edwards.name/weblog/2005/10/add-event/
375
+
376
+ function dean_addEvent(element, type, handler) {
377
+ if (element.addEventListener) {
378
+ element.addEventListener(type, handler, false);
379
+ } else {
380
+ // assign each event handler a unique ID
381
+ if (!handler.$$guid) handler.$$guid = dean_addEvent.guid++;
382
+ // create a hash table of event types for the element
383
+ if (!element.events) element.events = {};
384
+ // create a hash table of event handlers for each element/event pair
385
+ var handlers = element.events[type];
386
+ if (!handlers) {
387
+ handlers = element.events[type] = {};
388
+ // store the existing event handler (if there is one)
389
+ if (element["on" + type]) {
390
+ handlers[0] = element["on" + type];
391
+ }
392
+ }
393
+ // store the event handler in the hash table
394
+ handlers[handler.$$guid] = handler;
395
+ // assign a global event handler to do all the work
396
+ element["on" + type] = handleEvent;
397
+ }
398
+ };
399
+ // a counter used to create unique IDs
400
+ dean_addEvent.guid = 1;
401
+
402
+ function removeEvent(element, type, handler) {
403
+ if (element.removeEventListener) {
404
+ element.removeEventListener(type, handler, false);
405
+ } else {
406
+ // delete the event handler from the hash table
407
+ if (element.events && element.events[type]) {
408
+ delete element.events[type][handler.$$guid];
409
+ }
410
+ }
411
+ };
412
+
413
+ function handleEvent(event) {
414
+ var returnValue = true;
415
+ // grab the event object (IE uses a global event object)
416
+ event = event || fixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event);
417
+ // get a reference to the hash table of event handlers
418
+ var handlers = this.events[event.type];
419
+ // execute each event handler
420
+ for (var i in handlers) {
421
+ this.$$handleEvent = handlers[i];
422
+ if (this.$$handleEvent(event) === false) {
423
+ returnValue = false;
424
+ }
425
+ }
426
+ return returnValue;
427
+ };
428
+
429
+ function fixEvent(event) {
430
+ // add W3C standard event methods
431
+ event.preventDefault = fixEvent.preventDefault;
432
+ event.stopPropagation = fixEvent.stopPropagation;
433
+ return event;
434
+ };
435
+ fixEvent.preventDefault = function() {
436
+ this.returnValue = false;
437
+ };
438
+ fixEvent.stopPropagation = function() {
439
+ this.cancelBubble = true;
440
+ }
441
+
442
+ // Dean's forEach: http://dean.edwards.name/base/forEach.js
443
+ /*
444
+ forEach, version 1.0
445
+ Copyright 2006, Dean Edwards
446
+ License: http://www.opensource.org/licenses/mit-license.php
447
+ */
448
+
449
+ // array-like enumeration
450
+ if (!Array.forEach) { // mozilla already supports this
451
+ Array.forEach = function(array, block, context) {
452
+ for (var i = 0; i < array.length; i++) {
453
+ block.call(context, array[i], i, array);
454
+ }
455
+ };
456
+ }
457
+
458
+ // generic enumeration
459
+ Function.prototype.forEach = function(object, block, context) {
460
+ for (var key in object) {
461
+ if (typeof this.prototype[key] == "undefined") {
462
+ block.call(context, object[key], key, object);
463
+ }
464
+ }
465
+ };
466
+
467
+ // character enumeration
468
+ String.forEach = function(string, block, context) {
469
+ Array.forEach(string.split(""), function(chr, index) {
470
+ block.call(context, chr, index, string);
471
+ });
472
+ };
473
+
474
+ // globally resolve forEach enumeration
475
+ var forEach = function(object, block, context) {
476
+ if (object) {
477
+ var resolve = Object; // default
478
+ if (object instanceof Function) {
479
+ // functions have a "length" property
480
+ resolve = Function;
481
+ } else if (object.forEach instanceof Function) {
482
+ // the object implements a custom forEach method so use that
483
+ object.forEach(block, context);
484
+ return;
485
+ } else if (typeof object == "string") {
486
+ // the object is a string
487
+ resolve = String;
488
+ } else if (typeof object.length == "number") {
489
+ // the object is array-like
490
+ resolve = Array;
491
+ }
492
+ resolve.forEach(object, block, context);
493
+ }
494
+ };