src/js/widgets/annotationsWidget.js
branchnew-model
changeset 876 03967b6ada7c
parent 875 43629caa77bc
child 880 4c7b33bf2795
equal deleted inserted replaced
875:43629caa77bc 876:03967b6ada7c
     1 /* Internationalization for this widget */
       
     2 
       
     3 IriSP.i18n.addMessages(
       
     4     {
       
     5         "fr": {
       
     6             "keywords": "Mots-clés"
       
     7         },
       
     8         "en": {
       
     9             "keywords": "Keywords"
       
    10         }
       
    11     }
       
    12 );
       
    13 
       
    14 IriSP.AnnotationsWidget = function(Popcorn, config, Serializer) {
       
    15   IriSP.Widget.call(this, Popcorn, config, Serializer);
       
    16   /* flag used when we're creating an annotation */
       
    17   this._hidden = false;
       
    18 };
       
    19 
       
    20 
       
    21 IriSP.AnnotationsWidget.prototype = new IriSP.Widget();
       
    22 
       
    23 IriSP.AnnotationsWidget.prototype.clear = function() {
       
    24     this.selector.find(".Ldt-SaTitle").text("");
       
    25     this.selector.find(".Ldt-SaDescription").text("");
       
    26     this.selector.find(".Ldt-SaKeywordText").text("");
       
    27 };
       
    28 
       
    29 IriSP.AnnotationsWidget.prototype.displayAnnotation = function(annotation) {       
       
    30     var title = annotation.content.title;
       
    31     var description = annotation.content.description;
       
    32     var keywords =  "";
       
    33     var begin = +annotation.begin / 1000;
       
    34     var end = +annotation.end / 1000;
       
    35     var duration = this.getDuration();
       
    36     var tags = "";
       
    37     
       
    38     var title_templ = "{{title}} - ( {{begin}} - {{end}} )";
       
    39     var endstr = Mustache.to_html(title_templ, {title: title, begin: IriSP.secondsToTime(begin), end: IriSP.secondsToTime(end)});
       
    40 
       
    41     this.selector.find(".Ldt-SaTitle").text(endstr);
       
    42     this.selector.find(".Ldt-SaDescription").text(description);
       
    43     
       
    44     
       
    45     if (!IriSP.null_or_undefined(annotation.tags) && !IriSP.null_or_undefined(this._serializer._data.tags)) {
       
    46       /* save the tag id and keywords in a unique structure */
       
    47       var tag_list = {};
       
    48       for (var i = 0; i < this._serializer._data.tags.length; i++) {
       
    49         var id = this._serializer._data.tags[i]["id"];
       
    50         var keyword = IriSP.get_aliased(this._serializer._data.tags[i]["meta"], ["dc:title", "title"]);
       
    51 
       
    52         tag_list[id] = keyword;
       
    53       }
       
    54 
       
    55       /* then browse the list of defined tags for the current annotation */
       
    56       for (var i = 0; i < annotation.tags.length; i++) {
       
    57         if (tag_list.hasOwnProperty(annotation.tags[i]["id-ref"]))
       
    58           tags += tag_list[annotation.tags[i]["id-ref"]] + ", ";
       
    59       }
       
    60     }
       
    61     
       
    62     tags = IriSP.i18n.getMessage("keywords") + ": " + tags.slice(0, tags.length - 2);
       
    63     
       
    64     this.selector.find(".Ldt-SaKeywords").text(tags);
       
    65     
       
    66     // update sharing buttons
       
    67     var url = document.location.href + "#id=" + annotation.id;
       
    68     this.selector.find(".Ldt-fbShare").attr("href", IriSP.mkFbUrl(url, this.share_text));
       
    69     this.selector.find(".Ldt-TwShare").attr("href", IriSP.mkTweetUrl(url, this.share_text));
       
    70     this.selector.find(".Ldt-GplusShare").attr("href", IriSP.mkGplusUrl(url, this.share_text));
       
    71 };
       
    72 
       
    73 IriSP.AnnotationsWidget.prototype.clearWidget = function() {   
       
    74     /* retract the pane between two annotations */
       
    75     this.selector.find(".Ldt-SaTitle").text("");
       
    76     this.selector.find(".Ldt-SaDescription").text("");
       
    77     this.selector.find(".Ldt-SaKeywordText").html("");
       
    78     this.selector.find(".Ldt-ShowAnnotation").slideUp();
       
    79 };
       
    80 
       
    81 IriSP.AnnotationsWidget.prototype.draw = function() {
       
    82   var _this = this;
       
    83 
       
    84   var annotationMarkup = IriSP.templToHTML(IriSP.annotationWidget_template);
       
    85 	this.selector.append(annotationMarkup);
       
    86 
       
    87   this._Popcorn.listen("IriSP.AnnotationsWidget.show", 
       
    88                         IriSP.wrap(this, this.show));
       
    89   this._Popcorn.listen("IriSP.AnnotationsWidget.hide", 
       
    90                         IriSP.wrap(this, this.hide));
       
    91  
       
    92   var legal_ids = [];
       
    93   if (typeof(this._serializer.getChapitrage()) !== "undefined")
       
    94     legal_ids.push(this._serializer.getChapitrage());
       
    95   else 
       
    96     legal_ids = this._serializer.getNonTweetIds();
       
    97   
       
    98   var annotations = this._serializer._data.annotations;
       
    99   var i;
       
   100   
       
   101 	for (i in annotations) {    
       
   102     var annotation = annotations[i];
       
   103     var begin = Math.round((+ annotation.begin) / 1000);
       
   104     var end = Math.round((+ annotation.end) / 1000);
       
   105 
       
   106     if (typeof(annotation.meta) !== "undefined" && typeof(annotation.meta["id-ref"]) !== "undefined"
       
   107           && !IriSP.underscore.include(legal_ids, annotation.meta["id-ref"])) {
       
   108         continue;
       
   109     }
       
   110 
       
   111 
       
   112     var conf = {start: begin, end: end, 
       
   113                 onStart: 
       
   114                        function(annotation) { 
       
   115                         return function() { 
       
   116                             _this.displayAnnotation(annotation); 
       
   117                           
       
   118                         } }(annotation),
       
   119                 onEnd: 
       
   120                        function() { _this.clearWidget.call(_this); }
       
   121                 };
       
   122     this._Popcorn = this._Popcorn.code(conf);                                             
       
   123   }
       
   124 
       
   125 };
       
   126 
       
   127 IriSP.AnnotationsWidget.prototype.hide = function() {
       
   128   if (this._hidden == false) {
       
   129     this.selector.hide();
       
   130     this._hidden = true;
       
   131   }
       
   132 };
       
   133 
       
   134 IriSP.AnnotationsWidget.prototype.show = function() {
       
   135   if (this._hidden == true) {
       
   136     this.selector.show();
       
   137     this._hidden = false;
       
   138   }
       
   139 };