web/lib/metadataplayer/Annotation.js
changeset 0 7f8907368bd5
equal deleted inserted replaced
-1:000000000000 0:7f8907368bd5
       
     1 // TODO: Migrate Timeupdate functions to Extract
       
     2 
       
     3 IriSP.Widgets.Annotation = function(player, config) {
       
     4     IriSP.Widgets.Widget.call(this, player, config);
       
     5     this.lastAnnotation = false;
       
     6     this.minimized = this.start_minimized || false;
       
     7     this.bounds = [ 0, 0 ];
       
     8 };
       
     9 
       
    10 IriSP.Widgets.Annotation.prototype = new IriSP.Widgets.Widget();
       
    11 
       
    12 IriSP.Widgets.Annotation.prototype.messages = {
       
    13     fr: {
       
    14         watching: "Je regarde ",
       
    15         on_site: " sur ",
       
    16         tags_: "Mots-clés\u00a0:",
       
    17         description_: "Description\u00a0:",
       
    18         creator_: "Créé par\u00a0:",
       
    19         excerpt_from: "Extrait de\u00a0:",
       
    20         untitled: "Segment sans titre"
       
    21     },
       
    22     en: {
       
    23         watching: "I'm watching ",
       
    24         on_site: " on ",
       
    25         tags_: "Keywords:",
       
    26         description_: "Description:",
       
    27         creator_: "Created by\u00a0:",
       
    28         excerpt_from: "Excerpt from:",
       
    29         untitled: "Untitled segment"
       
    30     }
       
    31 };
       
    32 
       
    33 IriSP.Widgets.Annotation.prototype.template =
       
    34     '{{#show_arrow}}<div class="Ldt-Annotation-Arrow"></div>{{/show_arrow}}'
       
    35     + '<div class="Ldt-Annotation-Widget {{^show_arrow}}Ldt-Annotation-ShowTop{{/show_arrow}}">'
       
    36     + '<div class="Ldt-Annotation-Inner Ldt-Annotation-Empty{{#start_minimized}} Ldt-Annotation-Minimized{{/start_minimized}}">'
       
    37     + '<div class="Ldt-Annotation-HiddenWhenEmpty Ldt-Annotation-MaxMinButton"></div>'
       
    38     + '{{#show_social}}<div class="Ldt-Annotation-Social Ldt-Annotation-HiddenWhenMinimized Ldt-Annotation-HiddenWhenEmpty"></div>{{/show_social}}'
       
    39     + '<h3 class="Ldt-Annotation-HiddenWhenEmpty">{{#show_annotation_type}}<span class="Ldt-Annotation-Type"></span> » {{/show_annotation_type}}<a class="Ldt-Annotation-Title" href="#"></a> <span class="Ldt-Annotation-Time Ldt-Annotation-HiddenWhenMinimized">'
       
    40     + '(<span class="Ldt-Annotation-Begin"></span> - <span class="Ldt-Annotation-End"></span>)</span></h3>'
       
    41     + '<h3 class="Ldt-Annotation-MashupOrigin Ldt-Annotation-HiddenWhenEmpty">{{l10n.excerpt_from}} <span class="Ldt-Annotation-MashupMedia"></span> <span class="Ldt-Annotation-Time Ldt-Annotation-HiddenWhenMinimized">'
       
    42     + '(<span class="Ldt-Annotation-MashupBegin"></span> - <span class="Ldt-Annotation-MashupEnd"></span>)</span></h3>'
       
    43     + '<div class="Ldt-Annotation-Cleared Ldt-Annotation-HiddenWhenMinimized Ldt-Annotation-HiddenWhenEmpty Ldt-Annotation-Creator-Block"><div class="Ldt-Annotation-Label">{{l10n.creator_}}</div>'
       
    44     + '{{#show_creator}}<p class="Ldt-Annotation-Labelled Ldt-Annotation-Creator"></p></div>'
       
    45     + '<div class="Ldt-Annotation-Cleared Ldt-Annotation-HiddenWhenMinimized Ldt-Annotation-HiddenWhenEmpty Ldt-Annotation-Description-Block"><div class="Ldt-Annotation-Label">{{l10n.description_}}</div>{{/show_creator}}'
       
    46     + '<p class="Ldt-Annotation-Labelled Ldt-Annotation-Description"></p></div>'
       
    47     + '<div class="Ldt-Annotation-Tags-Block Ldt-Annotation-HiddenWhenMinimized Ldt-Annotation-HiddenWhenEmpty Ldt-Annotation-Cleared">'
       
    48     + '<div class="Ldt-Annotation-Label">{{l10n.tags_}}</div><ul class="Ldt-Annotation-Labelled Ldt-Annotation-Tags"></ul>'
       
    49     + '</div></div></div></div>';
       
    50 
       
    51 IriSP.Widgets.Annotation.prototype.defaults = {
       
    52     annotation_type : "chap",
       
    53     start_minimized: false,
       
    54     show_arrow : true,
       
    55     show_creator: true,
       
    56     arrow_position: .5,
       
    57     site_name : "Lignes de Temps",
       
    58     search_on_tag_click: true,
       
    59     show_social: true,
       
    60     show_annotation_type: false
       
    61 };
       
    62 
       
    63 IriSP.Widgets.Annotation.prototype.draw = function() {
       
    64     
       
    65     var _this = this,
       
    66         currentAnnotation;
       
    67     
       
    68     function timeupdate(_time) {
       
    69         var _list = _this.getWidgetAnnotationsAtTime();
       
    70         if (!_list.length) {
       
    71             _this.$.find(".Ldt-Annotation-Inner").addClass("Ldt-Annotation-Empty");
       
    72             if (_this.arrow) {
       
    73                 _this.arrow.moveToTime(_time);
       
    74             }
       
    75             _this.bounds = [ _time, _time ];
       
    76             _this.sendBounds();
       
    77         }
       
    78     }
       
    79     
       
    80     function highlightTitleAndDescription() {
       
    81         if (!currentAnnotation) {
       
    82             return;
       
    83         }
       
    84         var title = currentAnnotation.title,
       
    85             description = currentAnnotation.description.replace(/(^\s+|\s+$)/g,''),
       
    86             rx = (currentAnnotation.found ? (_this.source.getAnnotations().regexp || false) : false);
       
    87         _this.$.find(".Ldt-Annotation-Title").html(IriSP.textFieldHtml(title,rx)  || "(" + _this.l10n.untitled + ")");
       
    88         if (description) {
       
    89             _this.$.find(".Ldt-Annotation-Description-Block").removeClass("Ldt-Annotation-EmptyBlock");
       
    90             _this.$.find(".Ldt-Annotation-Description").html(IriSP.textFieldHtml(description,rx));
       
    91         } else {
       
    92             _this.$.find(".Ldt-Annotation-Description-Block").addClass("Ldt-Annotation-EmptyBlock");
       
    93         }
       
    94     }
       
    95     
       
    96     function drawAnnotation(_annotation) {
       
    97         currentAnnotation = _annotation;
       
    98         var _url = (typeof _annotation.url !== "undefined" 
       
    99                 ? _annotation.url
       
   100                 : (document.location.href.replace(/#.*$/,'') + '#id='  + _annotation.id)),
       
   101             _text = _this.l10n.watching + _annotation.title + (_this.site_name ? _this.l10n.on_site + _this.site_name : ''),
       
   102             _tags = _annotation.getTags(),
       
   103             _tagblock = _this.$.find(".Ldt-Annotation-Tags");
       
   104         _tagblock.empty();
       
   105         if (_tags.length) {
       
   106             _this.$.find(".Ldt-Annotation-Tags-Block").removeClass("Ldt-Annotation-EmptyBlock");
       
   107             _tags.forEach(function(_tag) {
       
   108                 var _trimmedTitle =  _tag.title.replace(/(^\s+|\s+$)/g,'');
       
   109                 if (_trimmedTitle) {
       
   110                     var _el = IriSP.jQuery('<li class="Ldt-Annotation-TagLabel"></li>').append(IriSP.jQuery('<span>').text(_trimmedTitle));
       
   111                     _el.click(function() {
       
   112                         if (_this.search_on_tag_click) {
       
   113                             _this.source.getAnnotations().search(_trimmedTitle);
       
   114                         }
       
   115                         _tag.trigger("click");
       
   116                     });
       
   117                     _tagblock.append(_el);
       
   118                 }
       
   119             });
       
   120         } else {
       
   121             _this.$.find(".Ldt-Annotation-Tags-Block").addClass("Ldt-Annotation-EmptyBlock");
       
   122         }
       
   123         highlightTitleAndDescription();
       
   124         if (_this.show_creator) {
       
   125              _this.$.find(".Ldt-Annotation-Creator").text(_annotation.creator);
       
   126         }
       
   127         if (_this.show_annotation_type) {
       
   128             _this.$.find(".Ldt-Annotation-Type").text(_annotation.getAnnotationType().title);
       
   129         }
       
   130         _this.$.find(".Ldt-Annotation-Begin").text(_annotation.begin.toString());
       
   131         _this.$.find(".Ldt-Annotation-End").text(_annotation.end.toString());
       
   132         if (_annotation.elementType === "mashedAnnotation") {
       
   133             _this.$.find('.Ldt-Annotation-Inner').addClass("Ldt-Annotation-isMashup");
       
   134             _this.$.find(".Ldt-Annotation-MashupMedia").text(_annotation.getMedia().title);
       
   135             _this.$.find(".Ldt-Annotation-MashupBegin").text(_annotation.annotation.begin.toString());
       
   136             _this.$.find(".Ldt-Annotation-MashupEnd").text(_annotation.annotation.end.toString());
       
   137         } else {
       
   138             _this.$.find('.Ldt-Annotation-Inner').removeClass("Ldt-Annotation-isMashup");
       
   139         }
       
   140         if (typeof _this.socialWidget !== "undefined") {
       
   141             _this.socialWidget.updateUrls(_url, _text);
       
   142         } else {
       
   143             setTimeout(function() {
       
   144                 if (typeof _this.socialWidget !== "undefined") {
       
   145                     _this.socialWidget.updateUrls(_url, _text);
       
   146                 }
       
   147             },800);
       
   148         }
       
   149         _this.$.find(".Ldt-Annotation-Inner").removeClass("Ldt-Annotation-Empty");
       
   150         _this.bounds = [ _annotation.begin, _annotation.end ];
       
   151         if (_this.arrow) {
       
   152             _this.arrow.moveToTime((1 - _this.arrow_position) * _annotation.begin + _this.arrow_position * _annotation.end);
       
   153         }
       
   154         _this.sendBounds();
       
   155     }
       
   156     
       
   157     this.renderTemplate();
       
   158     
       
   159     this.$.find(".Ldt-Annotation-Title").click(function() {
       
   160         if (currentAnnotation) {
       
   161             _this.media.setCurrentTime(currentAnnotation.begin);
       
   162         }
       
   163         return false;
       
   164     });
       
   165     
       
   166     if (this.show_social) {
       
   167         this.insertSubwidget(this.$.find(".Ldt-Annotation-Social"), { type: "Social" }, "socialWidget");
       
   168     }
       
   169     
       
   170     if (this.show_arrow) {
       
   171         this.insertSubwidget(this.$.find(".Ldt-Annotation-Arrow"), { type: "Arrow", width: this.width }, "arrow");
       
   172     }
       
   173     this.onMediaEvent("timeupdate",timeupdate);
       
   174     this.onMdpEvent("Annotation.hide","hide");
       
   175     this.onMdpEvent("Annotation.show","show");
       
   176     this.onMdpEvent("Annotation.minimize","minimize");
       
   177     this.onMdpEvent("Annotation.maximize","maximize");
       
   178     this.onMdpEvent("Annotation.getBounds","sendBounds");
       
   179     this.$.find(".Ldt-Annotation-MaxMinButton").click(this.functionWrapper("toggleSize"));
       
   180     this.getWidgetAnnotations().forEach(function(_a) {
       
   181         _a.on("enter", function() {
       
   182             drawAnnotation(_a);
       
   183         });
       
   184     });
       
   185     this.source.getAnnotations().on("found", highlightTitleAndDescription);
       
   186     this.source.getAnnotations().on("not-found", highlightTitleAndDescription);
       
   187     this.source.getAnnotations().on("search-cleared", highlightTitleAndDescription);
       
   188     IriSP.attachDndData(this.$.find("h3"), function() {
       
   189     	return {
       
   190 	    	title: currentAnnotation.title,
       
   191 	    	description: currentAnnotation.description,
       
   192 	    	image: currentAnnotation.thumbnail,
       
   193 	    	uri: (typeof currentAnnotation.url !== "undefined" 
       
   194 	            ? currentAnnotation.url
       
   195 	            : (document.location.href.replace(/#.*$/,'') + '#id='  + currentAnnotation.id))
       
   196 	    };
       
   197     });
       
   198 };
       
   199 
       
   200 IriSP.Widgets.Annotation.prototype.sendBounds = function() {
       
   201     this.player.trigger("Annotation.boundsChanged",this.bounds);
       
   202 };
       
   203 
       
   204 IriSP.Widgets.Annotation.prototype.drawAnnotation = function(_annotation) {
       
   205     this.lastAnnotation = _annotation.id;
       
   206 
       
   207 };
       
   208 
       
   209 IriSP.Widgets.Annotation.prototype.hide = function() {
       
   210     this.$.slideUp();
       
   211 };
       
   212 
       
   213 IriSP.Widgets.Annotation.prototype.show = function() {
       
   214     this.$.slideDown();
       
   215 };
       
   216 
       
   217 IriSP.Widgets.Annotation.prototype.toggleSize = function() {
       
   218     if (this.minimized) {
       
   219         this.maximize();
       
   220     } else {
       
   221         this.minimize();
       
   222     }
       
   223 };
       
   224 
       
   225 IriSP.Widgets.Annotation.prototype.minimize = function() {
       
   226     this.minimized = true;
       
   227     this.$.find('.Ldt-Annotation-Inner').addClass("Ldt-Annotation-Minimized");
       
   228 };
       
   229 
       
   230 IriSP.Widgets.Annotation.prototype.maximize = function() {
       
   231     this.minimized = false;
       
   232     this.$.find('.Ldt-Annotation-Inner').removeClass("Ldt-Annotation-Minimized");
       
   233 };