rrd-grapher 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (122) hide show
  1. data/.gitignore +9 -0
  2. data/.rvmrc +1 -0
  3. data/Gemfile +11 -0
  4. data/Guardfile +123 -0
  5. data/README.md +91 -0
  6. data/Rakefile +42 -0
  7. data/example_app/Gemfile +9 -0
  8. data/example_app/Guardfile +18 -0
  9. data/example_app/app.rb +38 -0
  10. data/example_app/assets/javascripts/available_rrds.coffee +13 -0
  11. data/example_app/assets/javascripts/collectd.coffee +48 -0
  12. data/example_app/config.ru +9 -0
  13. data/example_app/public/.gitignore +1 -0
  14. data/example_app/public/chosen/chosen-sprite.png +0 -0
  15. data/example_app/public/chosen/chosen.css +340 -0
  16. data/example_app/public/chosen/chosen.jquery.js +786 -0
  17. data/example_app/public/chosen/chosen.jquery.min.js +10 -0
  18. data/example_app/public/javascripts/available_rrds.js +16 -0
  19. data/example_app/public/javascripts/collectd.js +64 -0
  20. data/example_app/public/javascripts/jquery.timeago.js +148 -0
  21. data/example_app/public/stylesheets/Aristo/images/bg_fallback.png +0 -0
  22. data/example_app/public/stylesheets/Aristo/images/icon_sprite.png +0 -0
  23. data/example_app/public/stylesheets/Aristo/images/progress_bar.gif +0 -0
  24. data/example_app/public/stylesheets/Aristo/images/slider_handles.png +0 -0
  25. data/example_app/public/stylesheets/Aristo/images/ui-icons_222222_256x240.png +0 -0
  26. data/example_app/public/stylesheets/Aristo/images/ui-icons_454545_256x240.png +0 -0
  27. data/example_app/public/stylesheets/Aristo/theme.css +738 -0
  28. data/example_app/views/available_rrds.haml +26 -0
  29. data/example_app/views/collectd.haml +30 -0
  30. data/example_app/views/layout.haml +19 -0
  31. data/example_app/views/stylesheets/available_rrds.scss +7 -0
  32. data/example_notifier/Gemfile +2 -0
  33. data/example_notifier/notifier.rb +25 -0
  34. data/lib/rrd-grapher/assets/javascripts/app-dev.js +20 -0
  35. data/lib/rrd-grapher/assets/javascripts/app.js +20 -0
  36. data/lib/rrd-grapher/assets/javascripts/classes/format.coffee +35 -0
  37. data/lib/rrd-grapher/assets/javascripts/classes/graph.coffee +306 -0
  38. data/lib/rrd-grapher/assets/javascripts/classes/graph_definition.coffee +216 -0
  39. data/lib/rrd-grapher/assets/javascripts/classes/serie.coffee +13 -0
  40. data/lib/rrd-grapher/assets/javascripts/classes/size.coffee +5 -0
  41. data/lib/rrd-grapher/assets/javascripts/classes/static_line.coffee +44 -0
  42. data/lib/rrd-grapher/assets/javascripts/classes/time.coffee +17 -0
  43. data/lib/rrd-grapher/notifier/alarm_manager.rb +190 -0
  44. data/lib/rrd-grapher/notifier/alarm_trigger.rb +187 -0
  45. data/lib/rrd-grapher/notifier/alarms.rb +79 -0
  46. data/lib/rrd-grapher/notifier/collectdrb.rb +86 -0
  47. data/lib/rrd-grapher/notifier/data_struct.rb +46 -0
  48. data/lib/rrd-grapher/notifier/default_user_handler.rb +36 -0
  49. data/lib/rrd-grapher/notifier/parsers/bindata_parser.rb +144 -0
  50. data/lib/rrd-grapher/notifier/parsers/ruby_parser.rb +134 -0
  51. data/lib/rrd-grapher/notifier/structures.rb +80 -0
  52. data/lib/rrd-grapher/notifier.rb +87 -0
  53. data/lib/rrd-grapher/public/favicon.ico +0 -0
  54. data/lib/rrd-grapher/public/javascripts/app-dev.js +13709 -0
  55. data/lib/rrd-grapher/public/javascripts/app.js +4057 -0
  56. data/lib/rrd-grapher/public/javascripts/backbone/backbone.js +1155 -0
  57. data/lib/rrd-grapher/public/javascripts/backbone/backbone.min.js +32 -0
  58. data/lib/rrd-grapher/public/javascripts/backbone/underscore.js +841 -0
  59. data/lib/rrd-grapher/public/javascripts/backbone/underscore.min.js +27 -0
  60. data/lib/rrd-grapher/public/javascripts/classes/format.js +42 -0
  61. data/lib/rrd-grapher/public/javascripts/classes/graph.js +360 -0
  62. data/lib/rrd-grapher/public/javascripts/classes/graph_definition.js +298 -0
  63. data/lib/rrd-grapher/public/javascripts/classes/serie.js +32 -0
  64. data/lib/rrd-grapher/public/javascripts/classes/size.js +7 -0
  65. data/lib/rrd-grapher/public/javascripts/classes/static_line.js +48 -0
  66. data/lib/rrd-grapher/public/javascripts/classes/time.js +17 -0
  67. data/lib/rrd-grapher/public/javascripts/flot/.gitignore +4 -0
  68. data/lib/rrd-grapher/public/javascripts/flot/excanvas.min.js +1 -0
  69. data/lib/rrd-grapher/public/javascripts/flot/jquery.colorhelpers.min.js +1 -0
  70. data/lib/rrd-grapher/public/javascripts/flot/jquery.flot.crosshair.min.js +1 -0
  71. data/lib/rrd-grapher/public/javascripts/flot/jquery.flot.fillbetween.min.js +1 -0
  72. data/lib/rrd-grapher/public/javascripts/flot/jquery.flot.image.min.js +1 -0
  73. data/lib/rrd-grapher/public/javascripts/flot/jquery.flot.js +2604 -0
  74. data/lib/rrd-grapher/public/javascripts/flot/jquery.flot.navigate.min.js +1 -0
  75. data/lib/rrd-grapher/public/javascripts/flot/jquery.flot.pie.min.js +1 -0
  76. data/lib/rrd-grapher/public/javascripts/flot/jquery.flot.resize.min.js +1 -0
  77. data/lib/rrd-grapher/public/javascripts/flot/jquery.flot.selection.js +345 -0
  78. data/lib/rrd-grapher/public/javascripts/flot/jquery.flot.selection.min.js +1 -0
  79. data/lib/rrd-grapher/public/javascripts/flot/jquery.flot.stack.min.js +1 -0
  80. data/lib/rrd-grapher/public/javascripts/flot/jquery.flot.symbol.min.js +1 -0
  81. data/lib/rrd-grapher/public/javascripts/flot/jquery.flot.threshold.min.js +1 -0
  82. data/lib/rrd-grapher/public/javascripts/jquery/jquery-1.6.2.js +8981 -0
  83. data/lib/rrd-grapher/public/javascripts/jquery/jquery-1.6.2.min.js +18 -0
  84. data/lib/rrd-grapher/public/javascripts/jquery/jquery-ui-1.8.11.min.js +783 -0
  85. data/lib/rrd-grapher/public/javascripts/jquery/jquery.showtime.js +63 -0
  86. data/lib/rrd-grapher/public/javascripts/jquery/jquery.tpl.min.js +10 -0
  87. data/lib/rrd-grapher/resources.rb +14 -0
  88. data/lib/rrd-grapher/rrd.rb +238 -0
  89. data/lib/rrd-grapher/rrd_server.rb +78 -0
  90. data/lib/rrd-grapher/version.rb +4 -0
  91. data/lib/rrd-grapher/views/stylesheets/app.scss +111 -0
  92. data/lib/rrd-grapher.rb +12 -0
  93. data/rrd-grapher.gemspec +47 -0
  94. data/spec/common.rb +70 -0
  95. data/spec/data/myrouter.rrd +0 -0
  96. data/spec/data/subdata.rrd +0 -0
  97. data/spec/data/test.rrd +0 -0
  98. data/spec/factories.rb +23 -0
  99. data/spec/javascripts/helpers/jasmine-jquery-1.3.0.js +283 -0
  100. data/spec/javascripts/source/format_spec.coffee +18 -0
  101. data/spec/javascripts/source/graph_def_spec.coffee +27 -0
  102. data/spec/javascripts/source/graph_spec.coffee +63 -0
  103. data/spec/javascripts/source/serie_spec.coffee +28 -0
  104. data/spec/javascripts/source/static_line_spec.coffee +13 -0
  105. data/spec/javascripts/source/time_spec.coffee +26 -0
  106. data/spec/javascripts/support/jasmine.yml +78 -0
  107. data/spec/javascripts/support/jasmine_config.rb +23 -0
  108. data/spec/javascripts/support/jasmine_runner.rb +32 -0
  109. data/spec/unit/alarm_manager_spec.rb +252 -0
  110. data/spec/unit/alarm_trigger_spec.rb +26 -0
  111. data/spec/unit/data_struct_spec.rb +55 -0
  112. data/spec/unit/notifier_spec.rb +45 -0
  113. data/spec/unit/parsers/bindata_parser_spec.rb +184 -0
  114. data/spec/unit/parsers/ruby_parser_spec.rb +184 -0
  115. data/spec/unit/rrd_spec.rb +50 -0
  116. data/spec/unit/structures_spec.rb +28 -0
  117. data/tests/4series.rrd +0 -0
  118. data/tests/analyze_rrd.rb +62 -0
  119. data/tests/exact.rrd +0 -0
  120. data/tests/exact2.rrd +0 -0
  121. data/tests/filler.rb +46 -0
  122. metadata +414 -0
