mercury-rails 0.1.1 → 0.1.2

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 (74) hide show
  1. data/README.rdoc +81 -50
  2. data/VERSION +1 -1
  3. data/app/assets/javascripts/mercury/dialog.js.coffee +4 -4
  4. data/app/assets/javascripts/mercury/dialogs/backcolor.js.coffee +3 -3
  5. data/app/assets/javascripts/mercury/dialogs/forecolor.js.coffee +3 -3
  6. data/app/assets/javascripts/mercury/dialogs/formatblock.js.coffee +1 -1
  7. data/app/assets/javascripts/mercury/dialogs/objectspanel.js.coffee +3 -3
  8. data/app/assets/javascripts/mercury/dialogs/style.js.coffee +1 -1
  9. data/app/assets/javascripts/mercury/history_buffer.js.coffee +2 -2
  10. data/app/assets/javascripts/mercury/mercury.js.coffee +47 -54
  11. data/app/assets/javascripts/mercury/modal.js.coffee +13 -13
  12. data/app/assets/javascripts/mercury/modals/htmleditor.js.coffee +2 -2
  13. data/app/assets/javascripts/mercury/modals/insertcharacter.js.coffee +2 -2
  14. data/app/assets/javascripts/mercury/modals/insertlink.js.coffee +7 -7
  15. data/app/assets/javascripts/mercury/modals/insertmedia.js.coffee +5 -5
  16. data/app/assets/javascripts/mercury/modals/insertsnippet.js.coffee +2 -2
  17. data/app/assets/javascripts/mercury/modals/inserttable.js.coffee +14 -15
  18. data/app/assets/javascripts/mercury/native_extensions.js.coffee +2 -1
  19. data/app/assets/javascripts/mercury/page_editor.js.coffee +27 -27
  20. data/app/assets/javascripts/mercury/palette.js.coffee +6 -6
  21. data/app/assets/javascripts/mercury/panel.js.coffee +4 -4
  22. data/app/assets/javascripts/mercury/region.js.coffee +9 -9
  23. data/app/assets/javascripts/mercury/regions/editable.js.coffee +71 -71
  24. data/app/assets/javascripts/mercury/regions/markupable.js.coffee +51 -42
  25. data/app/assets/javascripts/mercury/regions/snippetable.js.coffee +7 -8
  26. data/app/assets/javascripts/mercury/select.js.coffee +8 -8
  27. data/app/assets/javascripts/mercury/snippet.js.coffee +6 -6
  28. data/app/assets/javascripts/mercury/snippet_toolbar.js.coffee +7 -7
  29. data/app/assets/javascripts/mercury/statusbar.js.coffee +7 -3
  30. data/app/assets/javascripts/mercury/table_editor.js.coffee +24 -24
  31. data/app/assets/javascripts/mercury/toolbar.button.js.coffee +20 -21
  32. data/app/assets/javascripts/mercury/toolbar.button_group.js.coffee +2 -2
  33. data/app/assets/javascripts/mercury/toolbar.expander.js.coffee +9 -9
  34. data/app/assets/javascripts/mercury/toolbar.js.coffee +12 -13
  35. data/app/assets/javascripts/mercury/tooltip.js.coffee +7 -7
  36. data/app/assets/javascripts/mercury/uploader.js.coffee +12 -13
  37. data/app/assets/javascripts/mercury_loader.js +98 -0
  38. data/app/assets/stylesheets/mercury/modal.scss +5 -5
  39. data/app/assets/stylesheets/mercury/toolbar.scss +62 -64
  40. data/app/views/layouts/mercury.html.haml +0 -4
  41. data/app/views/mercury/modals/character.html.haml +1 -1
  42. data/app/views/mercury/modals/htmleditor.html.haml +1 -1
  43. data/app/views/mercury/modals/table.html.haml +10 -10
  44. data/config/routes.rb +2 -2
  45. data/mercury-rails.gemspec +17 -20
  46. data/spec/javascripts/mercury/dialogs/backcolor_spec.js.coffee +3 -3
  47. data/spec/javascripts/mercury/dialogs/forecolor_spec.js.coffee +3 -3
  48. data/spec/javascripts/mercury/dialogs/objectspanel_spec.js.coffee +3 -3
  49. data/spec/javascripts/mercury/modals/htmleditor_spec.js.coffee +4 -4
  50. data/spec/javascripts/mercury/modals/insertcharacter_spec.js.coffee +2 -2
  51. data/spec/javascripts/mercury/modals/insertlink_spec.js.coffee +10 -10
  52. data/spec/javascripts/mercury/modals/insertmedia_spec.js.coffee +7 -7
  53. data/spec/javascripts/mercury/modals/insertsnippet_spec.js.coffee +4 -4
  54. data/spec/javascripts/mercury/modals/inserttable_spec.js.coffee +17 -17
  55. data/spec/javascripts/mercury/page_editor_spec.js.coffee +5 -6
  56. data/spec/javascripts/mercury/region_spec.js.coffee +5 -5
  57. data/spec/javascripts/mercury/regions/editable_spec.js.coffee +398 -0
  58. data/spec/javascripts/mercury/regions/snippetable_spec.js.coffee +18 -14
  59. data/spec/javascripts/mercury/snippet_spec.js.coffee +1 -1
  60. data/spec/javascripts/mercury/toolbar.button_spec.js.coffee +16 -16
  61. data/spec/javascripts/templates/mercury/modals/inserttable.html +11 -11
  62. data/spec/javascripts/templates/mercury/regions/editable.html +3 -0
  63. data/spec/javascripts/templates/mercury/toolbar.button.html +8 -8
  64. data/vendor/assets/javascripts/{jquery-ui-1.8.13.sortable.custom.js → jquery-ui-1.8.13.custom.js} +250 -0
  65. data/vendor/assets/javascripts/jquery.additions.js +151 -0
  66. data/vendor/assets/javascripts/showdown.js +1254 -1276
  67. metadata +26 -29
  68. data/app/assets/javascripts/mercury/websocket.js.coffee +0 -34
  69. data/spec/javascripts/mercury/regions/_editable_.js.coffee +0 -0
  70. data/vendor/assets/javascripts/jquery-ui-1.8.13.custom.min.js +0 -249
  71. data/vendor/assets/javascripts/jquery.easing.js +0 -173
  72. data/vendor/assets/javascripts/jquery.json2.js +0 -178
  73. data/vendor/assets/javascripts/jquery.serialize_object.js +0 -16
  74. data/vendor/assets/javascripts/jquery.ujs.js +0 -289
