1 | dojo.provide("dojox.mobile.app.SceneController"); |
---|
2 | dojo.experimental("dojox.mobile.app.SceneController"); |
---|
3 | dojo.require("dojox.mobile._base"); |
---|
4 | |
---|
5 | (function(){ |
---|
6 | |
---|
7 | var app = dojox.mobile.app; |
---|
8 | |
---|
9 | var templates = {}; |
---|
10 | |
---|
11 | dojo.declare("dojox.mobile.app.SceneController", dojox.mobile.View, { |
---|
12 | |
---|
13 | stageController: null, |
---|
14 | |
---|
15 | keepScrollPos: false, |
---|
16 | |
---|
17 | init: function(sceneName, params){ |
---|
18 | // summary: |
---|
19 | // Initializes the scene by loading the HTML template and code, if it has |
---|
20 | // not already been loaded |
---|
21 | |
---|
22 | this.sceneName = sceneName; |
---|
23 | this.params = params; |
---|
24 | var templateUrl = app.resolveTemplate(sceneName); |
---|
25 | |
---|
26 | this._deferredInit = new dojo.Deferred(); |
---|
27 | |
---|
28 | if(templates[sceneName]){ |
---|
29 | // If the template has been cached, do not load it again. |
---|
30 | this._setContents(templates[sceneName]); |
---|
31 | }else{ |
---|
32 | // Otherwise load the template |
---|
33 | dojo.xhrGet({ |
---|
34 | url: templateUrl, |
---|
35 | handleAs: "text" |
---|
36 | }).addCallback(dojo.hitch(this, this._setContents)); |
---|
37 | } |
---|
38 | |
---|
39 | return this._deferredInit; |
---|
40 | }, |
---|
41 | |
---|
42 | _setContents: function(templateHtml){ |
---|
43 | // summary: |
---|
44 | // Sets the content of the View, and invokes either the loading or |
---|
45 | // initialization of the scene assistant. |
---|
46 | templates[this.sceneName] = templateHtml; |
---|
47 | |
---|
48 | this.domNode.innerHTML = "<div>" + templateHtml + "</div>"; |
---|
49 | |
---|
50 | var sceneAssistantName = ""; |
---|
51 | |
---|
52 | var nameParts = this.sceneName.split("-"); |
---|
53 | |
---|
54 | for(var i = 0; i < nameParts.length; i++){ |
---|
55 | sceneAssistantName += nameParts[i].substring(0, 1).toUpperCase() |
---|
56 | + nameParts[i].substring(1); |
---|
57 | } |
---|
58 | sceneAssistantName += "Assistant"; |
---|
59 | this.sceneAssistantName = sceneAssistantName; |
---|
60 | |
---|
61 | var _this = this; |
---|
62 | |
---|
63 | dojox.mobile.app.loadResourcesForScene(this.sceneName, function(){ |
---|
64 | |
---|
65 | console.log("All resources for ",_this.sceneName," loaded"); |
---|
66 | |
---|
67 | var assistant; |
---|
68 | if(typeof(dojo.global[sceneAssistantName]) != "undefined"){ |
---|
69 | _this._initAssistant(); |
---|
70 | }else{ |
---|
71 | var assistantUrl = app.resolveAssistant(_this.sceneName); |
---|
72 | |
---|
73 | dojo.xhrGet({ |
---|
74 | url: assistantUrl, |
---|
75 | handleAs: "text" |
---|
76 | }).addCallback(function(text){ |
---|
77 | try{ |
---|
78 | dojo.eval(text); |
---|
79 | }catch(e){ |
---|
80 | console.log("Error initializing code for scene " + _this.sceneName |
---|
81 | + '. Please check for syntax errors'); |
---|
82 | throw e; |
---|
83 | } |
---|
84 | _this._initAssistant(); |
---|
85 | }); |
---|
86 | } |
---|
87 | }); |
---|
88 | |
---|
89 | }, |
---|
90 | |
---|
91 | _initAssistant: function(){ |
---|
92 | // summary: |
---|
93 | // Initializes the scene assistant. At this point, the View is |
---|
94 | // populated with the HTML template, and the scene assistant type |
---|
95 | // is declared. |
---|
96 | |
---|
97 | console.log("Instantiating the scene assistant " + this.sceneAssistantName); |
---|
98 | |
---|
99 | var cls = dojo.getObject(this.sceneAssistantName); |
---|
100 | |
---|
101 | if(!cls){ |
---|
102 | throw Error("Unable to resolve scene assistant " |
---|
103 | + this.sceneAssistantName); |
---|
104 | } |
---|
105 | |
---|
106 | this.assistant = new cls(this.params); |
---|
107 | |
---|
108 | this.assistant.controller = this; |
---|
109 | this.assistant.domNode = this.domNode.firstChild; |
---|
110 | |
---|
111 | this.assistant.setup(); |
---|
112 | |
---|
113 | this._deferredInit.callback(); |
---|
114 | }, |
---|
115 | |
---|
116 | query: function(selector, node){ |
---|
117 | // summary: |
---|
118 | // Queries for DOM nodes within either the node passed in as an argument |
---|
119 | // or within this view. |
---|
120 | |
---|
121 | return dojo.query(selector, node || this.domNode) |
---|
122 | }, |
---|
123 | |
---|
124 | parse: function(node){ |
---|
125 | var widgets = this._widgets = |
---|
126 | dojox.mobile.parser.parse(node || this.domNode, { |
---|
127 | controller: this |
---|
128 | }); |
---|
129 | |
---|
130 | // Tell all widgets what their controller is. |
---|
131 | for(var i = 0; i < widgets.length; i++){ |
---|
132 | widgets[i].set("controller", this); |
---|
133 | } |
---|
134 | }, |
---|
135 | |
---|
136 | getWindowSize: function(){ |
---|
137 | // TODO, this needs cross browser testing |
---|
138 | |
---|
139 | return { |
---|
140 | w: dojo.global.innerWidth, |
---|
141 | h: dojo.global.innerHeight |
---|
142 | } |
---|
143 | }, |
---|
144 | |
---|
145 | showAlertDialog: function(props){ |
---|
146 | |
---|
147 | var size = dojo.marginBox(this.assistant.domNode); |
---|
148 | var dialog = new dojox.mobile.app.AlertDialog( |
---|
149 | dojo.mixin(props, {controller: this})); |
---|
150 | this.assistant.domNode.appendChild(dialog.domNode); |
---|
151 | |
---|
152 | console.log("Appended " , dialog.domNode, " to ", this.assistant.domNode); |
---|
153 | dialog.show(); |
---|
154 | }, |
---|
155 | |
---|
156 | popupSubMenu: function(info){ |
---|
157 | var widget = new dojox.mobile.app.ListSelector({ |
---|
158 | controller: this, |
---|
159 | destroyOnHide: true, |
---|
160 | onChoose: info.onChoose |
---|
161 | }); |
---|
162 | |
---|
163 | this.assistant.domNode.appendChild(widget.domNode); |
---|
164 | |
---|
165 | widget.set("data", info.choices); |
---|
166 | widget.show(info.fromNode); |
---|
167 | } |
---|
168 | }); |
---|
169 | |
---|
170 | })(); |
---|