iqjax 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,367 @@
1
+ /**
2
+ * Unobtrusive scripting adapter for jQuery
3
+ *
4
+ * Requires jQuery 1.6.0 or later.
5
+ * https://github.com/rails/jquery-ujs
6
+
7
+ * Uploading file using rails.js
8
+ * =============================
9
+ *
10
+ * By default, browsers do not allow files to be uploaded via AJAX. As a result, if there are any non-blank file fields
11
+ * in the remote form, this adapter aborts the AJAX submission and allows the form to submit through standard means.
12
+ *
13
+ * The `ajax:aborted:file` event allows you to bind your own handler to process the form submission however you wish.
14
+ *
15
+ * Ex:
16
+ * $('form').live('ajax:aborted:file', function(event, elements){
17
+ * // Implement own remote file-transfer handler here for non-blank file inputs passed in `elements`.
18
+ * // Returning false in this handler tells rails.js to disallow standard form submission
19
+ * return false;
20
+ * });
21
+ *
22
+ * The `ajax:aborted:file` event is fired when a file-type input is detected with a non-blank value.
23
+ *
24
+ * Third-party tools can use this hook to detect when an AJAX file upload is attempted, and then use
25
+ * techniques like the iframe method to upload the file instead.
26
+ *
27
+ * Required fields in rails.js
28
+ * ===========================
29
+ *
30
+ * If any blank required inputs (required="required") are detected in the remote form, the whole form submission
31
+ * is canceled. Note that this is unlike file inputs, which still allow standard (non-AJAX) form submission.
32
+ *
33
+ * The `ajax:aborted:required` event allows you to bind your own handler to inform the user of blank required inputs.
34
+ *
35
+ * !! Note that Opera does not fire the form's submit event if there are blank required inputs, so this event may never
36
+ * get fired in Opera. This event is what causes other browsers to exhibit the same submit-aborting behavior.
37
+ *
38
+ * Ex:
39
+ * $('form').live('ajax:aborted:required', function(event, elements){
40
+ * // Returning false in this handler tells rails.js to submit the form anyway.
41
+ * // The blank required inputs are passed to this function in `elements`.
42
+ * return ! confirm("Would you like to submit the form with missing info?");
43
+ * });
44
+ */
45
+
46
+ (function($, undefined) {
47
+ // Shorthand to make it a little easier to call public rails functions from within rails.js
48
+ var rails;
49
+
50
+ $.rails = rails = {
51
+ // Link elements bound by jquery-ujs
52
+ linkClickSelector: 'a[data-confirm], a[data-method], a[data-remote], a[data-disable-with]',
53
+
54
+ // Select elements bound by jquery-ujs
55
+ inputChangeSelector: 'select[data-remote], input[data-remote], textarea[data-remote]',
56
+
57
+ // Form elements bound by jquery-ujs
58
+ formSubmitSelector: 'form',
59
+
60
+ // Form input elements bound by jquery-ujs
61
+ formInputClickSelector: 'form input[type=submit], form input[type=image], form button[type=submit], form button:not(button[type])',
62
+
63
+ // Form input elements disabled during form submission
64
+ disableSelector: 'input[data-disable-with], button[data-disable-with], textarea[data-disable-with]',
65
+
66
+ // Form input elements re-enabled after form submission
67
+ enableSelector: 'input[data-disable-with]:disabled, button[data-disable-with]:disabled, textarea[data-disable-with]:disabled',
68
+
69
+ // Form required input elements
70
+ requiredInputSelector: 'input[name][required]:not([disabled]),textarea[name][required]:not([disabled])',
71
+
72
+ // Form file input elements
73
+ fileInputSelector: 'input:file',
74
+
75
+ // Link onClick disable selector with possible reenable after remote submission
76
+ linkDisableSelector: 'a[data-disable-with]',
77
+
78
+ // Make sure that every Ajax request sends the CSRF token
79
+ CSRFProtection: function(xhr) {
80
+ var token = $('meta[name="csrf-token"]').attr('content');
81
+ if (token) xhr.setRequestHeader('X-CSRF-Token', token);
82
+ },
83
+
84
+ // Triggers an event on an element and returns false if the event result is false
85
+ fire: function(obj, name, data) {
86
+ var event = $.Event(name);
87
+ obj.trigger(event, data);
88
+ return event.result !== false;
89
+ },
90
+
91
+ // Default confirm dialog, may be overridden with custom confirm dialog in $.rails.confirm
92
+ confirm: function(message) {
93
+ return confirm(message);
94
+ },
95
+
96
+ // Default ajax function, may be overridden with custom function in $.rails.ajax
97
+ ajax: function(options) {
98
+ return $.ajax(options);
99
+ },
100
+
101
+ // Submits "remote" forms and links with ajax
102
+ handleRemote: function(element) {
103
+ var method, url, data,
104
+ crossDomain = element.data('cross-domain') || null,
105
+ dataType = element.data('type') || ($.ajaxSettings && $.ajaxSettings.dataType),
106
+ options;
107
+
108
+ if (rails.fire(element, 'ajax:before')) {
109
+
110
+ if (element.is('form')) {
111
+ method = element.attr('method');
112
+ url = element.attr('action');
113
+ data = element.serializeArray();
114
+ // memoized value from clicked submit button
115
+ var button = element.data('ujs:submit-button');
116
+ if (button) {
117
+ data.push(button);
118
+ element.data('ujs:submit-button', null);
119
+ }
120
+ } else if (element.is(rails.inputChangeSelector)) {
121
+ method = element.data('method');
122
+ url = element.data('url');
123
+ data = element.serialize();
124
+ if (element.data('params')) data = data + "&" + element.data('params');
125
+ } else {
126
+ method = element.data('method');
127
+ url = element.attr('href');
128
+ data = element.data('params') || null;
129
+ }
130
+
131
+ options = {
132
+ type: method || 'GET', data: data, dataType: dataType, crossDomain: crossDomain,
133
+ // stopping the "ajax:beforeSend" event will cancel the ajax request
134
+ beforeSend: function(xhr, settings) {
135
+ if (settings.dataType === undefined) {
136
+ xhr.setRequestHeader('accept', '*/*;q=0.5, ' + settings.accepts.script);
137
+ }
138
+ return rails.fire(element, 'ajax:beforeSend', [xhr, settings]);
139
+ },
140
+ success: function(data, status, xhr) {
141
+ element.trigger('ajax:success', [data, status, xhr]);
142
+ },
143
+ complete: function(xhr, status) {
144
+ element.trigger('ajax:complete', [xhr, status]);
145
+ },
146
+ error: function(xhr, status, error) {
147
+ element.trigger('ajax:error', [xhr, status, error]);
148
+ }
149
+ };
150
+ // Only pass url to `ajax` options if not blank
151
+ if (url) { options.url = url; }
152
+
153
+ rails.ajax(options);
154
+ }
155
+ },
156
+
157
+ // Handles "data-method" on links such as:
158
+ // <a href="/users/5" data-method="delete" rel="nofollow" data-confirm="Are you sure?">Delete</a>
159
+ handleMethod: function(link) {
160
+ var href = link.attr('href'),
161
+ method = link.data('method'),
162
+ target = link.attr('target'),
163
+ csrf_token = $('meta[name=csrf-token]').attr('content'),
164
+ csrf_param = $('meta[name=csrf-param]').attr('content'),
165
+ form = $('<form method="post" action="' + href + '"></form>'),
166
+ metadata_input = '<input name="_method" value="' + method + '" type="hidden" />';
167
+
168
+ if (csrf_param !== undefined && csrf_token !== undefined) {
169
+ metadata_input += '<input name="' + csrf_param + '" value="' + csrf_token + '" type="hidden" />';
170
+ }
171
+
172
+ if (target) { form.attr('target', target); }
173
+
174
+ form.hide().append(metadata_input).appendTo('body');
175
+ form.submit();
176
+ },
177
+
178
+ /* Disables form elements:
179
+ - Caches element value in 'ujs:enable-with' data store
180
+ - Replaces element text with value of 'data-disable-with' attribute
181
+ - Sets disabled property to true
182
+ */
183
+ disableFormElements: function(form) {
184
+ form.find(rails.disableSelector).each(function() {
185
+ var element = $(this), method = element.is('button') ? 'html' : 'val';
186
+ element.data('ujs:enable-with', element[method]());
187
+ element[method](element.data('disable-with'));
188
+ element.prop('disabled', true);
189
+ });
190
+ },
191
+
192
+ /* Re-enables disabled form elements:
193
+ - Replaces element text with cached value from 'ujs:enable-with' data store (created in `disableFormElements`)
194
+ - Sets disabled property to false
195
+ */
196
+ enableFormElements: function(form) {
197
+ form.find(rails.enableSelector).each(function() {
198
+ var element = $(this), method = element.is('button') ? 'html' : 'val';
199
+ if (element.data('ujs:enable-with')) element[method](element.data('ujs:enable-with'));
200
+ element.prop('disabled', false);
201
+ });
202
+ },
203
+
204
+ /* For 'data-confirm' attribute:
205
+ - Fires `confirm` event
206
+ - Shows the confirmation dialog
207
+ - Fires the `confirm:complete` event
208
+
209
+ Returns `true` if no function stops the chain and user chose yes; `false` otherwise.
210
+ Attaching a handler to the element's `confirm` event that returns a `falsy` value cancels the confirmation dialog.
211
+ Attaching a handler to the element's `confirm:complete` event that returns a `falsy` value makes this function
212
+ return false. The `confirm:complete` event is fired whether or not the user answered true or false to the dialog.
213
+ */
214
+ allowAction: function(element) {
215
+ var message = element.data('confirm'),
216
+ answer = false, callback;
217
+ if (!message) { return true; }
218
+
219
+ if (rails.fire(element, 'confirm')) {
220
+ answer = rails.confirm(message);
221
+ callback = rails.fire(element, 'confirm:complete', [answer]);
222
+ }
223
+ return answer && callback;
224
+ },
225
+
226
+ // Helper function which checks for blank inputs in a form that match the specified CSS selector
227
+ blankInputs: function(form, specifiedSelector, nonBlank) {
228
+ var inputs = $(), input,
229
+ selector = specifiedSelector || 'input,textarea';
230
+ form.find(selector).each(function() {
231
+ input = $(this);
232
+ // Collect non-blank inputs if nonBlank option is true, otherwise, collect blank inputs
233
+ if (nonBlank ? input.val() : !input.val()) {
234
+ inputs = inputs.add(input);
235
+ }
236
+ });
237
+ return inputs.length ? inputs : false;
238
+ },
239
+
240
+ // Helper function which checks for non-blank inputs in a form that match the specified CSS selector
241
+ nonBlankInputs: function(form, specifiedSelector) {
242
+ return rails.blankInputs(form, specifiedSelector, true); // true specifies nonBlank
243
+ },
244
+
245
+ // Helper function, needed to provide consistent behavior in IE
246
+ stopEverything: function(e) {
247
+ $(e.target).trigger('ujs:everythingStopped');
248
+ e.stopImmediatePropagation();
249
+ return false;
250
+ },
251
+
252
+ // find all the submit events directly bound to the form and
253
+ // manually invoke them. If anyone returns false then stop the loop
254
+ callFormSubmitBindings: function(form) {
255
+ var events = form.data('events'), continuePropagation = true;
256
+ if (events !== undefined && events['submit'] !== undefined) {
257
+ $.each(events['submit'], function(i, obj){
258
+ if (typeof obj.handler === 'function') return continuePropagation = obj.handler(obj.data);
259
+ });
260
+ }
261
+ return continuePropagation;
262
+ },
263
+
264
+ // replace element's html with the 'data-disable-with' after storing original html
265
+ // and prevent clicking on it
266
+ disableElement: function(element) {
267
+ element.data('ujs:enable-with', element.html()); // store enabled state
268
+ element.html(element.data('disable-with')); // set to disabled state
269
+ element.bind('click.railsDisable', function(e) { // prevent further clicking
270
+ return rails.stopEverything(e)
271
+ });
272
+ },
273
+
274
+ // restore element to its original state which was disabled by 'disableElement' above
275
+ enableElement: function(element) {
276
+ if (element.data('ujs:enable-with') !== undefined) {
277
+ element.html(element.data('ujs:enable-with')); // set to old enabled state
278
+ // this should be element.removeData('ujs:enable-with')
279
+ // but, there is currently a bug in jquery which makes hyphenated data attributes not get removed
280
+ element.data('ujs:enable-with', false); // clean up cache
281
+ }
282
+ element.unbind('click.railsDisable'); // enable element
283
+ }
284
+
285
+ };
286
+
287
+ $.ajaxPrefilter(function(options, originalOptions, xhr){ if ( !options.crossDomain ) { rails.CSRFProtection(xhr); }});
288
+
289
+ $(rails.linkDisableSelector).live('ajax:complete', function() {
290
+ rails.enableElement($(this));
291
+ });
292
+
293
+ $(rails.linkClickSelector).live('click.rails', function(e) {
294
+ var link = $(this), method = link.data('method'), data = link.data('params');
295
+ if (!rails.allowAction(link)) return rails.stopEverything(e);
296
+
297
+ if (link.is(rails.linkDisableSelector)) rails.disableElement(link);
298
+
299
+ if (link.data('remote') !== undefined) {
300
+ if ( (e.metaKey || e.ctrlKey) && (!method || method === 'GET') && !data ) { return true; }
301
+ rails.handleRemote(link);
302
+ return false;
303
+ } else if (link.data('method')) {
304
+ rails.handleMethod(link);
305
+ return false;
306
+ }
307
+ });
308
+
309
+ $(rails.inputChangeSelector).live('change.rails', function(e) {
310
+ var link = $(this);
311
+ if (!rails.allowAction(link)) return rails.stopEverything(e);
312
+
313
+ rails.handleRemote(link);
314
+ return false;
315
+ });
316
+
317
+ $(rails.formSubmitSelector).live('submit.rails', function(e) {
318
+ var form = $(this),
319
+ remote = form.data('remote') !== undefined,
320
+ blankRequiredInputs = rails.blankInputs(form, rails.requiredInputSelector),
321
+ nonBlankFileInputs = rails.nonBlankInputs(form, rails.fileInputSelector);
322
+
323
+ if (!rails.allowAction(form)) return rails.stopEverything(e);
324
+
325
+ // skip other logic when required values are missing or file upload is present
326
+ if (blankRequiredInputs && form.attr("novalidate") == undefined && rails.fire(form, 'ajax:aborted:required', [blankRequiredInputs])) {
327
+ return rails.stopEverything(e);
328
+ }
329
+
330
+ if (remote) {
331
+ if (nonBlankFileInputs) {
332
+ return rails.fire(form, 'ajax:aborted:file', [nonBlankFileInputs]);
333
+ }
334
+
335
+ // If browser does not support submit bubbling, then this live-binding will be called before direct
336
+ // bindings. Therefore, we should directly call any direct bindings before remotely submitting form.
337
+ if (!$.support.submitBubbles && rails.callFormSubmitBindings(form) === false) return rails.stopEverything(e);
338
+
339
+ rails.handleRemote(form);
340
+ return false;
341
+ } else {
342
+ // slight timeout so that the submit button gets properly serialized
343
+ setTimeout(function(){ rails.disableFormElements(form); }, 13);
344
+ }
345
+ });
346
+
347
+ $(rails.formInputClickSelector).live('click.rails', function(event) {
348
+ var button = $(this);
349
+
350
+ if (!rails.allowAction(button)) return rails.stopEverything(event);
351
+
352
+ // register the pressed submit button
353
+ var name = button.attr('name'),
354
+ data = name ? {name:name, value:button.val()} : null;
355
+
356
+ button.closest('form').data('ujs:submit-button', data);
357
+ });
358
+
359
+ $(rails.formSubmitSelector).live('ajax:beforeSend.rails', function(event) {
360
+ if (this == event.target) rails.disableFormElements($(this));
361
+ });
362
+
363
+ $(rails.formSubmitSelector).live('ajax:complete.rails', function(event) {
364
+ if (this == event.target) rails.enableFormElements($(this));
365
+ });
366
+
367
+ })( jQuery );
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: iqjax
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - FND
14
+ - Till Schulte-Coerne
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2012-05-07 00:00:00 Z
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: bundler
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: railties
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 15
44
+ segments:
45
+ - 3
46
+ - 2
47
+ - 0
48
+ version: 3.2.0
49
+ - - <
50
+ - !ruby/object:Gem::Version
51
+ hash: 31
52
+ segments:
53
+ - 5
54
+ - 0
55
+ version: "5.0"
56
+ type: :runtime
57
+ version_requirements: *id002
58
+ description: iQjax - a JS library for real unobtrusive Ajax
59
+ email:
60
+ - ""
61
+ - till.schulte-coerne@innoq.com
62
+ executables: []
63
+
64
+ extensions: []
65
+
66
+ extra_rdoc_files:
67
+ - README.md
68
+ - LICENSE
69
+ files:
70
+ - LICENSE
71
+ - README.md
72
+ - Rakefile
73
+ - iqjax.gemspec
74
+ - iqjax.js
75
+ - lib/iqjax/engine.rb
76
+ - lib/iqjax/version.rb
77
+ - lib/iqjax.rb
78
+ - vendor/assets/javascripts/iqjax.js
79
+ - vendor/jquery_ujs.js
80
+ - test/index.html
81
+ - test/lib/jquery.mockjax.js
82
+ - test/lib/qunit.css
83
+ - test/lib/qunit.js
84
+ - test/lib/run-qunit.js
85
+ - test/test.rb
86
+ - test/tests.js
87
+ homepage: http://github.com/innoq/iqjax
88
+ licenses: []
89
+
90
+ post_install_message:
91
+ rdoc_options: []
92
+
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ hash: 3
101
+ segments:
102
+ - 0
103
+ version: "0"
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ hash: 3
110
+ segments:
111
+ - 0
112
+ version: "0"
113
+ requirements: []
114
+
115
+ rubyforge_project:
116
+ rubygems_version: 1.8.10
117
+ signing_key:
118
+ specification_version: 3
119
+ summary: iQjax - a JS library for real unobtrusive Ajax
120
+ test_files:
121
+ - test/index.html
122
+ - test/lib/jquery.mockjax.js
123
+ - test/lib/qunit.css
124
+ - test/lib/qunit.js
125
+ - test/lib/run-qunit.js
126
+ - test/test.rb
127
+ - test/tests.js
128
+ has_rdoc: