transit 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (186) hide show
  1. data/Gemfile +19 -11
  2. data/app/assets/images/transit/icon24x24.png +0 -0
  3. data/app/assets/images/transit/icon24x24_files.png +0 -0
  4. data/app/assets/images/transit/jplayer.swf +0 -0
  5. data/app/assets/images/transit/uploadify.swf +0 -0
  6. data/app/assets/images/transit/video_player.swf +0 -0
  7. data/app/assets/javascripts/jqtools/expose.js +224 -0
  8. data/app/assets/javascripts/jqtools/flashembed.js +300 -0
  9. data/app/assets/javascripts/jqtools/overlay.js +294 -0
  10. data/app/assets/javascripts/jqtools/scrollable.js +3 -0
  11. data/app/assets/javascripts/jqtools/scrollable/autoscroll.js +81 -0
  12. data/app/assets/javascripts/jqtools/scrollable/base.js +335 -0
  13. data/app/assets/javascripts/jqtools/scrollable/navigator.js +139 -0
  14. data/app/assets/javascripts/jqtools/validator.js +590 -0
  15. data/app/assets/javascripts/jqueryui/accordion.js +611 -0
  16. data/app/assets/javascripts/jqueryui/autocomplete.js +612 -0
  17. data/app/assets/javascripts/jqueryui/button.js +388 -0
  18. data/app/assets/javascripts/jqueryui/core.js +312 -0
  19. data/app/assets/javascripts/jqueryui/datepicker.js +1791 -0
  20. data/app/assets/javascripts/jqueryui/dialog.js +878 -0
  21. data/app/assets/javascripts/jqueryui/draggable.js +815 -0
  22. data/app/assets/javascripts/jqueryui/droppable.js +285 -0
  23. data/app/assets/javascripts/jqueryui/effects/blind.js +49 -0
  24. data/app/assets/javascripts/jqueryui/effects/bounce.js +78 -0
  25. data/app/assets/javascripts/jqueryui/effects/clip.js +54 -0
  26. data/app/assets/javascripts/jqueryui/effects/core.js +746 -0
  27. data/app/assets/javascripts/jqueryui/effects/drop.js +50 -0
  28. data/app/assets/javascripts/jqueryui/effects/explode.js +79 -0
  29. data/app/assets/javascripts/jqueryui/effects/fade.js +32 -0
  30. data/app/assets/javascripts/jqueryui/effects/fold.js +56 -0
  31. data/app/assets/javascripts/jqueryui/effects/highlight.js +50 -0
  32. data/app/assets/javascripts/jqueryui/effects/pulsate.js +51 -0
  33. data/app/assets/javascripts/jqueryui/effects/scale.js +178 -0
  34. data/app/assets/javascripts/jqueryui/effects/shake.js +57 -0
  35. data/app/assets/javascripts/jqueryui/effects/slide.js +50 -0
  36. data/app/assets/javascripts/jqueryui/effects/transfer.js +45 -0
  37. data/app/assets/javascripts/jqueryui/mouse.js +160 -0
  38. data/app/assets/javascripts/jqueryui/position.js +252 -0
  39. data/app/assets/javascripts/jqueryui/progressbar.js +109 -0
  40. data/app/assets/javascripts/jqueryui/resizable.js +814 -0
  41. data/app/assets/javascripts/jqueryui/selectable.js +266 -0
  42. data/app/assets/javascripts/jqueryui/slider.js +666 -0
  43. data/app/assets/javascripts/jqueryui/sortable.js +1077 -0
  44. data/app/assets/javascripts/jqueryui/tabs.js +758 -0
  45. data/app/assets/javascripts/jqueryui/widget.js +262 -0
  46. data/app/assets/javascripts/libs/backbone.js +1152 -0
  47. data/app/assets/javascripts/libs/cookie.js +89 -0
  48. data/app/assets/javascripts/libs/fileinput.js +130 -0
  49. data/app/assets/javascripts/libs/jplayer.js +1768 -0
  50. data/app/assets/javascripts/libs/proper.js +541 -0
  51. data/app/assets/javascripts/libs/sanitize.js +282 -0
  52. data/app/assets/javascripts/libs/selecttolist.js +75 -0
  53. data/app/assets/javascripts/libs/underscore.js +807 -0
  54. data/app/assets/javascripts/libs/uploadify.js +677 -0
  55. data/app/assets/javascripts/libs/wymeditor.js +9538 -0
  56. data/app/assets/javascripts/transit.js +4 -0
  57. data/app/assets/javascripts/transit/admin.js +22 -0
  58. data/app/assets/javascripts/transit/admin/contexts.js +52 -0
  59. data/app/assets/javascripts/transit/admin/fields.js +36 -0
  60. data/app/assets/javascripts/transit/admin/upload.js +109 -0
  61. data/app/assets/javascripts/transit/config.js.erb +101 -0
  62. data/app/assets/javascripts/transit/contexts/audio.js +39 -0
  63. data/app/assets/javascripts/transit/contexts/video.js +79 -0
  64. data/app/assets/javascripts/transit/core.js +171 -0
  65. data/app/assets/javascripts/transit/frontend.js +3 -0
  66. data/app/assets/javascripts/transit/lib/base64.js +120 -0
  67. data/app/assets/javascripts/transit/lib/editor.js +177 -0
  68. data/app/assets/javascripts/transit/views/audio_player.jst +22 -0
  69. data/app/assets/javascripts/transit/views/editor_toolbar.jst +12 -0
  70. data/app/assets/javascripts/transit/views/file_upload.jst +5 -0
  71. data/app/assets/javascripts/transit/views/video_player.jst +20 -0
  72. data/app/assets/javascripts/transit/views/wym_box.jst +4 -0
  73. data/app/assets/javascripts/transit/views/wym_iframe.jst +3 -0
  74. data/app/assets/stylesheets/transit.css.scss.erb +42 -0
  75. data/app/assets/stylesheets/transit/forms.css.scss +66 -0
  76. data/app/assets/stylesheets/transit/media/audio.css.scss +65 -0
  77. data/app/assets/stylesheets/transit/media/video.css.scss +30 -0
  78. data/app/assets/stylesheets/transit/panel.css.scss +100 -0
  79. data/app/assets/stylesheets/transit/ui.css.scss +507 -0
  80. data/app/controllers/pages_controller.rb +3 -0
  81. data/app/controllers/posts_controller.rb +3 -0
  82. data/app/controllers/transit/assets_controller.rb +38 -0
  83. data/app/controllers/transit/contexts_controller.rb +12 -9
  84. data/app/controllers/transit/pages_controller.rb +26 -0
  85. data/app/controllers/transit/posts_controller.rb +31 -0
  86. data/app/controllers/transit/topics_controller.rb +5 -0
  87. data/app/controllers/transit_controller.rb +16 -0
  88. data/app/helpers/transit/admin_helper.rb +43 -0
  89. data/app/helpers/transit/form_helper.rb +17 -0
  90. data/app/helpers/transit/package_helper.rb +41 -0
  91. data/app/helpers/transit/pagination_helper.rb +58 -0
  92. data/app/helpers/transit_helper.rb +42 -0
  93. data/app/models/comment.rb +37 -0
  94. data/app/models/contexts/audio.rb +12 -0
  95. data/app/models/{text.rb → contexts/text.rb} +0 -0
  96. data/app/models/contexts/video.rb +24 -0
  97. data/app/models/topic.rb +19 -0
  98. data/app/models/transit/asset.rb +68 -0
  99. data/{lib → app/models}/transit/context.rb +29 -0
  100. data/app/views/contexts/_text.html.erb +1 -1
  101. data/app/views/posts/index.rss.builder +18 -0
  102. data/app/views/transit/assets/_file.html.erb +4 -0
  103. data/app/views/transit/assets/_image.html.erb +13 -0
  104. data/app/views/transit/assets/create.js.erb +8 -0
  105. data/app/views/transit/assets/destroy.js.erb +3 -0
  106. data/app/views/transit/assets/manage.html.erb +20 -0
  107. data/app/views/transit/contexts/_audio.html.erb +18 -0
  108. data/app/views/transit/contexts/_text.html.erb +6 -0
  109. data/app/views/transit/contexts/_video.html.erb +13 -0
  110. data/app/views/transit/contexts/destroy.js.erb +1 -0
  111. data/app/views/transit/contexts/index.html.erb +5 -0
  112. data/app/views/transit/contexts/new.js.erb +7 -0
  113. data/app/views/transit/contexts/show.html.erb +8 -0
  114. data/app/views/transit/index.html.erb +26 -0
  115. data/app/views/transit/index.js.erb +1 -0
  116. data/app/views/transit/interface/post_panel.html.erb +96 -0
  117. data/app/views/transit/pages/_table.html.erb +7 -0
  118. data/app/views/transit/pages/edit.html.erb +17 -0
  119. data/app/views/transit/pages/index.html.erb +17 -0
  120. data/app/views/transit/pages/update.js.erb +1 -0
  121. data/app/views/transit/posts/_form.html.erb +49 -0
  122. data/app/views/transit/posts/edit.html.erb +14 -0
  123. data/app/views/transit/posts/new.html.erb +21 -0
  124. data/app/views/transit/table.html.erb +13 -0
  125. data/app/views/transit/table.js.erb +8 -0
  126. data/app/views/transit/topics/manage.html.erb +28 -0
  127. data/config/locales/en.yml +22 -0
  128. data/config/routes.rb +3 -3
  129. data/lib/transit.rb +51 -17
  130. data/lib/transit/admin.rb +85 -0
  131. data/lib/transit/builders/form_builder.rb +319 -0
  132. data/lib/transit/builders/jst_builder.rb +38 -0
  133. data/lib/transit/builders/package_builder.rb +45 -0
  134. data/lib/transit/config.rb +20 -0
  135. data/lib/transit/controller/generator.rb +42 -0
  136. data/lib/transit/controller/responder.rb +34 -0
  137. data/lib/transit/core_ext.rb +18 -0
  138. data/lib/transit/errors/resource_not_found.rb +6 -0
  139. data/lib/transit/model/assets.rb +14 -0
  140. data/lib/transit/model/attachments.rb +55 -0
  141. data/lib/transit/model/auto_increment.rb +22 -0
  142. data/lib/transit/model/base.rb +56 -0
  143. data/lib/transit/model/comments.rb +19 -0
  144. data/lib/transit/model/hooks.rb +38 -0
  145. data/lib/transit/model/owners.rb +14 -0
  146. data/lib/transit/model/paginator.rb +92 -0
  147. data/lib/transit/model/topics.rb +14 -0
  148. data/lib/transit/package/page.rb +20 -12
  149. data/lib/transit/package/post.rb +87 -33
  150. data/lib/transit/package/post/validations.rb +14 -0
  151. data/lib/transit/rails/engine.rb +29 -13
  152. data/lib/transit/rails/railtie.rb +31 -0
  153. data/lib/transit/rails/routing.rb +11 -6
  154. data/lib/transit/services.rb +13 -0
  155. data/lib/transit/services/base.rb +14 -0
  156. data/lib/transit/services/facebook.rb +13 -0
  157. data/lib/transit/services/ted.rb +10 -0
  158. data/lib/transit/services/twitter.rb +13 -0
  159. data/lib/transit/services/vimeo.rb +10 -0
  160. data/lib/transit/services/you_tube.rb +12 -0
  161. data/lib/transit/version.rb +1 -1
  162. metadata +178 -31
  163. data/app/assets/stylesheets/includes/_compat.scss +0 -24
  164. data/app/assets/stylesheets/includes/_defaults.scss +0 -99
  165. data/app/assets/stylesheets/includes/_global.scss +0 -16
  166. data/app/assets/stylesheets/includes/_imports.scss +0 -27
  167. data/app/assets/stylesheets/includes/_mixins.scss +0 -38
  168. data/app/assets/stylesheets/includes/_setup.scss +0 -85
  169. data/app/assets/stylesheets/layout.css.scss +0 -29
  170. data/app/assets/stylesheets/transit.css +0 -3
  171. data/app/controllers/application_controller.rb +0 -5
  172. data/app/controllers/transit/index_controller.rb +0 -7
  173. data/app/controllers/transit/packages_controller.rb +0 -64
  174. data/app/controllers/transit/transit_controller.rb +0 -4
  175. data/app/helpers/routing_helpers.rb +0 -7
  176. data/app/models/audio.rb +0 -4
  177. data/app/models/package_asset.rb +0 -11
  178. data/app/models/video.rb +0 -8
  179. data/app/views/contexts/_audio.html.erb +0 -1
  180. data/app/views/contexts/_video.html.erb +0 -1
  181. data/app/views/layouts/transit.html.erb +0 -31
  182. data/app/views/transit/index/index.html.erb +0 -0
  183. data/lib/transit/helpers/controller_helpers.rb +0 -40
  184. data/lib/transit/helpers/model_helpers.rb +0 -26
  185. data/lib/transit/package.rb +0 -25
  186. data/lib/transit/package/base.rb +0 -49