@@ -0,0 +1,841 @@
1
+ /*
2
+ Underscore.js 1.1.7
3
+ (c) 2011 Jeremy Ashkenas, DocumentCloud Inc.
4
+ Underscore is freely distributable under the MIT license.
5
+ Portions of Underscore are inspired or borrowed from Prototype,
6
+ Oliver Steele's Functional, and John Resig's Micro-Templating.
7
+ For all details and documentation:
8
+ http://documentcloud.github.com/underscore
9
+ */
10
+
11
+ (function() {
12
+
13
+ // Baseline setup
14
+ // --------------
15
+
16
+ // Establish the root object, `window` in the browser, or `global` on the server.
17
+ var root = this;
18
+
19
+ // Save the previous value of the `_` variable.
20
+ var previousUnderscore = root._;
21
+
22
+ // Establish the object that gets returned to break out of a loop iteration.
23
+ var breaker = {};
24
+
25
+ // Save bytes in the minified (but not gzipped) version:
26
+ var ArrayProto = Array.prototype, ObjProto = Object.prototype, FuncProto = Function.prototype;
27
+
28
+ // Create quick reference variables for speed access to core prototypes.
29
+ var slice = ArrayProto.slice,
30
+ unshift = ArrayProto.unshift,
31
+ toString = ObjProto.toString,
32
+ hasOwnProperty = ObjProto.hasOwnProperty;
33
+
34
+ // All **ECMAScript 5** native function implementations that we hope to use
35
+ // are declared here.
36
+ var
37
+ nativeForEach = ArrayProto.forEach,
38
+ nativeMap = ArrayProto.map,
39
+ nativeReduce = ArrayProto.reduce,
40
+ nativeReduceRight = ArrayProto.reduceRight,
41
+ nativeFilter = ArrayProto.filter,
42
+ nativeEvery = ArrayProto.every,
43
+ nativeSome = ArrayProto.some,
44
+ nativeIndexOf = ArrayProto.indexOf,
45
+ nativeLastIndexOf = ArrayProto.lastIndexOf,
46
+ nativeIsArray = Array.isArray,
47
+ nativeKeys = Object.keys,
48
+ nativeBind = FuncProto.bind;
49
+
50
+ // Create a safe reference to the Underscore object for use below.
51
+ var _ = function(obj) { return new wrapper(obj); };
52
+
53
+ // Export the Underscore object for **CommonJS**, with backwards-compatibility
54
+ // for the old `require()` API. If we're not in CommonJS, add `_` to the
55
+ // global object.
56
+ if (typeof module !== 'undefined' && module.exports) {
57
+ module.exports = _;
58
+ _._ = _;
59
+ } else {
60
+ // Exported as a string, for Closure Compiler "advanced" mode.
61
+ root['_'] = _;
62
+ }
63
+
64
+ // Current version.
65
+ _.VERSION = '1.1.7';
66
+
67
+ // Collection Functions
68
+ // --------------------
69
+
70
+ // The cornerstone, an `each` implementation, aka `forEach`.
71
+ // Handles objects with the built-in `forEach`, arrays, and raw objects.
72
+ // Delegates to **ECMAScript 5**'s native `forEach` if available.
73
+ var each = _.each = _.forEach = function(obj, iterator, context) {
74
+ if (obj == null) return;
75
+ if (nativeForEach && obj.forEach === nativeForEach) {
76
+ obj.forEach(iterator, context);
77
+ } else if (obj.length === +obj.length) {
78
+ for (var i = 0, l = obj.length; i < l; i++) {
79
+ if (i in obj && iterator.call(context, obj[i], i, obj) === breaker) return;
80
+ }
81
+ } else {
82
+ for (var key in obj) {
83
+ if (hasOwnProperty.call(obj, key)) {
84
+ if (iterator.call(context, obj[key], key, obj) === breaker) return;
85
+ }
86
+ }
87
+ }
88
+ };
89
+
90
+ // Return the results of applying the iterator to each element.
91
+ // Delegates to **ECMAScript 5**'s native `map` if available.
92
+ _.map = function(obj, iterator, context) {
93
+ var results = [];
94
+ if (obj == null) return results;
95
+ if (nativeMap && obj.map === nativeMap) return obj.map(iterator, context);
96
+ each(obj, function(value, index, list) {
97
+ results[results.length] = iterator.call(context, value, index, list);
98
+ });
99
+ return results;
100
+ };
101
+
102
+ // **Reduce** builds up a single result from a list of values, aka `inject`,
103
+ // or `foldl`. Delegates to **ECMAScript 5**'s native `reduce` if available.
104
+ _.reduce = _.foldl = _.inject = function(obj, iterator, memo, context) {
105
+ var initial = memo !== void 0;
106
+ if (obj == null) obj = [];
107
+ if (nativeReduce && obj.reduce === nativeReduce) {
108
+ if (context) iterator = _.bind(iterator, context);
109
+ return initial ? obj.reduce(iterator, memo) : obj.reduce(iterator);
110
+ }
111
+ each(obj, function(value, index, list) {
112
+ if (!initial) {
113
+ memo = value;
114
+ initial = true;
115
+ } else {
116
+ memo = iterator.call(context, memo, value, index, list);
117
+ }
118
+ });
119
+ if (!initial) throw new TypeError("Reduce of empty array with no initial value");
120
+ return memo;
121
+ };
122
+
123
+ // The right-associative version of reduce, also known as `foldr`.
124
+ // Delegates to **ECMAScript 5**'s native `reduceRight` if available.
125
+ _.reduceRight = _.foldr = function(obj, iterator, memo, context) {
126
+ if (obj == null) obj = [];
127
+ if (nativeReduceRight && obj.reduceRight === nativeReduceRight) {
128
+ if (context) iterator = _.bind(iterator, context);
129
+ return memo !== void 0 ? obj.reduceRight(iterator, memo) : obj.reduceRight(iterator);
130
+ }
131
+ var reversed = (_.isArray(obj) ? obj.slice() : _.toArray(obj)).reverse();
132
+ return _.reduce(reversed, iterator, memo, context);
133
+ };
134
+
135
+ // Return the first value which passes a truth test. Aliased as `detect`.
136
+ _.find = _.detect = function(obj, iterator, context) {
137
+ var result;
138
+ any(obj, function(value, index, list) {
139
+ if (iterator.call(context, value, index, list)) {
140
+ result = value;
141
+ return true;
142
+ }
143
+ });
144
+ return result;
145
+ };
146
+
147
+ // Return all the elements that pass a truth test.
148
+ // Delegates to **ECMAScript 5**'s native `filter` if available.
149
+ // Aliased as `select`.
150
+ _.filter = _.select = function(obj, iterator, context) {
151
+ var results = [];
152
+ if (obj == null) return results;
153
+ if (nativeFilter && obj.filter === nativeFilter) return obj.filter(iterator, context);
154
+ each(obj, function(value, index, list) {
155
+ if (iterator.call(context, value, index, list)) results[results.length] = value;
156
+ });
157
+ return results;
158
+ };
159
+
160
+ // Return all the elements for which a truth test fails.
161
+ _.reject = function(obj, iterator, context) {
162
+ var results = [];
163
+ if (obj == null) return results;
164
+ each(obj, function(value, index, list) {
165
+ if (!iterator.call(context, value, index, list)) results[results.length] = value;
166
+ });
167
+ return results;
168
+ };
169
+
170
+ // Determine whether all of the elements match a truth test.
171
+ // Delegates to **ECMAScript 5**'s native `every` if available.
172
+ // Aliased as `all`.
173
+ _.every = _.all = function(obj, iterator, context) {
174
+ var result = true;
175
+ if (obj == null) return result;
176
+ if (nativeEvery && obj.every === nativeEvery) return obj.every(iterator, context);
177
+ each(obj, function(value, index, list) {
178
+ if (!(result = result && iterator.call(context, value, index, list))) return breaker;
179
+ });
180
+ return result;
181
+ };
182
+
183
+ // Determine if at least one element in the object matches a truth test.
184
+ // Delegates to **ECMAScript 5**'s native `some` if available.
185
+ // Aliased as `any`.
186
+ var any = _.some = _.any = function(obj, iterator, context) {
187
+ iterator = iterator || _.identity;
188
+ var result = false;
189
+ if (obj == null) return result;
190
+ if (nativeSome && obj.some === nativeSome) return obj.some(iterator, context);
191
+ each(obj, function(value, index, list) {
192
+ if (result |= iterator.call(context, value, index, list)) return breaker;
193
+ });
194
+ return !!result;
195
+ };
196
+
197
+ // Determine if a given value is included in the array or object using `===`.
198
+ // Aliased as `contains`.
199
+ _.include = _.contains = function(obj, target) {
200
+ var found = false;
201
+ if (obj == null) return found;
202
+ if (nativeIndexOf && obj.indexOf === nativeIndexOf) return obj.indexOf(target) != -1;
203
+ any(obj, function(value) {
204
+ if (found = value === target) return true;
205
+ });
206
+ return found;
207
+ };
208
+
209
+ // Invoke a method (with arguments) on every item in a collection.
210
+ _.invoke = function(obj, method) {
211
+ var args = slice.call(arguments, 2);
212
+ return _.map(obj, function(value) {
213
+ return (method.call ? method || value : value[method]).apply(value, args);
214
+ });
215
+ };
216
+
217
+ // Convenience version of a common use case of `map`: fetching a property.
218
+ _.pluck = function(obj, key) {
219
+ return _.map(obj, function(value){ return value[key]; });
220
+ };
221
+
222
+ // Return the maximum element or (element-based computation).
223
+ _.max = function(obj, iterator, context) {
224
+ if (!iterator && _.isArray(obj)) return Math.max.apply(Math, obj);
225
+ var result = {computed : -Infinity};
226
+ each(obj, function(value, index, list) {
227
+ var computed = iterator ? iterator.call(context, value, index, list) : value;
228
+ computed >= result.computed && (result = {value : value, computed : computed});
229
+ });
230
+ return result.value;
231
+ };
232
+
233
+ // Return the minimum element (or element-based computation).
234
+ _.min = function(obj, iterator, context) {
235
+ if (!iterator && _.isArray(obj)) return Math.min.apply(Math, obj);
236
+ var result = {computed : Infinity};
237
+ each(obj, function(value, index, list) {
238
+ var computed = iterator ? iterator.call(context, value, index, list) : value;
239
+ computed < result.computed && (result = {value : value, computed : computed});
240
+ });
241
+ return result.value;
242
+ };
243
+
244
+ // Sort the object's values by a criterion produced by an iterator.
245
+ _.sortBy = function(obj, iterator, context) {
246
+ return _.pluck(_.map(obj, function(value, index, list) {
247
+ return {
248
+ value : value,
249
+ criteria : iterator.call(context, value, index, list)
250
+ };
251
+ }).sort(function(left, right) {
252
+ var a = left.criteria, b = right.criteria;
253
+ return a < b ? -1 : a > b ? 1 : 0;
254
+ }), 'value');
255
+ };
256
+
257
+ // Groups the object's values by a criterion produced by an iterator
258
+ _.groupBy = function(obj, iterator) {
259
+ var result = {};
260
+ each(obj, function(value, index) {
261
+ var key = iterator(value, index);
262
+ (result[key] || (result[key] = [])).push(value);
263
+ });
264
+ return result;
265
+ };
266
+
267
+ // Use a comparator function to figure out at what index an object should
268
+ // be inserted so as to maintain order. Uses binary search.
269
+ _.sortedIndex = function(array, obj, iterator) {
270
+ iterator || (iterator = _.identity);
271
+ var low = 0, high = array.length;
272
+ while (low < high) {
273
+ var mid = (low + high) >> 1;
274
+ iterator(array[mid]) < iterator(obj) ? low = mid + 1 : high = mid;
275
+ }
276
+ return low;
277
+ };
278
+
279
+ // Safely convert anything iterable into a real, live array.
280
+ _.toArray = function(iterable) {
281
+ if (!iterable) return [];
282
+ if (iterable.toArray) return iterable.toArray();
283
+ if (_.isArray(iterable)) return slice.call(iterable);
284
+ if (_.isArguments(iterable)) return slice.call(iterable);
285
+ return _.values(iterable);
286
+ };
287
+
288
+ // Return the number of elements in an object.
289
+ _.size = function(obj) {
290
+ return _.toArray(obj).length;
291
+ };
292
+
293
+ // Array Functions
294
+ // ---------------
295
+
296
+ // Get the first element of an array. Passing **n** will return the first N
297
+ // values in the array. Aliased as `head`. The **guard** check allows it to work
298
+ // with `_.map`.
299
+ _.first = _.head = function(array, n, guard) {
300
+ return (n != null) && !guard ? slice.call(array, 0, n) : array[0];
301
+ };
302
+
303
+ // Returns everything but the first entry of the array. Aliased as `tail`.
304
+ // Especially useful on the arguments object. Passing an **index** will return
305
+ // the rest of the values in the array from that index onward. The **guard**
306
+ // check allows it to work with `_.map`.
307
+ _.rest = _.tail = function(array, index, guard) {
308
+ return slice.call(array, (index == null) || guard ? 1 : index);
309
+ };
310
+
311
+ // Get the last element of an array.
312
+ _.last = function(array) {
313
+ return array[array.length - 1];
314
+ };
315
+
316
+ // Trim out all falsy values from an array.
317
+ _.compact = function(array) {
318
+ return _.filter(array, function(value){ return !!value; });
319
+ };
320
+
321
+ // Return a completely flattened version of an array.
322
+ _.flatten = function(array) {
323
+ return _.reduce(array, function(memo, value) {
324
+ if (_.isArray(value)) return memo.concat(_.flatten(value));
325
+ memo[memo.length] = value;
326
+ return memo;
327
+ }, []);
328
+ };
329
+
330
+ // Return a version of the array that does not contain the specified value(s).
331
+ _.without = function(array) {
332
+ return _.difference(array, slice.call(arguments, 1));
333
+ };
334
+
335
+ // Produce a duplicate-free version of the array. If the array has already
336
+ // been sorted, you have the option of using a faster algorithm.
337
+ // Aliased as `unique`.
338
+ _.uniq = _.unique = function(array, isSorted) {
339
+ return _.reduce(array, function(memo, el, i) {
340
+ if (0 == i || (isSorted === true ? _.last(memo) != el : !_.include(memo, el))) memo[memo.length] = el;
341
+ return memo;
342
+ }, []);
343
+ };
344
+
345
+ // Produce an array that contains the union: each distinct element from all of
346
+ // the passed-in arrays.
347
+ _.union = function() {
348
+ return _.uniq(_.flatten(arguments));
349
+ };
350
+
351
+ // Produce an array that contains every item shared between all the
352
+ // passed-in arrays. (Aliased as "intersect" for back-compat.)
353
+ _.intersection = _.intersect = function(array) {
354
+ var rest = slice.call(arguments, 1);
355
+ return _.filter(_.uniq(array), function(item) {
356
+ return _.every(rest, function(other) {
357
+ return _.indexOf(other, item) >= 0;
358
+ });
359
+ });
360
+ };
361
+
362
+ // Take the difference between one array and another.
363
+ // Only the elements present in just the first array will remain.
364
+ _.difference = function(array, other) {
365
+ return _.filter(array, function(value){ return !_.include(other, value); });
366
+ };
367
+
368
+ // Zip together multiple lists into a single array -- elements that share
369
+ // an index go together.
370
+ _.zip = function() {
371
+ var args = slice.call(arguments);
372
+ var length = _.max(_.pluck(args, 'length'));
373
+ var results = new Array(length);
374
+ for (var i = 0; i < length; i++) results[i] = _.pluck(args, "" + i);
375
+ return results;
376
+ };
377
+
378
+ // If the browser doesn't supply us with indexOf (I'm looking at you, **MSIE**),
379
+ // we need this function. Return the position of the first occurrence of an
380
+ // item in an array, or -1 if the item is not included in the array.
381
+ // Delegates to **ECMAScript 5**'s native `indexOf` if available.
382
+ // If the array is large and already in sort order, pass `true`
383
+ // for **isSorted** to use binary search.
384
+ _.indexOf = function(array, item, isSorted) {
385
+ if (array == null) return -1;
386
+ var i, l;
387
+ if (isSorted) {
388
+ i = _.sortedIndex(array, item);
389
+ return array[i] === item ? i : -1;
390
+ }
391
+ if (nativeIndexOf && array.indexOf === nativeIndexOf) return array.indexOf(item);
392
+ for (i = 0, l = array.length; i < l; i++) if (array[i] === item) return i;
393
+ return -1;
394
+ };
395
+
396
+
397
+ // Delegates to **ECMAScript 5**'s native `lastIndexOf` if available.
398
+ _.lastIndexOf = function(array, item) {
399
+ if (array == null) return -1;
400
+ if (nativeLastIndexOf && array.lastIndexOf === nativeLastIndexOf) return array.lastIndexOf(item);
401
+ var i = array.length;
402
+ while (i--) if (array[i] === item) return i;
403
+ return -1;
404
+ };
405
+
406
+ // Generate an integer Array containing an arithmetic progression. A port of
407
+ // the native Python `range()` function. See
408
+ // [the Python documentation](http://docs.python.org/library/functions.html#range).
409
+ _.range = function(start, stop, step) {
410
+ if (arguments.length <= 1) {
411
+ stop = start || 0;
412
+ start = 0;
413
+ }
414
+ step = arguments[2] || 1;
415
+
416
+ var len = Math.max(Math.ceil((stop - start) / step), 0);
417
+ var idx = 0;
418
+ var range = new Array(len);
419
+
420
+ while(idx < len) {
421
+ range[idx++] = start;
422
+ start += step;
423
+ }
424
+
425
+ return range;
426
+ };
427
+
428
+ // Function (ahem) Functions
429
+ // ------------------
430
+
431
+ // Create a function bound to a given object (assigning `this`, and arguments,
432
+ // optionally). Binding with arguments is also known as `curry`.
433
+ // Delegates to **ECMAScript 5**'s native `Function.bind` if available.
434
+ // We check for `func.bind` first, to fail fast when `func` is undefined.
435
+ _.bind = function(func, obj) {
436
+ if (func.bind === nativeBind && nativeBind) return nativeBind.apply(func, slice.call(arguments, 1));
437
+ var args = slice.call(arguments, 2);
438
+ return function() {
439
+ return func.apply(obj, args.concat(slice.call(arguments)));
440
+ };
441
+ };
442
+
443
+ // Bind all of an object's methods to that object. Useful for ensuring that
444
+ // all callbacks defined on an object belong to it.
445
+ _.bindAll = function(obj) {
446
+ var funcs = slice.call(arguments, 1);
447
+ if (funcs.length == 0) funcs = _.functions(obj);
448
+ each(funcs, function(f) { obj[f] = _.bind(obj[f], obj); });
449
+ return obj;
450
+ };
451
+
452
+ // Memoize an expensive function by storing its results.
453
+ _.memoize = function(func, hasher) {
454
+ var memo = {};
455
+ hasher || (hasher = _.identity);
456
+ return function() {
457
+ var key = hasher.apply(this, arguments);
458
+ return hasOwnProperty.call(memo, key) ? memo[key] : (memo[key] = func.apply(this, arguments));
459
+ };
460
+ };
461
+
462
+ // Delays a function for the given number of milliseconds, and then calls
463
+ // it with the arguments supplied.
464
+ _.delay = function(func, wait) {
465
+ var args = slice.call(arguments, 2);
466
+ return setTimeout(function(){ return func.apply(func, args); }, wait);
467
+ };
468
+
469
+ // Defers a function, scheduling it to run after the current call stack has
470
+ // cleared.
471
+ _.defer = function(func) {
472
+ return _.delay.apply(_, [func, 1].concat(slice.call(arguments, 1)));
473
+ };
474
+
475
+ // Internal function used to implement `_.throttle` and `_.debounce`.
476
+ var limit = function(func, wait, debounce) {
477
+ var timeout;
478
+ return function() {
479
+ var context = this, args = arguments;
480
+ var throttler = function() {
481
+ timeout = null;
482
+ func.apply(context, args);
483
+ };
484
+ if (debounce) clearTimeout(timeout);
485
+ if (debounce || !timeout) timeout = setTimeout(throttler, wait);
486
+ };
487
+ };
488
+
489
+ // Returns a function, that, when invoked, will only be triggered at most once
490
+ // during a given window of time.
491
+ _.throttle = function(func, wait) {
492
+ return limit(func, wait, false);
493
+ };
494
+
495
+ // Returns a function, that, as long as it continues to be invoked, will not
496
+ // be triggered. The function will be called after it stops being called for
497
+ // N milliseconds.
498
+ _.debounce = function(func, wait) {
499
+ return limit(func, wait, true);
500
+ };
501
+
502
+ // Returns a function that will be executed at most one time, no matter how
503
+ // often you call it. Useful for lazy initialization.
504
+ _.once = function(func) {
505
+ var ran = false, memo;
506
+ return function() {
507
+ if (ran) return memo;
508
+ ran = true;
509
+ return memo = func.apply(this, arguments);
510
+ };
511
+ };
512
+
513
+ // Returns the first function passed as an argument to the second,
514
+ // allowing you to adjust arguments, run code before and after, and
515
+ // conditionally execute the original function.
516
+ _.wrap = function(func, wrapper) {
517
+ return function() {
518
+ var args = [func].concat(slice.call(arguments));
519
+ return wrapper.apply(this, args);
520
+ };
521
+ };
522
+
523
+ // Returns a function that is the composition of a list of functions, each
524
+ // consuming the return value of the function that follows.
525
+ _.compose = function() {
526
+ var funcs = slice.call(arguments);
527
+ return function() {
528
+ var args = slice.call(arguments);
529
+ for (var i = funcs.length - 1; i >= 0; i--) {
530
+ args = [funcs[i].apply(this, args)];
531
+ }
532
+ return args[0];
533
+ };
534
+ };
535
+
536
+ // Returns a function that will only be executed after being called N times.
537
+ _.after = function(times, func) {
538
+ return function() {
539
+ if (--times < 1) { return func.apply(this, arguments); }
540
+ };
541
+ };
542
+
543
+
544
+ // Object Functions
545
+ // ----------------
546
+
547
+ // Retrieve the names of an object's properties.
548
+ // Delegates to **ECMAScript 5**'s native `Object.keys`
549
+ _.keys = nativeKeys || function(obj) {
550
+ if (obj !== Object(obj)) throw new TypeError('Invalid object');
551
+ var keys = [];
552
+ for (var key in obj) if (hasOwnProperty.call(obj, key)) keys[keys.length] = key;
553
+ return keys;
554
+ };
555
+
556
+ // Retrieve the values of an object's properties.
557
+ _.values = function(obj) {
558
+ return _.map(obj, _.identity);
559
+ };
560
+
561
+ // Return a sorted list of the function names available on the object.
562
+ // Aliased as `methods`
563
+ _.functions = _.methods = function(obj) {
564
+ var names = [];
565
+ for (var key in obj) {
566
+ if (_.isFunction(obj[key])) names.push(key);
567
+ }
568
+ return names.sort();
569
+ };
570
+
571
+ // Extend a given object with all the properties in passed-in object(s).
572
+ _.extend = function(obj) {
573
+ each(slice.call(arguments, 1), function(source) {
574
+ for (var prop in source) {
575
+ if (source[prop] !== void 0) obj[prop] = source[prop];
576
+ }
577
+ });
578
+ return obj;
579
+ };
580
+
581
+ // Fill in a given object with default properties.
582
+ _.defaults = function(obj) {
583
+ each(slice.call(arguments, 1), function(source) {
584
+ for (var prop in source) {
585
+ if (obj[prop] == null) obj[prop] = source[prop];
586
+ }
587
+ });
588
+ return obj;
589
+ };
590
+
591
+ // Create a (shallow-cloned) duplicate of an object.
592
+ _.clone = function(obj) {
593
+ return _.isArray(obj) ? obj.slice() : _.extend({}, obj);
594
+ };
595
+
596
+ // Invokes interceptor with the obj, and then returns obj.
597
+ // The primary purpose of this method is to "tap into" a method chain, in
598
+ // order to perform operations on intermediate results within the chain.
599
+ _.tap = function(obj, interceptor) {
600
+ interceptor(obj);
601
+ return obj;
602
+ };
603
+
604
+ // Perform a deep comparison to check if two objects are equal.
605
+ _.isEqual = function(a, b) {
606
+ // Check object identity.
607
+ if (a === b) return true;
608
+ // Different types?
609
+ var atype = typeof(a), btype = typeof(b);
610
+ if (atype != btype) return false;
611
+ // Basic equality test (watch out for coercions).
612
+ if (a == b) return true;
613
+ // One is falsy and the other truthy.
614
+ if ((!a && b) || (a && !b)) return false;
615
+ // Unwrap any wrapped objects.
616
+ if (a._chain) a = a._wrapped;
617
+ if (b._chain) b = b._wrapped;
618
+ // One of them implements an isEqual()?
619
+ if (a.isEqual) return a.isEqual(b);
620
+ if (b.isEqual) return b.isEqual(a);
621
+ // Check dates' integer values.
622
+ if (_.isDate(a) && _.isDate(b)) return a.getTime() === b.getTime();
623
+ // Both are NaN?
624
+ if (_.isNaN(a) && _.isNaN(b)) return false;
625
+ // Compare regular expressions.
626
+ if (_.isRegExp(a) && _.isRegExp(b))
627
+ return a.source === b.source &&
628
+ a.global === b.global &&
629
+ a.ignoreCase === b.ignoreCase &&
630
+ a.multiline === b.multiline;
631
+ // If a is not an object by this point, we can't handle it.
632
+ if (atype !== 'object') return false;
633
+ // Check for different array lengths before comparing contents.
634
+ if (a.length && (a.length !== b.length)) return false;
635
+ // Nothing else worked, deep compare the contents.
636
+ var aKeys = _.keys(a), bKeys = _.keys(b);
637
+ // Different object sizes?
638
+ if (aKeys.length != bKeys.length) return false;
639
+ // Recursive comparison of contents.
640
+ for (var key in a) if (!(key in b) || !_.isEqual(a[key], b[key])) return false;
641
+ return true;
642
+ };
643
+
644
+ // Is a given array or object empty?
645
+ _.isEmpty = function(obj) {
646
+ if (_.isArray(obj) || _.isString(obj)) return obj.length === 0;
647
+ for (var key in obj) if (hasOwnProperty.call(obj, key)) return false;
648
+ return true;
649
+ };
650
+
651
+ // Is a given value a DOM element?
652
+ _.isElement = function(obj) {
653
+ return !!(obj && obj.nodeType == 1);
654
+ };
655
+
656
+ // Is a given value an array?
657
+ // Delegates to ECMA5's native Array.isArray
658
+ _.isArray = nativeIsArray || function(obj) {
659
+ return toString.call(obj) === '[object Array]';
660
+ };
661
+
662
+ // Is a given variable an object?
663
+ _.isObject = function(obj) {
664
+ return obj === Object(obj);
665
+ };
666
+
667
+ // Is a given variable an arguments object?
668
+ _.isArguments = function(obj) {
669
+ return !!(obj && hasOwnProperty.call(obj, 'callee'));
670
+ };
671
+
672
+ // Is a given value a function?
673
+ _.isFunction = function(obj) {
674
+ return !!(obj && obj.constructor && obj.call && obj.apply);
675
+ };
676
+
677
+ // Is a given value a string?
678
+ _.isString = function(obj) {
679
+ return !!(obj === '' || (obj && obj.charCodeAt && obj.substr));
680
+ };
681
+
682
+ // Is a given value a number?
683
+ _.isNumber = function(obj) {
684
+ return !!(obj === 0 || (obj && obj.toExponential && obj.toFixed));
685
+ };
686
+
687
+ // Is the given value `NaN`? `NaN` happens to be the only value in JavaScript
688
+ // that does not equal itself.
689
+ _.isNaN = function(obj) {
690
+ return obj !== obj;
691
+ };
692
+
693
+ // Is a given value a boolean?
694
+ _.isBoolean = function(obj) {
695
+ return obj === true || obj === false;
696
+ };
697
+
698
+ // Is a given value a date?
699
+ _.isDate = function(obj) {
700
+ return !!(obj && obj.getTimezoneOffset && obj.setUTCFullYear);
701
+ };
702
+
703
+ // Is the given value a regular expression?
704
+ _.isRegExp = function(obj) {
705
+ return !!(obj && obj.test && obj.exec && (obj.ignoreCase || obj.ignoreCase === false));
706
+ };
707
+
708
+ // Is a given value equal to null?
709
+ _.isNull = function(obj) {
710
+ return obj === null;
711
+ };
712
+
713
+ // Is a given variable undefined?
714
+ _.isUndefined = function(obj) {
715
+ return obj === void 0;
716
+ };
717
+
718
+ // Utility Functions
719
+ // -----------------
720
+
721
+ // Run Underscore.js in *noConflict* mode, returning the `_` variable to its
722
+ // previous owner. Returns a reference to the Underscore object.
723
+ _.noConflict = function() {
724
+ root._ = previousUnderscore;
725
+ return this;
726
+ };
727
+
728
+ // Keep the identity function around for default iterators.
729
+ _.identity = function(value) {
730
+ return value;
731
+ };
732
+
733
+ // Run a function **n** times.
734
+ _.times = function (n, iterator, context) {
735
+ for (var i = 0; i < n; i++) iterator.call(context, i);
736
+ };
737
+
738
+ // Add your own custom functions to the Underscore object, ensuring that
739
+ // they're correctly added to the OOP wrapper as well.
740
+ _.mixin = function(obj) {
741
+ each(_.functions(obj), function(name){
742
+ addToWrapper(name, _[name] = obj[name]);
743
+ });
744
+ };
745
+
746
+ // Generate a unique integer id (unique within the entire client session).
747
+ // Useful for temporary DOM ids.
748
+ var idCounter = 0;
749
+ _.uniqueId = function(prefix) {
750
+ var id = idCounter++;
751
+ return prefix ? prefix + id : id;
752
+ };
753
+
754
+ // By default, Underscore uses ERB-style template delimiters, change the
755
+ // following template settings to use alternative delimiters.
756
+ _.templateSettings = {
757
+ evaluate : /<%([\s\S]+?)%>/g,
758
+ interpolate : /<%=([\s\S]+?)%>/g
759
+ };
760
+
761
+ // JavaScript micro-templating, similar to John Resig's implementation.
762
+ // Underscore templating handles arbitrary delimiters, preserves whitespace,
763
+ // and correctly escapes quotes within interpolated code.
764
+ _.template = function(str, data) {
765
+ var c = _.templateSettings;
766
+ var tmpl = 'var __p=[],print=function(){__p.push.apply(__p,arguments);};' +
767
+ 'with(obj||{}){__p.push(\'' +
768
+ str.replace(/\\/g, '\\\\')
769
+ .replace(/'/g, "\\'")
770
+ .replace(c.interpolate, function(match, code) {
771
+ return "'," + code.replace(/\\'/g, "'") + ",'";
772
+ })
773
+ .replace(c.evaluate || null, function(match, code) {
774
+ return "');" + code.replace(/\\'/g, "'")
775
+ .replace(/[\r\n\t]/g, ' ') + "__p.push('";
776
+ })
777
+ .replace(/\r/g, '\\r')
778
+ .replace(/\n/g, '\\n')
779
+ .replace(/\t/g, '\\t')
780
+ + "');}return __p.join('');";
781
+ var func = new Function('obj', tmpl);
782
+ return data ? func(data) : func;
783
+ };
784
+
785
+ // The OOP Wrapper
786
+ // ---------------
787
+
788
+ // If Underscore is called as a function, it returns a wrapped object that
789
+ // can be used OO-style. This wrapper holds altered versions of all the
790
+ // underscore functions. Wrapped objects may be chained.
791
+ var wrapper = function(obj) { this._wrapped = obj; };
792
+
793
+ // Expose `wrapper.prototype` as `_.prototype`
794
+ _.prototype = wrapper.prototype;
795
+
796
+ // Helper function to continue chaining intermediate results.
797
+ var result = function(obj, chain) {
798
+ return chain ? _(obj).chain() : obj;
799
+ };
800
+
801
+ // A method to easily add functions to the OOP wrapper.
802
+ var addToWrapper = function(name, func) {
803
+ wrapper.prototype[name] = function() {
804
+ var args = slice.call(arguments);
805
+ unshift.call(args, this._wrapped);
806
+ return result(func.apply(_, args), this._chain);
807
+ };
808
+ };
809
+
810
+ // Add all of the Underscore functions to the wrapper object.
811
+ _.mixin(_);
812
+
813
+ // Add all mutator Array functions to the wrapper.
814
+ each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(name) {
815
+ var method = ArrayProto[name];
816
+ wrapper.prototype[name] = function() {
817
+ method.apply(this._wrapped, arguments);
818
+ return result(this._wrapped, this._chain);
819
+ };
820
+ });
821
+
822
+ // Add all accessor Array functions to the wrapper.
823
+ each(['concat', 'join', 'slice'], function(name) {
824
+ var method = ArrayProto[name];
825
+ wrapper.prototype[name] = function() {
826
+ return result(method.apply(this._wrapped, arguments), this._chain);
827
+ };
828
+ });
829
+
830
+ // Start chaining a wrapped Underscore object.
831
+ wrapper.prototype.chain = function() {
832
+ this._chain = true;
833
+ return this;
834
+ };
835
+
836
+ // Extracts the result from a wrapped and chained object.
837
+ wrapper.prototype.value = function() {
838
+ return this._wrapped;
839
+ };
840
+
841
+ })();