src/widgets/Controller.js
branchnew-model
changeset 875 43629caa77bc
parent 874 38b65761a7d5
child 880 4c7b33bf2795
equal deleted inserted replaced
874:38b65761a7d5 875:43629caa77bc
       
     1 /* Displays Play and Pause buttons, Search Button and Form, Volume Control */
       
     2 
       
     3 IriSP.Widgets.Controller = function(player, config) {
       
     4   IriSP.Widgets.Widget.call(this, player, config);
       
     5   
       
     6   this._searchLastValue = "";
       
     7 };
       
     8 
       
     9 IriSP.Widgets.Controller.prototype = new IriSP.Widgets.Widget();
       
    10 
       
    11 IriSP.Widgets.Controller.prototype.defaults = {}
       
    12 
       
    13 IriSP.Widgets.Controller.prototype.template =
       
    14     '<div class="Ldt-Ctrl">'
       
    15     + '<div class="Ldt-Ctrl-Left">'
       
    16     + '<div class="Ldt-Ctrl-button Ldt-Ctrl-Play Ldt-Ctrl-Play-PlayState Ldt-TraceMe" title="{{l10n.play_pause}}"></div>'
       
    17     + '<div class="Ldt-Ctrl-spacer"></div>'
       
    18     + '{{^disable_annotate_btn}}'
       
    19     + '<div class="Ldt-Ctrl-button Ldt-Ctrl-Annotate Ldt-TraceMe" title="{{l10n.annotate}}"></div>'
       
    20     + '<div class="Ldt-Ctrl-spacer"></div>'
       
    21     + '{{/disable_annotate_btn}}'
       
    22     + '{{^disable_search_btn}}'
       
    23     + '<div class="Ldt-Ctrl-button Ldt-Ctrl-SearchBtn Ldt-TraceMe" title="{{l10n.search}}"></div>'
       
    24     + '<div class="Ldt-Ctrl-spacer"></div>'
       
    25     + '{{/disable_search_btn}}'
       
    26     + '<div class="Ldt-Ctrl-Search">'
       
    27     + '<input class="Ldt-Ctrl-SearchInput Ldt-TraceMe"></input>'
       
    28     + '</div>'
       
    29     + '</div>'
       
    30     + '<div class="Ldt-Ctrl-Right">'
       
    31     + '<div class="Ldt-Ctrl-spacer"></div>'
       
    32     + '<div class="Ldt-Ctrl-Time">'
       
    33     + '<div class="Ldt-Ctrl-Time-Elapsed" title="{{l10n.elapsed_time}}">00:00</div>'
       
    34     + '<div class="Ldt-Ctrl-Time-Separator">/</div>'
       
    35     + '<div class="Ldt-Ctrl-Time-Total" title="{{l10n.total_time}}">00:00</div>'
       
    36     + '</div>'
       
    37     + '<div class="Ldt-Ctrl-spacer"></div>'
       
    38     + '<div class="Ldt-Ctrl-button Ldt-Ctrl-Sound Ldt-Ctrl-Sound-Full Ldt-TraceMe" title="{{l10n.mute_unmute}}"></div>'
       
    39     + '</div>'
       
    40     + '<div class="Ldt-Ctrl-Volume-Control" title="{{l10n.volume_control}}">'
       
    41     + '<div class="Ldt-Ctrl-Volume-Bar"></div>'
       
    42     + '</div>'
       
    43     + '</div>';
       
    44 
       
    45 IriSP.Widgets.Controller.prototype.messages = {
       
    46     "en": {
       
    47         "play_pause": "Play/Pause",
       
    48         "mute_unmute": "Mute/Unmute",
       
    49         "play": "Play",
       
    50         "pause": "Pause",
       
    51         "mute": "Mute",
       
    52         "unmute": "Unmute",
       
    53         "annotate": "Annotate",
       
    54         "search": "Search",
       
    55         "elapsed_time": "Elapsed time",
       
    56         "total_time": "Total time",
       
    57         "volume": "Volume",
       
    58         "volume_control": "Volume control"
       
    59     },
       
    60     "fr": {
       
    61         "play_pause": "Lecture/Pause",
       
    62         "mute_unmute": "Couper/Activer le son",
       
    63         "play": "Lecture",
       
    64         "pause": "Pause",
       
    65         "mute": "Couper le son",
       
    66         "unmute": "Activer le son",
       
    67         "annotate": "Annoter",
       
    68         "search": "Rechercher",
       
    69         "elapsed_time": "Durée écoulée",
       
    70         "total_time": "Durée totale",
       
    71         "volume": "Niveau sonore",
       
    72         "volume_control": "Réglage du niveau sonore"
       
    73     }
       
    74 };
       
    75 
       
    76 IriSP.Widgets.Controller.prototype.draw = function() {
       
    77     var _this = this;
       
    78     this.renderTemplate();
       
    79     
       
    80     // Define blocks
       
    81     this.$playButton = this.$.find(".Ldt-Ctrl-Play");
       
    82     this.$searchBlock = this.$.find(".Ldt-Ctrl-Search");
       
    83     this.$searchInput = this.$.find(".Ldt-Ctrl-SearchInput");
       
    84     this.$volumeBar = this.$.find(".Ldt-Ctrl-Volume-Bar");
       
    85     
       
    86     // handle events
       
    87     this.bindPopcorn("play","playButtonUpdater");
       
    88     this.bindPopcorn("pause","playButtonUpdater");
       
    89     this.bindPopcorn("volumechange","volumeUpdater");
       
    90     this.bindPopcorn("timeupdate","timeDisplayUpdater");
       
    91     this.bindPopcorn("loadedmetadata","timeDisplayUpdater");
       
    92     this.bindPopcorn("IriSP.search.matchFound","searchMatch");
       
    93     this.bindPopcorn("IriSP.search.noMatchFound","searchNoMatch");
       
    94     this.bindPopcorn("IriSP.search.triggeredSearch","triggeredSearch");
       
    95     
       
    96     // handle clicks
       
    97     this.$playButton.click(this.functionWrapper("playHandler"));
       
    98     
       
    99     this.$.find(".Ldt-Ctrl-Annotate").click(function() {
       
   100         _this.player.popcorn.trigger("IriSP.Player.AnnotateButton.clicked");
       
   101     });
       
   102     this.$.find(".Ldt-Ctrl-SearchBtn").click(this.functionWrapper("searchButtonHandler"));
       
   103     
       
   104     this.$searchInput.keyup(this.functionWrapper("searchHandler") );
       
   105   
       
   106 	var _volctrl = this.$.find(".Ldt-Ctrl-Volume-Control");
       
   107     this.$.find('.Ldt-Ctrl-Sound')
       
   108         .click(this.functionWrapper("muteHandler"))
       
   109         .mouseover(function() {
       
   110             _volctrl.show();
       
   111         })
       
   112         .mouseout(function() {
       
   113             _volctrl.hide();
       
   114         });
       
   115     _volctrl.mouseover(function() {
       
   116         _volctrl.show();
       
   117     }).mouseout(function() {
       
   118         _volctrl.hide();
       
   119     });
       
   120   
       
   121     
       
   122     // Allow Volume Cursor Dragging
       
   123     this.$volumeBar.slider({
       
   124         slide: function(event, ui) {
       
   125             _this.$volumeBar.attr("title",this.l10n.volume+': ' + ui.value + '%');
       
   126             _this.player.popcorn.volume(ui.value / 100);
       
   127         },
       
   128         stop: this.functionWrapper("volumeUpdater")
       
   129     });
       
   130 
       
   131     // trigger an IriSP.Player.MouseOver to the widgets that are interested (i.e : sliderWidget)
       
   132     this.$.hover(
       
   133         function() {
       
   134             _this.player.popcorn.trigger("IriSP.Player.MouseOver");
       
   135         }, 
       
   136         function() {
       
   137             _this.player.popcorn.trigger("IriSP.Player.MouseOut");
       
   138         });
       
   139     setTimeout(this.functionWrapper("volumeUpdater"), 1000);
       
   140     /* some players - including jwplayer - save the state of the mute button between sessions */
       
   141 };
       
   142 
       
   143 /* Update the elasped time div */
       
   144 IriSP.Widgets.Controller.prototype.timeDisplayUpdater = function() {
       
   145     var _curTime = this.player.popcorn.roundTime();
       
   146     if (typeof this._previousSecond !== "undefined" && _curTime === this._previousSecond) {
       
   147         return;
       
   148     }
       
   149   
       
   150     // we get it at each call because it may change.
       
   151     var _totalTime = this.source.getDuration(),
       
   152         _elapsedTime = new IriSP.Model.Time();
       
   153         
       
   154     _elapsedTime.setSeconds(_curTime);
       
   155   
       
   156     this.$.find(".Ldt-Ctrl-Time-Elapsed").html(_elapsedTime.toString());
       
   157     this.$.find(".Ldt-Ctrl-Time-Total").html(_totalTime.toString());
       
   158     this._previousSecond = _curTime;
       
   159 };
       
   160 
       
   161 /* update the icon of the button - separate function from playHandler
       
   162    because in some cases (for instance, when the user directly clicks on
       
   163    the jwplayer window) we have to change the icon without playing/pausing
       
   164 */
       
   165 IriSP.Widgets.Controller.prototype.playButtonUpdater = function() {
       
   166     
       
   167     var status = this.player.popcorn.media.paused;
       
   168   
       
   169     if (status) {
       
   170     /* the background sprite is changed by adding/removing the correct classes */
       
   171         this.$playButton
       
   172             .attr("title", this.l10n.play)
       
   173             .removeClass("Ldt-Ctrl-Play-PauseState")
       
   174             .addClass("Ldt-Ctrl-Play-PlayState");
       
   175     } else {
       
   176         this.$playButton
       
   177             .attr("title", this.l10n.pause)
       
   178             .removeClass("Ldt-Ctrl-Play-PlayState")
       
   179             .addClass("Ldt-Ctrl-Play-PauseState");
       
   180     }
       
   181 };
       
   182 
       
   183 
       
   184 IriSP.Widgets.Controller.prototype.playHandler = function() {
       
   185     
       
   186     var status = this.player.popcorn.media.paused;
       
   187   
       
   188     if (status) {        
       
   189         this.player.popcorn.play();   
       
   190     } else {
       
   191         this.player.popcorn.pause();
       
   192     }  
       
   193 };
       
   194 
       
   195 IriSP.Widgets.Controller.prototype.muteHandler = function() {
       
   196     this.player.popcorn.mute(!this.player.popcorn.muted());
       
   197 };
       
   198 
       
   199 IriSP.Widgets.Controller.prototype.volumeUpdater = function() {
       
   200     var _muted = this.player.popcorn.muted(),
       
   201         _vol = this.player.popcorn.volume();
       
   202     if (_vol === false) {
       
   203         _vol = .5;
       
   204     }
       
   205     var _soundCtl = this.$.find(".Ldt-Ctrl-Sound");
       
   206     _soundCtl.removeClass("Ldt-Ctrl-Sound-Mute Ldt-Ctrl-Sound-Half Ldt-Ctrl-Sound-Full");
       
   207     if (_muted) {        
       
   208         _soundCtl.attr("title", this.l10n.unmute)
       
   209             .addClass("Ldt-Ctrl-Sound-Mute");    
       
   210     } else {
       
   211         _soundCtl.attr("title", this.l10n.mute)
       
   212             .addClass(_vol < .5 ? "Ldt-Ctrl-Sound-Half" : "Ldt-Ctrl-Sound-Full" )
       
   213     }
       
   214     this.$volumeBar.slider("value", _muted ? 0 : 100 * _vol);
       
   215 };
       
   216 
       
   217 IriSP.Widgets.Controller.prototype.showSearchBlock = function() {
       
   218     this.$searchBlock.show("blind", { direction: "horizontal"}, 100);
       
   219     this.$searchInput.css('background-color','#fff');
       
   220    
       
   221     this.$searchInput.focus();
       
   222     
       
   223     // we need this variable because some widgets can find a match in
       
   224     // their data while at the same time others don't. As we want the
       
   225     // search field to become green when there's a match, we need a 
       
   226     // variable to remember that we had one.
       
   227     this._positiveMatch = false;
       
   228 
       
   229     // tell the world the field is open
       
   230     this.player.popcorn.trigger("IriSP.search.open");
       
   231 };
       
   232 
       
   233 IriSP.Widgets.Controller.prototype.hideSearchBlock = function() {
       
   234     this._searchLastValue = this.$searchInput.val();
       
   235     this.$searchInput.val('');
       
   236     this.$searchBlock.hide("blind", { direction: "horizontal"}, 75);
       
   237 
       
   238     this._positiveMatch = false;
       
   239     
       
   240     this.player.popcorn.trigger("IriSP.search.closed");
       
   241 };
       
   242 
       
   243 /** react to clicks on the search button */
       
   244 IriSP.Widgets.Controller.prototype.searchButtonHandler = function() {
       
   245     if ( this.$searchBlock.is(":hidden") ) {
       
   246         this.showSearchBlock();
       
   247         this.$searchInput.val(this._searchLastValue);      
       
   248         this.player.popcorn.trigger("IriSP.search", this._searchLastValue); // trigger the search to make it more natural.
       
   249 	} else {
       
   250         this.hideSearchBlock();
       
   251     }
       
   252 };
       
   253 
       
   254 /** this handler is called whenever the content of the search
       
   255    field changes */
       
   256 IriSP.Widgets.Controller.prototype.searchHandler = function() {
       
   257     this._searchLastValue = this.$searchInput.val();
       
   258     this._positiveMatch = false;
       
   259   
       
   260     // do nothing if the search field is empty, instead of highlighting everything.
       
   261     if (this._searchLastValue == "") {
       
   262         this.player.popcorn.trigger("IriSP.search.cleared");
       
   263         this.$searchInput.css('background-color','');
       
   264     } else {
       
   265         this.player.popcorn.trigger("IriSP.search", this._searchLastValue);
       
   266     }
       
   267 };
       
   268 
       
   269 /**
       
   270   handler for the IriSP.search.found message, which is sent by some views when they
       
   271   highlight a match.
       
   272 */
       
   273 IriSP.Widgets.Controller.prototype.searchMatch = function() {
       
   274     this._positiveMatch = true;
       
   275     this.$searchInput.css('background-color','#e1ffe1');
       
   276 };
       
   277 
       
   278 /** the same, except that no value could be found */
       
   279 IriSP.Widgets.Controller.prototype.searchNoMatch = function() {
       
   280     if (this._positiveMatch !== true) {
       
   281         this.$searchInput.css('background-color', "#d62e3a");
       
   282     }
       
   283 };
       
   284 
       
   285 /** react to an IriSP.Player.triggeredSearch - that is, when
       
   286     a widget ask the.Player to do a search on his behalf */
       
   287 IriSP.Widgets.Controller.prototype.triggeredSearch = function(searchString) {
       
   288     this.showSearchBlock();
       
   289     this.$searchInput.attr('value', searchString);      
       
   290     this.player.popcorn.trigger("IriSP.search", searchString); // trigger the search to make it more natural.
       
   291 };
       
   292 
       
   293