'use strict'; /** * Licensed Materials - Property of IBM * IBM Watson Analytics (C) Copyright IBM Corp. 2017, 2018 * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. */ define(['../../../lib/@waca/dashboard-common/dist/core/Model', '../../utils/DatasourceUtil', 'underscore'], function (Model, DatasourceUtil, _) { var findItemId = function findItemId(items, itemId) { var item = _.find(items, function (item) { return item.itemId === itemId; }); return item; }; var findSourceId = function findSourceId(items, id, tableRef) { var item = _.find(items, function (item) { //If there is tableRef, the tableRef collection must match return item.sourceId === id && (!tableRef || DatasourceUtil.haveATableReference(tableRef, item.tableRef, true)); }); return item; }; /** * Represents a single synchronizeDataEntry in the model */ var SynchronizeDataEntry = Model.extend({ whitelistAttrs: ['sourceId', 'tableRef', 'scope', 'eventGroupId', 'synchronizeItems', 'itemId', 'items'], init: function init() { SynchronizeDataEntry.inherited('init', this, arguments); }, inSameScope: function inSameScope(scope, eventGroupId) { return this.scope === scope && this.eventGroupId === eventGroupId; }, getSourceId: function getSourceId() { return this.sourceId; }, getTableRef: function getTableRef() { return this.tableRef; }, setTableRef: function setTableRef(value) { this.tableRef = value; }, getSynchronizeItems: function getSynchronizeItems() { return this.synchronizeItems; }, getSources: function getSources() { var sources = []; sources.push(this.sourceId); var itemFn = function itemFn(item) { if (sources.indexOf(item.sourceId) === -1) { sources.push(item.sourceId); } }; _.each(this.synchronizeItems, function (synchronizeItem) { _.each(synchronizeItem.items, itemFn); }); return sources; }, getSynchronizeItemId: function getSynchronizeItemId(itemId, sourceId, tableRef) { var item = findItemId(this.synchronizeItems, itemId); if (item) { item = findSourceId(item.items, sourceId, tableRef); } return item; }, addSynchronizeItem: function addSynchronizeItem(itemId, withSourceId, withItemId, tableRef) { this.synchronizeItems = this.synchronizeItems || []; var item = findItemId(this.synchronizeItems, itemId); if (!item) { item = this.synchronizeItems[this.synchronizeItems.push({ itemId: itemId }) - 1]; } item.items = item.items || []; var source = findSourceId(item.items, withSourceId, tableRef); if (!source) { item.items.push({ sourceId: withSourceId, itemId: withItemId, tableRef: tableRef }); } }, hasSynchronizeData: function hasSynchronizeData() { this._removeEmptyContents(); var found = false; if (this.synchronizeItems && this.synchronizeItems.length > 0) { found = !!_.find(this.synchronizeItems, function (item) { return item.items && item.items.length > 0; }); } return found; }, inSameTable: function inSameTable(tableRef) { return tableRef && this.tableRef === tableRef; }, _removeEmptyContents: function _removeEmptyContents() { //Delete empty arrays if (this.synchronizeItems) { var results; this.synchronizeItems.forEach(function (synchItem) { if (synchItem.items && synchItem.items.length > 0) { if (!results) { results = []; } results.push(synchItem); } }); this.synchronizeItems = results; } } }); return SynchronizeDataEntry; }); //# sourceMappingURL=SynchronizeDataEntry.js.map