ProgressSlider.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. define("dojox/av/widget/ProgressSlider", ['dojo', 'dijit', 'dijit/_Widget', 'dijit/_TemplatedMixin'],function(dojo, dijit){
  2. dojo.declare("dojox.av.widget.ProgressSlider", [dijit._Widget, dijit._TemplatedMixin], {
  3. // summary:
  4. // A custom slider widget to use with dojox.av.widget.Player.
  5. // description:
  6. // Displays the current playhead position of the media. Has two
  7. // progress bars: one for playhead position, and one for download
  8. // progress.
  9. //
  10. templateString: dojo.cache("dojox.av.widget","resources/ProgressSlider.html"),
  11. postCreate: function(){
  12. // summary:
  13. // Initialize slider.
  14. //
  15. this.seeking = false;
  16. this.handleWidth = dojo.marginBox(this.handle).w;
  17. var dim = dojo.coords(this.domNode);
  18. this.finalWidth = dim.w
  19. this.width = dim.w-this.handleWidth;
  20. this.x = dim.x;
  21. dojo.setSelectable(this.domNode, false);
  22. dojo.setSelectable(this.handle, false);
  23. },
  24. setMedia: function(/* Object */med, playerWidget){
  25. // summary:
  26. // A common method to set the media in all Player widgets.
  27. // May do connections and initializations.
  28. //
  29. this.playerWidget = playerWidget;
  30. this.media = med;
  31. dojo.connect(this.media, "onMetaData", this, function(data){
  32. if(data && data.duration){
  33. this.duration = data.duration;
  34. }
  35. });
  36. dojo.connect(this.media, "onEnd", this, function(){
  37. dojo.disconnect(this.posCon);
  38. this.setHandle(this.duration);
  39. });
  40. dojo.connect(this.media, "onStart", this, function(){
  41. this.posCon = dojo.connect(this.media, "onPosition", this, "setHandle");
  42. });
  43. dojo.connect(this.media, "onDownloaded", this, function(percent){
  44. this.setLoadedPosition(percent*.01);
  45. this.width = this.finalWidth * .01 * percent;
  46. });
  47. },
  48. onDrag: function(/* HTMLEvent */ evt){
  49. // summary:
  50. // Fired when the mouse is moved. Sets the slider.
  51. //
  52. var x = evt.clientX - this.x;
  53. if(x<0) x = 0;
  54. if(x>this.width-this.handleWidth) x=this.width-this.handleWidth;
  55. var p = x/this.finalWidth;
  56. this.media.seek( this.duration * p );
  57. dojo.style(this.handle, "marginLeft", x+"px");
  58. dojo.style(this.progressPosition, "width", x+"px");
  59. },
  60. startDrag: function(){
  61. // summary:
  62. // Fired onmousedown of the slider handle.
  63. //
  64. dojo.setSelectable(this.playerWidget.domNode, false);
  65. this.seeking = true;
  66. this.cmove = dojo.connect(dojo.doc, "mousemove", this, "onDrag");
  67. this.cup = dojo.connect(dojo.doc, "mouseup", this, "endDrag");
  68. },
  69. endDrag: function(){
  70. // summary:
  71. // Fired on document.onmouseup.
  72. //
  73. dojo.setSelectable(this.playerWidget.domNode, true);
  74. this.seeking = false;
  75. if(this.cmove) dojo.disconnect(this.cmove);
  76. if(this.cup) dojo.disconnect(this.cup);
  77. this.handleOut();
  78. },
  79. setHandle: function(time){
  80. // summary:
  81. // Sets the slider handle (when it is not being dragged)
  82. //
  83. if(!this.seeking){
  84. var w = this.width-this.handleWidth;
  85. var p = time/this.duration;
  86. var x = p*w;
  87. dojo.style(this.handle, "marginLeft", x+"px");
  88. dojo.style(this.progressPosition, "width", x+"px");
  89. }
  90. },
  91. setLoadedPosition: function(decimal){
  92. // summary:
  93. // Sets the download progress bar to the percentage of how much
  94. // the media has been downloaded.
  95. dojo.style(this.progressLoaded, "width", (this.finalWidth*decimal)+"px");
  96. },
  97. handleOver: function(){
  98. // summary:
  99. // Highlights the slider handle on mouseover, and
  100. // stays highlighted during drag.
  101. //
  102. dojo.addClass(this.handle, "over");
  103. },
  104. handleOut: function(){
  105. // summary:
  106. // Unhighlights handle onmouseover, or on endDrag.
  107. //
  108. if(!this.seeking){
  109. dojo.removeClass(this.handle, "over");
  110. }
  111. },
  112. onResize: function(playerDimensions){
  113. // summary:
  114. // Handles player resize. Need to recalculate the width of
  115. // position an download bars.
  116. var dim = dojo.coords(this.domNode);
  117. this.finalWidth = dim.w;
  118. }
  119. });
  120. return dojox.av.widget.ProgressSlider;
  121. });