SceneController.js 4.3 KB

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