Source: js/homescreen_window.js

  1. /* global BrowserConfigHelper, AppWindow */
  2. 'use strict';
  3. (function(exports) {
  4. /**
  5. * HomescreenWindow creates a instance of homescreen by give manifestURL.
  6. *
  7. * ##### Flow chart
  8. * ![Boot to Homescreen flow chart](http://i.imgur.com/vLA8YEN.png)
  9. *
  10. * @class HomescreenWindow
  11. * @param {String} manifestURL The manifestURL of the homescreen app.
  12. * @extends AppWindow
  13. */
  14. var HomescreenWindow = function HomescreenWindow(manifestURL) {
  15. this.instanceID = 'homescreen';
  16. this.setBrowserConfig(manifestURL);
  17. this.render();
  18. this.publish('created');
  19. this.createdTime = this.launchTime = Date.now();
  20. return this;
  21. };
  22. /**
  23. * Fired when the homescreen window is created.
  24. * @event HomescreenWindow#homescreencreated
  25. */
  26. /**
  27. * Fired when the homescreen window is removed.
  28. * @event HomescreenWindow#homescreenterminated
  29. */
  30. /**
  31. * Fired when the homescreen window is opening.
  32. * @event HomescreenWindow#homescreenopening
  33. */
  34. /**
  35. * Fired when the homescreen window is opened.
  36. * @event HomescreenWindow#homescreenopen
  37. */
  38. /**
  39. * Fired when the homescreen window is closed.
  40. * @event HomescreenWindow#homescreenclosed
  41. */
  42. /**
  43. * Fired when the homescreen window is closing.
  44. * @event HomescreenWindow#homescreenclosing
  45. */
  46. /**
  47. * Fired when the homescreen window is closed.
  48. * @event HomescreenWindow#homescreenclose
  49. */
  50. /**
  51. * Fired before the homescreen window is rendered.
  52. * @event HomescreenWindow#homescreenwillrender
  53. */
  54. /**
  55. * Fired when the homescreen window is rendered to the DOM tree.
  56. * @event HomescreenWindow#homescreenrendered
  57. */
  58. /**
  59. * Fired when the page visibility of the homescreen window is
  60. * changed to foreground.
  61. * @event HomescreenWindow#homescreenforeground
  62. */
  63. /**
  64. * Fired when the page visibility of the homescreen window is
  65. * changed to background.
  66. * @event HomescreenWindow#homescreenbackground
  67. */
  68. HomescreenWindow.prototype = Object.create(AppWindow.prototype);
  69. HomescreenWindow.prototype.constructor = HomescreenWindow;
  70. HomescreenWindow.prototype._DEBUG = false;
  71. HomescreenWindow.prototype.CLASS_NAME = 'HomescreenWindow';
  72. /**
  73. * Construct browser config object by manifestURL.
  74. * @param {String} manifestURL The manifestURL of homescreen.
  75. */
  76. HomescreenWindow.prototype.setBrowserConfig =
  77. function hw_setBrowserConfig(manifestURL) {
  78. var app = window.applications.getByManifestURL(manifestURL);
  79. this.origin = app.origin;
  80. this.manifestURL = app.manifestURL;
  81. this.url = app.origin + '/index.html#root';
  82. this.browser_config =
  83. new BrowserConfigHelper({
  84. url: this.origin,
  85. manifestURL: this.manifestURL
  86. });
  87. // Necessary for b2gperf now.
  88. this.name = this.browser_config.name;
  89. this.manifest = this.browser_config.manifest;
  90. // XXX: Remove this hardcode
  91. this.browser_config.url = this.url;
  92. this.browser_config.isHomescreen = true;
  93. this.config = this.browser_config;
  94. this.isHomescreen = true;
  95. };
  96. HomescreenWindow.REGISTERED_EVENTS = AppWindow.REGISTERED_EVENTS;
  97. HomescreenWindow.SUB_COMPONENTS = {
  98. 'transitionController': 'AppTransitionController',
  99. 'modalDialog': 'AppModalDialog',
  100. 'valueSelector': 'ValueSelector',
  101. 'authDialog': 'AppAuthenticationDialog',
  102. 'childWindowFactory': 'ChildWindowFactory',
  103. 'statusbar': 'AppStatusbar'
  104. };
  105. HomescreenWindow.prototype.openAnimation = 'zoom-out';
  106. HomescreenWindow.prototype.closeAnimation = 'zoom-in';
  107. HomescreenWindow.prototype._handle__opening = function hw__handle__opening() {
  108. this.ensure();
  109. };
  110. HomescreenWindow.prototype._handle_mozbrowserclose =
  111. function hw__handle_mozbrowserclose(evt) {
  112. this.restart();
  113. };
  114. HomescreenWindow.prototype._handle_mozbrowsererror =
  115. function hw__handle_mozbrowsererror(evt) {
  116. if (evt.detail.type == 'fatal') {
  117. this.loaded = false;
  118. this.publish('crashed');
  119. this.restart();
  120. }
  121. };
  122. HomescreenWindow.prototype.restart = function hw_restart() {
  123. // If the crashing app is the home screen app and it is the displaying app
  124. // we will need to relaunch it right away.
  125. // Alternatively, if home screen is not the displaying app,
  126. // we will not relaunch it until the foreground app is closed.
  127. // (to be dealt in setDisplayedApp(), not here)
  128. // If we're displayed, restart immediately.
  129. if (this.isActive()) {
  130. this.kill();
  131. // XXX workaround bug 810431.
  132. // we need this here and not in other situations
  133. // as it is expected that homescreen frame is available.
  134. setTimeout(function() {
  135. if (this.element) {
  136. return;
  137. }
  138. this.render();
  139. this.open();
  140. }.bind(this));
  141. } else {
  142. // Otherwise wait until next opening request.
  143. this.kill();
  144. }
  145. };
  146. HomescreenWindow.prototype.view = function hw_view() {
  147. var content = `<div class="appWindow homescreen" id="homescreen">
  148. <div class="fade-overlay"></div>
  149. <div class="browser-container"></div>
  150. </div>`;
  151. return content;
  152. };
  153. HomescreenWindow.prototype.eventPrefix = 'homescreen';
  154. HomescreenWindow.prototype.toggle = function hw_toggle(visible) {
  155. this.ensure();
  156. if (this.browser.element) {
  157. this.setVisible(visible);
  158. }
  159. };
  160. // Ensure the homescreen is loaded and return its frame. Restarts
  161. // the homescreen app if it was killed in the background.
  162. HomescreenWindow.prototype.ensure = function hw_ensure(reset) {
  163. this.debug('ensuring homescreen...', this.frontWindow, reset);
  164. if (!this.element) {
  165. this.render();
  166. } else if (reset) {
  167. if (this.frontWindow) {
  168. // Just kill front window but not switch to the first page.
  169. this.frontWindow.kill();
  170. } else {
  171. var urlWithoutHash = this.browser_config.url.split('#')[0];
  172. this.browser.element.src = urlWithoutHash + '#' + Date.now();
  173. }
  174. }
  175. return this.element;
  176. };
  177. /**
  178. *
  179. * The homescreen instance always resizes if it's needed.
  180. * (ie. the current layout is different from the current window dimensions)
  181. * This helps with the back-to-homescreen transition.
  182. *
  183. */
  184. HomescreenWindow.prototype.resize = function aw_resize() {
  185. this.debug('request RESIZE...');
  186. this.debug(' will resize... ');
  187. return this._resize();
  188. };
  189. /**
  190. * Hide the overlay element of this window.
  191. *
  192. * this {HomescreenWindow}
  193. * memberof HomescreenWindow
  194. */
  195. HomescreenWindow.prototype.hideFadeOverlay = function hw_hideFadeOverlay() {
  196. this.fadeOverlay.classList.add('hidden');
  197. };
  198. /**
  199. * Show the overlay element of this window.
  200. *
  201. * this {HomescreenWindow}
  202. * memberof HomescreenWindow
  203. */
  204. HomescreenWindow.prototype.showFadeOverlay = function hw_showFadeOverlay() {
  205. this.fadeOverlay.classList.remove('hidden');
  206. };
  207. exports.HomescreenWindow = HomescreenWindow;
  208. }(window));