src/widgets/LatestAnnotation.js
changeset 1054 cfea8c04c8c7
parent 1046 eb77616c245f
child 1055 b45522b1b2af
--- a/src/widgets/LatestAnnotation.js	Fri Sep 18 14:44:36 2015 +0200
+++ b/src/widgets/LatestAnnotation.js	Fri Sep 18 14:48:13 2015 +0200
@@ -7,6 +7,9 @@
 IriSP.Widgets.LatestAnnotation.prototype = new IriSP.Widgets.Widget();
 
 IriSP.Widgets.LatestAnnotation.prototype.defaults = {
+    pre_draw_callback: function(){
+        return this.importUsers();
+    },
     from_user: false,
     filter_by_segment: false,
     segments_annotation_type: "chap",
@@ -28,6 +31,9 @@
     starts_hidden: false,
     show_header: false,
     custom_header: false,
+    make_name_string_function: function(params){
+        return params.username ? params.username : "Anonymous";
+    }, 
 };
 
 IriSP.Widgets.LatestAnnotation.prototype.messages = {
@@ -64,6 +70,40 @@
     + "</div>"
 
 
+IriSP.Widgets.LatestAnnotation.prototype.importUsers = function(){
+    if (!this.source.users_data){
+        this.usernames = Array();
+        var _this = this,
+            _list = this.getWidgetAnnotations(),
+            usernames_list_string = "";
+        
+        _list.forEach(function(_annotation){
+            if(_this.usernames.indexOf(_annotation.creator) == -1){
+                _this.usernames.push(_annotation.creator);
+            }
+        });
+        this.usernames.forEach(function(_username){
+            usernames_list_string+=_username+","
+        })
+        usernames_list_string = usernames_list_string.substring(0, usernames_list_string.length - 1);
+        _url = Mustache.to_html(this.api_users_endpoint, {usernames_list_string: encodeURIComponent(usernames_list_string), usernames_list_length: this.usernames.length});
+        return IriSP.jQuery.ajax({
+            async: false,
+            url: _url,
+            type: "GET",
+            success: function(_data) {
+                _this.source.users_data = _data.objects
+            },
+            error: function(_xhr, _error, _thrown) {
+                console.log(_xhr)
+                console.log(_error)
+                console.log(_thrown)
+            }
+        })
+    }
+    console.log(this.source.users_data)
+}
+    
 IriSP.Widgets.LatestAnnotation.prototype.draw = function(){
     var _this = this;
     this.renderTemplate();
@@ -75,9 +115,19 @@
             _this.getWidgetAnnotations().forEach(function(_annotation){
                 _annotation.off("click");
                 _annotation.on("click", function(){
+                    _users = this.source.users_data.filter(function(_user_data){
+                        return _user_data.username == _annotation.creator
+                    })
+                    if (_users.length == 0){
+                        _user.username = _annotation.creator;
+                    }
+                    else {
+                        _user = _users[0];
+                    }
+                    _user_display_string = _this.make_name_string_function(_user)
                     _html = Mustache.to_html(_this.annotationTemplate, {
                         annotation_created: _annotation.created.toLocaleDateString()+", "+_annotation.created.toLocaleTimeString(),
-                        annotation_creator: _annotation.creator,
+                        annotation_creator: _user_display_string,
                         annotation_title: _annotation.title,
                         annotation_content: _annotation.description,
                         copy_and_edit_button: _this.copy_and_edit_button,
@@ -182,13 +232,25 @@
             return _annotation.created;
         });
         
-        var _latestAnnotation = false;
-        var _html="";
+        var _latestAnnotation = false,
+            _html="",
+            _user_display_string = "",
+            _user = {};
         if (_list.length != 0){
             _latestAnnotation = _list.pop();
+            _users = this.source.users_data.filter(function(_user_data){
+                return _user_data.username == _latestAnnotation.creator
+            })
+            if (_users.length == 0){
+                _user.username = _latestAnnotation.creator;
+            }
+            else {
+                _user = _users[0];
+            }
+            _user_display_string = this.make_name_string_function(_user)
             _html = Mustache.to_html(this.annotationTemplate, {
                 annotation_created: _latestAnnotation.created.toLocaleDateString()+", "+_latestAnnotation.created.toLocaleTimeString(),
-                annotation_creator: _latestAnnotation.creator,
+                annotation_creator: _user_display_string,
                 annotation_title: _latestAnnotation.title,
                 annotation_content: _latestAnnotation.description,
                 copy_and_edit_button: this.copy_and_edit_button,