You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

123 lines
2.0 KiB

  1. module.exports = {
  2. type: 'syncer',
  3. o: {
  4. components: []
  5. },
  6. oSelf: {
  7. components: []
  8. },
  9. reset: function () {
  10. this.o = {
  11. components: []
  12. };
  13. this.oSelf = {
  14. components: []
  15. };
  16. },
  17. get: function (self) {
  18. let o = this.o;
  19. if (self)
  20. o = this.oSelf;
  21. let keys = Object.keys(o);
  22. if (o.components.length === 0) {
  23. if (keys.length === 1)
  24. return null;
  25. delete o.components;
  26. }
  27. o.id = this.obj.id;
  28. return o;
  29. },
  30. set: function (self, cpnType, property, value) {
  31. let o = this.o;
  32. if (self)
  33. o = this.oSelf;
  34. if (cpnType) {
  35. let cpn = o.components.find(c => (c.type === cpnType));
  36. if (!cpn) {
  37. cpn = {
  38. type: cpnType
  39. };
  40. o.components.push(cpn);
  41. }
  42. cpn[property] = value;
  43. } else
  44. o[property] = value;
  45. },
  46. setObject: function (self, cpnType, object, property, value) {
  47. let o = this.o;
  48. if (self)
  49. o = this.oSelf;
  50. let cpn = o.components.find(c => (c.type === cpnType));
  51. if (!cpn) {
  52. cpn = {
  53. type: cpnType
  54. };
  55. o.components.push(cpn);
  56. }
  57. let obj = cpn[object];
  58. if (!obj) {
  59. obj = {};
  60. cpn[object] = obj;
  61. }
  62. obj[property] = value;
  63. },
  64. setArray: function (self, cpnType, property, value, noDuplicate) {
  65. let o = this.o;
  66. if (self)
  67. o = this.oSelf;
  68. let cpn = o.components.find(c => (c.type === cpnType));
  69. if (!cpn) {
  70. cpn = {
  71. type: cpnType
  72. };
  73. o.components.push(cpn);
  74. }
  75. if (cpn[property] === null)
  76. cpn[property] = [];
  77. if ((noDuplicate) && (cpn[property].find(f => (f === value))))
  78. return;
  79. cpn[property].push(value);
  80. },
  81. setSelfArray: function (self, property, value) {
  82. let o = this.o;
  83. if (self)
  84. o = this.oSelf;
  85. if (o[property] === null)
  86. o[property] = [];
  87. o[property].push(value);
  88. },
  89. delete: function (self, cpnType, property) {
  90. let o = this.o;
  91. if (self)
  92. o = this.oSelf;
  93. if (cpnType) {
  94. let cpn = o.components.find(c => (c.type === cpnType));
  95. if (!cpn)
  96. return;
  97. delete cpn[property];
  98. } else
  99. delete o[property];
  100. }
  101. };