src/widgets/KnowledgeConcierge.js
changeset 982 cfcbac34d020
parent 964 d7d56ea2d0a6
child 983 97fef7a4b189
equal deleted inserted replaced
981:982d2226771c 982:cfcbac34d020
     7 IriSP.Widgets.KnowledgeConcierge.prototype.defaults = {
     7 IriSP.Widgets.KnowledgeConcierge.prototype.defaults = {
     8     width: 600,
     8     width: 600,
     9     height: 500,
     9     height: 500,
    10     sketch_path: "tmgraph",
    10     sketch_path: "tmgraph",
    11     sketch_files: [ "tmgraph.pde", "physics.pde", "model.pde", "javascript.pde", "menu.pde", "event.pde", "constants.pde", "initialdata.pde"],
    11     sketch_files: [ "tmgraph.pde", "physics.pde", "model.pde", "javascript.pde", "menu.pde", "event.pde", "constants.pde", "initialdata.pde"],
    12     api_root: "/kn-concierge/",
    12     kc_api_root: "/kn-concierge/",
    13     use_word_boundaries: false
    13     related_api_endpoint: "",
       
    14     use_word_boundaries: false,
       
    15     related_data_type: 'json', // SET TO "jsonp" FOR CROSS-DOMAIN OPERATION
       
    16     related_count: 8
    14 }
    17 }
    15 
    18 
    16 IriSP.Widgets.KnowledgeConcierge.prototype.messages = {
    19 IriSP.Widgets.KnowledgeConcierge.prototype.messages = {
    17     "fr": {
    20     "fr": {
       
    21         related_videos: "Vidéos liées",
       
    22         duration_: "Durée :"
    18     },
    23     },
    19     "en": {
    24     "en": {
       
    25         related_videos: "Related Videos",
       
    26         duration_: "Duration:"
    20     }
    27     }
    21 }
    28 }
    22 
    29 
    23 IriSP.Widgets.KnowledgeConcierge.prototype.template =
    30 IriSP.Widgets.KnowledgeConcierge.prototype.template =
    24     '<div class="Ldt-Kc-Slider"></div><canvas class="Ldt-Kc-Canvas" />';
    31     '<div class="Ldt-Kc-Slider"></div><canvas class="Ldt-Kc-Canvas" />'
       
    32     + '<div class="Ldt-Kc-Waiting"></div>'
       
    33     + '<div class="Ldt-Kc-Related"><h2>{{ l10n.related_videos }}</h2>'
       
    34     + '<div class="Ldt-Kc-Related-List"></div></div>';
    25 
    35 
    26 IriSP.Widgets.KnowledgeConcierge.prototype.draw = function() {
    36 IriSP.Widgets.KnowledgeConcierge.prototype.draw = function() {
    27     this.renderTemplate();
    37     this.renderTemplate();
    28     var _canvasHeight = this.height - 16,
    38     var _canvasHeight = this.height - 16,
    29         _canvasWidth = this.width - 2,
    39         _canvasWidth = this.width - 2,
    30         _canvas = this.$.find(".Ldt-Kc-Canvas"),
    40         _canvas = this.$.find(".Ldt-Kc-Canvas"),
    31         _tmpId = IriSP._.uniqueId("Processing-"),
    41         _tmpId = IriSP._.uniqueId("Processing-"),
    32         _slider = this.$.find(".Ldt-Kc-Slider");
    42         _slider = this.$.find(".Ldt-Kc-Slider"),
       
    43         radius = .375 * Math.min(_canvasHeight, _canvasWidth);
    33     _canvas.attr({
    44     _canvas.attr({
    34         width: _canvasWidth,
    45         width: _canvasWidth,
    35         height: _canvasHeight,
    46         height: _canvasHeight,
    36         id: _tmpId
    47         id: _tmpId
    37     }).css({
    48     }).css({
    38         width: _canvasWidth,
    49         width: _canvasWidth,
    39         height: _canvasHeight
    50         height: _canvasHeight
    40     });
    51     });
    41     var _this = this,
    52     var _this = this,
    42         _pjsfiles = IriSP._(this.sketch_files).map(function(_f) { return _this.sketch_path + "/" + _f }),
    53         _pjsfiles = IriSP._(this.sketch_files).map(function(_f) { return _this.sketch_path + "/" + _f }),
    43         _selectedText = "";
    54         _selectedText = "",
       
    55         currentNodesList = "",
       
    56         relatedCache = {},
       
    57         relatedTemplate = '<div class="Ldt-Kc-Related-Item"><a href="{{ widget.video_url_base }}{{ media.iri_id }}"><img src="{{ media.image }}"></a>'
       
    58             + '<h3><a href="{{ widget.video_url_base }}{{ media.iri_id }}">{{ media.title }}</a></h3><p>{{ description }}</p>'
       
    59             + '<p>{{ widget.l10n.duration_ }} <span class="Ldt-Kc-Item-Duration">{{ duration }}</span></p>'
       
    60             + '</a><div class="Ldt-Kc-Clearer"></div></div>';
       
    61             
    44     Processing.loadSketchFromSources(_canvas[0],_pjsfiles);
    62     Processing.loadSketchFromSources(_canvas[0],_pjsfiles);
    45     
    63     
       
    64     function renderRelated(keywords) {
       
    65         _this.$.find(".Ldt-Kc-Waiting").hide();
       
    66         if (relatedCache[keywords].length) {
       
    67             var _html = '<div class="Ldt-Kc-Row">';
       
    68             IriSP._(relatedCache[keywords]).each(function(media, i) {
       
    69                 var _tmpldata = {
       
    70                     widget: _this,
       
    71                     media: media,
       
    72                     description: media.description.replace(/(\n|\r|\r\n)/mg,' ').replace(/(^.{120,140})[\s].+$/m,'$1&hellip;'),
       
    73                     duration: new IriSP.Model.Time(media.duration).toString()
       
    74                 }
       
    75                 _html += Mustache.to_html(relatedTemplate, _tmpldata);
       
    76                 if (i % 2) {
       
    77                     _html += '</div><div class="Ldt-Kc-Row">';
       
    78                 }
       
    79             });
       
    80             _html += '</div>';
       
    81             _this.$.find(".Ldt-Kc-Related-List").html(_html);
       
    82             _this.$.find(".Ldt-Kc-Related").show();
       
    83         } else {
       
    84             _this.$.find(".Ldt-Kc-Related").hide();
       
    85         }
       
    86     }    
       
    87 
    46     function triggerSearch(text) {
    88     function triggerSearch(text) {
    47         if (_selectedText !== text) {
    89         if (_selectedText !== text) {
    48             //console.log("Trigger search for '" + text + "'");
    90             //console.log("Trigger search for '" + text + "'");
    49             _selectedText = text;
    91             _selectedText = text;
    50             _this.player.trigger("search.triggeredSearch", text);
    92             _this.player.trigger("search.triggeredSearch", text);
    53     
    95     
    54     function searchNodes(tags) {
    96     function searchNodes(tags) {
    55         var _tlist = (_this.use_word_boundaries ? IriSP._(tags).map(function(t) { return "\\\\y" + t + "\\\\y" }) : tags),
    97         var _tlist = (_this.use_word_boundaries ? IriSP._(tags).map(function(t) { return "\\\\y" + t + "\\\\y" }) : tags),
    56             _q = "(?i)(" + _tlist.join("|") + ")";
    98             _q = "(?i)(" + _tlist.join("|") + ")";
    57         jQuery.getJSON(
    99         jQuery.getJSON(
    58             _this.api_root + "topics.jsp",
   100             _this.kc_api_root + "topics.jsp",
    59             {
   101             {
    60                 proj: _this.project_id,
   102                 proj: _this.project_id,
    61                 q: _q
   103                 q: _q
    62             },
   104             },
    63             function(data) {
   105             function(data) {
    72                             node.root = true;
   114                             node.root = true;
    73                             node.fix();
   115                             node.fix();
    74                         }
   116                         }
    75                         _fns.countassoc(node.id, node.proj);
   117                         _fns.countassoc(node.id, node.proj);
    76                         if (l > 1) {
   118                         if (l > 1) {
    77                             node.position(Math.floor(200*Math.sin(2 * Math.PI * i / l)),Math.floor(200*Math.cos(2 * Math.PI * i / l)));
   119                             node.position(Math.floor(radius*Math.sin(2 * Math.PI * i / l)),Math.floor(radius*Math.cos(2 * Math.PI * i / l)));
    78                         }
   120                         }
    79                     }
   121                     }
    80                 } else {
   122                 } else {
    81                     console.log("No match found");
   123                     console.log("No match found");
    82                 }
   124                 }
    83             }
   125             }
    84         );
   126         );
    85     }
   127     }
    86     
   128     
       
   129     function listNodes() {
       
   130         var nodes = _pjs.getNodes().values().toArray(),
       
   131             nodetexts = IriSP._(nodes).chain().pluck("name").sortBy().value().join(",");
       
   132         if (currentNodesList !== nodetexts) {
       
   133             if (typeof relatedCache[nodetexts] === "undefined") {
       
   134                 _this.$.find(".Ldt-Kc-Waiting").show();
       
   135                 _this.$.find(".Ldt-Kc-Related").hide();
       
   136                 IriSP.jQuery.ajax({
       
   137                     url: _this.related_api_endpoint,
       
   138                     data: {
       
   139                         format: _this.related_data_type,
       
   140                         keywords: nodetexts
       
   141                     },
       
   142                     dataType: _this.related_data_type,
       
   143                     success: function(data) {
       
   144                         relatedCache[nodetexts] = IriSP._(data.objects)
       
   145                             .chain()
       
   146                             .filter(function(o) {
       
   147                                 return o.iri_id !== _this.media.id;
       
   148                             })
       
   149                             .sortBy(function(o) {
       
   150                                 return - o.score;
       
   151                             })
       
   152                             .first(_this.related_count)
       
   153                             .value();
       
   154                         renderRelated(nodetexts);
       
   155                     }
       
   156                 })
       
   157             } else {
       
   158                 renderRelated(nodetexts);
       
   159             }
       
   160             currentNodesList = nodetexts;
       
   161         }
       
   162     }
       
   163     
    87     function rootNode(id, proj) {
   164     function rootNode(id, proj) {
    88         jQuery.getJSON(
   165         jQuery.getJSON(
    89             _this.api_root + "topic.jsp",
   166             _this.kc_api_root + "topic.jsp",
    90             {
   167             {
    91                 id: id,
   168                 id: id,
    92                 proj: proj
   169                 proj: proj
    93             },
   170             },
    94             function(response) {
   171             function(response) {
   132     
   209     
   133     var _fns = {
   210     var _fns = {
   134         adjacentnodes: function(id, proj, adj, both) {
   211         adjacentnodes: function(id, proj, adj, both) {
   135             //console.log("Function adjacentnodes called with", arguments);
   212             //console.log("Function adjacentnodes called with", arguments);
   136             jQuery.ajax({
   213             jQuery.ajax({
   137                 url: _this.api_root + "associations-bd.jsp",
   214                 url: _this.kc_api_root + "associations-bd.jsp",
   138                 cache: false,
   215                 cache: false,
   139                 data: {
   216                 data: {
   140                     id: id,
   217                     id: id,
   141                     proj: proj,
   218                     proj: proj,
   142                     both: both,
   219                     both: both,
   157                             _pjs.setNodeValue(item.to_id,item.to_proj,  item.to_name,item.to_grp,item.to_abst,item.to_uid);
   234                             _pjs.setNodeValue(item.to_id,item.to_proj,  item.to_name,item.to_grp,item.to_abst,item.to_uid);
   158                             if (item.to_assoc!=null) {
   235                             if (item.to_assoc!=null) {
   159                                 _pjs.setNodeAssoc(item.to_id, item.to_proj, item.to_assoc);
   236                                 _pjs.setNodeAssoc(item.to_id, item.to_proj, item.to_assoc);
   160                             }
   237                             }
   161                         }
   238                         }
       
   239                         listNodes();
   162                         return response;
   240                         return response;
   163                     } else {
   241                     } else {
   164                         console.debug('No such topic.');
   242                         //.debug('No such topic.');
   165                         return null;
   243                         return null;
   166                     }
   244                     }
   167                 }
   245                 }
   168             });
   246             });
   169         },
       
   170         selectnode: function(id, proj) {
       
   171             //console.log("Function selectnode called with", arguments);
       
   172             /* Originally, open dialog with info from
       
   173              * /kn-concierge/topic.jsp?id={{id}}&proj={{proj}}
       
   174              * /kn-concierge/topicContent.jsp?id={{id}}&proj={{proj}}
       
   175              */
       
   176         },
       
   177         selectedge: function(asc_id) {
       
   178             //console.log("Function selectedge called with", arguments);
       
   179             /* /kn-concierge/association.jsp?asc_id={{asc_id}}&proj={{proj}}" */
       
   180         },
       
   181         topicnode: function(id){
       
   182             //console.log("Function topicnode called with", arguments);
       
   183         },
   247         },
   184         setscale: function(scl){
   248         setscale: function(scl){
   185             //console.log("Function setscale called with", arguments);
   249             //console.log("Function setscale called with", arguments);
   186             _slider.slider("value", 10*Math.log(scl));
   250             _slider.slider("value", 10*Math.log(scl));
   187         },
   251         },
   188         group_shapes: function(){
       
   189             //console.log("Function group_shapes");
       
   190         },
       
   191         allbackup: function(){
       
   192         },
       
   193         allretrieve: function(){
       
   194         },
       
   195         new_topic: function(){
       
   196         },
       
   197         pedia: function() {
       
   198         },
       
   199         set_mode: function(){
       
   200         },
       
   201         countassoc: function(id, proj) {
   252         countassoc: function(id, proj) {
   202             //console.log("Fonction countassoc called with", arguments);
   253             //console.log("Fonction countassoc called with", arguments);
   203             jQuery.ajax({
   254             jQuery.ajax({
   204                 url: _this.api_root + "count-assoc.jsp",
   255                 url: _this.kc_api_root + "count-assoc.jsp",
   205                 data: {
   256                 data: {
   206                     id: id,
   257                     id: id,
   207                     proj: proj
   258                     proj: proj
   208                 },
   259                 },
   209                 success: function(response) {
   260                 success: function(response) {
   218                         console.debug('No such topic.');
   269                         console.debug('No such topic.');
   219                     }
   270                     }
   220                 }
   271                 }
   221             });
   272             });
   222         },
   273         },
   223         new_relation: function() {
       
   224         },
       
   225         new_select: function(id, proj) {
   274         new_select: function(id, proj) {
   226             var node = _pjs.findNode(id, proj);
   275             var node = _pjs.findNode(id, proj);
   227             triggerSearch(node.name);
   276             triggerSearch(node.name);
   228             //console.log("Mouse over node named '" + node.name + "'");
   277             //console.log("Mouse over node named '" + node.name + "'");
   229         },
   278         },
   230         startexpand: function() {
   279         username: listNodes
   231             //console.log("Function startexpand()");
   280     }
   232         },
   281     var uselessfuncts = [
   233         endexpand: function() {
   282         "selectnode", "selectedge", "topicnode","group_shapes",
   234             //console.log("Function endexpand()");
   283         "allbackup", "allretrieve", "new_topic", "pedia", "set_mode",
   235         },
   284         "new_relation", "startexpand", "endexpand" //, "username"
   236         username: function() {
   285     ];
   237             //console.log("Function username()");
   286     
   238         }
   287     IriSP._(uselessfuncts).each(function(funcname) {
   239     }
   288         _fns[funcname] = function() {
       
   289 //            console.log("Function", funcname, "called with arguments", arguments);
       
   290         }
       
   291     });
   240     
   292     
   241     this.getWidgetAnnotations().forEach(function(annotation) {
   293     this.getWidgetAnnotations().forEach(function(annotation) {
   242         annotation.on("click", function() {
   294         annotation.on("click", function() {
   243             var _tags = annotation.getTagTexts();
   295             var _tags = annotation.getTagTexts();
   244             if (_tags.length) {
   296             if (_tags.length) {