ProgressSlider.js 4.5 KB

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