ajax_scaffold_generator 1.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/templates/script.js CHANGED
@@ -1,47 +1,327 @@
1
- var <%= plural_name %>Block = {
2
- edit: function(id) {
3
- Element.hide('<%= singular_name %>_' + id);
4
- Element.show('edit_<%= singular_name %>_dialog_' + id);
5
- },
6
-
7
- editSuccess: function(id) {
8
- this.editCancel(id);
9
-
10
- new Effect.Highlight('<%= singular_name %>_' + id);
11
- },
12
-
13
- editCancel: function(id) {
14
- $('edit_<%= singular_name %>_error_' + id).innerHTML = '';
15
-
16
- Element.show('<%= singular_name %>_' + id);
17
- Element.hide('edit_<%= singular_name %>_dialog_' + id);
18
- },
19
-
20
- add: function() {
21
- Element.hide('add_<%= singular_name %>');
22
- Element.show('add_<%= singular_name %>_dialog');
23
- Form.reset('add_<%= singular_name %>_form');
24
- },
25
-
26
- addSuccess: function(request) {
27
- this.addCancel();
28
-
29
- new Insertion.Bottom('<%= plural_name %>', request.responseText);
30
- new Effect.Highlight('<%= singular_name %>_' + this.getNewId(request));
31
- },
32
-
33
- addFailure: function(request) {
34
- $('add_<%= singular_name %>_error').innerHTML = request.responseText;
35
- },
36
-
37
- addCancel: function() {
38
- $('add_<%= singular_name %>_error').innerHTML = '';
39
-
40
- Element.show('add_<%= singular_name %>');
41
- Element.hide('add_<%= singular_name %>_dialog');
42
- },
43
-
44
- getNewId: function(request) {
45
- return request.getResponseHeader('<%= model_name %>ID');
46
- }
47
- }
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
+ });