ScrollPane.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. require({cache:{
  2. 'url:dojox/layout/resources/ScrollPane.html':"<div class=\"dojoxScrollWindow\" dojoAttachEvent=\"onmouseenter: _enter, onmouseleave: _leave\">\n <div class=\"dojoxScrollWrapper\" style=\"${style}\" dojoAttachPoint=\"wrapper\" dojoAttachEvent=\"onmousemove: _calc\">\n\t<div class=\"dojoxScrollPane\" dojoAttachPoint=\"containerNode\"></div>\n </div>\n <div dojoAttachPoint=\"helper\" class=\"dojoxScrollHelper\"><span class=\"helperInner\">|</span></div>\n</div>"}});
  3. define("dojox/layout/ScrollPane", ["dojo/_base/kernel","dojo/_base/declare","dojo/_base/html","dojo/_base/fx",
  4. "dijit/_Templated","dijit/layout/ContentPane","dojo/dom-class",
  5. "dojo/text!./resources/ScrollPane.html"],
  6. function(kernel,declare,html,baseFx,Templated,ContentPane,domClass,template){
  7. kernel.experimental("dojox.layout.ScrollPane");
  8. // FIXME: need to adust the _line somehow, it stops scrolling
  9. /*=====
  10. var ContentPane = dijit.layout.ContentPane,
  11. Templated = dijit._Templated;
  12. =====*/
  13. declare("dojox.layout.ScrollPane",[ContentPane, Templated],{
  14. // summary: A pane that "scrolls" its content based on the mouse poisition inside
  15. //
  16. // description:
  17. // A sizable container that takes it's content's natural size and creates
  18. // a scroll effect based on the relative mouse position. It is an interesting
  19. // way to display lists of data, or blocks of content, within a confined
  20. // space.
  21. //
  22. // Horizontal scrolling is supported. Combination scrolling is not.
  23. //
  24. // example:
  25. // | <div dojoType="dojox.layout.ScrollPane" style="width:150px height:300px;">
  26. // | <!-- any height content -->
  27. // | </div>
  28. //
  29. // _line: dojo._Line
  30. // storage for our top and bottom most scrollpoints
  31. _line: null,
  32. // _lo: the height of the visible pane
  33. _lo: null,
  34. _offset: 15,
  35. // orientation: String
  36. // either "horizontal" or "vertical" for scroll orientation.
  37. orientation: "vertical",
  38. // alwaysShow: Boolean
  39. // whether the scroll helper should hide when mouseleave
  40. autoHide: true,
  41. templateString: template,
  42. resize: function(size){
  43. // summary: calculates required sizes. Call this if you add/remove content manually, or reload the content.
  44. // if size is passed, it means we need to take care of sizing ourself (this is for IE<8)
  45. if(size){
  46. if(size.h){
  47. html.style(this.domNode,'height',size.h+'px');
  48. }
  49. if(size.w){
  50. html.style(this.domNode,'width',size.w+'px');
  51. }
  52. }
  53. var dir = this._dir,
  54. vert = this._vertical,
  55. val = this.containerNode[(vert ? "scrollHeight" : "scrollWidth")];
  56. html.style(this.wrapper, this._dir, this.domNode.style[this._dir]);
  57. this._lo = html.coords(this.wrapper, true);
  58. this._size = Math.max(0, val - this._lo[(vert ? "h" : "w")]);
  59. if(!this._size){
  60. this.helper.style.display="none";
  61. //make sure we reset scroll position, otherwise the content may be hidden
  62. this.wrapper[this._scroll]=0;
  63. return;
  64. }else{
  65. this.helper.style.display="";
  66. }
  67. this._line = new baseFx._Line(0 - this._offset, this._size + (this._offset * 2));
  68. // share a relative position w the scroll offset via a line
  69. var u = this._lo[(vert ? "h" : "w")],
  70. r = Math.min(1, u / val), // ratio
  71. s = u * r, // size
  72. c = Math.floor(u - (u * r)); // center
  73. this._helpLine = new baseFx._Line(0, c);
  74. // size the helper
  75. html.style(this.helper, dir, Math.floor(s) + "px");
  76. },
  77. postCreate: function(){
  78. this.inherited(arguments);
  79. // for the helper
  80. if(this.autoHide){
  81. this._showAnim = baseFx._fade({ node:this.helper, end:0.5, duration:350 });
  82. this._hideAnim = baseFx.fadeOut({ node:this.helper, duration: 750 });
  83. }
  84. // orientation helper
  85. this._vertical = (this.orientation == "vertical");
  86. if(!this._vertical){
  87. domClass.add(this.containerNode,"dijitInline");
  88. this._dir = "width";
  89. this._edge = "left";
  90. this._scroll = "scrollLeft";
  91. }else{
  92. this._dir = "height";
  93. this._edge = "top";
  94. this._scroll = "scrollTop";
  95. }
  96. if(this._hideAnim){
  97. this._hideAnim.play();
  98. }
  99. html.style(this.wrapper,"overflow","hidden");
  100. },
  101. _set: function(/* Float */n){
  102. if(!this._size){ return; }
  103. // summary: set the pane's scroll offset, and position the virtual scroll helper
  104. this.wrapper[this._scroll] = Math.floor(this._line.getValue(n));
  105. html.style(this.helper, this._edge, Math.floor(this._helpLine.getValue(n)) + "px");
  106. },
  107. _calc: function(/* Event */e){
  108. // summary: calculate the relative offset of the cursor over the node, and call _set
  109. if(!this._lo){ this.resize(); }
  110. this._set(this._vertical ?
  111. ((e.pageY - this._lo.y) / this._lo.h) :
  112. ((e.pageX - this._lo.x) / this._lo.w)
  113. );
  114. },
  115. _enter: function(e){
  116. if(this._hideAnim){
  117. if(this._hideAnim.status() == "playing"){
  118. this._hideAnim.stop();
  119. }
  120. this._showAnim.play();
  121. }
  122. },
  123. _leave: function(e){
  124. if(this._hideAnim){
  125. this._hideAnim.play();
  126. }
  127. }
  128. });
  129. });