_base.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.mobile.app._base"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.mobile.app._base"] = true;
  8. dojo.provide("dojox.mobile.app._base");
  9. dojo.experimental("dojox.mobile.app._base");
  10. dojo.require("dijit._base");
  11. dojo.require("dijit._WidgetBase");
  12. dojo.require("dojox.mobile");
  13. dojo.require("dojox.mobile.parser");
  14. dojo.require("dojox.mobile.app._event");
  15. dojo.require("dojox.mobile.app._Widget");
  16. dojo.require("dojox.mobile.app.StageController");
  17. dojo.require("dojox.mobile.app.SceneController");
  18. dojo.require("dojox.mobile.app.SceneAssistant");
  19. dojo.require("dojox.mobile.app.AlertDialog");
  20. dojo.require("dojox.mobile.app.List");
  21. dojo.require("dojox.mobile.app.ListSelector");
  22. dojo.require("dojox.mobile.app.TextBox");
  23. dojo.require("dojox.mobile.app.ImageView");
  24. dojo.require("dojox.mobile.app.ImageThumbView");
  25. (function(){
  26. var stageController;
  27. var appInfo;
  28. var jsDependencies = [
  29. "dojox.mobile",
  30. "dojox.mobile.parser"
  31. ];
  32. var loadedResources = {};
  33. var loadingDependencies;
  34. var rootNode;
  35. var sceneResources = [];
  36. // Load the required resources asynchronously, since not all mobile OSes
  37. // support dojo.require and sync XHR
  38. function loadResources(resources, callback){
  39. // summary:
  40. // Loads one or more JavaScript files asynchronously. When complete,
  41. // the first scene is pushed onto the stack.
  42. // resources:
  43. // An array of module names, e.g. 'dojox.mobile.AlertDialog'
  44. var resource;
  45. var url;
  46. do {
  47. resource = resources.pop();
  48. if (resource.source) {
  49. url = resource.source;
  50. }else if (resource.module) {
  51. url = dojo.baseUrl + dojo._getModuleSymbols(resource.module).join("/") + '.js';
  52. }else {
  53. alert("Error: invalid JavaScript resource " + dojo.toJson(resource));
  54. return;
  55. }
  56. }while (resources.length > 0 && loadedResources[url]);
  57. if(resources.length < 1 && loadedResources[url]){
  58. // All resources have already been loaded
  59. callback();
  60. return;
  61. }
  62. dojo.xhrGet({
  63. url: url,
  64. sync: false
  65. }).addCallbacks(function(text){
  66. dojo["eval"](text);
  67. loadedResources[url] = true;
  68. if(resources.length > 0){
  69. loadResources(resources, callback);
  70. }else{
  71. callback();
  72. }
  73. },
  74. function(){
  75. alert("Failed to load resource " + url);
  76. });
  77. }
  78. var pushFirstScene = function(){
  79. // summary:
  80. // Pushes the first scene onto the stack.
  81. stageController = new dojox.mobile.app.StageController(rootNode);
  82. var defaultInfo = {
  83. id: "com.test.app",
  84. version: "1.0.0",
  85. initialScene: "main"
  86. };
  87. // If the application info has been defined, as it should be,
  88. // use it.
  89. if(dojo.global["appInfo"]){
  90. dojo.mixin(defaultInfo, dojo.global["appInfo"]);
  91. }
  92. appInfo = dojox.mobile.app.info = defaultInfo;
  93. // Set the document title from the app info title if it exists
  94. if(appInfo.title){
  95. var titleNode = dojo.query("head title")[0] ||
  96. dojo.create("title", {},dojo.query("head")[0]);
  97. document.title = appInfo.title;
  98. }
  99. stageController.pushScene(appInfo.initialScene);
  100. };
  101. var initBackButton = function(){
  102. var hasNativeBack = false;
  103. if(dojo.global.BackButton){
  104. // Android phonegap support
  105. BackButton.override();
  106. dojo.connect(document, 'backKeyDown', function(e) {
  107. dojo.publish("/dojox/mobile/app/goback");
  108. });
  109. hasNativeBack = true;
  110. }else if(dojo.global.Mojo){
  111. // TODO: add webOS support
  112. }
  113. if(hasNativeBack){
  114. dojo.addClass(dojo.body(), "mblNativeBack");
  115. }
  116. };
  117. dojo.mixin(dojox.mobile.app, {
  118. init: function(node){
  119. // summary:
  120. // Initializes the mobile app. Creates the
  121. rootNode = node || dojo.body();
  122. dojox.mobile.app.STAGE_CONTROLLER_ACTIVE = true;
  123. dojo.subscribe("/dojox/mobile/app/goback", function(){
  124. stageController.popScene();
  125. });
  126. dojo.subscribe("/dojox/mobile/app/alert", function(params){
  127. dojox.mobile.app.getActiveSceneController().showAlertDialog(params);
  128. });
  129. dojo.subscribe("/dojox/mobile/app/pushScene", function(sceneName, params){
  130. stageController.pushScene(sceneName, params || {});
  131. });
  132. // Get the list of files to load per scene/view
  133. dojo.xhrGet({
  134. url: "view-resources.json",
  135. load: function(data){
  136. var resources = [];
  137. if(data){
  138. // Should be an array
  139. sceneResources = data = dojo.fromJson(data);
  140. // Get the list of files to load that have no scene
  141. // specified, and therefore should be loaded on
  142. // startup
  143. for(var i = 0; i < data.length; i++){
  144. if(!data[i].scene){
  145. resources.push(data[i]);
  146. }
  147. }
  148. }
  149. if(resources.length > 0){
  150. loadResources(resources, pushFirstScene);
  151. }else{
  152. pushFirstScene();
  153. }
  154. },
  155. error: pushFirstScene
  156. });
  157. initBackButton();
  158. },
  159. getActiveSceneController: function(){
  160. // summary:
  161. // Gets the controller for the active scene.
  162. return stageController.getActiveSceneController();
  163. },
  164. getStageController: function(){
  165. // summary:
  166. // Gets the stage controller.
  167. return stageController;
  168. },
  169. loadResources: function(resources, callback){
  170. loadResources(resources, callback);
  171. },
  172. loadResourcesForScene: function(sceneName, callback){
  173. var resources = [];
  174. // Get the list of files to load that have no scene
  175. // specified, and therefore should be loaded on
  176. // startup
  177. for(var i = 0; i < sceneResources.length; i++){
  178. if(sceneResources[i].scene == sceneName){
  179. resources.push(sceneResources[i]);
  180. }
  181. }
  182. if(resources.length > 0){
  183. loadResources(resources, callback);
  184. }else{
  185. callback();
  186. }
  187. },
  188. resolveTemplate: function(sceneName){
  189. // summary:
  190. // Given the name of a scene, returns the path to it's template
  191. // file. For example, for a scene named 'main', the file
  192. // returned is 'app/views/main/main-scene.html'
  193. // This function can be overridden if it is desired to have
  194. // a different name to file mapping.
  195. return "app/views/" + sceneName + "/" + sceneName + "-scene.html";
  196. },
  197. resolveAssistant: function(sceneName){
  198. // summary:
  199. // Given the name of a scene, returns the path to it's assistant
  200. // file. For example, for a scene named 'main', the file
  201. // returned is 'app/assistants/main-assistant.js'
  202. // This function can be overridden if it is desired to have
  203. // a different name to file mapping.
  204. return "app/assistants/" + sceneName + "-assistant.js";
  205. }
  206. });
  207. })();
  208. }