_ScrollableMixin.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. define("dojox/mobile/_ScrollableMixin", [
  2. "dojo/_base/kernel",
  3. "dojo/_base/declare",
  4. "dojo/_base/lang",
  5. "dojo/_base/window",
  6. "dojo/dom",
  7. "dojo/dom-class",
  8. "dijit/registry", // registry.byNode
  9. "./scrollable"
  10. ], function(dojo, declare, lang, win, dom, domClass, registry, Scrollable){
  11. // module:
  12. // dojox/mobile/_ScrollableMixin
  13. // summary:
  14. // Mixin for widgets to have a touch scrolling capability.
  15. var cls = declare("dojox.mobile._ScrollableMixin", null, {
  16. // summary:
  17. // Mixin for widgets to have a touch scrolling capability.
  18. // description:
  19. // Actual implementation is in scrollable.js.
  20. // scrollable.js is not a dojo class, but just a collection
  21. // of functions. This module makes scrollable.js a dojo class.
  22. // fixedHeader: String
  23. // Id of the fixed header.
  24. fixedHeader: "",
  25. // fixedFooter: String
  26. // Id of the fixed footer.
  27. fixedFooter: "",
  28. // scrollableParams: Object
  29. // Parameters for dojox.mobile.scrollable.init().
  30. scrollableParams: null,
  31. // allowNestedScrolls: Boolean
  32. // e.g. Allow ScrollableView in a SwapView.
  33. allowNestedScrolls: true,
  34. constructor: function(){
  35. this.scrollableParams = {};
  36. },
  37. destroy: function(){
  38. this.cleanup();
  39. this.inherited(arguments);
  40. },
  41. startup: function(){
  42. if(this._started){ return; }
  43. var node;
  44. var params = this.scrollableParams;
  45. if(this.fixedHeader){
  46. node = dom.byId(this.fixedHeader);
  47. if(node.parentNode == this.domNode){ // local footer
  48. this.isLocalHeader = true;
  49. }
  50. params.fixedHeaderHeight = node.offsetHeight;
  51. }
  52. if(this.fixedFooter){
  53. node = dom.byId(this.fixedFooter);
  54. if(node.parentNode == this.domNode){ // local footer
  55. this.isLocalFooter = true;
  56. node.style.bottom = "0px";
  57. }
  58. params.fixedFooterHeight = node.offsetHeight;
  59. }
  60. this.init(params);
  61. if(this.allowNestedScrolls){
  62. for(var p = this.getParent(); p; p = p.getParent()){
  63. if(p && p.scrollableParams){
  64. this.isNested = true;
  65. this.dirLock = true;
  66. p.dirLock = true;
  67. break;
  68. }
  69. }
  70. }
  71. this.inherited(arguments);
  72. },
  73. findAppBars: function(){
  74. // summary:
  75. // Search for application-specific header or footer.
  76. var i, len, c;
  77. for(i = 0, len = win.body().childNodes.length; i < len; i++){
  78. c = win.body().childNodes[i];
  79. this.checkFixedBar(c, false);
  80. }
  81. if(this.domNode.parentNode){
  82. for(i = 0, len = this.domNode.parentNode.childNodes.length; i < len; i++){
  83. c = this.domNode.parentNode.childNodes[i];
  84. this.checkFixedBar(c, false);
  85. }
  86. }
  87. this.fixedFooterHeight = this.fixedFooter ? this.fixedFooter.offsetHeight : 0;
  88. },
  89. checkFixedBar: function(/*DomNode*/node, /*Boolean*/local){
  90. // summary:
  91. // Checks if the given node is a fixed bar or not.
  92. if(node.nodeType === 1){
  93. var fixed = node.getAttribute("fixed")
  94. || (registry.byNode(node) && registry.byNode(node).fixed);
  95. if(fixed === "top"){
  96. domClass.add(node, "mblFixedHeaderBar");
  97. if(local){
  98. node.style.top = "0px";
  99. this.fixedHeader = node;
  100. }
  101. return fixed;
  102. }else if(fixed === "bottom"){
  103. domClass.add(node, "mblFixedBottomBar");
  104. this.fixedFooter = node;
  105. return fixed;
  106. }
  107. }
  108. return null;
  109. }
  110. });
  111. lang.extend(cls, new Scrollable(dojo, dojox));
  112. return cls;
  113. });