NavigationView.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2014, 2021
  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/core/View', 'underscore', 'jquery', 'text!./templates/NavigationView.html', '../nls/StringResources'], function (BaseView, _, $, Template, stringResources) {
  8. var View = BaseView.extend({
  9. templateString: Template,
  10. items: null,
  11. currentPosition: -1,
  12. callbacks: null,
  13. rendered: false,
  14. pendingRender: Promise.resolve(),
  15. pendingClear: Promise.resolve(),
  16. events: {
  17. 'click .navigationBar .clickable': 'onNavigationClick'
  18. },
  19. init: function init(options) {
  20. View.inherited('init', this, arguments);
  21. this.callbacks = options.callbacks;
  22. this.segue = options.segue;
  23. this.items = [];
  24. },
  25. _animationFactor: function _animationFactor() {
  26. return 1;
  27. },
  28. render: function render() {
  29. var html = this.dotTemplate({});
  30. this.$el.html(html);
  31. this.rendered = true;
  32. var promise = void 0;
  33. if (this.items.length) {
  34. var item = this.items[this.currentPosition];
  35. promise = this.displayItem(item);
  36. } else {
  37. promise = Promise.resolve();
  38. }
  39. return promise;
  40. },
  41. remove: function remove() {
  42. View.inherited('remove', this, arguments);
  43. this.rendered = false;
  44. },
  45. getCount: function getCount() {
  46. return this.items.length;
  47. },
  48. updateTitle: function updateTitle() {
  49. var canGoBack = false;
  50. var title = null;
  51. if (this.currentPosition >= 0) {
  52. var item = this.items[this.currentPosition];
  53. if (item.getTitle) {
  54. title = item.getTitle();
  55. }
  56. }
  57. if (this.currentPosition > 0 && title === '') {
  58. title = stringResources.get('navigationBack');
  59. }
  60. canGoBack = this.currentPosition > 0;
  61. var navigationBarLink = this.$el.find('.navigationBar').toggle(title ? true : false).find('> div');
  62. navigationBarLink.find('.title').html(title);
  63. navigationBarLink.toggleClass('clickable', canGoBack);
  64. },
  65. cleanItem: function cleanItem(item) {
  66. var _this = this;
  67. var promise = void 0;
  68. if (item) {
  69. promise = new Promise(function (resolve, reject) {
  70. try {
  71. item.$el.animate({
  72. 'opacity': 0
  73. }, 300 * _this._animationFactor(), function () {
  74. item.remove();
  75. resolve();
  76. });
  77. } catch (error) {
  78. reject(error);
  79. }
  80. });
  81. } else {
  82. promise = Promise.resolve();
  83. }
  84. return promise;
  85. },
  86. renderItem: function renderItem(item, currentItem) {
  87. var _this2 = this;
  88. var container = $('<div></div>');
  89. container.appendTo(this.$el.find('.navigationPanel'));
  90. var promises = [];
  91. item.$el = container;
  92. var rendered = item.render();
  93. if (rendered && rendered.then) {
  94. // We don't know what promise item.render() returns.
  95. // For safety, wrap it in a `Promise.resolve()`
  96. // TODO: get rid of the wrapper
  97. promises.push(Promise.resolve().then(rendered));
  98. }
  99. item.$el.css('opacity', 0);
  100. var promise = new Promise(function (resolve, reject) {
  101. try {
  102. item.$el.animate({
  103. opacity: 1
  104. }, 300 * _this2._animationFactor(), resolve);
  105. } catch (error) {
  106. reject(error);
  107. }
  108. });
  109. promises.push(promise);
  110. if (currentItem) {
  111. currentItem.$el.animate({
  112. opacity: 0
  113. }, 300 * this._animationFactor());
  114. }
  115. return Promise.all(promises);
  116. },
  117. displayItem: function displayItem(item, currentItem) {
  118. this.updateTitle();
  119. return this.renderItem(item, currentItem);
  120. },
  121. pushTop: function pushTop(item) {
  122. var currentItem = this.items[this.currentPosition];
  123. this.items = [item];
  124. this.currentPosition = 0;
  125. var promise = void 0;
  126. if (this.rendered) {
  127. // Prevent any pointer events during the animation and render phase.
  128. this.$el.css('pointer-events', 'none');
  129. // Another call to render the item is executing, wait for it to complete, and then push the item.
  130. promise = this.pendingRender = this.pendingRender.then(this.pushTopItem.bind(this, item, currentItem));
  131. } else {
  132. promise = this.pendingRender;
  133. }
  134. return promise;
  135. },
  136. pushTopItem: function pushTopItem(item, currentItem) {
  137. var _this3 = this;
  138. return this.displayItem(item, currentItem).then(function () {
  139. if (currentItem) {
  140. currentItem.remove();
  141. }
  142. // Renable pointer events. The animation and render phase is complete.
  143. _this3.$el.css('pointer-events', 'auto');
  144. });
  145. },
  146. push: function push(item) {
  147. this.items.push(item);
  148. this.currentPosition++;
  149. var promise = void 0;
  150. if (this.rendered) {
  151. // Prevent any pointer events during the animation and render phase.
  152. this.$el.css('pointer-events', 'none');
  153. var currentItem = this.items[this.currentPosition - 1];
  154. // Another call to render the item is executing, wait for it to complete, and then push the item.
  155. promise = this.pendingRender = this.pendingRender.then(this.pushItem.bind(this, item, currentItem));
  156. } else {
  157. promise = this.pendingRender;
  158. }
  159. return promise;
  160. },
  161. pushItem: function pushItem(item, currentItem) {
  162. var _this4 = this;
  163. return this.displayItem(item, currentItem).then(function () {
  164. if (currentItem) {
  165. currentItem.hide();
  166. }
  167. // Renable pointer events. The animation and render phase is complete.
  168. _this4.$el.css('pointer-events', 'auto');
  169. });
  170. },
  171. pop: function pop() {
  172. var currentItem = this.items.pop();
  173. if (currentItem) {
  174. this.currentPosition--;
  175. }
  176. var promise = void 0;
  177. if (this.rendered) {
  178. // Prevent any pointer events during the animation and render phase.
  179. this.$el.css('pointer-events', 'none');
  180. var item = _.last(this.items);
  181. // Another call to render the item is executing, wait for it to complete, and then push the item.
  182. promise = this.pendingRender = this.pendingRender.then(this.popItem.bind(this, item, currentItem));
  183. } else {
  184. promise = this.pendingRender;
  185. }
  186. return promise;
  187. },
  188. popItem: function popItem(item, currentItem) {
  189. var _this5 = this;
  190. if (item) {
  191. item.$el.show();
  192. item.$el.css('opacity', 0);
  193. item.$el.animate({
  194. opacity: 1
  195. }, 300 * this._animationFactor(), function () {});
  196. }
  197. return this.cleanItem(currentItem).then(function () {
  198. _this5.updateTitle();
  199. if (_this5.callbacks && _this5.callbacks.onPop) {
  200. _this5.callbacks.onPop();
  201. }
  202. // Renable pointer events. The animation and render phase is complete.
  203. _this5.$el.css('pointer-events', 'auto');
  204. });
  205. },
  206. _clear: function _clear() {
  207. if (this.items.length > 0) {
  208. var currentItem = _.last(this.items);
  209. // Clear the array.
  210. this.items = [];
  211. this.currentPosition = -1;
  212. if (this.rendered) {
  213. this.pendingClear = this.pendingClear.then(this.cleanItem.bind(this, currentItem));
  214. } else {
  215. this.pendingClear = Promise.resolve();
  216. }
  217. }
  218. return this.pendingClear;
  219. },
  220. onNavigationClick: function onNavigationClick() {
  221. return this.pop();
  222. }
  223. });
  224. return View;
  225. });
  226. //# sourceMappingURL=NavigationView.js.map