VolumeButton.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. define("dojox/av/widget/VolumeButton", ['dojo', 'dijit', 'dijit/_Widget', 'dijit/_TemplatedMixin', 'dijit/form/Button'],function(dojo, dijit){
  2. dojo.declare("dojox.av.widget.VolumeButton", [dijit._Widget, dijit._TemplatedMixin], {
  3. // summary:
  4. // A volume widget to use with dojox.av.widget.Player
  5. //
  6. // description:
  7. // Controls and displays the volume of the media. This widget
  8. // opens a slider on click that is used to adjust the volume.
  9. // The icon changes according to the volume level.
  10. //
  11. templateString: dojo.cache("dojox.av.widget","resources/VolumeButton.html"),
  12. //
  13. postCreate: function(){
  14. // summary:
  15. // Initialize the widget.
  16. //
  17. this.handleWidth = dojo.marginBox(this.handle).w;
  18. this.width = dojo.marginBox(this.volumeSlider).w;
  19. this.slotWidth = 100;
  20. dojo.setSelectable(this.handle, false);
  21. this.volumeSlider = this.domNode.removeChild(this.volumeSlider);
  22. },
  23. setMedia: function(/* Object */med){
  24. // summary:
  25. // A common method to set the media in all Player widgets.
  26. // May do connections and initializations.
  27. //
  28. this.media = med;
  29. this.updateIcon();
  30. },
  31. updateIcon: function(/*Float*/ vol){
  32. // summary:
  33. // Changes the icon on the button according to volume level.
  34. //
  35. vol = (vol===undefined) ? this.media.volume() : vol;
  36. if(vol===0){
  37. dojo.attr(this.domNode, "class", "Volume mute");
  38. }else if(vol<.334){
  39. dojo.attr(this.domNode, "class", "Volume low");
  40. }else if(vol<.667){
  41. dojo.attr(this.domNode, "class", "Volume med");
  42. }else{
  43. dojo.attr(this.domNode, "class", "Volume high");
  44. }
  45. },
  46. onShowVolume: function(/*DOMEvent*/evt){
  47. // summary:
  48. // Shows the volume slider.
  49. //
  50. if(this.showing==undefined){
  51. dojo.body().appendChild(this.volumeSlider);
  52. this.showing = false;
  53. }
  54. if(!this.showing){
  55. var TOPMARG = 2;
  56. var LEFTMARG = 7;
  57. var vol = this.media.volume();
  58. var dim = this._getVolumeDim();
  59. var hand = this._getHandleDim();
  60. this.x = dim.x - this.width;
  61. dojo.style(this.volumeSlider, "display", "");
  62. dojo.style(this.volumeSlider, "top", dim.y+"px");
  63. dojo.style(this.volumeSlider, "left", (this.x)+"px");
  64. var x = (this.slotWidth * vol);
  65. dojo.style(this.handle, "top", (TOPMARG+(hand.w/2))+"px");
  66. dojo.style(this.handle, "left", (x+LEFTMARG+(hand.h/2))+"px");
  67. this.showing = true;
  68. //this.startDrag();
  69. this.clickOff = dojo.connect(dojo.doc, "onmousedown", this, "onDocClick");
  70. }else{
  71. this.onHideVolume();
  72. }
  73. },
  74. onDocClick: function(/*DOMEvent*/evt){
  75. // summary:
  76. // Fired on document.onmousedown. Checks if clicked inside
  77. // of this widget or not.
  78. //
  79. if(!dojo.isDescendant(evt.target, this.domNode) && !dojo.isDescendant(evt.target, this.volumeSlider)){
  80. this.onHideVolume();
  81. }
  82. },
  83. onHideVolume: function(){
  84. // summary:
  85. // Hides volume slider.
  86. //
  87. this.endDrag();
  88. dojo.style(this.volumeSlider, "display", "none");
  89. this.showing = false;
  90. },
  91. onDrag: function(/*DOMEvent*/evt){
  92. // summary:
  93. // Fired on mousemove. Updates volume and position of
  94. // slider handle.
  95. var beg = this.handleWidth/2;
  96. var end = beg + this.slotWidth
  97. var x = evt.clientX - this.x;
  98. if(x<beg) x = beg;
  99. if(x>end) x=end;
  100. dojo.style(this.handle, "left", (x)+"px");
  101. var p = (x-beg)/(end-beg);
  102. this.media.volume(p);
  103. this.updateIcon(p);
  104. },
  105. startDrag: function(){
  106. // summary:
  107. // Fired on mousedown of the slider handle.
  108. //
  109. this.isDragging = true;
  110. this.cmove = dojo.connect(dojo.doc, "mousemove", this, "onDrag");
  111. this.cup = dojo.connect(dojo.doc, "mouseup", this, "endDrag");
  112. },
  113. endDrag: function(){
  114. // summary:
  115. // Fired on mouseup of the slider handle.
  116. //
  117. this.isDragging = false;
  118. if(this.cmove) dojo.disconnect(this.cmove);
  119. if(this.cup) dojo.disconnect(this.cup);
  120. this.handleOut();
  121. },
  122. handleOver: function(){
  123. // summary:
  124. // Highlights the slider handle on mouseover, and
  125. // stays highlighted during drag.
  126. //
  127. dojo.addClass(this.handle, "over");
  128. },
  129. handleOut: function(){
  130. // summary:
  131. // Unhighlights handle onmouseover, or on endDrag.
  132. //
  133. if(!this.isDragging){
  134. dojo.removeClass(this.handle, "over");
  135. }
  136. },
  137. _getVolumeDim: function(){
  138. // summary:
  139. // Gets dimensions of slider background node.
  140. // Only uses dojo.coords once, unless the page
  141. // or player is resized.
  142. //
  143. if(this._domCoords){
  144. return this._domCoords;
  145. }
  146. this._domCoords = dojo.coords(this.domNode);
  147. return this._domCoords;
  148. },
  149. _getHandleDim: function(){
  150. // summary:
  151. // Gets dimensions of slider handle.
  152. // Only uses dojo.marginBox once.
  153. if(this._handleCoords){
  154. return this._handleCoords;
  155. }
  156. this._handleCoords = dojo.marginBox(this.handle);
  157. return this._handleCoords;
  158. },
  159. onResize: function(/*Object*/playerDimensions){
  160. // summary:
  161. // Fired on player resize. Zeros dimensions
  162. // so that it can be calculated again.
  163. //
  164. this.onHideVolume();
  165. this._domCoords = null;
  166. }
  167. });
  168. return dojox.av.widget.VolumeButton;
  169. });