PropertiesCreator.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. 'use strict';
  2. /**
  3. *+------------------------------------------------------------------------+
  4. *| Licensed Materials - Property of IBM
  5. *| IBM Cognos Products: Dashboard
  6. *| (C) Copyright IBM Corp. 2018, 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', 'dashboard-analytics/widgets/livewidget/nls/StringResources'], function (_, StringResources) {
  13. 'use strict';
  14. //[241060] This sliderLoc is used in core so we re-use to avoid
  15. var UiSliderLoc = '../../core-client/ui/UiSlider';
  16. /*
  17. * The purpose of this object is to provide a singleton that can create
  18. * property descriptions of different types that is consumeable by the
  19. * properties panel.
  20. */
  21. var PropertiesCreator = function () {
  22. /**
  23. * Instance stores a reference to the Singleton
  24. * @type {object}
  25. */
  26. var instance = null;
  27. function init() {
  28. return {
  29. /**
  30. * @returns Create the checkbox property JSON in a format consumeable by the
  31. * properties panel
  32. */
  33. createCheckboxProperty: function createCheckboxProperty(options) {
  34. var defaultValue = !!(options.defaultValue ? options.defaultValue : options.value);
  35. return {
  36. id: options.name,
  37. type: 'ToggleButton',
  38. propertyType: 'CheckBox',
  39. label: options.caption,
  40. ariaLabel: options.caption,
  41. public: this._isPropertyPublic(options),
  42. name: options.name,
  43. displayPos: this._isPropertyValidNumber(options.displayPos) ? options.displayPos : 10,
  44. checked: defaultValue,
  45. defaultValue: defaultValue,
  46. active: options.active ? options.active : false
  47. };
  48. },
  49. /**
  50. * @returns Create the text input property JSON in a format consumeable by the
  51. * properties panel
  52. */
  53. createTextInputProperty: function createTextInputProperty(options) {
  54. return {
  55. id: options.name,
  56. type: 'InputLabel',
  57. propertyType: 'InputLabel',
  58. label: options.caption,
  59. 'ariaLabel': options.caption,
  60. public: true,
  61. name: options.name,
  62. handleReturnKey: true,
  63. displayPos: 10,
  64. defaultValue: options.defaultValue ? options.defaultValue : '',
  65. active: options.active ? options.active : false,
  66. multilingual: options.multilingual ? options.multilingual : false
  67. };
  68. },
  69. /**
  70. * @returns Create the font size property object
  71. */
  72. createFontSizeProperty: function createFontSizeProperty(options) {
  73. var possibleValues = [{
  74. 'value': 'auto',
  75. 'label': StringResources.get('fontSizeAuto')
  76. }, {
  77. 'value': '12',
  78. 'label': '12'
  79. }, {
  80. 'value': '14',
  81. 'label': '14'
  82. }, {
  83. 'value': '16',
  84. 'label': '16'
  85. }, {
  86. 'value': '24',
  87. 'label': '24'
  88. }];
  89. return {
  90. type: 'DropDown',
  91. id: options.name,
  92. name: options.name,
  93. label: options.caption,
  94. propertyType: 'font',
  95. defaultValue: options.defaultValue ? options.defaultValue : 'auto',
  96. public: false,
  97. active: options.active ? options.active : false,
  98. options: possibleValues
  99. };
  100. },
  101. /**
  102. * Create an number in a form that is consumable by the properties panel
  103. * @param options - It must be a number type
  104. * @returns number property description consumable by the properties panel
  105. */
  106. createNumberProperty: function createNumberProperty(options) {
  107. var propertyDesc = {
  108. 'type': 'InputLabel',
  109. 'inputType': 'text',
  110. 'id': options.name,
  111. 'label': options.caption,
  112. 'defaultValue': options.defaultValue ? options.defaultValue : null,
  113. 'ariaLabel': options.caption,
  114. 'displayMultiplier': options.displayMultiplier,
  115. 'value': options.value ? options.value : null,
  116. 'active': options.active ? options.active : false,
  117. 'public': this._isPropertyPublic(options),
  118. 'parseFloat': true
  119. };
  120. if (options.supportsValidationChecks && options.validationInfo) {
  121. instance._addValidationChecksForNumberProp(options, propertyDesc);
  122. }
  123. return propertyDesc;
  124. },
  125. /**
  126. * Add generic validation information for the number property if the
  127. * property is configured to support it. Note, it is assumed that we
  128. * should be handling return key for this prop as well. This method
  129. * may change over time if new requirements are put forward.
  130. * @param {Object} options - property configuration
  131. * @param {Object} prop - property description being built.
  132. */
  133. _addValidationChecksForNumberProp: function _addValidationChecksForNumberProp(options, prop) {
  134. prop.handleReturnKey = true;
  135. prop.showInlineError = true;
  136. if (options.validationInfo && options.validationInfo.placeHolderTextId) {
  137. prop.placeHolderText = StringResources.get(options.validationInfo.placeHolderTextId);
  138. }
  139. prop.customValidatorCallback = function (value) {
  140. return {
  141. isValid: Number.isInteger(value) || value === '' && options.allowNull,
  142. message: StringResources.get(options.validationInfo.messageId)
  143. };
  144. };
  145. },
  146. /**
  147. * Ok, so some enums are defaulted to a string value, some to the actual
  148. * object. Even the object might be different. Being tired of finding
  149. * issues this method is used to check all cases.
  150. * @params {Object} options - property description.
  151. * @returns {String} the default value to use.
  152. */
  153. _getDefaultValueForEnum: function _getDefaultValueForEnum(options) {
  154. var defaultValue = void 0;
  155. if (options.hasOwnProperty('def') && options['def'].hasOwnProperty('defaultValue') && options['def']['defaultValue']) {
  156. defaultValue = options['def']['defaultValue'].name;
  157. } else if (options.hasOwnProperty('defaultValue')) {
  158. if (options['defaultValue'] && options['defaultValue'].name) {
  159. defaultValue = options['defaultValue'].name;
  160. } else {
  161. defaultValue = options['defaultValue'];
  162. }
  163. } else {
  164. defaultValue = options.value;
  165. }
  166. return defaultValue;
  167. },
  168. /**
  169. * @param options - It must be an enum type
  170. * @returns enum property description consumable by the properties panel
  171. */
  172. createEnumProperty: function createEnumProperty(options) {
  173. var values = [];
  174. /*
  175. possibleValues is an array of values with a structure of
  176. { caption: 'x', name: 'y'} but we need {label: 'x', value: 'y'} so we
  177. need to do some basic conversion.
  178. */
  179. _.each(options.possibleValues, function (possibleValue) {
  180. /*
  181. * A label can either have been pre-localized or we need to take care of it now. If the possible value has
  182. * a resourceLabel defined then we localize it, otherwise we assume that there is a localizedLabel defined.
  183. */
  184. var valSpec = { label: possibleValue.caption, value: possibleValue.name };
  185. values.push(valSpec);
  186. });
  187. var defaultValue = instance._getDefaultValueForEnum(options);
  188. return {
  189. 'type': 'DropDown',
  190. 'id': options.name,
  191. 'label': options.caption,
  192. 'ariaLabel': options.caption,
  193. 'defaultValue': defaultValue,
  194. 'options': values,
  195. 'active': options.active ? options.active : false,
  196. 'public': this._isPropertyPublic(options)
  197. };
  198. },
  199. // properties are public unless they are configured to not be
  200. _isPropertyPublic: function _isPropertyPublic(property) {
  201. return property.public === undefined ? true : property.public;
  202. },
  203. _isPropertyValidNumber: function _isPropertyValidNumber(prop) {
  204. return typeof prop === 'number' ? true : false;
  205. },
  206. getMaintainAxisScaleProperty: function getMaintainAxisScaleProperty() {
  207. return {
  208. 'type': 'ToggleButton',
  209. 'id': 'maintainAxisScales',
  210. name: 'maintainAxisScales',
  211. 'label': StringResources.get('propMaintainAxisScales'),
  212. 'ariaLabel': StringResources.get('propMaintainAxisScales'),
  213. 'public': true,
  214. 'defautValue': false,
  215. 'displayPos': 0,
  216. active: true
  217. };
  218. },
  219. /**
  220. * Donut radius is a special case. Its type is a number but so
  221. * is the element color so we can't just create a generic number type property.
  222. * Few things to note as slider type properties are treated differently (i.e.
  223. * they are modified in the PropertiesUtil class):
  224. * 1) We have to set the id... of course
  225. * 2) The module is an exact pattern match in PropertiesUtil so.... don't
  226. * plan on changing it lol.
  227. * 3) PropertiesUtil will read the current value straight from the visApi
  228. * @param options - actual donut radius property description
  229. * @returns a properties utils readable property.
  230. */
  231. getDonutRadius: function getDonutRadius(options) {
  232. return {
  233. 'name': 'donutRadius',
  234. 'id': 'donutRadius',
  235. 'label': StringResources.get('propDonutRadius'),
  236. 'ariaLabel': StringResources.get('propDonutRadius'),
  237. 'type': null,
  238. 'module': UiSliderLoc,
  239. 'sliderType': 'percentage',
  240. 'connect': [true, false],
  241. 'defaultValue': options.value || options.defaultValue ? options.value || options.defaultValue : 0.50,
  242. 'range': {
  243. 'min': 0,
  244. 'max': 100
  245. },
  246. 'step': 1,
  247. 'format': {
  248. 'decimals': 1,
  249. 'postfix': '%'
  250. },
  251. 'displayPos': 19,
  252. 'noInputView': true,
  253. active: options.active ? options.active : false
  254. };
  255. },
  256. /**
  257. * @param options - transparency property description of interest
  258. * @returns transparency property description
  259. */
  260. getTransparencyPropertyForLayer: function getTransparencyPropertyForLayer(options) {
  261. if (options) {
  262. return {
  263. 'name': options.name,
  264. 'id': options.name,
  265. 'label': options.caption,
  266. 'ariaLabel': options.caption,
  267. 'type': null,
  268. 'module': UiSliderLoc,
  269. 'sliderType': 'percentage',
  270. 'connect': [true, false],
  271. 'defaultValue': options.value || options.defaultValue ? options.value || options.defaultValue : 0.20,
  272. 'range': {
  273. 'min': 0,
  274. 'max': 100
  275. },
  276. 'step': 1,
  277. 'format': {
  278. 'decimals': 1,
  279. 'postfix': '%'
  280. },
  281. 'displayPos': 19,
  282. 'noInputView': true,
  283. active: options.active ? options.active : false
  284. };
  285. }
  286. return null;
  287. }
  288. };
  289. }
  290. return {
  291. // Get the Singleton instance if one exists
  292. // or create one if it doesn't
  293. getInstance: function getInstance() {
  294. if (!instance) {
  295. instance = init();
  296. }
  297. return instance;
  298. }
  299. };
  300. }();
  301. return PropertiesCreator;
  302. });
  303. //# sourceMappingURL=PropertiesCreator.js.map