web/lib/metadataplayer/Renkan.js
changeset 14 960eb22c078c
child 24 701af897de95
equal deleted inserted replaced
13:e47226cc243d 14:960eb22c078c
       
     1 IriSP.Widgets.Renkan = function(player, config) {
       
     2     IriSP.Widgets.Widget.call(this, player, config);
       
     3 };
       
     4 
       
     5 IriSP.Widgets.Renkan.prototype = new IriSP.Widgets.Widget();
       
     6 
       
     7 IriSP.Widgets.Renkan.prototype.defaults = {
       
     8     annotation_regexp: /player\/([a-zA-Z0-9_-]+)\/.*id=([a-zA-Z0-9_-]+)/,
       
     9     tag_regexp: /search=([^&=]+)/,
       
    10     min_duration: 5000
       
    11 };
       
    12 
       
    13 IriSP.Widgets.Renkan.prototype.messages = {
       
    14     "fr": {
       
    15     },
       
    16     "en": {
       
    17     }
       
    18 };
       
    19 
       
    20 IriSP.Widgets.Renkan.prototype.template =
       
    21     '<div class="Ldt-Renkan-Container"><div class="Ldt-Renkan"></div></div>';
       
    22 
       
    23 IriSP.Widgets.Renkan.prototype.draw = function() {
       
    24     this.renderTemplate();
       
    25     var _id = IriSP.Model.getUID();
       
    26     this.$.find(".Ldt-Renkan").attr("id", _id);
       
    27     this.renkan = new Rkns.Renkan({
       
    28         container: _id,
       
    29         editor_mode: false,
       
    30         show_bins: false,
       
    31         show_top_bar: false,
       
    32         force_resize: true,
       
    33         language: IriSP.language
       
    34     });
       
    35     this.node_times = [];
       
    36     var _this = this,
       
    37         _list = this.getWidgetAnnotations();
       
    38     this.renkan.project.on("add:nodes", function(_node) {
       
    39         var _uri = _node.get("uri"),
       
    40             _annmatch = _uri.match(_this.annotation_regexp);
       
    41         if (_annmatch) {
       
    42             var _annotations = _list.filter(function(_ann) {
       
    43                 return _ann.getMedia().id == _annmatch[1] && _ann.id == _annmatch[2];
       
    44             });
       
    45             _annotations.forEach(function(_ann) {
       
    46                 var _duration = _ann.getDuration(),
       
    47                     _preroll = + ( _duration < _this.min_duration ) * ( _this.min_duration / 2);
       
    48                 var _nt = {
       
    49                     uri: _uri,
       
    50                     selected: false,
       
    51                     node: _node,
       
    52                     annotation: _ann,
       
    53                     begin: _ann.begin - _preroll,
       
    54                     end: _ann.end + _preroll
       
    55                 };
       
    56                 _this.node_times.push(_nt);
       
    57                 var _annselected = false,
       
    58                     _nodeselected = false;
       
    59                 _ann.on("select", function() {
       
    60                     _annselected = true;
       
    61                     if (!_nodeselected) {
       
    62                         _node.trigger("select",true);
       
    63                     }
       
    64                 });
       
    65                 _node.on("selected", function() {
       
    66                     _nodeselected = true;
       
    67                     if (!_annselected) {
       
    68                         _ann.trigger("select",true);
       
    69                     }
       
    70                 });
       
    71                 _ann.on("unselect", function() {
       
    72                     _annselected = false;
       
    73                     if (_nodeselected) {
       
    74                         _node.trigger("unselect",true);
       
    75                     }
       
    76                 });
       
    77                 _node.on("unselected", function() {
       
    78                     _nodeselected = false;
       
    79                     _nt.selected = false;
       
    80                     if (_annselected) {
       
    81                         _ann.trigger("unselect",true);
       
    82                     }
       
    83                 });
       
    84                 _node.on("clicked", function() {
       
    85                     _ann.trigger("click");
       
    86                 });
       
    87             });
       
    88         }
       
    89         var _tagmatch = _uri.match(_this.tag_regexp);
       
    90         if (_tagmatch) {
       
    91             _node.on("select", function() {
       
    92                 _this.source.getAnnotations().search(_tagmatch[1]);
       
    93             });
       
    94             _node.on("unselect", function() {
       
    95                 _this.source.getAnnotations().search("");
       
    96             });
       
    97         }
       
    98     });
       
    99     Rkns.jsonIO(this.renkan, {
       
   100         url: this.data
       
   101     });
       
   102     
       
   103     this.onMediaEvent("timeupdate","onTimeupdate");
       
   104     
       
   105     this.$.find(".Rk-Editor").on("click", "a", function() {
       
   106         var href = this.href,
       
   107             times = _this.node_times.filter(function(t) {
       
   108                 return t.uri == href;
       
   109             });
       
   110         if (times.length) {
       
   111             IriSP._(times).each(function(t) {
       
   112                 t.annotation.trigger("click");
       
   113             });
       
   114             return false;
       
   115         }
       
   116     });
       
   117 };
       
   118 
       
   119 IriSP.Widgets.Renkan.prototype.onTimeupdate = function(_time) {
       
   120     IriSP._(this.node_times).each(function(_nt) {
       
   121         if (_nt.begin <= _time && _nt.end >= _time) {
       
   122             if (!_nt.selected) {
       
   123                 _nt.selected = true;
       
   124                 _nt.node.trigger("select");
       
   125             }
       
   126         } else {
       
   127             if (_nt.selected) {
       
   128                 _nt.node.trigger("unselect");
       
   129             }
       
   130         }
       
   131     });
       
   132 };