WidgetRegistry.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2014, 2018
  5. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6. */
  7. define(['underscore', '../../app/nls/StringResources', '../../lib/@waca/core-client/js/core-client/ui/core/Class'], function (_, stringResources, Class) {
  8. /**
  9. * NOTE: All functions defined in widgetRegistry (other than initialize)
  10. * begin with the _fn prefix to emphasize that they are functions not widget class keys.
  11. */
  12. var WidgetRegistry = Class.extend({
  13. initialize: function initialize(dashboardApi) {
  14. var collectionId = dashboardApi.getCollectionConfig('widget').id;
  15. return dashboardApi.findGlassCollection(collectionId).then(function (collection) {
  16. //TODO: Remove this when server has the filter widget contribution.
  17. // collection=collection.concat(this._fnHackInFilterWidgetForDevelopers());
  18. return this._fnInitializeRegistryItems(collection).then(function () {
  19. //TODO: next step is to change the callers to use bluebird promises as well.
  20. return this;
  21. }.bind(this));
  22. }.bind(this)).fail(function () {
  23. throw stringResources.get('errorLoadingWidgetList');
  24. }.bind(this));
  25. },
  26. /**
  27. * @returns a promise which resolves when all initialized widget registry items have been loaded.
  28. */
  29. _fnInitializeRegistryItems: function _fnInitializeRegistryItems(collection) {
  30. if (!collection || !collection.length) {
  31. //No registry items to process.
  32. return Promise.resolve(collection);
  33. }
  34. var promises = [];
  35. _.each(collection, function (item) {
  36. promises.push(this._fnInitializeRegistryItem(item));
  37. }.bind(this));
  38. return Promise.all(promises);
  39. },
  40. /**
  41. * @returns a promise which resolves when a single widgetRegistry entry has been loaded.
  42. * In cases where a widgetRegistry entry has a custom model, the ModelClass is loaded.
  43. */
  44. _fnInitializeRegistryItem: function _fnInitializeRegistryItem(item) {
  45. // convert to local identifier for all built-in widgets
  46. if (item.builtin && item.id.indexOf('.') >= 0) {
  47. item.id = item.id.replace(/(\w+\.)/g, '');
  48. } else {
  49. this._fnInitCustomWidget(item);
  50. }
  51. // default values for all widgets
  52. _.defaults(item, {
  53. layoutAuthoringView: 'dashboard-core/js/dashboard/layout/authoring/views/Widget',
  54. disableTitle: false,
  55. ownTitle: true,
  56. createContentNode: true
  57. });
  58. // store the item to the widget registry
  59. this[item.id] = item;
  60. if (!item.modelClassPath || item.ModelClass) {
  61. //There is no modelClassPath in the widget definition or the ModelClass prototype has already been loaded.
  62. return Promise.resolve(item);
  63. }
  64. return this._fnLoadWidgetModelClass(item.modelClassPath).then(function (ModelClass) {
  65. item.ModelClass = ModelClass;
  66. return item;
  67. });
  68. },
  69. _fnLoadWidgetModelClass: function _fnLoadWidgetModelClass(modelClassPath) {
  70. return new Promise(function (resolve, reject) {
  71. try {
  72. require([modelClassPath], resolve, reject);
  73. } catch (error) {
  74. reject(error);
  75. }
  76. });
  77. },
  78. _fnInitCustomWidget: function _fnInitCustomWidget(widget) {
  79. // default values for custom widgets
  80. widget = _.defaults(widget, {
  81. ownTitle: false,
  82. title: stringResources.get('customWidget'),
  83. layoutConsumeView: 'dashboard-core/js/dashboard/layout/views/Widget',
  84. svgIcon: 'dashboard-default-custom-widget',
  85. scroll: 'scroll', // [scroll | scrollNone | scrollX | scrollY],
  86. propertyLayoutList: [{
  87. id: 'banner',
  88. value: widget.title,
  89. name: 'banner',
  90. type: 'Banner'
  91. }],
  92. propertyList: [{
  93. id: 'fillColor',
  94. defaultValue: 'transparent',
  95. editor: {
  96. sectionId: 'general.appearance',
  97. uiControl: {
  98. type: 'ColorPicker',
  99. label: stringResources.get('propFillColor'),
  100. showHexValue: true,
  101. addButton: true,
  102. open: false,
  103. ariaLabel: stringResources.get('propFillColor'),
  104. paletteType: 'DashboardColorSet'
  105. }
  106. }
  107. }, {
  108. id: 'borderColor',
  109. defaultValue: 'transparent',
  110. editor: {
  111. sectionId: 'general.appearance',
  112. uiControl: {
  113. type: 'ColorPicker',
  114. label: stringResources.get('propBorderColor'),
  115. showHexValue: true,
  116. addButton: true,
  117. open: false,
  118. ariaLabel: stringResources.get('propBorderColor'),
  119. paletteType: 'DashboardColorSet'
  120. }
  121. }
  122. }]
  123. });
  124. // define a default spec for custom widget
  125. widget.defaultSpec = _.defaults(widget.defaultSpec || {}, {
  126. model: {
  127. type: widget.id,
  128. name: widget.ownTitle ? null : widget.title
  129. },
  130. layoutProperties: {
  131. style: {
  132. 'display': 'flex',
  133. 'border': '1px solid transparent',
  134. 'border-radius': '5px',
  135. 'flex-direction': 'column',
  136. 'width': '250px',
  137. 'height': '250px'
  138. }
  139. }
  140. });
  141. }
  142. });
  143. return WidgetRegistry;
  144. });
  145. //# sourceMappingURL=WidgetRegistry.js.map