CustomTopicTab.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. "use strict";
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: Cognos Analytics
  5. * Copyright IBM Corp. 2017,2018
  6. * US Government Users Restricted Rights - Use, duplication or disclosure
  7. * restricted by GSA ADP Schedule Contract with IBM Corp.
  8. */
  9. define(['underscore', 'bi/admin/nls/StringResource', 'bacontentnav/common/ContentListPageView', 'bi/admin/system/services/TopicUploader', 'bi/commons/ui/properties/PropertyUIControl', 'bi/commons/ui/dialogs/ConfirmationDialog', 'bi/admin/system/services/TopicsListController', 'bi/commons/utils/Downloader'], function (_, StringResource, View, Uploader, PropertyUIControl, ConfirmationDialog, TopicsListController, Downloader) {
  10. 'use strict'; //NOSONAR: meant to be strict
  11. var CustomTopicTab = View.extend({
  12. loggingConfigUrl: 'v1/glug/config/logging',
  13. builtinTopicUrl: 'v1/glug/topics/builtin',
  14. customTopicUrl: 'v1/glug/topics/custom',
  15. getAddOnContextMenuId: function getAddOnContextMenuId() {
  16. return "com.ibm.bi.admin.loggingAddOnTopicListViewMenu";
  17. },
  18. currentCustomSelection: {
  19. topicName: '',
  20. topicType: 'CUSTOM'
  21. },
  22. currentTopic: {
  23. topicName: '',
  24. topicType: ''
  25. },
  26. init: function init(options) {
  27. CustomTopicTab.inherited('init', this, arguments);
  28. this.showTitle = true;
  29. _.extend(this, options);
  30. this.topicsController = new TopicsListController({
  31. glassContext: this.glassContext
  32. });
  33. if (this.currentHomeValue.topicType === 'CUSTOM') {
  34. this.currentCustomSelection = this.currentHomeValue;
  35. }
  36. },
  37. renderContent: function renderContent() {
  38. var aControls = [];
  39. this.$el.addClass("adminContentListView");
  40. aControls.push({
  41. 'type': 'SingleLineLinks',
  42. 'items': [{
  43. 'align': 'right',
  44. 'items': [{
  45. 'type': 'icon',
  46. 'svgIcon': 'common-upload',
  47. 'clickCallback': this.uploadTopic.bind(this),
  48. 'iconTooltip': StringResource.get('uploadTopic')
  49. }, {
  50. 'type': 'text',
  51. 'value': StringResource.get('uploadTopic')
  52. }]
  53. }]
  54. }, {
  55. 'module': 'bi/content_apps/ui/RenderCallback',
  56. 'renderCallback': this.renderListControl.bind(this),
  57. 'el': this.$el
  58. });
  59. this._oPropertyUIControl = this._newPropertyUIControl(aControls);
  60. return this._oPropertyUIControl.render();
  61. },
  62. _newPropertyUIControl: function _newPropertyUIControl(items) {
  63. return new PropertyUIControl({
  64. 'glassContext': this.glassContext,
  65. 'el': this.$el,
  66. 'items': items
  67. });
  68. },
  69. _getSelectedTopic: function _getSelectedTopic() {
  70. var selectedDefaultItem = this._listControl.getSelectedObjects()[0];
  71. if (!_.isUndefined(selectedDefaultItem)) {
  72. this.currentCustomSelection.topicName = selectedDefaultItem.id;
  73. this.currentValue = this.currentCustomSelection;
  74. }
  75. },
  76. getSelectedObjects: function getSelectedObjects() {
  77. var i;
  78. var rowObjects = [];
  79. var ancestors = this._listControl.contentView.getAncestors();
  80. for (i = 0; i < this.aSelectedRows.length; i += 1) {
  81. var obj = this._dTable.fnGetData(this.aSelectedRows[i]);
  82. if (!obj[ContentStoreObject.ANCESTORS] && ancestors && ancestors.length > 0) {
  83. obj[ContentStoreObject.ANCESTORS] = ancestors;
  84. }
  85. rowObjects.push(obj);
  86. }
  87. return rowObjects;
  88. },
  89. renderListControl: function renderListControl(container) {
  90. if (!_.isUndefined(container)) {
  91. this.$container = $(container);
  92. }
  93. if (_.isUndefined(this.$container)) {
  94. this.$container = this.$el;
  95. }
  96. this.$wrapper = $('<div>');
  97. this.$container.append(this.$wrapper);
  98. this.$wrapper.append(this.$contentBars);
  99. return this._renderContentList();
  100. },
  101. _renderContentList: function _renderContentList() {
  102. return this.renderContentList({
  103. 'minHeight': 350,
  104. '$container': this.$el,
  105. 'columns': this._getColumnSpecs(),
  106. 'ajaxProp': '',
  107. 'getJSONDataCallback': this.topicsController._getAddOnTopicList.bind(this),
  108. 'defaultSort': [1, 'asc'],
  109. 'multiSelect': false,
  110. 'selectedRow': ['name', StringResource.get(this.currentCustomSelection.topicName)],
  111. 'disableColumnHeaders': true,
  112. 'singleSelectCallback': this.onSelected.bind(this),
  113. 'getSelectedObjectWPermissions': function getSelectedObjectWPermissions(selectedObjects) {
  114. return Promise.resolve(selectedObjects);
  115. },
  116. 'getContextMenuId': this.getAddOnContextMenuId,
  117. 'dataTableOptions': {
  118. 'iDisplayLength': 100
  119. },
  120. '_showLoadBuffer': 100,
  121. 'emptyFolderString': StringResource.get('noCustomTopic')
  122. });
  123. },
  124. onSelected: function onSelected() {
  125. this._getSelectedTopic();
  126. return this.onSelectCallback(this.currentValue);
  127. },
  128. _getContentBarAccesibleLabel: function _getContentBarAccesibleLabel() {
  129. return StringResource.get('topics');
  130. },
  131. _getColumnSpecs: function _getColumnSpecs() {
  132. return [{
  133. 'type': 'RadioButtons'
  134. }, {
  135. 'type': 'Text',
  136. 'propertyName': 'name',
  137. '_bNavigable': true,
  138. 'label': StringResource.get('name'),
  139. 'scope': 'row'
  140. }, {
  141. 'type': 'ContextMenu',
  142. 'width': '10%',
  143. 'module': 'bacontentnav/common/ui/list_columns/ContextMenu'
  144. }];
  145. },
  146. _deleteTopic: function _deleteTopic() {
  147. var oDialog = new ConfirmationDialog('confirmDelete', StringResource.get('confirmDelete'), StringResource.get('confirmDeleteMessage'));
  148. oDialog.confirm(function () {
  149. this._getSelectedTopic();
  150. var selectedTopicName = this.currentValue.topicName;
  151. this.topicsController.deleteAddOnTopic(selectedTopicName).then(function () {
  152. var sText = StringResource.get('topicDeleteMsg', {
  153. 'topicName': selectedTopicName
  154. });
  155. this.glassContext.appController.showToast(sText, {
  156. 'type': 'success'
  157. });
  158. this._listControl.removeSelectedRows(); // when the topic is deleted, need to update the selected item to null
  159. this.currentValue = null;
  160. this.onSelectCallback(this.currentValue);
  161. }.bind(this), function (ajaxObj, err) {
  162. this.glassContext.appController.showErrorMessage(AJAXUtils.buildErrorMessage(err.responseJSON.errors), StringResource.get('error'));
  163. }.bind(this));
  164. }.bind(this));
  165. oDialog.renderContent($('<div>'));
  166. },
  167. uploadTopic: function uploadTopic() {
  168. var ajaxOptions = {
  169. 'isUpload': true,
  170. 'type': 'topic'
  171. };
  172. var uploader = new Uploader({
  173. '$el': this.$el,
  174. 'glassContext': this.glassContext,
  175. 'ajax': this.updateOrUpload.bind(this),
  176. 'ajaxOptions': ajaxOptions,
  177. 'fileType': 'json'
  178. });
  179. return uploader.doUpload().then();
  180. },
  181. updateOrUpload: function updateOrUpload(options) {
  182. return this.topicsController._updateOrUpload(options).then(this.refreshList.bind(this));
  183. },
  184. _downloadTopic: function _downloadTopic() {
  185. var selectedItem = this._listControl.getSelectedObjects()[0];
  186. if (_.isUndefined(selectedItem)) {
  187. //in this case nothing selected so nothing to download
  188. return;
  189. } else {
  190. var name = this._listControl.getSelectedObjects()[0].name;
  191. var promise = this.topicsController._getAddOnTopicDescription(name);
  192. return promise.then(function (data) {
  193. var downloader = new Downloader({
  194. 'url': 'data:text/json;charset=utf-8,' + encodeURIComponent(JSON.stringify(data, 0, 4)),
  195. 'name': name + '.json'
  196. });
  197. downloader.doDownload();
  198. });
  199. }
  200. },
  201. cancel: function cancel() {},
  202. onContextMenu: function onContextMenu(id) {
  203. switch (id) {
  204. case 'deleteTopic':
  205. this._deleteTopic();
  206. break;
  207. case 'downloadTopic':
  208. this._downloadTopic();
  209. break;
  210. default:
  211. this.logger.error("Unexpected context menu id for logging list pane: " + id);
  212. }
  213. },
  214. refreshList: function refreshList() {
  215. this._listControl.remove();
  216. this.renderListControl();
  217. }
  218. });
  219. return CustomTopicTab;
  220. });