Guest User

Untitled

a guest
Jan 16th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. const menuState = (function() {
  2. class MenuData {
  3. constructor(attached = [], available = []) {
  4. // attached,available MUST be arrays!
  5. if ((!Array.isArray(attached)) || (!Array.isArray(available))) {
  6. throw TypeError("passed data MUST be in the form of Arrays!")
  7. }
  8. this.attached = attached;
  9. this.available = available;
  10. }
  11.  
  12. add(entities) {
  13. // entities MUST be Array
  14. if (!Array.isArray(entities)) throw ReferenceError("entities must be array")
  15. // from here, we simply move from this.available to this.attached, the entities
  16. // but first, let's check if they're even available
  17. if (hasEntities(entities, this.available)) {
  18. // attach them
  19. this.attached = this.attached.concat(entities)
  20. // they are no longer available
  21. this.available= this.available
  22. .filter(excluderFactory(entities))
  23. }
  24. // if they're not attached, that is an error
  25. if (!hasEntities(entities, this.attached)) {
  26. throw Error('The entities you were trying to add were neither attached nor available')
  27. }
  28. }
  29.  
  30. remove(entities) {
  31. // entities MUST be Array
  32. if (!Array.isArray(entities)) throw ReferenceError("entities must be array")
  33. // from here, we simply move from this.attached to this.available, the entities
  34. // but first, let's check if they're even attached
  35. if (hasEntities(entities, this.attached)) {
  36. // make them available
  37. this.available = this.available.concat(entities)
  38. // detach them
  39. this.attached = this.attached
  40. .filter(excluderFactory(entities))
  41. }
  42. // if they're not available, that is an error
  43. if (!hasEntities(entities, this.available)) {
  44. throw Error('The entities you were trying to remove were neither attached nor available')
  45. }
  46.  
  47. }
  48. };
  49.  
  50. let _categories = new MenuData(),
  51. _items = new MenuData()
  52.  
  53. let _itemPool = [],
  54. _categoryItems = {}
  55.  
  56. /**
  57. * Determines if an array has entities with an Id.
  58. * @param {Object[]} entities
  59. * @param {Object[]} arrayToCheck
  60. * @returns {boolean} whether arrayToCheck has objects with entities' Ids.
  61. */
  62. function hasEntities(entities, arrayToCheck) {
  63. for (let idx in entities) {
  64. if (arrayToCheck.find((element) => element.Id === entities[idx].Id)) {
  65. if (idx == entities.length - 1) {
  66. return true;
  67. }
  68. continue;
  69. }
  70. return false;
  71. }
  72.  
  73. }
  74.  
  75. /**
  76. * Returns a callback for the purpose of excluding entities
  77. * @param {Object[]} entities the entities to exclude
  78. */
  79. function excluderFactory(entities) {
  80. return function(model) {
  81. return !entities.find((entity) => entity.Id === model.Id)
  82. }
  83. }
  84.  
  85. return {
  86. // some methods to expose to other parts of the page
  87. }
  88. })()
Add Comment
Please, Sign In to add comment