rails-ajax 0.2.0.20130731 → 0.3.0.20130913
Sign up to get free protection for your applications and to get access to all the features.
- data/AUTHORS.rdoc +2 -0
- data/ChangeLog +5 -0
- data/ReleaseInfo +1 -1
- data/assets/javascripts/jquery.rails-ajax.js.erb +300 -300
- data/gen/config/initializers/rails-ajax-config.rb +1 -0
- data/lib/rails-ajax/configuration.rb +1 -1
- data/lib/rails-ajax/controller.rb +4 -2
- data/lib/rails-ajax/form_tag_helper.rb +3 -3
- data/lib/rails-ajax/rails-ajax.rb +12 -0
- data/lib/rails-ajax/url_helper.rb +7 -4
- metadata +2 -2
data/AUTHORS.rdoc
CHANGED
data/ChangeLog
CHANGED
data/ReleaseInfo
CHANGED
@@ -1,300 +1,300 @@
|
|
1
|
-
//--
|
2
|
-
// Copyright (c) 2012 Muriel Salvan (muriel@x-aeon.com)
|
3
|
-
// Licensed under the terms specified in LICENSE file. No warranty is provided.
|
4
|
-
//++
|
5
|
-
|
6
|
-
<% if (RailsAjax.config.Enabled) %>
|
7
|
-
|
8
|
-
//=========================================
|
9
|
-
// Browser detection code (removed from jQuery >=1.9):
|
10
|
-
// Can be removed along with setFavicon as soon as Firefox corrects bug: https://bugzilla.mozilla.org/show_bug.cgi?id=519028
|
11
|
-
var matched, browser;
|
12
|
-
|
13
|
-
// Use of jQuery.browser is frowned upon.
|
14
|
-
// More details: http://api.jquery.com/jQuery.browser
|
15
|
-
// jQuery.uaMatch maintained for back-compat
|
16
|
-
jQuery.uaMatch = function( ua ) {
|
17
|
-
ua = ua.toLowerCase();
|
18
|
-
|
19
|
-
var match = /(chrome)[ \/]([\w.]+)/.exec( ua ) ||
|
20
|
-
/(webkit)[ \/]([\w.]+)/.exec( ua ) ||
|
21
|
-
/(opera)(?:.*version|)[ \/]([\w.]+)/.exec( ua ) ||
|
22
|
-
/(msie) ([\w.]+)/.exec( ua ) ||
|
23
|
-
ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec( ua ) ||
|
24
|
-
[];
|
25
|
-
|
26
|
-
return {
|
27
|
-
browser: match[ 1 ] || "",
|
28
|
-
version: match[ 2 ] || "0"
|
29
|
-
};
|
30
|
-
};
|
31
|
-
|
32
|
-
matched = jQuery.uaMatch( navigator.userAgent );
|
33
|
-
browser = {};
|
34
|
-
|
35
|
-
if ( matched.browser ) {
|
36
|
-
browser[ matched.browser ] = true;
|
37
|
-
browser.version = matched.version;
|
38
|
-
}
|
39
|
-
|
40
|
-
// Chrome is Webkit, but Webkit is also Safari.
|
41
|
-
if ( browser.chrome ) {
|
42
|
-
browser.webkit = true;
|
43
|
-
} else if ( browser.webkit ) {
|
44
|
-
browser.safari = true;
|
45
|
-
}
|
46
|
-
|
47
|
-
jQuery.browser = browser;
|
48
|
-
//===========================================
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
// Encapsulate everything in a nice namespace
|
53
|
-
var railsAjax = {
|
54
|
-
|
55
|
-
// Initialization, to be called just once when the DOM is ready
|
56
|
-
init: function() {
|
57
|
-
try {
|
58
|
-
// Register bindings to every Rails remote element
|
59
|
-
railsAjax.attachAjaxToElements('*[data-remote=true]');
|
60
|
-
var lRootURL = History.getRootUrl();
|
61
|
-
jQuery(window).bind('statechange', function() {
|
62
|
-
try {
|
63
|
-
var lState = History.getState();
|
64
|
-
<%= 'alert(\'[RailsAjax] History statechange triggered #\' + lState.data.id + \' via \' + lState.data.type + \' to \' + lState.url.replace(lRootURL, \'/\') + \' (anchor: \' + lState.data.anchor + \')\');' if (RailsAjax.config.DebugAlerts) %>
|
65
|
-
jQuery.ajax({
|
66
|
-
// TODO (History.js): Remove the anchor attribute when History.js handles anchors correctly.
|
67
|
-
anchor: lState.data.anchor,
|
68
|
-
// Get back all parameters set by Rails' jquery-ujs
|
69
|
-
type: lState.data.type,
|
70
|
-
data: lState.data.data,
|
71
|
-
dataType: lState.data.dataType,
|
72
|
-
crossDomain: lState.data.crossDomain,
|
73
|
-
// Set the URL to query
|
74
|
-
url: lState.url.replace(lRootURL, '/'),
|
75
|
-
// And the callbacks
|
76
|
-
// TODO (History.js): Remove the beforeSend callback when anchors will be handled correctly by History.js
|
77
|
-
beforeSend: function(ioXHR, iSettings) {
|
78
|
-
// Remember the anchor used
|
79
|
-
ioXHR.anchor = iSettings.anchor;
|
80
|
-
},
|
81
|
-
success: function(iData, iStatus, iXHR) {
|
82
|
-
railsAjax.ajaxSuccess(iXHR, iData);
|
83
|
-
},
|
84
|
-
error: function(iXHR, iStatus, iError) {
|
85
|
-
railsAjax.ajaxError(iXHR, iError);
|
86
|
-
}
|
87
|
-
});
|
88
|
-
} catch (iError) {
|
89
|
-
alert('[RailsAjax] Exception in statechange callback: ' + iError.name + ' - ' + iError.message);
|
90
|
-
}
|
91
|
-
});
|
92
|
-
} catch (iError) {
|
93
|
-
alert('[RailsAjax] Exception during railsAjax initialization: ' + iError.name + ' - ' + iError.message);
|
94
|
-
}
|
95
|
-
},
|
96
|
-
|
97
|
-
// Attach Ajax delegations to elements needing them
|
98
|
-
//
|
99
|
-
// Parameters::
|
100
|
-
// * *iCSSSelector* (_String_): CSS selector of elements needing attachment
|
101
|
-
attachAjaxToElements: function(iCSSSelector) {
|
102
|
-
try {
|
103
|
-
<%= 'alert(\'[RailsAjax] Attach Ajax bindings to elements \' + iCSSSelector + \' (\' + jQuery(iCSSSelector).length + \' elements)\');' if (RailsAjax.config.DebugAlerts) %>
|
104
|
-
jQuery(document)
|
105
|
-
.delegate(iCSSSelector, 'ajax:beforeSend', function(iEvent, ioXHR, iSettings) {
|
106
|
-
try {
|
107
|
-
var lThis = jQuery(this);
|
108
|
-
var lURL = ((lThis.is('a')) ? lThis.attr('href') : lThis.attr('action'));
|
109
|
-
<%= 'alert(\'[RailsAjax] Ajax before send. URL=\' + lURL + \' Method=\' + iSettings.type + \' Data=\' + iSettings.data);' if (RailsAjax.config.DebugAlerts) %>
|
110
|
-
var lContinue = true;
|
111
|
-
if (railsAjax.beforeSend != undefined) {
|
112
|
-
try {
|
113
|
-
lContinue = (railsAjax.beforeSend(ioXHR, iSettings) != false);
|
114
|
-
} catch (iUserError) {
|
115
|
-
alert('[RailsAjax] Exception in user callback railsAjax.beforeSend: ' + iUserError.name + ' - ' + iUserError.message);
|
116
|
-
}
|
117
|
-
}
|
118
|
-
<%= 'alert(\'[RailsAjax] Continue on the Ajax load ? \' + lContinue);' if (RailsAjax.config.DebugAlerts) %>
|
119
|
-
if (lContinue) {
|
120
|
-
railsAjax.pushNewState(lURL,
|
121
|
-
{
|
122
|
-
// Replicate the whole Rails' jquery-ujs parameters, as we will need to pass them to the new ajax options object created during onstatechange event.
|
123
|
-
type: iSettings.type,
|
124
|
-
data: iSettings.data,
|
125
|
-
dataType: iSettings.dataType,
|
126
|
-
crossDomain: iSettings.crossDomain
|
127
|
-
},
|
128
|
-
false
|
129
|
-
);
|
130
|
-
}
|
131
|
-
// Cancel AJAX processing right now.
|
132
|
-
// It will be taken care of by the changestate event.
|
133
|
-
return false;
|
134
|
-
} catch (iError) {
|
135
|
-
alert('[RailsAjax] Exception while ajax:beforeSend: ' + iError.name + ' - ' + iError.message);
|
136
|
-
}
|
137
|
-
})
|
138
|
-
} catch (iError) {
|
139
|
-
alert('[RailsAjax] Exception while binding Ajax events to elements ' + iCSSSelector + ': ' + iError.name + ' - ' + iError.message);
|
140
|
-
}
|
141
|
-
},
|
142
|
-
|
143
|
-
// Callback called when Ajax call is successfull
|
144
|
-
//
|
145
|
-
// Parameters::
|
146
|
-
// * *iXHR* (_Object_): The XHR object responsible for the Ajax call
|
147
|
-
// * *iData* (_String_): The received data
|
148
|
-
ajaxSuccess: function(iXHR, iData) {
|
149
|
-
try {
|
150
|
-
<%= 'alert(\'[RailsAjax] Ajax success. Loading data: \' + iData);' if (RailsAjax.config.DebugAlerts) %>
|
151
|
-
|
152
|
-
// 1. Change the window's title if needed
|
153
|
-
if ('page_title' in iData) {
|
154
|
-
document.title = iData.page_title;
|
155
|
-
}
|
156
|
-
|
157
|
-
// 2. Replace HTML data, and execute scripts in the same time
|
158
|
-
jQuery.each(iData.div_contents, function(iCSSSelector, iContent) {
|
159
|
-
// This replaces HTML content and also executes scripts (even document.ready ones)
|
160
|
-
jQuery(iCSSSelector).html(iContent);
|
161
|
-
});
|
162
|
-
|
163
|
-
// 3. Scroll to anchor if needed
|
164
|
-
if (iXHR.anchor) {
|
165
|
-
railsAjax.scrollToAnchor(iXHR.anchor);
|
166
|
-
}
|
167
|
-
|
168
|
-
// 4. For Firefox only, workaround a bug by setting the Favicon
|
169
|
-
if (jQuery.browser.mozilla) {
|
170
|
-
railsAjax.setFavicon();
|
171
|
-
}
|
172
|
-
|
173
|
-
// 5. Execute scripts that were added
|
174
|
-
if ('js_to_execute' in iData) {
|
175
|
-
jQuery.each(iData.js_to_execute, function(iIdx, iJS) {
|
176
|
-
<%= 'alert(\'[RailsAjax] Execute script #\' + iIdx + \':\\n\' + iJS);' if (RailsAjax.config.DebugAlerts) %>
|
177
|
-
try {
|
178
|
-
eval(iJS);
|
179
|
-
} catch (iError) {
|
180
|
-
alert('[RailsAjax] Exception while executing Ajax script: ' + iError.name + ' - ' + iError.message + '\nScript:\n' + iJS);
|
181
|
-
}
|
182
|
-
});
|
183
|
-
}
|
184
|
-
|
185
|
-
// 6. Call user callbacks
|
186
|
-
if (railsAjax.success != undefined) {
|
187
|
-
try {
|
188
|
-
railsAjax.success(iXHR, iData);
|
189
|
-
} catch (iUserError) {
|
190
|
-
alert('[RailsAjax] Exception in user callback railsAjax.success: ' + iUserError.name + ' - ' + iUserError.message);
|
191
|
-
}
|
192
|
-
}
|
193
|
-
if (railsAjax.complete != undefined) {
|
194
|
-
try {
|
195
|
-
railsAjax.complete(iXHR);
|
196
|
-
} catch (iUserError) {
|
197
|
-
alert('[RailsAjax] Exception in user callback railsAjax.complete: ' + iUserError.name + ' - ' + iUserError.message);
|
198
|
-
}
|
199
|
-
}
|
200
|
-
|
201
|
-
// 7. Handle redirects
|
202
|
-
if ('redirect_to' in iData) {
|
203
|
-
railsAjax.pushNewState(iData.redirect_to, {}, true);
|
204
|
-
}
|
205
|
-
|
206
|
-
} catch (iError) {
|
207
|
-
alert('[RailsAjax] Exception while Ajax success callback: ' + iError.name + ' - ' + iError.message);
|
208
|
-
}
|
209
|
-
},
|
210
|
-
|
211
|
-
// Callback called when Ajax call has failed
|
212
|
-
//
|
213
|
-
// Parameters::
|
214
|
-
// * *iXHR* (_Object_): The xhr object
|
215
|
-
// * *iError* (_Object_): The error
|
216
|
-
ajaxError: function(iXHR, iError) {
|
217
|
-
if (railsAjax.error != undefined) {
|
218
|
-
try {
|
219
|
-
railsAjax.error(iXHR, iError);
|
220
|
-
} catch (iUserError) {
|
221
|
-
alert('[RailsAjax] Exception in user callback railsAjax.error: ' + iUserError.name + ' - ' + iUserError.message);
|
222
|
-
}
|
223
|
-
}
|
224
|
-
if (railsAjax.complete != undefined) {
|
225
|
-
try {
|
226
|
-
railsAjax.complete(iXHR);
|
227
|
-
} catch (iUserError) {
|
228
|
-
alert('[RailsAjax] Exception in user callback railsAjax.complete: ' + iUserError.name + ' - ' + iUserError.message);
|
229
|
-
}
|
230
|
-
}
|
231
|
-
},
|
232
|
-
|
233
|
-
// Used to identify states in debugging logs
|
234
|
-
<%= 'gStateID: 0,' if (RailsAjax.config.DebugAlerts) %>
|
235
|
-
|
236
|
-
// Push a new History state
|
237
|
-
//
|
238
|
-
// Parameters::
|
239
|
-
// * *iURL* (_String_): The URL to be pushed
|
240
|
-
// * *iData* (_Object_): The state object to store with it
|
241
|
-
// * *iReplaceState* (_Boolean_): Do we replace the current state ?
|
242
|
-
pushNewState: function(iURL, iData, iReplaceState) {
|
243
|
-
try {
|
244
|
-
// Remove the hash if it is present, as otherwise anchorchange is triggered and statechange ignored.
|
245
|
-
// We don't want to handle anchorchange callback as it is unable to get the real History state correctly.
|
246
|
-
// TODO (History.js): Correct bug https://github.com/balupton/history.js/issues/42 and https://github.com/balupton/history.js/issues/173, then remove this special hash and anchor treatment
|
247
|
-
var lSplittedURL = iURL.split('#');
|
248
|
-
var lState = jQuery.extend(
|
249
|
-
{
|
250
|
-
// Used to identify states in debugging logs
|
251
|
-
<%= 'id: railsAjax.gStateID,' if (RailsAjax.config.DebugAlerts) %>
|
252
|
-
anchor: lSplittedURL[1]
|
253
|
-
},
|
254
|
-
iData
|
255
|
-
);
|
256
|
-
if (iReplaceState) {
|
257
|
-
History.replaceState(lState, null, lSplittedURL[0]);
|
258
|
-
} else {
|
259
|
-
History.pushState(lState, null, lSplittedURL[0]);
|
260
|
-
}
|
261
|
-
<%= 'railsAjax.gStateID = railsAjax.gStateID + 1;' if (RailsAjax.config.DebugAlerts) %>
|
262
|
-
} catch (iError) {
|
263
|
-
alert('[RailsAjax] Exception in pushNewState: ' + iError.name + ' - ' + iError.message);
|
264
|
-
}
|
265
|
-
},
|
266
|
-
|
267
|
-
// Scroll the web page to display a given anchor.
|
268
|
-
// An anchor is either a div id, or a named "a"
|
269
|
-
//
|
270
|
-
// Parameters::
|
271
|
-
// * *iAnchor* (_String_): Anchor name
|
272
|
-
scrollToAnchor: function(iAnchor) {
|
273
|
-
var lDivElement = jQuery('div#' + iAnchor)[0]
|
274
|
-
if (lDivElement != undefined) {
|
275
|
-
lDivElement.scrollIntoView(true);
|
276
|
-
} else {
|
277
|
-
var lLstElements = window.document.getElementsByName(iAnchor);
|
278
|
-
if (lLstElements.length > 0) {
|
279
|
-
lLstElements[0].scrollIntoView(true);
|
280
|
-
} else {
|
281
|
-
alert('[RailsAjax] No anchor named ' + iAnchor);
|
282
|
-
}
|
283
|
-
}
|
284
|
-
},
|
285
|
-
|
286
|
-
// Reload the favicon.
|
287
|
-
// This is needed to workaround a Firefox bug: http://kilianvalkhof.com/2010/javascript/the-case-of-the-disappearing-favicon/
|
288
|
-
setFavicon: function() {
|
289
|
-
var link = jQuery('link[type="image/x-icon"]').remove().attr('href');
|
290
|
-
jQuery('<link href="'+ link +'" rel="shortcut icon" type="image/x-icon" />').appendTo('head');
|
291
|
-
}
|
292
|
-
|
293
|
-
};
|
294
|
-
|
295
|
-
// Initialize everything once the DOM is loaded and ready
|
296
|
-
jQuery(document).ready(function(){
|
297
|
-
railsAjax.init();
|
298
|
-
});
|
299
|
-
|
300
|
-
<% end %>
|
1
|
+
//--
|
2
|
+
// Copyright (c) 2012 Muriel Salvan (muriel@x-aeon.com)
|
3
|
+
// Licensed under the terms specified in LICENSE file. No warranty is provided.
|
4
|
+
//++
|
5
|
+
|
6
|
+
<% if (RailsAjax.config.Enabled) %>
|
7
|
+
|
8
|
+
//=========================================
|
9
|
+
// Browser detection code (removed from jQuery >=1.9):
|
10
|
+
// Can be removed along with setFavicon as soon as Firefox corrects bug: https://bugzilla.mozilla.org/show_bug.cgi?id=519028
|
11
|
+
var matched, browser;
|
12
|
+
|
13
|
+
// Use of jQuery.browser is frowned upon.
|
14
|
+
// More details: http://api.jquery.com/jQuery.browser
|
15
|
+
// jQuery.uaMatch maintained for back-compat
|
16
|
+
jQuery.uaMatch = function( ua ) {
|
17
|
+
ua = ua.toLowerCase();
|
18
|
+
|
19
|
+
var match = /(chrome)[ \/]([\w.]+)/.exec( ua ) ||
|
20
|
+
/(webkit)[ \/]([\w.]+)/.exec( ua ) ||
|
21
|
+
/(opera)(?:.*version|)[ \/]([\w.]+)/.exec( ua ) ||
|
22
|
+
/(msie) ([\w.]+)/.exec( ua ) ||
|
23
|
+
ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec( ua ) ||
|
24
|
+
[];
|
25
|
+
|
26
|
+
return {
|
27
|
+
browser: match[ 1 ] || "",
|
28
|
+
version: match[ 2 ] || "0"
|
29
|
+
};
|
30
|
+
};
|
31
|
+
|
32
|
+
matched = jQuery.uaMatch( navigator.userAgent );
|
33
|
+
browser = {};
|
34
|
+
|
35
|
+
if ( matched.browser ) {
|
36
|
+
browser[ matched.browser ] = true;
|
37
|
+
browser.version = matched.version;
|
38
|
+
}
|
39
|
+
|
40
|
+
// Chrome is Webkit, but Webkit is also Safari.
|
41
|
+
if ( browser.chrome ) {
|
42
|
+
browser.webkit = true;
|
43
|
+
} else if ( browser.webkit ) {
|
44
|
+
browser.safari = true;
|
45
|
+
}
|
46
|
+
|
47
|
+
jQuery.browser = browser;
|
48
|
+
//===========================================
|
49
|
+
|
50
|
+
|
51
|
+
|
52
|
+
// Encapsulate everything in a nice namespace
|
53
|
+
var railsAjax = {
|
54
|
+
|
55
|
+
// Initialization, to be called just once when the DOM is ready
|
56
|
+
init: function() {
|
57
|
+
try {
|
58
|
+
// Register bindings to every Rails remote element
|
59
|
+
railsAjax.attachAjaxToElements('*[data-rails-ajax-remote=true]');
|
60
|
+
var lRootURL = History.getRootUrl();
|
61
|
+
jQuery(window).bind('statechange', function() {
|
62
|
+
try {
|
63
|
+
var lState = History.getState();
|
64
|
+
<%= 'alert(\'[RailsAjax] History statechange triggered #\' + lState.data.id + \' via \' + lState.data.type + \' to \' + lState.url.replace(lRootURL, \'/\') + \' (anchor: \' + lState.data.anchor + \')\');' if (RailsAjax.config.DebugAlerts) %>
|
65
|
+
jQuery.ajax({
|
66
|
+
// TODO (History.js): Remove the anchor attribute when History.js handles anchors correctly.
|
67
|
+
anchor: lState.data.anchor,
|
68
|
+
// Get back all parameters set by Rails' jquery-ujs
|
69
|
+
type: lState.data.type,
|
70
|
+
data: lState.data.data,
|
71
|
+
dataType: lState.data.dataType,
|
72
|
+
crossDomain: lState.data.crossDomain,
|
73
|
+
// Set the URL to query
|
74
|
+
url: lState.url.replace(lRootURL, '/'),
|
75
|
+
// And the callbacks
|
76
|
+
// TODO (History.js): Remove the beforeSend callback when anchors will be handled correctly by History.js
|
77
|
+
beforeSend: function(ioXHR, iSettings) {
|
78
|
+
// Remember the anchor used
|
79
|
+
ioXHR.anchor = iSettings.anchor;
|
80
|
+
},
|
81
|
+
success: function(iData, iStatus, iXHR) {
|
82
|
+
railsAjax.ajaxSuccess(iXHR, iData);
|
83
|
+
},
|
84
|
+
error: function(iXHR, iStatus, iError) {
|
85
|
+
railsAjax.ajaxError(iXHR, iError);
|
86
|
+
}
|
87
|
+
});
|
88
|
+
} catch (iError) {
|
89
|
+
alert('[RailsAjax] Exception in statechange callback: ' + iError.name + ' - ' + iError.message);
|
90
|
+
}
|
91
|
+
});
|
92
|
+
} catch (iError) {
|
93
|
+
alert('[RailsAjax] Exception during railsAjax initialization: ' + iError.name + ' - ' + iError.message);
|
94
|
+
}
|
95
|
+
},
|
96
|
+
|
97
|
+
// Attach Ajax delegations to elements needing them
|
98
|
+
//
|
99
|
+
// Parameters::
|
100
|
+
// * *iCSSSelector* (_String_): CSS selector of elements needing attachment
|
101
|
+
attachAjaxToElements: function(iCSSSelector) {
|
102
|
+
try {
|
103
|
+
<%= 'alert(\'[RailsAjax] Attach Ajax bindings to elements \' + iCSSSelector + \' (\' + jQuery(iCSSSelector).length + \' elements)\');' if (RailsAjax.config.DebugAlerts) %>
|
104
|
+
jQuery(document)
|
105
|
+
.delegate(iCSSSelector, 'ajax:beforeSend', function(iEvent, ioXHR, iSettings) {
|
106
|
+
try {
|
107
|
+
var lThis = jQuery(this);
|
108
|
+
var lURL = ((lThis.is('a')) ? lThis.attr('href') : lThis.attr('action'));
|
109
|
+
<%= 'alert(\'[RailsAjax] Ajax before send. URL=\' + lURL + \' Method=\' + iSettings.type + \' Data=\' + iSettings.data);' if (RailsAjax.config.DebugAlerts) %>
|
110
|
+
var lContinue = true;
|
111
|
+
if (railsAjax.beforeSend != undefined) {
|
112
|
+
try {
|
113
|
+
lContinue = (railsAjax.beforeSend(ioXHR, iSettings) != false);
|
114
|
+
} catch (iUserError) {
|
115
|
+
alert('[RailsAjax] Exception in user callback railsAjax.beforeSend: ' + iUserError.name + ' - ' + iUserError.message);
|
116
|
+
}
|
117
|
+
}
|
118
|
+
<%= 'alert(\'[RailsAjax] Continue on the Ajax load ? \' + lContinue);' if (RailsAjax.config.DebugAlerts) %>
|
119
|
+
if (lContinue) {
|
120
|
+
railsAjax.pushNewState(lURL,
|
121
|
+
{
|
122
|
+
// Replicate the whole Rails' jquery-ujs parameters, as we will need to pass them to the new ajax options object created during onstatechange event.
|
123
|
+
type: iSettings.type,
|
124
|
+
data: iSettings.data,
|
125
|
+
dataType: iSettings.dataType,
|
126
|
+
crossDomain: iSettings.crossDomain
|
127
|
+
},
|
128
|
+
false
|
129
|
+
);
|
130
|
+
}
|
131
|
+
// Cancel AJAX processing right now.
|
132
|
+
// It will be taken care of by the changestate event.
|
133
|
+
return false;
|
134
|
+
} catch (iError) {
|
135
|
+
alert('[RailsAjax] Exception while ajax:beforeSend: ' + iError.name + ' - ' + iError.message);
|
136
|
+
}
|
137
|
+
})
|
138
|
+
} catch (iError) {
|
139
|
+
alert('[RailsAjax] Exception while binding Ajax events to elements ' + iCSSSelector + ': ' + iError.name + ' - ' + iError.message);
|
140
|
+
}
|
141
|
+
},
|
142
|
+
|
143
|
+
// Callback called when Ajax call is successfull
|
144
|
+
//
|
145
|
+
// Parameters::
|
146
|
+
// * *iXHR* (_Object_): The XHR object responsible for the Ajax call
|
147
|
+
// * *iData* (_String_): The received data
|
148
|
+
ajaxSuccess: function(iXHR, iData) {
|
149
|
+
try {
|
150
|
+
<%= 'alert(\'[RailsAjax] Ajax success. Loading data: \' + iData);' if (RailsAjax.config.DebugAlerts) %>
|
151
|
+
|
152
|
+
// 1. Change the window's title if needed
|
153
|
+
if ('page_title' in iData) {
|
154
|
+
document.title = iData.page_title;
|
155
|
+
}
|
156
|
+
|
157
|
+
// 2. Replace HTML data, and execute scripts in the same time
|
158
|
+
jQuery.each(iData.div_contents, function(iCSSSelector, iContent) {
|
159
|
+
// This replaces HTML content and also executes scripts (even document.ready ones)
|
160
|
+
jQuery(iCSSSelector).html(iContent);
|
161
|
+
});
|
162
|
+
|
163
|
+
// 3. Scroll to anchor if needed
|
164
|
+
if (iXHR.anchor) {
|
165
|
+
railsAjax.scrollToAnchor(iXHR.anchor);
|
166
|
+
}
|
167
|
+
|
168
|
+
// 4. For Firefox only, workaround a bug by setting the Favicon
|
169
|
+
if (jQuery.browser.mozilla) {
|
170
|
+
railsAjax.setFavicon();
|
171
|
+
}
|
172
|
+
|
173
|
+
// 5. Execute scripts that were added
|
174
|
+
if ('js_to_execute' in iData) {
|
175
|
+
jQuery.each(iData.js_to_execute, function(iIdx, iJS) {
|
176
|
+
<%= 'alert(\'[RailsAjax] Execute script #\' + iIdx + \':\\n\' + iJS);' if (RailsAjax.config.DebugAlerts) %>
|
177
|
+
try {
|
178
|
+
eval(iJS);
|
179
|
+
} catch (iError) {
|
180
|
+
alert('[RailsAjax] Exception while executing Ajax script: ' + iError.name + ' - ' + iError.message + '\nScript:\n' + iJS);
|
181
|
+
}
|
182
|
+
});
|
183
|
+
}
|
184
|
+
|
185
|
+
// 6. Call user callbacks
|
186
|
+
if (railsAjax.success != undefined) {
|
187
|
+
try {
|
188
|
+
railsAjax.success(iXHR, iData);
|
189
|
+
} catch (iUserError) {
|
190
|
+
alert('[RailsAjax] Exception in user callback railsAjax.success: ' + iUserError.name + ' - ' + iUserError.message);
|
191
|
+
}
|
192
|
+
}
|
193
|
+
if (railsAjax.complete != undefined) {
|
194
|
+
try {
|
195
|
+
railsAjax.complete(iXHR);
|
196
|
+
} catch (iUserError) {
|
197
|
+
alert('[RailsAjax] Exception in user callback railsAjax.complete: ' + iUserError.name + ' - ' + iUserError.message);
|
198
|
+
}
|
199
|
+
}
|
200
|
+
|
201
|
+
// 7. Handle redirects
|
202
|
+
if ('redirect_to' in iData) {
|
203
|
+
railsAjax.pushNewState(iData.redirect_to, {}, true);
|
204
|
+
}
|
205
|
+
|
206
|
+
} catch (iError) {
|
207
|
+
alert('[RailsAjax] Exception while Ajax success callback: ' + iError.name + ' - ' + iError.message);
|
208
|
+
}
|
209
|
+
},
|
210
|
+
|
211
|
+
// Callback called when Ajax call has failed
|
212
|
+
//
|
213
|
+
// Parameters::
|
214
|
+
// * *iXHR* (_Object_): The xhr object
|
215
|
+
// * *iError* (_Object_): The error
|
216
|
+
ajaxError: function(iXHR, iError) {
|
217
|
+
if (railsAjax.error != undefined) {
|
218
|
+
try {
|
219
|
+
railsAjax.error(iXHR, iError);
|
220
|
+
} catch (iUserError) {
|
221
|
+
alert('[RailsAjax] Exception in user callback railsAjax.error: ' + iUserError.name + ' - ' + iUserError.message);
|
222
|
+
}
|
223
|
+
}
|
224
|
+
if (railsAjax.complete != undefined) {
|
225
|
+
try {
|
226
|
+
railsAjax.complete(iXHR);
|
227
|
+
} catch (iUserError) {
|
228
|
+
alert('[RailsAjax] Exception in user callback railsAjax.complete: ' + iUserError.name + ' - ' + iUserError.message);
|
229
|
+
}
|
230
|
+
}
|
231
|
+
},
|
232
|
+
|
233
|
+
// Used to identify states in debugging logs
|
234
|
+
<%= 'gStateID: 0,' if (RailsAjax.config.DebugAlerts) %>
|
235
|
+
|
236
|
+
// Push a new History state
|
237
|
+
//
|
238
|
+
// Parameters::
|
239
|
+
// * *iURL* (_String_): The URL to be pushed
|
240
|
+
// * *iData* (_Object_): The state object to store with it
|
241
|
+
// * *iReplaceState* (_Boolean_): Do we replace the current state ?
|
242
|
+
pushNewState: function(iURL, iData, iReplaceState) {
|
243
|
+
try {
|
244
|
+
// Remove the hash if it is present, as otherwise anchorchange is triggered and statechange ignored.
|
245
|
+
// We don't want to handle anchorchange callback as it is unable to get the real History state correctly.
|
246
|
+
// TODO (History.js): Correct bug https://github.com/balupton/history.js/issues/42 and https://github.com/balupton/history.js/issues/173, then remove this special hash and anchor treatment
|
247
|
+
var lSplittedURL = iURL.split('#');
|
248
|
+
var lState = jQuery.extend(
|
249
|
+
{
|
250
|
+
// Used to identify states in debugging logs
|
251
|
+
<%= 'id: railsAjax.gStateID,' if (RailsAjax.config.DebugAlerts) %>
|
252
|
+
anchor: lSplittedURL[1]
|
253
|
+
},
|
254
|
+
iData
|
255
|
+
);
|
256
|
+
if (iReplaceState) {
|
257
|
+
History.replaceState(lState, null, lSplittedURL[0]);
|
258
|
+
} else {
|
259
|
+
History.pushState(lState, null, lSplittedURL[0]);
|
260
|
+
}
|
261
|
+
<%= 'railsAjax.gStateID = railsAjax.gStateID + 1;' if (RailsAjax.config.DebugAlerts) %>
|
262
|
+
} catch (iError) {
|
263
|
+
alert('[RailsAjax] Exception in pushNewState: ' + iError.name + ' - ' + iError.message);
|
264
|
+
}
|
265
|
+
},
|
266
|
+
|
267
|
+
// Scroll the web page to display a given anchor.
|
268
|
+
// An anchor is either a div id, or a named "a"
|
269
|
+
//
|
270
|
+
// Parameters::
|
271
|
+
// * *iAnchor* (_String_): Anchor name
|
272
|
+
scrollToAnchor: function(iAnchor) {
|
273
|
+
var lDivElement = jQuery('div#' + iAnchor)[0]
|
274
|
+
if (lDivElement != undefined) {
|
275
|
+
lDivElement.scrollIntoView(true);
|
276
|
+
} else {
|
277
|
+
var lLstElements = window.document.getElementsByName(iAnchor);
|
278
|
+
if (lLstElements.length > 0) {
|
279
|
+
lLstElements[0].scrollIntoView(true);
|
280
|
+
} else {
|
281
|
+
alert('[RailsAjax] No anchor named ' + iAnchor);
|
282
|
+
}
|
283
|
+
}
|
284
|
+
},
|
285
|
+
|
286
|
+
// Reload the favicon.
|
287
|
+
// This is needed to workaround a Firefox bug: http://kilianvalkhof.com/2010/javascript/the-case-of-the-disappearing-favicon/
|
288
|
+
setFavicon: function() {
|
289
|
+
var link = jQuery('link[type="image/x-icon"]').remove().attr('href');
|
290
|
+
jQuery('<link href="'+ link +'" rel="shortcut icon" type="image/x-icon" />').appendTo('head');
|
291
|
+
}
|
292
|
+
|
293
|
+
};
|
294
|
+
|
295
|
+
// Initialize everything once the DOM is loaded and ready
|
296
|
+
jQuery(document).ready(function(){
|
297
|
+
railsAjax.init();
|
298
|
+
});
|
299
|
+
|
300
|
+
<% end %>
|
@@ -4,6 +4,7 @@ RailsAjax::configure do
|
|
4
4
|
# Enable or disable RailsAjax.
|
5
5
|
# If disabled, RailsAjax methods will be useless, and corresponding JavaScript will be removed.
|
6
6
|
# When changing this switch, you have to:
|
7
|
+
# * Remove the tmp/cache directory
|
7
8
|
# * Precompile your assets in production mode
|
8
9
|
# * Restart your server
|
9
10
|
enable true
|
@@ -17,7 +17,10 @@ module RailsAjax
|
|
17
17
|
lArgs = _normalize_args(*iArgs, &iBlock)
|
18
18
|
if ((request.xhr?) and
|
19
19
|
(!lArgs.has_key?(:partial)) and
|
20
|
-
(!lArgs.has_key?(:layout))
|
20
|
+
(!lArgs.has_key?(:layout)) and
|
21
|
+
(!lArgs.has_key?(:json)) and
|
22
|
+
(params['format'] != 'json') and
|
23
|
+
(self.content_type != 'application/json'))
|
21
24
|
logger.debug "[RailsAjax] render: iArgs=#{iArgs.inspect} iBlock?#{iBlock != nil} flash=#{flash.inspect} | Normalized arguments: #{lArgs.inspect}"
|
22
25
|
|
23
26
|
# If we have a redirection, use redirect_to
|
@@ -25,7 +28,6 @@ module RailsAjax
|
|
25
28
|
# Complete arguments if needed
|
26
29
|
# We don't want a special layout for Ajax requests: this was asked using AJAX for a page to be displayed in the main content
|
27
30
|
lArgs[:layout] = false
|
28
|
-
|
29
31
|
# Render
|
30
32
|
lMainPage = nil
|
31
33
|
if (iBlock == nil)
|
@@ -11,13 +11,13 @@ module RailsAjax
|
|
11
11
|
# Replaces form_tag with an Ajax updated version
|
12
12
|
def form_tag(url_for_options = {}, options = {}, &block)
|
13
13
|
if (RailsAjax.config.Enabled and
|
14
|
-
(options
|
14
|
+
RailsAjax::rails_ajaxifiable?(options))
|
15
15
|
if block_given?
|
16
|
-
return super(url_for_options, options.merge({:remote => true})) do
|
16
|
+
return super(url_for_options, options.merge({ :remote => true, :'data-rails-ajax-remote' => true })) do
|
17
17
|
block.call
|
18
18
|
end
|
19
19
|
else
|
20
|
-
return super(url_for_options, options.merge({:remote => true}))
|
20
|
+
return super(url_for_options, options.merge({ :remote => true, :'data-rails-ajax-remote' => true }))
|
21
21
|
end
|
22
22
|
elsif block_given?
|
23
23
|
return super(url_for_options, options) do
|
@@ -25,4 +25,16 @@ module RailsAjax
|
|
25
25
|
@@Configuration.instance_eval(&iProcConfig)
|
26
26
|
end
|
27
27
|
|
28
|
+
# Find if we use rails-ajax for a given set of options applicable to a link
|
29
|
+
#
|
30
|
+
# Parameters::
|
31
|
+
# * *iOptions* (<em>map<Symbol,Object></em>): The options provided to a link (used in link_to or form_tag)
|
32
|
+
# Result::
|
33
|
+
# * _Boolean_: Would this link be handled by rails-ajax?
|
34
|
+
def self.rails_ajaxifiable?(iOptions)
|
35
|
+
return ((iOptions[:use_rails_ajax] != false) and # User has not removed rails-ajax voluntarily
|
36
|
+
(iOptions[:target] == nil) and # Open in the same window and
|
37
|
+
(!iOptions.has_key?(:remote))) # User has not specified Ajax call itself
|
38
|
+
end
|
39
|
+
|
28
40
|
end
|
@@ -14,16 +14,18 @@ module RailsAjax
|
|
14
14
|
lName = args.first
|
15
15
|
lOptions = args.second || {}
|
16
16
|
lHTMLOptions = args.third || {}
|
17
|
-
if
|
17
|
+
# Decide if we use rails-ajax or not for this link
|
18
|
+
if RailsAjax::rails_ajaxifiable?(lHTMLOptions)
|
19
|
+
# Adapt the link
|
18
20
|
if block_given?
|
19
|
-
return super(lName, lOptions, lHTMLOptions.merge({:remote => true})) do
|
21
|
+
return super(lName, lOptions, lHTMLOptions.merge({ :remote => true, :'data-rails-ajax-remote' => true })) do
|
20
22
|
block.call
|
21
23
|
end
|
22
24
|
else
|
23
|
-
return super(lName, lOptions, lHTMLOptions.merge({:remote => true}))
|
25
|
+
return super(lName, lOptions, lHTMLOptions.merge({ :remote => true, :'data-rails-ajax-remote' => true }))
|
24
26
|
end
|
25
27
|
elsif block_given?
|
26
|
-
#
|
28
|
+
# Don't use Rails-Ajax
|
27
29
|
return super(*args) do
|
28
30
|
block.call
|
29
31
|
end
|
@@ -31,6 +33,7 @@ module RailsAjax
|
|
31
33
|
return super(*args)
|
32
34
|
end
|
33
35
|
elsif block_given?
|
36
|
+
# Don't use Rails-Ajax
|
34
37
|
return super(*args) do
|
35
38
|
block.call
|
36
39
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-ajax
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0.20130913
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-09-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|