amber-rails 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. data/.gitignore +2 -0
  2. data/.rvmrc +4 -0
  3. data/CHANGELOG.md +3 -0
  4. data/Gemfile +2 -0
  5. data/Gemfile.lock +96 -0
  6. data/LICENSE +7 -0
  7. data/README.md +13 -0
  8. data/Rakefile +15 -0
  9. data/amber-rails.gemspec +21 -0
  10. data/app/assets/.DS_Store +0 -0
  11. data/app/assets/javascripts/.DS_Store +0 -0
  12. data/app/assets/javascripts/codemirror/codemirror.js +2144 -0
  13. data/app/assets/javascripts/codemirror/overlay.js +51 -0
  14. data/app/assets/javascripts/codemirror/runmode.js +27 -0
  15. data/app/assets/javascripts/codemirror/smalltalk/index.html +56 -0
  16. data/app/assets/javascripts/codemirror/smalltalk/smalltalk.js +134 -0
  17. data/app/assets/javascripts/html5.js +35 -0
  18. data/app/assets/javascripts/jquery.js +8981 -0
  19. data/app/assets/javascripts/jquery.textarea.js +267 -0
  20. data/app/assets/javascripts/jquery.tmpl.js +503 -0
  21. data/app/assets/javascripts/jquery.ui.js +272 -0
  22. data/app/assets/javascripts/json2.js +481 -0
  23. data/app/assets/javascripts/jtalk-development.js.coffee +4 -0
  24. data/app/assets/javascripts/jtalk-todos.js.coffee +4 -0
  25. data/app/assets/javascripts/jtalk.js.coffee +16 -0
  26. data/app/assets/javascripts/jtalk_core/boot.js +466 -0
  27. data/app/assets/javascripts/jtalk_core/st/.DS_Store +0 -0
  28. data/app/assets/javascripts/jtalk_core/st/Benchfib.js +159 -0
  29. data/app/assets/javascripts/jtalk_core/st/Canvas.js +1479 -0
  30. data/app/assets/javascripts/jtalk_core/st/Compiler.js +1587 -0
  31. data/app/assets/javascripts/jtalk_core/st/Examples.js +863 -0
  32. data/app/assets/javascripts/jtalk_core/st/HTML5.js +54 -0
  33. data/app/assets/javascripts/jtalk_core/st/IDE.js +3457 -0
  34. data/app/assets/javascripts/jtalk_core/st/JQuery.js +898 -0
  35. data/app/assets/javascripts/jtalk_core/st/Kernel.js +6761 -0
  36. data/app/assets/javascripts/jtalk_core/st/Parser.js +1655 -0
  37. data/app/assets/javascripts/jtalk_core/st/SUnit.js +1059 -0
  38. data/app/assets/stylesheets/codemirror.css +67 -0
  39. data/app/assets/stylesheets/codemirror_jtalk.css +21 -0
  40. data/app/assets/stylesheets/jtalk.css +369 -0
  41. data/app/assets/stylesheets/sunit.css +65 -0
  42. data/config/routes.rb +10 -0
  43. data/examples/todos/.gitignore +5 -0
  44. data/examples/todos/.rvmrc +4 -0
  45. data/examples/todos/Gemfile +12 -0
  46. data/examples/todos/Gemfile.lock +126 -0
  47. data/examples/todos/README +261 -0
  48. data/examples/todos/Rakefile +15 -0
  49. data/examples/todos/app/assets/images/rails.png +0 -0
  50. data/examples/todos/app/assets/javascripts/application.js.coffee +5 -0
  51. data/examples/todos/app/assets/javascripts/jtalk/Examples.deploy.js +863 -0
  52. data/examples/todos/app/assets/javascripts/jtalk/Examples.js +863 -0
  53. data/examples/todos/app/assets/smalltalk/Examples.st +397 -0
  54. data/examples/todos/app/assets/stylesheets/application.css +8 -0
  55. data/examples/todos/app/assets/stylesheets/jtalk-todos.css +154 -0
  56. data/examples/todos/app/controllers/application_controller.rb +3 -0
  57. data/examples/todos/app/controllers/todos_controller.rb +5 -0
  58. data/examples/todos/app/helpers/application_helper.rb +2 -0
  59. data/examples/todos/app/models/.gitkeep +0 -0
  60. data/examples/todos/app/views/todos/index.html.haml +16 -0
  61. data/examples/todos/config.ru +4 -0
  62. data/examples/todos/config/application.rb +31 -0
  63. data/examples/todos/config/boot.rb +6 -0
  64. data/examples/todos/config/database.yml +25 -0
  65. data/examples/todos/config/environment.rb +5 -0
  66. data/examples/todos/config/environments/development.rb +27 -0
  67. data/examples/todos/config/environments/production.rb +60 -0
  68. data/examples/todos/config/environments/test.rb +42 -0
  69. data/examples/todos/config/initializers/backtrace_silencers.rb +7 -0
  70. data/examples/todos/config/initializers/inflections.rb +10 -0
  71. data/examples/todos/config/initializers/mime_types.rb +5 -0
  72. data/examples/todos/config/initializers/secret_token.rb +7 -0
  73. data/examples/todos/config/initializers/session_store.rb +8 -0
  74. data/examples/todos/config/initializers/wrap_parameters.rb +14 -0
  75. data/examples/todos/config/locales/en.yml +5 -0
  76. data/examples/todos/config/routes.rb +3 -0
  77. data/examples/todos/log/.gitkeep +0 -0
  78. data/examples/todos/public/404.html +26 -0
  79. data/examples/todos/public/422.html +26 -0
  80. data/examples/todos/public/500.html +26 -0
  81. data/examples/todos/public/favicon.ico +0 -0
  82. data/examples/todos/public/robots.txt +5 -0
  83. data/examples/todos/script/rails +6 -0
  84. data/examples/todos/vendor/assets/stylesheets/.gitkeep +0 -0
  85. data/examples/todos/vendor/plugins/.gitkeep +0 -0
  86. data/lib/amber-rails.rb +1 -0
  87. data/lib/amber/rails.rb +30 -0
  88. data/lib/amber/rails/engine.rb +12 -0
  89. data/lib/amber/rails/version.rb +5 -0
  90. metadata +167 -0
