web/lib/metadataplayer/Controller.js
changeset 0 7f8907368bd5
equal deleted inserted replaced
-1:000000000000 0:7f8907368bd5
       
     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     this.lastSearchValue = "";
       
     6 };
       
     7 
       
     8 IriSP.Widgets.Controller.prototype = new IriSP.Widgets.Widget();
       
     9 
       
    10 IriSP.Widgets.Controller.prototype.defaults = {
       
    11     disable_annotate_btn: false,
       
    12     disable_search_btn: false,
       
    13     disable_ctrl_f: false
       
    14 };
       
    15 
       
    16 IriSP.Widgets.Controller.prototype.template =
       
    17     '<div class="Ldt-Ctrl">'
       
    18     + '<div class="Ldt-Ctrl-Left">'
       
    19     + '<div class="Ldt-Ctrl-button Ldt-Ctrl-Play Ldt-Ctrl-Play-PlayState Ldt-TraceMe" title="{{l10n.play_pause}}"></div>'
       
    20     + '<div class="Ldt-Ctrl-spacer"></div>'
       
    21     + '{{^disable_annotate_btn}}'
       
    22     + '<div class="Ldt-Ctrl-button Ldt-Ctrl-Annotate Ldt-TraceMe" title="{{l10n.annotate}}"></div>'
       
    23     + '<div class="Ldt-Ctrl-spacer"></div>'
       
    24     + '{{/disable_annotate_btn}}'
       
    25     + '{{^disable_search_btn}}'
       
    26     + '<div class="Ldt-Ctrl-button Ldt-Ctrl-SearchBtn Ldt-TraceMe" title="{{l10n.search}}"></div>'
       
    27     + '<div class="Ldt-Ctrl-spacer"></div>'
       
    28     + '{{/disable_search_btn}}'
       
    29     + '<div class="Ldt-Ctrl-Search">'
       
    30     + '<input class="Ldt-Ctrl-SearchInput Ldt-TraceMe"></input>'
       
    31     + '</div>'
       
    32     + '</div>'
       
    33     + '<div class="Ldt-Ctrl-Right">'
       
    34     + '<div class="Ldt-Ctrl-spacer"></div>'
       
    35     + '<div class="Ldt-Ctrl-Time">'
       
    36     + '<div class="Ldt-Ctrl-Time-Elapsed" title="{{l10n.elapsed_time}}">00:00</div>'
       
    37     + '<div class="Ldt-Ctrl-Time-Separator">/</div>'
       
    38     + '<div class="Ldt-Ctrl-Time-Total" title="{{l10n.total_time}}">00:00</div>'
       
    39     + '</div>'
       
    40     + '<div class="Ldt-Ctrl-spacer"></div>'
       
    41     + '<div class="Ldt-Ctrl-button Ldt-Ctrl-Sound Ldt-Ctrl-Sound-Full Ldt-TraceMe" title="{{l10n.mute_unmute}}"></div>'
       
    42     + '</div>'
       
    43     + '<div class="Ldt-Ctrl-Volume-Control" title="{{l10n.volume_control}}">'
       
    44     + '<div class="Ldt-Ctrl-Volume-Bar"></div>'
       
    45     + '</div>'
       
    46     + '</div>';
       
    47 
       
    48 IriSP.Widgets.Controller.prototype.messages = {
       
    49     en: {
       
    50         play_pause: "Play/Pause",
       
    51         mute_unmute: "Mute/Unmute",
       
    52         play: "Play",
       
    53         pause: "Pause",
       
    54         mute: "Mute",
       
    55         unmute: "Unmute",
       
    56         annotate: "Annotate",
       
    57         search: "Search",
       
    58         elapsed_time: "Elapsed time",
       
    59         total_time: "Total duration",
       
    60         volume: "Volume",
       
    61         volume_control: "Volume control"
       
    62     },
       
    63     fr: {
       
    64         play_pause: "Lecture/Pause",
       
    65         mute_unmute: "Couper/Activer le son",
       
    66         play: "Lecture",
       
    67         pause: "Pause",
       
    68         mute: "Couper le son",
       
    69         unmute: "Activer le son",
       
    70         annotate: "Annoter",
       
    71         search: "Rechercher",
       
    72         elapsed_time: "Temps écoulé",
       
    73         total_time: "Durée totale",
       
    74         volume: "Niveau sonore",
       
    75         volume_control: "Réglage du niveau sonore"
       
    76     }
       
    77 };
       
    78 
       
    79 IriSP.Widgets.Controller.prototype.draw = function() {
       
    80     var _this = this;
       
    81     this.renderTemplate();
       
    82     
       
    83     // Define blocks
       
    84     this.$playButton = this.$.find(".Ldt-Ctrl-Play");
       
    85     this.$searchBlock = this.$.find(".Ldt-Ctrl-Search");
       
    86     this.$searchInput = this.$.find(".Ldt-Ctrl-SearchInput");
       
    87     this.$volumeBar = this.$.find(".Ldt-Ctrl-Volume-Bar");
       
    88     
       
    89     // handle events
       
    90     this.onMediaEvent("play","playButtonUpdater");
       
    91     this.onMediaEvent("pause","playButtonUpdater");
       
    92     this.onMediaEvent("volumechange","volumeUpdater");
       
    93     this.onMediaEvent("timeupdate","timeDisplayUpdater");
       
    94     this.onMediaEvent("loadedmetadata","volumeUpdater");
       
    95     
       
    96     // handle clicks
       
    97     this.$playButton.click(this.functionWrapper("playHandler"));
       
    98     
       
    99     this.$.find(".Ldt-Ctrl-Annotate").click(function() {
       
   100         _this.player.trigger("CreateAnnotation.toggle");
       
   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     // Handle CTRL-F
       
   122     if (!this.disable_ctrl_f) {
       
   123         var _fKey = "F".charCodeAt(0),
       
   124             _lastCtrlFTime = 0;
       
   125         IriSP.jQuery(document).keydown(function(_event) {
       
   126             if (_event.keyCode === _fKey && (_event.ctrlKey || _event.metaKey)) {
       
   127                 var _time = IriSP.jQuery.now();
       
   128                 if (_time - _lastCtrlFTime > 2000) {
       
   129                     _this.searchButtonHandler();
       
   130                 }
       
   131                 _lastCtrlFTime = _time;
       
   132                 return false;
       
   133             }
       
   134         });
       
   135     }
       
   136     
       
   137     // Allow Volume Cursor Dragging
       
   138     this.$volumeBar.slider({
       
   139         slide: function(event, ui) {
       
   140             _this.$volumeBar.attr("title",_this.l10n.volume+': ' + ui.value + '%');
       
   141             _this.media.setVolume(ui.value / 100);
       
   142         },
       
   143         stop: this.functionWrapper("volumeUpdater")
       
   144     });
       
   145 
       
   146     // trigger an IriSP.Player.MouseOver to the widgets that are interested (i.e : sliderWidget)
       
   147     this.$.hover(
       
   148         function() {
       
   149             _this.player.trigger("Player.MouseOver");
       
   150         }, 
       
   151         function() {
       
   152             _this.player.trigger("Player.MouseOut");
       
   153         });
       
   154     
       
   155     this.timeDisplayUpdater(new IriSP.Model.Time(0));
       
   156     
       
   157     var annotations = this.source.getAnnotations();
       
   158     annotations.on("search", function(_text) {
       
   159         _this.$searchInput.val(_text);
       
   160         _this.showSearchBlock();
       
   161     });
       
   162     annotations.on("found", function(_text) {
       
   163         _this.$searchInput.css('background-color','#e1ffe1');
       
   164     });
       
   165     annotations.on("not-found", function(_text) {
       
   166         _this.$searchInput.css('background-color', "#d62e3a");
       
   167     });
       
   168     annotations.on("search-cleared", function() {
       
   169         _this.hideSearchBlock();
       
   170     });
       
   171    
       
   172 };
       
   173 
       
   174 /* Update the elasped time div */
       
   175 IriSP.Widgets.Controller.prototype.timeDisplayUpdater = function(_time) {
       
   176   
       
   177     // we get it at each call because it may change.
       
   178     var _totalTime = this.media.duration;
       
   179     this.$.find(".Ldt-Ctrl-Time-Elapsed").html(_time.toString());
       
   180     this.$.find(".Ldt-Ctrl-Time-Total").html(_totalTime.toString());
       
   181 };
       
   182 
       
   183 /* update the icon of the button - separate function from playHandler
       
   184    because in some cases (for instance, when the user directly clicks on
       
   185    the jwplayer window) we have to change the icon without playing/pausing
       
   186 */
       
   187 IriSP.Widgets.Controller.prototype.playButtonUpdater = function() {
       
   188     if (this.media.getPaused()) {
       
   189     /* the background sprite is changed by adding/removing the correct classes */
       
   190         this.$playButton
       
   191             .attr("title", this.l10n.play)
       
   192             .removeClass("Ldt-Ctrl-Play-PauseState")
       
   193             .addClass("Ldt-Ctrl-Play-PlayState");
       
   194     } else {
       
   195         this.$playButton
       
   196             .attr("title", this.l10n.pause)
       
   197             .removeClass("Ldt-Ctrl-Play-PlayState")
       
   198             .addClass("Ldt-Ctrl-Play-PauseState");
       
   199     }
       
   200 };
       
   201 
       
   202 
       
   203 IriSP.Widgets.Controller.prototype.playHandler = function() {
       
   204     if (this.media.getPaused()) {        
       
   205         this.media.play();
       
   206     } else {
       
   207         this.media.pause();
       
   208     }  
       
   209 };
       
   210 
       
   211 IriSP.Widgets.Controller.prototype.muteHandler = function() {
       
   212     this.media.setMuted(!this.media.getMuted());
       
   213 };
       
   214 
       
   215 IriSP.Widgets.Controller.prototype.volumeUpdater = function() {
       
   216     var _muted = this.media.getMuted(),
       
   217         _vol = this.media.getVolume();
       
   218     if (_vol === false) {
       
   219         _vol = .5;
       
   220     }
       
   221     var _soundCtl = this.$.find(".Ldt-Ctrl-Sound");
       
   222     _soundCtl.removeClass("Ldt-Ctrl-Sound-Mute Ldt-Ctrl-Sound-Half Ldt-Ctrl-Sound-Full");
       
   223     if (_muted) {        
       
   224         _soundCtl.attr("title", this.l10n.unmute)
       
   225             .addClass("Ldt-Ctrl-Sound-Mute");    
       
   226     } else {
       
   227         _soundCtl.attr("title", this.l10n.mute)
       
   228             .addClass(_vol < .5 ? "Ldt-Ctrl-Sound-Half" : "Ldt-Ctrl-Sound-Full" );
       
   229     }
       
   230     this.$volumeBar.slider("value", _muted ? 0 : 100 * _vol);
       
   231 };
       
   232 
       
   233 IriSP.Widgets.Controller.prototype.showSearchBlock = function() {
       
   234     this.$searchBlock.animate({ width:"160px" }, 200);
       
   235     this.$searchInput.css('background-color','#fff');
       
   236     this.$searchInput.focus();
       
   237 };
       
   238 
       
   239 IriSP.Widgets.Controller.prototype.hideSearchBlock = function() {
       
   240     this.$searchBlock.animate( { width: 0 }, 200);
       
   241 };
       
   242 
       
   243 /** react to clicks on the search button */
       
   244 IriSP.Widgets.Controller.prototype.searchButtonHandler = function() {
       
   245     if ( !this.$searchBlock.width() ) {
       
   246         this.showSearchBlock();
       
   247         var _val = this.$searchInput.val();
       
   248         if (_val) {
       
   249             this.source.getAnnotations().search(_val);
       
   250         }
       
   251 	} else {
       
   252         this.hideSearchBlock();
       
   253     }
       
   254 };
       
   255 
       
   256 /** this handler is called whenever the content of the search
       
   257    field changes */
       
   258 IriSP.Widgets.Controller.prototype.searchHandler = function() {
       
   259     if ( !this.$searchBlock.width() ) {
       
   260         this.$searchBlock.css({ width:"160px" });
       
   261         this.$searchInput.css('background-color','#fff');
       
   262     }
       
   263     var _val = this.$searchInput.val();
       
   264     this._positiveMatch = false;
       
   265     
       
   266     // do nothing if the search field is empty, instead of highlighting everything.
       
   267     if (_val !== this.lastSearchValue) {
       
   268         if (_val) {
       
   269             this.source.getAnnotations().search(_val);
       
   270         } else {
       
   271             this.source.getAnnotations().trigger("clear-search");
       
   272             this.$searchInput.css('background-color','');
       
   273         }
       
   274     }
       
   275     this.lastSearchValue = _val;
       
   276 };
       
   277