EventInstanceRepo.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. var EventInstanceRepo = Class.extend({
  2. byDefId: null,
  3. constructor: function() {
  4. this.byDefId = {};
  5. },
  6. getEventInstancesForDef: function(eventDef) {
  7. return (this.byDefId[eventDef.id] || []).filter(function(eventInstance) {
  8. return eventInstance.def === eventDef;
  9. });
  10. },
  11. getEventInstances: function() { // TODO: use iterator?
  12. var byDefId = this.byDefId;
  13. var a = [];
  14. var id;
  15. for (id in byDefId) {
  16. a.push.apply(a, byDefId[id]);
  17. }
  18. return a;
  19. },
  20. getEventInstancesWithId: function(eventDefId) {
  21. var bucket = this.byDefId[eventDefId];
  22. if (bucket) {
  23. bucket.slice(); // clone
  24. }
  25. return [];
  26. },
  27. getEventInstancesWithoutId: function(eventDefId) {
  28. var byDefId = this.byDefId;
  29. var a = [];
  30. var id;
  31. for (id in byDefId) {
  32. if (id !== eventDefId) {
  33. a.push.apply(a, byDefId[id]);
  34. }
  35. }
  36. return a;
  37. },
  38. addEventInstance: function(eventInstance) {
  39. var id = eventInstance.def.id;
  40. (this.byDefId[id] || (this.byDefId[id] = []))
  41. .push(eventInstance);
  42. },
  43. removeEventInstance: function(eventInstance) {
  44. var id = eventInstance.def.id;
  45. var bucket = this.byDefId[id];
  46. if (bucket) {
  47. removeExact(bucket, eventInstance);
  48. }
  49. if (!bucket.length) {
  50. delete this.byDefId[id];
  51. }
  52. },
  53. clear: function() {
  54. this.byDefId = {};
  55. },
  56. applyChangeset: function(changeset) {
  57. var ourHash = this.byDefId;
  58. var theirHash = changeset.byDefId;
  59. var id;
  60. var ourBucket;
  61. changeset.removals.forEach(this.removeEventInstance.bind(this));
  62. for (id in theirHash) {
  63. ourBucket = (ourHash[id] || (ourHash[id] = []));
  64. ourBucket.push.apply(ourBucket, theirHash[id]);
  65. }
  66. }
  67. });