active_scaffold 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (272) hide show
  1. data/.autotest +27 -0
  2. data/CHANGELOG +152 -0
  3. data/Gemfile +4 -0
  4. data/MIT-LICENSE +20 -0
  5. data/README +51 -0
  6. data/Rakefile +24 -0
  7. data/active_scaffold.gemspec +24 -0
  8. data/environment.rb +22 -0
  9. data/frontends/default/images/add.gif +0 -0
  10. data/frontends/default/images/arrow_down.gif +0 -0
  11. data/frontends/default/images/arrow_up.gif +0 -0
  12. data/frontends/default/images/close.gif +0 -0
  13. data/frontends/default/images/cross.png +0 -0
  14. data/frontends/default/images/indicator-small.gif +0 -0
  15. data/frontends/default/images/indicator.gif +0 -0
  16. data/frontends/default/images/magnifier.png +0 -0
  17. data/frontends/default/javascripts/jquery/active_scaffold.js +957 -0
  18. data/frontends/default/javascripts/jquery/jquery.editinplace.js +726 -0
  19. data/frontends/default/javascripts/prototype/active_scaffold.js +954 -0
  20. data/frontends/default/javascripts/prototype/dhtml_history.js +867 -0
  21. data/frontends/default/javascripts/prototype/form_enhancements.js +117 -0
  22. data/frontends/default/javascripts/prototype/rico_corner.js +370 -0
  23. data/frontends/default/stylesheets/stylesheet-ie.css +35 -0
  24. data/frontends/default/stylesheets/stylesheet.css +858 -0
  25. data/frontends/default/views/_add_existing_form.html.erb +30 -0
  26. data/frontends/default/views/_base_form.html.erb +41 -0
  27. data/frontends/default/views/_create_form.html.erb +6 -0
  28. data/frontends/default/views/_create_form_on_list.html.erb +5 -0
  29. data/frontends/default/views/_field_search.html.erb +32 -0
  30. data/frontends/default/views/_form.html.erb +24 -0
  31. data/frontends/default/views/_form_association.html.erb +14 -0
  32. data/frontends/default/views/_form_association_footer.html.erb +40 -0
  33. data/frontends/default/views/_form_attribute.html.erb +15 -0
  34. data/frontends/default/views/_form_hidden_attribute.html.erb +2 -0
  35. data/frontends/default/views/_form_messages.html.erb +5 -0
  36. data/frontends/default/views/_horizontal_subform.html.erb +19 -0
  37. data/frontends/default/views/_horizontal_subform_header.html.erb +10 -0
  38. data/frontends/default/views/_horizontal_subform_record.html.erb +37 -0
  39. data/frontends/default/views/_human_conditions.html.erb +1 -0
  40. data/frontends/default/views/_list.html.erb +18 -0
  41. data/frontends/default/views/_list_actions.html.erb +16 -0
  42. data/frontends/default/views/_list_calculations.html.erb +16 -0
  43. data/frontends/default/views/_list_column_headings.html.erb +12 -0
  44. data/frontends/default/views/_list_header.html.erb +12 -0
  45. data/frontends/default/views/_list_inline_adapter.html.erb +10 -0
  46. data/frontends/default/views/_list_messages.html.erb +32 -0
  47. data/frontends/default/views/_list_pagination.html.erb +11 -0
  48. data/frontends/default/views/_list_pagination_links.html.erb +9 -0
  49. data/frontends/default/views/_list_record.html.erb +14 -0
  50. data/frontends/default/views/_list_record_columns.html.erb +8 -0
  51. data/frontends/default/views/_list_with_header.html.erb +32 -0
  52. data/frontends/default/views/_messages.html.erb +10 -0
  53. data/frontends/default/views/_render_field.js.rjs +13 -0
  54. data/frontends/default/views/_row.html.erb +12 -0
  55. data/frontends/default/views/_search.html.erb +34 -0
  56. data/frontends/default/views/_search_attribute.html.erb +10 -0
  57. data/frontends/default/views/_show.html.erb +8 -0
  58. data/frontends/default/views/_show_columns.html.erb +12 -0
  59. data/frontends/default/views/_update_actions.html.erb +9 -0
  60. data/frontends/default/views/_update_form.html.erb +5 -0
  61. data/frontends/default/views/_vertical_subform.html.erb +12 -0
  62. data/frontends/default/views/_vertical_subform_record.html.erb +38 -0
  63. data/frontends/default/views/add_existing.js.rjs +17 -0
  64. data/frontends/default/views/add_existing_form.html.erb +5 -0
  65. data/frontends/default/views/create.html.erb +5 -0
  66. data/frontends/default/views/delete.html.erb +13 -0
  67. data/frontends/default/views/destroy.js.rjs +5 -0
  68. data/frontends/default/views/edit_associated.js.rjs +11 -0
  69. data/frontends/default/views/field_search.html.erb +5 -0
  70. data/frontends/default/views/form_messages.js.rjs +1 -0
  71. data/frontends/default/views/list.html.erb +1 -0
  72. data/frontends/default/views/list.js.rjs +1 -0
  73. data/frontends/default/views/on_action_update.js.rjs +8 -0
  74. data/frontends/default/views/on_create.js.rjs +24 -0
  75. data/frontends/default/views/on_update.js.rjs +15 -0
  76. data/frontends/default/views/search.html.erb +5 -0
  77. data/frontends/default/views/show.html.erb +5 -0
  78. data/frontends/default/views/update.html.erb +8 -0
  79. data/frontends/default/views/update_column.js.rjs +13 -0
  80. data/frontends/default/views/update_row.js.rjs +1 -0
  81. data/init.rb +1 -0
  82. data/install_assets.rb +44 -0
  83. data/lib/active_record_permissions.rb +134 -0
  84. data/lib/active_scaffold.rb +279 -0
  85. data/lib/active_scaffold/actions/common_search.rb +22 -0
  86. data/lib/active_scaffold/actions/core.rb +150 -0
  87. data/lib/active_scaffold/actions/create.rb +152 -0
  88. data/lib/active_scaffold/actions/delete.rb +72 -0
  89. data/lib/active_scaffold/actions/field_search.rb +82 -0
  90. data/lib/active_scaffold/actions/list.rb +128 -0
  91. data/lib/active_scaffold/actions/mark.rb +50 -0
  92. data/lib/active_scaffold/actions/nested.rb +241 -0
  93. data/lib/active_scaffold/actions/search.rb +47 -0
  94. data/lib/active_scaffold/actions/show.rb +54 -0
  95. data/lib/active_scaffold/actions/subform.rb +17 -0
  96. data/lib/active_scaffold/actions/update.rb +134 -0
  97. data/lib/active_scaffold/attribute_params.rb +207 -0
  98. data/lib/active_scaffold/bridges/ancestry/bridge.rb +5 -0
  99. data/lib/active_scaffold/bridges/ancestry/lib/ancestry_bridge.rb +38 -0
  100. data/lib/active_scaffold/bridges/bridge.rb +52 -0
  101. data/lib/active_scaffold/bridges/calendar_date_select/bridge.rb +16 -0
  102. data/lib/active_scaffold/bridges/calendar_date_select/lib/as_cds_bridge.rb +79 -0
  103. data/lib/active_scaffold/bridges/carrierwave/bridge.rb +7 -0
  104. data/lib/active_scaffold/bridges/carrierwave/lib/carrierwave_bridge.rb +38 -0
  105. data/lib/active_scaffold/bridges/carrierwave/lib/carrierwave_bridge_helpers.rb +26 -0
  106. data/lib/active_scaffold/bridges/carrierwave/lib/form_ui.rb +35 -0
  107. data/lib/active_scaffold/bridges/carrierwave/lib/list_ui.rb +17 -0
  108. data/lib/active_scaffold/bridges/date_picker/bridge.rb +22 -0
  109. data/lib/active_scaffold/bridges/date_picker/lib/datepicker_bridge.rb +225 -0
  110. data/lib/active_scaffold/bridges/date_picker/public/javascripts/date_picker_bridge.js +22 -0
  111. data/lib/active_scaffold/bridges/file_column/bridge.rb +11 -0
  112. data/lib/active_scaffold/bridges/file_column/lib/as_file_column_bridge.rb +46 -0
  113. data/lib/active_scaffold/bridges/file_column/lib/file_column_helpers.rb +59 -0
  114. data/lib/active_scaffold/bridges/file_column/lib/form_ui.rb +37 -0
  115. data/lib/active_scaffold/bridges/file_column/lib/list_ui.rb +26 -0
  116. data/lib/active_scaffold/bridges/file_column/test/functional/file_column_keep_test.rb +43 -0
  117. data/lib/active_scaffold/bridges/file_column/test/mock_model.rb +9 -0
  118. data/lib/active_scaffold/bridges/file_column/test/test_helper.rb +15 -0
  119. data/lib/active_scaffold/bridges/paperclip/bridge.rb +12 -0
  120. data/lib/active_scaffold/bridges/paperclip/lib/form_ui.rb +27 -0
  121. data/lib/active_scaffold/bridges/paperclip/lib/list_ui.rb +16 -0
  122. data/lib/active_scaffold/bridges/paperclip/lib/paperclip_bridge.rb +38 -0
  123. data/lib/active_scaffold/bridges/paperclip/lib/paperclip_bridge_helpers.rb +26 -0
  124. data/lib/active_scaffold/bridges/semantic_attributes/bridge.rb +5 -0
  125. data/lib/active_scaffold/bridges/semantic_attributes/lib/semantic_attributes_bridge.rb +20 -0
  126. data/lib/active_scaffold/bridges/shared/date_bridge.rb +187 -0
  127. data/lib/active_scaffold/bridges/tiny_mce/bridge.rb +5 -0
  128. data/lib/active_scaffold/bridges/tiny_mce/lib/tiny_mce_bridge.rb +45 -0
  129. data/lib/active_scaffold/bridges/validation_reflection/bridge.rb +8 -0
  130. data/lib/active_scaffold/bridges/validation_reflection/lib/validation_reflection_bridge.rb +21 -0
  131. data/lib/active_scaffold/config/base.rb +54 -0
  132. data/lib/active_scaffold/config/core.rb +229 -0
  133. data/lib/active_scaffold/config/create.rb +43 -0
  134. data/lib/active_scaffold/config/delete.rb +25 -0
  135. data/lib/active_scaffold/config/field_search.rb +74 -0
  136. data/lib/active_scaffold/config/form.rb +46 -0
  137. data/lib/active_scaffold/config/list.rb +174 -0
  138. data/lib/active_scaffold/config/mark.rb +22 -0
  139. data/lib/active_scaffold/config/nested.rb +43 -0
  140. data/lib/active_scaffold/config/search.rb +68 -0
  141. data/lib/active_scaffold/config/show.rb +34 -0
  142. data/lib/active_scaffold/config/subform.rb +35 -0
  143. data/lib/active_scaffold/config/update.rb +38 -0
  144. data/lib/active_scaffold/configurable.rb +29 -0
  145. data/lib/active_scaffold/constraints.rb +179 -0
  146. data/lib/active_scaffold/data_structures/action_columns.rb +133 -0
  147. data/lib/active_scaffold/data_structures/action_link.rb +162 -0
  148. data/lib/active_scaffold/data_structures/action_links.rb +59 -0
  149. data/lib/active_scaffold/data_structures/actions.rb +45 -0
  150. data/lib/active_scaffold/data_structures/column.rb +348 -0
  151. data/lib/active_scaffold/data_structures/columns.rb +75 -0
  152. data/lib/active_scaffold/data_structures/error_message.rb +24 -0
  153. data/lib/active_scaffold/data_structures/nested_info.rb +108 -0
  154. data/lib/active_scaffold/data_structures/set.rb +62 -0
  155. data/lib/active_scaffold/data_structures/sorting.rb +168 -0
  156. data/lib/active_scaffold/finder.rb +333 -0
  157. data/lib/active_scaffold/helpers/association_helpers.rb +40 -0
  158. data/lib/active_scaffold/helpers/controller_helpers.rb +40 -0
  159. data/lib/active_scaffold/helpers/country_helpers.rb +352 -0
  160. data/lib/active_scaffold/helpers/form_column_helpers.rb +343 -0
  161. data/lib/active_scaffold/helpers/human_condition_helpers.rb +59 -0
  162. data/lib/active_scaffold/helpers/id_helpers.rb +131 -0
  163. data/lib/active_scaffold/helpers/list_column_helpers.rb +363 -0
  164. data/lib/active_scaffold/helpers/pagination_helpers.rb +55 -0
  165. data/lib/active_scaffold/helpers/search_column_helpers.rb +238 -0
  166. data/lib/active_scaffold/helpers/show_column_helpers.rb +46 -0
  167. data/lib/active_scaffold/helpers/view_helpers.rb +315 -0
  168. data/lib/active_scaffold/locale/de.rb +113 -0
  169. data/lib/active_scaffold/locale/en.rb +118 -0
  170. data/lib/active_scaffold/locale/es.yml +112 -0
  171. data/lib/active_scaffold/locale/fr.rb +113 -0
  172. data/lib/active_scaffold/locale/hu.yml +63 -0
  173. data/lib/active_scaffold/locale/ja.yml +64 -0
  174. data/lib/active_scaffold/locale/ru.yml +62 -0
  175. data/lib/active_scaffold/marked_model.rb +38 -0
  176. data/lib/dhtml_confirm.rb +54 -0
  177. data/lib/extensions/action_controller_rendering.rb +20 -0
  178. data/lib/extensions/action_view_rendering.rb +113 -0
  179. data/lib/extensions/action_view_resolver.rb +7 -0
  180. data/lib/extensions/active_record_offset.rb +12 -0
  181. data/lib/extensions/array.rb +7 -0
  182. data/lib/extensions/localize.rb +10 -0
  183. data/lib/extensions/name_option_for_datetime.rb +12 -0
  184. data/lib/extensions/nil_id_in_url_params.rb +7 -0
  185. data/lib/extensions/paginator_extensions.rb +26 -0
  186. data/lib/extensions/reverse_associations.rb +62 -0
  187. data/lib/extensions/routing_mapper.rb +34 -0
  188. data/lib/extensions/to_label.rb +8 -0
  189. data/lib/extensions/unsaved_associated.rb +61 -0
  190. data/lib/extensions/unsaved_record.rb +20 -0
  191. data/lib/extensions/usa_state.rb +46 -0
  192. data/lib/generators/active_scaffold/USAGE +29 -0
  193. data/lib/generators/active_scaffold/active_scaffold_generator.rb +20 -0
  194. data/lib/generators/active_scaffold_controller/USAGE +19 -0
  195. data/lib/generators/active_scaffold_controller/active_scaffold_controller_generator.rb +28 -0
  196. data/lib/generators/active_scaffold_controller/templates/controller.rb +4 -0
  197. data/lib/generators/active_scaffold_setup/USAGE +10 -0
  198. data/lib/generators/active_scaffold_setup/active_scaffold_setup_generator.rb +53 -0
  199. data/lib/paginator.rb +136 -0
  200. data/lib/responds_to_parent.rb +70 -0
  201. data/public/blank.html +33 -0
  202. data/shoulda_macros/macros.rb +136 -0
  203. data/test/bridges/bridge_test.rb +47 -0
  204. data/test/config/base_test.rb +15 -0
  205. data/test/config/create_test.rb +55 -0
  206. data/test/config/list_test.rb +74 -0
  207. data/test/config/show_test.rb +43 -0
  208. data/test/config/update_test.rb +17 -0
  209. data/test/const_mocker.rb +36 -0
  210. data/test/data_structures/action_columns_test.rb +113 -0
  211. data/test/data_structures/action_link_test.rb +78 -0
  212. data/test/data_structures/action_links_test.rb +78 -0
  213. data/test/data_structures/actions_test.rb +25 -0
  214. data/test/data_structures/association_column_test.rb +42 -0
  215. data/test/data_structures/column_test.rb +185 -0
  216. data/test/data_structures/columns_test.rb +69 -0
  217. data/test/data_structures/error_message_test.rb +28 -0
  218. data/test/data_structures/set_test.rb +86 -0
  219. data/test/data_structures/sorting_test.rb +126 -0
  220. data/test/data_structures/standard_column_test.rb +24 -0
  221. data/test/data_structures/virtual_column_test.rb +23 -0
  222. data/test/extensions/active_record_test.rb +45 -0
  223. data/test/extensions/array_test.rb +12 -0
  224. data/test/helpers/form_column_helpers_test.rb +31 -0
  225. data/test/helpers/list_column_helpers_test.rb +31 -0
  226. data/test/helpers/pagination_helpers_test.rb +55 -0
  227. data/test/misc/active_record_permissions_test.rb +154 -0
  228. data/test/misc/attribute_params_test.rb +110 -0
  229. data/test/misc/configurable_test.rb +96 -0
  230. data/test/misc/constraints_test.rb +193 -0
  231. data/test/misc/finder_test.rb +93 -0
  232. data/test/misc/lang_test.rb +12 -0
  233. data/test/mock_app/.gitignore +2 -0
  234. data/test/mock_app/app/controllers/application_controller.rb +10 -0
  235. data/test/mock_app/app/helpers/application_helper.rb +3 -0
  236. data/test/mock_app/config/boot.rb +110 -0
  237. data/test/mock_app/config/database.yml +16 -0
  238. data/test/mock_app/config/environment.rb +43 -0
  239. data/test/mock_app/config/environments/development.rb +17 -0
  240. data/test/mock_app/config/environments/production.rb +28 -0
  241. data/test/mock_app/config/environments/test.rb +28 -0
  242. data/test/mock_app/config/initializers/backtrace_silencers.rb +7 -0
  243. data/test/mock_app/config/initializers/inflections.rb +10 -0
  244. data/test/mock_app/config/initializers/mime_types.rb +5 -0
  245. data/test/mock_app/config/initializers/new_rails_defaults.rb +19 -0
  246. data/test/mock_app/config/initializers/session_store.rb +15 -0
  247. data/test/mock_app/config/locales/en.yml +5 -0
  248. data/test/mock_app/config/routes.rb +43 -0
  249. data/test/mock_app/db/test.sqlite3 +1 -0
  250. data/test/mock_app/public/blank.html +33 -0
  251. data/test/mock_app/public/images/active_scaffold/DO_NOT_EDIT +2 -0
  252. data/test/mock_app/public/images/active_scaffold/default/add.gif +0 -0
  253. data/test/mock_app/public/images/active_scaffold/default/arrow_down.gif +0 -0
  254. data/test/mock_app/public/images/active_scaffold/default/arrow_up.gif +0 -0
  255. data/test/mock_app/public/images/active_scaffold/default/close.gif +0 -0
  256. data/test/mock_app/public/images/active_scaffold/default/cross.png +0 -0
  257. data/test/mock_app/public/images/active_scaffold/default/indicator-small.gif +0 -0
  258. data/test/mock_app/public/images/active_scaffold/default/indicator.gif +0 -0
  259. data/test/mock_app/public/images/active_scaffold/default/magnifier.png +0 -0
  260. data/test/mock_app/public/javascripts/active_scaffold/DO_NOT_EDIT +2 -0
  261. data/test/mock_app/public/javascripts/active_scaffold/default/active_scaffold.js +532 -0
  262. data/test/mock_app/public/javascripts/active_scaffold/default/dhtml_history.js +867 -0
  263. data/test/mock_app/public/javascripts/active_scaffold/default/form_enhancements.js +117 -0
  264. data/test/mock_app/public/javascripts/active_scaffold/default/rico_corner.js +370 -0
  265. data/test/mock_app/public/stylesheets/active_scaffold/DO_NOT_EDIT +2 -0
  266. data/test/mock_app/public/stylesheets/active_scaffold/default/stylesheet-ie.css +35 -0
  267. data/test/mock_app/public/stylesheets/active_scaffold/default/stylesheet.css +839 -0
  268. data/test/model_stub.rb +55 -0
  269. data/test/run_all.rb +8 -0
  270. data/test/test_helper.rb +39 -0
  271. data/uninstall.rb +13 -0
  272. metadata +478 -0
