src/widgets/LatestAnnotation.js
changeset 1054 cfea8c04c8c7
parent 1046 eb77616c245f
child 1055 b45522b1b2af
equal deleted inserted replaced
1053:149a4ea20ea7 1054:cfea8c04c8c7
     5 };
     5 };
     6 
     6 
     7 IriSP.Widgets.LatestAnnotation.prototype = new IriSP.Widgets.Widget();
     7 IriSP.Widgets.LatestAnnotation.prototype = new IriSP.Widgets.Widget();
     8 
     8 
     9 IriSP.Widgets.LatestAnnotation.prototype.defaults = {
     9 IriSP.Widgets.LatestAnnotation.prototype.defaults = {
       
    10     pre_draw_callback: function(){
       
    11         return this.importUsers();
       
    12     },
    10     from_user: false,
    13     from_user: false,
    11     filter_by_segment: false,
    14     filter_by_segment: false,
    12     segments_annotation_type: "chap",
    15     segments_annotation_type: "chap",
    13     hide_without_segment: false,
    16     hide_without_segment: false,
    14     annotation_type: "contribution",
    17     annotation_type: "contribution",
    26     selectable_annotations: false,
    29     selectable_annotations: false,
    27     empty_message: false,
    30     empty_message: false,
    28     starts_hidden: false,
    31     starts_hidden: false,
    29     show_header: false,
    32     show_header: false,
    30     custom_header: false,
    33     custom_header: false,
       
    34     make_name_string_function: function(params){
       
    35         return params.username ? params.username : "Anonymous";
       
    36     }, 
    31 };
    37 };
    32 
    38 
    33 IriSP.Widgets.LatestAnnotation.prototype.messages = {
    39 IriSP.Widgets.LatestAnnotation.prototype.messages = {
    34     fr : {
    40     fr : {
    35         copy_and_edit: "Copier et Editer",
    41         copy_and_edit: "Copier et Editer",
    62     +     "</div>"
    68     +     "</div>"
    63     + "{{#copy_and_edit_button}}<div class='Ldt-LatestAnnotation-CopyEditButton'>{{button_text}}</div>{{/copy_and_edit_button}}"
    69     + "{{#copy_and_edit_button}}<div class='Ldt-LatestAnnotation-CopyEditButton'>{{button_text}}</div>{{/copy_and_edit_button}}"
    64     + "</div>"
    70     + "</div>"
    65 
    71 
    66 
    72 
       
    73 IriSP.Widgets.LatestAnnotation.prototype.importUsers = function(){
       
    74     if (!this.source.users_data){
       
    75         this.usernames = Array();
       
    76         var _this = this,
       
    77             _list = this.getWidgetAnnotations(),
       
    78             usernames_list_string = "";
       
    79         
       
    80         _list.forEach(function(_annotation){
       
    81             if(_this.usernames.indexOf(_annotation.creator) == -1){
       
    82                 _this.usernames.push(_annotation.creator);
       
    83             }
       
    84         });
       
    85         this.usernames.forEach(function(_username){
       
    86             usernames_list_string+=_username+","
       
    87         })
       
    88         usernames_list_string = usernames_list_string.substring(0, usernames_list_string.length - 1);
       
    89         _url = Mustache.to_html(this.api_users_endpoint, {usernames_list_string: encodeURIComponent(usernames_list_string), usernames_list_length: this.usernames.length});
       
    90         return IriSP.jQuery.ajax({
       
    91             async: false,
       
    92             url: _url,
       
    93             type: "GET",
       
    94             success: function(_data) {
       
    95                 _this.source.users_data = _data.objects
       
    96             },
       
    97             error: function(_xhr, _error, _thrown) {
       
    98                 console.log(_xhr)
       
    99                 console.log(_error)
       
   100                 console.log(_thrown)
       
   101             }
       
   102         })
       
   103     }
       
   104     console.log(this.source.users_data)
       
   105 }
       
   106     
    67 IriSP.Widgets.LatestAnnotation.prototype.draw = function(){
   107 IriSP.Widgets.LatestAnnotation.prototype.draw = function(){
    68     var _this = this;
   108     var _this = this;
    69     this.renderTemplate();
   109     this.renderTemplate();
    70     
   110     
    71     this.annotationContainer_$ = this.$.find('.Ldt-LatestAnnotation');
   111     this.annotationContainer_$ = this.$.find('.Ldt-LatestAnnotation');
    73     if (this.selectable_annotations){
   113     if (this.selectable_annotations){
    74         this.onMdpEvent("AnnotationsList.refresh", function(){
   114         this.onMdpEvent("AnnotationsList.refresh", function(){
    75             _this.getWidgetAnnotations().forEach(function(_annotation){
   115             _this.getWidgetAnnotations().forEach(function(_annotation){
    76                 _annotation.off("click");
   116                 _annotation.off("click");
    77                 _annotation.on("click", function(){
   117                 _annotation.on("click", function(){
       
   118                     _users = this.source.users_data.filter(function(_user_data){
       
   119                         return _user_data.username == _annotation.creator
       
   120                     })
       
   121                     if (_users.length == 0){
       
   122                         _user.username = _annotation.creator;
       
   123                     }
       
   124                     else {
       
   125                         _user = _users[0];
       
   126                     }
       
   127                     _user_display_string = _this.make_name_string_function(_user)
    78                     _html = Mustache.to_html(_this.annotationTemplate, {
   128                     _html = Mustache.to_html(_this.annotationTemplate, {
    79                         annotation_created: _annotation.created.toLocaleDateString()+", "+_annotation.created.toLocaleTimeString(),
   129                         annotation_created: _annotation.created.toLocaleDateString()+", "+_annotation.created.toLocaleTimeString(),
    80                         annotation_creator: _annotation.creator,
   130                         annotation_creator: _user_display_string,
    81                         annotation_title: _annotation.title,
   131                         annotation_title: _annotation.title,
    82                         annotation_content: _annotation.description,
   132                         annotation_content: _annotation.description,
    83                         copy_and_edit_button: _this.copy_and_edit_button,
   133                         copy_and_edit_button: _this.copy_and_edit_button,
    84                         button_text: _this.l10n.copy_and_edit,
   134                         button_text: _this.l10n.copy_and_edit,
    85                     });
   135                     });
   180         }
   230         }
   181         _list = _list.sortBy(function(_annotation){
   231         _list = _list.sortBy(function(_annotation){
   182             return _annotation.created;
   232             return _annotation.created;
   183         });
   233         });
   184         
   234         
   185         var _latestAnnotation = false;
   235         var _latestAnnotation = false,
   186         var _html="";
   236             _html="",
       
   237             _user_display_string = "",
       
   238             _user = {};
   187         if (_list.length != 0){
   239         if (_list.length != 0){
   188             _latestAnnotation = _list.pop();
   240             _latestAnnotation = _list.pop();
       
   241             _users = this.source.users_data.filter(function(_user_data){
       
   242                 return _user_data.username == _latestAnnotation.creator
       
   243             })
       
   244             if (_users.length == 0){
       
   245                 _user.username = _latestAnnotation.creator;
       
   246             }
       
   247             else {
       
   248                 _user = _users[0];
       
   249             }
       
   250             _user_display_string = this.make_name_string_function(_user)
   189             _html = Mustache.to_html(this.annotationTemplate, {
   251             _html = Mustache.to_html(this.annotationTemplate, {
   190                 annotation_created: _latestAnnotation.created.toLocaleDateString()+", "+_latestAnnotation.created.toLocaleTimeString(),
   252                 annotation_created: _latestAnnotation.created.toLocaleDateString()+", "+_latestAnnotation.created.toLocaleTimeString(),
   191                 annotation_creator: _latestAnnotation.creator,
   253                 annotation_creator: _user_display_string,
   192                 annotation_title: _latestAnnotation.title,
   254                 annotation_title: _latestAnnotation.title,
   193                 annotation_content: _latestAnnotation.description,
   255                 annotation_content: _latestAnnotation.description,
   194                 copy_and_edit_button: this.copy_and_edit_button,
   256                 copy_and_edit_button: this.copy_and_edit_button,
   195                 button_text: this.l10n.copy_and_edit,
   257                 button_text: this.l10n.copy_and_edit,
   196             });
   258             });