src/widgets/Markers.js
changeset 1069 2409cb4cebaf
parent 1067 539c9bee5372
child 1072 ac1eacb3aa33
equal deleted inserted replaced
1068:7623f9af9272 1069:2409cb4cebaf
     4 };
     4 };
     5 
     5 
     6 IriSP.Widgets.Markers.prototype = new IriSP.Widgets.Widget();
     6 IriSP.Widgets.Markers.prototype = new IriSP.Widgets.Widget();
     7 
     7 
     8 IriSP.Widgets.Markers.prototype.defaults = {
     8 IriSP.Widgets.Markers.prototype.defaults = {
       
     9     pre_draw_callback: function(){
       
    10         return this.importUsers();
       
    11     },
     9     annotation_type: "markers",
    12     annotation_type: "markers",
    10     line_height: 8,
    13     line_height: 8,
    11     background: "#e0e0e0",
    14     background: "#e0e0e0",
    12     marker_color: "#ff80fc",
    15     marker_color: "#ff80fc",
    13     placeholder_color: "#ffffff",
    16     placeholder_color: "#ffffff",
    30     allow_empty_markers: false,
    33     allow_empty_markers: false,
    31     close_after_send: false,
    34     close_after_send: false,
    32     custom_send_button: false,
    35     custom_send_button: false,
    33     custom_cancel_button: false,
    36     custom_cancel_button: false,
    34     preview_mode: false,
    37     preview_mode: false,
       
    38     filter_per_user: false,
       
    39     api_users_endpoint: "",
       
    40     make_username_string_function: function(params){
       
    41         return params.username ? params.username : "Anonymous";
       
    42     },
       
    43     hide_if_empty: false,
    35 };
    44 };
    36 
    45 
    37 IriSP.Widgets.Markers.prototype.template = 
    46 IriSP.Widgets.Markers.prototype.template = 
    38     '<div class="Ldt-Markers-Display" style="height:{{line_height}}px;">'
    47     '<div class="Ldt-Markers-Display" style="height:{{line_height}}px;">'
    39     +     '<div class="Ldt-Markers-List" style="height:{{line_height}}px; position: relative;"></div>'
    48     +     '<div class="Ldt-Markers-List" style="height:{{line_height}}px; position: relative;"></div>'
    40     +     '<div class="Ldt-Markers-Position"></div>'
    49     +     '<div class="Ldt-Markers-Position" style="height: {{line_height}}px; top: -{{line_height}};"></div>'
    41     + '</div>'
    50     + '</div>'
    42     + '<div class="Ldt-Markers-Inputs">'
    51     + '<div class="Ldt-Markers-Inputs">'
    43     +     '<div class="Ldt-Markers-Screen Ldt-Markers-ScreenMain">'
    52     +     '<div class="Ldt-Markers-Screen Ldt-Markers-ScreenMain">'
    44     +         '<div class="Ldt-Markers-RoundButton Ldt-Markers-CannotCreate" title="{{#preview_mode}}{{l10n.preview_mode_submit}}{{/preview_mode}}{{^preview_mode}}{{l10n.cannot_create}}{{/preview_mode}}">+</div>'
    53     +         '<div class="Ldt-Markers-RoundButton Ldt-Markers-CannotCreate" title="{{#preview_mode}}{{l10n.preview_mode_submit}}{{/preview_mode}}{{^preview_mode}}{{l10n.cannot_create}}{{/preview_mode}}">+</div>'
    45     +         '<div class="Ldt-Markers-RoundButton Ldt-Markers-Create">+</div>'
    54     +         '<div class="Ldt-Markers-RoundButton Ldt-Markers-Create">+</div>'
    46     +         '{{^preview_mode}}<div class="Ldt-Markers-RoundButton Ldt-Markers-Delete">&#10006;</div>{{/preview_mode}}'
    55     +         '{{^preview_mode}}<div class="Ldt-Markers-RoundButton Ldt-Markers-Delete">&#10006;</div>{{/preview_mode}}'
    47     +         '{{#preview_mode}}<div class="Ldt-Markers-RoundButton Ldt-Markers-PreviewDelete" title="{{l10n.preview_mode_delete}}">&#10006;</div>{{/preview_mode}}'
    56     +         '{{#preview_mode}}<div class="Ldt-Markers-RoundButton Ldt-Markers-PreviewDelete" title="{{l10n.preview_mode_delete}}">&#10006;</div>{{/preview_mode}}'
       
    57     +         '{{#filter_per_user}}{{#preview_mode}}<select class="Ldt-Markers-userFilter-dropdown" id="Ldt-Markers-userFilter"></select>{{/preview_mode}}{{/filter_per_user}}'
    48     +         '<div class="Ldt-Markers-Info"></div>'
    58     +         '<div class="Ldt-Markers-Info"></div>'
    49     +     '</div>'
    59     +     '</div>'
    50     +     '<div class="Ldt-Markers-Screen Ldt-Markers-ScreenSending">'  
    60     +     '<div class="Ldt-Markers-Screen Ldt-Markers-ScreenSending">'  
    51     +         '<div class="Ldt-Markers-Screen-InnerBox">{{l10n.wait_while_processing}}</div>'
    61     +         '<div class="Ldt-Markers-Screen-InnerBox">{{l10n.wait_while_processing}}</div>'
    52     +     '</div>'
    62     +     '</div>'
   123         close_widget: "Fermer",
   133         close_widget: "Fermer",
   124         cannot_create: "Impossible de créer un marqueur sur ce timecode"
   134         cannot_create: "Impossible de créer un marqueur sur ce timecode"
   125     }
   135     }
   126 }
   136 }
   127 
   137 
       
   138 IriSP.Widgets.Markers.prototype.importUsers = function(){
       
   139     if (this.filter_per_user && this.preview_mode){
       
   140         this.usernames = Array();
       
   141         if (!this.source.users_data && this.api_users_endpoint){
       
   142             var _this = this,
       
   143                 _list = this.getWidgetAnnotations(),
       
   144                 usernames_list_string = "";
       
   145 
       
   146             _list.forEach(function(_annotation){
       
   147                 if(_this.usernames.indexOf(_annotation.creator) == -1){
       
   148                     _this.usernames.push(_annotation.creator);
       
   149                 }
       
   150             });
       
   151             this.usernames.forEach(function(_username){
       
   152                 usernames_list_string+=_username+","
       
   153             })
       
   154             usernames_list_string = usernames_list_string.substring(0, usernames_list_string.length - 1);
       
   155             _url = Mustache.to_html(this.api_users_endpoint, {usernames_list_string: encodeURIComponent(usernames_list_string), usernames_list_length: this.usernames.length});
       
   156             return IriSP.jQuery.ajax({
       
   157                 async: false,
       
   158                 url: _url,
       
   159                 type: "GET",
       
   160                 success: function(_data) {
       
   161                     _this.source.users_data = _data.objects
       
   162                 },
       
   163                 error: function(_xhr, _error, _thrown) {
       
   164                     console.log(_xhr)
       
   165                     console.log(_error)
       
   166                     console.log(_thrown)
       
   167                 }
       
   168             })
       
   169         }
       
   170     }
       
   171 }
       
   172 
   128 IriSP.Widgets.Markers.prototype.draw = function(){
   173 IriSP.Widgets.Markers.prototype.draw = function(){
   129     var _this = this;
   174     var _this = this;
   130     
       
   131     this.renderTemplate();
   175     this.renderTemplate();
       
   176     if ((!this.filter_per_user) || (!this.preview_mode) || (this.usernames.length <= 1)){
       
   177         this.$.find(".Ldt-Markers-userFilter-dropdown").hide();
       
   178     }
       
   179     else {
       
   180         this.usernames.forEach(function(_user){
       
   181             var _users = _this.source.users_data.filter(function(_user_data){
       
   182                 return _user_data.username == _user;
       
   183             }),
       
   184                _user_data = {};
       
   185             if (_users.length == 0){
       
   186                 _user_data.username = _user;
       
   187             }
       
   188             else{
       
   189                 _user_data = _users[0];
       
   190             }
       
   191             _this.$.find(".Ldt-Markers-userFilter-dropdown").append("<option value='"+_user+"'>"+_this.make_name_string_function(_user_data)+"</option>")
       
   192         }); 
       
   193         this.$.find(".Ldt-Markers-userFilter-dropdown").change(this.functionWrapper("drawMarkers"))
       
   194         this.$.find(".Ldt-Markers-userFilter-dropdown").change(this.functionWrapper("clearSelectedMarker"))
       
   195         
       
   196     }
   132     
   197     
   133     this.markers = this.getWidgetAnnotations().filter(function(_ann) {
   198     this.markers = this.getWidgetAnnotations().filter(function(_ann) {
   134         return ((_ann.getDuration() == 0) || (_ann.begin == _ann.end));
   199         return ((_ann.getDuration() == 0) || (_ann.begin == _ann.end));
   135     });
   200     });
   136     this.drawMarkers();
   201     if (this.hide_if_empty && this.markers.length <= 0){
   137     
   202         this.$.hide();
   138     this.$.find(".Ldt-Markers-Create").click(this.functionWrapper("onCreateClick"));
   203     } 
   139     this.$.find(".Ldt-Markers-Delete").click(this.functionWrapper("onDeleteClick"));
   204     else {
   140     this.$.find(".Ldt-Markers-RoundButton").hide()
   205         this.drawMarkers();
   141     this.updateCreateButtonState(this.media.getCurrentTime())
   206         
   142     this.$.find(".Ldt-Markers-Screen-SubmitDelete").click(this.functionWrapper("sendDelete"));
   207         this.$.find(".Ldt-Markers-Create").click(this.functionWrapper("onCreateClick"));
   143     this.$.find(".Ldt-Markers-Screen-CancelDelete").click(function(){
   208         this.$.find(".Ldt-Markers-Delete").click(this.functionWrapper("onDeleteClick"));
   144         _this.showScreen("Main");
   209         this.$.find(".Ldt-Markers-RoundButton").hide()
   145         _this.cancelEdit();
   210         this.updateCreateButtonState(this.media.getCurrentTime())
   146     })
   211         this.$.find(".Ldt-Markers-Screen-SubmitDelete").click(this.functionWrapper("sendDelete"));
   147     this.showScreen("Main");
   212         this.$.find(".Ldt-Markers-Screen-CancelDelete").click(function(){
   148     this.$.css({
   213             _this.showScreen("Main");
   149         margin: "1px 0",
   214             _this.cancelEdit();
   150         height: this.line_height,
   215         })
   151         background: this.background
   216         this.showScreen("Main");
   152     });
   217         this.$.css({
   153     
   218             margin: "1px 0",
   154     this.$.find(".Ldt-Markers-Close").click(this.functionWrapper("revertToMainScreen"));
   219             background: this.background
   155     
   220         });
   156     this.onMediaEvent("timeupdate", this.functionWrapper("updatePosition"));
   221         
   157     this.onMediaEvent("timeupdate", this.functionWrapper("updateCreateButtonState"));
   222         this.$.find(".Ldt-Markers-Close").click(this.functionWrapper("revertToMainScreen"));
   158     this.onMediaEvent("play", this.functionWrapper("clearSelectedMarker"));
   223         
   159     this.onMdpEvent("Markers.refresh", this.functionWrapper("drawMarkers"));
   224         this.onMediaEvent("timeupdate", this.functionWrapper("updatePosition"));
   160    
   225         this.onMediaEvent("timeupdate", this.functionWrapper("updateCreateButtonState"));
   161     this.newMarkerTimeCode = 0;
   226         this.onMediaEvent("play", this.functionWrapper("clearSelectedMarker"));
   162     this.selectedMarker = false;
   227         this.onMdpEvent("Markers.refresh", this.functionWrapper("drawMarkers"));
       
   228        
       
   229         this.newMarkerTimeCode = 0;
       
   230         this.selectedMarker = false;
       
   231     }
   163 }
   232 }
   164 
   233 
   165 
   234 
   166 IriSP.Widgets.Markers.prototype.updatePosition = function(_time) {    
   235 IriSP.Widgets.Markers.prototype.updatePosition = function(_time) {    
   167     var _x = Math.floor( this.width * _time / this.media.duration);
   236     var _x = Math.floor( this.width * _time / this.media.duration);
   357 }
   426 }
   358 
   427 
   359 IriSP.Widgets.Markers.prototype.drawMarkers = function(){
   428 IriSP.Widgets.Markers.prototype.drawMarkers = function(){
   360     var _this = this,
   429     var _this = this,
   361         _scale = this.width / this.source.getDuration(),
   430         _scale = this.width / this.source.getDuration(),
   362         list_$ = this.$.find('.Ldt-Markers-List');
   431         list_$ = this.$.find('.Ldt-Markers-List'),
       
   432         _displayed_markers = this.markers;
   363 
   433 
   364     this.$.remove("Ldt-Markers-Marker");
   434     this.$.remove("Ldt-Markers-Marker");
   365     list_$.html("");
   435     list_$.html("");
   366     this.markers.forEach(function(_marker){
   436     
       
   437     if (this.filter_per_user && this.usernames.length > 1){
       
   438         var _username = this.$.find(".Ldt-Markers-userFilter-dropdown")[0].options[this.$.find(".Ldt-Markers-userFilter-dropdown")[0].selectedIndex].value;
       
   439         _displayed_markers = _displayed_markers.filter(function(_marker){
       
   440             return _marker.creator == _username;
       
   441         })
       
   442     }
       
   443     
       
   444     _displayed_markers.forEach(function(_marker){
   367         var _left = _marker.begin * _scale -1,
   445         var _left = _marker.begin * _scale -1,
   368             _data = {
   446             _data = {
   369                 left: _left,
   447                 left: _left,
   370                 height: _this.line_height-1,
   448                 height: _this.line_height-1,
   371                 ball_top: (_this.ball_radius*2 > _this.line_height) ? 0 : ((_this.line_height - _this.ball_radius*2)/2)-1,
   449                 ball_top: (_this.ball_radius*2 > _this.line_height) ? 0 : ((_this.line_height - _this.ball_radius*2)/2)-1,
   457         _annotationType = (_annotationTypes.length ? _annotationTypes[0] : new IriSP.Model.AnnotationType(false, _export)); /* If it doesn't already exists, we create it */
   535         _annotationType = (_annotationTypes.length ? _annotationTypes[0] : new IriSP.Model.AnnotationType(false, _export)); /* If it doesn't already exists, we create it */
   458     if (this.selectedMarker){
   536     if (this.selectedMarker){
   459         var _annotation = this.selectedMarker,
   537         var _annotation = this.selectedMarker,
   460             _url = Mustache.to_html(this.api_endpoint_template_edit, {annotation_id: this.selectedMarker ? this.selectedMarker.id : ""});
   538             _url = Mustache.to_html(this.api_endpoint_template_edit, {annotation_id: this.selectedMarker ? this.selectedMarker.id : ""});
   461         _annotation.source = _export
   539         _annotation.source = _export
   462         _annotation.description = this.$.find(".Ldt-Markers-MarkerTextArea").val(), /* Description field */
   540         _annotation.description = this.$.find(".Ldt-Markers-MarkerTextArea").val(); /* Description field */
   463     }
   541     }
   464     else {
   542     else {
   465         var _annotation = new IriSP.Model.Annotation(false, _export),
   543         var _annotation = new IriSP.Model.Annotation(false, _export),
   466             _url = Mustache.to_html(this.api_endpoint_template_create);
   544             _url = Mustache.to_html(this.api_endpoint_template_create);
   467         
   545