src/widgets/KnowledgeConcierge.js
changeset 964 d7d56ea2d0a6
child 982 cfcbac34d020
equal deleted inserted replaced
963:d58d0dcdb640 964:d7d56ea2d0a6
       
     1 IriSP.Widgets.KnowledgeConcierge = function(player, config) {
       
     2     IriSP.Widgets.Widget.call(this, player, config);
       
     3 };
       
     4 
       
     5 IriSP.Widgets.KnowledgeConcierge.prototype = new IriSP.Widgets.Widget();
       
     6 
       
     7 IriSP.Widgets.KnowledgeConcierge.prototype.defaults = {
       
     8     width: 600,
       
     9     height: 500,
       
    10     sketch_path: "tmgraph",
       
    11     sketch_files: [ "tmgraph.pde", "physics.pde", "model.pde", "javascript.pde", "menu.pde", "event.pde", "constants.pde", "initialdata.pde"],
       
    12     api_root: "/kn-concierge/",
       
    13     use_word_boundaries: false
       
    14 }
       
    15 
       
    16 IriSP.Widgets.KnowledgeConcierge.prototype.messages = {
       
    17     "fr": {
       
    18     },
       
    19     "en": {
       
    20     }
       
    21 }
       
    22 
       
    23 IriSP.Widgets.KnowledgeConcierge.prototype.template =
       
    24     '<div class="Ldt-Kc-Slider"></div><canvas class="Ldt-Kc-Canvas" />';
       
    25 
       
    26 IriSP.Widgets.KnowledgeConcierge.prototype.draw = function() {
       
    27     this.renderTemplate();
       
    28     var _canvasHeight = this.height - 16,
       
    29         _canvasWidth = this.width - 2,
       
    30         _canvas = this.$.find(".Ldt-Kc-Canvas"),
       
    31         _tmpId = IriSP._.uniqueId("Processing-"),
       
    32         _slider = this.$.find(".Ldt-Kc-Slider");
       
    33     _canvas.attr({
       
    34         width: _canvasWidth,
       
    35         height: _canvasHeight,
       
    36         id: _tmpId
       
    37     }).css({
       
    38         width: _canvasWidth,
       
    39         height: _canvasHeight
       
    40     });
       
    41     var _this = this,
       
    42         _pjsfiles = IriSP._(this.sketch_files).map(function(_f) { return _this.sketch_path + "/" + _f }),
       
    43         _selectedText = "";
       
    44     Processing.loadSketchFromSources(_canvas[0],_pjsfiles);
       
    45     
       
    46     function triggerSearch(text) {
       
    47         if (_selectedText !== text) {
       
    48             //console.log("Trigger search for '" + text + "'");
       
    49             _selectedText = text;
       
    50             _this.player.trigger("search.triggeredSearch", text);
       
    51         }
       
    52     }
       
    53     
       
    54     function searchNodes(tags) {
       
    55         var _tlist = (_this.use_word_boundaries ? IriSP._(tags).map(function(t) { return "\\\\y" + t + "\\\\y" }) : tags),
       
    56             _q = "(?i)(" + _tlist.join("|") + ")";
       
    57         jQuery.getJSON(
       
    58             _this.api_root + "topics.jsp",
       
    59             {
       
    60                 proj: _this.project_id,
       
    61                 q: _q
       
    62             },
       
    63             function(data) {
       
    64                 if (data && data.items && data.items.length) {
       
    65                     for (var i=0, l=data.items.length; i<l; i++) {
       
    66                         var node = data.items[i];
       
    67                         if (i == 0) {
       
    68                             _pjs.initNode(node.id, node.name, node.grp, node.uid, node.proj);
       
    69                             var node = _pjs.findNode(node.id, node.proj);
       
    70                         } else {
       
    71                             var node = _pjs.newNode(node.id, node.name, node.grp, node.uid, node.proj);
       
    72                             node.root = true;
       
    73                             node.fix();
       
    74                         }
       
    75                         _fns.countassoc(node.id, node.proj);
       
    76                         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)));
       
    78                         }
       
    79                     }
       
    80                 } else {
       
    81                     console.log("No match found");
       
    82                 }
       
    83             }
       
    84         );
       
    85     }
       
    86     
       
    87     function rootNode(id, proj) {
       
    88         jQuery.getJSON(
       
    89             _this.api_root + "topic.jsp",
       
    90             {
       
    91                 id: id,
       
    92                 proj: proj
       
    93             },
       
    94             function(response) {
       
    95                 if (response != null && response.items.length > 0){
       
    96                     item = response.items[0];
       
    97                     _pjs.initNode(item.id, item.name, item.grp, item.uid, item.proj);
       
    98                     _fns.countassoc(item.id, item.proj);
       
    99                 } else {
       
   100                     console.debug('No such topic.');
       
   101                 }
       
   102         });
       
   103     }
       
   104     
       
   105     function bindJavascript() {
       
   106         _pjs = Processing.getInstanceById(_tmpId);
       
   107         if (_pjs && typeof _pjs.bindJavascript === "function") {
       
   108             setTimeout(function() {
       
   109                 _pjs.bindJavascript(_fns);
       
   110                 _pjs.setSize(_canvasWidth,_canvasHeight);
       
   111                 var _edit = false,
       
   112                     _teamMode = true;
       
   113                 _pjs.saveMode("en",false,_teamMode,false,"both",_edit);
       
   114                 rootNode(_this.topic_id, _this.project_id);
       
   115                 _canvas.click(function() {
       
   116                     triggerSearch("")
       
   117                 });
       
   118                 _slider.slider({
       
   119                     min: -20,
       
   120                     max: 20,
       
   121                     value: 0,
       
   122                     range: "min",
       
   123                     slide: function(event, ui) {
       
   124                         _pjs.zoom(Math.exp(ui.value / 10));
       
   125                     }
       
   126                 });
       
   127             }, 1000);
       
   128         } else {
       
   129             setTimeout(bindJavascript, 1000); 
       
   130         }
       
   131     }
       
   132     
       
   133     var _fns = {
       
   134         adjacentnodes: function(id, proj, adj, both) {
       
   135             //console.log("Function adjacentnodes called with", arguments);
       
   136             jQuery.ajax({
       
   137                 url: _this.api_root + "associations-bd.jsp",
       
   138                 cache: false,
       
   139                 data: {
       
   140                     id: id,
       
   141                     proj: proj,
       
   142                     both: both,
       
   143                     adj: adj
       
   144                 },
       
   145                 success: function(response) {
       
   146                     if (response.items.length > 0){
       
   147                         for(i = 0, end = response.items.length; i < end; i++) {
       
   148                             item = response.items[i];
       
   149                             _pjs.addEdge(item.asc_id, item.id, item.from_proj, item.to_id, item.to_proj,
       
   150                                         item.r_name, item.r_from,  item.r_to, item.uid, item.proj);
       
   151                             _pjs.setNodeName( item.id,   item.from_proj,item.name);
       
   152                             _pjs.setNodeValue(item.id,   item.from_proj,item.name,   item.grp,   item.abst,   item.from_uid);
       
   153                             if (item.from_assoc!=null) {
       
   154                                 _pjs.setNodeAssoc(item.id, item.from_proj, item.from_assoc);
       
   155                             }
       
   156                             _pjs.setNodeName( item.to_id,item.to_proj,  item.to_name);
       
   157                             _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) {
       
   159                                 _pjs.setNodeAssoc(item.to_id, item.to_proj, item.to_assoc);
       
   160                             }
       
   161                         }
       
   162                         return response;
       
   163                     } else {
       
   164                         console.debug('No such topic.');
       
   165                         return null;
       
   166                     }
       
   167                 }
       
   168             });
       
   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         },
       
   184         setscale: function(scl){
       
   185             //console.log("Function setscale called with", arguments);
       
   186             _slider.slider("value", 10*Math.log(scl));
       
   187         },
       
   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) {
       
   202             //console.log("Fonction countassoc called with", arguments);
       
   203             jQuery.ajax({
       
   204                 url: _this.api_root + "count-assoc.jsp",
       
   205                 data: {
       
   206                     id: id,
       
   207                     proj: proj
       
   208                 },
       
   209                 success: function(response) {
       
   210                     if (response.items.length > 0){
       
   211                         for(i = 0, end = response.items.length; i < end; i++) {
       
   212                             item = response.items[i];
       
   213                             _pjs.setNodeValue(item.id, item.proj, item.name, item.grp, item.abst);
       
   214                             if (item.assoc!=null) _pjs.setNodeAssoc(item.id, item.proj, item.assoc);
       
   215                             if (item.mass!=null) _pjs.setNodeMass( item.id, item.proj, item.mass);
       
   216                         }
       
   217                     } else {
       
   218                         console.debug('No such topic.');
       
   219                     }
       
   220                 }
       
   221             });
       
   222         },
       
   223         new_relation: function() {
       
   224         },
       
   225         new_select: function(id, proj) {
       
   226             var node = _pjs.findNode(id, proj);
       
   227             triggerSearch(node.name);
       
   228             //console.log("Mouse over node named '" + node.name + "'");
       
   229         },
       
   230         startexpand: function() {
       
   231             //console.log("Function startexpand()");
       
   232         },
       
   233         endexpand: function() {
       
   234             //console.log("Function endexpand()");
       
   235         },
       
   236         username: function() {
       
   237             //console.log("Function username()");
       
   238         }
       
   239     }
       
   240     
       
   241     this.getWidgetAnnotations().forEach(function(annotation) {
       
   242         annotation.on("click", function() {
       
   243             var _tags = annotation.getTagTexts();
       
   244             if (_tags.length) {
       
   245                 searchNodes(_tags);
       
   246             }
       
   247         });
       
   248     });
       
   249     
       
   250     this.source.getTags().forEach(function(tag) {
       
   251         tag.on("click", function() {
       
   252             if (tag.title) {
       
   253                 searchNodes([tag.title]);
       
   254             }
       
   255         });
       
   256     });
       
   257     
       
   258     bindJavascript();
       
   259     
       
   260 }