Guest User

Untitled

a guest
Apr 24th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. (function() {
  2.  
  3. var ns = mico;
  4.  
  5. /*
  6. * unit of micro-content
  7. */
  8.  
  9. ns.Mico = function(title) {
  10. this.id = ns.util.generateID();
  11. this.title = title;
  12. // TODO: add body and tags as part of the standard attributes?
  13. };
  14.  
  15. /*
  16. * collection of MicoS
  17. */
  18.  
  19. ns.Collection = function(micos) {
  20. micos = micos || [];
  21. this.index = {};
  22. this.linkmap = {}; // XXX: rename?
  23. for(var i = 0; i < micos.length; i++) {
  24. this.add(micos[i]);
  25. }
  26. this.length = micos.length; // pseudo-array
  27. };
  28.  
  29. ns.Collection.prototype = new Array();
  30.  
  31. ns.Collection.prototype.add = function(mico) {
  32. this.push(mico);
  33. this.index[mico.id] = this.length - 1;
  34. if(this.linkmap[mico.title] === undefined) {
  35. this.linkmap[mico.title] = [];
  36. }
  37. this.linkmap[mico.title].push(mico.id);
  38. };
  39.  
  40. ns.Collection.prototype.remove = function(id) {
  41. var i = this.index[id];
  42. if(i === undefined) {
  43. return false;
  44. }
  45. var title = this[i].title;
  46. delete this.index[id];
  47. delete this.linkmap[title];
  48. var mico = this.splice(i, 1)[0];
  49. for(var key in this.index) { // update index references -- XXX: inefficient!?
  50. if(this.index[key] > i) {
  51. this.index[key]--;
  52. }
  53. }
  54. return mico;
  55. };
  56.  
  57. ns.Collection.prototype.filter = function(field, value, invert) {
  58. if(arguments.length == 1 && typeof arguments[0] == "function") { // XXX: hacky?
  59. var check = arguments[0];
  60. } else if(invert) {
  61. check = function(a, b) { return a !== b; };
  62. } else {
  63. check = function(a, b) { return a === b; };
  64. }
  65. var matches = jQuery.map(this, function(mico, i) { // XXX: use Array.filter to avoid dependency
  66. return check(mico[field], value) ? mico : null;
  67. });
  68. return new ns.Collection(matches);
  69. };
  70.  
  71. })();
Add Comment
Please, Sign In to add comment