SceneController.js 4.6 KB

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