Source: components/init.js

  1. define('application/components/init', [
  2. 'rofl/widgets/component',
  3. 'rofl/widgets/label',
  4. 'antie/runtimecontext',
  5. 'rofl/lib/promise',
  6. 'application/managers/channel',
  7. 'rofl/media/source',
  8. 'rofl/devices/mediaplayer/mediaplayer',
  9. 'application/managers/session',
  10. 'rofl/lib/utils',
  11. 'rofl/analytics/web/google',
  12. 'application/managers/feature',
  13. 'rofl/logging/graylog',
  14. 'antie/storageprovider',
  15. 'rofl/lib/l10n'
  16. ], function (
  17. Component,
  18. Label,
  19. RuntimeContext,
  20. Promise,
  21. ChannelManager,
  22. MediaSource,
  23. MediaPlayer,
  24. SessionManager,
  25. Utils,
  26. GoogleAnalytics,
  27. FeatureManager,
  28. Graylog,
  29. StorageProvider,
  30. L10N
  31. ) {
  32. 'use strict';
  33. var device = RuntimeContext.getDevice(),
  34. application = RuntimeContext.getCurrentApplication(),
  35. splash = application.getConfiguration().videos.LOGO,
  36. maxSplashTime = application.getConfiguration().MAX_SPLASH_TIME,
  37. mediaPlayer = device.getMediaPlayer(),
  38. sessionManager = SessionManager.getInstance(),
  39. GA = GoogleAnalytics.getInstance(),
  40. graylog = Graylog.getInstance(),
  41. featureManager = FeatureManager.getInstance(),
  42. configuration = application.getConfiguration(),
  43. storage = device.getStorage(StorageProvider.STORAGE_TYPE_PERSISTENT, 'survey'),
  44. l10n = L10N.getInstance(),
  45. sd = new Date(configuration.surveyDate),
  46. surveyCookieName = 'survey' + sd.getDate() + (sd.getMonth() + 1) + sd.getFullYear();
  47. return Component.extend({
  48. /**
  49. * PlayerEvent.
  50. *
  51. * @param {Object} e - The event data.
  52. */
  53. onPlayerEvent: function (e) {
  54. if (e.type === MediaPlayer.EVENT.COMPLETE) {
  55. this._promise
  56. .then(Utils.bind(this._onReady, this));
  57. clearTimeout(this._timeout);
  58. }
  59. },
  60. /**
  61. * BeforeHide event.
  62. */
  63. onBeforeHide: function () {
  64. mediaPlayer.removeEventCallback(this, this.onPlayerEvent);
  65. },
  66. /**
  67. * BeforeRender event.
  68. */
  69. onBeforeRender: function () {
  70. this._loadingTime = application.getDate();
  71. mediaPlayer.addEventCallback(this, this.onPlayerEvent);
  72. },
  73. /**
  74. * BeforeShow event.
  75. */
  76. onBeforeShow: function () {
  77. if (featureManager.isBrandingEnabled()) {
  78. this._promise = this._prepare();
  79. // starts countdown
  80. this._timeout = setTimeout(Utils.bind(function () {
  81. /**
  82. * Stops loading video and waits until necessary user requests are finished,
  83. * then routes to the app,
  84. */
  85. if (mediaPlayer.getState() !== MediaPlayer.STATE.EMPTY) {
  86. mediaPlayer.stop();
  87. mediaPlayer.reset();
  88. }
  89. this._promise.then(Utils.bind(function () {
  90. this._onReady();
  91. }, this));
  92. }, this), maxSplashTime);
  93. } else {
  94. if (sessionManager.isEntryConfirmed()) {
  95. application.showLoader();
  96. this._prepare()
  97. .then(Utils.bind(this._onReady, this));
  98. } else {
  99. application.route('preconnect');
  100. }
  101. }
  102. },
  103. /**
  104. * AfterShow event.
  105. */
  106. onAfterShow: function () {
  107. var mediaSource = new MediaSource(splash),
  108. value = ((application.getDate() - this._loadingTime) / 1000).toFixed(2);
  109. if (featureManager.isBrandingEnabled()) {
  110. mediaPlayer.setMediaSource(mediaSource);
  111. mediaSource.prepare()
  112. .then(function () {
  113. // Dirty hack to force playback from the 0 moment.
  114. mediaPlayer.beginLivePlayback();
  115. });
  116. } else {
  117. application.getRootWidget().addClass('branding-disabled');
  118. application.route('menu');
  119. }
  120. GA.onEvent('page', 'load', {
  121. eventLabel: 'init',
  122. eventValue: value
  123. });
  124. },
  125. /**
  126. * Prepares any data needed for launch.
  127. *
  128. * @returns {Promise} - Promise resolving after preparation.
  129. * @private
  130. */
  131. _prepare: function () {
  132. var promises = [];
  133. // We already check if the user is logged in while loading, and prepare the user if needed.
  134. if (sessionManager.isLoggedIn()) {
  135. promises.push(this._prepareUser());
  136. }
  137. return Promise.all(promises);
  138. },
  139. /**
  140. * Gets executed when the initialisation has completed.
  141. *
  142. * @private
  143. */
  144. _onReady: function () {
  145. if (mediaPlayer.getState() !== MediaPlayer.STATE.EMPTY) {
  146. mediaPlayer.stop();
  147. mediaPlayer.reset();
  148. }
  149. if (sessionManager.isLoggedIn()) {
  150. if (application.getUser().isOutOfHome()) {
  151. sessionManager.logout();
  152. application.route('outofhome');
  153. return;
  154. }
  155. application.getComponent('main')._historyStack = [];
  156. this.parentWidget.back();
  157. if (this._shouldShowSurvey()) {
  158. this._showSurvey();
  159. } else {
  160. application.route(configuration.routeAfterLogin);
  161. }
  162. graylog.onAppNotice('User logged in');
  163. } else {
  164. if (this._shouldShowSurvey()) {
  165. this._showSurvey();
  166. } else {
  167. application.route('landing');
  168. }
  169. }
  170. },
  171. /**
  172. * Check if we should show the survey.
  173. *
  174. * @returns {boolean} - Show survey.
  175. * @private
  176. */
  177. _shouldShowSurvey: function () {
  178. var cookie = storage.getItem(surveyCookieName),
  179. isSurveyShown = Utils.isUndefined(cookie) ? false : cookie;
  180. return isSurveyShown === false &&
  181. application.getDate() <= configuration.surveyDate &&
  182. featureManager.isSurveyEnabled();
  183. },
  184. /**
  185. * Show the survey.
  186. *
  187. * @private
  188. */
  189. _showSurvey: function () {
  190. var qrModalConfig = {
  191. title: l10n.get('survey.modal.title'),
  192. text: l10n.get('survey.modal.text'),
  193. imgUrl: 'src/assets/images/survey.png',
  194. buttons: [
  195. {
  196. label: l10n.get('survey.modal.close'),
  197. class: 'confirm_button',
  198. button: 'confirmbutton',
  199. labelname: 'confirmButtonLabel'
  200. }
  201. ],
  202. footer: l10n.get('survey.modal.footer'),
  203. callback: '',
  204. showGoBackButton: false
  205. };
  206. storage.setItem(surveyCookieName, true);
  207. if (sessionManager.isLoggedIn()) {
  208. qrModalConfig.callback = function () {
  209. application.route(configuration.routeAfterLogin);
  210. this.parentWidget.back();
  211. };
  212. } else {
  213. qrModalConfig.callback = function () {
  214. application.route('landing');
  215. };
  216. }
  217. application.route('qrmodal', qrModalConfig);
  218. },
  219. /**
  220. * Prepares the user.
  221. *
  222. * @returns {Promise} - Promise resolving with the user.
  223. */
  224. _prepareUser: function () {
  225. // If the user fails to prepare, log out the user.
  226. return sessionManager.prepareUser()
  227. ['catch'](function () {
  228. return sessionManager.logout();
  229. });
  230. }
  231. });
  232. });