source: Dev/branches/rest-dojo-ui/client/dojox/av/widget/VolumeButton.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

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