PageRenderTime 61ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 1ms

/ajax/libs/backbone.projections/1.0.0/backbone.projections.js

https://github.com/ninosys/cdnjs
JavaScript | 278 lines | 227 code | 47 blank | 4 comment | 56 complexity | 19b53f9c9d7398f7b599b694168a693b MD5 | raw file
Possible License(s): MIT, BSD-3-Clause
  1. (function(e){if("function"==typeof bootstrap)bootstrap("backboneprojections",e);else if("object"==typeof exports)module.exports=e();else if("function"==typeof define&&define.amd)define(e);else if("undefined"!=typeof ses){if(!ses.ok())return;ses.makeBackboneProjections=e}else"undefined"!=typeof window?window.BackboneProjections=e():global.BackboneProjections=e()})(function(){var define,ses,bootstrap,module,exports;
  2. return (function(e,t,n){function i(n,s){if(!t[n]){if(!e[n]){var o=typeof require=="function"&&require;if(!s&&o)return o(n,!0);if(r)return r(n,!0);throw new Error("Cannot find module '"+n+"'")}var u=t[n]={exports:{}};e[n][0].call(u.exports,function(t){var r=e[n][1][t];return i(r?r:t)},u,u.exports)}return t[n].exports}var r=typeof require=="function"&&require;for(var s=0;s<n.length;s++)i(n[s]);return i})({"lib/index.js":[function(require,module,exports){
  3. // Generated by CoffeeScript 1.6.2
  4. var extend;
  5. extend = ((typeof window !== "undefined" && window !== null ? window._ : void 0) || require('underscore')).extend;
  6. module.exports = extend({}, require('./capped'), require('./filtered'), require('./sorted'));
  7. },{"./capped":"lib/capped.js","./filtered":"lib/filtered.js","./sorted":"lib/sorted.js","underscore":"underscore"}],"lib/capped.js":[function(require,module,exports){
  8. // Generated by CoffeeScript 1.6.2
  9. var Collection, extend, inducedOrdering, toArray, _ref,
  10. __hasProp = {}.hasOwnProperty,
  11. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  12. Collection = ((typeof window !== "undefined" && window !== null ? window.Backbone : void 0) || require('backbone')).Collection;
  13. _ref = (typeof window !== "undefined" && window !== null ? window._ : void 0) || require('underscore'), toArray = _ref.toArray, extend = _ref.extend;
  14. inducedOrdering = function(collection) {
  15. var func;
  16. func = function(model) {
  17. return collection.indexOf(model);
  18. };
  19. func.induced = true;
  20. return func;
  21. };
  22. exports.Capped = (function(_super) {
  23. __extends(Capped, _super);
  24. function Capped(underlying, options) {
  25. var _this = this;
  26. if (options == null) {
  27. options = {};
  28. }
  29. this.underlying = underlying;
  30. this.model = underlying.model;
  31. this.comparator = options.comparator || inducedOrdering(underlying);
  32. this.options = extend({
  33. cap: 5
  34. }, underlying.options, options);
  35. Capped.__super__.constructor.call(this, this._capped(this.underlying.models), options);
  36. this.listenTo(this.underlying, {
  37. reset: function() {
  38. return _this.reset(_this._capped(_this.underlying.models));
  39. },
  40. remove: function(model) {
  41. var capped;
  42. if (_this.contains(model)) {
  43. _this.remove(model);
  44. capped = _this._capped(_this.underlying.models);
  45. return _this.add(capped[_this.options.cap - 1]);
  46. }
  47. },
  48. add: function(model) {
  49. if (_this.length < _this.options.cap) {
  50. return _this.add(model);
  51. } else if (_this.comparator(model) < _this.comparator(_this.last())) {
  52. _this.add(model);
  53. return _this.remove(_this.at(_this.options.cap));
  54. }
  55. },
  56. sort: function() {
  57. if (_this.comparator.induced) {
  58. return _this.reset(_this._capped(_this.underlying.models));
  59. }
  60. }
  61. });
  62. }
  63. Capped.prototype._capped = function(models) {
  64. var _this = this;
  65. models = toArray(models);
  66. models.sort(function(a, b) {
  67. a = _this.comparator(a);
  68. b = _this.comparator(b);
  69. if (a > b) {
  70. return 1;
  71. } else if (a < b) {
  72. return -1;
  73. } else {
  74. return 0;
  75. }
  76. });
  77. return models.slice(0, this.options.cap);
  78. };
  79. Capped.prototype.resize = function(cap) {
  80. var capped, idx, model, _i, _ref1;
  81. if (this.options.cap > cap) {
  82. this.options.cap = cap;
  83. _ref1 = this.models;
  84. for (idx = _i = _ref1.length - 1; _i >= 0; idx = _i += -1) {
  85. model = _ref1[idx];
  86. if (idx < cap) {
  87. break;
  88. }
  89. this.remove(model);
  90. }
  91. } else if (this.options.cap < cap) {
  92. this.options.cap = cap;
  93. capped = this._capped(this.underlying.models);
  94. this.add(capped.slice(this.length, this.options.cap));
  95. }
  96. return this.trigger('resize');
  97. };
  98. return Capped;
  99. })(Collection);
  100. exports.CappedCollection = exports.Capped;
  101. },{"backbone":"backbone","underscore":"underscore"}],"lib/filtered.js":[function(require,module,exports){
  102. // Generated by CoffeeScript 1.6.2
  103. var Collection, extend, inducedOrdering,
  104. __hasProp = {}.hasOwnProperty,
  105. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  106. Collection = ((typeof window !== "undefined" && window !== null ? window.Backbone : void 0) || require('backbone')).Collection;
  107. extend = ((typeof window !== "undefined" && window !== null ? window._ : void 0) || require('underscore')).extend;
  108. inducedOrdering = function(collection) {
  109. var func;
  110. func = function(model) {
  111. return collection.indexOf(model);
  112. };
  113. func.induced = true;
  114. return func;
  115. };
  116. exports.Filtered = (function(_super) {
  117. __extends(Filtered, _super);
  118. function Filtered(underlying, options) {
  119. var _this = this;
  120. if (options == null) {
  121. options = {};
  122. }
  123. this.underlying = underlying;
  124. this.model = underlying.model;
  125. this.comparator = options.comparator || inducedOrdering(underlying);
  126. this.options = extend({}, underlying.options, options);
  127. Filtered.__super__.constructor.call(this, this.underlying.models.filter(this.options.filter), options);
  128. this.listenTo(this.underlying, {
  129. reset: function() {
  130. return _this.reset(_this.underlying.models.filter(_this.options.filter));
  131. },
  132. remove: function(model) {
  133. if (_this.contains(model)) {
  134. return _this.remove(model);
  135. }
  136. },
  137. add: function(model) {
  138. if (_this.options.filter(model)) {
  139. return _this.add(model);
  140. }
  141. },
  142. change: function(model) {
  143. return _this.decideOn(model);
  144. },
  145. sort: function() {
  146. if (_this.comparator.induced) {
  147. return _this.sort();
  148. }
  149. }
  150. });
  151. }
  152. Filtered.prototype.update = function() {
  153. var model, _i, _len, _ref, _results;
  154. _ref = this.underlying.models;
  155. _results = [];
  156. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  157. model = _ref[_i];
  158. _results.push(this.decideOn(model));
  159. }
  160. return _results;
  161. };
  162. Filtered.prototype.decideOn = function(model) {
  163. if (this.contains(model)) {
  164. if (!this.options.filter(model)) {
  165. return this.remove(model);
  166. }
  167. } else {
  168. if (this.options.filter(model)) {
  169. return this.add(model);
  170. }
  171. }
  172. };
  173. return Filtered;
  174. })(Collection);
  175. exports.FilteredCollection = exports.Filtered;
  176. },{"backbone":"backbone","underscore":"underscore"}],"lib/sorted.js":[function(require,module,exports){
  177. // Generated by CoffeeScript 1.6.2
  178. var Collection, extend,
  179. __hasProp = {}.hasOwnProperty,
  180. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  181. Collection = ((typeof window !== "undefined" && window !== null ? window.Backbone : void 0) || require('backbone')).Collection;
  182. extend = ((typeof window !== "undefined" && window !== null ? window._ : void 0) || require('underscore')).extend;
  183. exports.Sorted = (function(_super) {
  184. __extends(Sorted, _super);
  185. function Sorted(underlying, options) {
  186. var _this = this;
  187. if (options == null) {
  188. options = {};
  189. }
  190. if (!options.comparator) {
  191. throw new Error("provide a comparator");
  192. }
  193. this.underlying = underlying;
  194. this.model = underlying.model;
  195. this.comparator = options.comparator;
  196. this.options = extend({}, underlying.options, options);
  197. Sorted.__super__.constructor.call(this, this.underlying.models, options);
  198. this.listenTo(this.underlying, {
  199. reset: function() {
  200. return _this.reset(_this.underlying.models);
  201. },
  202. remove: function(model) {
  203. return _this.remove(model);
  204. },
  205. add: function(model) {
  206. return _this.add(model);
  207. }
  208. });
  209. }
  210. return Sorted;
  211. })(Collection);
  212. exports.Reversed = (function(_super) {
  213. __extends(Reversed, _super);
  214. function Reversed(underlying, options) {
  215. if (options == null) {
  216. options = {};
  217. }
  218. options.comparator = function(model) {
  219. return -underlying.indexOf(model);
  220. };
  221. Reversed.__super__.constructor.call(this, underlying, options);
  222. this.listenTo(this.underlying, {
  223. sort: this.sort
  224. });
  225. }
  226. return Reversed;
  227. })(exports.Sorted);
  228. exports.SortedCollection = exports.Sorted;
  229. exports.ReversedCollection = exports.Reversed;
  230. },{"backbone":"backbone","underscore":"underscore"}]},{},["lib/index.js"])('lib/index.js')
  231. });