ScrollableView.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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.ScrollableView"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.mobile.ScrollableView"] = true;
  8. dojo.provide("dojox.mobile.ScrollableView");
  9. dojo.require("dijit._WidgetBase");
  10. dojo.require("dojox.mobile");
  11. dojo.require("dojox.mobile._ScrollableMixin");
  12. // summary:
  13. // A container that has a touch scrolling capability.
  14. // description:
  15. // ScrollableView is a subclass of View (=dojox.mobile.View).
  16. // Unlike the base View class, ScrollableView's domNode always stays
  17. // at the top of the screen and its height is "100%" of the screen.
  18. // In this fixed domNode, containerNode scrolls. Browser's default
  19. // scrolling behavior is disabled, and the scrolling machinery is
  20. // re-implemented with JavaScript. Thus the user does not need to use the
  21. // two-finger operation to scroll an inner DIV (containerNode).
  22. // The main purpose of this widget is to realize fixed-positioned header
  23. // and/or footer bars.
  24. dojo.declare(
  25. "dojox.mobile.ScrollableView",
  26. [dojox.mobile.View, dojox.mobile._ScrollableMixin],
  27. {
  28. flippable: false,
  29. buildRendering: function(){
  30. this.inherited(arguments);
  31. dojo.addClass(this.domNode, "mblScrollableView");
  32. this.domNode.style.overflow = "hidden";
  33. this.domNode.style.top = "0px";
  34. this.domNode.style.height = "100%";
  35. this.containerNode = dojo.create("DIV",
  36. {className:"mblScrollableViewContainer"}, this.domNode);
  37. this.containerNode.style.position = "absolute";
  38. if(this.scrollDir === "v" || this.flippable){
  39. this.containerNode.style.width = "100%";
  40. }
  41. this.reparent();
  42. this.findAppBars();
  43. },
  44. addChild: function(widget){
  45. var c = widget.domNode;
  46. var fixed = this._checkFixedBar(c, true);
  47. if(fixed){
  48. this.domNode.appendChild(c);
  49. if(fixed === "top"){
  50. this.fixedHeaderHeight = c.offsetHeight;
  51. this.containerNode.style.paddingTop = this.fixedHeaderHeight + "px";
  52. }else if(fixed === "bottom"){
  53. this.fixedFooterHeight = c.offsetHeight;
  54. this.isLocalFooter = true;
  55. c.style.bottom = "0px";
  56. }
  57. this.resizeView();
  58. }else{
  59. this.containerNode.appendChild(c);
  60. }
  61. },
  62. reparent: function(){
  63. // move all the children, except header and footer, to containerNode.
  64. var i, idx, len, c;
  65. for(i = 0, idx = 0, len = this.domNode.childNodes.length; i < len; i++){
  66. c = this.domNode.childNodes[idx];
  67. // search for view-specific header or footer
  68. if(c === this.containerNode || this._checkFixedBar(c, true)){
  69. idx++;
  70. continue;
  71. }
  72. this.containerNode.appendChild(this.domNode.removeChild(c));
  73. }
  74. },
  75. findAppBars: function(){
  76. // search for application-specific header or footer
  77. var i, len, c;
  78. for(i = 0, len = dojo.body().childNodes.length; i < len; i++){
  79. c = dojo.body().childNodes[i];
  80. this._checkFixedBar(c, false);
  81. }
  82. if(this.domNode.parentNode){
  83. for(i = 0, len = this.domNode.parentNode.childNodes.length; i < len; i++){
  84. c = this.domNode.parentNode.childNodes[i];
  85. this._checkFixedBar(c, false);
  86. }
  87. }
  88. this.fixedHeaderHeight = this.fixedHeader ? this.fixedHeader.offsetHeight : 0;
  89. this.fixedFooterHeight = this.fixedFooter ? this.fixedFooter.offsetHeight : 0;
  90. },
  91. _checkFixedBar: function(/*DomNode*/node){
  92. if(node.nodeType === 1){
  93. var fixed = node.getAttribute("fixed")
  94. || (dijit.byNode(node) && dijit.byNode(node).fixed);
  95. if(fixed){
  96. dojo.style(node, {
  97. position: "absolute",
  98. width: "100%",
  99. zIndex: 1
  100. });
  101. }
  102. if(fixed === "top"){
  103. node.style.top = "0px";
  104. this.fixedHeader = node;
  105. return fixed;
  106. }else if(fixed === "bottom"){
  107. this.fixedFooter = node;
  108. return fixed;
  109. }
  110. }
  111. return null;
  112. },
  113. onAfterTransitionIn: function(moveTo, dir, transition, context, method){
  114. this.flashScrollBar();
  115. }
  116. });
  117. }