Debian Codegraph @

dojox/mobile/app/_base.js

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