src/widgets/KnowledgeConcierge.js
changeset 984 e034099276f6
parent 983 97fef7a4b189
child 986 f9d51dd4a3fe
equal deleted inserted replaced
983:97fef7a4b189 984:e034099276f6
    18 
    18 
    19 IriSP.Widgets.KnowledgeConcierge.prototype.messages = {
    19 IriSP.Widgets.KnowledgeConcierge.prototype.messages = {
    20     "fr": {
    20     "fr": {
    21         related_videos: "Vidéos liées",
    21         related_videos: "Vidéos liées",
    22         duration_: "Durée :",
    22         duration_: "Durée :",
    23         for_keywords_: "pour le(s) mots-clé(s) :"
    23         for_keywords_: "pour le(s) mots-clé(s) :",
       
    24         no_matching_videos: "Pas de vidéos correspondantes"
    24     },
    25     },
    25     "en": {
    26     "en": {
    26         related_videos: "Related Videos",
    27         related_videos: "Related Videos",
    27         duration_: "Duration:",
    28         duration_: "Duration:",
    28         for_keywords_: "for keyword(s):"
    29         for_keywords_: "for keyword(s):",
       
    30         no_matching_videos: "No matching videos"
    29     }
    31     }
    30 }
    32 }
    31 
    33 
    32 IriSP.Widgets.KnowledgeConcierge.prototype.template =
    34 IriSP.Widgets.KnowledgeConcierge.prototype.template =
    33     '<div class="Ldt-Kc-Slider"></div><canvas class="Ldt-Kc-Canvas" />'
    35     '<div class="Ldt-Kc-Slider"></div><canvas class="Ldt-Kc-Canvas" />'
    34     + '<div class="Ldt-Kc-Waiting"></div>'
       
    35     + '<div class="Ldt-Kc-Related"><h2>{{ l10n.related_videos }}</h2>'
    36     + '<div class="Ldt-Kc-Related"><h2>{{ l10n.related_videos }}</h2>'
    36     + '<h3 class="Ldt-Kc-For-Keywords">{{l10n.for_keywords_}} <span class="Ldt-Kc-Keywords"></span></h3>'
    37     + '<h3 class="Ldt-Kc-For-Keywords">{{l10n.for_keywords_}} <span class="Ldt-Kc-Keywords"></span></h3>'
       
    38     + '<div class="Ldt-Kc-Waiting"></div>'
    37     + '<div class="Ldt-Kc-Related-List"></div></div>';
    39     + '<div class="Ldt-Kc-Related-List"></div></div>';
    38 
    40 
    39 IriSP.Widgets.KnowledgeConcierge.prototype.draw = function() {
    41 IriSP.Widgets.KnowledgeConcierge.prototype.draw = function() {
    40     this.renderTemplate();
    42     this.renderTemplate();
    41     var _canvasHeight = this.height - 16,
    43     var _canvasHeight = this.height - 16,
    56         _pjsfiles = IriSP._(this.sketch_files).map(function(_f) { return _this.sketch_path + "/" + _f }),
    58         _pjsfiles = IriSP._(this.sketch_files).map(function(_f) { return _this.sketch_path + "/" + _f }),
    57         _selectedText = "",
    59         _selectedText = "",
    58         currentNodesList = "",
    60         currentNodesList = "",
    59         relatedCache = {},
    61         relatedCache = {},
    60         relatedRequests = {},
    62         relatedRequests = {},
    61         relatedTemplate = '<div class="Ldt-Kc-Related-Item"><a href="{{ widget.video_url_base }}{{ media.iri_id }}"><img src="{{ media.image }}"></a>'
    63         relatedTemplate = '<div class="Ldt-Kc-Related-Item"><a href="{{ widget.video_url_base }}{{ media.iri_id }}#keyword={{ escaped_keyword }}"><img src="{{ media.image }}"></a>'
    62             + '<h3><a href="{{ widget.video_url_base }}{{ media.iri_id }}">{{ media.title }}</a></h3><p>{{ description }}</p>'
    64             + '<h3><a href="{{ widget.video_url_base }}{{ media.iri_id }}#keyword={{ escaped_keyword }}">{{ media.title }}</a></h3><p>{{ description }}</p>'
    63             + '<p>{{ widget.l10n.duration_ }} <span class="Ldt-Kc-Item-Duration">{{ duration }}</span></p>'
    65             + '<p>{{ widget.l10n.duration_ }} <span class="Ldt-Kc-Item-Duration">{{ duration }}</span></p>'
    64             + '</a><div class="Ldt-Kc-Clearer"></div></div>';
    66             + '</a><div class="Ldt-Kc-Clearer"></div></div>';
    65             
    67             
    66     Processing.loadSketchFromSources(_canvas[0],_pjsfiles);
    68     Processing.loadSketchFromSources(_canvas[0],_pjsfiles);
    67     
    69     
    68     function renderRelated() {
    70     function renderRelated() {
    69         var keywords = currentNodesList;
    71         var keywords = currentNodesList;
       
    72         _this.$.find(".Ldt-Kc-Related").show();
    70         if (typeof relatedCache[keywords] === "undefined") {
    73         if (typeof relatedCache[keywords] === "undefined") {
    71             return;
    74             return;
    72         }
    75         }
    73         _this.$.find(".Ldt-Kc-Waiting").hide();
    76         _this.$.find(".Ldt-Kc-Waiting").hide();
    74         if (relatedCache[keywords].length) {
    77         if (relatedCache[keywords].length) {
    75             _this.$.find(".Ldt-Kc-Keywords").html(keywords.replace(/\,/g,", "));
       
    76             var _html = '<div class="Ldt-Kc-Row">';
    78             var _html = '<div class="Ldt-Kc-Row">';
    77             IriSP._(relatedCache[keywords]).each(function(media, i) {
    79             IriSP._(relatedCache[keywords]).each(function(media, i) {
    78                 var _tmpldata = {
    80                 var _tmpldata = {
    79                     widget: _this,
    81                     widget: _this,
    80                     media: media,
    82                     media: media,
    81                     description: media.description.replace(/(\n|\r|\r\n)/mg,' ').replace(/(^.{120,140})[\s].+$/m,'$1&hellip;'),
    83                     description: media.description.replace(/(\n|\r|\r\n)/mg,' ').replace(/(^.{120,140})[\s].+$/m,'$1&hellip;'),
    82                     duration: new IriSP.Model.Time(media.duration).toString()
    84                     duration: new IriSP.Model.Time(media.duration).toString(),
       
    85                     escaped_keyword: encodeURIComponent(keywords.split(",")[0])
    83                 }
    86                 }
    84                 _html += Mustache.to_html(relatedTemplate, _tmpldata);
    87                 _html += Mustache.to_html(relatedTemplate, _tmpldata);
    85                 if (i % 2) {
    88                 if (i % 2) {
    86                     _html += '</div><div class="Ldt-Kc-Row">';
    89                     _html += '</div><div class="Ldt-Kc-Row">';
    87                 }
    90                 }
    88             });
    91             });
    89             _html += '</div>';
    92             _html += '</div>';
    90             _this.$.find(".Ldt-Kc-Related-List").html(_html);
    93             _this.$.find(".Ldt-Kc-Related-List").html(_html);
    91             _this.$.find(".Ldt-Kc-Related").show();
       
    92         } else {
    94         } else {
    93             _this.$.find(".Ldt-Kc-Related").hide();
    95             _this.$.find(".Ldt-Kc-Related-List").html("<p class='Ldt-Kc-Related-Empty'>" + _this.l10n.no_matching_videos + "</p>");
    94         }
    96         }
    95     }    
    97     }    
    96 
    98 
    97     function triggerSearch(text) {
    99     function triggerSearch(text) {
    98         if (_selectedText !== text) {
   100         if (_selectedText !== text) {
   132         );
   134         );
   133     }
   135     }
   134     
   136     
   135     function showRelated(nodetexts) {
   137     function showRelated(nodetexts) {
   136         currentNodesList = nodetexts;
   138         currentNodesList = nodetexts;
       
   139         _this.$.find(".Ldt-Kc-Related-List").html("");
       
   140         _this.$.find(".Ldt-Kc-Keywords").html(nodetexts.replace(/\,/g,", "));
   137         if (typeof relatedCache[nodetexts] === "undefined") {
   141         if (typeof relatedCache[nodetexts] === "undefined") {
   138             _this.$.find(".Ldt-Kc-Waiting").show();
   142             _this.$.find(".Ldt-Kc-Waiting").show();
   139             _this.$.find(".Ldt-Kc-Related").hide();
       
   140             if (relatedRequests[nodetexts]) {
   143             if (relatedRequests[nodetexts]) {
   141                 return;
   144                 return;
   142             }
   145             }
   143             relatedRequests[nodetexts] = true;
   146             relatedRequests[nodetexts] = true;
   144             IriSP.jQuery.ajax({
   147             IriSP.jQuery.ajax({
   206             }, 1000);
   209             }, 1000);
   207         } else {
   210         } else {
   208             setTimeout(bindJavascript, 1000); 
   211             setTimeout(bindJavascript, 1000); 
   209         }
   212         }
   210     }
   213     }
   211     var currentSelection = null, lockMode = false;
   214     var currentSelection = null;
   212     var _fns = {
   215     var _fns = {
   213         adjacentnodes: function(id, proj, adj, both) {
   216         adjacentnodes: function(id, proj, adj, both) {
   214             jQuery.ajax({
   217             jQuery.ajax({
   215                 url: _this.kc_api_root + "associations-bd.jsp",
   218                 url: _this.kc_api_root + "associations-bd.jsp",
   216                 cache: false,
   219                 cache: false,
   272                 nodetexts = IriSP._(nodes).chain().pluck("name").sortBy().value().join(",");
   275                 nodetexts = IriSP._(nodes).chain().pluck("name").sortBy().value().join(",");
   273             showRelated(nodetexts);
   276             showRelated(nodetexts);
   274         },
   277         },
   275         mousemove: function(selection) {
   278         mousemove: function(selection) {
   276             if (selection !== currentSelection) {
   279             if (selection !== currentSelection) {
   277                 if (selection && !lockMode) {
   280                 if (selection) {
   278                     triggerSearch(selection.name);
   281                     triggerSearch(selection.name);
   279                 }
   282                 }
   280                 currentSelection = selection;
   283                 currentSelection = selection;
   281             }
   284             }
   282         },
   285         },
   283         click: function(selection) {
   286         click: function(selection) {
   284             if (selection) {
   287             if (selection) {
   285                 lockMode = true;
       
   286                 triggerSearch(selection.name);
   288                 triggerSearch(selection.name);
   287                 showRelated(selection.name);
   289                 showRelated(selection.name);
   288             } else {
   290             } else {
   289                 lockMode = false;
   291                 triggerSearch();
   290                 triggerSearch()
       
   291             }
   292             }
   292         }
   293         }
   293     }
   294     }
   294     var uselessfuncts = [
   295     var uselessfuncts = [
   295         "selectnode", "selectedge", "topicnode","group_shapes",
   296         "selectnode", "selectedge", "topicnode","group_shapes",
   318                 searchNodes([tag.title]);
   319                 searchNodes([tag.title]);
   319             }
   320             }
   320         });
   321         });
   321     });
   322     });
   322     
   323     
       
   324     var keywmatch = document.location.hash.match(/keyword=([^#?&]+)/);
       
   325     if (keywmatch) {
       
   326         this.player.on("widgets-loaded", function() {
       
   327             triggerSearch(keywmatch[1]);
       
   328         })
       
   329     }
       
   330     
   323     bindJavascript();
   331     bindJavascript();
   324     
   332     
   325 }
   333 }