@@ -1,173 +0,0 @@
1
- /*
2
- * jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
3
- *
4
- * Uses the built in easing capabilities added In jQuery 1.1
5
- * to offer multiple easing options
6
- *
7
- * TERMS OF USE - jQuery Easing
8
- *
9
- * Open source under the BSD License.
10
- *
11
- * Copyright © 2008 George McGinley Smith
12
- * All rights reserved.
13
- *
14
- * Redistribution and use in source and binary forms, with or without modification,
15
- * are permitted provided that the following conditions are met:
16
- *
17
- * Redistributions of source code must retain the above copyright notice, this list of
18
- * conditions and the following disclaimer.
19
- * Redistributions in binary form must reproduce the above copyright notice, this list
20
- * of conditions and the following disclaimer in the documentation and/or other materials
21
- * provided with the distribution.
22
- *
23
- * Neither the name of the author nor the names of contributors may be used to endorse
24
- * or promote products derived from this software without specific prior written permission.
25
- *
26
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
27
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
28
- * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
31
- * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
32
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
34
- * OF THE POSSIBILITY OF SUCH DAMAGE.
35
- *
36
- */
37
-
38
- // t: current time, b: begInnIng value, c: change In value, d: duration
39
- jQuery.easing['jswing'] = jQuery.easing['swing'];
40
-
41
- jQuery.extend( jQuery.easing,
42
- {
43
- def: 'easeOutQuad',
44
- swing: function (x, t, b, c, d) {
45
- //alert(jQuery.easing.default);
46
- return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
47
- },
48
- easeInQuad: function (x, t, b, c, d) {
49
- return c*(t/=d)*t + b;
50
- },
51
- easeOutQuad: function (x, t, b, c, d) {
52
- return -c *(t/=d)*(t-2) + b;
53
- },
54
- easeInOutQuad: function (x, t, b, c, d) {
55
- if ((t/=d/2) < 1) return c/2*t*t + b;
56
- return -c/2 * ((--t)*(t-2) - 1) + b;
57
- },
58
- easeInCubic: function (x, t, b, c, d) {
59
- return c*(t/=d)*t*t + b;
60
- },
61
- easeOutCubic: function (x, t, b, c, d) {
62
- return c*((t=t/d-1)*t*t + 1) + b;
63
- },
64
- easeInOutCubic: function (x, t, b, c, d) {
65
- if ((t/=d/2) < 1) return c/2*t*t*t + b;
66
- return c/2*((t-=2)*t*t + 2) + b;
67
- },
68
- easeInQuart: function (x, t, b, c, d) {
69
- return c*(t/=d)*t*t*t + b;
70
- },
71
- easeOutQuart: function (x, t, b, c, d) {
72
- return -c * ((t=t/d-1)*t*t*t - 1) + b;
73
- },
74
- easeInOutQuart: function (x, t, b, c, d) {
75
- if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
76
- return -c/2 * ((t-=2)*t*t*t - 2) + b;
77
- },
78
- easeInQuint: function (x, t, b, c, d) {
79
- return c*(t/=d)*t*t*t*t + b;
80
- },
81
- easeOutQuint: function (x, t, b, c, d) {
82
- return c*((t=t/d-1)*t*t*t*t + 1) + b;
83
- },
84
- easeInOutQuint: function (x, t, b, c, d) {
85
- if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
86
- return c/2*((t-=2)*t*t*t*t + 2) + b;
87
- },
88
- easeInSine: function (x, t, b, c, d) {
89
- return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
90
- },
91
- easeOutSine: function (x, t, b, c, d) {
92
- return c * Math.sin(t/d * (Math.PI/2)) + b;
93
- },
94
- easeInOutSine: function (x, t, b, c, d) {
95
- return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
96
- },
97
- easeInExpo: function (x, t, b, c, d) {
98
- return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
99
- },
100
- easeOutExpo: function (x, t, b, c, d) {
101
- return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
102
- },
103
- easeInOutExpo: function (x, t, b, c, d) {
104
- if (t==0) return b;
105
- if (t==d) return b+c;
106
- if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
107
- return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
108
- },
109
- easeInCirc: function (x, t, b, c, d) {
110
- return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
111
- },
112
- easeOutCirc: function (x, t, b, c, d) {
113
- return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
114
- },
115
- easeInOutCirc: function (x, t, b, c, d) {
116
- if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
117
- return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
118
- },
119
- easeInElastic: function (x, t, b, c, d) {
120
- var s=1.70158;var p=0;var a=c;
121
- if (t==0) return b; if ((t/=d)==1) return b+c; if (!p) p=d*.3;
122
- if (a < Math.abs(c)) { a=c; var s=p/4; }
123
- else var s = p/(2*Math.PI) * Math.asin (c/a);
124
- return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
125
- },
126
- easeOutElastic: function (x, t, b, c, d) {
127
- var s=1.70158;var p=0;var a=c;
128
- if (t==0) return b; if ((t/=d)==1) return b+c; if (!p) p=d*.3;
129
- if (a < Math.abs(c)) { a=c; var s=p/4; }
130
- else var s = p/(2*Math.PI) * Math.asin (c/a);
131
- return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
132
- },
133
- easeInOutElastic: function (x, t, b, c, d) {
134
- var s=1.70158;var p=0;var a=c;
135
- if (t==0) return b; if ((t/=d/2)==2) return b+c; if (!p) p=d*(.3*1.5);
136
- if (a < Math.abs(c)) { a=c; var s=p/4; }
137
- else var s = p/(2*Math.PI) * Math.asin (c/a);
138
- if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
139
- return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
140
- },
141
- easeInBack: function (x, t, b, c, d, s) {
142
- if (s == undefined) s = 1.70158;
143
- return c*(t/=d)*t*((s+1)*t - s) + b;
144
- },
145
- easeOutBack: function (x, t, b, c, d, s) {
146
- if (s == undefined) s = 1.70158;
147
- return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
148
- },
149
- easeInOutBack: function (x, t, b, c, d, s) {
150
- if (s == undefined) s = 1.70158;
151
- if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
152
- return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
153
- },
154
- easeInBounce: function (x, t, b, c, d) {
155
- return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b;
156
- },
157
- easeOutBounce: function (x, t, b, c, d) {
158
- if ((t/=d) < (1/2.75)) {
159
- return c*(7.5625*t*t) + b;
160
- } else if (t < (2/2.75)) {
161
- return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
162
- } else if (t < (2.5/2.75)) {
163
- return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
164
- } else {
165
- return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
166
- }
167
- },
168
- easeInOutBounce: function (x, t, b, c, d) {
169
- if (t < d/2) return jQuery.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b;
170
- return jQuery.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
171
- }
172
- });
173
-
@@ -1,178 +0,0 @@
1
- /*
2
- * jQuery JSON Plugin
3
- * version: 2.1 (2009-08-14)
4
- *
5
- * This document is licensed as free software under the terms of the
6
- * MIT License: http://www.opensource.org/licenses/mit-license.php
7
- *
8
- * Brantley Harris wrote this plugin. It is based somewhat on the JSON.org
9
- * website's http://www.json.org/json2.js, which proclaims:
10
- * "NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.", a sentiment that
11
- * I uphold.
12
- *
13
- * It is also influenced heavily by MochiKit's serializeJSON, which is
14
- * copyrighted 2005 by Bob Ippolito.
15
- */
16
-
17
- (function($) {
18
- /** jQuery.toJSON( json-serializble )
19
- Converts the given argument into a JSON respresentation.
20
-
21
- If an object has a "toJSON" function, that will be used to get the representation.
22
- Non-integer/string keys are skipped in the object, as are keys that point to a function.
23
-
24
- json-serializble:
25
- The *thing* to be converted.
26
- **/
27
- $.toJSON = function(o)
28
- {
29
- if (typeof(JSON) == 'object' && JSON.stringify)
30
- return JSON.stringify(o);
31
-
32
- var type = typeof(o);
33
-
34
- if (o === null)
35
- return "null";
36
-
37
- if (type == "undefined")
38
- return undefined;
39
-
40
- if (type == "number" || type == "boolean")
41
- return o + "";
42
-
43
- if (type == "string")
44
- return $.quoteString(o);
45
-
46
- if (type == 'object')
47
- {
48
- if (typeof o.toJSON == "function")
49
- return $.toJSON( o.toJSON() );
50
-
51
- if (o.constructor === Date)
52
- {
53
- var month = o.getUTCMonth() + 1;
54
- if (month < 10) month = '0' + month;
55
-
56
- var day = o.getUTCDate();
57
- if (day < 10) day = '0' + day;
58
-
59
- var year = o.getUTCFullYear();
60
-
61
- var hours = o.getUTCHours();
62
- if (hours < 10) hours = '0' + hours;
63
-
64
- var minutes = o.getUTCMinutes();
65
- if (minutes < 10) minutes = '0' + minutes;
66
-
67
- var seconds = o.getUTCSeconds();
68
- if (seconds < 10) seconds = '0' + seconds;
69
-
70
- var milli = o.getUTCMilliseconds();
71
- if (milli < 100) milli = '0' + milli;
72
- if (milli < 10) milli = '0' + milli;
73
-
74
- return '"' + year + '-' + month + '-' + day + 'T' +
75
- hours + ':' + minutes + ':' + seconds +
76
- '.' + milli + 'Z"';
77
- }
78
-
79
- if (o.constructor === Array)
80
- {
81
- var ret = [];
82
- for (var i = 0; i < o.length; i++)
83
- ret.push( $.toJSON(o[i]) || "null" );
84
-
85
- return "[" + ret.join(",") + "]";
86
- }
87
-
88
- var pairs = [];
89
- for (var k in o) {
90
- var name;
91
- var type = typeof k;
92
-
93
- if (type == "number")
94
- name = '"' + k + '"';
95
- else if (type == "string")
96
- name = $.quoteString(k);
97
- else
98
- continue; //skip non-string or number keys
99
-
100
- if (typeof o[k] == "function")
101
- continue; //skip pairs where the value is a function.
102
-
103
- var val = $.toJSON(o[k]);
104
-
105
- pairs.push(name + ":" + val);
106
- }
107
-
108
- return "{" + pairs.join(", ") + "}";
109
- }
110
- };
111
-
112
- /** jQuery.evalJSON(src)
113
- Evaluates a given piece of json source.
114
- **/
115
- $.evalJSON = function(src)
116
- {
117
- if (typeof(JSON) == 'object' && JSON.parse)
118
- return JSON.parse(src);
119
- return eval("(" + src + ")");
120
- };
121
-
122
- /** jQuery.secureEvalJSON(src)
123
- Evals JSON in a way that is *more* secure.
124
- **/
125
- $.secureEvalJSON = function(src)
126
- {
127
- if (typeof(JSON) == 'object' && JSON.parse)
128
- return JSON.parse(src);
129
-
130
- var filtered = src;
131
- filtered = filtered.replace(/\\["\\\/bfnrtu]/g, '@');
132
- filtered = filtered.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']');
133
- filtered = filtered.replace(/(?:^|:|,)(?:\s*\[)+/g, '');
134
-
135
- if (/^[\],:{}\s]*$/.test(filtered))
136
- return eval("(" + src + ")");
137
- else
138
- throw new SyntaxError("Error parsing JSON, source is not valid.");
139
- };
140
-
141
- /** jQuery.quoteString(string)
142
- Returns a string-repr of a string, escaping quotes intelligently.
143
- Mostly a support function for toJSON.
144
-
145
- Examples:
146
- >>> jQuery.quoteString("apple")
147
- "apple"
148
-
149
- >>> jQuery.quoteString('"Where are we going?", she asked.')
150
- "\"Where are we going?\", she asked."
151
- **/
152
- $.quoteString = function(string)
153
- {
154
- if (string.match(_escapeable))
155
- {
156
- return '"' + string.replace(_escapeable, function (a)
157
- {
158
- var c = _meta[a];
159
- if (typeof c === 'string') return c;
160
- c = a.charCodeAt();
161
- return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16);
162
- }) + '"';
163
- }
164
- return '"' + string + '"';
165
- };
166
-
167
- var _escapeable = /["\\\x00-\x1f\x7f-\x9f]/g;
168
-
169
- var _meta = {
170
- '\b': '\\b',
171
- '\t': '\\t',
172
- '\n': '\\n',
173
- '\f': '\\f',
174
- '\r': '\\r',
175
- '"' : '\\"',
176
- '\\': '\\\\'
177
- };
178
- })(jQuery);
@@ -1,16 +0,0 @@
1
- $.fn.serializeObject = function()
2
- {
3
- var o = {};
4
- var a = this.serializeArray();
5
- $.each(a, function() {
6
- if (o[this.name] !== undefined) {
7
- if (!o[this.name].push) {
8
- o[this.name] = [o[this.name]];
9
- }
10
- o[this.name].push(this.value || '');
11
- } else {
12
- o[this.name] = this.value || '';
13
- }
14
- });
15
- return o;
16
- };
@@ -1,289 +0,0 @@
1
- /**
2
- * Unobtrusive scripting adapter for jQuery
3
- *
4
- * Requires jQuery 1.4.3 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($) {
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]',
53
-
54
- // Form elements bound by jquery-ujs
55
- formSubmitSelector: 'form',
56
-
57
- // Form input elements bound by jquery-ujs
58
- formInputClickSelector: 'form input[type=submit], form input[type=image], form button[type=submit], form button:not([type])',
59
-
60
- // Form input elements disabled during form submission
61
- disableSelector: 'input[data-disable-with], button[data-disable-with], textarea[data-disable-with]',
62
-
63
- // Form input elements re-enabled after form submission
64
- enableSelector: 'input[data-disable-with]:disabled, button[data-disable-with]:disabled, textarea[data-disable-with]:disabled',
65
-
66
- // Form required input elements
67
- requiredInputSelector: 'input[name][required],textarea[name][required]',
68
-
69
- // Form file input elements
70
- fileInputSelector: 'input:file',
71
-
72
- // Make sure that every Ajax request sends the CSRF token
73
- CSRFProtection: function(xhr) {
74
- var token = $('meta[name="csrf-token"]').attr('content');
75
- if (token) xhr.setRequestHeader('X-CSRF-Token', token);
76
- },
77
-
78
- // Triggers an event on an element and returns false if the event result is false
79
- fire: function(obj, name, data) {
80
- var event = $.Event(name);
81
- obj.trigger(event, data);
82
- return event.result !== false;
83
- },
84
-
85
- // Submits "remote" forms and links with ajax
86
- handleRemote: function(element) {
87
- var method, url, data,
88
- dataType = element.data('type') || ($.ajaxSettings && $.ajaxSettings.dataType);
89
-
90
- if (rails.fire(element, 'ajax:before')) {
91
-
92
- if (element.is('form')) {
93
- method = element.attr('method');
94
- url = element.attr('action');
95
- data = element.serializeArray();
96
- // memoized value from clicked submit button
97
- var button = element.data('ujs:submit-button');
98
- if (button) {
99
- data.push(button);
100
- element.data('ujs:submit-button', null);
101
- }
102
- } else {
103
- method = element.data('method');
104
- url = element.attr('href');
105
- data = null;
106
- }
107
-
108
- $.ajax({
109
- url: url, type: method || 'GET', data: data, dataType: dataType,
110
- // stopping the "ajax:beforeSend" event will cancel the ajax request
111
- beforeSend: function(xhr, settings) {
112
- if (settings.dataType === undefined) {
113
- xhr.setRequestHeader('accept', '*/*;q=0.5, ' + settings.accepts.script);
114
- }
115
- return rails.fire(element, 'ajax:beforeSend', [xhr, settings]);
116
- },
117
- success: function(data, status, xhr) {
118
- element.trigger('ajax:success', [data, status, xhr]);
119
- },
120
- complete: function(xhr, status) {
121
- element.trigger('ajax:complete', [xhr, status]);
122
- },
123
- error: function(xhr, status, error) {
124
- element.trigger('ajax:error', [xhr, status, error]);
125
- }
126
- });
127
- }
128
- },
129
-
130
- // Handles "data-method" on links such as:
131
- // <a href="/users/5" data-method="delete" rel="nofollow" data-confirm="Are you sure?">Delete</a>
132
- handleMethod: function(link) {
133
- var href = link.attr('href'),
134
- method = link.data('method'),
135
- csrf_token = $('meta[name=csrf-token]').attr('content'),
136
- csrf_param = $('meta[name=csrf-param]').attr('content'),
137
- form = $('<form method="post" action="' + href + '"></form>'),
138
- metadata_input = '<input name="_method" value="' + method + '" type="hidden" />';
139
-
140
- if (csrf_param !== undefined && csrf_token !== undefined) {
141
- metadata_input += '<input name="' + csrf_param + '" value="' + csrf_token + '" type="hidden" />';
142
- }
143
-
144
- form.hide().append(metadata_input).appendTo('body');
145
- form.submit();
146
- },
147
-
148
- /* Disables form elements:
149
- - Caches element value in 'ujs:enable-with' data store
150
- - Replaces element text with value of 'data-disable-with' attribute
151
- - Adds disabled=disabled attribute
152
- */
153
- disableFormElements: function(form) {
154
- form.find(rails.disableSelector).each(function() {
155
- var element = $(this), method = element.is('button') ? 'html' : 'val';
156
- element.data('ujs:enable-with', element[method]());
157
- element[method](element.data('disable-with'));
158
- element.attr('disabled', 'disabled');
159
- });
160
- },
161
-
162
- /* Re-enables disabled form elements:
163
- - Replaces element text with cached value from 'ujs:enable-with' data store (created in `disableFormElements`)
164
- - Removes disabled attribute
165
- */
166
- enableFormElements: function(form) {
167
- form.find(rails.enableSelector).each(function() {
168
- var element = $(this), method = element.is('button') ? 'html' : 'val';
169
- if (element.data('ujs:enable-with')) element[method](element.data('ujs:enable-with'));
170
- element.removeAttr('disabled');
171
- });
172
- },
173
-
174
- // If message provided in 'data-confirm' attribute, fires `confirm` event and returns result of confirm dialog.
175
- // Attaching a handler to the element's `confirm` event that returns false cancels the confirm dialog.
176
- allowAction: function(element) {
177
- var message = element.data('confirm');
178
- return !message || (rails.fire(element, 'confirm') && confirm(message));
179
- },
180
-
181
- // Helper function which checks for blank inputs in a form that match the specified CSS selector
182
- blankInputs: function(form, specifiedSelector, nonBlank) {
183
- var inputs = $(), input,
184
- selector = specifiedSelector || 'input,textarea';
185
- form.find(selector).each(function() {
186
- input = $(this);
187
- // Collect non-blank inputs if nonBlank option is true, otherwise, collect blank inputs
188
- if (nonBlank ? input.val() : !input.val()) {
189
- inputs = inputs.add(input);
190
- }
191
- });
192
- return inputs.length ? inputs : false;
193
- },
194
-
195
- // Helper function which checks for non-blank inputs in a form that match the specified CSS selector
196
- nonBlankInputs: function(form, specifiedSelector) {
197
- return rails.blankInputs(form, specifiedSelector, true); // true specifies nonBlank
198
- },
199
-
200
- // Helper function, needed to provide consistent behavior in IE
201
- stopEverything: function(e) {
202
- e.stopImmediatePropagation();
203
- return false;
204
- },
205
-
206
- // find all the submit events directly bound to the form and
207
- // manually invoke them. If anyone returns false then stop the loop
208
- callFormSubmitBindings: function(form) {
209
- var events = form.data('events'), continuePropagation = true;
210
- if (events !== undefined && events['submit'] !== undefined) {
211
- $.each(events['submit'], function(i, obj){
212
- if (typeof obj.handler === 'function') return continuePropagation = obj.handler(obj.data);
213
- });
214
- }
215
- return continuePropagation;
216
- }
217
- };
218
-
219
- // ajaxPrefilter is a jQuery 1.5 feature
220
- if ('ajaxPrefilter' in $) {
221
- $.ajaxPrefilter(function(options, originalOptions, xhr){ rails.CSRFProtection(xhr); });
222
- } else {
223
- $(document).ajaxSend(function(e, xhr){ rails.CSRFProtection(xhr); });
224
- }
225
-
226
- $(rails.linkClickSelector).live('click.rails', function(e) {
227
- var link = $(this);
228
- if (!rails.allowAction(link)) return rails.stopEverything(e);
229
-
230
- if (link.data('remote') !== undefined) {
231
- rails.handleRemote(link);
232
- return false;
233
- } else if (link.data('method')) {
234
- rails.handleMethod(link);
235
- return false;
236
- }
237
- });
238
-
239
- $(rails.formSubmitSelector).live('submit.rails', function(e) {
240
- var form = $(this),
241
- remote = form.data('remote') !== undefined,
242
- blankRequiredInputs = rails.blankInputs(form, rails.requiredInputSelector),
243
- nonBlankFileInputs = rails.nonBlankInputs(form, rails.fileInputSelector);
244
-
245
- if (!rails.allowAction(form)) return rails.stopEverything(e);
246
-
247
- // skip other logic when required values are missing or file upload is present
248
- if (blankRequiredInputs && rails.fire(form, 'ajax:aborted:required', [blankRequiredInputs])) {
249
- return !remote;
250
- }
251
-
252
- if (remote) {
253
- if (nonBlankFileInputs) {
254
- return rails.fire(form, 'ajax:aborted:file', [nonBlankFileInputs]);
255
- }
256
-
257
- // If browser does not support submit bubbling, then this live-binding will be called before direct
258
- // bindings. Therefore, we should directly call any direct bindings before remotely submitting form.
259
- if (!$.support.submitBubbles && rails.callFormSubmitBindings(form) === false) return rails.stopEverything(e);
260
-
261
- rails.handleRemote(form);
262
- return false;
263
- } else {
264
- // slight timeout so that the submit button gets properly serialized
265
- setTimeout(function(){ rails.disableFormElements(form); }, 13);
266
- }
267
- });
268
-
269
- $(rails.formInputClickSelector).live('click.rails', function(event) {
270
- var button = $(this);
271
-
272
- if (!rails.allowAction(button)) return rails.stopEverything(event);
273
-
274
- // register the pressed submit button
275
- var name = button.attr('name'),
276
- data = name ? {name:name, value:button.val()} : null;
277
-
278
- button.closest('form').data('ujs:submit-button', data);
279
- });
280
-
281
- $(rails.formSubmitSelector).live('ajax:beforeSend.rails', function(event) {
282
- if (this == event.target) rails.disableFormElements($(this));
283
- });
284
-
285
- $(rails.formSubmitSelector).live('ajax:complete.rails', function(event) {
286
- if (this == event.target) rails.enableFormElements($(this));
287
- });
288
-
289
- })( jQuery );