ContentPane.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. define("dojox/mobile/ContentPane", [
  2. "dojo/_base/kernel",
  3. "dojo/_base/array",
  4. "dojo/_base/declare",
  5. "dojo/_base/lang",
  6. "dojo/_base/window",
  7. "dijit/_Contained",
  8. "dijit/_WidgetBase",
  9. "dojo/_base/xhr",
  10. "./ProgressIndicator"
  11. ], function(dojo, array, declare, lang, win, Contained, WidgetBase, xhr, ProgressIndicator){
  12. /*=====
  13. var Contained = dijit._Contained;
  14. var WidgetBase = dijit._WidgetBase;
  15. =====*/
  16. // module:
  17. // dojox/mobile/ContentPane
  18. // summary:
  19. // A very simple content pane to embed an HTML fragment.
  20. return declare("dojox.mobile.ContentPane", [WidgetBase, Contained],{
  21. // summary:
  22. // A very simple content pane to embed an HTML fragment.
  23. // description:
  24. // This widget embeds an HTML fragment and run the parser. onLoad()
  25. // is called when parsing is done and the content is ready.
  26. // "dojo/_base/xhr" is in the dependency list. Usually this is not
  27. // necessary, but there is a case where dojox.mobile custom build
  28. // does not contain xhr. Note that this widget does not inherit
  29. // from dijit._Container.
  30. // href: String
  31. // URL of the content to embed.
  32. href: "",
  33. // content: String
  34. // An html fragment to embed.
  35. content: "",
  36. // parseOnLoad: Boolean
  37. // If true, runs the parser when the load completes.
  38. parseOnLoad: true,
  39. // prog: Boolean
  40. // If true, shows progress indicator.
  41. prog: true,
  42. buildRendering: function(){
  43. this.inherited(arguments);
  44. this.domNode.className = "mblContentPane";
  45. if(!this.containerNode){
  46. this.containerNode = this.domNode;
  47. }
  48. },
  49. startup: function(){
  50. if(this._started){ return; }
  51. if(this.prog){
  52. this._p = ProgressIndicator.getInstance();
  53. }
  54. var parent = this.getParent && this.getParent();
  55. if(!parent || !parent.resize){ // top level widget
  56. this.resize();
  57. }
  58. this.inherited(arguments);
  59. },
  60. resize: function(){
  61. // summary:
  62. // Calls resize() of each child widget.
  63. array.forEach(this.getChildren(), function(child){
  64. if(child.resize){ child.resize(); }
  65. });
  66. },
  67. loadHandler: function(/*String*/response){
  68. // summary:
  69. // A handler called when load completes.
  70. this.set("content", response);
  71. },
  72. errorHandler: function(err){
  73. // summary:
  74. // An error handler called when load fails.
  75. if(this._p){ this._p.stop(); }
  76. },
  77. onLoad: function(){
  78. // summary:
  79. // Stub method to allow the application to connect to.
  80. // Called when parsing is done and the content is ready.
  81. },
  82. _setHrefAttr: function(/*String*/href){
  83. var p = this._p;
  84. if(p){
  85. win.body().appendChild(p.domNode);
  86. p.start();
  87. }
  88. this.href = href;
  89. xhr.get({
  90. url: href,
  91. handleAs: "text",
  92. load: lang.hitch(this, "loadHandler"),
  93. error: lang.hitch(this, "errorHandler")
  94. });
  95. },
  96. _setContentAttr: function(/*String|DomNode*/data){
  97. this.destroyDescendants();
  98. if(typeof data === "object"){
  99. this.domNode.appendChild(data);
  100. }else{
  101. this.domNode.innerHTML = data;
  102. }
  103. if(this.parseOnLoad){
  104. dojo.parser.parse(this.domNode);
  105. }
  106. if(this._p){ this._p.stop(); }
  107. this.onLoad();
  108. }
  109. });
  110. });