ShortcutPlaceholderView.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: Storytelling (C) Copyright IBM Corp. 2018
  5. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6. */
  7. define(['./PlaceholderView', 'text!./ShortcutPlaceholder.html', '../../../lib/@waca/core-client/js/core-client/utils/Utils', '../../../app/nls/StringResources', 'jquery'], function (BaseClass, Template, Utils, stringResources, $) {
  8. var ShortcutPlaceholderView = null;
  9. ShortcutPlaceholderView = BaseClass.extend({
  10. templateString: Template,
  11. init: function init() {
  12. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  13. ShortcutPlaceholderView.inherited('init', this, arguments);
  14. this.onVisualizationSelected = options.onVisualizationSelected;
  15. this.onTextSelected = options.onTextSelected;
  16. this.onImageSelected = options.onImageSelected;
  17. this.activeButtons = [];
  18. },
  19. render: function render() {
  20. this.$el.append(this.dotTemplate({
  21. visLabel: stringResources.get('live_label'),
  22. textLabel: stringResources.get('textWidgetLabel'),
  23. imageLabel: stringResources.get('imageWidgetLabel')
  24. }));
  25. var $visualizationBtn = this.$el.find('.visualization');
  26. var $textBtn = this.$el.find('.text');
  27. var $imageBtn = this.$el.find('.image');
  28. Utils.setIcon($visualizationBtn, 'common-visualization');
  29. Utils.setIcon($textBtn, 'dashboard-type-text');
  30. Utils.setIcon($imageBtn, 'dashboard-type-image');
  31. this._eventHandlers([[$visualizationBtn, this.onVisualizationSelected], [$textBtn, this.onTextSelected], [$imageBtn, this.onImageSelected]]);
  32. },
  33. _isCompact: function _isCompact() {
  34. var maxWidth = 110;
  35. return this.$el.width() < maxWidth;
  36. },
  37. resize: function resize() {
  38. this.$el.find('.shortcut').toggleClass('compact', this._isCompact());
  39. },
  40. _onKeyDown: function _onKeyDown(handler, e) {
  41. if (e.which === 32 || e.which === 13) {
  42. // enter, space
  43. e.preventDefault();
  44. handler();
  45. }
  46. },
  47. destroy: function destroy() {
  48. this.activeButtons.forEach(function (button) {
  49. return button.off();
  50. });
  51. if (this.eventNamespace) {
  52. $(document).off(this.eventNamespace);
  53. }
  54. ShortcutPlaceholderView.inherited('destroy', this, arguments);
  55. },
  56. _eventHandlers: function _eventHandlers(buttons) {
  57. var _this = this;
  58. this.eventNamespace = 'mouseup.' + this.viewId;
  59. buttons.forEach(function (pair) {
  60. var button = pair[0];
  61. var handler = pair[1].bind(_this);
  62. button.on('keydown', _this._onKeyDown.bind(_this, handler));
  63. button.on('primaryaction', handler);
  64. button.on('mouseup', function () {
  65. return _this.activeHandler = null;
  66. });
  67. button.on('mousedown', function (e) {
  68. _this.time = e.timeStamp;
  69. _this.activeHandler = handler;
  70. });
  71. _this.activeButtons.push(button);
  72. });
  73. $(document).on(this.eventNamespace, function (e) {
  74. if (_this.activeHandler && e.timeStamp - _this.time <= 300) {
  75. _this.activeHandler();
  76. }
  77. });
  78. }
  79. });
  80. return ShortcutPlaceholderView;
  81. });
  82. //# sourceMappingURL=ShortcutPlaceholderView.js.map