hdalab/js/gomina.js
changeset 127 8642f1fb6499
parent 124 f937ccc6c144
child 128 21c31c3c317b
equal deleted inserted replaced
124:f937ccc6c144 127:8642f1fb6499
     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     displayedDates: [-5000,0,500,1000,1200,1400,1600,1700,1750,1800,1850,1900,1950,2010],
       
    12     mappingLibrary: 'leaflet',
       
    13 }
       
    14 
       
    15 function yearToPx(year) {
       
    16     return gomNs.tlPixels * Math.pow( ( year - gomNs.minYear ) / ( gomNs.maxYear - gomNs.minYear ), gomNs.tlGamma );
       
    17 }
       
    18 
       
    19 function pxToYear(px) {
       
    20     return gomNs.minYear + ( gomNs.maxYear - gomNs.minYear ) * Math.pow( px / gomNs.tlPixels, 1 / gomNs.tlGamma );
       
    21 }
       
    22 
       
    23 
       
    24 function tagInfo(_taglabel) {
       
    25     var _urlParam = { "label": _taglabel };
       
    26     $.getJSON("taginfo.php",
       
    27         _urlParam,
       
    28         function(data) {
       
    29             $("#tagsearch").val(data.requested_label).removeClass("grise");
       
    30             $("#tagcount").html(data.content_count
       
    31                 + ' contenu' + (data.content_count > 1 ? 's' : '') + ' pour ce tag');
       
    32             var _html = '';
       
    33             if (data.wikipedia_url) {
       
    34                 _html += '<h3><a href="'
       
    35                     + data.wikipedia_url
       
    36                     + '" target="_blank">Wikipédia: '
       
    37                     + decodeURI(data.wikipedia_url.match(/[^\/]+$/)[0]).replace("_"," ")
       
    38                     + '</a></h3>'
       
    39             }
       
    40             if (data.thumbnail) {
       
    41                 _html += '<img id="img_wikipedia" src="'
       
    42                     + data.thumbnail
       
    43                     + '" />';
       
    44             }
       
    45             if (data.abstract) {
       
    46                 _html += '<p>' + _(data.abstract).escape().replace(/(^.{0,240})([\s]|$)(.*)/,'$1&hellip;') + '</p>';
       
    47             }
       
    48 /*            if (data.links) {
       
    49                 var _lC = data.requested_label.toLowerCase();
       
    50                 var _t = data.links.map(function(d) {
       
    51                     return (d.subject.toLowerCase() == _lC) ? d.object : d.subject;
       
    52                 });
       
    53                 _t.sort();
       
    54                 _t = _(_t).uniq(true);
       
    55                 _html += '<h3>Tags liés (dbpedia)</h3><ul class="content-tags">'
       
    56                     + _t.map(function(d) {
       
    57                         return '<li class="content-tag-item"><a href="#" onclick="tagInfo($(this).text()); return false;">'
       
    58                             + d
       
    59                             + '</a></li>';
       
    60                     }).join('')
       
    61                     + '</ul>';
       
    62             } */
       
    63             $("#tagdata").html(_html);
       
    64         });
       
    65     addFilter('tag', _taglabel);
       
    66     $("#showlist").removeClass("actif");
       
    67 }
       
    68 
       
    69 function addFilter(_type, _label) {
       
    70     var _curView = gomNs.sessiondata.views[gomNs.sessiondata.view];
       
    71     if (_curView.type == 'filter') {
       
    72         _curView[_type].push(_label);
       
    73         console.log(_curView[_type]);
       
    74         updateFilters();
       
    75         debouncedSaveChanges();
       
    76     }
       
    77 }
       
    78 
       
    79 function removeFilter(_type, _index) {
       
    80     var _curView = gomNs.sessiondata.views[gomNs.sessiondata.view];
       
    81     if (_curView.type == 'filter') {
       
    82         _curView[_type].splice(_index,1);
       
    83         updateFilters();
       
    84         debouncedSaveChanges();
       
    85     }
       
    86 }
       
    87 
       
    88 function getUpdates() {
       
    89     var _params = {},
       
    90         _curView = gomNs.sessiondata.views[gomNs.sessiondata.view];
       
    91     if (_curView.type == 'filter') {
       
    92         if (_curView.period && !(_curView.period[0] == gomNs.minYear && _curView.period[1] == gomNs.maxYear)) {
       
    93             _params.period = _curView.period.join(',');
       
    94         }
       
    95         if (_curView.tag.length) {
       
    96             _params.label = _curView.tag.join(',');
       
    97         }
       
    98         if (_curView.country.length) {
       
    99             _params.country = _curView.country.join(',');
       
   100         }
       
   101         $.getJSON("filter.php", _params, updateDisplay);
       
   102     }
       
   103 }
       
   104 
       
   105 var debouncedGetUpdates = _.debounce(getUpdates, 300);
       
   106 
       
   107 function updatePeriod(_n, _val) {
       
   108     var _curView = gomNs.sessiondata.views[gomNs.sessiondata.view];
       
   109     if (_curView.type == 'filter') {
       
   110         var _int = parseInt(_val);
       
   111         if (_int != NaN) {
       
   112             if ((_n == 1 && _val >= _curView.period[0]) || (_n == 0 && _val <= _curView.period[1])) {
       
   113                 _curView.period[_n] = _int;
       
   114             }
       
   115             updateFilters();
       
   116             debouncedSaveChanges();
       
   117         }
       
   118     }
       
   119 }
       
   120 
       
   121 function changeSpan(_this) {
       
   122     var _curView = gomNs.sessiondata.views[gomNs.sessiondata.view];
       
   123     if (_curView.type == 'filter') {
       
   124         if (!$(_this).children().length) {
       
   125             var _el = document.createElement('input'),
       
   126                 _n = _this.id.split('_')[1];
       
   127             _el.value = _curView.period[_n];
       
   128             _el.style.width = $(_this).width() + 'px';
       
   129             $(_el).focusout(function() {
       
   130                 updatePeriod(_n, this.value);
       
   131             }).keypress(function(e) {
       
   132                 if (e.keyCode == 13) {
       
   133                     updatePeriod(_n, this.value);
       
   134                 }
       
   135             });
       
   136             $(_this).html(_el);
       
   137             _el.focus();
       
   138             _el.select();
       
   139         }
       
   140     }
       
   141 }
       
   142 
       
   143 function updateFilters() {
       
   144     var _curView = gomNs.sessiondata.views[gomNs.sessiondata.view];
       
   145     if (_curView.type != 'filter') {
       
   146         return;
       
   147     }
       
   148     var _htmFilters = '',
       
   149         _fl = _curView.tag.length,
       
   150         _cl = _curView.country.length;
       
   151     if (!_curView.period && !_fl && !_cl) {
       
   152         _htmFilters = '<li class="nofilter">Aucun filtre</li>';
       
   153     }
       
   154     if (_curView.period) {
       
   155         _htmFilters += '<li class="filperiod">Période&nbsp;: <span class="spyr" id="sp_0" onclick="changeSpan(this);">'
       
   156             + _curView.period[0]
       
   157             + '</span> à <span class="spyr" id="sp_1" onclick="changeSpan(this);">'
       
   158             + _curView.period[1]
       
   159             + '</span></li>';
       
   160         $("#handle_0").css({
       
   161             "left" : yearToPx(_curView.period[0])+"px",
       
   162         }).attr("year", _curView.period[0])
       
   163         .find(".handleinner")
       
   164         .css({
       
   165             "margin-left" : "-20px"
       
   166         });
       
   167         $("#handle_1").css({
       
   168             "left" : yearToPx(_curView.period[1])+"px",
       
   169         }).attr("year", _curView.period[1])
       
   170         .find(".handleinner")
       
   171         .css({
       
   172             "margin-left" : "0"
       
   173         });
       
   174     }
       
   175     if (_cl) {
       
   176         _htmFilters += _(_curView.country).map(function(_t, _i) {
       
   177             return '<li class="filcountry">Pays&nbsp;: '
       
   178                 + ( gomNs.countryLabels[_t] || _t )
       
   179                 + '<a href="#" class="remfil" onclick="removeFilter(\'country\','
       
   180                 + _i
       
   181                 + '); return false;">[x]</a></li>'; 
       
   182         }).join("");
       
   183     }
       
   184     if (_fl) {
       
   185         _htmFilters += _(_curView.tag).map(function(_t, _i) {
       
   186             return '<li class="filtag">Tag&nbsp;: '
       
   187                 + _t
       
   188                 + '<a href="#" class="remfil" onclick="removeFilter(\'tag\','
       
   189                 + _i
       
   190                 + '); return false;">[x]</a></li>'; 
       
   191         }).join("");
       
   192     }
       
   193     $("#filters").html(_htmFilters);
       
   194     debouncedGetUpdates();
       
   195 }
       
   196 
       
   197 function displayContents(contentdata) {
       
   198     if (contentdata && contentdata.length) {
       
   199         var _htmlCl = '<ul id="contentlist">'
       
   200             + contentdata.map(function(_d) {
       
   201                 var _html = '<li class="content-item">'
       
   202                     + ( _d.latitude != null && _d.longitude != null ?
       
   203                         '<img class="maplet" src="http://maps.googleapis.com/maps/api/staticmap?center=47,1.5&zoom=4&size=160x160&maptype=roadmap&markers=color:red%7C'
       
   204                         + _d.latitude
       
   205                         + ','
       
   206                         + _d.longitude
       
   207                         + '&sensor=false" />'
       
   208                         : '')
       
   209                     + '<h3>'
       
   210                     + _d.title
       
   211                     + '</h3><h4><a href="'
       
   212                     + _d.url
       
   213                     + '" target="_blank">'
       
   214                     + _d.url
       
   215                     + '</a></h4><p>'
       
   216                     + _d.description.replace(/(^.{0,160})([\s]|$)(.*)/,'$1&hellip;')
       
   217                     + '</p><ul class="content-tags">'
       
   218                     + _d.tags.map(function(_t) {
       
   219                         return '<li class="content-tag-item"><a href="#" onclick="tagInfo($(this).text()); return false;"'
       
   220                             + (_t.match ? ' class="tagmatch"' : '')
       
   221                             + '>'
       
   222                             + _t.label
       
   223                             + '</a></li>';
       
   224                     }).join('')
       
   225                     + '</ul><h4>Annotations</h4><div class="content-annotation" contentid="'
       
   226                     + _d.id
       
   227                     + '">'
       
   228                     + ( gomNs.sessiondata.annotations[_d.id] && gomNs.sessiondata.annotations[_d.id].texte
       
   229                         ? '<ul><li>'
       
   230                             + _.escape(gomNs.sessiondata.annotations[_d.id].texte).replace(/\n/gm,"</li><li>")
       
   231                             + '</li>'
       
   232                         : ( gomNs.write_allowed ? '<ul><li>Annoter ce contenu...</li></ul>' : '' ) )
       
   233                     + '</div>';
       
   234                 if (gomNs.write_allowed) {
       
   235                     _(gomNs.sessiondata.views).each(function(_view, _k) {
       
   236                         if (_view.type == 'list') {
       
   237                             _html += '<p><a href="#" class="addremlist" contentid="'
       
   238                                 + _d.id
       
   239                                 + '" viewid="'
       
   240                                 + _k
       
   241                                 + '">'
       
   242                                 + ( _view.list.indexOf(_d.id) == -1 ? 'Ajouter à' : 'Retirer de' )
       
   243                                 + ' "'
       
   244                                 + _.escape(_view.name)
       
   245                                 + '"</a></p>'
       
   246                         }
       
   247                     });
       
   248                 }
       
   249                 _html += '</li>';
       
   250                 return _html;
       
   251             }).join('')
       
   252             + '</ul>';
       
   253         $("#contents").html(_htmlCl).scrollTop(0);
       
   254         $("a.addremlist").click(function() {
       
   255             var _id = $(this).attr("contentid"),
       
   256                 _vid = 
       
   257                 _view = gomNs.sessiondata.views[$(this).attr("viewid")],
       
   258                 _io = _view.list.indexOf(_id);
       
   259             if ( _io == -1) {
       
   260                 _view.list.push(_id);
       
   261                 $(this).html('Retirer de "' + _.escape(_view.name) + '"' );
       
   262             } else {
       
   263                 _view.list.splice(_io, 1);
       
   264                 $(this).html('Ajouter à "' + _.escape(_view.name) + '"');
       
   265             }
       
   266             if (gomNs.sessiondata.view == 1) {
       
   267                 showView();
       
   268             }
       
   269             debouncedSaveChanges();
       
   270             return false;
       
   271         })
       
   272         if (gomNs.write_allowed) {
       
   273             $("div.content-annotation").click(function() {
       
   274                 if (this.children[0].tagName == 'UL') {
       
   275                     var _el = document.createElement('textarea'),
       
   276                         _id = $(this).attr("contentid");
       
   277                     _el.innerHTML = (gomNs.sessiondata.annotations[_id] && gomNs.sessiondata.annotations[_id].texte) ? gomNs.sessiondata.annotations[_id].texte : '';
       
   278                     $(_el).focusout(function() {
       
   279                         var _id = this.parentNode.attributes.contentid.nodeValue;
       
   280                         if (!gomNs.sessiondata.annotations[_id]) {
       
   281                             gomNs.sessiondata.annotations[_id] = {};
       
   282                         }
       
   283                         gomNs.sessiondata.annotations[_id].texte = this.value;
       
   284                         this.parentNode.innerHTML = '<ul><li>' + (this.value.length ? _.escape(gomNs.sessiondata.annotations[_id].texte).replace(/\n/gm,"</li><li>") : 'Annoter ce contenu...' ) + '</ul>';
       
   285                         debouncedSaveChanges();
       
   286                     });
       
   287                     $(this).html(_el);
       
   288                     _el.focus();
       
   289                     _el.select();
       
   290                 }
       
   291             });
       
   292         }
       
   293     }
       
   294     else {
       
   295         $("#contents").html("");
       
   296     }
       
   297 }
       
   298 
       
   299 function updateDisplay(data) {
       
   300     if (gomNs.dhmPaper) {
       
   301         gomNs.dhmPaper.clear();
       
   302     } else {
       
   303         gomNs.dhmPaper = new Raphael("dateheat");
       
   304     }
       
   305     var _s = (data.count>1?'s':'');
       
   306     $("#contentcount").html('<b>'+data.count+'</b> notice'+_s);
       
   307     var _sl = data.sparkline.length;
       
   308     if (_sl) {
       
   309         var _maxheat = _(data.sparkline).max(function(_d) { return parseInt(_d.score); }).score,
       
   310             _exp = 1 / gomNs.heatGamma,
       
   311             _scale = Math.pow(_maxheat, - _exp);
       
   312         _(data.sparkline).each(function(_d, _i) {
       
   313             var _nxt = (_i == _sl - 1) ? gomNs.maxYear + 1 : data.sparkline[_i + 1].year,
       
   314                 _x1 = yearToPx(_d.year),
       
   315                 _x2 = yearToPx(_nxt),
       
   316                 _heat = _scale * Math.pow(_d.score, _exp);
       
   317             gomNs.dhmPaper.rect(_x1, 0, _x2 - _x1, 20).attr({
       
   318                 "fill" : "rgb(255, 128, 128)",
       
   319                 "opacity" : _heat,
       
   320                 "stroke" : "none"
       
   321             })
       
   322         });
       
   323     }
       
   324     if (gomNs.sessiondata.view == 0) {
       
   325         var _h0 = $("#handle_0").position().left,
       
   326             _h1 = $("#handle_1").position().left;
       
   327         gomNs.dhmPaper.rect(Math.min(_h0, _h1) - 1, 0, Math.abs(_h0 - _h1) + 2, 20).attr({
       
   328             "stroke" : "rgb(128,0,0)",
       
   329             "stroke-width" : "3"
       
   330         });
       
   331     }
       
   332     if (data.tags.length) {
       
   333         var _scores = _(data.tags).map(function(_d) { return parseInt(_d.score)}),
       
   334             _maxTag = _(_scores).max(),
       
   335             _minTag = Math.min(_(_scores).min(), _maxTag - 1),
       
   336             _scale = 10 / Math.sqrt(_maxTag - _minTag),
       
   337             _htmlTc = '<ul id="tclist">'
       
   338             + _(data.tags).map(function(_d) {
       
   339                 return '<li style="font-size:'
       
   340                     + parseInt(10 + _scale * Math.sqrt(_d.score - _minTag))
       
   341                     + 'px;"><a href="#" onclick="tagInfo($(this).text()); return false;"'
       
   342                     + (_d.match ? ' class="tagmatch"' : '')
       
   343                     + '>'
       
   344                     + _d.label
       
   345                     + '</a></li>';
       
   346             }).join('')
       
   347             + '</ul>';
       
   348         $("#tagcloud").html(_htmlTc);
       
   349     }
       
   350     else {
       
   351         $("#tagcloud").html("<h4>Pas de mots-clés trouvés</h4>");
       
   352     }
       
   353     displayContents(data.contents);
       
   354     if (gomNs.countries && data.countries) {
       
   355         var _max = Math.max(1, _(data.countries).max());
       
   356         _(gomNs.countries).each(function(_country, _k) {
       
   357             var _val = data.countries[_k] || 0,
       
   358                 _gb = parseInt(255 * (1 - _val / _max )),
       
   359                 _fill = ( _val ? "rgb(255," + _gb + "," + _gb + ")" : "#7070a0" );
       
   360             switch(gomNs.mappingLibrary) {
       
   361                 case 'gmaps':
       
   362                     _(_country.gPolygons).each(function(_p) {
       
   363                         _p.setOptions({
       
   364                             "fillColor" : _fill
       
   365                         });
       
   366                     });
       
   367                 break;
       
   368                 case 'leaflet':
       
   369                     _country.setStyle({
       
   370                             "fillColor" : _fill
       
   371                     })
       
   372                 break;
       
   373             }
       
   374         });
       
   375     }
       
   376     if (data.disciplines) {
       
   377         var _disc = data.disciplines.filter(function(_d) {
       
   378             return +_d.score > 0;
       
   379         }),
       
   380             _echelle = d3.scale
       
   381                 .linear()
       
   382                 .range([0, 120])
       
   383                 .domain([0, d3.max(_disc, function(_d) { return +_d.score })]),
       
   384             _barres = gomNs.disChart.selectAll("g.discbarre").data(_disc);
       
   385             
       
   386         var _newels = _barres.enter()
       
   387             .append("svg:g")
       
   388             .attr("class","discbarre");
       
   389         
       
   390         _newels.append("svg:rect")
       
   391             .attr("stroke","none")
       
   392             .attr("fill","rgb(255,128,128)")
       
   393             .attr("x","0").
       
   394             attr("y","0")
       
   395             .attr("height","25");
       
   396         _newels.append("svg:text")
       
   397             .attr("font-size","12px")
       
   398             .attr("x","5")
       
   399             .attr("y","16");
       
   400         
       
   401         _barres.exit().remove();
       
   402         
       
   403         _barres.on("click", function(_d) {
       
   404             tagInfo(_d.label);
       
   405         }).on("mouseover", function() {
       
   406             d3.select(this).select("rect").attr("fill","rgb(128,128,255)");
       
   407         }).on("mouseout", function() {
       
   408             d3.select(this).select("rect").attr("fill","rgb(255,128,128)");
       
   409         }).attr("transform",function(_d,_k) {
       
   410            return "translate(" + ( 30 + 40 * _k ) + ",120) rotate(-90)";
       
   411         });
       
   412         _barres.select("text").text(function(_d) {
       
   413             return _d.label;
       
   414         });
       
   415         _barres.select("rect").attr("width", function(_d) {
       
   416             return _echelle(_d.score);
       
   417         });
       
   418     }
       
   419 }
       
   420 
       
   421 function saveChanges() {
       
   422     if (gomNs.sessionid && gomNs.sessionkey) {
       
   423         $.getJSON("sessioninfo.php", {
       
   424             "sessionid" : gomNs.sessionid,
       
   425             "sessionkey" : gomNs.sessionkey,
       
   426             "data" : JSON.stringify(gomNs.sessiondata),
       
   427         });
       
   428     }
       
   429 }
       
   430 
       
   431 var debouncedSaveChanges = _.debounce(saveChanges, 3000);
       
   432 
       
   433 function changeView(nview) {
       
   434     var _curView = gomNs.sessiondata.views[nview];
       
   435     if (_curView.type == 'list' && (!_curView.list || !_curView.list.length)) {
       
   436         alert("La liste de contenus est vide ! Ajoutez des contenus pour afficher la liste !");
       
   437     } else {
       
   438         gomNs.sessiondata.view = nview;
       
   439         debouncedSaveChanges();
       
   440         showView();
       
   441     }
       
   442 }
       
   443 
       
   444 function showView() {
       
   445     $(".lienvue").removeClass("actif");
       
   446     $("#view_" + gomNs.sessiondata.view).addClass("actif");
       
   447     var _curView = gomNs.sessiondata.views[gomNs.sessiondata.view];
       
   448     $("#titrevue").val(_curView.name);
       
   449     $("#bloc_gestvue h2").html('Gérer la vue "' + _.escape(_curView.name) + '"');
       
   450     $("div.bloc").show();
       
   451     $("#widgetlist input").prop("checked",true);
       
   452     _(_curView.hiddenWidgets).each(function(_w) {
       
   453         $("#chbx_" + _w).prop("checked",false);
       
   454         $("#" + _w).hide();
       
   455     });
       
   456     $("#notes").html( _curView.notes
       
   457         ? '<ul><li>' + _.escape(_curView.notes).replace(/\n/gm,"</li><li>") + '</li></ul>'
       
   458         : ( gomNs.write_allowed ? '<ul><li>Annoter cette vue...</li></ul>' : '' ) );
       
   459     switch(_curView.type) {
       
   460         case 'list':
       
   461             $("#bandefiltre, .handle").hide();
       
   462             $.getJSON("filter.php", {
       
   463                 contentlist: _curView.list.join(',')
       
   464             }, updateDisplay);
       
   465             break;
       
   466         case 'filter':
       
   467             $("#bandefiltre, .handle").show();
       
   468             updateFilters();
       
   469             break;
       
   470     }
       
   471 }
       
   472 
       
   473 function displayViewList() {
       
   474     $("#ongletsvues").html(gomNs.sessiondata.views.map(function(_v, _k) {
       
   475             return '<li class="lienvue" id="view_'
       
   476                 + _k
       
   477                 + '" onclick="changeView('
       
   478                 + _k
       
   479                 + '); return false;"><a href="#">'
       
   480                 + _.escape(_v.name)
       
   481                 + '</a></li>';
       
   482         }).join(""))
       
   483 }
       
   484 
       
   485 function getInitialView() {
       
   486     var _urlParam = {};
       
   487     if (document.location.hash) {
       
   488         var _tab = document.location.hash.replace("#","").split("-");
       
   489         _urlParam.sessionid = _tab[0];
       
   490         if (_tab.length > 1) {
       
   491             _urlParam.sessionkey = _tab[1];
       
   492         }
       
   493     }
       
   494     $.getJSON("sessioninfo.php", _urlParam, function(data) {
       
   495         gomNs.sessionid = data.sessionid;
       
   496         if (data.sessionkey) {
       
   497             gomNs.sessionkey = data.sessionkey;
       
   498         }
       
   499         gomNs.write_allowed = data.write_allowed;
       
   500         if (data.write_allowed) {
       
   501             $("#partagerw").show();
       
   502         } else {
       
   503             $("#partagerw").hide();
       
   504         }
       
   505         var _baseUrl = document.location.href.split("#")[0];
       
   506         $("#rourl").html(_baseUrl + "#" + data.sessionid );
       
   507         gomNs.hash = "#" + data.sessionid + (data.sessionkey ? '-' + data.sessionkey : '');
       
   508         document.location.hash = gomNs.hash;
       
   509         $("#rwurl").html(_baseUrl + gomNs.hash);
       
   510         gomNs.sessiondata = JSON.parse(data.data);
       
   511         if (!gomNs.sessiondata.title) {
       
   512             gomNs.sessiondata.title = 'Nouvelle session';
       
   513         }
       
   514         if (!gomNs.sessiondata.views) {
       
   515             gomNs.sessiondata.views = [];
       
   516         }
       
   517         if (!gomNs.sessiondata.views.length) {
       
   518             addView( 'filter', 'Mes résultats de recherche' );
       
   519             addView( 'list', 'Ma liste' );
       
   520         }
       
   521         if (!gomNs.sessiondata.annotations) {
       
   522             gomNs.sessiondata.annotations = {};
       
   523         }
       
   524         if (data.write_allowed) {
       
   525             $("#bloc_gestvue").show();
       
   526         } else {
       
   527             $("#bloc_gestvue").hide();
       
   528         }
       
   529         $("#sessionname").html(gomNs.sessiondata.title
       
   530             + ( data.write_allowed ? '' : '<span class="lectseul"> (lecture seule)</span>' ) );
       
   531         gomNs.hrefinterval = setInterval(function() {
       
   532             if (document.location.hash != gomNs.hash) {
       
   533                 clearInterval(gomNs.hrefinterval);
       
   534                 getInitialView();
       
   535             }
       
   536         }, 500);
       
   537         displayViewList();
       
   538         showView();
       
   539     });
       
   540 }
       
   541 
       
   542 function changeSessionTitle(title) {
       
   543     gomNs.sessiondata.title = _.escape(title);
       
   544     $("#sessionname").html(gomNs.sessiondata.title);
       
   545     debouncedSaveChanges();
       
   546 }
       
   547 
       
   548 function addView(viewtype, viewname) {
       
   549     var _content = {
       
   550         type: viewtype,
       
   551         name: viewname,
       
   552         hiddenWidgets: [],
       
   553     };
       
   554     switch(viewtype) {
       
   555         case 'filter':
       
   556             _content.period = [ gomNs.minYear, gomNs.maxYear ];
       
   557             _content.tag = [];
       
   558             _content.country = [];
       
   559             gomNs.sessiondata.view = gomNs.sessiondata.views.length;
       
   560             break;
       
   561         case 'list':
       
   562             _content.list = [];
       
   563             break;
       
   564     }
       
   565     gomNs.sessiondata.views.push(_content);
       
   566     debouncedSaveChanges();
       
   567 }
       
   568 
       
   569 $(document).ready(function() {
       
   570     switch(gomNs.mappingLibrary) {
       
   571         case 'gmaps':
       
   572             gomNs.map = new google.maps.Map(document.getElementById("map"),
       
   573                 {
       
   574                     center: new google.maps.LatLng(30, 0),
       
   575                     zoom: 1,
       
   576                     mapTypeId: google.maps.MapTypeId.SATELLITE
       
   577                 });
       
   578             $.getJSON('lib/countries.geo.json', showCountriesGmap);
       
   579             break;
       
   580         case 'leaflet':
       
   581             gomNs.map = new L.Map('map', {
       
   582                 center: new L.LatLng(30, 0),
       
   583                 zoom: 1
       
   584             });
       
   585             gomNs.map.addLayer(new L.TileLayer("http://s3.amazonaws.com/com.modestmaps.bluemarble/{z}-r{y}-c{x}.jpg", {maxZoom: 9}));
       
   586             $.getJSON('lib/countries.geo.json', showCountriesLeaflet);
       
   587             break;
       
   588     }
       
   589     var _html = gomNs.displayedDates.map(function(_v) {
       
   590         return '<li style="left: '
       
   591             + parseInt(yearToPx(_v))
       
   592             + 'px"><div class="datelabel">'
       
   593             + _v
       
   594             + '</div></li>'
       
   595     }).join('');
       
   596     $("#dates").html(_html);
       
   597     $("#apartager").click(function() {
       
   598         var _pu = $("#partageurls");
       
   599         $(this).attr("class",_pu.is(":visible") ? "" : "actif");
       
   600         _pu.slideToggle();
       
   601         return false;
       
   602     })
       
   603     $(".handle").draggable({
       
   604         "axis" : "x",
       
   605         "containment" : "parent",
       
   606         "drag": function() {
       
   607             $(this).attr("year",parseInt(pxToYear($(this).position().left)));
       
   608             var _curView = gomNs.sessiondata.views[gomNs.sessiondata.view];
       
   609             if (_curView.type == 'filter') {
       
   610                 var _h0 = $("#handle_0"),
       
   611                     _h1 = $("#handle_1"),
       
   612                     _h0v = parseInt(_h0.attr("year")),
       
   613                     _h1v = parseInt(_h1.attr("year"));
       
   614                 _curView.period = [ Math.min(_h0v, _h1v), Math.max(_h0v, _h1v)];
       
   615                 _h0.find(".handleinner").css({
       
   616                     "margin-left" : (_h0v>_h1v ? "0" : "-20px")
       
   617                 })
       
   618                 _h1.find(".handleinner").css({
       
   619                     "margin-left" : (_h1v>_h0v ? "0" : "-20px")
       
   620                 })
       
   621                 updateFilters();
       
   622                 debouncedSaveChanges();
       
   623             }
       
   624         }
       
   625     })
       
   626     var _defLab = $( "#tagsearch" ).val();
       
   627     $( "#tagsearch" ).autocomplete({
       
   628         source: "tagsearch.php",
       
   629         minLength: 2,
       
   630         select: function( event, ui ) {
       
   631             tagInfo(ui.item.label);
       
   632             return false;
       
   633         }
       
   634     }).addClass("grise")
       
   635     .focusin(function() {
       
   636         if ($(this).val() == _defLab) {
       
   637             $(this).val("").removeClass("grise");
       
   638         }
       
   639     });
       
   640     $("#sessionname").click(function() {
       
   641         if (gomNs.write_allowed && !$(this).children().length) {
       
   642             var _el = document.createElement('input');
       
   643             _el.value = gomNs.sessiondata.title;
       
   644             $(_el).focusout(function() {
       
   645                 changeSessionTitle(this.value);
       
   646             }).keypress(function(e) {
       
   647                 if (e.keyCode == 13) {
       
   648                     changeSessionTitle(this.value);
       
   649                 }
       
   650             });
       
   651             $(this).html(_el);
       
   652             _el.focus();
       
   653             _el.select();
       
   654         }
       
   655     });
       
   656     $("#titrevue").keyup(function() {
       
   657         var _curView = gomNs.sessiondata.views[gomNs.sessiondata.view];
       
   658         _curView.name = $(this).val();
       
   659         $("#view_" + gomNs.sessiondata.view + " a").html(_.escape(_curView.name))
       
   660         $("#bloc_gestvue h2").html('Gérer la vue "' + _.escape(_curView.name) + '"');
       
   661         debouncedSaveChanges();
       
   662     });
       
   663     $("#notes").click(function() {
       
   664         if (gomNs.write_allowed) {
       
   665             if (this.children[0].tagName == 'UL') {
       
   666                 var _el = document.createElement('textarea'),
       
   667                     _curView = gomNs.sessiondata.views[gomNs.sessiondata.view];
       
   668                 _el.innerHTML = _curView.notes ? _curView.notes : '';
       
   669                 $(_el).focusout(function() {
       
   670                     _curView.notes = this.value;
       
   671                     this.parentNode.innerHTML = '<ul><li>' + (this.value.length ? _.escape(_curView.notes).replace(/\n/gm,"</li><li>") : 'Annoter cette vue...' ) + '</ul>';
       
   672                     debouncedSaveChanges();
       
   673                 });
       
   674                 $(this).html(_el);
       
   675                 _el.focus();
       
   676                 _el.select();
       
   677             }
       
   678         }
       
   679     });
       
   680     gomNs.disChart = d3.select("#disciplines")
       
   681         .append("svg:svg")
       
   682         .attr("width", 475)
       
   683         .attr("height", 120);
       
   684     getInitialView();
       
   685     $(".barrebloc").click(function() {
       
   686         $(this).next().slideToggle(); 
       
   687     });
       
   688     gomNs.widgetList = [];
       
   689     var _html = '<ul id="widgetlist">';
       
   690     $("div.bloc").each(function() {
       
   691        gomNs.widgetList.push(this.id);
       
   692        _html += '<li><input type="checkbox" id="chbx_'
       
   693         + this.id
       
   694         + '" /><label>' + $(this).find("h2").html() + '</label></li>'
       
   695     });
       
   696     _html + '</ul>';
       
   697     $("#gestvue").append(_html);
       
   698     $("#nouvellevue").click(function() {
       
   699         $("#plusdevues").slideToggle();
       
   700         return false;
       
   701     })
       
   702     $("#widgetlist input").change(function() {
       
   703         var _newWL = [];
       
   704         $("#widgetlist input").each(function(_k, _e) {
       
   705             var _id = _e.id.substr(5);
       
   706             if (!$(_e).prop("checked")) {
       
   707                 $("#" + _id).hide();
       
   708                 _newWL.push(_id);
       
   709             } else {
       
   710                 $("#" + _id).show();
       
   711             }
       
   712         });
       
   713         gomNs.sessiondata.views[gomNs.sessiondata.view].hiddenWidgets = _newWL;
       
   714         debouncedSaveChanges();
       
   715     });
       
   716     $("#nouv_resrech").click(function() {
       
   717        var _txt = prompt("Comment souhaitez-vous nommer votre nouvelle vue ?","Nouveaux résultats de recherche");
       
   718        if (_txt !== null) {
       
   719            addView('filter', _txt); 
       
   720            displayViewList();
       
   721        }
       
   722        return false;
       
   723     });
       
   724     $("#nouv_liste").click(function() {
       
   725        var _txt = prompt("Comment souhaitez-vous nommer votre nouvelle vue ?","Nouvelle liste");
       
   726        if (_txt !== null) {
       
   727            addView('list', _txt);
       
   728            displayViewList();
       
   729        }
       
   730        return false;
       
   731     });
       
   732 });