InfographicScaleView.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. *
  5. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2016, 2018
  6. *
  7. * US Government Users Restricted Rights - Use, duplication or disclosure
  8. * restricted by GSA ADP Schedule Contract with IBM Corp.
  9. */
  10. define(['underscore', '../../../widgets/livewidget/nls/StringResources', '../../../lib/@waca/core-client/js/core-client/ui/core/View', '../../../lib/@waca/dashboard-common/dist/ui/Slider', '../../../lib/@waca/dashboard-common/dist/utils/ScaleUtil'], function (_, stringResources, View, Slider, ScaleUtil) {
  11. 'use strict';
  12. // Create message map only once
  13. var _SCALE_MESSAGE_MAP = {};
  14. _SCALE_MESSAGE_MAP[ScaleUtil.SCALE_VALUE_FEW] = stringResources.get('propScaleFew');
  15. _SCALE_MESSAGE_MAP[ScaleUtil.SCALE_VALUE_DEFAULT] = stringResources.get('propScaleDefault');
  16. _SCALE_MESSAGE_MAP[ScaleUtil.SCALE_VALUE_MANY] = stringResources.get('propScaleMany');
  17. var InfographicScaleView = View.extend({
  18. init: function init(options) {
  19. InfographicScaleView.inherited('init', this, arguments);
  20. _.extend(this, options);
  21. },
  22. /**
  23. * Called to render the flyout with the slider/title to edit scale
  24. * @returns Flyout and slider are rendered
  25. */
  26. render: function render() {
  27. // Required to set dimensions of flyout. If not set, will appear tiny
  28. this.$el.css({
  29. height: 93,
  30. width: 200
  31. });
  32. var extremes = ScaleUtil.getMinMax(this.widgetValue, this.isPercent);
  33. var options = {
  34. el: this.$el[0],
  35. name: 'scaleSlider',
  36. label: stringResources.get('propScaleLabel'),
  37. type: 'slider',
  38. description: stringResources.get('propScaleDescription'),
  39. min: extremes.min,
  40. max: extremes.max,
  41. step: 1,
  42. showMinMax: false,
  43. value: this.currentScaleOption,
  44. style: 'simple',
  45. valueFormatter: this.valueFormatter.bind(this)
  46. };
  47. this.slider = new Slider(options);
  48. return this.slider.render();
  49. },
  50. /**
  51. * Called when the subview has finished rendering
  52. */
  53. notifyRenderComplete: function notifyRenderComplete() {
  54. if (this.slider) {
  55. // slider needs to re-layout after it's rendered to ensure size calculations are correct
  56. this.slider.layout();
  57. if (this.slider.slider) {
  58. // hook up slider event
  59. this.slider.slider.on('slideStop', this.onWidgetScaleUpdate.bind(this));
  60. }
  61. }
  62. },
  63. /**
  64. * Used to update the model when the scale is changed via the widget. Calls internal function to update model and reRender infographic.
  65. * @param {object} info - Object with a property (value) that contains the new slider value
  66. */
  67. onWidgetScaleUpdate: function onWidgetScaleUpdate(info) {
  68. this.content.setPropertyValue('value.graphic.currentScaleOption', info.value);
  69. },
  70. /**
  71. * Returns the slider text for the given value
  72. * @param {string} value
  73. * @returns {string}
  74. */
  75. valueFormatter: function valueFormatter(value) {
  76. return _SCALE_MESSAGE_MAP[value] || value;
  77. },
  78. setFocus: function setFocus() {
  79. if (this.slider) {
  80. this.slider.setFocus();
  81. }
  82. }
  83. });
  84. return InfographicScaleView;
  85. });
  86. //# sourceMappingURL=InfographicScaleView.js.map