hdalab/js/gomina.js
changeset 112 e7086d345a7c
parent 110 6455b65fa3b7
child 115 46c0f7a935d1
equal deleted inserted replaced
111:ceb381f5b0c7 112:e7086d345a7c
       
     1 /**
       
     2  * @author raph
       
     3  */
       
     4 
       
     5 var gomNs = {
       
     6     minYear: -5000,
       
     7     maxYear: 2010,
       
     8     tlPixels: 960,
       
     9     tlGamma: 6,
       
    10     heatGamma: 4,
       
    11 }
       
    12 
       
    13 function yearToPx(year) {
       
    14     return gomNs.tlPixels * Math.pow( ( year - gomNs.minYear ) / ( gomNs.maxYear - gomNs.minYear ), gomNs.tlGamma );
       
    15 }
       
    16 
       
    17 function pxToYear(px) {
       
    18     return gomNs.minYear + ( gomNs.maxYear - gomNs.minYear ) * Math.pow( px / gomNs.tlPixels, 1 / gomNs.tlGamma );
       
    19 }
       
    20 
       
    21 
       
    22 function tagInfo(_taglabel) {
       
    23     var _urlParam = { "label": _taglabel };
       
    24     $.getJSON("taginfo.php",
       
    25         _urlParam,
       
    26         function(data) {
       
    27             var _html = '<div id="addtofilter"><h3>'
       
    28                 + data.content_count
       
    29                 + ' contenus pour ce tag</h3><p><a href="#" onclick="filterTag($(\'#tagname\').text()); return false;">Ajouter au filtrage</a></p></div><h2 id="tagname">'
       
    30                 + data.requested_label
       
    31                 + '</h2></div>';
       
    32             if (data.wikipedia_url) {
       
    33                 _html += '<h3><a href="'
       
    34                     + data.wikipedia_url
       
    35                     + '" target="_blank">Wikipédia: '
       
    36                     + decodeURI(data.wikipedia_url.match(/[^\/]+$/)[0]).replace("_"," ")
       
    37                     + '</a></h3>'
       
    38             }
       
    39             if (data.thumbnail) {
       
    40                 _html += '<img id="img_wikipedia" src="'
       
    41                     + data.thumbnail
       
    42                     + '" />';
       
    43             }
       
    44             if (data.abstract) {
       
    45                 _html += '<p>' + _(data.abstract).escape() + '</p>';
       
    46             }
       
    47             if (data.links) {
       
    48                 var _lC = data.requested_label.toLowerCase();
       
    49                 var _t = data.links.map(function(d) {
       
    50                     return (d.subject.toLowerCase() == _lC) ? d.object : d.subject;
       
    51                 });
       
    52                 _t.sort();
       
    53                 _t = _(_t).uniq(true);
       
    54                 _html += '<h3>Tags liés (dbpedia)</h3><ul class="content-tags">'
       
    55                     + _t.map(function(d) {
       
    56                         return '<li class="content-tag-item"><a href="#" onclick="tagInfo($(this).text()); return false;">'
       
    57                             + d
       
    58                             + '</a></li>';
       
    59                     }).join('')
       
    60                     + '</ul>';
       
    61             }
       
    62             $("#taginfo").html(_html);
       
    63         });
       
    64     $.getJSON("filter.php", _urlParam, updateDisplay);
       
    65     if (gomNs.sessiondata.period || gomNs.sessiondata.filters.tag.length != 1 || gomNs.sessiondata.filters.tag[0] != _taglabel) {
       
    66         $("#bandefiltre").addClass("inactif");
       
    67         $("#showsearch").removeClass("actif");
       
    68     } else {
       
    69         $("#bandefiltre").removeClass("inactif");
       
    70         $("#showsearch").addClass("actif");
       
    71     }
       
    72     $("#showlist").removeClass("actif");
       
    73 }
       
    74 
       
    75 function filterTag(_tagLabel) {
       
    76     gomNs.sessiondata.filters.tag.push(_tagLabel);
       
    77     updateFilters();
       
    78     debouncedSaveChanges();
       
    79 }
       
    80 
       
    81 function removeFilter(_index) {
       
    82     gomNs.sessiondata.filters.tag.splice(_index,1);
       
    83     updateFilters();
       
    84     debouncedSaveChanges();
       
    85 }
       
    86 
       
    87 function removePeriod() {
       
    88     $("#dateslider").slider("values",0,0).slider("values",1,gomNs.tlPixels).slideUp();
       
    89     gomNs.sessiondata.filters.period = null;
       
    90     updateFilters();
       
    91     debouncedSaveChanges();
       
    92 }
       
    93 
       
    94 function getUpdates() {
       
    95     var _params = {};
       
    96     if (gomNs.sessiondata.filters.period) {
       
    97         _params.period = gomNs.sessiondata.filters.period.join(',');
       
    98     }
       
    99     if (gomNs.sessiondata.filters.tag.length) {
       
   100         _params.label = gomNs.sessiondata.filters.tag.join(',');
       
   101     }
       
   102     $.getJSON("filter.php", _params, updateDisplay);
       
   103 }
       
   104 
       
   105 var debouncedGetUpdates = _.debounce(getUpdates, 200);
       
   106 
       
   107 function updatePeriod(_n, _val) {
       
   108     var _int = parseInt(_val);
       
   109     if (_int != NaN) {
       
   110         gomNs.sessiondata.filters.period[_n] = _int;
       
   111         updateFilters();
       
   112         debouncedSaveChanges();
       
   113         debouncedHideSlider();
       
   114     }
       
   115 }
       
   116 
       
   117 function changeSpan(_this) {
       
   118     if (!$(_this).children().length) {
       
   119         var _el = document.createElement('input'),
       
   120             _n = _this.id.split('_')[1];
       
   121         _el.value = gomNs.sessiondata.filters.period[_n];
       
   122         _el.style.width = $(_this).width() + 'px';
       
   123         $(_el).focusout(function() {
       
   124             updatePeriod(_n, this.value);
       
   125         }).keypress(function(e) {
       
   126             if (e.keyCode == 13) {
       
   127                 updatePeriod(_n, this.value);
       
   128             }
       
   129         });
       
   130         $(_this).html(_el);
       
   131         _el.focus();
       
   132         _el.select();
       
   133     }
       
   134 }
       
   135 
       
   136 function updateFilters() {
       
   137     var _htmFilters = '',
       
   138         _fl = gomNs.sessiondata.filters.tag.length;
       
   139     if (!gomNs.sessiondata.filters.period && !_fl) {
       
   140         _htmFilters = '<li class="nofilter">Aucun filtre</li>';
       
   141     }
       
   142     if (_fl) {
       
   143         _htmFilters += _(gomNs.sessiondata.filters.tag).map(function(_t, _i) {
       
   144             return '<li class="filtag">'
       
   145                 + _t
       
   146                 + '<a href="#" class="remfil" onclick="removeFilter('
       
   147                 + _i
       
   148                 + '); return false;">[x]</a></li>'; 
       
   149         }).join("");
       
   150     }
       
   151     if (gomNs.sessiondata.filters.period) {
       
   152         _htmFilters += '<li class="filperiod" onclick="$(\'#dateslider\').show(); debouncedHideSlider();"><span class="spyr" id="sp_0" onclick="changeSpan(this);">'
       
   153             + gomNs.sessiondata.filters.period[0]
       
   154             + '</span>-<span class="spyr" id="sp_1" onclick="changeSpan(this);">'
       
   155             + gomNs.sessiondata.filters.period[1]
       
   156             + '</span><a href="#" class="remfil" onclick="removePeriod(); return false;">[x]</a></li>';
       
   157         $("#dateslider").slider("values",0,yearToPx(gomNs.sessiondata.filters.period[0]))
       
   158             .slider("values",1,yearToPx(gomNs.sessiondata.filters.period[1]));
       
   159     } else {
       
   160         _htmFilters += '<li><a href="#" onclick="$(\'#dateslider\').show(); debouncedHideSlider(); return false;">Filtrer par période</a></li>';
       
   161     }
       
   162     $("#filters").html(_htmFilters);
       
   163     $("#bandefiltre").attr("class","");
       
   164     debouncedGetUpdates();
       
   165 }
       
   166 
       
   167 function displayContents(contentdata) {
       
   168     if (contentdata && contentdata.length) {
       
   169         var _htmlCl = '<ul id="contentlist">'
       
   170             + contentdata.map(function(_d) {
       
   171                 return '<li class="content-item"><h3>'
       
   172                     + _d.title
       
   173                     + '</h3><h4><a href="'
       
   174                     + _d.url
       
   175                     + '" target="_blank">'
       
   176                     + _d.url
       
   177                     + '</a></h4><p>'
       
   178                     + _d.description
       
   179                     + '</p><ul class="content-tags">'
       
   180                     + _d.tags.map(function(_t) {
       
   181                         return '<li class="content-tag-item"><a href="#" onclick="tagInfo($(this).text()); return false;"'
       
   182                             + (_t.match ? ' class="tagmatch"' : '')
       
   183                             + '>'
       
   184                             + _t.label
       
   185                             + '</a></li>';
       
   186                     }).join('')
       
   187                     + '</ul><h4>Annotations</h4><div class="content-annotation" contentid="'
       
   188                     + _d.id
       
   189                     + '"><ul><li>'
       
   190                     + (gomNs.sessiondata.annotations[_d.id] && gomNs.sessiondata.annotations[_d.id].texte ? gomNs.sessiondata.annotations[_d.id].texte.replace(/\n/gm,"</p><p>") : 'Annoter ce contenu...')
       
   191                     + '</li></ul></div>'
       
   192                     + ( gomNs.write_allowed
       
   193                         ? '<p><a href="#" class="addremlist" contentid="'
       
   194                             + _d.id
       
   195                             + '">'
       
   196                             + ( gomNs.sessiondata.liste.indexOf(_d.id) == -1 ? 'Ajouter à ma liste' : 'Retirer de ma liste' )
       
   197                             + '</a></p>'
       
   198                         : '' )
       
   199                     + '</li>';
       
   200             }).join('')
       
   201             + '</ul>';
       
   202         $("#contents").html(_htmlCl).scrollTop(0);
       
   203         $("a.addremlist").click(function() {
       
   204             var _id = $(this).attr("contentid"),
       
   205                 _io = gomNs.sessiondata.liste.indexOf(_id);
       
   206             if ( _io == -1) {
       
   207                 gomNs.sessiondata.liste.push(_id);
       
   208                 $(this).html('Retirer de ma liste');
       
   209             } else {
       
   210                 gomNs.sessiondata.liste.splice(_io, 1);
       
   211                 $(this).html('Ajouter à ma liste');
       
   212             }
       
   213             if (gomNs.sessiondata.view == 1) {
       
   214                 showView();
       
   215             }
       
   216             debouncedSaveChanges();
       
   217             return false;
       
   218         })
       
   219         $("div.content-annotation").click(function() {
       
   220             if (gomNs.write_allowed && this.children[0].tagName == 'UL') {
       
   221             var _el = document.createElement('textarea'),
       
   222                 _id = $(this).attr("contentid");
       
   223             _el.innerHTML = (gomNs.sessiondata.annotations[_id] && gomNs.sessiondata.annotations[_id].texte) ? gomNs.sessiondata.annotations[_id].texte : '';
       
   224             $(_el).focusout(function() {
       
   225                 var _id = this.parentNode.attributes.contentid.nodeValue;
       
   226                 if (!gomNs.sessiondata.annotations[_id]) {
       
   227                     gomNs.sessiondata.annotations[_id] = {};
       
   228                 }
       
   229                 gomNs.sessiondata.annotations[_id].texte = _.escape(this.value);
       
   230                 this.parentNode.innerHTML = '<ul><li>' + (this.value.length ? gomNs.sessiondata.annotations[_id].texte.replace(/\n/gm,"</li><li>") : 'Annoter ce contenu...' ) + '</ul>';
       
   231                 debouncedSaveChanges();
       
   232             });
       
   233             $(this).html(_el);
       
   234             _el.focus();
       
   235             _el.select();
       
   236         }
       
   237         });
       
   238     }
       
   239     else {
       
   240         $("#contents").html("");
       
   241     }
       
   242 }
       
   243 
       
   244 function updateDisplay(data) {
       
   245     if (gomNs.dhmPaper) {
       
   246         gomNs.dhmPaper.clear();
       
   247     } else {
       
   248         gomNs.dhmPaper = new Raphael("dateheat");
       
   249     }
       
   250     var _s = (data.count>1?'s':'');
       
   251     $("#contentcount").html('<b>'+data.count+'</b> notice'+_s);
       
   252     var _sl = data.sparkline.length;
       
   253     if (_sl) {
       
   254         var _maxheat = _(data.sparkline).max(function(_d) { return parseInt(_d.score); }).score,
       
   255             _exp = 1 / gomNs.heatGamma,
       
   256             _scale = Math.pow(_maxheat, - _exp);
       
   257         _(data.sparkline).each(function(_d, _i) {
       
   258             var _nxt = (_i == _sl - 1) ? gomNs.maxYear + 1 : data.sparkline[_i + 1].year,
       
   259                 _x1 = yearToPx(_d.year),
       
   260                 _x2 = yearToPx(_nxt),
       
   261                 _heat = _scale * Math.pow(_d.score, _exp);
       
   262             gomNs.dhmPaper.rect(_x1, 0, _x2 - _x1, 20).attr({
       
   263                 "fill" : "rgb(255, 128, 128)",
       
   264                 "opacity" : _heat,
       
   265                 "stroke" : "none"
       
   266             })
       
   267         });
       
   268     }
       
   269     if (data.tags.length) {
       
   270         var _scores = _(data.tags).map(function(_d) { return parseInt(_d.score)}),
       
   271             _maxTag = _(_scores).max(),
       
   272             _minTag = Math.min(_(_scores).min(), _maxTag - 1),
       
   273             _scale = 20 / Math.sqrt(_maxTag - _minTag),
       
   274             _htmlTc = '<ul id="tclist">'
       
   275             + _(data.tags).map(function(_d) {
       
   276                 return '<li style="font-size:'
       
   277                     + parseInt(10 + _scale * Math.sqrt(_d.score - _minTag))
       
   278                     + 'px;"><a href="#" onclick="tagInfo($(this).text()); return false;"'
       
   279                     + (_d.match ? ' class="tagmatch"' : '')
       
   280                     + '>'
       
   281                     + _d.label
       
   282                     + '</a></li>';
       
   283             }).join('')
       
   284             + '</ul>';
       
   285         $("#tagcloud").html(_htmlTc);
       
   286     }
       
   287     else {
       
   288         $("#tagcloud").html("<h4>Pas de mots-clés trouvés</h4>");
       
   289     }
       
   290     displayContents(data.contents);
       
   291     if (gomNs.countries && data.countries) {
       
   292         var _max = Math.max(1, _(data.countries).max(function(_c) { return _c.score}).score);
       
   293         _(data.countries).each(function(_c) {
       
   294             var _cc = gomNs.countries[_c.isocode];
       
   295             if (_cc) {
       
   296                 var _r = parseInt(255 * _c.score / _max),
       
   297                     _b = parseInt( (255 - _r)/2) ;
       
   298                 _(_cc.gPolygons).each(function(_p) {
       
   299                     _p.setOptions({
       
   300                        "fillColor" : "rgb(" + _r + ",0," + _b + ")",
       
   301                     })
       
   302                 } );
       
   303            }
       
   304         });
       
   305     }
       
   306 }
       
   307 
       
   308 function saveChanges() {
       
   309     if (gomNs.sessionid && gomNs.sessionkey) {
       
   310         $.getJSON("sessioninfo.php", {
       
   311             "sessionid" : gomNs.sessionid,
       
   312             "sessionkey" : gomNs.sessionkey,
       
   313             "data" : JSON.stringify(gomNs.sessiondata),
       
   314         });
       
   315     }
       
   316 }
       
   317 
       
   318 var debouncedSaveChanges = _.debounce(saveChanges, 1000);
       
   319 
       
   320 function changeView(nview) {
       
   321     gomNs.sessiondata.view = nview;
       
   322     debouncedSaveChanges();
       
   323     showView();
       
   324 }
       
   325 
       
   326 function showView() {
       
   327     $("#showsearch, #showlist").removeClass("actif");
       
   328     switch(gomNs.sessiondata.view) {
       
   329         case 1:
       
   330             if (gomNs.sessiondata.liste && gomNs.sessiondata.liste.length) {
       
   331                 $("#showlist").addClass("actif");
       
   332                 $("#bandefiltre").hide();
       
   333                 $.getJSON("filter.php", {
       
   334                     contentlist: gomNs.sessiondata.liste.join(',')
       
   335                 }, updateDisplay);
       
   336                 break;
       
   337             } else {
       
   338                 alert("La liste de contenus est vide ! Ajoutez des contenus pour afficher la liste !");
       
   339             }
       
   340         default:
       
   341             $("#showsearch").addClass("actif");
       
   342             $("#bandefiltre").show();
       
   343             updateFilters();
       
   344     }
       
   345 }
       
   346 
       
   347 function getInitialView() {
       
   348     var _urlParam = {};
       
   349     if (document.location.hash) {
       
   350         var _tab = document.location.hash.replace("#","").split("-");
       
   351         _urlParam.sessionid = _tab[0];
       
   352         if (_tab.length > 1) {
       
   353             _urlParam.sessionkey = _tab[1];
       
   354         }
       
   355     }
       
   356     $.getJSON("sessioninfo.php", _urlParam, function(data) {
       
   357         gomNs.sessionid = data.sessionid;
       
   358         if (data.sessionkey) {
       
   359             gomNs.sessionkey = data.sessionkey;
       
   360         }
       
   361         gomNs.write_allowed = data.write_allowed;
       
   362         if (data.write_allowed) {
       
   363             $("#partagerw").show();
       
   364         } else {
       
   365             $("#partagerw").hide();
       
   366         }
       
   367         var _baseUrl = document.location.href.split("#")[0];
       
   368         $("#rourl").html(_baseUrl + "#" + data.sessionid );
       
   369         gomNs.hash = "#" + data.sessionid + (data.sessionkey ? '-' + data.sessionkey : '');
       
   370         document.location.hash = gomNs.hash;
       
   371         $("#rwurl").html(_baseUrl + gomNs.hash);
       
   372         gomNs.sessiondata = JSON.parse(data.data);
       
   373         if (!gomNs.sessiondata.title) {
       
   374             gomNs.sessiondata.title = 'Nouvelle session';
       
   375         }
       
   376         if (!gomNs.sessiondata.view) {
       
   377             gomNs.sessiondata.view = 0;
       
   378             // 0 pour les résultats de recherche, 1 pour la liste
       
   379         }
       
   380         if (!gomNs.sessiondata.filters) {
       
   381             gomNs.sessiondata.filters = {
       
   382                 period : null,
       
   383                 tag : [],
       
   384             }
       
   385         }
       
   386         if (!gomNs.sessiondata.liste) {
       
   387             gomNs.sessiondata.liste = [];
       
   388         }
       
   389         if (!gomNs.sessiondata.annotations) {
       
   390             gomNs.sessiondata.annotations = {};
       
   391         }
       
   392         $("#sessionname").html(gomNs.sessiondata.title + ( data.write_allowed ? '' : '<span class="lectseul"> (lecture seule)</span>' ) );
       
   393         gomNs.hrefinterval = setInterval(function() {
       
   394             if (document.location.hash != gomNs.hash) {
       
   395                 console.log("Changement de hash");
       
   396                 clearInterval(gomNs.hrefinterval);
       
   397                 getInitialView();
       
   398             }
       
   399         }, 500);
       
   400         showView();
       
   401     });
       
   402 }
       
   403 
       
   404 function changeSessionTitle(title) {
       
   405     gomNs.sessiondata.title = _.escape(title);
       
   406     $("#sessionname").html(gomNs.sessiondata.title);
       
   407     debouncedSaveChanges();
       
   408 }
       
   409 
       
   410 var debouncedHideSlider = _.debounce(function() {
       
   411     $("#dateslider").slideUp();
       
   412 }, 2000);
       
   413 
       
   414 $(document).ready(function() {
       
   415     gomNs.map = new google.maps.Map(document.getElementById("map"),
       
   416         {
       
   417             center: new google.maps.LatLng(30, 0),
       
   418             zoom: 1,
       
   419             mapTypeId: google.maps.MapTypeId.SATELLITE
       
   420         });
       
   421     $.getJSON('lib/countries.geo.json', showCountries);
       
   422     $("#dates li").each(function() {
       
   423         $(this).css({
       
   424             "left" : parseInt(yearToPx(parseInt($(this).text()))) + "px"
       
   425         });
       
   426     });
       
   427     $("#apartager").click(function() {
       
   428         var _pu = $("#partageurls");
       
   429         $(this).attr("class",_pu.is(":visible") ? "" : "actif");
       
   430         _pu.slideToggle();
       
   431         return false;
       
   432     })
       
   433     $( "#dateslider" ).slider({
       
   434         range: true,
       
   435         min: 0,
       
   436         max: gomNs.tlPixels,
       
   437         values: [ 0, gomNs.tlPixels ],
       
   438         slide: function( event, ui ) {
       
   439             gomNs.sessiondata.filters.period = ui.values.map(function(_v) {
       
   440                 return parseInt(pxToYear(_v));
       
   441             });
       
   442             updateFilters();
       
   443             debouncedSaveChanges();
       
   444             debouncedHideSlider();
       
   445         }
       
   446     }).hide();
       
   447     var _defLab = $( "#tagsearch" ).val();
       
   448     $( "#tagsearch" ).autocomplete({
       
   449         source: "tagsearch.php",
       
   450         minLength: 2,
       
   451         select: function( event, ui ) {
       
   452             tagInfo(ui.item.label);
       
   453             $(this).val(_defLab);
       
   454             return false;
       
   455         }
       
   456     }).focusin(function() {
       
   457         if ($(this).val() == _defLab) {
       
   458             $(this).val("");
       
   459         }
       
   460     });
       
   461     $("#sessionname").click(function() {
       
   462         if (gomNs.write_allowed && !$(this).children().length) {
       
   463             var _el = document.createElement('input');
       
   464             _el.value = gomNs.sessiondata.title;
       
   465             $(_el).focusout(function() {
       
   466                 changeSessionTitle(this.value);
       
   467             }).keypress(function(e) {
       
   468                 if (e.keyCode == 13) {
       
   469                     changeSessionTitle(this.value);
       
   470                 }
       
   471             });
       
   472             $(this).html(_el);
       
   473             _el.focus();
       
   474             _el.select();
       
   475         }
       
   476     });
       
   477     $("#showlist").click(function() {
       
   478         changeView(1);
       
   479         return false;
       
   480     });
       
   481     $("#affres, #showsearch").click(function() {
       
   482         changeView(0);
       
   483         return false;
       
   484     });
       
   485     getInitialView();
       
   486 });