@@ -0,0 +1,954 @@
1
+ if (typeof Prototype == 'undefined')
2
+ {
3
+ warning = "ActiveScaffold Error: Prototype could not be found. Please make sure that your application's layout includes prototype.js (e.g. <%= javascript_include_tag :defaults %>) *before* it includes active_scaffold.js (e.g. <%= active_scaffold_includes %>).";
4
+ alert(warning);
5
+ }
6
+ if (Prototype.Version.substring(0, 3) < '1.6')
7
+ {
8
+ warning = "ActiveScaffold Error: Prototype version 1.6.x or higher is required. Please update prototype.js (rake rails:update:javascripts).";
9
+ alert(warning);
10
+ }
11
+ if (!Element.Methods.highlight) Element.addMethods({highlight: Prototype.emptyFunction});
12
+
13
+
14
+ document.observe("dom:loaded", function() {
15
+ document.on('ajax:create', 'form.as_form', function(event) {
16
+ var source = event.findElement();
17
+ var as_form = event.findElement('form');
18
+ if (source.nodeName.toUpperCase() == 'INPUT' && source.readAttribute('type') == 'button') {
19
+ // Hack: Prototype or rails.js somehow screw up event handling if someone clicks
20
+ // a button of type button such as Create Another <Association>
21
+ // as a result form is disabled but never reenabled..
22
+ } else {
23
+ if (as_form && as_form.readAttribute('data-loading') == 'true') {
24
+ ActiveScaffold.disable_form(as_form);
25
+ }
26
+ }
27
+ return true;
28
+ });
29
+ document.on('ajax:complete', 'form.as_form', function(event) {
30
+ var as_form = event.findElement('form');
31
+ if (as_form && as_form.readAttribute('data-loading') == 'true') {
32
+ ActiveScaffold.enable_form(as_form);
33
+ event.stop();
34
+ return false;
35
+ }
36
+ });
37
+ document.on('ajax:failure', 'form.as_form', function(event) {
38
+ var as_div = event.findElement('div.active-scaffold');
39
+ if (as_div) {
40
+ ActiveScaffold.report_500_response(as_div)
41
+ event.stop();
42
+ return false;
43
+ }
44
+ });
45
+ document.on('submit', 'form.as_form.as_remote_upload', function(event) {
46
+ var as_form = event.findElement('form');
47
+ if (as_form && as_form.readAttribute('data-loading') == 'true') {
48
+ setTimeout("ActiveScaffold.disable_form('" + as_form.id + "')", 10);
49
+ }
50
+ return true;
51
+ });
52
+ document.on('ajax:before', 'a.as_action', function(event) {
53
+ var action_link = ActiveScaffold.ActionLink.get(event.findElement());
54
+ if (action_link) {
55
+ if (action_link.is_disabled()) {
56
+ event.stop();
57
+ } else {
58
+ if (action_link.loading_indicator) action_link.loading_indicator.style.visibility = 'visible';
59
+ action_link.disable();
60
+ }
61
+ }
62
+ return true;
63
+ });
64
+ document.on('ajax:success', 'a.as_action', function(event) {
65
+ var action_link = ActiveScaffold.ActionLink.get(event.findElement());
66
+ if (action_link && event.memo && event.memo.request) {
67
+ if (action_link.position) {
68
+ action_link.insert(event.memo.request.transport.responseText);
69
+ if (action_link.hide_target) action_link.target.hide();
70
+ } else {
71
+ //event.memo.request.evalResponse(); // (clyfe) prototype evals the response by itself checking headers, this would eval twice
72
+ action_link.enable();
73
+ }
74
+ event.stop();
75
+ }
76
+ return true;
77
+ });
78
+ document.on('ajax:complete', 'a.as_action', function(event) {
79
+ var action_link = ActiveScaffold.ActionLink.get(event.findElement());
80
+ if (action_link && action_link.loading_indicator) {
81
+ action_link.loading_indicator.style.visibility = 'hidden';
82
+ }
83
+ return true;
84
+ });
85
+ document.on('ajax:failure', 'a.as_action', function(event) {
86
+ var action_link = ActiveScaffold.ActionLink.get(event.findElement());
87
+ if (action_link) {
88
+ ActiveScaffold.report_500_response(action_link.scaffold_id());
89
+ action_link.enable();
90
+ }
91
+ return true;
92
+ });
93
+ document.on('ajax:before', 'a.as_cancel', function(event) {
94
+ var as_cancel = event.findElement();
95
+ var action_link = ActiveScaffold.find_action_link(as_cancel);
96
+
97
+ if (action_link) {
98
+ var refresh_data = as_cancel.readAttribute('data-refresh');
99
+ if (refresh_data === 'true' && action_link.refresh_url) {
100
+ event.memo.url = action_link.refresh_url;
101
+ } else if (refresh_data === 'false' || as_cancel.readAttribute('href').blank()) {
102
+ action_link.close();
103
+ event.stop();
104
+ }
105
+ }
106
+ return true;
107
+ });
108
+ document.on('ajax:success', 'a.as_cancel', function(event) {
109
+ var action_link = ActiveScaffold.find_action_link(event.findElement());
110
+ if (action_link) {
111
+ if (action_link.position) {
112
+ action_link.close(event.memo.request.responseText);
113
+ } else {
114
+ event.memo.request.evalResponse();
115
+ }
116
+ }
117
+ return true;
118
+ });
119
+ document.on('ajax:failure', 'a.as_cancel', function(event) {
120
+ var action_link = ActiveScaffold.find_action_link(event.findElement());
121
+ if (action_link) {
122
+ ActiveScaffold.report_500_response(action_link.scaffold_id());
123
+ }
124
+ return true;
125
+ });
126
+ document.on('ajax:before', 'a.as_sort', function(event) {
127
+ var as_sort = event.findElement();
128
+ var history_controller_id = as_sort.readAttribute('data-page-history');
129
+ if (history_controller_id) addActiveScaffoldPageToHistory(as_sort.readAttribute('href'), history_controller_id);
130
+ as_sort.up('th').addClassName('loading');
131
+ return true;
132
+ });
133
+ document.on('ajax:failure', 'a.as_sort', function(event) {
134
+ var as_scaffold = event.findElement('.active-scaffold');
135
+ ActiveScaffold.report_500_response(as_scaffold);
136
+ return true;
137
+ });
138
+ document.on('mouseover', 'span.in_place_editor_field', function(event) {
139
+ event.findElement().addClassName('hover');
140
+ });
141
+ document.on('mouseout', 'span.in_place_editor_field', function(event) {
142
+ event.findElement().removeClassName('hover');
143
+ });
144
+ document.on('click', 'span.in_place_editor_field', function(event) {
145
+ var span = event.findElement('span.in_place_editor_field');
146
+
147
+ if (typeof(span.inplace_edit) === 'undefined') {
148
+ var options = {htmlResponse: false,
149
+ onEnterHover: null,
150
+ onLeaveHover: null,
151
+ onComplete: null,
152
+ params: '',
153
+ ajaxOptions: {method: 'post'}},
154
+ csrf_param = $$('meta[name=csrf-param]')[0],
155
+ csrf_token = $$('meta[name=csrf-token]')[0],
156
+ my_parent = span.up(),
157
+ column_heading = null;
158
+
159
+ if (my_parent.nodeName.toLowerCase() === 'td') {
160
+ var heading_selector = '.' + span.up().readAttribute('class').split(' ')[0] + '_heading';
161
+ column_heading = span.up('.active-scaffold').down(heading_selector);
162
+ } else if (my_parent.nodeName.toLowerCase() === 'th') {
163
+ column_heading = my_parent;
164
+ }
165
+
166
+ var render_url = column_heading.readAttribute('data-ie_render_url'),
167
+ mode = column_heading.readAttribute('data-ie_mode'),
168
+ record_id = span.readAttribute('data-ie_id');
169
+
170
+ ActiveScaffold.read_inplace_edit_heading_attributes(column_heading, options);
171
+
172
+ if (span.readAttribute('data-ie_url')) {
173
+ options.url = span.readAttribute('data-ie_url');
174
+ } else {
175
+ options.url = column_heading.readAttribute('data-ie_url');
176
+ }
177
+ if (record_id) options.url = options.url.sub('__id__', record_id);
178
+
179
+ if (csrf_param) options['params'] = csrf_param.readAttribute('content') + '=' + csrf_token.readAttribute('content');
180
+
181
+ if (mode === 'clone') {
182
+ options.nodeIdSuffix = record_id;
183
+ options.inplacePatternSelector = '#' + column_heading.id + ' .as_inplace_pattern';
184
+ options['onFormCustomization'] = new Function('element', 'form', 'element.clonePatternField();');
185
+ }
186
+
187
+ if (render_url) {
188
+ var plural = false;
189
+ if (column_heading.readAttribute('data-ie_plural')) plural = true;
190
+ options['onFormCustomization'] = new Function('element', 'form', 'element.setFieldFromAjax(' + "'" + render_url.sub('__id__', record_id) + "', {plural: " + plural + '});');
191
+ }
192
+
193
+ if (mode === 'inline_checkbox') {
194
+ ActiveScaffold.process_checkbox_inplace_edit(span.down('input[type="checkbox"]'), options);
195
+ } else {
196
+ ActiveScaffold.create_inplace_editor(span, options);
197
+ }
198
+ }
199
+ return true;
200
+ });
201
+ document.on('ajax:before', 'a.as_paginate', function(event) {
202
+ var as_paginate = event.findElement();
203
+ var loading_indicator = as_paginate.up().down('img.loading-indicator');
204
+ var history_controller_id = as_paginate.readAttribute('data-page-history');
205
+
206
+ if (history_controller_id) addActiveScaffoldPageToHistory(as_paginate.readAttribute('href'), history_controller_id);
207
+ if (loading_indicator) loading_indicator.style.visibility = 'visible';
208
+ return true;
209
+ });
210
+ document.on('ajax:failure', 'a.as_paginate', function(event) {
211
+ var as_scaffold = event.findElement('.active-scaffold');
212
+ ActiveScaffold.report_500_response(as_scaffold);
213
+ return true;
214
+ });
215
+ document.on('ajax:complete', 'a.as_paginate', function(event) {
216
+ var as_paginate = event.findElement();
217
+ var loading_indicator = as_paginate.up().down('img.loading-indicator');
218
+
219
+ if(loading_indicator) loading_indicator.style.visibility = 'hidden';
220
+ return true;
221
+ });
222
+ document.on('ajax:before', 'input[type=button].as_add_existing', function(event) {
223
+ var button = event.findElement();
224
+ var url = button.readAttribute('href').sub('--ID--', button.previous().getValue());
225
+ event.memo.url = url;
226
+ return true;
227
+ });
228
+ document.on('change', 'input.update_form, select.update_form', function(event) {
229
+ var element = event.findElement();
230
+ var as_form = element.up('form.as_form');
231
+
232
+ new Ajax.Request(element.readAttribute('data-update_url'), {
233
+ method: 'get',
234
+ parameters: {value: element.getValue()},
235
+ onLoading: function(response) {
236
+ element.next('img.loading-indicator').style.visibility = 'visible';
237
+ as_form.disable();
238
+ },
239
+ onComplete: function(response) {
240
+ element.next('img.loading-indicator').style.visibility = 'hidden';
241
+ as_form.enable();
242
+ },
243
+ onFailure: function(request) {
244
+ var as_div = event.findElement('div.active-scaffold');
245
+ if (as_div) {
246
+ ActiveScaffold.report_500_response(as_div)
247
+ }
248
+ }
249
+ });
250
+ return true;
251
+ });
252
+ document.on('change', 'select.as_search_range_option', function(event) {
253
+ var element = event.findElement();
254
+ Element[element.value == 'BETWEEN' ? 'show' : 'hide'](element.id.sub('_opt', '_between'));
255
+ return true;
256
+ });
257
+ document.on('change', 'select.as_search_date_time_option', function(event) {
258
+ var element = event.findElement();
259
+ Element[!(element.value == 'PAST' || element.value == 'FUTURE' || element.value == 'RANGE') ? 'show' : 'hide'](element.id.sub('_opt', '_numeric'));
260
+ Element[(element.value == 'PAST' || element.value == 'FUTURE') ? 'show' : 'hide'](element.id.sub('_opt', '_trend'));
261
+ Element[element.value == 'RANGE' ? 'show' : 'hide'](element.id.sub('_opt', '_range'));
262
+ return true;
263
+ });
264
+ document.on('change', 'select.as_update_date_operator', function(event) {
265
+ var element = event.findElement();
266
+ Element[element.value == 'REPLACE' ? 'show' : 'hide'](element.next());
267
+ Element[element.value == 'REPLACE' ? 'show' : 'hide'](element.next().next());
268
+ Element[element.value == 'REPLACE' ? 'hide' : 'show'](element.next('span'));
269
+ return true;
270
+ });
271
+ });
272
+
273
+
274
+ /*
275
+ * Simple utility methods
276
+ */
277
+
278
+ var ActiveScaffold = {
279
+ records_for: function(tbody_id) {
280
+ var rows = [];
281
+ var child = $(tbody_id).down('.record');
282
+ while (child) {
283
+ rows.push(child);
284
+ child = child.next('.record');
285
+ }
286
+ return rows;
287
+ },
288
+ stripe: function(tbody_id) {
289
+ var even = false;
290
+ var rows = this.records_for(tbody_id);
291
+ for (var i = 0; i < rows.length; i++) {
292
+ var child = rows[i];
293
+ //Make sure to skip rows that are create or edit rows or messages
294
+ if (child.tagName != 'SCRIPT'
295
+ && !child.hasClassName("create")
296
+ && !child.hasClassName("update")
297
+ && !child.hasClassName("inline-adapter")
298
+ && !child.hasClassName("active-scaffold-calculations")) {
299
+
300
+ if (even) child.addClassName("even-record");
301
+ else child.removeClassName("even-record");
302
+
303
+ even = !even;
304
+ }
305
+ }
306
+ },
307
+ hide_empty_message: function(tbody) {
308
+ if (this.records_for(tbody).length != 0) {
309
+ var empty_message_nodes = $(tbody).up().select('tbody.messages p.empty-message')
310
+ empty_message_nodes.invoke('hide');
311
+ }
312
+ },
313
+ reload_if_empty: function(tbody, url) {
314
+ if (this.records_for(tbody).length == 0) {
315
+ new Ajax.Request(url, {
316
+ method: 'get',
317
+ asynchronous: true,
318
+ evalScripts: true
319
+ });
320
+ }
321
+ },
322
+ removeSortClasses: function(scaffold) {
323
+ scaffold = $(scaffold)
324
+ scaffold.select('td.sorted').each(function(element) {
325
+ element.removeClassName("sorted");
326
+ });
327
+ scaffold.select('th.sorted').each(function(element) {
328
+ element.removeClassName("sorted");
329
+ element.removeClassName("asc");
330
+ element.removeClassName("desc");
331
+ });
332
+ },
333
+ decrement_record_count: function(scaffold) {
334
+ // decrement the last record count, firsts record count are in nested lists
335
+ scaffold = $(scaffold)
336
+ count = scaffold.select('span.active-scaffold-records').last();
337
+ if (count) count.update(parseInt(count.innerHTML, 10) - 1);
338
+ },
339
+ increment_record_count: function(scaffold) {
340
+ // increment the last record count, firsts record count are in nested lists
341
+ scaffold = $(scaffold)
342
+ count = scaffold.select('span.active-scaffold-records').last();
343
+ if (count) count.update(parseInt(count.innerHTML, 10) + 1);
344
+ },
345
+ update_row: function(row, html) {
346
+ row = $(row);
347
+ var new_row = this.replace(row, html)
348
+ if (row.hasClassName('even-record')) new_row.addClassName('even-record');
349
+ new_row.highlight();
350
+ },
351
+
352
+ replace: function(element, html) {
353
+ element = $(element)
354
+ Element.replace(element, html);
355
+ element = $(element.id);
356
+ return element;
357
+ },
358
+
359
+ replace_html: function(element, html) {
360
+ element = $(element);
361
+ element.update(html);
362
+ return element;
363
+ },
364
+
365
+ remove: function(element) {
366
+ $(element).remove();
367
+ },
368
+
369
+ hide: function(element) {
370
+ $(element).hide();
371
+ },
372
+
373
+ show: function(element) {
374
+ $(element).show();
375
+ },
376
+
377
+ reset_form: function(element) {
378
+ $(element).reset();
379
+ },
380
+
381
+ disable_form: function(as_form) {
382
+ as_form = $(as_form)
383
+ var loading_indicator = $(as_form.id.sub('-form', '-loading-indicator'));
384
+ if (loading_indicator) loading_indicator.style.visibility = 'visible';
385
+ as_form.disable();
386
+ },
387
+
388
+ enable_form: function(as_form) {
389
+ as_form = $(as_form)
390
+ var loading_indicator = $(as_form.id.sub('-form', '-loading-indicator'));
391
+ if (loading_indicator) loading_indicator.style.visibility = 'hidden';
392
+ as_form.enable();
393
+ },
394
+
395
+ focus_first_element_of_form: function(form_element) {
396
+ Form.focusFirstElement(form_element);
397
+ },
398
+
399
+ create_record_row: function(tbody, html, options) {
400
+ tbody = $(tbody);
401
+ var new_row = null;
402
+
403
+ if (options.insert_at == 'top') {
404
+ tbody.insert({top: html});
405
+ new_row = tbody.firstDescendant();
406
+ } else if (options.insert_at == 'bottom') {
407
+ var last_row = tbody.childElements().reverse().detect(function(node) { return node.hasClassName('record') || node.hasClassName('inline-adapter')});
408
+ if (last_row) {
409
+ last_row.insert({after: html});
410
+ } else {
411
+ tbody.insert({bottom: html});
412
+ }
413
+ new_row = Selector.findChildElements(tbody, ['tr.record']).last();
414
+ }
415
+
416
+ this.stripe(tbody);
417
+ this.hide_empty_message(tbody);
418
+ this.increment_record_count(tbody.up('div.active-scaffold'));
419
+ new_row.highlight();
420
+ },
421
+
422
+ delete_record_row: function(row, page_reload_url) {
423
+ row = $(row);
424
+ var tbody = row.up('tbody.records');
425
+
426
+ var current_action_node = row.down('td.actions a.disabled');
427
+
428
+ if (current_action_node) {
429
+ var action_link = ActiveScaffold.ActionLink.get(current_action_node);
430
+ if (action_link) {
431
+ action_link.close_previous_adapter();
432
+ }
433
+ }
434
+ row.remove();
435
+ this.stripe(tbody);
436
+ this.decrement_record_count(tbody.up('div.active-scaffold'));
437
+ this.reload_if_empty(tbody, page_reload_url);
438
+ },
439
+
440
+ report_500_response: function(active_scaffold_id) {
441
+ server_error = $(active_scaffold_id).down('td.messages-container p.server-error');
442
+ if (server_error.visible()) {
443
+ server_error.highlight();
444
+ } else {
445
+ server_error.show();
446
+ }
447
+ },
448
+
449
+ find_action_link: function(element) {
450
+ return ActiveScaffold.ActionLink.get($(element).up('.as_adapter'));
451
+ },
452
+
453
+ scroll_to: function(element) {
454
+ $(element).scrollTo();
455
+ },
456
+
457
+ process_checkbox_inplace_edit: function(checkbox, options) {
458
+ var checked = checkbox.readAttribute('checked');
459
+ // checked attribute is nt updated
460
+ if (checked !== 'checked') options['params'] += '&value=1';
461
+ new Ajax.Request(options.url, {
462
+ method: 'post',
463
+ parameters: options['params'],
464
+ onCreate: function(response) {
465
+ checkbox.disable();
466
+ },
467
+ onComplete: function(response) {
468
+ checkbox.enable();
469
+ }
470
+ });
471
+ },
472
+
473
+ read_inplace_edit_heading_attributes: function(column_heading, options) {
474
+ if (column_heading.readAttribute('data-ie_cancel_text')) options.cancelText = column_heading.readAttribute('data-ie_cancel_text');
475
+ if (column_heading.readAttribute('data-ie_loading_text')) options.loadingText = column_heading.readAttribute('data-ie_loading_text');
476
+ if (column_heading.readAttribute('data-ie_saving_text')) options.savingText = column_heading.readAttribute('data-ie_saving_text');
477
+ if (column_heading.readAttribute('data-ie_save_text')) options.okText = column_heading.readAttribute('data-ie_save_text');
478
+ if (column_heading.readAttribute('data-ie_rows')) options.rows = column_heading.readAttribute('data-ie_rows');
479
+ if (column_heading.readAttribute('data-ie_cols')) options.cols = column_heading.readAttribute('data-ie_cols');
480
+ if (column_heading.readAttribute('data-ie_size')) options.size = column_heading.readAttribute('data-ie_size');
481
+ },
482
+
483
+ create_inplace_editor: function(span, options) {
484
+ if (options['params'].length > 0) {
485
+ options['callback'] = new Function('form', 'return Form.serialize(form) + ' + "'&" + options['params'] + "';");
486
+ }
487
+ span.removeClassName('hover');
488
+ span.inplace_edit = new ActiveScaffold.InPlaceEditor(span.id, options.url, options)
489
+ span.inplace_edit.enterEditMode();
490
+ },
491
+
492
+ create_visibility_toggle: function(element, options) {
493
+ var toggable = $(element);
494
+ var toggler = toggable.previous();
495
+ var initial_label = (options.default_visible === true) ? options.hide_label : options.show_label;
496
+
497
+ toggler.insert(' (<a class="visibility-toggle" href="#">' + initial_label + '</a>)');
498
+ toggler.firstDescendant().observe('click', function(event) {
499
+ var element = event.element();
500
+ toggable.toggle();
501
+ element.innerHTML = (toggable.style.display == 'none') ? options.show_label : options.hide_label;
502
+ });
503
+ },
504
+
505
+ create_associated_record_form: function(element, content, options) {
506
+ var element = $(element);
507
+ if (options.singular == false) {
508
+ if (!(options.id && $(options.id))) {
509
+ element.insert(content);
510
+ }
511
+ } else {
512
+ if (current = $$('#' + element.id + ' tr.association-record')[0]) {
513
+ this.replace(current, content);
514
+ } else {
515
+ element.insert({top: content});
516
+ }
517
+ }
518
+ },
519
+
520
+ render_form_field: function(element, content, options) {
521
+ var element = $(element);
522
+ if (options.is_subform == false) {
523
+ this.replace(element.up('dl'), content);
524
+ } else {
525
+ this.replace_html(element, content);
526
+ }
527
+ },
528
+
529
+ record_select_onselect: function(edit_associated_url, active_scaffold_id, id){
530
+ new Ajax.Request(
531
+ edit_associated_url.sub('--ID--', id), {
532
+ asynchronous: true,
533
+ evalScripts: true,
534
+ onFailure: function(){
535
+ ActiveScaffold.report_500_response(active_scaffold_id.to_json)
536
+ }
537
+ }
538
+ );
539
+ }
540
+ }
541
+
542
+ /*
543
+ * DHTML history tie-in
544
+ */
545
+ function addActiveScaffoldPageToHistory(url, active_scaffold_id) {
546
+ if (typeof dhtmlHistory == 'undefined') return; // it may not be loaded
547
+
548
+ var array = url.split('?');
549
+ var qs = new Querystring(array[1]);
550
+ var sort = qs.get('sort')
551
+ var dir = qs.get('sort_direction')
552
+ var page = qs.get('page')
553
+ if (sort || dir || page) dhtmlHistory.add(active_scaffold_id+":"+page+":"+sort+":"+dir, url);
554
+ }
555
+
556
+ /*
557
+ * Add-ons/Patches to Prototype
558
+ */
559
+
560
+ /* patch to support replacing TR/TD/TBODY in Internet Explorer, courtesy of http://dev.rubyonrails.org/ticket/4273 */
561
+ Element.replace = function(element, html) {
562
+ element = $(element);
563
+ if (element.outerHTML) {
564
+ try {
565
+ element.outerHTML = html.stripScripts();
566
+ } catch (e) {
567
+ var tn = element.tagName;
568
+ if(tn=='TBODY' || tn=='TR' || tn=='TD')
569
+ {
570
+ var tempDiv = document.createElement("div");
571
+ tempDiv.innerHTML = '<table id="tempTable" style="display: none">' + html.stripScripts() + '</table>';
572
+ element.parentNode.replaceChild(tempDiv.getElementsByTagName(tn).item(0), element);
573
+ }
574
+ else throw e;
575
+ }
576
+ } else {
577
+ var range = element.ownerDocument.createRange();
578
+ /* patch to fix <form> replaces in Firefox. see http://dev.rubyonrails.org/ticket/8010 */
579
+ range.selectNodeContents(element.parentNode);
580
+ element.parentNode.replaceChild(range.createContextualFragment(html.stripScripts()), element);
581
+ }
582
+ setTimeout(function() {html.evalScripts()}, 10);
583
+ return element;
584
+ };
585
+
586
+ /*
587
+ * URL modification support. Incomplete functionality.
588
+ */
589
+ Object.extend(String.prototype, {
590
+ append_params: function(params) {
591
+ url = this;
592
+ if (url.indexOf('?') == -1) url += '?';
593
+ else if (url.lastIndexOf('&') != url.length) url += '&';
594
+
595
+ url += $H(params).collect(function(item) {
596
+ return item.key + '=' + item.value;
597
+ }).join('&');
598
+
599
+ return url;
600
+ }
601
+ });
602
+
603
+ /*
604
+ * Prototype's implementation was throwing an error instead of false
605
+ */
606
+ Element.Methods.Simulated = {
607
+ hasAttribute: function(element, attribute) {
608
+ var t = Element._attributeTranslations;
609
+ attribute = (t.names && t.names[attribute]) || attribute;
610
+ // Return false if we get an error here
611
+ try {
612
+ return $(element).getAttributeNode(attribute).specified;
613
+ } catch (e) {
614
+ return false;
615
+ }
616
+ }
617
+ };
618
+
619
+ /**
620
+ * A set of links. As a set, they can be controlled such that only one is "open" at a time, etc.
621
+ */
622
+ ActiveScaffold.Actions = new Object();
623
+ ActiveScaffold.Actions.Abstract = Class.create({
624
+ initialize: function(links, target, loading_indicator, options) {
625
+ this.target = $(target);
626
+ this.loading_indicator = $(loading_indicator);
627
+ this.options = options;
628
+ this.links = links.collect(function(link) {
629
+ return this.instantiate_link(link);
630
+ }.bind(this));
631
+ },
632
+
633
+ instantiate_link: function(link) {
634
+ throw 'unimplemented'
635
+ }
636
+ });
637
+
638
+ /**
639
+ * A DataStructures::ActionLink, represented in JavaScript.
640
+ * Concerned with AJAX-enabling a link and adapting the result for insertion into the table.
641
+ */
642
+ ActiveScaffold.ActionLink = {
643
+ get: function(element) {
644
+ var element = $(element);
645
+ if (typeof(element.retrieve('action_link')) === 'undefined' && !element.hasClassName('as_adapter')) {
646
+ var parent = element.up();
647
+ if (parent && parent.nodeName.toUpperCase() == 'TD') {
648
+ // record action
649
+ parent = parent.up('tr.record')
650
+ new ActiveScaffold.Actions.Record(parent.select('a.as_action'), parent, parent.down('td.actions .loading-indicator'));
651
+ } else if (parent && parent.nodeName.toUpperCase() == 'DIV') {
652
+ //table action
653
+ new ActiveScaffold.Actions.Table(parent.select('a.as_action'), parent.up('div.active-scaffold').down('tbody.before-header'), parent.down('.loading-indicator'));
654
+ }
655
+ element = $(element);
656
+ }
657
+ return element.retrieve('action_link');
658
+ }
659
+ };
660
+
661
+ ActiveScaffold.ActionLink.Abstract = Class.create({
662
+ initialize: function(a, target, loading_indicator) {
663
+ this.tag = $(a);
664
+ this.url = this.tag.href;
665
+ this.method = 'get';
666
+
667
+ if(this.url.match('_method=delete')){
668
+ this.method = 'delete';
669
+ // action delete is special case cause in ajax world it will be destroy
670
+ } else if(this.url.match('/delete')){
671
+ this.url = this.url.replace('/delete', '');
672
+ this.tag.href = this.url;
673
+ this.method = 'delete';
674
+ } else if(this.url.match('_method=post')){
675
+ this.method = 'post';
676
+ } else if(this.url.match('_method=put')){
677
+ this.method = 'put';
678
+ }
679
+ if (this.method != 'get') this.tag.writeAttribute('data-method', this.method);
680
+ this.target = target;
681
+ this.loading_indicator = loading_indicator;
682
+ this.hide_target = false;
683
+ this.position = this.tag.getAttribute('data-position');
684
+
685
+ this.tag.store('action_link', this);
686
+ },
687
+
688
+ open: function(event) {
689
+ },
690
+
691
+ insert: function(content) {
692
+ throw 'unimplemented'
693
+ },
694
+
695
+ close: function() {
696
+ this.enable();
697
+ this.adapter.remove();
698
+ if (this.hide_target) this.target.show();
699
+ },
700
+
701
+ reload: function() {
702
+ this.close();
703
+ this.open();
704
+ },
705
+
706
+ get_new_adapter_id: function() {
707
+ var id = 'adapter_';
708
+ var i = 0;
709
+ while ($(id + i)) i++;
710
+ return id + i;
711
+ },
712
+
713
+ enable: function() {
714
+ return this.tag.removeClassName('disabled');
715
+ },
716
+
717
+ disable: function() {
718
+ return this.tag.addClassName('disabled');
719
+ },
720
+
721
+ is_disabled: function() {
722
+ return this.tag.hasClassName('disabled');
723
+ },
724
+
725
+ scaffold_id: function() {
726
+ return this.tag.up('div.active-scaffold').id;
727
+ },
728
+
729
+ update_flash_messages: function(messages) {
730
+ message_node = $(this.scaffold_id().sub('-active-scaffold', '-messages'));
731
+ if (message_node) message_node.update(messages);
732
+ },
733
+
734
+ set_adapter: function(element) {
735
+ this.adapter = element;
736
+ this.adapter.addClassName('as_adapter');
737
+ this.adapter.store('action_link', this);
738
+ }
739
+ });
740
+
741
+ /**
742
+ * Concrete classes for record actions
743
+ */
744
+ ActiveScaffold.Actions.Record = Class.create(ActiveScaffold.Actions.Abstract, {
745
+ instantiate_link: function(link) {
746
+ var l = new ActiveScaffold.ActionLink.Record(link, this.target, this.loading_indicator);
747
+ if (!this.target.readAttribute('data-refresh').blank()) l.refresh_url = this.target.readAttribute('data-refresh');
748
+
749
+ if (link.hasClassName('delete')) {
750
+ l.url = l.url.replace(/\/delete(\?.*)?$/, '$1');
751
+ l.url = l.url.replace(/\/delete\/(.*)/, '/destroy/$1');
752
+ l.tag.href = l.url;
753
+ }
754
+ if (l.position) {
755
+ l.url = l.url.append_params({adapter: '_list_inline_adapter'});
756
+ l.tag.href = l.url;
757
+ }
758
+ l.set = this;
759
+ return l;
760
+ }
761
+ });
762
+
763
+ ActiveScaffold.ActionLink.Record = Class.create(ActiveScaffold.ActionLink.Abstract, {
764
+ close_previous_adapter: function() {
765
+ this.set.links.each(function(item) {
766
+ if (item.url != this.url && item.is_disabled() && item.adapter) {
767
+ item.enable();
768
+ item.adapter.remove();
769
+ }
770
+ }.bind(this));
771
+ },
772
+
773
+ insert: function(content) {
774
+ this.close_previous_adapter();
775
+
776
+ if (this.position == 'replace') {
777
+ this.position = 'after';
778
+ this.hide_target = true;
779
+ }
780
+
781
+ if (this.position == 'after') {
782
+ this.target.insert({after:content});
783
+ this.set_adapter(this.target.next());
784
+ }
785
+ else if (this.position == 'before') {
786
+ this.target.insert({before:content});
787
+ this.set_adapter(this.target.previous());
788
+ }
789
+ else {
790
+ return false;
791
+ }
792
+ this.adapter.down('td').down().highlight();
793
+ },
794
+
795
+ close: function($super, refreshed_content) {
796
+ if (refreshed_content) {
797
+ ActiveScaffold.update_row(this.target, refreshed_content);
798
+ }
799
+ $super();
800
+ },
801
+
802
+ enable: function() {
803
+ this.set.links.each(function(item) {
804
+ if (item.url != this.url) return;
805
+ item.tag.removeClassName('disabled');
806
+ }.bind(this));
807
+ },
808
+
809
+ disable: function() {
810
+ this.set.links.each(function(item) {
811
+ if (item.url != this.url) return;
812
+ item.tag.addClassName('disabled');
813
+ }.bind(this));
814
+ },
815
+
816
+ set_opened: function() {
817
+ if (this.position == 'after') {
818
+ this.set_adapter(this.target.next());
819
+ }
820
+ else if (this.position == 'before') {
821
+ this.set_adapter(this.target.previous());
822
+ }
823
+ this.disable();
824
+ }
825
+ });
826
+
827
+ /**
828
+ * Concrete classes for table actions
829
+ */
830
+ ActiveScaffold.Actions.Table = Class.create(ActiveScaffold.Actions.Abstract, {
831
+ instantiate_link: function(link) {
832
+ var l = new ActiveScaffold.ActionLink.Table(link, this.target, this.loading_indicator);
833
+ if (l.position) {
834
+ l.url = l.url.append_params({adapter: '_list_inline_adapter'});
835
+ l.tag.href = l.url;
836
+ }
837
+ return l;
838
+ }
839
+ });
840
+
841
+ ActiveScaffold.ActionLink.Table = Class.create(ActiveScaffold.ActionLink.Abstract, {
842
+ insert: function(content) {
843
+ if (this.position == 'top') {
844
+ this.target.insert({top:content});
845
+ this.set_adapter(this.target.immediateDescendants().first());
846
+ }
847
+ else {
848
+ throw 'Unknown position "' + this.position + '"'
849
+ }
850
+ this.adapter.down('td').down().highlight();
851
+ }
852
+ });
853
+
854
+ if (Ajax.InPlaceEditor) {
855
+ ActiveScaffold.InPlaceEditor = Class.create(Ajax.InPlaceEditor, {
856
+ initialize: function($super, element, url, options) {
857
+ $super(element, url, options);
858
+ if (this._originalBackground == 'transparent') {
859
+ this._originalBackground = null;
860
+ }
861
+ },
862
+
863
+ setFieldFromAjax: function(url, options) {
864
+ var ipe = this;
865
+ $(ipe._controls.editor).remove();
866
+ new Ajax.Request(url, {
867
+ method: 'get',
868
+ onComplete: function(response) {
869
+ ipe._form.insert({top: response.responseText});
870
+ if (options.plural) {
871
+ ipe._form.getElements().each(function(el) {
872
+ if (el.type != "submit" && el.type != "image") {
873
+ el.name = ipe.options.paramName + '[]';
874
+ el.className = 'editor_field';
875
+ }
876
+ });
877
+ } else {
878
+ var fld = ipe._form.findFirstElement();
879
+ fld.name = ipe.options.paramName;
880
+ fld.className = 'editor_field';
881
+ if (ipe.options.submitOnBlur)
882
+ fld.onblur = ipe._boundSubmitHandler;
883
+ ipe._controls.editor = fld;
884
+ }
885
+ }
886
+ });
887
+ },
888
+
889
+ clonePatternField: function() {
890
+ var patternNodes = this.getPatternNodes(this.options.inplacePatternSelector);
891
+ if (patternNodes.editNode == null) {
892
+ alert('did not find any matching node for ' + this.options.editFieldSelector);
893
+ return;
894
+ }
895
+
896
+ var fld = patternNodes.editNode.cloneNode(true);
897
+ if (fld.id.length > 0) fld.id += this.options.nodeIdSuffix;
898
+ fld.name = this.options.paramName;
899
+ fld.className = 'editor_field';
900
+ this.setValue(fld, this._controls.editor.value);
901
+ if (this.options.submitOnBlur)
902
+ fld.onblur = this._boundSubmitHandler;
903
+ $(this._controls.editor).remove();
904
+ this._controls.editor = fld;
905
+ this._form.appendChild(this._controls.editor);
906
+
907
+ $A(patternNodes.additionalNodes).each(function(node) {
908
+ var patternNode = node.cloneNode(true);
909
+ if (patternNode.id.length > 0) {
910
+ patternNode.id = patternNode.id + this.options.nodeIdSuffix;
911
+ }
912
+ this._form.appendChild(patternNode);
913
+ }.bind(this));
914
+ },
915
+
916
+ getPatternNodes: function(inplacePatternSelector) {
917
+ var nodes = {editNode: null, additionalNodes: []};
918
+ var selectedNodes = $$(inplacePatternSelector);
919
+ var firstNode = selectedNodes.first();
920
+
921
+ if (typeof(firstNode) !== 'undefined') {
922
+ // AS inplace_edit_control_container -> we have to select all child nodes
923
+ // Workaround for ie which does not support css > selector
924
+ if (firstNode.className.indexOf('as_inplace_pattern') !== -1) {
925
+ selectedNodes = firstNode.childElements();
926
+ }
927
+ nodes.editNode = selectedNodes.first();
928
+ selectedNodes.shift();
929
+ nodes.additionalNodes = selectedNodes;
930
+ }
931
+ return nodes;
932
+ },
933
+
934
+ setValue: function(editField, textValue) {
935
+ var function_name = 'setValueFor' + editField.nodeName.toLowerCase();
936
+ if (typeof(this[function_name]) == 'function') {
937
+ this[function_name](editField, textValue);
938
+ } else {
939
+ editField.value = textValue;
940
+ }
941
+ },
942
+
943
+ setValueForselect: function(editField, textValue) {
944
+ var len = editField.options.length;
945
+ var i = 0;
946
+ while (i < len && editField.options[i].text != textValue) {
947
+ i++;
948
+ }
949
+ if (i < len) {
950
+ editField.value = editField.options[i].value
951
+ }
952
+ }
953
+ });
954
+ }