mizugumo 0.1.4 → 0.1.5

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