EventGroups.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: BI
  5. * (C) Copyright IBM Corp. 2016, 2019
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. */
  8. define(['../../lib/@waca/dashboard-common/dist/core/Collection', './EventGroupEntry', 'underscore'], function (Collection, EventGroupEntry, _) {
  9. /**
  10. * The Event Groups associated with the board model
  11. */
  12. // Declare the class
  13. var EventGroups = null;
  14. EventGroups = Collection.extend({
  15. // The class of objects that this collection manages
  16. modelClass: EventGroupEntry,
  17. defaultGroupIndex: 1,
  18. maxGroupsPerPage: 5,
  19. init: function init() {
  20. EventGroups.inherited('init', this, arguments);
  21. },
  22. findGroup: function findGroup(widgetId) {
  23. return _.find(this.getModels(), function (group) {
  24. return group.widgetIds.indexOf(widgetId) > -1;
  25. });
  26. },
  27. _createGroupId: function _createGroupId(pageId, groupIndex) {
  28. return pageId + ':' + groupIndex;
  29. },
  30. _nextGroupIndex: function _nextGroupIndex(pageId) {
  31. var groups = _.filter(this.models, function (model) {
  32. return model.id.indexOf(pageId + ':') === 0;
  33. });
  34. return groups.length + 1;
  35. },
  36. getNumberOfGroups: function getNumberOfGroups(pageId) {
  37. var groups = _.filter(this.models, function (model) {
  38. return model.id.indexOf(pageId + ':') === 0;
  39. });
  40. return groups.length;
  41. },
  42. getDefaultGroup: function getDefaultGroup(pageId, options) {
  43. var defaultGroupId = this.createDefaultGroupId(pageId);
  44. if (!this.get(defaultGroupId)) {
  45. this.add([{
  46. id: defaultGroupId,
  47. widgetIds: []
  48. }], this._createTransactionOptions(options));
  49. }
  50. return this.get(defaultGroupId);
  51. },
  52. getGroupsOnPage: function getGroupsOnPage(pageId) {
  53. var groups = _.filter(this.models, function (model) {
  54. return model.id.indexOf(pageId + ':') === 0;
  55. });
  56. return groups;
  57. },
  58. _createGroup: function _createGroup(pageId, options) {
  59. var id = this._createGroupId(pageId, this._nextGroupIndex(pageId));
  60. this.add([{
  61. id: id,
  62. widgetIds: []
  63. }], this._createTransactionOptions(options));
  64. return this.get(id);
  65. },
  66. //remove the widgets from their current group, destroy the group if empty and relabel the groups
  67. removeWidgetsFromGroup: function removeWidgetsFromGroup(widgetIds, options) {
  68. var oldGroup;
  69. widgetIds.forEach(function (element) {
  70. oldGroup = this.findGroup(element);
  71. if (oldGroup) {
  72. if (oldGroup.widgetIds.length === 1) {
  73. // simply remove the entire group if the widget is the only widget in the group to avoid two events
  74. // destroy group and relabel the groups (e.g. 1, 3,4,5 => 1,2,3,4 )
  75. this._destroyGroupAndRelabel(oldGroup, this._createTransactionOptions(options));
  76. } else {
  77. // just remove the widget from the group
  78. oldGroup.removeWidget(element, this._createTransactionOptions(options));
  79. }
  80. }
  81. }.bind(this));
  82. },
  83. _addWidgetsToGroup: function _addWidgetsToGroup(widgetIds, group, options) {
  84. widgetIds.forEach(function (element) {
  85. group.addWidget(element, this._createTransactionOptions(options));
  86. }.bind(this));
  87. },
  88. _destroyGroupAndRelabel: function _destroyGroupAndRelabel(group, options) {
  89. this.remove(group, this._createTransactionOptions(options));
  90. //relabel
  91. var indexMap = {};
  92. var temp = [];
  93. var groups = this.getModels();
  94. groups.forEach(function (element) {
  95. var pageId = element.getPageId();
  96. if (indexMap[pageId]) {
  97. indexMap[pageId]++;
  98. } else {
  99. indexMap[pageId] = 1;
  100. }
  101. element.setGroupIndex(indexMap[pageId], this._createTransactionOptions(options));
  102. temp.push(element);
  103. }.bind(this));
  104. this.reset(temp, this._createTransactionOptions(options));
  105. },
  106. canCreateGroup: function canCreateGroup(pageId) {
  107. return this.get(pageId + ':' + this.maxGroupsPerPage) ? false : true;
  108. },
  109. //removes the widgets from their old group and creates a new group containing the widgets
  110. //The target page for the new group is either derived from the page of the first widget or via the targetPageId option.
  111. createGroup: function createGroup(widgetIds, options) {
  112. options = options || {};
  113. var pageId = options.targetPageId || this.findGroup(widgetIds[0]).getPageId();
  114. this.removeWidgetsFromGroup(widgetIds, options);
  115. var newGroup = this._createGroup(pageId, options);
  116. this._addWidgetsToGroup(widgetIds, newGroup, options);
  117. return newGroup;
  118. },
  119. //remove the widgets from their old group and add them to another existing group
  120. addToGroup: function addToGroup(groupId, widgetIds, options) {
  121. var group = this.get(groupId);
  122. widgetIds = widgetIds.filter(function (id) {
  123. return group.widgetIds.indexOf(id) === -1;
  124. });
  125. this.removeWidgetsFromGroup(widgetIds, options);
  126. this._addWidgetsToGroup(widgetIds, group, options);
  127. },
  128. //remove the widgets from their old group and add them to their own individual group
  129. disconnectFromGroup: function disconnectFromGroup(widgetIds, options) {
  130. var _this = this;
  131. var pageId = this.findGroup(widgetIds[0]).getPageId();
  132. this.removeWidgetsFromGroup(widgetIds, options);
  133. return widgetIds.map(function (element) {
  134. var group = _this._createGroup(pageId, options);
  135. _this._addWidgetsToGroup([element], group, options);
  136. return group;
  137. });
  138. },
  139. _createTransactionOptions: function _createTransactionOptions(options) {
  140. return options ? options.payloadData ? {
  141. payloadData: options.payloadData,
  142. sender: !options.sender ? this : options.sender
  143. } : {
  144. sender: this
  145. } : null;
  146. },
  147. createDefaultGroupId: function createDefaultGroupId(pageId) {
  148. return this._createGroupId(pageId, this.defaultGroupIndex);
  149. }
  150. });
  151. return EventGroups;
  152. });
  153. //# sourceMappingURL=EventGroups.js.map