VolumeButton.js 5.7 KB

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