@@ -0,0 +1,282 @@
1
+ /**
2
+ * Copyright (c) 2010 by Gabriel Birke
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the 'Software'), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in all
12
+ * copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+ function Sanitize(){
24
+ var i, e, options;
25
+ options = arguments[0] || {}
26
+ this.config = {}
27
+ this.config.elements = options.elements ? options.elements : [];
28
+ this.config.attributes = options.attributes ? options.attributes : {};
29
+ this.config.attributes[Sanitize.ALL] = this.config.attributes[Sanitize.ALL] ? this.config.attributes[Sanitize.ALL] : [];
30
+ this.config.allow_comments = options.allow_comments ? options.allow_comments : false;
31
+ this.allowed_elements = {};
32
+ this.config.protocols = options.protocols ? options.protocols : {};
33
+ this.config.add_attributes = options.add_attributes ? options.add_attributes : {};
34
+ this.dom = options.dom ? options.dom : document;
35
+ for(i=0;i<this.config.elements.length;i++) {
36
+ this.allowed_elements[this.config.elements[i]] = true;
37
+ }
38
+ this.config.remove_element_contents = {};
39
+ this.config.remove_all_contents = false;
40
+ if(options.remove_contents) {
41
+
42
+ if(options.remove_contents instanceof Array) {
43
+ for(i=0;i<options.remove_contents.length;i++) {
44
+ this.config.remove_element_contents[options.remove_contents[i]] = true;
45
+ }
46
+ }
47
+ else {
48
+ this.config.remove_all_contents = true;
49
+ }
50
+ }
51
+ this.transformers = options.transformers ? options.transformers : [];
52
+ }
53
+
54
+ Sanitize.REGEX_PROTOCOL = /^([A-Za-z0-9\+\-\.\&\;\*\s]*?)(?:\:|&*0*58|&*x0*3a)/i
55
+ Sanitize.RELATIVE = '__relative__'; // emulate Ruby symbol with string constant
56
+
57
+ Sanitize.prototype.clean_node = function(container) {
58
+ var fragment = this.dom.createDocumentFragment();
59
+ this.current_element = fragment;
60
+ this.whitelist_nodes = [];
61
+
62
+
63
+
64
+ /**
65
+ * Utility function to check if an element exists in an array
66
+ */
67
+ function _array_index(needle, haystack) {
68
+ var i;
69
+ for(i=0; i < haystack.length; i++) {
70
+ if(haystack[i] == needle)
71
+ return i;
72
+ }
73
+ return -1;
74
+ }
75
+
76
+ function _merge_arrays_uniq() {
77
+ var result = [];
78
+ var uniq_hash = {}
79
+ var i,j;
80
+ for(i=0;i<arguments.length;i++) {
81
+ if(!arguments[i] || !arguments[i].length)
82
+ continue;
83
+ for(j=0;j<arguments[i].length;j++) {
84
+ if(uniq_hash[arguments[i][j]])
85
+ continue;
86
+ uniq_hash[arguments[i][j]] = true;
87
+ result.push(arguments[i][j]);
88
+ }
89
+ }
90
+ return result;
91
+ }
92
+
93
+ /**
94
+ * Clean function that checks the different node types and cleans them up accordingly
95
+ * @param elem DOM Node to clean
96
+ */
97
+ function _clean(elem) {
98
+ var clone;
99
+ switch(elem.nodeType) {
100
+ // Element
101
+ case 1:
102
+ _clean_element.call(this, elem)
103
+ break;
104
+ // Text
105
+ case 3:
106
+ var clone = elem.cloneNode(false);
107
+ this.current_element.appendChild(clone);
108
+ break;
109
+ // Entity-Reference (normally not used)
110
+ case 5:
111
+ var clone = elem.cloneNode(false);
112
+ this.current_element.appendChild(clone);
113
+ break;
114
+ // Comment
115
+ case 8:
116
+ if(this.config.allow_comments) {
117
+ var clone = elem.cloneNode(false);
118
+ this.current_element.appendChild(clone);
119
+ }
120
+ default:
121
+ //console.log("unknown node type", elem.nodeType)
122
+ }
123
+
124
+ }
125
+
126
+ function _clean_element(elem) {
127
+ var i, j, clone, parent_element, name, allowed_attributes, attr, attr_name, attr_node, protocols, del, attr_ok;
128
+ var transform = _transform_element.call(this, elem);
129
+
130
+ elem = transform.node;
131
+ name = elem.nodeName.toLowerCase();
132
+
133
+ // check if element itself is allowed
134
+ parent_element = this.current_element;
135
+ if(this.allowed_elements[name] || transform.whitelist) {
136
+ this.current_element = this.dom.createElement(elem.nodeName);
137
+ parent_element.appendChild(this.current_element);
138
+
139
+ // clean attributes
140
+ allowed_attributes = _merge_arrays_uniq(
141
+ this.config.attributes[name],
142
+ this.config.attributes['__ALL__'],
143
+ transform.attr_whitelist
144
+ );
145
+ for(i=0;i<allowed_attributes.length;i++) {
146
+ attr_name = allowed_attributes[i];
147
+ attr = elem.attributes[attr_name];
148
+ if(attr) {
149
+ attr_ok = true;
150
+ // Check protocol attributes for valid protocol
151
+ if(this.config.protocols[name] && this.config.protocols[name][attr_name]) {
152
+ protocols = this.config.protocols[name][attr_name];
153
+ del = attr.nodeValue.toLowerCase().match(Sanitize.REGEX_PROTOCOL);
154
+ if(del) {
155
+ attr_ok = (_array_index(del[1], protocols) != -1);
156
+ }
157
+ else {
158
+ attr_ok = (_array_index(Sanitize.RELATIVE, protocols) != -1);
159
+ }
160
+ }
161
+ if(attr_ok) {
162
+ attr_node = document.createAttribute(attr_name);
163
+ attr_node.value = attr.nodeValue;
164
+ this.current_element.setAttributeNode(attr_node);
165
+ }
166
+ }
167
+ }
168
+
169
+ // Add attributes
170
+ if(this.config.add_attributes[name]) {
171
+ for(attr_name in this.config.add_attributes[name]) {
172
+ attr_node = document.createAttribute(attr_name);
173
+ attr_node.value = this.config.add_attributes[name][attr_name];
174
+ this.current_element.setAttributeNode(attr_node);
175
+ }
176
+ }
177
+ } // End checking if element is allowed
178
+ // If this node is in the dynamic whitelist array (built at runtime by
179
+ // transformers), let it live with all of its attributes intact.
180
+ else if(_array_index(elem, this.whitelist_nodes) != -1) {
181
+ this.current_element = elem.cloneNode(true);
182
+ // Remove child nodes, they will be sanitiazied and added by other code
183
+ while(this.current_element.childNodes.length > 0) {
184
+ this.current_element.removeChild(this.current_element.firstChild);
185
+ }
186
+ parent_element.appendChild(this.current_element);
187
+ }
188
+
189
+ // iterate over child nodes
190
+ if(!this.config.remove_all_contents && !this.config.remove_element_contents[name]) {
191
+ for(i=0;i<elem.childNodes.length;i++) {
192
+ _clean.call(this, elem.childNodes[i]);
193
+ }
194
+ }
195
+
196
+ // some versions of IE don't support normalize.
197
+ if(this.current_element.normalize) {
198
+ this.current_element.normalize();
199
+ }
200
+ this.current_element = parent_element;
201
+ } // END clean_element function
202
+
203
+ function _transform_element(node) {
204
+ var output = {
205
+ attr_whitelist:[],
206
+ node: node,
207
+ whitelist: false
208
+ }
209
+ var i, j, transform;
210
+ for(i=0;i<this.transformers.length;i++) {
211
+ transform = this.transformers[i]({
212
+ allowed_elements: this.allowed_elements,
213
+ config: this.config,
214
+ node: node,
215
+ node_name: node.nodeName.toLowerCase(),
216
+ whitelist_nodes: this.whitelist_nodes,
217
+ dom: this.dom
218
+ });
219
+ if(transform == null)
220
+ continue;
221
+ else if(typeof transform == 'object') {
222
+ if(transform.whitelist_nodes && transform.whitelist_nodes instanceof Array) {
223
+ for(j=0;j<transform.whitelist_nodes.length;j++) {
224
+ if(_array_index(transform.whitelist_nodes[j], this.whitelist_nodes) == -1) {
225
+ this.whitelist_nodes.push(transform.whitelist_nodes[j]);
226
+ }
227
+ }
228
+ }
229
+ output.whitelist = transform.whitelist ? true : false;
230
+ if(transform.attr_whitelist) {
231
+ output.attr_whitelist = _merge_arrays_uniq(output.attr_whitelist, transform.attr_whitelist);
232
+ }
233
+ output.node = transform.node ? transform.node : output.node;
234
+ }
235
+ else {
236
+ throw new Error("transformer output must be an object or null");
237
+ }
238
+ }
239
+ return output;
240
+ }
241
+
242
+
243
+
244
+ for(i=0;i<container.childNodes.length;i++) {
245
+ _clean.call(this, container.childNodes[i]);
246
+ }
247
+
248
+ if(fragment.normalize) {
249
+ fragment.normalize();
250
+ }
251
+
252
+ return fragment;
253
+
254
+ }
255
+
256
+ if(!Sanitize.Config) {
257
+ Sanitize.Config = {};
258
+ }
259
+
260
+ Sanitize.Config.BASIC = {
261
+ elements: [
262
+ 'a', 'b', 'blockquote', 'br', 'cite', 'code', 'dd', 'dl', 'dt', 'em',
263
+ 'i', 'li', 'ol', 'p', 'pre', 'q', 'small', 'strike', 'strong', 'sub',
264
+ 'sup', 'u', 'ul'],
265
+
266
+ attributes: {
267
+ 'a' : ['href'],
268
+ 'blockquote': ['cite'],
269
+ 'q' : ['cite']
270
+ },
271
+
272
+ add_attributes: {
273
+ 'a': {'rel': 'nofollow'}
274
+ },
275
+
276
+ protocols: {
277
+ 'a' : {'href': ['ftp', 'http', 'https', 'mailto',
278
+ Sanitize.RELATIVE]},
279
+ 'blockquote': {'cite': ['http', 'https', Sanitize.RELATIVE]},
280
+ 'q' : {'cite': ['http', 'https', Sanitize.RELATIVE]}
281
+ }
282
+ };
@@ -0,0 +1,75 @@
1
+ /**
2
+ * selectToList
3
+ *
4
+ * Uses a select dropdown and an ordered/unordered list to simulate a more user-friendly multiple-select list.
5
+ *
6
+ **/
7
+ (function( $, undefined ) {
8
+
9
+ jQuery.fn.selectToList = function(conf){
10
+
11
+ var target,
12
+ add_link,
13
+ rem_link,
14
+ opts = {},
15
+ field_name,
16
+ existing = [],
17
+ all_list = $(this);
18
+
19
+ if ( $.type(conf) == 'string' ){
20
+ target = $(conf);
21
+ opts = {};
22
+ }else{
23
+ target = $(conf.target);
24
+ opts = conf;
25
+ }
26
+
27
+ add_link = opts.add || target.closest('a.add_to_list');
28
+ rem_link = opts.rem || $('<a href="#" class="icon_delete remove_from_list">Remove</a>');
29
+ field_name = opts.name || this.attr('name');
30
+
31
+ $('a.remove_from_list').bind('click.selectToList', remove_from_list);
32
+
33
+ add_link.bind('click.selectToList', function(event, id, name){
34
+ event.preventDefault();
35
+ var to_add = id || all_list.val().toString(),
36
+ newli, newel, new_rem_link;
37
+ if ( $.inArray(to_add, existing) != -1 ) return true;
38
+
39
+ newli = $("<li></li>");
40
+ newel = $("<input type='hidden' />");
41
+
42
+ target.append(newli);
43
+ newli.text(name || all_list.find("option:selected").text())
44
+ .append(newel);
45
+
46
+ new_rem_link = rem_link.clone(true);
47
+
48
+ newli.attr('rel', to_add);
49
+ newel.attr({ name:field_name, value: to_add });
50
+ newli.append(new_rem_link);
51
+ new_rem_link.bind("click.selectToList", remove_from_list);
52
+ existing.push(to_add);
53
+ });
54
+
55
+ function find_existing(){
56
+ var input;
57
+ existing = [];
58
+ target.find('li').each(function(i, li){
59
+ input = $(li).find('input:first');
60
+ existing.push($(input).val().toString());
61
+ });
62
+ }
63
+
64
+ find_existing();
65
+
66
+ function remove_from_list(event){
67
+ event.preventDefault();
68
+ var self = $(this), li = self.parent('li');
69
+ li.remove();
70
+ find_existing();
71
+ }
72
+
73
+ };
74
+
75
+ })( jQuery );
@@ -0,0 +1,807 @@
1
+ // Underscore.js 1.1.6
2
+ // (c) 2011 Jeremy Ashkenas, DocumentCloud Inc.
3
+ // Underscore is freely distributable under the MIT license.
4
+ // Portions of Underscore are inspired or borrowed from Prototype,
5
+ // Oliver Steele's Functional, and John Resig's Micro-Templating.
6
+ // For all details and documentation:
7
+ // http://documentcloud.github.com/underscore
8
+
9
+ (function() {
10
+
11
+ // Baseline setup
12
+ // --------------
13
+
14
+ // Establish the root object, `window` in the browser, or `global` on the server.
15
+ var root = this;
16
+
17
+ // Save the previous value of the `_` variable.
18
+ var previousUnderscore = root._;
19
+
20
+ // Establish the object that gets returned to break out of a loop iteration.
21
+ var breaker = {};
22
+
23
+ // Save bytes in the minified (but not gzipped) version:
24
+ var ArrayProto = Array.prototype, ObjProto = Object.prototype, FuncProto = Function.prototype;
25
+
26
+ // Create quick reference variables for speed access to core prototypes.
27
+ var slice = ArrayProto.slice,
28
+ unshift = ArrayProto.unshift,
29
+ toString = ObjProto.toString,
30
+ hasOwnProperty = ObjProto.hasOwnProperty;
31
+
32
+ // All **ECMAScript 5** native function implementations that we hope to use
33
+ // are declared here.
34
+ var
35
+ nativeForEach = ArrayProto.forEach,
36
+ nativeMap = ArrayProto.map,
37
+ nativeReduce = ArrayProto.reduce,
38
+ nativeReduceRight = ArrayProto.reduceRight,
39
+ nativeFilter = ArrayProto.filter,
40
+ nativeEvery = ArrayProto.every,
41
+ nativeSome = ArrayProto.some,
42
+ nativeIndexOf = ArrayProto.indexOf,
43
+ nativeLastIndexOf = ArrayProto.lastIndexOf,
44
+ nativeIsArray = Array.isArray,
45
+ nativeKeys = Object.keys,
46
+ nativeBind = FuncProto.bind;
47
+
48
+ // Create a safe reference to the Underscore object for use below.
49
+ var _ = function(obj) { return new wrapper(obj); };
50
+
51
+ // Export the Underscore object for **CommonJS**, with backwards-compatibility
52
+ // for the old `require()` API. If we're not in CommonJS, add `_` to the
53
+ // global object.
54
+ if (typeof module !== 'undefined' && module.exports) {
55
+ module.exports = _;
56
+ _._ = _;
57
+ } else {
58
+ root._ = _;
59
+ }
60
+
61
+ // Current version.
62
+ _.VERSION = '1.1.6';
63
+
64
+ // Collection Functions
65
+ // --------------------
66
+
67
+ // The cornerstone, an `each` implementation, aka `forEach`.
68
+ // Handles objects implementing `forEach`, arrays, and raw objects.
69
+ // Delegates to **ECMAScript 5**'s native `forEach` if available.
70
+ var each = _.each = _.forEach = function(obj, iterator, context) {
71
+ if (obj == null) return;
72
+ if (nativeForEach && obj.forEach === nativeForEach) {
73
+ obj.forEach(iterator, context);
74
+ } else if (_.isNumber(obj.length)) {
75
+ for (var i = 0, l = obj.length; i < l; i++) {
76
+ if (iterator.call(context, obj[i], i, obj) === breaker) return;
77
+ }
78
+ } else {
79
+ for (var key in obj) {
80
+ if (hasOwnProperty.call(obj, key)) {
81
+ if (iterator.call(context, obj[key], key, obj) === breaker) return;
82
+ }
83
+ }
84
+ }
85
+ };
86
+
87
+ // Return the results of applying the iterator to each element.
88
+ // Delegates to **ECMAScript 5**'s native `map` if available.
89
+ _.map = function(obj, iterator, context) {
90
+ var results = [];
91
+ if (obj == null) return results;
92
+ if (nativeMap && obj.map === nativeMap) return obj.map(iterator, context);
93
+ each(obj, function(value, index, list) {
94
+ results[results.length] = iterator.call(context, value, index, list);
95
+ });
96
+ return results;
97
+ };
98
+
99
+ // **Reduce** builds up a single result from a list of values, aka `inject`,
100
+ // or `foldl`. Delegates to **ECMAScript 5**'s native `reduce` if available.
101
+ _.reduce = _.foldl = _.inject = function(obj, iterator, memo, context) {
102
+ var initial = memo !== void 0;
103
+ if (obj == null) obj = [];
104
+ if (nativeReduce && obj.reduce === nativeReduce) {
105
+ if (context) iterator = _.bind(iterator, context);
106
+ return initial ? obj.reduce(iterator, memo) : obj.reduce(iterator);
107
+ }
108
+ each(obj, function(value, index, list) {
109
+ if (!initial && index === 0) {
110
+ memo = value;
111
+ initial = true;
112
+ } else {
113
+ memo = iterator.call(context, memo, value, index, list);
114
+ }
115
+ });
116
+ if (!initial) throw new TypeError("Reduce of empty array with no initial value");
117
+ return memo;
118
+ };
119
+
120
+ // The right-associative version of reduce, also known as `foldr`.
121
+ // Delegates to **ECMAScript 5**'s native `reduceRight` if available.
122
+ _.reduceRight = _.foldr = function(obj, iterator, memo, context) {
123
+ if (obj == null) obj = [];
124
+ if (nativeReduceRight && obj.reduceRight === nativeReduceRight) {
125
+ if (context) iterator = _.bind(iterator, context);
126
+ return memo !== void 0 ? obj.reduceRight(iterator, memo) : obj.reduceRight(iterator);
127
+ }
128
+ var reversed = (_.isArray(obj) ? obj.slice() : _.toArray(obj)).reverse();
129
+ return _.reduce(reversed, iterator, memo, context);
130
+ };
131
+
132
+ // Return the first value which passes a truth test. Aliased as `detect`.
133
+ _.find = _.detect = function(obj, iterator, context) {
134
+ var result;
135
+ any(obj, function(value, index, list) {
136
+ if (iterator.call(context, value, index, list)) {
137
+ result = value;
138
+ return true;
139
+ }
140
+ });
141
+ return result;
142
+ };
143
+
144
+ // Return all the elements that pass a truth test.
145
+ // Delegates to **ECMAScript 5**'s native `filter` if available.
146
+ // Aliased as `select`.
147
+ _.filter = _.select = function(obj, iterator, context) {
148
+ var results = [];
149
+ if (obj == null) return results;
150
+ if (nativeFilter && obj.filter === nativeFilter) return obj.filter(iterator, context);
151
+ each(obj, function(value, index, list) {
152
+ if (iterator.call(context, value, index, list)) results[results.length] = value;
153
+ });
154
+ return results;
155
+ };
156
+
157
+ // Return all the elements for which a truth test fails.
158
+ _.reject = function(obj, iterator, context) {
159
+ var results = [];
160
+ if (obj == null) return results;
161
+ each(obj, function(value, index, list) {
162
+ if (!iterator.call(context, value, index, list)) results[results.length] = value;
163
+ });
164
+ return results;
165
+ };
166
+
167
+ // Determine whether all of the elements match a truth test.
168
+ // Delegates to **ECMAScript 5**'s native `every` if available.
169
+ // Aliased as `all`.
170
+ _.every = _.all = function(obj, iterator, context) {
171
+ var result = true;
172
+ if (obj == null) return result;
173
+ if (nativeEvery && obj.every === nativeEvery) return obj.every(iterator, context);
174
+ each(obj, function(value, index, list) {
175
+ if (!(result = result && iterator.call(context, value, index, list))) return breaker;
176
+ });
177
+ return result;
178
+ };
179
+
180
+ // Determine if at least one element in the object matches a truth test.
181
+ // Delegates to **ECMAScript 5**'s native `some` if available.
182
+ // Aliased as `any`.
183
+ var any = _.some = _.any = function(obj, iterator, context) {
184
+ iterator || (iterator = _.identity);
185
+ var result = false;
186
+ if (obj == null) return result;
187
+ if (nativeSome && obj.some === nativeSome) return obj.some(iterator, context);
188
+ each(obj, function(value, index, list) {
189
+ if (result = iterator.call(context, value, index, list)) return breaker;
190
+ });
191
+ return result;
192
+ };
193
+
194
+ // Determine if a given value is included in the array or object using `===`.
195
+ // Aliased as `contains`.
196
+ _.include = _.contains = function(obj, target) {
197
+ var found = false;
198
+ if (obj == null) return found;
199
+ if (nativeIndexOf && obj.indexOf === nativeIndexOf) return obj.indexOf(target) != -1;
200
+ any(obj, function(value) {
201
+ if (found = value === target) return true;
202
+ });
203
+ return found;
204
+ };
205
+
206
+ // Invoke a method (with arguments) on every item in a collection.
207
+ _.invoke = function(obj, method) {
208
+ var args = slice.call(arguments, 2);
209
+ return _.map(obj, function(value) {
210
+ return (method.call ? method || value : value[method]).apply(value, args);
211
+ });
212
+ };
213
+
214
+ // Convenience version of a common use case of `map`: fetching a property.
215
+ _.pluck = function(obj, key) {
216
+ return _.map(obj, function(value){ return value[key]; });
217
+ };
218
+
219
+ // Return the maximum element or (element-based computation).
220
+ _.max = function(obj, iterator, context) {
221
+ if (!iterator && _.isArray(obj)) return Math.max.apply(Math, obj);
222
+ var result = {computed : -Infinity};
223
+ each(obj, function(value, index, list) {
224
+ var computed = iterator ? iterator.call(context, value, index, list) : value;
225
+ computed >= result.computed && (result = {value : value, computed : computed});
226
+ });
227
+ return result.value;
228
+ };
229
+
230
+ // Return the minimum element (or element-based computation).
231
+ _.min = function(obj, iterator, context) {
232
+ if (!iterator && _.isArray(obj)) return Math.min.apply(Math, obj);
233
+ var result = {computed : Infinity};
234
+ each(obj, function(value, index, list) {
235
+ var computed = iterator ? iterator.call(context, value, index, list) : value;
236
+ computed < result.computed && (result = {value : value, computed : computed});
237
+ });
238
+ return result.value;
239
+ };
240
+
241
+ // Sort the object's values by a criterion produced by an iterator.
242
+ _.sortBy = function(obj, iterator, context) {
243
+ return _.pluck(_.map(obj, function(value, index, list) {
244
+ return {
245
+ value : value,
246
+ criteria : iterator.call(context, value, index, list)
247
+ };
248
+ }).sort(function(left, right) {
249
+ var a = left.criteria, b = right.criteria;
250
+ return a < b ? -1 : a > b ? 1 : 0;
251
+ }), 'value');
252
+ };
253
+
254
+ // Use a comparator function to figure out at what index an object should
255
+ // be inserted so as to maintain order. Uses binary search.
256
+ _.sortedIndex = function(array, obj, iterator) {
257
+ iterator || (iterator = _.identity);
258
+ var low = 0, high = array.length;
259
+ while (low < high) {
260
+ var mid = (low + high) >> 1;
261
+ iterator(array[mid]) < iterator(obj) ? low = mid + 1 : high = mid;
262
+ }
263
+ return low;
264
+ };
265
+
266
+ // Safely convert anything iterable into a real, live array.
267
+ _.toArray = function(iterable) {
268
+ if (!iterable) return [];
269
+ if (iterable.toArray) return iterable.toArray();
270
+ if (_.isArray(iterable)) return iterable;
271
+ if (_.isArguments(iterable)) return slice.call(iterable);
272
+ return _.values(iterable);
273
+ };
274
+
275
+ // Return the number of elements in an object.
276
+ _.size = function(obj) {
277
+ return _.toArray(obj).length;
278
+ };
279
+
280
+ // Array Functions
281
+ // ---------------
282
+
283
+ // Get the first element of an array. Passing **n** will return the first N
284
+ // values in the array. Aliased as `head`. The **guard** check allows it to work
285
+ // with `_.map`.
286
+ _.first = _.head = function(array, n, guard) {
287
+ return (n != null) && !guard ? slice.call(array, 0, n) : array[0];
288
+ };
289
+
290
+ // Returns everything but the first entry of the array. Aliased as `tail`.
291
+ // Especially useful on the arguments object. Passing an **index** will return
292
+ // the rest of the values in the array from that index onward. The **guard**
293
+ // check allows it to work with `_.map`.
294
+ _.rest = _.tail = function(array, index, guard) {
295
+ return slice.call(array, (index == null) || guard ? 1 : index);
296
+ };
297
+
298
+ // Get the last element of an array.
299
+ _.last = function(array) {
300
+ return array[array.length - 1];
301
+ };
302
+
303
+ // Trim out all falsy values from an array.
304
+ _.compact = function(array) {
305
+ return _.filter(array, function(value){ return !!value; });
306
+ };
307
+
308
+ // Return a completely flattened version of an array.
309
+ _.flatten = function(array) {
310
+ return _.reduce(array, function(memo, value) {
311
+ if (_.isArray(value)) return memo.concat(_.flatten(value));
312
+ memo[memo.length] = value;
313
+ return memo;
314
+ }, []);
315
+ };
316
+
317
+ // Return a version of the array that does not contain the specified value(s).
318
+ _.without = function(array) {
319
+ var values = slice.call(arguments, 1);
320
+ return _.filter(array, function(value){ return !_.include(values, value); });
321
+ };
322
+
323
+ // Produce a duplicate-free version of the array. If the array has already
324
+ // been sorted, you have the option of using a faster algorithm.
325
+ // Aliased as `unique`.
326
+ _.uniq = _.unique = function(array, isSorted) {
327
+ return _.reduce(array, function(memo, el, i) {
328
+ if (0 == i || (isSorted === true ? _.last(memo) != el : !_.include(memo, el))) memo[memo.length] = el;
329
+ return memo;
330
+ }, []);
331
+ };
332
+
333
+ // Produce an array that contains every item shared between all the
334
+ // passed-in arrays.
335
+ _.intersect = function(array) {
336
+ var rest = slice.call(arguments, 1);
337
+ return _.filter(_.uniq(array), function(item) {
338
+ return _.every(rest, function(other) {
339
+ return _.indexOf(other, item) >= 0;
340
+ });
341
+ });
342
+ };
343
+
344
+ // Zip together multiple lists into a single array -- elements that share
345
+ // an index go together.
346
+ _.zip = function() {
347
+ var args = slice.call(arguments);
348
+ var length = _.max(_.pluck(args, 'length'));
349
+ var results = new Array(length);
350
+ for (var i = 0; i < length; i++) results[i] = _.pluck(args, "" + i);
351
+ return results;
352
+ };
353
+
354
+ // If the browser doesn't supply us with indexOf (I'm looking at you, **MSIE**),
355
+ // we need this function. Return the position of the first occurrence of an
356
+ // item in an array, or -1 if the item is not included in the array.
357
+ // Delegates to **ECMAScript 5**'s native `indexOf` if available.
358
+ // If the array is large and already in sort order, pass `true`
359
+ // for **isSorted** to use binary search.
360
+ _.indexOf = function(array, item, isSorted) {
361
+ if (array == null) return -1;
362
+ var i, l;
363
+ if (isSorted) {
364
+ i = _.sortedIndex(array, item);
365
+ return array[i] === item ? i : -1;
366
+ }
367
+ if (nativeIndexOf && array.indexOf === nativeIndexOf) return array.indexOf(item);
368
+ for (i = 0, l = array.length; i < l; i++) if (array[i] === item) return i;
369
+ return -1;
370
+ };
371
+
372
+
373
+ // Delegates to **ECMAScript 5**'s native `lastIndexOf` if available.
374
+ _.lastIndexOf = function(array, item) {
375
+ if (array == null) return -1;
376
+ if (nativeLastIndexOf && array.lastIndexOf === nativeLastIndexOf) return array.lastIndexOf(item);
377
+ var i = array.length;
378
+ while (i--) if (array[i] === item) return i;
379
+ return -1;
380
+ };
381
+
382
+ // Generate an integer Array containing an arithmetic progression. A port of
383
+ // the native Python `range()` function. See
384
+ // [the Python documentation](http://docs.python.org/library/functions.html#range).
385
+ _.range = function(start, stop, step) {
386
+ if (arguments.length <= 1) {
387
+ stop = start || 0;
388
+ start = 0;
389
+ }
390
+ step = arguments[2] || 1;
391
+
392
+ var len = Math.max(Math.ceil((stop - start) / step), 0);
393
+ var idx = 0;
394
+ var range = new Array(len);
395
+
396
+ while(idx < len) {
397
+ range[idx++] = start;
398
+ start += step;
399
+ }
400
+
401
+ return range;
402
+ };
403
+
404
+ // Function (ahem) Functions
405
+ // ------------------
406
+
407
+ // Create a function bound to a given object (assigning `this`, and arguments,
408
+ // optionally). Binding with arguments is also known as `curry`.
409
+ // Delegates to **ECMAScript 5**'s native `Function.bind` if available.
410
+ // We check for `func.bind` first, to fail fast when `func` is undefined.
411
+ _.bind = function(func, obj) {
412
+ if (func.bind === nativeBind && nativeBind) return nativeBind.apply(func, slice.call(arguments, 1));
413
+ var args = slice.call(arguments, 2);
414
+ return function() {
415
+ return func.apply(obj, args.concat(slice.call(arguments)));
416
+ };
417
+ };
418
+
419
+ // Bind all of an object's methods to that object. Useful for ensuring that
420
+ // all callbacks defined on an object belong to it.
421
+ _.bindAll = function(obj) {
422
+ var funcs = slice.call(arguments, 1);
423
+ if (funcs.length == 0) funcs = _.functions(obj);
424
+ each(funcs, function(f) { obj[f] = _.bind(obj[f], obj); });
425
+ return obj;
426
+ };
427
+
428
+ // Memoize an expensive function by storing its results.
429
+ _.memoize = function(func, hasher) {
430
+ var memo = {};
431
+ hasher || (hasher = _.identity);
432
+ return function() {
433
+ var key = hasher.apply(this, arguments);
434
+ return hasOwnProperty.call(memo, key) ? memo[key] : (memo[key] = func.apply(this, arguments));
435
+ };
436
+ };
437
+
438
+ // Delays a function for the given number of milliseconds, and then calls
439
+ // it with the arguments supplied.
440
+ _.delay = function(func, wait) {
441
+ var args = slice.call(arguments, 2);
442
+ return setTimeout(function(){ return func.apply(func, args); }, wait);
443
+ };
444
+
445
+ // Defers a function, scheduling it to run after the current call stack has
446
+ // cleared.
447
+ _.defer = function(func) {
448
+ return _.delay.apply(_, [func, 1].concat(slice.call(arguments, 1)));
449
+ };
450
+
451
+ // Internal function used to implement `_.throttle` and `_.debounce`.
452
+ var limit = function(func, wait, debounce) {
453
+ var timeout;
454
+ return function() {
455
+ var context = this, args = arguments;
456
+ var throttler = function() {
457
+ timeout = null;
458
+ func.apply(context, args);
459
+ };
460
+ if (debounce) clearTimeout(timeout);
461
+ if (debounce || !timeout) timeout = setTimeout(throttler, wait);
462
+ };
463
+ };
464
+
465
+ // Returns a function, that, when invoked, will only be triggered at most once
466
+ // during a given window of time.
467
+ _.throttle = function(func, wait) {
468
+ return limit(func, wait, false);
469
+ };
470
+
471
+ // Returns a function, that, as long as it continues to be invoked, will not
472
+ // be triggered. The function will be called after it stops being called for
473
+ // N milliseconds.
474
+ _.debounce = function(func, wait) {
475
+ return limit(func, wait, true);
476
+ };
477
+
478
+ // Returns a function that will be executed at most one time, no matter how
479
+ // often you call it. Useful for lazy initialization.
480
+ _.once = function(func) {
481
+ var ran = false, memo;
482
+ return function() {
483
+ if (ran) return memo;
484
+ ran = true;
485
+ return memo = func.apply(this, arguments);
486
+ };
487
+ };
488
+
489
+ // Returns the first function passed as an argument to the second,
490
+ // allowing you to adjust arguments, run code before and after, and
491
+ // conditionally execute the original function.
492
+ _.wrap = function(func, wrapper) {
493
+ return function() {
494
+ var args = [func].concat(slice.call(arguments));
495
+ return wrapper.apply(this, args);
496
+ };
497
+ };
498
+
499
+ // Returns a function that is the composition of a list of functions, each
500
+ // consuming the return value of the function that follows.
501
+ _.compose = function() {
502
+ var funcs = slice.call(arguments);
503
+ return function() {
504
+ var args = slice.call(arguments);
505
+ for (var i=funcs.length-1; i >= 0; i--) {
506
+ args = [funcs[i].apply(this, args)];
507
+ }
508
+ return args[0];
509
+ };
510
+ };
511
+
512
+ // Returns a function that will only be executed after being called N times.
513
+ _.after = function(times, func) {
514
+ return function() {
515
+ if (--times < 1) { return func.apply(this, arguments); }
516
+ };
517
+ };
518
+
519
+
520
+ // Object Functions
521
+ // ----------------
522
+
523
+ // Retrieve the names of an object's properties.
524
+ // Delegates to **ECMAScript 5**'s native `Object.keys`
525
+ _.keys = nativeKeys || function(obj) {
526
+ if (obj !== Object(obj)) throw new TypeError('Invalid object');
527
+ var keys = [];
528
+ for (var key in obj) if (hasOwnProperty.call(obj, key)) keys[keys.length] = key;
529
+ return keys;
530
+ };
531
+
532
+ // Retrieve the values of an object's properties.
533
+ _.values = function(obj) {
534
+ return _.map(obj, _.identity);
535
+ };
536
+
537
+ // Return a sorted list of the function names available on the object.
538
+ // Aliased as `methods`
539
+ _.functions = _.methods = function(obj) {
540
+ return _.filter(_.keys(obj), function(key){ return _.isFunction(obj[key]); }).sort();
541
+ };
542
+
543
+ // Extend a given object with all the properties in passed-in object(s).
544
+ _.extend = function(obj) {
545
+ each(slice.call(arguments, 1), function(source) {
546
+ for (var prop in source) {
547
+ if (source[prop] !== void 0) obj[prop] = source[prop];
548
+ }
549
+ });
550
+ return obj;
551
+ };
552
+
553
+ // Fill in a given object with default properties.
554
+ _.defaults = function(obj) {
555
+ each(slice.call(arguments, 1), function(source) {
556
+ for (var prop in source) {
557
+ if (obj[prop] == null) obj[prop] = source[prop];
558
+ }
559
+ });
560
+ return obj;
561
+ };
562
+
563
+ // Create a (shallow-cloned) duplicate of an object.
564
+ _.clone = function(obj) {
565
+ return _.isArray(obj) ? obj.slice() : _.extend({}, obj);
566
+ };
567
+
568
+ // Invokes interceptor with the obj, and then returns obj.
569
+ // The primary purpose of this method is to "tap into" a method chain, in
570
+ // order to perform operations on intermediate results within the chain.
571
+ _.tap = function(obj, interceptor) {
572
+ interceptor(obj);
573
+ return obj;
574
+ };
575
+
576
+ // Perform a deep comparison to check if two objects are equal.
577
+ _.isEqual = function(a, b) {
578
+ // Check object identity.
579
+ if (a === b) return true;
580
+ // Different types?
581
+ var atype = typeof(a), btype = typeof(b);
582
+ if (atype != btype) return false;
583
+ // Basic equality test (watch out for coercions).
584
+ if (a == b) return true;
585
+ // One is falsy and the other truthy.
586
+ if ((!a && b) || (a && !b)) return false;
587
+ // Unwrap any wrapped objects.
588
+ if (a._chain) a = a._wrapped;
589
+ if (b._chain) b = b._wrapped;
590
+ // One of them implements an isEqual()?
591
+ if (a.isEqual) return a.isEqual(b);
592
+ // Check dates' integer values.
593
+ if (_.isDate(a) && _.isDate(b)) return a.getTime() === b.getTime();
594
+ // Both are NaN?
595
+ if (_.isNaN(a) && _.isNaN(b)) return false;
596
+ // Compare regular expressions.
597
+ if (_.isRegExp(a) && _.isRegExp(b))
598
+ return a.source === b.source &&
599
+ a.global === b.global &&
600
+ a.ignoreCase === b.ignoreCase &&
601
+ a.multiline === b.multiline;
602
+ // If a is not an object by this point, we can't handle it.
603
+ if (atype !== 'object') return false;
604
+ // Check for different array lengths before comparing contents.
605
+ if (a.length && (a.length !== b.length)) return false;
606
+ // Nothing else worked, deep compare the contents.
607
+ var aKeys = _.keys(a), bKeys = _.keys(b);
608
+ // Different object sizes?
609
+ if (aKeys.length != bKeys.length) return false;
610
+ // Recursive comparison of contents.
611
+ for (var key in a) if (!(key in b) || !_.isEqual(a[key], b[key])) return false;
612
+ return true;
613
+ };
614
+
615
+ // Is a given array or object empty?
616
+ _.isEmpty = function(obj) {
617
+ if (_.isArray(obj) || _.isString(obj)) return obj.length === 0;
618
+ for (var key in obj) if (hasOwnProperty.call(obj, key)) return false;
619
+ return true;
620
+ };
621
+
622
+ // Is a given value a DOM element?
623
+ _.isElement = function(obj) {
624
+ return !!(obj && obj.nodeType == 1);
625
+ };
626
+
627
+ // Is a given value an array?
628
+ // Delegates to ECMA5's native Array.isArray
629
+ _.isArray = nativeIsArray || function(obj) {
630
+ return toString.call(obj) === '[object Array]';
631
+ };
632
+
633
+ // Is a given variable an arguments object?
634
+ _.isArguments = function(obj) {
635
+ return !!(obj && hasOwnProperty.call(obj, 'callee'));
636
+ };
637
+
638
+ // Is a given value a function?
639
+ _.isFunction = function(obj) {
640
+ return !!(obj && obj.constructor && obj.call && obj.apply);
641
+ };
642
+
643
+ // Is a given value a string?
644
+ _.isString = function(obj) {
645
+ return !!(obj === '' || (obj && obj.charCodeAt && obj.substr));
646
+ };
647
+
648
+ // Is a given value a number?
649
+ _.isNumber = function(obj) {
650
+ return !!(obj === 0 || (obj && obj.toExponential && obj.toFixed));
651
+ };
652
+
653
+ // Is the given value `NaN`? `NaN` happens to be the only value in JavaScript
654
+ // that does not equal itself.
655
+ _.isNaN = function(obj) {
656
+ return obj !== obj;
657
+ };
658
+
659
+ // Is a given value a boolean?
660
+ _.isBoolean = function(obj) {
661
+ return obj === true || obj === false;
662
+ };
663
+
664
+ // Is a given value a date?
665
+ _.isDate = function(obj) {
666
+ return !!(obj && obj.getTimezoneOffset && obj.setUTCFullYear);
667
+ };
668
+
669
+ // Is the given value a regular expression?
670
+ _.isRegExp = function(obj) {
671
+ return !!(obj && obj.test && obj.exec && (obj.ignoreCase || obj.ignoreCase === false));
672
+ };
673
+
674
+ // Is a given value equal to null?
675
+ _.isNull = function(obj) {
676
+ return obj === null;
677
+ };
678
+
679
+ // Is a given variable undefined?
680
+ _.isUndefined = function(obj) {
681
+ return obj === void 0;
682
+ };
683
+
684
+ // Utility Functions
685
+ // -----------------
686
+
687
+ // Run Underscore.js in *noConflict* mode, returning the `_` variable to its
688
+ // previous owner. Returns a reference to the Underscore object.
689
+ _.noConflict = function() {
690
+ root._ = previousUnderscore;
691
+ return this;
692
+ };
693
+
694
+ // Keep the identity function around for default iterators.
695
+ _.identity = function(value) {
696
+ return value;
697
+ };
698
+
699
+ // Run a function **n** times.
700
+ _.times = function (n, iterator, context) {
701
+ for (var i = 0; i < n; i++) iterator.call(context, i);
702
+ };
703
+
704
+ // Add your own custom functions to the Underscore object, ensuring that
705
+ // they're correctly added to the OOP wrapper as well.
706
+ _.mixin = function(obj) {
707
+ each(_.functions(obj), function(name){
708
+ addToWrapper(name, _[name] = obj[name]);
709
+ });
710
+ };
711
+
712
+ // Generate a unique integer id (unique within the entire client session).
713
+ // Useful for temporary DOM ids.
714
+ var idCounter = 0;
715
+ _.uniqueId = function(prefix) {
716
+ var id = idCounter++;
717
+ return prefix ? prefix + id : id;
718
+ };
719
+
720
+ // By default, Underscore uses ERB-style template delimiters, change the
721
+ // following template settings to use alternative delimiters.
722
+ _.templateSettings = {
723
+ evaluate : /<%([\s\S]+?)%>/g,
724
+ interpolate : /<%=([\s\S]+?)%>/g
725
+ };
726
+
727
+ // JavaScript micro-templating, similar to John Resig's implementation.
728
+ // Underscore templating handles arbitrary delimiters, preserves whitespace,
729
+ // and correctly escapes quotes within interpolated code.
730
+ _.template = function(str, data) {
731
+ var c = _.templateSettings;
732
+ var tmpl = 'var __p=[],print=function(){__p.push.apply(__p,arguments);};' +
733
+ 'with(obj||{}){__p.push(\'' +
734
+ str.replace(/\\/g, '\\\\')
735
+ .replace(/'/g, "\\'")
736
+ .replace(c.interpolate, function(match, code) {
737
+ return "'," + code.replace(/\\'/g, "'") + ",'";
738
+ })
739
+ .replace(c.evaluate || null, function(match, code) {
740
+ return "');" + code.replace(/\\'/g, "'")
741
+ .replace(/[\r\n\t]/g, ' ') + "__p.push('";
742
+ })
743
+ .replace(/\r/g, '\\r')
744
+ .replace(/\n/g, '\\n')
745
+ .replace(/\t/g, '\\t')
746
+ + "');}return __p.join('');";
747
+ var func = new Function('obj', tmpl);
748
+ return data ? func(data) : func;
749
+ };
750
+
751
+ // The OOP Wrapper
752
+ // ---------------
753
+
754
+ // If Underscore is called as a function, it returns a wrapped object that
755
+ // can be used OO-style. This wrapper holds altered versions of all the
756
+ // underscore functions. Wrapped objects may be chained.
757
+ var wrapper = function(obj) { this._wrapped = obj; };
758
+
759
+ // Expose `wrapper.prototype` as `_.prototype`
760
+ _.prototype = wrapper.prototype;
761
+
762
+ // Helper function to continue chaining intermediate results.
763
+ var result = function(obj, chain) {
764
+ return chain ? _(obj).chain() : obj;
765
+ };
766
+
767
+ // A method to easily add functions to the OOP wrapper.
768
+ var addToWrapper = function(name, func) {
769
+ wrapper.prototype[name] = function() {
770
+ var args = slice.call(arguments);
771
+ unshift.call(args, this._wrapped);
772
+ return result(func.apply(_, args), this._chain);
773
+ };
774
+ };
775
+
776
+ // Add all of the Underscore functions to the wrapper object.
777
+ _.mixin(_);
778
+
779
+ // Add all mutator Array functions to the wrapper.
780
+ each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(name) {
781
+ var method = ArrayProto[name];
782
+ wrapper.prototype[name] = function() {
783
+ method.apply(this._wrapped, arguments);
784
+ return result(this._wrapped, this._chain);
785
+ };
786
+ });
787
+
788
+ // Add all accessor Array functions to the wrapper.
789
+ each(['concat', 'join', 'slice'], function(name) {
790
+ var method = ArrayProto[name];
791
+ wrapper.prototype[name] = function() {
792
+ return result(method.apply(this._wrapped, arguments), this._chain);
793
+ };
794
+ });
795
+
796
+ // Start chaining a wrapped Underscore object.
797
+ wrapper.prototype.chain = function() {
798
+ this._chain = true;
799
+ return this;
800
+ };
801
+
802
+ // Extracts the result from a wrapped and chained object.
803
+ wrapper.prototype.value = function() {
804
+ return this._wrapped;
805
+ };
806
+
807
+ })();