jqmobi-rails 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. data/.gitignore +17 -0
  2. data/Gemfile +8 -0
  3. data/LICENSE +22 -0
  4. data/README.md +29 -0
  5. data/Rakefile +68 -0
  6. data/jqmobi-rails.gemspec +17 -0
  7. data/lib/jqmobi/rails/engine.rb +6 -0
  8. data/lib/jqmobi/rails/version.rb +7 -0
  9. data/lib/jqmobi/rails.rb +7 -0
  10. data/lib/jqmobi-rails.rb +1 -0
  11. data/vendor/assets/javascripts/jq.mobi.js +1894 -0
  12. data/vendor/assets/javascripts/jq.mobi_ujs.js +393 -0
  13. data/vendor/assets/javascripts/jq.ui.js +3396 -0
  14. data/vendor/assets/javascripts/plugins/jq.actionsheet.js +99 -0
  15. data/vendor/assets/javascripts/plugins/jq.alphatable.js +136 -0
  16. data/vendor/assets/javascripts/plugins/jq.carousel.js +415 -0
  17. data/vendor/assets/javascripts/plugins/jq.css3animate.js +155 -0
  18. data/vendor/assets/javascripts/plugins/jq.drawer.js +224 -0
  19. data/vendor/assets/javascripts/plugins/jq.fx.js +110 -0
  20. data/vendor/assets/javascripts/plugins/jq.passwordBox.js +45 -0
  21. data/vendor/assets/javascripts/plugins/jq.popup.js +201 -0
  22. data/vendor/assets/javascripts/plugins/jq.scroller.js +540 -0
  23. data/vendor/assets/javascripts/plugins/jq.selectBox.js +315 -0
  24. data/vendor/assets/javascripts/plugins/jq.shake.js +39 -0
  25. data/vendor/assets/javascripts/plugins/jq.social.js +113 -0
  26. data/vendor/assets/javascripts/plugins/jq.swipe.js +121 -0
  27. data/vendor/assets/javascripts/plugins/jq.template.js +26 -0
  28. data/vendor/assets/javascripts/plugins/jq.web.min.js +66 -0
  29. data/vendor/assets/stylesheets/plugins/jq.actionsheet.css +57 -0
  30. data/vendor/assets/stylesheets/plugins/jq.popup.css +73 -0
  31. data/vendor/assets/stylesheets/plugins/jq.scroller.css +10 -0
  32. data/vendor/assets/stylesheets/plugins/jq.selectBox.css +35 -0
  33. metadata +77 -0
