FocusView.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2014, 2020
  5. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6. */
  7. define(['../lib/@waca/core-client/js/core-client/ui/AccessibleView', '../nls/StringResources', 'jquery', 'doT', 'text!./templates/FocusView.template'], function (View, resources, $, dot, template) {
  8. //TODO: this logic should be moved to a dashboard-core feature where a content can have the focus
  9. // and may be setting the focus could be something like: dashboardAPI.getFeature('Focus').setFocus('contentId');
  10. var FocusView = View.extend({
  11. init: function init(options) {
  12. FocusView.inherited('init', this, arguments);
  13. this._content = options.content;
  14. this._owner = options.owner;
  15. this._widgetNode = options.data;
  16. this._dashboardState = options.dashboardState;
  17. this._iconsFeature = this._owner.dashboardApi.getFeature('Icons');
  18. },
  19. onKeyPress: function onKeyPress(event) {
  20. if (event.keyCode === 27) {
  21. // Cancel focus view on ESC key.
  22. this.cancel();
  23. }
  24. },
  25. onTapBackground: function onTapBackground() {
  26. // Tap outside card area - same as cancel on mobile devices.
  27. this.cancel();
  28. },
  29. _onClick: function _onClick() {
  30. this._owner.layoutAPI.offFocus();
  31. },
  32. render: function render() {
  33. var _this = this;
  34. var parentNode = this.containerElement || document.body;
  35. var focusViewContainerNode = document.createElement('div');
  36. focusViewContainerNode.classList.add('focusViewContainer');
  37. focusViewContainerNode.setAttribute('tabindex', '-1');
  38. var $container = $(parentNode[0].appendChild(focusViewContainerNode));
  39. $container.addClass('expandedView');
  40. this._$container = $container;
  41. var $card = $('<div class="card gigante widgetFocus" tabindex="-1">');
  42. $container.append($card);
  43. var pos = this._owner.getExpandStartingPosition ? this._owner.getExpandStartingPosition() : this._getCardPosition(this._widgetNode.rowDiv);
  44. var visExpandModeFeature = this._content.getFeature('VisExpandMode');
  45. if (visExpandModeFeature) {
  46. this._setFocusState(true);
  47. visExpandModeFeature.renderExpandedModeContent($card.get(0) /*contentContainer*/);
  48. var html = dot.template(template)({
  49. label: resources.get('evCollapse'),
  50. collapseIcon: this._iconsFeature.getIcon('minimize').id
  51. });
  52. var $collapseBtn = $(html);
  53. $collapseBtn.on('primaryaction', this._collapse.bind(this));
  54. $card.children().first().append($collapseBtn);
  55. }
  56. // setup animation
  57. var endpos = {
  58. 'left': 0,
  59. 'top': 0,
  60. 'height': '100%',
  61. 'width': '100%'
  62. };
  63. $card.css({
  64. 'min-height': 'inherit', //not sure why home page set min-height
  65. top: pos.top,
  66. left: pos.left
  67. }).height(pos.height).width(pos.width);
  68. this._returnPosition = pos;
  69. $container.show();
  70. var promise = new Promise(function (resolve) {
  71. $card.animate(endpos, 'fast', function () {
  72. if (this.postRenderAnimation) {
  73. this.postRenderAnimation({
  74. height: $card.height()
  75. });
  76. }
  77. resolve($container);
  78. var $popover = $('.popover');
  79. // close any popover that might have opened while the focus view is being launched.
  80. if ($popover.length) {
  81. $popover.popover('hide');
  82. }
  83. }.bind(this._owner));
  84. }.bind(this));
  85. $('body').on('tap.focusView', '.focusViewContainer.expandedView', this.onTapBackground.bind(this));
  86. $('body').on('keydown.pageView', this.onKeyPress.bind(this));
  87. $('body').on('click.widgetContent', '.widgetContent', this._onClick.bind(this));
  88. // handlers
  89. $container.on('touch', 'span.expand', function (event) {
  90. // 'this' points to the expand icon in the card
  91. event.preventDefault();
  92. event.stopImmediatePropagation();
  93. _this._collapse();
  94. }).on('tap', 'div.card', function (event) {
  95. // tap inside card should not end things
  96. event.preventDefault();
  97. event.stopImmediatePropagation();
  98. });
  99. return promise;
  100. },
  101. remove: function remove() {
  102. this._setFocusState(false);
  103. if (this._$container) {
  104. this._$container.remove();
  105. this._$container = null;
  106. }
  107. $('body').off('tap.focusView');
  108. $('body').off('keydown.pageView');
  109. $('body').off('click.widgetContent');
  110. return FocusView.inherited('remove', this, arguments);
  111. },
  112. _getCardPosition: function _getCardPosition(sourceDiv) {
  113. var sourceContainer = $(sourceDiv).parents('.cardscroll')[0];
  114. var scrollOffset = $(sourceContainer).scrollTop();
  115. var divPos = this._getPosition(sourceDiv);
  116. divPos.y -= scrollOffset; // adjust by vertical scroll amount
  117. return {
  118. top: divPos.y,
  119. left: divPos.x,
  120. height: $(sourceDiv).height(),
  121. width: $(sourceDiv).width()
  122. };
  123. },
  124. /**
  125. * restore
  126. * - the widget that was expanded
  127. * - set the focus to the launch point
  128. */
  129. _restoreUI: function _restoreUI() {
  130. if (this._owner.onRestore) {
  131. this._owner.onRestore();
  132. }
  133. if (this.getLaunchPoint()) {
  134. this.getLaunchPoint().focus();
  135. }
  136. },
  137. _collapse: function _collapse() {
  138. this._setFocusState(false);
  139. var $card = this._$container.find('div.card');
  140. $card.css({
  141. 'overflow': 'hidden'
  142. });
  143. var fRemove = this.remove.bind(this);
  144. $card.animate(this._returnPosition, 'fast', function () {
  145. this._$container.fadeOut('fast', fRemove);
  146. }.bind(this));
  147. this._restoreUI();
  148. },
  149. cancel: function cancel() {
  150. this._setFocusState(false);
  151. if (this._$container) {
  152. this._$container.fadeOut('fast', this.remove.bind(this));
  153. this._restoreUI();
  154. }
  155. },
  156. _getPosition: function _getPosition(element) {
  157. var xPosition = 0;
  158. var yPosition = 0;
  159. while (element) {
  160. xPosition += element.offsetLeft - element.scrollLeft + element.clientLeft;
  161. yPosition += element.offsetTop - element.scrollTop + element.clientTop;
  162. element = element.offsetParent;
  163. }
  164. return {
  165. x: xPosition,
  166. y: yPosition
  167. };
  168. },
  169. _setFocusState: function _setFocusState(isFocus) {
  170. if (this._dashboardState.getUiState().focus != isFocus) {
  171. this._dashboardState.updateUiState({
  172. stateChange: {
  173. focus: isFocus
  174. }
  175. });
  176. }
  177. }
  178. });
  179. return FocusView;
  180. });
  181. //# sourceMappingURL=FocusView.js.map