SynchronizeDataEntry.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Watson Analytics (C) Copyright IBM Corp. 2017, 2018
  5. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6. */
  7. define(['../../../lib/@waca/dashboard-common/dist/core/Model', '../../utils/DatasourceUtil', 'underscore'], function (Model, DatasourceUtil, _) {
  8. var findItemId = function findItemId(items, itemId) {
  9. var item = _.find(items, function (item) {
  10. return item.itemId === itemId;
  11. });
  12. return item;
  13. };
  14. var findSourceId = function findSourceId(items, id, tableRef) {
  15. var item = _.find(items, function (item) {
  16. //If there is tableRef, the tableRef collection must match
  17. return item.sourceId === id && (!tableRef || DatasourceUtil.haveATableReference(tableRef, item.tableRef, true));
  18. });
  19. return item;
  20. };
  21. /**
  22. * Represents a single synchronizeDataEntry in the model
  23. */
  24. var SynchronizeDataEntry = Model.extend({
  25. whitelistAttrs: ['sourceId', 'tableRef', 'scope', 'eventGroupId', 'synchronizeItems', 'itemId', 'items'],
  26. init: function init() {
  27. SynchronizeDataEntry.inherited('init', this, arguments);
  28. },
  29. inSameScope: function inSameScope(scope, eventGroupId) {
  30. return this.scope === scope && this.eventGroupId === eventGroupId;
  31. },
  32. getSourceId: function getSourceId() {
  33. return this.sourceId;
  34. },
  35. getTableRef: function getTableRef() {
  36. return this.tableRef;
  37. },
  38. setTableRef: function setTableRef(value) {
  39. this.tableRef = value;
  40. },
  41. getSynchronizeItems: function getSynchronizeItems() {
  42. return this.synchronizeItems;
  43. },
  44. getSources: function getSources() {
  45. var sources = [];
  46. sources.push(this.sourceId);
  47. var itemFn = function itemFn(item) {
  48. if (sources.indexOf(item.sourceId) === -1) {
  49. sources.push(item.sourceId);
  50. }
  51. };
  52. _.each(this.synchronizeItems, function (synchronizeItem) {
  53. _.each(synchronizeItem.items, itemFn);
  54. });
  55. return sources;
  56. },
  57. getSynchronizeItemId: function getSynchronizeItemId(itemId, sourceId, tableRef) {
  58. var item = findItemId(this.synchronizeItems, itemId);
  59. if (item) {
  60. item = findSourceId(item.items, sourceId, tableRef);
  61. }
  62. return item;
  63. },
  64. addSynchronizeItem: function addSynchronizeItem(itemId, withSourceId, withItemId, tableRef) {
  65. this.synchronizeItems = this.synchronizeItems || [];
  66. var item = findItemId(this.synchronizeItems, itemId);
  67. if (!item) {
  68. item = this.synchronizeItems[this.synchronizeItems.push({ itemId: itemId }) - 1];
  69. }
  70. item.items = item.items || [];
  71. var source = findSourceId(item.items, withSourceId, tableRef);
  72. if (!source) {
  73. item.items.push({ sourceId: withSourceId, itemId: withItemId, tableRef: tableRef });
  74. }
  75. },
  76. hasSynchronizeData: function hasSynchronizeData() {
  77. this._removeEmptyContents();
  78. var found = false;
  79. if (this.synchronizeItems && this.synchronizeItems.length > 0) {
  80. found = !!_.find(this.synchronizeItems, function (item) {
  81. return item.items && item.items.length > 0;
  82. });
  83. }
  84. return found;
  85. },
  86. inSameTable: function inSameTable(tableRef) {
  87. return tableRef && this.tableRef === tableRef;
  88. },
  89. _removeEmptyContents: function _removeEmptyContents() {
  90. //Delete empty arrays
  91. if (this.synchronizeItems) {
  92. var results;
  93. this.synchronizeItems.forEach(function (synchItem) {
  94. if (synchItem.items && synchItem.items.length > 0) {
  95. if (!results) {
  96. results = [];
  97. }
  98. results.push(synchItem);
  99. }
  100. });
  101. this.synchronizeItems = results;
  102. }
  103. }
  104. });
  105. return SynchronizeDataEntry;
  106. });
  107. //# sourceMappingURL=SynchronizeDataEntry.js.map