ajax-scaffold-generator 2.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
- <fieldset>
2
- <div class="row">
3
- <%= template_for_inclusion %>
4
- </div>
1
+ <fieldset>
2
+ <div class="row">
3
+ <%= template_for_inclusion %>
4
+ </div>
5
5
  </fieldset>
@@ -1,88 +1,88 @@
1
- require File.dirname(__FILE__) + '<%= "/.." * controller_class_nesting_depth %>/../test_helper'
2
- require '<%= controller_file_path %>_controller'
3
-
4
- # Re-raise errors caught by the controller.
5
- class <%= controller_class_name %>Controller; def rescue_action(e) raise e end; end
6
-
7
- class <%= controller_class_name %>ControllerTest < Test::Unit::TestCase
8
- fixtures :<%= table_name %>
9
-
10
- def setup
11
- @controller = <%= controller_class_name %>Controller.new
12
- @request = ActionController::TestRequest.new
13
- @response = ActionController::TestResponse.new
14
- end
15
-
16
- <% for action in unscaffolded_actions -%>
17
- def test_<%= action %>
18
- get :<%= action %>
19
- assert_response :success
20
- assert_template '<%= action %>'
21
- end
22
-
23
- <% end -%>
24
- <% unless suffix -%>
25
- def test_index
26
- get :index
27
- assert_response :success
28
- assert_template 'list'
29
- end
30
-
31
- <% end -%>
32
- def test_list<%= suffix %>
33
- get :list<%= suffix %>
34
-
35
- assert_response :success
36
- assert_template 'list<%= suffix %>'
37
-
38
- assert_not_nil assigns(:<%= plural_name %>)
39
- end
40
-
41
- def test_new<%= suffix %>
42
- get :new<%= suffix %>
43
-
44
- assert_response :success
45
- assert_template 'new<%= suffix %>'
46
-
47
- assert_not_nil assigns(:<%= singular_name %>)
48
- end
49
-
50
- def test_create
51
- num_<%= plural_name %> = <%= model_name %>.count
52
-
53
- post :create<%= suffix %>, :<%= singular_name %> => {}
54
-
55
- assert_response :redirect
56
- assert_redirected_to :action => 'list<%= suffix %>'
57
-
58
- assert_equal num_<%= plural_name %> + 1, <%= model_name %>.count
59
- end
60
-
61
- def test_edit<%= suffix %>
62
- get :edit<%= suffix %>, :id => 1
63
-
64
- assert_response :success
65
- assert_template 'edit<%= suffix %>'
66
-
67
- assert_not_nil assigns(:<%= singular_name %>)
68
- assert assigns(:<%= singular_name %>).valid?
69
- end
70
-
71
- def test_update<%= suffix %>
72
- post :update<%= suffix %>, :id => 1
73
- assert_response :redirect
74
- assert_redirected_to :action => 'show<%= suffix %>', :id => 1
75
- end
76
-
77
- def test_destroy<%= suffix %>
78
- assert_not_nil <%= model_name %>.find(1)
79
-
80
- post :destroy, :id => 1
81
- assert_response :redirect
82
- assert_redirected_to :action => 'list<%= suffix %>'
83
-
84
- assert_raise(ActiveRecord::RecordNotFound) {
85
- <%= model_name %>.find(1)
86
- }
87
- end
88
- end
1
+ require File.dirname(__FILE__) + '<%= "/.." * controller_class_nesting_depth %>/../test_helper'
2
+ require '<%= controller_file_path %>_controller'
3
+
4
+ # Re-raise errors caught by the controller.
5
+ class <%= controller_class_name %>Controller; def rescue_action(e) raise e end; end
6
+
7
+ class <%= controller_class_name %>ControllerTest < Test::Unit::TestCase
8
+ fixtures :<%= table_name %>
9
+
10
+ def setup
11
+ @controller = <%= controller_class_name %>Controller.new
12
+ @request = ActionController::TestRequest.new
13
+ @response = ActionController::TestResponse.new
14
+ end
15
+
16
+ <% for action in unscaffolded_actions -%>
17
+ def test_<%= action %>
18
+ get :<%= action %>
19
+ assert_response :success
20
+ assert_template '<%= action %>'
21
+ end
22
+
23
+ <% end -%>
24
+ <% unless suffix -%>
25
+ def test_index
26
+ get :index
27
+ assert_response :success
28
+ assert_template 'list'
29
+ end
30
+
31
+ <% end -%>
32
+ def test_list<%= suffix %>
33
+ get :list<%= suffix %>
34
+
35
+ assert_response :success
36
+ assert_template 'list<%= suffix %>'
37
+
38
+ assert_not_nil assigns(:<%= plural_name %>)
39
+ end
40
+
41
+ def test_new<%= suffix %>
42
+ get :new<%= suffix %>
43
+
44
+ assert_response :success
45
+ assert_template 'new<%= suffix %>'
46
+
47
+ assert_not_nil assigns(:<%= singular_name %>)
48
+ end
49
+
50
+ def test_create
51
+ num_<%= plural_name %> = <%= model_name %>.count
52
+
53
+ post :create<%= suffix %>, :<%= singular_name %> => {}
54
+
55
+ assert_response :redirect
56
+ assert_redirected_to :action => 'list<%= suffix %>'
57
+
58
+ assert_equal num_<%= plural_name %> + 1, <%= model_name %>.count
59
+ end
60
+
61
+ def test_edit<%= suffix %>
62
+ get :edit<%= suffix %>, :id => 1
63
+
64
+ assert_response :success
65
+ assert_template 'edit<%= suffix %>'
66
+
67
+ assert_not_nil assigns(:<%= singular_name %>)
68
+ assert assigns(:<%= singular_name %>).valid?
69
+ end
70
+
71
+ def test_update<%= suffix %>
72
+ post :update<%= suffix %>, :id => 1
73
+ assert_response :redirect
74
+ assert_redirected_to :action => 'show<%= suffix %>', :id => 1
75
+ end
76
+
77
+ def test_destroy<%= suffix %>
78
+ assert_not_nil <%= model_name %>.find(1)
79
+
80
+ post :destroy, :id => 1
81
+ assert_response :redirect
82
+ assert_redirected_to :action => 'list<%= suffix %>'
83
+
84
+ assert_raise(ActiveRecord::RecordNotFound) {
85
+ <%= model_name %>.find(1)
86
+ }
87
+ end
88
+ end
@@ -1,12 +1,12 @@
1
- module <%= controller_class_name %>Helper
2
-
3
- def num_columns
4
- <%= model_name %>.content_columns.length + 1
5
- end
6
-
7
- # This can be moved into application_helper.rb
8
- def loading_indicator_tag(scope,id)
9
- "<img src=\"/images/indicator.gif\" style=\"display: none;\" id=\"#{scope}-#{id}-loading-indicator\" alt=\"loading indicator\" class=\"loading-indicator\" />"
10
- end
11
-
12
- end
1
+ module <%= controller_class_name %>Helper
2
+
3
+ def num_columns
4
+ <%= model_name %>.content_columns.length + 1
5
+ end
6
+
7
+ # This can be moved into application_helper.rb
8
+ def loading_indicator_tag(scope,id)
9
+ "<img src=\"/images/indicator.gif\" style=\"display: none;\" id=\"#{scope}-#{id}-loading-indicator\" alt=\"loading indicator\" class=\"loading-indicator\" />"
10
+ end
11
+
12
+ end
@@ -1,15 +1,15 @@
1
- <!DOCTYPE html
2
- PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
3
- "DTD/xhtml1-strict.dtd">
4
- <html xmlns="http://www.w3.org/1999/xhtml" lang="en">
5
- <head>
6
- <title><%= Inflector.titleize(plural_name) %></title>
7
- <%%= stylesheet_link_tag 'ajax_scaffold', :media => 'all' %>
8
- <%%= javascript_include_tag 'prototype', 'effects', 'rico_corner', 'ajax_scaffold' %>
9
- </head>
10
- <body>
11
-
12
- <%%= @content_for_layout %>
13
-
14
- </body>
15
- </html>
1
+ <!DOCTYPE html
2
+ PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
3
+ "DTD/xhtml1-strict.dtd">
4
+ <html xmlns="http://www.w3.org/1999/xhtml" lang="en">
5
+ <head>
6
+ <title><%= Inflector.titleize(plural_name) %></title>
7
+ <%%= stylesheet_link_tag 'ajax_scaffold', :media => 'all' %>
8
+ <%%= javascript_include_tag 'prototype', 'effects', 'rico_corner', 'ajax_scaffold' %>
9
+ </head>
10
+ <body>
11
+
12
+ <%%= @content_for_layout %>
13
+
14
+ </body>
15
+ </html>
@@ -1,25 +1,25 @@
1
- <tr id="view-<%= singular_name %>-<%%= <%= singular_name %>.id %>">
2
- <%% for column in <%= model_name %>.content_columns %>
3
- <td><%%=h <%= singular_name %>.send(column.name) %>&nbsp;</td>
4
- <%% end %>
5
- <td class="actions">
6
- <div>
7
- <%%= loading_indicator_tag '<%= singular_name %>', "edit-#{<%= singular_name %>.id}" %>
8
- <%%= link_to_remote "Edit",
9
- :url => { :controller => '<%= controller_name %>', :action => 'edit', :id => <%= singular_name %> },
10
- :loading => "AjaxScaffold.editOnLoading(request,'<%= singular_name %>', #{<%= singular_name %>.id});",
11
- :success => "AjaxScaffold.editOnSuccess(request,'<%= singular_name %>', #{<%= singular_name %>.id});",
12
- :failure => "AjaxScaffold.editOnFailure(request,'<%= singular_name %>', #{<%= singular_name %>.id});",
13
- :complete => "AjaxScaffold.editOnComplete(request,'<%= singular_name %>', #{<%= singular_name %>.id});",
14
- :post => true; %>
15
- <%%= link_to_remote "Delete",
16
- :url => { :controller => '<%= controller_name %>',:action => 'destroy', :id => <%= singular_name %> },
17
- :confirm => 'Are you sure?',
18
- :loading => "AjaxScaffold.deleteOnLoading('<%= singular_name %>', #{<%= singular_name %>.id});",
19
- :success => "AjaxScaffold.deleteOnSuccess('<%= singular_name %>', #{<%= singular_name %>.id});",
20
- :failure => "AjaxScaffold.deleteOnFailure('<%= singular_name %>', #{<%= singular_name %>.id});",
21
- :complete => "AjaxScaffold.deleteOnComplete('<%= singular_name %>', #{<%= singular_name %>.id});",
22
- :post => true; %>
23
- </div>
24
- </td>
1
+ <tr id="view-<%= singular_name %>-<%%= <%= singular_name %>.id %>" <%%= "style=\"display: none;\"" if hidden %>>
2
+ <%% for column in <%= model_name %>.content_columns %>
3
+ <td><%%=h <%= singular_name %>.send(column.name) %>&nbsp;</td>
4
+ <%% end %>
5
+ <td class="actions">
6
+ <div>
7
+ <%%= loading_indicator_tag '<%= singular_name %>', "edit-#{<%= singular_name %>.id}" %>
8
+ <%%= link_to_remote "Edit",
9
+ :url => { :controller => '<%= controller_name %>', :action => 'edit', :id => <%= singular_name %> },
10
+ :loading => "AjaxScaffold.editOnLoading(request,'<%= singular_name %>', #{<%= singular_name %>.id});",
11
+ :success => "AjaxScaffold.editOnSuccess(request,'<%= singular_name %>', #{<%= singular_name %>.id});",
12
+ :failure => "AjaxScaffold.editOnFailure(request,'<%= singular_name %>', #{<%= singular_name %>.id});",
13
+ :complete => "AjaxScaffold.editOnComplete(request,'<%= singular_name %>', #{<%= singular_name %>.id});",
14
+ :post => true; %>
15
+ <%%= link_to_remote "Delete",
16
+ :url => { :controller => '<%= controller_name %>',:action => 'destroy', :id => <%= singular_name %> },
17
+ :confirm => 'Are you sure?',
18
+ :loading => "AjaxScaffold.deleteOnLoading('<%= singular_name %>', #{<%= singular_name %>.id});",
19
+ :success => "AjaxScaffold.deleteOnSuccess('<%= singular_name %>', #{<%= singular_name %>.id});",
20
+ :failure => "AjaxScaffold.deleteOnFailure('<%= singular_name %>', #{<%= singular_name %>.id});",
21
+ :complete => "AjaxScaffold.deleteOnComplete('<%= singular_name %>', #{<%= singular_name %>.id});",
22
+ :post => true; %>
23
+ </div>
24
+ </td>
25
25
  </tr>