@@ -0,0 +1,393 @@
1
+ (function($, undefined) {
2
+
3
+ /**
4
+ * Unobtrusive scripting adapter for jqMobi
5
+ *
6
+ * Based on jquery-ujs: https://github.com/rails/jquery-ujs
7
+ *
8
+ * Uploading file using rails.js
9
+ * =============================
10
+ *
11
+ * By default, browsers do not allow files to be uploaded via AJAX. As a result, if there are any non-blank file fields
12
+ * in the remote form, this adapter aborts the AJAX submission and allows the form to submit through standard means.
13
+ *
14
+ * The `ajax:aborted:file` event allows you to bind your own handler to process the form submission however you wish.
15
+ *
16
+ * Ex:
17
+ * $('form').live('ajax:aborted:file', function(event, elements){
18
+ * // Implement own remote file-transfer handler here for non-blank file inputs passed in `elements`.
19
+ * // Returning false in this handler tells rails.js to disallow standard form submission
20
+ * return false;
21
+ * });
22
+ *
23
+ * The `ajax:aborted:file` event is fired when a file-type input is detected with a non-blank value.
24
+ *
25
+ * Third-party tools can use this hook to detect when an AJAX file upload is attempted, and then use
26
+ * techniques like the iframe method to upload the file instead.
27
+ *
28
+ * Required fields in rails.js
29
+ * ===========================
30
+ *
31
+ * If any blank required inputs (required="required") are detected in the remote form, the whole form submission
32
+ * is canceled. Note that this is unlike file inputs, which still allow standard (non-AJAX) form submission.
33
+ *
34
+ * The `ajax:aborted:required` event allows you to bind your own handler to inform the user of blank required inputs.
35
+ *
36
+ * !! Note that Opera does not fire the form's submit event if there are blank required inputs, so this event may never
37
+ * get fired in Opera. This event is what causes other browsers to exhibit the same submit-aborting behavior.
38
+ *
39
+ * Ex:
40
+ * $('form').live('ajax:aborted:required', function(event, elements){
41
+ * // Returning false in this handler tells rails.js to submit the form anyway.
42
+ * // The blank required inputs are passed to this function in `elements`.
43
+ * return ! confirm("Would you like to submit the form with missing info?");
44
+ * });
45
+ */
46
+
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 jqmobi-ujs
52
+ linkClickSelector: 'a[data-confirm], a[data-method], a[data-remote], a[data-disable-with]',
53
+
54
+ // Select elements bound by jqmobi-ujs
55
+ inputChangeSelector: 'select[data-remote], input[data-remote], textarea[data-remote]',
56
+
57
+ // Form elements bound by jqmobi-ujs
58
+ formSubmitSelector: 'form',
59
+
60
+ // Form input elements bound by jqmobi-ujs
61
+ formInputClickSelector: 'form input[type=submit], form input[type=image], form button[type=submit], form button:not([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[type=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 || event.returnValue === 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
+ // Default way to get an element's href. May be overridden at $.rails.href.
102
+ href: function(element) {
103
+ return element.attr('href');
104
+ },
105
+
106
+ // Submits "remote" forms and links with ajax
107
+ handleRemote: function(element) {
108
+ var method, url, data, crossDomain, dataType, options;
109
+
110
+ if (rails.fire(element, 'ajax:before')) {
111
+ crossDomain = eval(element.data('cross-domain')) || null;
112
+ dataType = element.data('type') || 'json';
113
+
114
+ if (element.filter('form').length > 0) {
115
+ method = element.attr('method');
116
+ url = element.attr('action');
117
+ data = element.serialize();
118
+ // memoized value from clicked submit button
119
+ var button = element.data('ujs:submit-button');
120
+ if (button) {
121
+ throw "not implemented";
122
+ //data += '&' + encodeURIComponent(button);
123
+ element.data('ujs:submit-button', null);
124
+ }
125
+ } else if (element.filter(rails.inputChangeSelector).length > 0) {
126
+ method = element.data('method');
127
+ url = element.data('url');
128
+ data = $('<form />').append(element).serialize();
129
+ if (element.data('params')) data = data + "&" + element.data('params');
130
+ } else {
131
+ method = element.data('method');
132
+ url = rails.href(element);
133
+ data = element.data('params') || null;
134
+ }
135
+
136
+ options = {
137
+ type: method || 'GET', data: data, dataType: dataType, crossDomain: crossDomain,
138
+ // stopping the "ajax:beforeSend" event will cancel the ajax request
139
+ beforeSend: function(xhr, settings) {
140
+ if (element.data('type') === null) {
141
+ xhr.setRequestHeader('accept', '*/*;q=0.5, text/javascript, application/javascript');
142
+ }else if(settings.dataType.split('/')[1] == 'json'){
143
+ xhr.setRequestHeader('accept', 'application/json, text/javascript, */*; q=0.01');
144
+ }
145
+ return rails.fire(element, 'ajax:beforeSend', [xhr, settings]);
146
+ },
147
+ success: function(data, status, xhr) {
148
+ element.trigger('ajax:success', [data, status, xhr]);
149
+ },
150
+ complete: function(xhr, status) {
151
+ element.trigger('ajax:complete', [xhr, status]);
152
+ },
153
+ error: function(xhr, status, error) {
154
+ element.trigger('ajax:error', [xhr, status, xhr.statusText]);
155
+ }
156
+ };
157
+ // Only pass url to `ajax` options if not blank
158
+ if (url) { options.url = url; } else { options.url = window.location.href; }
159
+
160
+ return rails.ajax(options);
161
+ } else {
162
+ return false;
163
+ }
164
+ },
165
+
166
+ // Handles "data-method" on links such as:
167
+ // <a href="/users/5" data-method="delete" rel="nofollow" data-confirm="Are you sure?">Delete</a>
168
+ handleMethod: function(link) {
169
+ var href = rails.href(link),
170
+ method = link.data('method'),
171
+ target = link.attr('target'),
172
+ csrf_token = $('meta[name=csrf-token]').attr('content'),
173
+ csrf_param = $('meta[name=csrf-param]').attr('content'),
174
+ form = $('<form method="post" action="' + href + '"></form>'),
175
+ metadata_input = '<input name="_method" value="' + method + '" type="hidden" />';
176
+
177
+ if (csrf_param !== null && csrf_token !== null) {
178
+ metadata_input += '<input name="' + csrf_param + '" value="' + csrf_token + '" type="hidden" />';
179
+ }
180
+
181
+ if (target) { form.attr('target', target); }
182
+
183
+ $('body').append(form.hide().append(metadata_input));
184
+ if(rails.fire(form, 'submit') !== false){ form[0].submit(); }
185
+ },
186
+
187
+ /* Disables form elements:
188
+ - Caches element value in 'ujs:enable-with' data store
189
+ - Replaces element text with value of 'data-disable-with' attribute
190
+ - Sets disabled property to true
191
+ */
192
+ disableFormElements: function(form) {
193
+ form.find(rails.disableSelector).each(function() {
194
+ var element = $(this), method = (element.filter('button').length > 0) ? 'html' : 'val';
195
+ element.data('ujs:enable-with', element[method]());
196
+ element[method](element.data('disable-with'));
197
+ element.attr('disabled', 'disabled');
198
+ });
199
+ },
200
+
201
+ /* Re-enables disabled form elements:
202
+ - Replaces element text with cached value from 'ujs:enable-with' data store (created in `disableFormElements`)
203
+ - Sets disabled property to false
204
+ */
205
+ enableFormElements: function(form) {
206
+ form.find(rails.enableSelector).each(function() {
207
+ var element = $(this), method = (element.filter('button').length > 0) ? 'html' : 'val';
208
+ if (element.data('ujs:enable-with')) element[method](element.data('ujs:enable-with'));
209
+ element.removeAttr('disabled');
210
+ });
211
+ },
212
+
213
+ /* For 'data-confirm' attribute:
214
+ - Fires `confirm` event
215
+ - Shows the confirmation dialog
216
+ - Fires the `confirm:complete` event
217
+
218
+ Returns `true` if no function stops the chain and user chose yes; `false` otherwise.
219
+ Attaching a handler to the element's `confirm` event that returns a `falsy` value cancels the confirmation dialog.
220
+ Attaching a handler to the element's `confirm:complete` event that returns a `falsy` value makes this function
221
+ return false. The `confirm:complete` event is fired whether or not the user answered true or false to the dialog.
222
+ */
223
+ allowAction: function(element) {
224
+ var message = element.data('confirm'),
225
+ answer = false, callback;
226
+ if (!message) { return true; }
227
+
228
+ if (rails.fire(element, 'confirm')) {
229
+ answer = rails.confirm(message);
230
+ callback = rails.fire(element, 'confirm:complete', [answer]);
231
+ }
232
+ return answer && callback;
233
+ },
234
+
235
+ // Helper function which checks for blank inputs in a form that match the specified CSS selector
236
+ blankInputs: function(form, specifiedSelector, nonBlank) {
237
+ var inputs = $(), input,
238
+ selector = specifiedSelector || 'input,textarea';
239
+ form.find(selector).each(function() {
240
+ input = $(this);
241
+ // Collect non-blank inputs if nonBlank option is true, otherwise, collect blank inputs
242
+ if (nonBlank ? input.val() : !input.val()) {
243
+ inputs[inputs.length++] = this;
244
+ }
245
+ });
246
+ return inputs.length ? inputs : false;
247
+ },
248
+
249
+ // Helper function which checks for non-blank inputs in a form that match the specified CSS selector
250
+ nonBlankInputs: function(form, specifiedSelector) {
251
+ return rails.blankInputs(form, specifiedSelector, true); // true specifies nonBlank
252
+ },
253
+
254
+ // Helper function, needed to provide consistent behavior in IE
255
+ stopEverything: function(e) {
256
+ $(e.target).trigger('ujs:everythingStopped');
257
+ e.stopImmediatePropagation();
258
+ return false;
259
+ },
260
+
261
+ // find all the submit events directly bound to the form and
262
+ // manually invoke them. If anyone returns false then stop the loop
263
+ callFormSubmitBindings: function(form, event) {
264
+ var events = form.data('events'), continuePropagation = true;
265
+ if (events !== null && events['submit'] !== undefined) {
266
+ $.each(events['submit'], function(i, obj){
267
+ if (typeof obj.handler === 'function') return continuePropagation = obj.handler(event);
268
+ });
269
+ }
270
+ return continuePropagation;
271
+ },
272
+
273
+ // replace element's html with the 'data-disable-with' after storing original html
274
+ // and prevent clicking on it
275
+ disableElement: function(element) {
276
+ element.data('ujs:enable-with', element.html()); // store enabled state
277
+ element.html(element.data('disable-with')); // set to disabled state
278
+ element.bind('click.railsDisable', function(e) { // prevent further clicking
279
+ return rails.stopEverything(e)
280
+ });
281
+ },
282
+
283
+ // restore element to its original state which was disabled by 'disableElement' above
284
+ enableElement: function(element) {
285
+ if (element.data('ujs:enable-with') !== null) {
286
+ element.html(element.data('ujs:enable-with')); // set to old enabled state
287
+ // this should be element.removeData('ujs:enable-with')
288
+ // but, there is currently a bug in jquery which makes hyphenated data attributes not get removed
289
+ element.removeAttr('data-ujs:enable-with'); // clean up cache
290
+ }
291
+ element.unbind('click.railsDisable'); // enable element
292
+ }
293
+
294
+ };
295
+
296
+ $.ajaxWithoutCSRFProtection = $.ajax;
297
+ $.ajax = function(opts){
298
+ var settings = opts || {};
299
+ settings.hookedBeforeSend = settings.beforeSend;
300
+ settings.beforeSend = function(xhr, options){
301
+ if ( !options.crossDomain ) { rails.CSRFProtection(xhr); }
302
+ if ( options.hookedBeforeSend ){ return options.hookedBeforeSend.call(this, xhr, options); }
303
+ };
304
+ return this.ajaxWithoutCSRFProtection(settings);
305
+ };
306
+
307
+ $(document).delegate(rails.linkDisableSelector, 'ajax:complete', function() {
308
+ rails.enableElement($(this));
309
+ });
310
+
311
+ $(document).delegate(rails.linkClickSelector, 'click.rails', function(e) {
312
+ var link = $(this), method = link.data('method'), data = link.data('params');
313
+ if (!rails.allowAction(link)) return rails.stopEverything(e);
314
+
315
+ if (link.filter(rails.linkDisableSelector).length > 0) rails.disableElement(link);
316
+
317
+ if (link.data('remote') !== null) {
318
+ if ( (e.metaKey || e.ctrlKey) && (!method || method === 'GET') && !data ) { return true; }
319
+
320
+ if (rails.handleRemote(link) === false) { rails.enableElement(link); }
321
+ return false;
322
+
323
+ } else if (link.data('method')) {
324
+ rails.handleMethod(link);
325
+ return false;
326
+ }
327
+ });
328
+
329
+ $(document).delegate(rails.inputChangeSelector, 'change.rails', function(e) {
330
+ var link = $(this);
331
+ if (!rails.allowAction(link)) return rails.stopEverything(e);
332
+
333
+ rails.handleRemote(link);
334
+ return false;
335
+ });
336
+
337
+ $(document).delegate(rails.formSubmitSelector, 'submit.rails', function(e) {
338
+ var form = $(this),
339
+ remote = form.data('remote') !== null,
340
+ blankRequiredInputs = rails.blankInputs(form, rails.requiredInputSelector),
341
+ nonBlankFileInputs = rails.nonBlankInputs(form, rails.fileInputSelector);
342
+
343
+ if (!rails.allowAction(form)) return rails.stopEverything(e);
344
+
345
+ // skip other logic when required values are missing or file upload is present
346
+ if (blankRequiredInputs && form.attr("novalidate") == null && rails.fire(form, 'ajax:aborted:required', [blankRequiredInputs])) {
347
+ return rails.stopEverything(e);
348
+ }
349
+
350
+ if (remote) {
351
+ if (nonBlankFileInputs) {
352
+ return rails.fire(form, 'ajax:aborted:file', [nonBlankFileInputs]);
353
+ }
354
+
355
+ if (e.defaultPrevented) return rails.stopEverything(e);
356
+
357
+ rails.handleRemote(form);
358
+ return false;
359
+
360
+ } else {
361
+ // slight timeout so that the submit button gets properly serialized
362
+ setTimeout(function(){ rails.disableFormElements(form); }, 13);
363
+ }
364
+ });
365
+
366
+ $(document).delegate(rails.formInputClickSelector, 'click.rails', function(event) {
367
+ var button = $(this);
368
+
369
+ if (!rails.allowAction(button)) return rails.stopEverything(event);
370
+
371
+ // register the pressed submit button
372
+ var name = button.attr('name'),
373
+ data = name ? {name:name, value:button.val()} : null;
374
+
375
+ button.closest('form').data('ujs:submit-button', data);
376
+ });
377
+
378
+ $(document).delegate(rails.formSubmitSelector, 'ajax:beforeSend.rails', function(event) {
379
+ if (this == event.target && !event.defaultPrevented) rails.disableFormElements($(this));
380
+ });
381
+
382
+ $(document).delegate(rails.formSubmitSelector, 'ajax:complete.rails', function(event) {
383
+ if (this == event.target) rails.enableFormElements($(this));
384
+ });
385
+
386
+ $(function(){
387
+ // making sure that all forms have actual up-to-date token(cached forms contain old one)
388
+ csrf_token = $('meta[name=csrf-token]').attr('content');
389
+ csrf_param = $('meta[name=csrf-param]').attr('content');
390
+ $('form input[name="' + csrf_param + '"]').val(csrf_token);
391
+ });
392
+
393
+ })( jq );