Source: js/activity_window.js

  1. /* global AppWindow, BrowserFrame, Service */
  2. 'use strict';
  3. (function(exports) {
  4. var _id = 0;
  5. /**
  6. * ActivityWindow is the wrapper for the inline activity instances.
  7. * For window disposition activity, they are done in AppWindow.
  8. *
  9. * ##### Flow chart
  10. * ![ActivityWindow flow](http://i.imgur.com/4O1Frs3.png)
  11. *
  12. * @example
  13. * var app = new AppWindow({
  14. * url: 'http://uitest.gaiamobile.org:8080/index.html',
  15. * manifestURL: 'http://uitest.gaiamobile.org:8080/manifest.webapp'
  16. * });
  17. * var activity = new ActivityWindow({
  18. * url: 'http://gallery.gaiamobile.org:8080/pick.html',
  19. * manifestURL: 'http://gallery.gaiamobile.org:8080/manifest.webapp'
  20. * }, app);
  21. *
  22. * @class ActivityWindow
  23. * @param {Object} config The configuration object of this activity.
  24. * @param {AppWindow|ActivityWindow} caller The caller of this activity.
  25. */
  26. /**
  27. * Fired when the activity window is created.
  28. * @event ActivityWindow#activitycreated
  29. */
  30. /**
  31. * Fired when the activity window is removed.
  32. * @event ActivityWindow#activityterminated
  33. */
  34. /**
  35. * Fired when the activity window is opening.
  36. * @event ActivityWindow#activityopening
  37. */
  38. /**
  39. * Fired when the activity window is opened.
  40. * @event ActivityWindow#activityopen
  41. */
  42. /**
  43. * Fired when the activity window is cloing.
  44. * @event ActivityWindow#activityclosing
  45. */
  46. /**
  47. * Fired when the activity window is closed.
  48. * @event ActivityWindow#activityclose
  49. */
  50. /**
  51. * Fired before the activity window is rendered.
  52. * @event ActivityWindow#activitywillrender
  53. */
  54. /**
  55. * Fired when the activity window is rendered to the DOM tree.
  56. * @event ActivityWindow#activityrendered
  57. */
  58. /**
  59. * Fired when the page visibility of the activity window is
  60. * changed to foreground.
  61. * @event ActivityWindow#activityforeground
  62. */
  63. /**
  64. * Fired when the page visibility of the activity window is
  65. * changed to background.
  66. * @event ActivityWindow#activitybackground
  67. */
  68. var ActivityWindow = function ActivityWindow(config, caller) {
  69. this.config = config;
  70. for (var key in config) {
  71. this[key] = config[key];
  72. }
  73. if (caller) {
  74. caller.setFrontWindow(this);
  75. this.rearWindow = caller;
  76. if (caller.element) {
  77. this.containerElement = caller.element;
  78. }
  79. }
  80. this.publish('creating');
  81. this.render();
  82. this.publish('created');
  83. };
  84. ActivityWindow.prototype = Object.create(AppWindow.prototype);
  85. ActivityWindow.prototype.constructor = ActivityWindow;
  86. ActivityWindow.prototype.eventPrefix = 'activity';
  87. ActivityWindow.prototype.CLASS_NAME = 'ActivityWindow';
  88. /**
  89. * Turn on this flag to dump debugging messages for all activity windows.
  90. * @type {Boolean}
  91. */
  92. ActivityWindow.prototype._DEBUG = false;
  93. ActivityWindow.prototype.openAnimation = 'fade-in';
  94. ActivityWindow.prototype.closeAnimation = 'fade-out';
  95. /**
  96. * ActivityWindow's fullscreen state is copying from the caller
  97. * instead of reading manifest so we just overwrite the
  98. * isFullScreen method of AppWindow.
  99. *
  100. * @return {Boolean} The activity window is fullscreen or not.
  101. */
  102. ActivityWindow.prototype.isFullScreen = function acw_isFullScreen() {
  103. if (typeof(this._fullscreen) !== 'undefined') {
  104. return this._fullscreen;
  105. }
  106. this._fullscreen = (this.manifest && !!this.manifest.fullscreen) ?
  107. !!this.manifest.fullscreen :
  108. this.rearWindow ?
  109. this.rearWindow.isFullScreen() :
  110. false;
  111. return this._fullscreen;
  112. };
  113. /**
  114. * Lock or unlock orientation for this activity.
  115. */
  116. ActivityWindow.prototype.lockOrientation =
  117. function acw_lockOrientation(noCapture) {
  118. if (this.isActive()) {
  119. var orientation1 = (this.manifest) ?
  120. this.manifest.orientation : null;
  121. var orientation2 = (this.config.manifest) ?
  122. this.config.manifest.orientation : null;
  123. var orientation3 = (this.rearWindow.manifest) ?
  124. this.rearWindow.manifest.orientation : null;
  125. var orientation4 = Service.query('globalOrientation');
  126. var orientation = orientation1 ||
  127. orientation2 ||
  128. orientation3 ||
  129. orientation4;
  130. if (orientation) {
  131. var rv = false;
  132. if ('lockOrientation' in screen) {
  133. rv = screen.lockOrientation(orientation);
  134. } else if ('mozLockOrientation' in screen) {
  135. rv = screen.mozLockOrientation(orientation);
  136. }
  137. if (rv === false) {
  138. console.warn('screen.mozLockOrientation() returned false for',
  139. this.origin, 'orientation', orientation);
  140. }
  141. } else { // If no orientation was requested, then let it rotate
  142. if ('unlockOrientation' in screen) {
  143. screen.unlockOrientation();
  144. } else if ('mozUnlockOrientation' in screen) {
  145. screen.mozUnlockOrientation();
  146. }
  147. }
  148. }
  149. };
  150. ActivityWindow.prototype.view = function acw_view() {
  151. this.instanceID = this.CLASS_NAME + '_' + _id;
  152. _id++;
  153. return `<div id="${this.instanceID}"
  154. class="appWindow activityWindow inline-activity">
  155. <div class="fade-overlay"></div>
  156. <div class="browser-container">
  157. <div class="screenshot-overlay"></div>
  158. </div>
  159. </div>`;
  160. };
  161. ActivityWindow.SUB_COMPONENTS = {
  162. 'transitionController': 'AppTransitionController',
  163. 'modalDialog': 'AppModalDialog',
  164. 'valueSelector': 'ValueSelector',
  165. 'authDialog': 'AppAuthenticationDialog',
  166. 'childWindowFactory': 'ChildWindowFactory',
  167. 'statusbar': 'AppStatusbar'
  168. };
  169. ActivityWindow.SUB_MODULES = {
  170. 'contextmenu': 'BrowserContextMenu'
  171. };
  172. ActivityWindow.REGISTERED_EVENTS = AppWindow.REGISTERED_EVENTS;
  173. ActivityWindow.prototype._handle_mozbrowseractivitydone =
  174. function aw__handle_mozbrowseractivitydone() {
  175. this.kill();
  176. };
  177. ActivityWindow.prototype.render = function acw_render() {
  178. this.publish('willrender');
  179. this.containerElement.insertAdjacentHTML('beforeend', this.view());
  180. // TODO: Use BrowserConfigHelper.
  181. this.browser_config = {
  182. parentApp: this.parentApp,
  183. origin: this.origin,
  184. url: this.url,
  185. name: this.name,
  186. manifest: this.manifest,
  187. manifestURL: this.manifestURL,
  188. window_name: 'inline' + this.instanceID,
  189. oop: true
  190. };
  191. this.browser = new BrowserFrame(this.browser_config);
  192. this.element =
  193. document.getElementById(this.instanceID);
  194. this.browserContainer = this.element.querySelector('.browser-container');
  195. this.browserContainer.appendChild(this.browser.element);
  196. this.frame = this.element;
  197. this.iframe = this.browser.element;
  198. this.screenshotOverlay = this.element.querySelector('.screenshot-overlay');
  199. this.fadeOverlay = this.element.querySelector('.fade-overlay');
  200. // Copy fullscreen state from caller.
  201. if (this.isFullScreen()) {
  202. this.element.classList.add('fullscreen-app');
  203. }
  204. this._registerEvents();
  205. this.installSubComponents();
  206. this.publish('rendered');
  207. };
  208. /**
  209. * ActivityWindow's default container is '#windows'.
  210. * However, we could dynamically change this in layout manager
  211. * after it recieves the activitywillrender event.
  212. */
  213. ActivityWindow.prototype.containerElement =
  214. document.getElementById('windows');
  215. /**
  216. * Bringing ourselves up. To do so we simply do
  217. * requestOpen() the original caller app window instance.
  218. * Note: this overrides AppWindow.prototype.requestOpen().
  219. */
  220. ActivityWindow.prototype.requestOpen = function acw_requestOpen() {
  221. if (this.rearWindow) {
  222. this.rearWindow.requestOpen();
  223. }
  224. };
  225. /**
  226. * Request to close. We don't need to reset the visibility of
  227. * the bottom window here so we simply close.
  228. * Note: this overrides AppWindow.prototype.requestClose().
  229. */
  230. ActivityWindow.prototype.requestClose = function acw_requestOpen() {
  231. this.close();
  232. };
  233. exports.ActivityWindow = ActivityWindow;
  234. }(window));