WidgetsPanelHandler.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. 'use strict';
  2. /*
  3. *+------------------------------------------------------------------------+
  4. *| Licensed Materials - Property of IBM
  5. *| IBM Cognos Products: Dashboard
  6. *| (C) Copyright IBM Corp. 2017, 2020
  7. *|
  8. *| US Government Users Restricted Rights - Use, duplication or disclosure
  9. *| restricted by GSA ADP Schedule Contract with IBM Corp.
  10. *+------------------------------------------------------------------------+
  11. */
  12. define(['underscore', '../../../lib/@waca/dashboard-common/dist/glass/controllers/WidgetBaseActionHandler', '../../../app/nls/StringResources', '../../../lib/@waca/core-client/js/core-client/utils/ClassFactory', '../../widgets/staticwidget/ShapeWidget'], function (_, BaseClass, stringResources, ClassFactory, ShapeWidget) {
  13. var ActionHandler = BaseClass.extend({
  14. getDashboardView: function getDashboardView(context) {
  15. return context.glassContext.appController.getCurrentContentView();
  16. },
  17. getUiHelperClass: function getUiHelperClass() {
  18. return ClassFactory.loadModule('dashboard-core/js/lib/@waca/dashboard-common/dist/ui/WidgetAddUIHelper');
  19. },
  20. createWidgetAddUIHelper: function createWidgetAddUIHelper(context) {
  21. var view = this.getDashboardView(context);
  22. this.uiHelperClassOptions = {
  23. dashboardApi: view.getDashboardApi(),
  24. services: view.services
  25. };
  26. return this.getUiHelperClass().then(function (UIHelperClass) {
  27. var widgetAddUIHelper = new UIHelperClass(this.uiHelperClassOptions);
  28. widgetAddUIHelper.panelAttributes = {
  29. canvasController: view.boardLoader.canvasController,
  30. cdnUrl: view.getCDNUrl(),
  31. ajaxSvc: view.ajaxSvc,
  32. glassContext: context.glassContext
  33. };
  34. return widgetAddUIHelper;
  35. }.bind(this));
  36. },
  37. getOptions: function getOptions(context) {
  38. var view = this.getDashboardView(context);
  39. var options = {
  40. className: 'widgetsPanelSlideout',
  41. title: stringResources.get('widgetsPanelTitle'),
  42. module: 'dashboard-core/js/dashboard/views/WidgetsPanelView',
  43. getEntries: this.getEntries.bind(this, context),
  44. services: view.services,
  45. dashboardApi: view.getDashboardApi()
  46. };
  47. return Promise.resolve(options);
  48. },
  49. getEntries: function getEntries(context) {
  50. var glassContext = context.glassContext;
  51. return Promise.all([this._getShapeWidgets(glassContext), this._getImageLibrary(glassContext), this._getWidgets(glassContext)]).then(function (results) {
  52. var shapeWidgets = results[0];
  53. var images = results[1];
  54. var widgets = results[2];
  55. var entries = {
  56. widgets: widgets,
  57. images: {},
  58. shapeWidgets: shapeWidgets
  59. };
  60. if (images && images.list && images.list.length > 0) {
  61. entries.images = images;
  62. }
  63. return entries;
  64. }).then(this.getSpecs.bind(this, context));
  65. },
  66. getSpecs: function getSpecs(context, entries) {
  67. var defaultColors = this._getWidgetColorsHelper(entries.widgets);
  68. // Notebook config check required here until Story 264685 complete
  69. var notebookConfigPromise = context.glassContext.getCoreSvc('.Config').getConfigValue('BIProxy.jupyter', false);
  70. return Promise.all([this.createWidgetAddUIHelper(context), this._getShapeSpecHelper(entries.shapeWidgets, defaultColors), this._getImageLibrarySpecHelper(entries.images), notebookConfigPromise]).then(function (results) {
  71. var helper = results[0],
  72. shapes = results[1],
  73. images = results[2],
  74. notebook = results[3];
  75. var icons = helper.dashboardApi.getFeature('Icons');
  76. var spec = {
  77. items: [{
  78. value: stringResources.get('widgetsPanelTitle'),
  79. name: 'widgetsPanel',
  80. type: 'Banner',
  81. editable: false
  82. }, {
  83. type: 'Separator'
  84. }]
  85. };
  86. var options = {
  87. onItemClick: helper.addWidgetBySelection.bind(helper),
  88. onItemStartDrag: helper.addWidgetByDrag.bind(helper)
  89. };
  90. // Basic widgets
  91. var basicWidgetItems = {
  92. name: stringResources.get('widgetsPanelTitle'),
  93. module: 'dashboard-core/js/dashboard/contentpane/PropertyUIControlView',
  94. items: [{
  95. type: 'SectionLabel',
  96. name: 'basicWidgets',
  97. label: stringResources.get('widgetsPanelSectionBasic')
  98. }, {
  99. type: 'IconCollection',
  100. id: _.uniqueId('basic_'),
  101. options: options,
  102. items: [{
  103. label: stringResources.get('textWidgetLabel'),
  104. name: stringResources.get('textWidgetLabel'),
  105. icon: icons.getIcon('text-creation').id,
  106. content: {
  107. widget: 'dashboard-core/js/dashboard/widgets/staticwidget/TextWidget',
  108. title: stringResources.get('textWidgetLabel')
  109. }
  110. }, {
  111. label: stringResources.get('imageWidgetLabel'),
  112. name: stringResources.get('imageWidgetLabel'),
  113. icon: icons.getIcon('image').id,
  114. content: {
  115. widget: 'dashboard-core/js/dashboard/widgets/staticwidget/ImageWidget',
  116. name: stringResources.get('imageWidgetLabel'),
  117. title: stringResources.get('imageWidgetLabel')
  118. }
  119. }, {
  120. label: stringResources.get('mediaWidgetLabel'),
  121. name: stringResources.get('mediaWidgetLabel'),
  122. icon: icons.getIcon('video-file').id,
  123. content: {
  124. widget: 'dashboard-core/js/dashboard/widgets/staticwidget/MediaWidget',
  125. name: stringResources.get('mediaWidgetLabel'),
  126. title: stringResources.get('mediaWidgetLabel')
  127. }
  128. }, {
  129. label: stringResources.get('webpageWidgetLabel'),
  130. name: stringResources.get('webpageWidgetLabel'),
  131. icon: icons.getIcon('webpage').id,
  132. content: {
  133. widget: 'dashboard-core/js/dashboard/widgets/staticwidget/WebpageWidget',
  134. name: stringResources.get('webpageWidgetLabel'),
  135. title: stringResources.get('webpageWidgetLabel')
  136. }
  137. }]
  138. }, {
  139. type: 'Separator'
  140. }]
  141. };
  142. this._addWidgets(entries.widgets.list, 'basicWidgets', basicWidgetItems.items[1].items);
  143. // Notebook
  144. if (notebook) {
  145. var advancedWidgetItems = {
  146. name: stringResources.get('widgetsPanelTitle'),
  147. module: 'dashboard-core/js/dashboard/contentpane/PropertyUIControlView',
  148. items: [{
  149. type: 'SectionLabel',
  150. name: 'advancedWidgets',
  151. label: stringResources.get('widgetsPanelSectionAdvanced')
  152. }, {
  153. type: 'IconCollection',
  154. id: _.uniqueId('advanced_'),
  155. options: options,
  156. items: [{
  157. label: stringResources.get('notebookWidgetLabel'),
  158. name: stringResources.get('notebookWidgetLabel'),
  159. icon: icons.getIcon('data-notebook').id,
  160. content: {
  161. widget: 'notebook/DashboardWidget',
  162. title: stringResources.get('notebookWidgetLabel')
  163. }
  164. }]
  165. }, {
  166. type: 'Separator'
  167. }]
  168. };
  169. basicWidgetItems.items.push(advancedWidgetItems);
  170. }
  171. // Shapes
  172. var shapesSpec = {
  173. name: stringResources.get('widgetsPanelTitle'),
  174. module: 'dashboard-core/js/dashboard/contentpane/PropertyUIControlView',
  175. items: [{
  176. type: 'SectionLabel',
  177. name: 'shapeWidgets',
  178. label: stringResources.get('widgetsPanelSectionShapes')
  179. }, {
  180. type: 'IconCollection',
  181. id: _.uniqueId('shapes_'),
  182. options: options,
  183. items: shapes
  184. }]
  185. };
  186. basicWidgetItems.items.push(shapesSpec);
  187. // Images
  188. if (images && images.length > 0) {
  189. var imageWidgetSpec = {
  190. name: stringResources.get('widgetsPanelImageLibraryTab'),
  191. module: 'dashboard-core/js/dashboard/contentpane/PropertyUIControlView',
  192. items: [{
  193. type: 'IconCollection',
  194. id: _.uniqueId('images_'),
  195. options: options,
  196. items: images
  197. }]
  198. };
  199. spec.items.push({
  200. type: 'TabControl',
  201. name: 'tabControl',
  202. items: [basicWidgetItems, imageWidgetSpec]
  203. });
  204. } else {
  205. spec.items.push(basicWidgetItems);
  206. }
  207. return spec;
  208. }.bind(this));
  209. },
  210. // Private
  211. /**
  212. * Adds widgets from the collection to the given section widget list if they have been attributed the given section name
  213. */
  214. _addWidgets: function _addWidgets(widgetsList, sectionName, sectionWidgets) {
  215. var widgetsToAdd = (widgetsList || []).filter(function (widget) {
  216. return widget.sections && widget.sections.indexOf(sectionName) !== -1;
  217. });
  218. widgetsToAdd.forEach(function (widget) {
  219. sectionWidgets.push({
  220. label: widget.label,
  221. name: widget.label,
  222. icon: widget.icon,
  223. content: {
  224. widget: widget.widget,
  225. name: widget.label,
  226. title: widget.label
  227. }
  228. });
  229. });
  230. },
  231. _getShapeSpecHelper: function _getShapeSpecHelper(shapes, colors) {
  232. var shapePromises = [];
  233. var shapeColors = colors && colors.shape;
  234. _.each(shapes.list, function (shape) {
  235. shape.options.fillColor = shapeColors.fillColor;
  236. shape.options.borderColor = shapeColors.borderColor;
  237. shape.options.fillColorIndex = shapeColors.fillColorIndex;
  238. shape.options.borderColorIndex = shapeColors.borderColorInder;
  239. var promise = ShapeWidget.getDefaultSpec(shape.name, shape.options).then(function (spec) {
  240. return {
  241. label: stringResources.get(shape.name),
  242. name: stringResources.get(shape.name),
  243. svg: spec.model.content,
  244. content: shape
  245. };
  246. });
  247. shapePromises.push(promise);
  248. });
  249. return Promise.all(shapePromises);
  250. },
  251. _getImageLibrarySpecHelper: function _getImageLibrarySpecHelper(images) {
  252. var imageSpecs = [];
  253. if (images.list) {
  254. _.each(images.list, function (image) {
  255. imageSpecs.push({
  256. label: image.name,
  257. name: image.name,
  258. image: image.options,
  259. content: image
  260. });
  261. });
  262. }
  263. return Promise.resolve(imageSpecs);
  264. },
  265. _getWidgetColorsHelper: function _getWidgetColorsHelper(widgets) {
  266. var widgetColors = {};
  267. if (widgets.list) {
  268. var getProperty = function getProperty(properties, color) {
  269. return _.find(properties, function (property) {
  270. return property.id === color;
  271. });
  272. };
  273. _.each(widgets.list, function (widget) {
  274. widgetColors[widget.id] = {};
  275. if (widget.propertyList) {
  276. var fillColor = getProperty(widget.propertyList, 'fillColor');
  277. var borderColor = getProperty(widget.propertyList, 'borderColor');
  278. widgetColors[widget.id].fillColor = fillColor && fillColor.defaultValue;
  279. widgetColors[widget.id].borderColor = borderColor && borderColor.defaultValue;
  280. }
  281. });
  282. }
  283. return widgetColors;
  284. },
  285. _getShapeWidgets: function _getShapeWidgets(context) {
  286. return this._getEntries(context, 'com.ibm.bi.dashboard.shapes', 'dashboard-core/js/dashboard/widgets/staticwidget/ShapeWidget');
  287. },
  288. _getImageLibrary: function _getImageLibrary(context) {
  289. return this._getEntries(context, 'com.ibm.bi.common.media', 'dashboard-core/js/dashboard/widgets/staticwidget/ImageWidget');
  290. },
  291. _getWidgets: function _getWidgets(context) {
  292. return this._getEntries(context, 'com.ibm.bi.dashboard.widgets', 'dashboard-core/js/dashboard/widgets/staticwidget/StaticWidget');
  293. },
  294. _getEntries: function _getEntries(context, perspectiveId, widgetModule) {
  295. return this.createWidgetAddUIHelper({ glassContext: context }).then(function (widgetAddUIHelper) {
  296. return widgetAddUIHelper.fetchWidgetListFromPerspective(perspectiveId, context);
  297. }).then(function (entries) {
  298. if (entries) {
  299. _.each(entries.list, function (entry) {
  300. if (!entry.widget) {
  301. entry.widget = widgetModule;
  302. }
  303. });
  304. }
  305. return entries;
  306. });
  307. }
  308. });
  309. return ActionHandler;
  310. });
  311. //# sourceMappingURL=WidgetsPanelHandler.js.map