@@ -1,327 +1,425 @@
1
-
2
- var Abstract = new Object();
3
- Abstract.Table = function() {};
4
- Abstract.Table.prototype = {
5
- tagTest: function(element, tagName) {
6
- return $(element).tagName.toLowerCase() == tagName.toLowerCase();
7
- }
8
- };
9
-
10
- Abstract.TableEffect = function() {};
11
- Abstract.TableEffect.prototype = Object.extend(new Abstract.Table(), {
12
- callEffect: function(target, command) {
13
- var target = $(target);
14
- if (this.tagTest(target,'tr')) {
15
- var length = target.cells.length;
16
- for (var i = 0; i < length; i++) {
17
- eval("new " + command + "(target.cells[i]);");
18
- }
19
- } else {
20
- eval("new " + command + "(targetElement);");
21
- }
22
- }
23
- });
24
-
25
- Abstract.TableRow = function() {};
26
- Abstract.TableRow.prototype = Object.extend(new Abstract.Table(), {
27
- initialize: function(target, source) {
28
- try {
29
- var target = $(target);
30
- var source = $(source);
31
- if (this.tagTest(target,'tr') && this.tagTest(source,'tr')) {
32
- var newRow = this.findParentTable(target).insertRow(this.getNewRowIndex(target));
33
- newRow.parentNode.replaceChild(source, newRow);
34
- }
35
- } catch (e) {
36
- alert(e);
37
- }
38
- },
39
- findParentTable: function(element) {
40
- var element = $(element);
41
- var maxSearchDepth = 3;
42
- var currentSearchDepth = 1;
43
- var current = element;
44
- while (currentSearchDepth <= maxSearchDepth) {
45
- current = current.parentNode;
46
- if (this.tagTest(current, 'table')) {
47
- return current;
48
- }
49
- currentSearchDepth++;
50
- }
51
- }
52
- });
53
-
54
- var TableRow = new Object();
55
-
56
- TableRow.MoveBefore = Class.create();
57
- TableRow.MoveBefore.prototype = Object.extend(new Abstract.TableRow(), {
58
- getNewRowIndex: function(target) {
59
- return target.rowIndex;
60
- }
61
- });
62
-
63
- TableRow.MoveAfter = Class.create();
64
- TableRow.MoveAfter.prototype = Object.extend(new Abstract.TableRow(), {
65
- getNewRowIndex: function(target) {
66
- return target.rowIndex+1;
67
- }
68
- });
69
-
70
- //Since scriptaculous doesn't support tables and causes and error applying effects to rows in IE 6.0 and Safari we need this wrapper
71
- var TableEffect = new Object();
72
-
73
- TableEffect.Fade = Class.create();
74
- TableEffect.Fade.prototype = Object.extend(new Abstract.TableEffect(), {
75
- initialize: function(target) {
76
- this.callEffect(target, 'Effect.Fade');
77
- }
78
- });
79
-
80
- TableEffect.SlideDown = Class.create();
81
- TableEffect.SlideDown.prototype = Object.extend(new Abstract.TableEffect(), {
82
- initialize: function(target) {
83
- this.callEffect(target, 'Effect.SlideDown');
84
- }
85
- });
86
-
87
- TableEffect.Highlight = Class.create();
88
- TableEffect.Highlight.prototype = Object.extend(new Abstract.TableEffect(), {
89
- initialize: function(target) {
90
- this.callEffect(target, 'Effect.Highlight');
91
- }
92
- });
93
-
94
- var AjaxScaffold = new Object();
95
-
96
- Object.extend(AjaxScaffold, {
97
-
98
- newOnLoading: function(request, type) {
99
- Element.show(this.getNewIndicator(type));
100
- },
101
- newOnFailure: function(request, type) {
102
- this.showError(type, request.responseText);
103
- Element.hide(this.getNewIndicator(type));
104
- },
105
- newOnSuccess: function(request, type) {
106
- var createForm = request.responseText;
107
- new Insertion.Top(this.getTableBodyElement(type), createForm);
108
-
109
- Element.hide(this.getEmptyMessageElement(type));
110
- Element.hide(this.getNewIndicator(type));
111
-
112
- var createElement = this.getCreateElement(type, this.getId(request,type));
113
- Element.show(createElement);
114
- new TableEffect.Highlight(createElement);
115
- Form.focusFirstElement(createElement.getElementsByTagName('form')[0]);
116
- },
117
- newOnComplete: function(request, type) {
118
-
119
- },
120
- getNewIndicator: function(type) {
121
- return $(type + "-new-loading-indicator");
122
- },
123
-
124
-
125
-
126
-
127
- createOnLoading: function(request,type,id) {
128
- Element.show(this.getIndicator('create',type,id));
129
- },
130
- createOnFailure: function(request,type,id) {
131
- var errorElement = this.getFormErrorElement('create',type,id)
132
- var errorMessages = request.responseText;
133
- errorElement.innerHTML = errorMessages;
134
- Element.show(errorElement);
135
- Element.hide(this.getIndicator('create',type,id));
136
- },
137
- createOnSuccess: function(request,type,id) {
138
- var view = request.responseText;
139
- new Insertion.Top(this.getTableBodyElement(type), view);
140
- var createElement = this.getCreateElement(type,id);
141
-
142
- Element.remove(createElement);
143
-
144
-
145
- var viewElement = this.getViewElement(type, this.getId(request,type));
146
- new TableEffect.Highlight(viewElement);
147
-
148
- this.stripeTable(type);
149
- },
150
- createOnCancel: function(type,id) {
151
- var createElement = this.getCreateElement(type,id);
152
- Element.remove(createElement);
153
- if (this.countRows(type) == 0) {
154
- Element.show(this.getEmptyMessageElement(type));
155
- }
156
- },
157
-
158
-
159
- deleteOnLoading: function(type,id) {
160
-
161
- },
162
- deleteOnFailure: function(type,id) {
163
- this.showError(type, request.responseText);
164
- },
165
- deleteOnComplete: function(type,id) {
166
- var viewElement = this.getViewElement(type,id);
167
-
168
- new TableEffect.Fade(viewElement);
169
- //We cannot set a timeout to remove this element from the DOM b/c once fade is complete
170
- // get by ID no longer works on this element. So we'll set a class so that it isn't picked up in a re striping
171
- Element.addClassName(viewElement, 'deleted');
172
-
173
- if (this.countRows(type) == 0) {
174
- Element.show(this.getEmptyMessageElement(type));
175
- } else {
176
- this.stripeTable(type);
177
- }
178
- },
179
-
180
-
181
- editOnLoading: function(request,type,id) {
182
- Element.show(this.getIndicator('edit',type,id));
183
- },
184
- editOnFailure: function(request,type,id) {
185
- this.showError(type, request.responseText);
186
- Element.hide(this.getIndicator('edit',type,id));
187
- },
188
- editOnSuccess: function(request,type,id) {
189
- var viewElement = this.getViewElement(type,id);
190
-
191
- //Ajax.Update with Insertion.top does not work when being used as a component with other scaffolds on the screen
192
- // the only safe way it seems it to always insert new elements at the bottom and then move them into the appropriate location
193
- var editForm = request.responseText;
194
- new Insertion.Bottom(this.getTableBodyElement(type), editForm);
195
- var editElement = this.getEditElement(type,id);
196
- new TableRow.MoveAfter(viewElement, editElement);
197
-
198
- var formElement = this.getFormElement(type,id);
199
-
200
- Element.hide(viewElement);
201
- Element.hide(this.getIndicator('edit',type,id));
202
- Element.show(editElement);
203
- new Effect.Highlight(editElement);
204
- Form.focusFirstElement(formElement);
205
- },
206
-
207
-
208
- updateOnLoading: function(request,type,id) {
209
- Element.show(this.getIndicator('update',type,id));
210
- },
211
- updateOnFailure: function(request,type,id) {
212
- var errorElement = this.getFormErrorElement('update',type,id)
213
- var errorMessages = request.responseText;
214
- errorElement.innerHTML = errorMessages;
215
- Element.show(errorElement);
216
- Element.hide(this.getIndicator('update',type,id));
217
- },
218
- updateOnSuccess: function(request,type,id) {
219
- var editElement = this.getEditElement(type,id);
220
- var formElement = this.getFormElement(type,id);
221
- var viewElement = this.getViewElement(type,id);
222
-
223
- Element.remove(viewElement);
224
-
225
- var view = request.responseText;
226
- new Insertion.Bottom(this.getTableBodyElement(type), view);
227
- var viewElement = this.getViewElement(type,id);
228
- new TableRow.MoveBefore(editElement, viewElement);
229
-
230
- Element.remove(editElement);
231
- this.stripeTable(type);
232
- new TableEffect.Highlight(viewElement);
233
- },
234
- updateOnCancel: function(type,id) {
235
- var viewElement = this.getViewElement(type,id);
236
- var editElement = this.getEditElement(type,id);
237
-
238
- Element.show(viewElement);
239
- Element.remove(editElement);
240
- },
241
-
242
-
243
- showError: function(type,html) {
244
- var errorElement = this.getErrorMessageElement(type);
245
- var errorMessageElement = errorElement.getElementsByTagName("p")[0];
246
- errorMessageElement.innerHTML = html;
247
- Element.show(errorElement);
248
- new TableEffect.Highlight(errorElement);
249
- },
250
- hideError: function(type) {
251
- var errorElement = this.getErrorMessageElement(type);
252
- Element.hide(errorElement);
253
- },
254
-
255
-
256
-
257
-
258
- getIndicator: function(scope, type,id) {
259
- return $(type + '-' + scope + '-' + id + "-loading-indicator");
260
- },
261
- getFormErrorElement: function(scope,type,id) {
262
- return $(scope + '-' + type + '-' + id + '-errors')
263
- },
264
- getId: function(request, type) {
265
- return request.getResponseHeader(type + '-id');
266
- },
267
- getCreateElement: function(type,id) {
268
- return $('create-' + type + '-' + id);
269
- },
270
- getViewElement: function(type,id) {
271
- return $('view-' + type + '-' + id);
272
- },
273
- getEditElement: function(type,id) {
274
- return $('edit-' + type + '-' + id);
275
- },
276
- getFormElement: function(type,id) {
277
- return $(type + '-' + id + '-form');
278
- },
279
- getEmptyMessageElement: function(type) {
280
- return $(type + '-empty-message');
281
- },
282
- getErrorMessageElement: function(type) {
283
- return $(type + '-error-message');
284
- },
285
- getTableBodyElement: function(type) {
286
- return $(type + '-list-body');
287
- },
288
- stripeTable: function(type) {
289
- var even = false;
290
- var tableBody = this.getTableBodyElement(type);
291
- var tableRows = tableBody.getElementsByTagName("tr");
292
- var length = tableRows.length;
293
-
294
- for (var i = 0; i < length; i++) {
295
- var tableRow = tableRows[i];
296
- //Make sure to skip rows that are create or edit rows or messages
297
- if (!Element.hasClassName(tableRow, "edit")
298
- && !Element.hasClassName(tableRow, "create")
299
- && !Element.hasClassName(tableRow, "deleted")
300
- && !Element.hasClassName(tableRow, "message")) {
301
- if (even) {
302
- Element.addClassName(tableRow, "even");
303
- } else {
304
- Element.removeClassName(tableRow, "even");
305
- }
306
- even = !even;
307
- }
308
- }
309
- },
310
- countRows: function(type) {
311
- var tableBody = this.getTableBodyElement(type);
312
- var tableRows = tableBody.getElementsByTagName("tr");
313
- var length = tableRows.length;
314
-
315
- var validRows = 0;
316
-
317
- for (var i = 0; i < length; i++) {
318
- var tableRow = tableRows[i];
319
- //Make sure to skip rows that are deleted or message
320
- if (!Element.hasClassName(tableRow, "deleted")
321
- && !Element.hasClassName(tableRow, "message")) {
322
- validRows++;
323
- }
324
- }
325
- return validRows;
326
- }
327
- });
1
+ // The following is a cross browser way to move around <tr> elements in a <table> or <tbody>
2
+
3
+ var Abstract = new Object();
4
+ Abstract.Table = function() {};
5
+ Abstract.Table.prototype = {
6
+ tagTest: function(element, tagName) {
7
+ return $(element).tagName.toLowerCase() == tagName.toLowerCase();
8
+ }
9
+ };
10
+
11
+ Abstract.TableRow = function() {};
12
+ Abstract.TableRow.prototype = Object.extend(new Abstract.Table(), {
13
+ initialize: function(targetTableRow, sourceTableRow) {
14
+ try {
15
+ var sourceTableRow = $(sourceTableRow);
16
+ var targetTableRow = $(targetTableRow);
17
+
18
+ if (targetTableRow == null || !this.tagTest(targetTableRow,'tr')
19
+ || sourceTableRow == null || !this.tagTest(sourceTableRow,'tr')) {
20
+ throw("TableRow: both parameters must be a <tr> tag.");
21
+ }
22
+
23
+ var tableOrTbody = this.findParentTableOrTbody(targetTableRow);
24
+
25
+ var newRow = tableOrTbody.insertRow(this.getNewRowIndex(targetTableRow) - this.getRowOffset(tableOrTbody));
26
+ newRow.parentNode.replaceChild(sourceTableRow, newRow);
27
+
28
+ } catch (e) {
29
+ alert(e);
30
+ }
31
+ },
32
+ getRowOffset: function(tableOrTbody) {
33
+ //If we are inserting into a tablebody we would need figure out the rowIndex of the first
34
+ // row in that tbody and subtract that offset from the new row index
35
+ var rowOffset = 0;
36
+ if (this.tagTest(tableOrTbody,'tbody')) {
37
+ rowOffset = tableOrTbody.rows[0].rowIndex;
38
+ }
39
+ return rowOffset;
40
+ },
41
+ findParentTableOrTbody: function(element) {
42
+ var element = $(element);
43
+ // Completely arbitrary value
44
+ var maxSearchDepth = 3;
45
+ var currentSearchDepth = 1;
46
+ var current = element;
47
+ while (currentSearchDepth <= maxSearchDepth) {
48
+ current = current.parentNode;
49
+ if (this.tagTest(current, 'tbody') || this.tagTest(current, 'table')) {
50
+ return current;
51
+ }
52
+ currentSearchDepth++;
53
+ }
54
+ }
55
+ });
56
+
57
+ var TableRow = new Object();
58
+
59
+ TableRow.MoveBefore = Class.create();
60
+ TableRow.MoveBefore.prototype = Object.extend(new Abstract.TableRow(), {
61
+ getNewRowIndex: function(target) {
62
+ return target.rowIndex;
63
+ }
64
+ });
65
+
66
+ TableRow.MoveAfter = Class.create();
67
+ TableRow.MoveAfter.prototype = Object.extend(new Abstract.TableRow(), {
68
+ getNewRowIndex: function(target) {
69
+ return target.rowIndex+1;
70
+ }
71
+ });
72
+
73
+ //Since scriptaculous doesn't support tables and causes and error applying effects to rows in IE 6.0 and Safari we need this wrapper
74
+
75
+ Abstract.TableEffect = function() {};
76
+ Abstract.TableEffect.prototype = Object.extend(new Abstract.Table(), {
77
+ callEffect: function(target, command) {
78
+ var target = $(target);
79
+ if (this.tagTest(target,'tr')) {
80
+ var length = target.cells.length;
81
+ for (var i = 0; i < length; i++) {
82
+ eval("new " + command + "(target.cells[i]);");
83
+ }
84
+ } else {
85
+ eval("new " + command + "(targetElement);");
86
+ }
87
+ }
88
+ });
89
+
90
+ var TableEffect = new Object();
91
+
92
+ TableEffect.Fade = Class.create();
93
+ TableEffect.Fade.prototype = Object.extend(new Abstract.TableEffect(), {
94
+ initialize: function(target) {
95
+ this.callEffect(target, 'Effect.Fade');
96
+ }
97
+ });
98
+
99
+ TableEffect.Highlight = Class.create();
100
+ TableEffect.Highlight.prototype = Object.extend(new Abstract.TableEffect(), {
101
+ initialize: function(target) {
102
+ this.callEffect(target, 'Effect.Highlight');
103
+ }
104
+ });
105
+
106
+ //The following is a utility to paint beautiful stripes on our table rows, a lot of logic for setting the colors explicitly on each element
107
+ // if hightlighting is going to overwrite out CSS styles otherwise.
108
+
109
+ var TableBodyUtil = {
110
+ enableHighlighting: function(tableBody) {
111
+ this.getColorStore(tableBody).highlighting = true;
112
+ },
113
+ disableHighlighting: function(tableBody) {
114
+ this.getColorStore(tableBody).highlighting = false;
115
+ },
116
+ highlightingEnabled: function(tableBody) {
117
+ var returnValue = true;
118
+ if (this.getColorStore(tableBody).highlighting != null) {
119
+ returnValue = this.getColorStore(tableBody).highlighting;
120
+ }
121
+ return returnValue;
122
+ },
123
+ // We are going to proxy all highlighting through this so that we can uniformly allow/block it (and subsequently paintStripes depending on this)
124
+ highlight: function(target, tableBody) {
125
+ if (this.highlightingEnabled(tableBody)) {
126
+ new TableEffect.Highlight(target);
127
+ }
128
+ },
129
+ getColorStore: function(tableBody) {
130
+ var uniqueId = tableBody.id;
131
+
132
+ if (this.tables == null) {
133
+ this.tables = new Object();
134
+ }
135
+
136
+ if (this.tables[String(uniqueId)] == null) {
137
+ this.tables[String(uniqueId)] = new Object();
138
+ }
139
+
140
+ return this.tables[String(uniqueId)];
141
+ },
142
+ paintStripes: function(tableBody) {
143
+ var even = false;
144
+ var tableBody = tableBody;
145
+ var tableRows = tableBody.getElementsByTagName("tr");
146
+ var length = tableBody.rows.length;
147
+
148
+ var colorStore = this.getColorStore(tableBody);
149
+
150
+ for (var i = 0; i < length; i++) {
151
+ var tableRow = tableBody.rows[i];
152
+ //Make sure to skip rows that are create or edit rows or messages
153
+ if (!Element.hasClassName(tableRow, "edit")
154
+ && !Element.hasClassName(tableRow, "create")
155
+ && !Element.hasClassName(tableRow, "deleted")
156
+ && !Element.hasClassName(tableRow, "message")) {
157
+
158
+ if (even) {
159
+ Element.addClassName(tableRow, "even");
160
+ if (this.highlightingEnabled(tableBody)) {
161
+ //If we don't already know what the color is supposed to be we'll poll it from the styles and then save that to apply later
162
+ if (!colorStore.evenColor) {
163
+ colorStore.evenColor = this.getCellBackgroundColor(tableRow);
164
+ // Safari won't pick up the style of the color if the row is not displayed so we are going to hide and then show
165
+ // the dummy row if thats what this is.
166
+ if (colorStore.evenColor == null) {
167
+ Element.show(tableRow);
168
+ colorStore.evenColor = this.getCellBackgroundColor(tableRow);
169
+ Element.hide(tableRow);
170
+ }
171
+ }
172
+ this.setCellBackgroundColor(tableRow, colorStore.evenColor);
173
+ }
174
+ } else {
175
+ Element.removeClassName(tableRow, "even");
176
+
177
+ if (this.highlightingEnabled(tableBody)) {
178
+ if (!colorStore.oddColor) {
179
+ colorStore.oddColor = this.getCellBackgroundColor(tableRow);
180
+ }
181
+ this.setCellBackgroundColor(tableRow, colorStore.oddColor);
182
+ }
183
+ }
184
+ even = !even;
185
+ }
186
+ }
187
+ },
188
+ getCellBackgroundColor: function(tableRow) {
189
+ var tableCell = tableRow.getElementsByTagName("td")[0];
190
+ return Element.getStyle(tableCell, 'background-color');
191
+ },
192
+ setCellBackgroundColor: function(tableRow, color) {
193
+ var length = tableRow.cells.length;
194
+ for (var i = 0; i < length; i++) {
195
+ try {
196
+ tableRow.cells[i].style.backgroundColor = color;
197
+ } catch (e) {
198
+ alert(e);
199
+ }
200
+ }
201
+ },
202
+ countRows: function(tableBody) {
203
+ var tableBody = tableBody;
204
+ var length = tableBody.rows.length;
205
+
206
+ var validRows = 0;
207
+
208
+ for (var i = 0; i < length; i++) {
209
+ var tableRow = tableBody.rows[i];
210
+ //Make sure to skip rows that are deleted or message
211
+ if (!Element.hasClassName(tableRow, "deleted")
212
+ && !Element.hasClassName(tableRow, "message")
213
+ && !Element.hasClassName(tableRow, "ignore")) {
214
+ validRows++;
215
+ }
216
+ }
217
+ return validRows;
218
+ }
219
+ }
220
+
221
+ var AjaxScaffold = {
222
+ newOnLoading: function(request, type) {
223
+ Element.show(this.getNewIndicator(type));
224
+ },
225
+ newOnFailure: function(request, type) {
226
+ this.showError(type, request.responseText);
227
+ Element.hide(this.getNewIndicator(type));
228
+ },
229
+ newOnSuccess: function(request, type) {
230
+ var createForm = request.responseText;
231
+ var id = this.getId(request,type);
232
+
233
+ new Insertion.Top(this.getTableBodyElement(type), createForm);
234
+
235
+ Element.hide(this.getEmptyMessageElement(type));
236
+ Element.hide(this.getNewIndicator(type));
237
+
238
+ var createElement = this.getCreateElement(type, id);
239
+ Element.show(createElement);
240
+ TableBodyUtil.highlight(createElement,this.getTableBodyElement(type));
241
+ Form.focusFirstElement(this.getFormElement('create',type,id));
242
+ },
243
+ newOnComplete: function(request, type) {
244
+
245
+ },
246
+ getNewIndicator: function(type) {
247
+ return $(type + "-new-loading-indicator");
248
+ },
249
+
250
+
251
+ createOnLoading: function(request,type,id) {
252
+ Element.show(this.getIndicator('create',type,id));
253
+ },
254
+ createOnFailure: function(request,type,id) {
255
+ var errorElement = this.getFormErrorElement('create',type,id)
256
+ var errorMessages = request.responseText;
257
+ errorElement.innerHTML = errorMessages;
258
+ Element.show(errorElement);
259
+ Element.hide(this.getIndicator('create',type,id));
260
+ },
261
+ createOnSuccess: function(request,type,id) {
262
+ var createElement = this.getCreateElement(type,id);
263
+
264
+ var view = request.responseText;
265
+ var viewId = this.getId(request,type);
266
+
267
+ // TODO : Convert this into TableRow.InsertAfter
268
+ new Insertion.Bottom(this.getTableBodyElement(type), view);
269
+ var viewElement = this.getViewElement(type,viewId);
270
+ new TableRow.MoveAfter(createElement, viewElement);
271
+
272
+ Element.remove(createElement);
273
+ Element.show(viewElement);
274
+
275
+ TableBodyUtil.paintStripes(this.getTableBodyElement(type));
276
+ TableBodyUtil.highlight(viewElement,this.getTableBodyElement(type));
277
+ },
278
+ createOnCancel: function(type,id) {
279
+ var createElement = this.getCreateElement(type,id);
280
+ Element.remove(createElement);
281
+ if (TableBodyUtil.countRows(this.getTableBodyElement(type)) == 0) {
282
+ Element.show(this.getEmptyMessageElement(type));
283
+ }
284
+ },
285
+
286
+
287
+ deleteOnLoading: function(type,id) {
288
+
289
+ },
290
+ deleteOnFailure: function(type,id) {
291
+ this.showError(type, request.responseText);
292
+ },
293
+ deleteOnSuccess: function(type,id) {
294
+ var viewElement = this.getViewElement(type,id);
295
+
296
+ new TableEffect.Fade(viewElement);
297
+ //We cannot set a timeout to remove this element from the DOM b/c once fade is complete
298
+ // get by ID no longer works on this element. So we'll set a class so that it isn't picked up in a re striping
299
+ Element.addClassName(viewElement, 'deleted');
300
+
301
+ if (TableBodyUtil.countRows(this.getTableBodyElement(type)) == 0) {
302
+ Element.show(this.getEmptyMessageElement(type));
303
+ } else {
304
+ TableBodyUtil.paintStripes(this.getTableBodyElement(type));
305
+ }
306
+ },
307
+
308
+
309
+ editOnLoading: function(request,type,id) {
310
+ Element.show(this.getIndicator('edit',type,id));
311
+ },
312
+ editOnFailure: function(request,type,id) {
313
+ this.showError(type, request.responseText);
314
+ Element.hide(this.getIndicator('edit',type,id));
315
+ },
316
+ editOnSuccess: function(request,type,id) {
317
+ var viewElement = this.getViewElement(type,id);
318
+
319
+ //Ajax.Update with Insertion.top does not work when being used as a component with other scaffolds on the screen
320
+ // the only safe way it seems it to always insert new elements at the bottom and then move them into the appropriate location
321
+ var editForm = request.responseText;
322
+
323
+ // TODO : Convert this into TableRow.InsertAfter
324
+ new Insertion.Bottom(this.getTableBodyElement(type), editForm);
325
+ var editElement = this.getEditElement(type,id);
326
+ new TableRow.MoveAfter(viewElement, editElement);
327
+
328
+ var formElement = this.getFormElement('edit',type,id);
329
+
330
+ Element.hide(viewElement);
331
+ Element.hide(this.getIndicator('edit',type,id));
332
+ Element.show(editElement);
333
+ new Effect.Highlight(editElement);
334
+ Form.focusFirstElement(formElement);
335
+ },
336
+
337
+
338
+ updateOnLoading: function(request,type,id) {
339
+ Element.show(this.getIndicator('update',type,id));
340
+ },
341
+ updateOnFailure: function(request,type,id) {
342
+ var errorElement = this.getFormErrorElement('update',type,id)
343
+ var errorMessages = request.responseText;
344
+ errorElement.innerHTML = errorMessages;
345
+ Element.show(errorElement);
346
+ Element.hide(this.getIndicator('update',type,id));
347
+ },
348
+ updateOnSuccess: function(request,type,id) {
349
+ var editElement = this.getEditElement(type,id);
350
+ var formElement = this.getFormElement(type,id);
351
+ var viewElement = this.getViewElement(type,id);
352
+
353
+ Element.remove(viewElement);
354
+
355
+ var view = request.responseText;
356
+
357
+ // TODO : Convert this into a TableRow.InsertBefore
358
+ new Insertion.Bottom(this.getTableBodyElement(type), view);
359
+ var viewElement = this.getViewElement(type,id);
360
+ new TableRow.MoveBefore(editElement, viewElement);
361
+
362
+ Element.remove(editElement);
363
+ Element.show(viewElement);
364
+
365
+ TableBodyUtil.paintStripes(this.getTableBodyElement(type));
366
+ TableBodyUtil.highlight(viewElement,this.getTableBodyElement(type));
367
+ },
368
+ updateOnCancel: function(type,id) {
369
+ var viewElement = this.getViewElement(type,id);
370
+ var editElement = this.getEditElement(type,id);
371
+
372
+ Element.show(viewElement);
373
+ Element.remove(editElement);
374
+ },
375
+
376
+
377
+ showError: function(type,html) {
378
+ var errorElement = this.getErrorMessageElement(type);
379
+ var errorMessageElement = errorElement.getElementsByTagName("p")[0];
380
+ errorMessageElement.innerHTML = html;
381
+ Element.show(errorElement);
382
+ TableBodyUtil.highlight(errorElement,this.getTableBodyElement(type));
383
+ },
384
+ hideError: function(type) {
385
+ var errorElement = this.getErrorMessageElement(type);
386
+ Element.hide(errorElement);
387
+ },
388
+
389
+
390
+
391
+
392
+ getIndicator: function(scope, type,id) {
393
+ return $(type + '-' + scope + '-' + id + "-loading-indicator");
394
+ },
395
+ getFormErrorElement: function(scope,type,id) {
396
+ return $(scope + '-' + type + '-' + id + '-errors')
397
+ },
398
+ getId: function(request, type) {
399
+ return request.getResponseHeader(type + '-id');
400
+ },
401
+ getCreateElementId: function(type,id) {
402
+ return 'create-' + type + '-' + id;
403
+ },
404
+ getCreateElement: function(type,id) {
405
+ return $('create-' + type + '-' + id);
406
+ },
407
+ getViewElement: function(type,id) {
408
+ return $('view-' + type + '-' + id);
409
+ },
410
+ getEditElement: function(type,id) {
411
+ return $('edit-' + type + '-' + id);
412
+ },
413
+ getFormElement: function(scope,type,id) {
414
+ return $(scope+'-'+type + '-' + id + '-form');
415
+ },
416
+ getEmptyMessageElement: function(type) {
417
+ return $(type + '-empty-message');
418
+ },
419
+ getErrorMessageElement: function(type) {
420
+ return $(type + '-error-message');
421
+ },
422
+ getTableBodyElement: function(type) {
423
+ return $(type + '-list-body');
424
+ }
425
+ }