@@ -0,0 +1,267 @@
1
+ /*
2
+ * Tabby jQuery plugin version 0.12
3
+ *
4
+ * Ted Devito - http://teddevito.com/demos/textarea.html
5
+ *
6
+ * Copyright (c) 2009 Ted Devito
7
+ *
8
+ * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
9
+ * conditions are met:
10
+ *
11
+ * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
12
+ * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
13
+ * in the documentation and/or other materials provided with the distribution.
14
+ * 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written
15
+ * permission.
16
+ *
17
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
19
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
22
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
+ *
24
+ */
25
+
26
+ // create closure
27
+
28
+ (function($) {
29
+
30
+ // plugin definition
31
+
32
+ $.fn.tabby = function(options) {
33
+ //debug(this);
34
+ // build main options before element iteration
35
+ var opts = $.extend({}, $.fn.tabby.defaults, options);
36
+ var pressed = $.fn.tabby.pressed;
37
+
38
+ // iterate and reformat each matched element
39
+ return this.each(function() {
40
+ $this = $(this);
41
+
42
+ // build element specific options
43
+ var options = $.meta ? $.extend({}, opts, $this.data()) : opts;
44
+
45
+ $this.bind('keydown',function (e) {
46
+ var kc = $.fn.tabby.catch_kc(e);
47
+ if (16 == kc) pressed.shft = true;
48
+ /*
49
+ because both CTRL+TAB and ALT+TAB default to an event (changing tab/window) that
50
+ will prevent js from capturing the keyup event, we'll set a timer on releasing them.
51
+ */
52
+ if (17 == kc) {pressed.ctrl = true; setTimeout("$.fn.tabby.pressed.ctrl = false;",1000);}
53
+ if (18 == kc) {pressed.alt = true; setTimeout("$.fn.tabby.pressed.alt = false;",1000);}
54
+
55
+ if (9 == kc && !pressed.ctrl && !pressed.alt) {
56
+ e.preventDefault; // does not work in O9.63 ??
57
+ pressed.last = kc; setTimeout("$.fn.tabby.pressed.last = null;",0);
58
+ process_keypress ($(e.target).get(0), pressed.shft, options);
59
+ return false;
60
+ }
61
+
62
+ }).bind('keyup',function (e) {
63
+ if (16 == $.fn.tabby.catch_kc(e)) pressed.shft = false;
64
+ }).bind('blur',function (e) { // workaround for Opera -- http://www.webdeveloper.com/forum/showthread.php?p=806588
65
+ if (9 == pressed.last) $(e.target).one('focus',function (e) {pressed.last = null;}).get(0).focus();
66
+ });
67
+
68
+ });
69
+ };
70
+
71
+ // define and expose any extra methods
72
+ $.fn.tabby.catch_kc = function(e) { return e.keyCode ? e.keyCode : e.charCode ? e.charCode : e.which; };
73
+ $.fn.tabby.pressed = {shft : false, ctrl : false, alt : false, last: null};
74
+
75
+ // private function for debugging
76
+ function debug($obj) {
77
+ if (window.console && window.console.log)
78
+ window.console.log('textarea count: ' + $obj.size());
79
+ };
80
+
81
+ function process_keypress (o,shft,options) {
82
+ var scrollTo = o.scrollTop;
83
+ //var tabString = String.fromCharCode(9);
84
+
85
+ // gecko; o.setSelectionRange is only available when the text box has focus
86
+ if (o.setSelectionRange) gecko_tab (o, shft, options);
87
+
88
+ // ie; document.selection is always available
89
+ else if (document.selection) ie_tab (o, shft, options);
90
+
91
+ o.scrollTop = scrollTo;
92
+ }
93
+
94
+ // plugin defaults
95
+ $.fn.tabby.defaults = {tabString : String.fromCharCode(9)};
96
+
97
+ function gecko_tab (o, shft, options) {
98
+ var ss = o.selectionStart;
99
+ var es = o.selectionEnd;
100
+
101
+ // when there's no selection and we're just working with the caret, we'll add/remove the tabs at the caret, providing more control
102
+ if(ss == es) {
103
+ // SHIFT+TAB
104
+ if (shft) {
105
+ // check to the left of the caret first
106
+ if ("\t" == o.value.substring(ss-options.tabString.length, ss)) {
107
+ o.value = o.value.substring(0, ss-options.tabString.length) + o.value.substring(ss); // put it back together omitting one character to the left
108
+ o.focus();
109
+ o.setSelectionRange(ss - options.tabString.length, ss - options.tabString.length);
110
+ }
111
+ // then check to the right of the caret
112
+ else if ("\t" == o.value.substring(ss, ss + options.tabString.length)) {
113
+ o.value = o.value.substring(0, ss) + o.value.substring(ss + options.tabString.length); // put it back together omitting one character to the right
114
+ o.focus();
115
+ o.setSelectionRange(ss,ss);
116
+ }
117
+ }
118
+ // TAB
119
+ else {
120
+ o.value = o.value.substring(0, ss) + options.tabString + o.value.substring(ss);
121
+ o.focus();
122
+ o.setSelectionRange(ss + options.tabString.length, ss + options.tabString.length);
123
+ }
124
+ }
125
+ // selections will always add/remove tabs from the start of the line
126
+ else {
127
+ // split the textarea up into lines and figure out which lines are included in the selection
128
+ var lines = o.value.split("\n");
129
+ var indices = new Array();
130
+ var sl = 0; // start of the line
131
+ var el = 0; // end of the line
132
+ var sel = false;
133
+ for (var i in lines) {
134
+ el = sl + lines[i].length;
135
+ indices.push({start: sl, end: el, selected: (sl <= ss && el > ss) || (el >= es && sl < es) || (sl > ss && el < es)});
136
+ sl = el + 1;// for "\n"
137
+ }
138
+
139
+ // walk through the array of lines (indices) and add tabs where appropriate
140
+ var modifier = 0;
141
+ for (var i in indices) {
142
+ if (indices[i].selected) {
143
+ var pos = indices[i].start + modifier; // adjust for tabs already inserted/removed
144
+ // SHIFT+TAB
145
+ if (shft && options.tabString == o.value.substring(pos,pos+options.tabString.length)) { // only SHIFT+TAB if there's a tab at the start of the line
146
+ o.value = o.value.substring(0,pos) + o.value.substring(pos + options.tabString.length); // omit the tabstring to the right
147
+ modifier -= options.tabString.length;
148
+ }
149
+ // TAB
150
+ else if (!shft) {
151
+ o.value = o.value.substring(0,pos) + options.tabString + o.value.substring(pos); // insert the tabstring
152
+ modifier += options.tabString.length;
153
+ }
154
+ }
155
+ }
156
+ o.focus();
157
+ var ns = ss + ((modifier > 0) ? options.tabString.length : (modifier < 0) ? -options.tabString.length : 0);
158
+ var ne = es + modifier;
159
+ o.setSelectionRange(ns,ne);
160
+ }
161
+ }
162
+
163
+ function ie_tab (o, shft, options) {
164
+ var range = document.selection.createRange();
165
+
166
+ if (o == range.parentElement()) {
167
+ // when there's no selection and we're just working with the caret, we'll add/remove the tabs at the caret, providing more control
168
+ if ('' == range.text) {
169
+ // SHIFT+TAB
170
+ if (shft) {
171
+ var bookmark = range.getBookmark();
172
+ //first try to the left by moving opening up our empty range to the left
173
+ range.moveStart('character', -options.tabString.length);
174
+ if (options.tabString == range.text) {
175
+ range.text = '';
176
+ } else {
177
+ // if that didn't work then reset the range and try opening it to the right
178
+ range.moveToBookmark(bookmark);
179
+ range.moveEnd('character', options.tabString.length);
180
+ if (options.tabString == range.text)
181
+ range.text = '';
182
+ }
183
+ // move the pointer to the start of them empty range and select it
184
+ range.collapse(true);
185
+ range.select();
186
+ }
187
+
188
+ else {
189
+ // very simple here. just insert the tab into the range and put the pointer at the end
190
+ range.text = options.tabString;
191
+ range.collapse(false);
192
+ range.select();
193
+ }
194
+ }
195
+ // selections will always add/remove tabs from the start of the line
196
+ else {
197
+
198
+ var selection_text = range.text;
199
+ var selection_len = selection_text.length;
200
+ var selection_arr = selection_text.split("\r\n");
201
+
202
+ var before_range = document.body.createTextRange();
203
+ before_range.moveToElementText(o);
204
+ before_range.setEndPoint("EndToStart", range);
205
+ var before_text = before_range.text;
206
+ var before_arr = before_text.split("\r\n");
207
+ var before_len = before_text.length; // - before_arr.length + 1;
208
+
209
+ var after_range = document.body.createTextRange();
210
+ after_range.moveToElementText(o);
211
+ after_range.setEndPoint("StartToEnd", range);
212
+ var after_text = after_range.text; // we can accurately calculate distance to the end because we're not worried about MSIE trimming a \r\n
213
+
214
+ var end_range = document.body.createTextRange();
215
+ end_range.moveToElementText(o);
216
+ end_range.setEndPoint("StartToEnd", before_range);
217
+ var end_text = end_range.text; // we can accurately calculate distance to the end because we're not worried about MSIE trimming a \r\n
218
+
219
+ var check_html = $(o).html();
220
+ $("#r3").text(before_len + " + " + selection_len + " + " + after_text.length + " = " + check_html.length);
221
+ if((before_len + end_text.length) < check_html.length) {
222
+ before_arr.push("");
223
+ before_len += 2; // for the \r\n that was trimmed
224
+ if (shft && options.tabString == selection_arr[0].substring(0,options.tabString.length))
225
+ selection_arr[0] = selection_arr[0].substring(options.tabString.length);
226
+ else if (!shft) selection_arr[0] = options.tabString + selection_arr[0];
227
+ } else {
228
+ if (shft && options.tabString == before_arr[before_arr.length-1].substring(0,options.tabString.length))
229
+ before_arr[before_arr.length-1] = before_arr[before_arr.length-1].substring(options.tabString.length);
230
+ else if (!shft) before_arr[before_arr.length-1] = options.tabString + before_arr[before_arr.length-1];
231
+ }
232
+
233
+ for (var i = 1; i < selection_arr.length; i++) {
234
+ if (shft && options.tabString == selection_arr[i].substring(0,options.tabString.length))
235
+ selection_arr[i] = selection_arr[i].substring(options.tabString.length);
236
+ else if (!shft) selection_arr[i] = options.tabString + selection_arr[i];
237
+ }
238
+
239
+ if (1 == before_arr.length && 0 == before_len) {
240
+ if (shft && options.tabString == selection_arr[0].substring(0,options.tabString.length))
241
+ selection_arr[0] = selection_arr[0].substring(options.tabString.length);
242
+ else if (!shft) selection_arr[0] = options.tabString + selection_arr[0];
243
+ }
244
+
245
+ if ((before_len + selection_len + after_text.length) < check_html.length) {
246
+ selection_arr.push("");
247
+ selection_len += 2; // for the \r\n that was trimmed
248
+ }
249
+
250
+ before_range.text = before_arr.join("\r\n");
251
+ range.text = selection_arr.join("\r\n");
252
+
253
+ var new_range = document.body.createTextRange();
254
+ new_range.moveToElementText(o);
255
+
256
+ if (0 < before_len) new_range.setEndPoint("StartToEnd", before_range);
257
+ else new_range.setEndPoint("StartToStart", before_range);
258
+ new_range.setEndPoint("EndToEnd", range);
259
+
260
+ new_range.select();
261
+
262
+ }
263
+ }
264
+ }
265
+
266
+ // end of closure
267
+ })(jQuery);
@@ -0,0 +1,503 @@
1
+ /*!
2
+ * jQuery Templates Plugin
3
+ * http://github.com/jquery/jquery-tmpl
4
+ *
5
+ * Copyright Software Freedom Conservancy, Inc.
6
+ * Dual licensed under the MIT or GPL Version 2 licenses.
7
+ * http://jquery.org/license
8
+ */
9
+ (function( jQuery, undefined ){
10
+ var oldManip = jQuery.fn.domManip, tmplItmAtt = "_tmplitem", htmlExpr = /^[^<]*(<[\w\W]+>)[^>]*$|\{\{\! /,
11
+ newTmplItems = {}, wrappedItems = {}, appendToTmplItems, topTmplItem = { key: 0, data: {} }, itemKey = 0, cloneIndex = 0, stack = [];
12
+
13
+ function newTmplItem( options, parentItem, fn, data ) {
14
+ // Returns a template item data structure for a new rendered instance of a template (a 'template item').
15
+ // The content field is a hierarchical array of strings and nested items (to be
16
+ // removed and replaced by nodes field of dom elements, once inserted in DOM).
17
+ var newItem = {
18
+ data: data || (parentItem ? parentItem.data : {}),
19
+ _wrap: parentItem ? parentItem._wrap : null,
20
+ tmpl: null,
21
+ parent: parentItem || null,
22
+ nodes: [],
23
+ calls: tiCalls,
24
+ nest: tiNest,
25
+ wrap: tiWrap,
26
+ html: tiHtml,
27
+ update: tiUpdate
28
+ };
29
+ if ( options ) {
30
+ jQuery.extend( newItem, options, { nodes: [], parent: parentItem } );
31
+ }
32
+ if ( fn ) {
33
+ // Build the hierarchical content to be used during insertion into DOM
34
+ newItem.tmpl = fn;
35
+ newItem._ctnt = newItem._ctnt || newItem.tmpl( jQuery, newItem );
36
+ newItem.key = ++itemKey;
37
+ // Keep track of new template item, until it is stored as jQuery Data on DOM element
38
+ (stack.length ? wrappedItems : newTmplItems)[itemKey] = newItem;
39
+ }
40
+ return newItem;
41
+ }
42
+
43
+ // Override appendTo etc., in order to provide support for targeting multiple elements. (This code would disappear if integrated in jquery core).
44
+ jQuery.each({
45
+ appendTo: "append",
46
+ prependTo: "prepend",
47
+ insertBefore: "before",
48
+ insertAfter: "after",
49
+ replaceAll: "replaceWith"
50
+ }, function( name, original ) {
51
+ jQuery.fn[ name ] = function( selector ) {
52
+ var ret = [], insert = jQuery( selector ), elems, i, l, tmplItems,
53
+ parent = this.length === 1 && this[0].parentNode;
54
+
55
+ appendToTmplItems = newTmplItems || {};
56
+ if ( parent && parent.nodeType === 11 && parent.childNodes.length === 1 && insert.length === 1 ) {
57
+ insert[ original ]( this[0] );
58
+ ret = this;
59
+ } else {
60
+ for ( i = 0, l = insert.length; i < l; i++ ) {
61
+ cloneIndex = i;
62
+ elems = (i > 0 ? this.clone(true) : this).get();
63
+ jQuery.fn[ original ].apply( jQuery(insert[i]), elems );
64
+ ret = ret.concat( elems );
65
+ }
66
+ cloneIndex = 0;
67
+ ret = this.pushStack( ret, name, insert.selector );
68
+ }
69
+ tmplItems = appendToTmplItems;
70
+ appendToTmplItems = null;
71
+ jQuery.tmpl.complete( tmplItems );
72
+ return ret;
73
+ };
74
+ });
75
+
76
+ jQuery.fn.extend({
77
+ // Use first wrapped element as template markup.
78
+ // Return wrapped set of template items, obtained by rendering template against data.
79
+ tmpl: function( data, options, parentItem ) {
80
+ return jQuery.tmpl( this[0], data, options, parentItem );
81
+ },
82
+
83
+ // Find which rendered template item the first wrapped DOM element belongs to
84
+ tmplItem: function() {
85
+ return jQuery.tmplItem( this[0] );
86
+ },
87
+
88
+ tmplElement: function() {
89
+ return jQuery.tmplElement( this[0] );
90
+ },
91
+
92
+ // Consider the first wrapped element as a template declaration, and get the compiled template or store it as a named template.
93
+ template: function( name ) {
94
+ return jQuery.template( name, this[0] );
95
+ },
96
+
97
+ domManip: function( args, table, callback, options ) {
98
+ // This appears to be a bug in the appendTo, etc. implementation
99
+ // it should be doing .call() instead of .apply(). See #6227
100
+ if ( args[0] && args[0].nodeType ) {
101
+ var dmArgs = jQuery.makeArray( arguments ), argsLength = args.length, i = 0, tmplItem;
102
+ while ( i < argsLength && !(tmplItem = jQuery.data( args[i++], "tmplItem" ))) {}
103
+ if ( argsLength > 1 ) {
104
+ dmArgs[0] = [jQuery.makeArray( args )];
105
+ }
106
+ if ( tmplItem && cloneIndex ) {
107
+ dmArgs[2] = function( fragClone ) {
108
+ // Handler called by oldManip when rendered template has been inserted into DOM.
109
+ jQuery.tmpl.afterManip( this, fragClone, callback );
110
+ };
111
+ }
112
+ oldManip.apply( this, dmArgs );
113
+ } else {
114
+ oldManip.apply( this, arguments );
115
+ }
116
+ cloneIndex = 0;
117
+ if ( !appendToTmplItems ) {
118
+ jQuery.tmpl.complete( newTmplItems );
119
+ }
120
+ return this;
121
+ }
122
+ });
123
+
124
+ jQuery.extend({
125
+ // Return wrapped set of template items, obtained by rendering template against data.
126
+ tmpl: function( tmpl, data, options, parentItem ) {
127
+ var ret, topLevel = !parentItem;
128
+ if ( topLevel ) {
129
+ // This is a top-level tmpl call (not from a nested template using {{tmpl}})
130
+ parentItem = topTmplItem;
131
+ if ( typeof tmpl != "function" )
132
+ tmpl = jQuery.template[tmpl] || jQuery.template( null, tmpl );
133
+ wrappedItems = {}; // Any wrapped items will be rebuilt, since this is top level
134
+ } else if ( !tmpl ) {
135
+ // The template item is already associated with DOM - this is a refresh.
136
+ // Re-evaluate rendered template for the parentItem
137
+ tmpl = parentItem.tmpl;
138
+ newTmplItems[parentItem.key] = parentItem;
139
+ parentItem.nodes = [];
140
+ if ( parentItem.wrapped ) {
141
+ updateWrapped( parentItem, parentItem.wrapped );
142
+ }
143
+ // Rebuild, without creating a new template item
144
+ return jQuery( build( parentItem, null, parentItem.tmpl( jQuery, parentItem ) ));
145
+ }
146
+ if ( !tmpl ) {
147
+ return []; // Could throw...
148
+ }
149
+ if ( typeof data === "function" ) {
150
+ data = data.call( parentItem || {} );
151
+ }
152
+ if ( options && options.wrapped ) {
153
+ updateWrapped( options, options.wrapped );
154
+ }
155
+ ret = jQuery.isArray( data ) ?
156
+ jQuery.map( data, function( dataItem ) {
157
+ return dataItem ? newTmplItem( options, parentItem, tmpl, dataItem ) : null;
158
+ }) :
159
+ [ newTmplItem( options, parentItem, tmpl, data ) ];
160
+ return topLevel ? jQuery( build( parentItem, null, ret ) ) : ret;
161
+ },
162
+
163
+ // Return rendered template item for an element.
164
+ tmplItem: function( elem ) {
165
+ var tmplItem;
166
+ if ( elem instanceof jQuery ) {
167
+ elem = elem[0];
168
+ }
169
+ while ( elem && elem.nodeType === 1 && !(tmplItem = jQuery.data( elem, "tmplItem" )) && (elem = elem.parentNode) ) {}
170
+ return tmplItem || topTmplItem;
171
+ },
172
+
173
+ tmplElement: function( elem ) {
174
+ var tmplItem;
175
+ if ( elem instanceof jQuery ) {
176
+ elem = elem[0];
177
+ }
178
+ while ( elem && elem.nodeType === 1 && !jQuery.data( elem, "tmplItem" ) && (elem = elem.parentNode) ) {}
179
+ return elem;
180
+ },
181
+
182
+ // Set:
183
+ // Use $.template( name, tmpl ) to cache a named template,
184
+ // where tmpl is a template string, a script element or a jQuery instance wrapping a script element, etc.
185
+ // Use $( "selector" ).template( name ) to provide access by name to a script block template declaration.
186
+
187
+ // Get:
188
+ // Use $.template( name ) to access a cached template.
189
+ // Also $( selectorToScriptBlock ).template(), or $.template( null, templateString )
190
+ // will return the compiled template, without adding a name reference.
191
+ // If templateString includes at least one HTML tag, $.template( templateString ) is equivalent
192
+ // to $.template( null, templateString )
193
+ template: function( name, tmpl ) {
194
+ if (tmpl) {
195
+ // Compile template and associate with name
196
+ if ( typeof tmpl === "string" ) {
197
+ // This is an HTML string being passed directly in.
198
+ tmpl = buildTmplFn( tmpl )
199
+ } else if ( tmpl instanceof jQuery ) {
200
+ tmpl = tmpl[0] || {};
201
+ }
202
+ if ( tmpl.nodeType ) {
203
+ // If this is a template block, use cached copy, or generate tmpl function and cache.
204
+ tmpl = jQuery.data( tmpl, "tmpl" ) || jQuery.data( tmpl, "tmpl", buildTmplFn( tmpl.innerHTML ));
205
+ }
206
+ return typeof name === "string" ? (jQuery.template[name] = tmpl) : tmpl;
207
+ }
208
+ // Return named compiled template
209
+ return name ? (typeof name !== "string" ? jQuery.template( null, name ):
210
+ (jQuery.template[name] ||
211
+ // If not in map, treat as a selector. (If integrated with core, use quickExpr.exec)
212
+ jQuery.template( null, htmlExpr.test( name ) ? name : jQuery( name )))) : null;
213
+ },
214
+
215
+ encode: function( text ) {
216
+ // Do HTML encoding replacing < > & and ' and " by corresponding entities.
217
+ return ("" + text).split("<").join("&lt;").split(">").join("&gt;").split('"').join("&#34;").split("'").join("&#39;");
218
+ }
219
+ });
220
+
221
+ jQuery.extend( jQuery.tmpl, {
222
+ tag: {
223
+ "tmpl": {
224
+ _default: { $2: "null" },
225
+ open: "if($notnull_1){_=_.concat($item.nest($1,$2));}"
226
+ // tmpl target parameter can be of type function, so use $1, not $1a (so not auto detection of functions)
227
+ // This means that {{tmpl foo}} treats foo as a template (which IS a function).
228
+ // Explicit parens can be used if foo is a function that returns a template: {{tmpl foo()}}.
229
+ },
230
+ "wrap": {
231
+ _default: { $2: "null" },
232
+ open: "$item.calls(_,$1,$2);_=[];",
233
+ close: "call=$item.calls();_=call._.concat($item.wrap(call,_));"
234
+ },
235
+ "each": {
236
+ _default: { $2: "$index, $value" },
237
+ open: "if($notnull_1){$.each($1a,function($2){with(this){",
238
+ close: "}});}"
239
+ },
240
+ "if": {
241
+ open: "if(($notnull_1) && $1a){",
242
+ close: "}"
243
+ },
244
+ "else": {
245
+ _default: { $1: "true" },
246
+ open: "}else if(($notnull_1) && $1a){"
247
+ },
248
+ "html": {
249
+ // Unecoded expression evaluation.
250
+ open: "if($notnull_1){_.push($1a);}"
251
+ },
252
+ "=": {
253
+ // Encoded expression evaluation. Abbreviated form is ${}.
254
+ _default: { $1: "$data" },
255
+ open: "if($notnull_1){_.push($.encode($1a));}"
256
+ },
257
+ "!": {
258
+ // Comment tag. Skipped by parser
259
+ open: ""
260
+ }
261
+ },
262
+
263
+ // This stub can be overridden, e.g. in jquery.tmplPlus for providing rendered events
264
+ complete: function( items ) {
265
+ newTmplItems = {};
266
+ },
267
+
268
+ // Call this from code which overrides domManip, or equivalent
269
+ // Manage cloning/storing template items etc.
270
+ afterManip: function afterManip( elem, fragClone, callback ) {
271
+ // Provides cloned fragment ready for fixup prior to and after insertion into DOM
272
+ var content = fragClone.nodeType === 11 ?
273
+ jQuery.makeArray(fragClone.childNodes) :
274
+ fragClone.nodeType === 1 ? [fragClone] : [];
275
+
276
+ // Return fragment to original caller (e.g. append) for DOM insertion
277
+ callback.call( elem, fragClone );
278
+
279
+ // Fragment has been inserted:- Add inserted nodes to tmplItem data structure. Replace inserted element annotations by jQuery.data.
280
+ storeTmplItems( content );
281
+ cloneIndex++;
282
+ }
283
+ });
284
+
285
+ //========================== Private helper functions, used by code above ==========================
286
+
287
+ function build( tmplItem, nested, content ) {
288
+ // Convert hierarchical content into flat string array
289
+ // and finally return array of fragments ready for DOM insertion
290
+ var frag, ret = content ? jQuery.map( content, function( item ) {
291
+ return (typeof item === "string") ?
292
+ // Insert template item annotations, to be converted to jQuery.data( "tmplItem" ) when elems are inserted into DOM.
293
+ (tmplItem.key ? item.replace( /(<\w+)(?=[\s>])(?![^>]*_tmplitem)([^>]*)/g, "$1 " + tmplItmAtt + "=\"" + tmplItem.key + "\" $2" ) : item) :
294
+ // This is a child template item. Build nested template.
295
+ build( item, tmplItem, item._ctnt );
296
+ }) :
297
+ // If content is not defined, insert tmplItem directly. Not a template item. May be a string, or a string array, e.g. from {{html $item.html()}}.
298
+ tmplItem;
299
+ if ( nested ) {
300
+ return ret;
301
+ }
302
+
303
+ // top-level template
304
+ ret = ret.join("");
305
+
306
+ // Support templates which have initial or final text nodes, or consist only of text
307
+ // Also support HTML entities within the HTML markup.
308
+ ret.replace( /^\s*([^<\s][^<]*)?(<[\w\W]+>)([^>]*[^>\s])?\s*$/, function( all, before, middle, after) {
309
+ frag = jQuery( middle ).get();
310
+
311
+ storeTmplItems( frag );
312
+ if ( before ) {
313
+ frag = unencode( before ).concat(frag);
314
+ }
315
+ if ( after ) {
316
+ frag = frag.concat(unencode( after ));
317
+ }
318
+ });
319
+ return frag ? frag : unencode( ret );
320
+ }
321
+
322
+ function unencode( text ) {
323
+ // Use createElement, since createTextNode will not render HTML entities correctly
324
+ var el = document.createElement( "div" );
325
+ el.innerHTML = text;
326
+ return jQuery.makeArray(el.childNodes);
327
+ }
328
+
329
+ // Generate a reusable function that will serve to render a template against data
330
+ function buildTmplFn( markup ) {
331
+ return new Function("jQuery","$item",
332
+ "var $=jQuery,call,_=[],$data=$item.data;" +
333
+
334
+ // Introduce the data as local variables using with(){}
335
+ "with($data){_.push('" +
336
+
337
+ // Convert the template into pure JavaScript
338
+ jQuery.trim(markup)
339
+ .replace( /([\\'])/g, "\\$1" )
340
+ .replace( /[\r\t\n]/g, " " )
341
+ .replace( /\$\{([^\}]*)\}/g, "{{= $1}}" )
342
+ .replace( /\{\{(\/?)(\w+|.)(?:\(((?:[^\}]|\}(?!\}))*?)?\))?(?:\s+(.*?)?)?(\(((?:[^\}]|\}(?!\}))*?)\))?\s*\}\}/g,
343
+ function( all, slash, type, fnargs, target, parens, args ) {
344
+ var tag = jQuery.tmpl.tag[ type ], def, expr, exprAutoFnDetect;
345
+ if ( !tag ) {
346
+ throw "Template command not found: " + type;
347
+ }
348
+ def = tag._default || [];
349
+ if ( parens && !/\w$/.test(target)) {
350
+ target += parens;
351
+ parens = "";
352
+ }
353
+ if ( target ) {
354
+ target = unescape( target );
355
+ args = args ? ("," + unescape( args ) + ")") : (parens ? ")" : "");
356
+ // Support for target being things like a.toLowerCase();
357
+ // In that case don't call with template item as 'this' pointer. Just evaluate...
358
+ expr = parens ? (target.indexOf(".") > -1 ? target + parens : ("(" + target + ").call($data" + args)) : target;
359
+ exprAutoFnDetect = parens ? expr : "(typeof(" + target + ")==='function'?(" + target + ").call($item):(" + target + "))";
360
+ } else {
361
+ exprAutoFnDetect = expr = def.$1 || "null";
362
+ }
363
+ fnargs = unescape( fnargs );
364
+ return "');" +
365
+ tag[ slash ? "close" : "open" ]
366
+ .split( "$notnull_1" ).join( target ? "typeof(" + target + ")!=='undefined' && (" + target + ")!=null" : "true" )
367
+ .split( "$1a" ).join( exprAutoFnDetect )
368
+ .split( "$1" ).join( expr )
369
+ .split( "$2" ).join( fnargs ?
370
+ fnargs.replace( /\s*([^\(]+)\s*(\((.*?)\))?/g, function( all, name, parens, params ) {
371
+ params = params ? ("," + params + ")") : (parens ? ")" : "");
372
+ return params ? ("(" + name + ").call($item" + params) : all;
373
+ })
374
+ : (def.$2||"")
375
+ ) +
376
+ "_.push('";
377
+ }) +
378
+ "');}return _;"
379
+ );
380
+ }
381
+ function updateWrapped( options, wrapped ) {
382
+ // Build the wrapped content.
383
+ options._wrap = build( options, true,
384
+ // Suport imperative scenario in which options.wrapped can be set to a selector or an HTML string.
385
+ jQuery.isArray( wrapped ) ? wrapped : [htmlExpr.test( wrapped ) ? wrapped : jQuery( wrapped ).html()]
386
+ ).join("");
387
+ }
388
+
389
+ function unescape( args ) {
390
+ return args ? args.replace( /\\'/g, "'").replace(/\\\\/g, "\\" ) : null;
391
+ }
392
+ function outerHtml( elem ) {
393
+ var div = document.createElement("div");
394
+ div.appendChild( elem.cloneNode(true) );
395
+ return div.innerHTML;
396
+ }
397
+
398
+ // Store template items in jQuery.data(), ensuring a unique tmplItem data data structure for each rendered template instance.
399
+ function storeTmplItems( content ) {
400
+ var keySuffix = "_" + cloneIndex, elem, elems, newClonedItems = {}, i, l, m;
401
+ for ( i = 0, l = content.length; i < l; i++ ) {
402
+ if ( (elem = content[i]).nodeType !== 1 ) {
403
+ continue;
404
+ }
405
+ elems = elem.getElementsByTagName("*");
406
+ for ( m = elems.length - 1; m >= 0; m-- ) {
407
+ processItemKey( elems[m] );
408
+ }
409
+ processItemKey( elem );
410
+ }
411
+ function processItemKey( el ) {
412
+ var pntKey, pntNode = el, pntItem, tmplItem, key;
413
+ // Ensure that each rendered template inserted into the DOM has its own template item,
414
+ if ( (key = el.getAttribute( tmplItmAtt ))) {
415
+ while ( pntNode.parentNode && (pntNode = pntNode.parentNode).nodeType === 1 && !(pntKey = pntNode.getAttribute( tmplItmAtt ))) { }
416
+ if ( pntKey !== key ) {
417
+ // The next ancestor with a _tmplitem expando is on a different key than this one.
418
+ // So this is a top-level element within this template item
419
+ // Set pntNode to the key of the parentNode, or to 0 if pntNode.parentNode is null, or pntNode is a fragment.
420
+ pntNode = pntNode.parentNode ? (pntNode.nodeType === 11 ? 0 : (pntNode.getAttribute( tmplItmAtt ) || 0)) : 0;
421
+ if ( !(tmplItem = newTmplItems[key]) ) {
422
+ // The item is for wrapped content, and was copied from the temporary parent wrappedItem.
423
+ tmplItem = wrappedItems[key];
424
+ tmplItem = newTmplItem( tmplItem, newTmplItems[pntNode]||wrappedItems[pntNode], null, true );
425
+ tmplItem.key = ++itemKey;
426
+ newTmplItems[itemKey] = tmplItem;
427
+ }
428
+ if ( cloneIndex ) {
429
+ cloneTmplItem( key );
430
+ }
431
+ }
432
+ el.removeAttribute( tmplItmAtt );
433
+ } else if ( cloneIndex && (tmplItem = jQuery.data( el, "tmplItem" )) ) {
434
+ // This was a rendered element, cloned during append or appendTo etc.
435
+ // TmplItem stored in jQuery data has already been cloned in cloneCopyEvent. We must replace it with a fresh cloned tmplItem.
436
+ cloneTmplItem( tmplItem.key );
437
+ newTmplItems[tmplItem.key] = tmplItem;
438
+ pntNode = jQuery.data( el.parentNode, "tmplItem" );
439
+ pntNode = pntNode ? pntNode.key : 0;
440
+ }
441
+ if ( tmplItem ) {
442
+ pntItem = tmplItem;
443
+ // Find the template item of the parent element.
444
+ // (Using !=, not !==, since pntItem.key is number, and pntNode may be a string)
445
+ while ( pntItem && pntItem.key != pntNode ) {
446
+ // Add this element as a top-level node for this rendered template item, as well as for any
447
+ // ancestor items between this item and the item of its parent element
448
+ pntItem.nodes.push( el );
449
+ pntItem = pntItem.parent;
450
+ }
451
+ // Delete content built during rendering - reduce API surface area and memory use, and avoid exposing of stale data after rendering...
452
+ delete tmplItem._ctnt;
453
+ delete tmplItem._wrap;
454
+ // Store template item as jQuery data on the element
455
+ jQuery.data( el, "tmplItem", tmplItem );
456
+ }
457
+ function cloneTmplItem( key ) {
458
+ key = key + keySuffix;
459
+ tmplItem = newClonedItems[key] =
460
+ (newClonedItems[key] || newTmplItem( tmplItem, newTmplItems[tmplItem.parent.key + keySuffix] || tmplItem.parent, null, true ));
461
+ }
462
+ }
463
+ }
464
+
465
+ //---- Helper functions for template item ----
466
+
467
+ function tiCalls( content, tmpl, data, options ) {
468
+ if ( !content ) {
469
+ return stack.pop();
470
+ }
471
+ stack.push({ _: content, tmpl: tmpl, item:this, data: data, options: options });
472
+ }
473
+
474
+ function tiNest( tmpl, data, options ) {
475
+ // nested template, using {{tmpl}} tag
476
+ return jQuery.tmpl( jQuery.template( tmpl ), data, options, this );
477
+ }
478
+
479
+ function tiWrap( call, wrapped ) {
480
+ // nested template, using {{wrap}} tag
481
+ var options = call.options || {};
482
+ options.wrapped = wrapped;
483
+ // Apply the template, which may incorporate wrapped content,
484
+ return jQuery.tmpl( jQuery.template( call.tmpl ), call.data, options, call.item );
485
+ }
486
+
487
+ function tiHtml( filter, textOnly ) {
488
+ var wrapped = this._wrap;
489
+ return jQuery.map(
490
+ jQuery( jQuery.isArray( wrapped ) ? wrapped.join("") : wrapped ).filter( filter || "*" ),
491
+ function(e) {
492
+ return textOnly ?
493
+ e.innerText || e.textContent :
494
+ e.outerHTML || outerHtml(e);
495
+ });
496
+ }
497
+
498
+ function tiUpdate() {
499
+ var coll = this.nodes;
500
+ jQuery.tmpl( null, null, null, this).insertBefore( coll[0] );
501
+ jQuery( coll ).remove();
502
+ }
503
+ })( jQuery );