Initial Commit from Base app
[base_ror3_portal.git] / public / javascripts / rails.js
blob776588addc1e5df5f4cf3f3cbd44b205354e395b
1 jQuery(function ($) {\r
2     var csrf_token = $('meta[name=csrf-token]').attr('content'),\r
3         csrf_param = $('meta[name=csrf-param]').attr('content');\r
4 \r
5     $.fn.extend({\r
6         /**\r
7          * Triggers a custom event on an element and returns the event result\r
8          * this is used to get around not being able to ensure callbacks are placed\r
9          * at the end of the chain.\r
10          *\r
11          * TODO: deprecate with jQuery 1.4.2 release, in favor of subscribing to our\r
12          *       own events and placing ourselves at the end of the chain.\r
13          */\r
14         triggerAndReturn: function (name, data) {\r
15             var event = new $.Event(name);\r
16             this.trigger(event, data);\r
18             return event.result !== false;\r
19         },\r
21         /**\r
22          * Handles execution of remote calls firing overridable events along the way\r
23          */\r
24         callRemote: function () {\r
25             var el      = this,\r
26                 method  = el.attr('method') || el.attr('data-method') || 'GET',\r
27                 url     = el.attr('action') || el.attr('href'),\r
28                 dataType  = el.attr('data-type')  || 'script';\r
30             if (url === undefined) {\r
31               throw "No URL specified for remote call (action or href must be present).";\r
32             } else {\r
33                 if (el.triggerAndReturn('ajax:before')) {\r
34                     var data = el.is('form') ? el.serializeArray() : [];\r
35                     $.ajax({\r
36                         url: url,\r
37                         data: data,\r
38                         dataType: dataType,\r
39                         type: method.toUpperCase(),\r
40                         beforeSend: function (xhr) {\r
41                             el.trigger('ajax:loading', xhr);\r
42                         },\r
43                         success: function (data, status, xhr) {\r
44                             el.trigger('ajax:success', [data, status, xhr]);\r
45                         },\r
46                         complete: function (xhr) {\r
47                             el.trigger('ajax:complete', xhr);\r
48                         },\r
49                         error: function (xhr, status, error) {\r
50                             el.trigger('ajax:failure', [xhr, status, error]);\r
51                         }\r
52                     });\r
53                 }\r
55                 el.trigger('ajax:after');\r
56             }\r
57         }\r
58     });\r
60     /**\r
61      *  confirmation handler\r
62      */\r
63     $('a[data-confirm],input[data-confirm]').live('click', function () {\r
64         var el = $(this);\r
65         if (el.triggerAndReturn('confirm')) {\r
66             if (!confirm(el.attr('data-confirm'))) {\r
67                 return false;\r
68             }\r
69         }\r
70     });\r
73     /**\r
74      * remote handlers\r
75      */\r
76     $('form[data-remote]').live('submit', function (e) {\r
77         $(this).callRemote();\r
78         e.preventDefault();\r
79     });\r
81     $('a[data-remote],input[data-remote]').live('click', function (e) {\r
82         $(this).callRemote();\r
83         e.preventDefault();\r
84     });\r
86     $('a[data-method]:not([data-remote])').live('click', function (e){\r
87         var link = $(this),\r
88             href = link.attr('href'),\r
89             method = link.attr('data-method'),\r
90             form = $('<form method="post" action="'+href+'"></form>'),\r
91             metadata_input = '<input name="_method" value="'+method+'" type="hidden" />';\r
93         if (csrf_param != null && csrf_token != null) {\r
94           metadata_input += '<input name="'+csrf_param+'" value="'+csrf_token+'" type="hidden" />';\r
95         }\r
97         form.hide()\r
98             .append(metadata_input)\r
99             .appendTo('body');\r
101         e.preventDefault();\r
102         form.submit();\r
103     });\r
105     /**\r
106      * disable-with handlers\r
107      */\r
108     var disable_with_input_selector           = 'input[data-disable-with]';\r
109     var disable_with_form_remote_selector     = 'form[data-remote]:has('       + disable_with_input_selector + ')';\r
110     var disable_with_form_not_remote_selector = 'form:not([data-remote]):has(' + disable_with_input_selector + ')';\r
112     var disable_with_input_function = function () {\r
113         $(this).find(disable_with_input_selector).each(function () {\r
114             var input = $(this);\r
115             input.data('enable-with', input.val())\r
116                 .attr('value', input.attr('data-disable-with'))\r
117                 .attr('disabled', 'disabled');\r
118         });\r
119     };\r
121     $(disable_with_form_remote_selector).live('ajax:before', disable_with_input_function);\r
122     $(disable_with_form_not_remote_selector).live('submit', disable_with_input_function);\r
124     $(disable_with_form_remote_selector).live('ajax:complete', function () {\r
125         $(this).find(disable_with_input_selector).each(function () {\r
126             var input = $(this);\r
127             input.removeAttr('disabled')\r
128                  .val(input.data('enable-with'));\r
129         });\r
130     });\r
132 });\r