diff -r 98d264282ed9 -r b26c5f16e180 hdalab/js/gomina.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/hdalab/js/gomina.js Mon Feb 27 11:14:08 2012 +0100 @@ -0,0 +1,732 @@ +/** + * @author raph + */ + +var gomNs = { + minYear: -5000, + maxYear: 2010, + tlPixels: 960, + tlGamma: 6, + heatGamma: 4, + displayedDates: [-5000,0,500,1000,1200,1400,1600,1700,1750,1800,1850,1900,1950,2010], + mappingLibrary: 'leaflet', +} + +function yearToPx(year) { + return gomNs.tlPixels * Math.pow( ( year - gomNs.minYear ) / ( gomNs.maxYear - gomNs.minYear ), gomNs.tlGamma ); +} + +function pxToYear(px) { + return gomNs.minYear + ( gomNs.maxYear - gomNs.minYear ) * Math.pow( px / gomNs.tlPixels, 1 / gomNs.tlGamma ); +} + + +function tagInfo(_taglabel) { + var _urlParam = { "label": _taglabel }; + $.getJSON("taginfo.php", + _urlParam, + function(data) { + $("#tagsearch").val(data.requested_label).removeClass("grise"); + $("#tagcount").html(data.content_count + + ' contenu' + (data.content_count > 1 ? 's' : '') + ' pour ce tag'); + var _html = ''; + if (data.wikipedia_url) { + _html += '

Wikipédia: ' + + decodeURI(data.wikipedia_url.match(/[^\/]+$/)[0]).replace("_"," ") + + '

' + } + if (data.thumbnail) { + _html += ''; + } + if (data.abstract) { + _html += '

' + _(data.abstract).escape().replace(/(^.{0,240})([\s]|$)(.*)/,'$1…') + '

'; + } +/* if (data.links) { + var _lC = data.requested_label.toLowerCase(); + var _t = data.links.map(function(d) { + return (d.subject.toLowerCase() == _lC) ? d.object : d.subject; + }); + _t.sort(); + _t = _(_t).uniq(true); + _html += '

Tags liés (dbpedia)

'; + } */ + $("#tagdata").html(_html); + }); + addFilter('tag', _taglabel); + $("#showlist").removeClass("actif"); +} + +function addFilter(_type, _label) { + var _curView = gomNs.sessiondata.views[gomNs.sessiondata.view]; + if (_curView.type == 'filter') { + _curView[_type].push(_label); + console.log(_curView[_type]); + updateFilters(); + debouncedSaveChanges(); + } +} + +function removeFilter(_type, _index) { + var _curView = gomNs.sessiondata.views[gomNs.sessiondata.view]; + if (_curView.type == 'filter') { + _curView[_type].splice(_index,1); + updateFilters(); + debouncedSaveChanges(); + } +} + +function getUpdates() { + var _params = {}, + _curView = gomNs.sessiondata.views[gomNs.sessiondata.view]; + if (_curView.type == 'filter') { + if (_curView.period && !(_curView.period[0] == gomNs.minYear && _curView.period[1] == gomNs.maxYear)) { + _params.period = _curView.period.join(','); + } + if (_curView.tag.length) { + _params.label = _curView.tag.join(','); + } + if (_curView.country.length) { + _params.country = _curView.country.join(','); + } + $.getJSON("filter.php", _params, updateDisplay); + } +} + +var debouncedGetUpdates = _.debounce(getUpdates, 300); + +function updatePeriod(_n, _val) { + var _curView = gomNs.sessiondata.views[gomNs.sessiondata.view]; + if (_curView.type == 'filter') { + var _int = parseInt(_val); + if (_int != NaN) { + if ((_n == 1 && _val >= _curView.period[0]) || (_n == 0 && _val <= _curView.period[1])) { + _curView.period[_n] = _int; + } + updateFilters(); + debouncedSaveChanges(); + } + } +} + +function changeSpan(_this) { + var _curView = gomNs.sessiondata.views[gomNs.sessiondata.view]; + if (_curView.type == 'filter') { + if (!$(_this).children().length) { + var _el = document.createElement('input'), + _n = _this.id.split('_')[1]; + _el.value = _curView.period[_n]; + _el.style.width = $(_this).width() + 'px'; + $(_el).focusout(function() { + updatePeriod(_n, this.value); + }).keypress(function(e) { + if (e.keyCode == 13) { + updatePeriod(_n, this.value); + } + }); + $(_this).html(_el); + _el.focus(); + _el.select(); + } + } +} + +function updateFilters() { + var _curView = gomNs.sessiondata.views[gomNs.sessiondata.view]; + if (_curView.type != 'filter') { + return; + } + var _htmFilters = '', + _fl = _curView.tag.length, + _cl = _curView.country.length; + if (!_curView.period && !_fl && !_cl) { + _htmFilters = '
  • Aucun filtre
  • '; + } + if (_curView.period) { + _htmFilters += '
  • Période : ' + + _curView.period[0] + + ' à ' + + _curView.period[1] + + '
  • '; + $("#handle_0").css({ + "left" : yearToPx(_curView.period[0])+"px", + }).attr("year", _curView.period[0]) + .find(".handleinner") + .css({ + "margin-left" : "-20px" + }); + $("#handle_1").css({ + "left" : yearToPx(_curView.period[1])+"px", + }).attr("year", _curView.period[1]) + .find(".handleinner") + .css({ + "margin-left" : "0" + }); + } + if (_cl) { + _htmFilters += _(_curView.country).map(function(_t, _i) { + return '
  • Pays : ' + + ( gomNs.countryLabels[_t] || _t ) + + '[x]
  • '; + }).join(""); + } + if (_fl) { + _htmFilters += _(_curView.tag).map(function(_t, _i) { + return '
  • Tag : ' + + _t + + '[x]
  • '; + }).join(""); + } + $("#filters").html(_htmFilters); + debouncedGetUpdates(); +} + +function displayContents(contentdata) { + if (contentdata && contentdata.length) { + var _htmlCl = ''; + $("#contents").html(_htmlCl).scrollTop(0); + $("a.addremlist").click(function() { + var _id = $(this).attr("contentid"), + _vid = + _view = gomNs.sessiondata.views[$(this).attr("viewid")], + _io = _view.list.indexOf(_id); + if ( _io == -1) { + _view.list.push(_id); + $(this).html('Retirer de "' + _.escape(_view.name) + '"' ); + } else { + _view.list.splice(_io, 1); + $(this).html('Ajouter à "' + _.escape(_view.name) + '"'); + } + if (gomNs.sessiondata.view == 1) { + showView(); + } + debouncedSaveChanges(); + return false; + }) + if (gomNs.write_allowed) { + $("div.content-annotation").click(function() { + if (this.children[0].tagName == 'UL') { + var _el = document.createElement('textarea'), + _id = $(this).attr("contentid"); + _el.innerHTML = (gomNs.sessiondata.annotations[_id] && gomNs.sessiondata.annotations[_id].texte) ? gomNs.sessiondata.annotations[_id].texte : ''; + $(_el).focusout(function() { + var _id = this.parentNode.attributes.contentid.nodeValue; + if (!gomNs.sessiondata.annotations[_id]) { + gomNs.sessiondata.annotations[_id] = {}; + } + gomNs.sessiondata.annotations[_id].texte = this.value; + this.parentNode.innerHTML = ''; + debouncedSaveChanges(); + }); + $(this).html(_el); + _el.focus(); + _el.select(); + } + }); + } + } + else { + $("#contents").html(""); + } +} + +function updateDisplay(data) { + if (gomNs.dhmPaper) { + gomNs.dhmPaper.clear(); + } else { + gomNs.dhmPaper = new Raphael("dateheat"); + } + var _s = (data.count>1?'s':''); + $("#contentcount").html(''+data.count+' notice'+_s); + var _sl = data.sparkline.length; + if (_sl) { + var _maxheat = _(data.sparkline).max(function(_d) { return parseInt(_d.score); }).score, + _exp = 1 / gomNs.heatGamma, + _scale = Math.pow(_maxheat, - _exp); + _(data.sparkline).each(function(_d, _i) { + var _nxt = (_i == _sl - 1) ? gomNs.maxYear + 1 : data.sparkline[_i + 1].year, + _x1 = yearToPx(_d.year), + _x2 = yearToPx(_nxt), + _heat = _scale * Math.pow(_d.score, _exp); + gomNs.dhmPaper.rect(_x1, 0, _x2 - _x1, 20).attr({ + "fill" : "rgb(255, 128, 128)", + "opacity" : _heat, + "stroke" : "none" + }) + }); + } + if (gomNs.sessiondata.view == 0) { + var _h0 = $("#handle_0").position().left, + _h1 = $("#handle_1").position().left; + gomNs.dhmPaper.rect(Math.min(_h0, _h1) - 1, 0, Math.abs(_h0 - _h1) + 2, 20).attr({ + "stroke" : "rgb(128,0,0)", + "stroke-width" : "3" + }); + } + if (data.tags.length) { + var _scores = _(data.tags).map(function(_d) { return parseInt(_d.score)}), + _maxTag = _(_scores).max(), + _minTag = Math.min(_(_scores).min(), _maxTag - 1), + _scale = 10 / Math.sqrt(_maxTag - _minTag), + _htmlTc = ''; + $("#tagcloud").html(_htmlTc); + } + else { + $("#tagcloud").html("

    Pas de mots-clés trouvés

    "); + } + displayContents(data.contents); + if (gomNs.countries && data.countries) { + var _max = Math.max(1, _(data.countries).max()); + _(gomNs.countries).each(function(_country, _k) { + var _val = data.countries[_k] || 0, + _gb = parseInt(255 * (1 - _val / _max )), + _fill = ( _val ? "rgb(255," + _gb + "," + _gb + ")" : "#7070a0" ); + switch(gomNs.mappingLibrary) { + case 'gmaps': + _(_country.gPolygons).each(function(_p) { + _p.setOptions({ + "fillColor" : _fill + }); + }); + break; + case 'leaflet': + _country.setStyle({ + "fillColor" : _fill + }) + break; + } + }); + } + if (data.disciplines) { + var _disc = data.disciplines.filter(function(_d) { + return +_d.score > 0; + }), + _echelle = d3.scale + .linear() + .range([0, 120]) + .domain([0, d3.max(_disc, function(_d) { return +_d.score })]), + _barres = gomNs.disChart.selectAll("g.discbarre").data(_disc); + + var _newels = _barres.enter() + .append("svg:g") + .attr("class","discbarre"); + + _newels.append("svg:rect") + .attr("stroke","none") + .attr("fill","rgb(255,128,128)") + .attr("x","0"). + attr("y","0") + .attr("height","25"); + _newels.append("svg:text") + .attr("font-size","12px") + .attr("x","5") + .attr("y","16"); + + _barres.exit().remove(); + + _barres.on("click", function(_d) { + tagInfo(_d.label); + }).on("mouseover", function() { + d3.select(this).select("rect").attr("fill","rgb(128,128,255)"); + }).on("mouseout", function() { + d3.select(this).select("rect").attr("fill","rgb(255,128,128)"); + }).attr("transform",function(_d,_k) { + return "translate(" + ( 30 + 40 * _k ) + ",120) rotate(-90)"; + }); + _barres.select("text").text(function(_d) { + return _d.label; + }); + _barres.select("rect").attr("width", function(_d) { + return _echelle(_d.score); + }); + } +} + +function saveChanges() { + if (gomNs.sessionid && gomNs.sessionkey) { + $.getJSON("sessioninfo.php", { + "sessionid" : gomNs.sessionid, + "sessionkey" : gomNs.sessionkey, + "data" : JSON.stringify(gomNs.sessiondata), + }); + } +} + +var debouncedSaveChanges = _.debounce(saveChanges, 3000); + +function changeView(nview) { + var _curView = gomNs.sessiondata.views[nview]; + if (_curView.type == 'list' && (!_curView.list || !_curView.list.length)) { + alert("La liste de contenus est vide ! Ajoutez des contenus pour afficher la liste !"); + } else { + gomNs.sessiondata.view = nview; + debouncedSaveChanges(); + showView(); + } +} + +function showView() { + $(".lienvue").removeClass("actif"); + $("#view_" + gomNs.sessiondata.view).addClass("actif"); + var _curView = gomNs.sessiondata.views[gomNs.sessiondata.view]; + $("#titrevue").val(_curView.name); + $("#bloc_gestvue h2").html('Gérer la vue "' + _.escape(_curView.name) + '"'); + $("div.bloc").show(); + $("#widgetlist input").prop("checked",true); + _(_curView.hiddenWidgets).each(function(_w) { + $("#chbx_" + _w).prop("checked",false); + $("#" + _w).hide(); + }); + $("#notes").html( _curView.notes + ? '' + : ( gomNs.write_allowed ? '' : '' ) ); + switch(_curView.type) { + case 'list': + $("#bandefiltre, .handle").hide(); + $.getJSON("filter.php", { + contentlist: _curView.list.join(',') + }, updateDisplay); + break; + case 'filter': + $("#bandefiltre, .handle").show(); + updateFilters(); + break; + } +} + +function displayViewList() { + $("#ongletsvues").html(gomNs.sessiondata.views.map(function(_v, _k) { + return '
  • ' + + _.escape(_v.name) + + '
  • '; + }).join("")) +} + +function getInitialView() { + var _urlParam = {}; + if (document.location.hash) { + var _tab = document.location.hash.replace("#","").split("-"); + _urlParam.sessionid = _tab[0]; + if (_tab.length > 1) { + _urlParam.sessionkey = _tab[1]; + } + } + $.getJSON("sessioninfo.php", _urlParam, function(data) { + gomNs.sessionid = data.sessionid; + if (data.sessionkey) { + gomNs.sessionkey = data.sessionkey; + } + gomNs.write_allowed = data.write_allowed; + if (data.write_allowed) { + $("#partagerw").show(); + } else { + $("#partagerw").hide(); + } + var _baseUrl = document.location.href.split("#")[0]; + $("#rourl").html(_baseUrl + "#" + data.sessionid ); + gomNs.hash = "#" + data.sessionid + (data.sessionkey ? '-' + data.sessionkey : ''); + document.location.hash = gomNs.hash; + $("#rwurl").html(_baseUrl + gomNs.hash); + gomNs.sessiondata = JSON.parse(data.data); + if (!gomNs.sessiondata.title) { + gomNs.sessiondata.title = 'Nouvelle session'; + } + if (!gomNs.sessiondata.views) { + gomNs.sessiondata.views = []; + } + if (!gomNs.sessiondata.views.length) { + addView( 'filter', 'Mes résultats de recherche' ); + addView( 'list', 'Ma liste' ); + } + if (!gomNs.sessiondata.annotations) { + gomNs.sessiondata.annotations = {}; + } + if (data.write_allowed) { + $("#bloc_gestvue").show(); + } else { + $("#bloc_gestvue").hide(); + } + $("#sessionname").html(gomNs.sessiondata.title + + ( data.write_allowed ? '' : ' (lecture seule)' ) ); + gomNs.hrefinterval = setInterval(function() { + if (document.location.hash != gomNs.hash) { + clearInterval(gomNs.hrefinterval); + getInitialView(); + } + }, 500); + displayViewList(); + showView(); + }); +} + +function changeSessionTitle(title) { + gomNs.sessiondata.title = _.escape(title); + $("#sessionname").html(gomNs.sessiondata.title); + debouncedSaveChanges(); +} + +function addView(viewtype, viewname) { + var _content = { + type: viewtype, + name: viewname, + hiddenWidgets: [], + }; + switch(viewtype) { + case 'filter': + _content.period = [ gomNs.minYear, gomNs.maxYear ]; + _content.tag = []; + _content.country = []; + gomNs.sessiondata.view = gomNs.sessiondata.views.length; + break; + case 'list': + _content.list = []; + break; + } + gomNs.sessiondata.views.push(_content); + debouncedSaveChanges(); +} + +$(document).ready(function() { + switch(gomNs.mappingLibrary) { + case 'gmaps': + gomNs.map = new google.maps.Map(document.getElementById("map"), + { + center: new google.maps.LatLng(30, 0), + zoom: 1, + mapTypeId: google.maps.MapTypeId.SATELLITE + }); + $.getJSON('lib/countries.geo.json', showCountriesGmap); + break; + case 'leaflet': + gomNs.map = new L.Map('map', { + center: new L.LatLng(30, 0), + zoom: 1 + }); + gomNs.map.addLayer(new L.TileLayer("http://s3.amazonaws.com/com.modestmaps.bluemarble/{z}-r{y}-c{x}.jpg", {maxZoom: 9})); + $.getJSON('lib/countries.geo.json', showCountriesLeaflet); + break; + } + var _html = gomNs.displayedDates.map(function(_v) { + return '
  • ' + + _v + + '
  • ' + }).join(''); + $("#dates").html(_html); + $("#apartager").click(function() { + var _pu = $("#partageurls"); + $(this).attr("class",_pu.is(":visible") ? "" : "actif"); + _pu.slideToggle(); + return false; + }) + $(".handle").draggable({ + "axis" : "x", + "containment" : "parent", + "drag": function() { + $(this).attr("year",parseInt(pxToYear($(this).position().left))); + var _curView = gomNs.sessiondata.views[gomNs.sessiondata.view]; + if (_curView.type == 'filter') { + var _h0 = $("#handle_0"), + _h1 = $("#handle_1"), + _h0v = parseInt(_h0.attr("year")), + _h1v = parseInt(_h1.attr("year")); + _curView.period = [ Math.min(_h0v, _h1v), Math.max(_h0v, _h1v)]; + _h0.find(".handleinner").css({ + "margin-left" : (_h0v>_h1v ? "0" : "-20px") + }) + _h1.find(".handleinner").css({ + "margin-left" : (_h1v>_h0v ? "0" : "-20px") + }) + updateFilters(); + debouncedSaveChanges(); + } + } + }) + var _defLab = $( "#tagsearch" ).val(); + $( "#tagsearch" ).autocomplete({ + source: "tagsearch.php", + minLength: 2, + select: function( event, ui ) { + tagInfo(ui.item.label); + return false; + } + }).addClass("grise") + .focusin(function() { + if ($(this).val() == _defLab) { + $(this).val("").removeClass("grise"); + } + }); + $("#sessionname").click(function() { + if (gomNs.write_allowed && !$(this).children().length) { + var _el = document.createElement('input'); + _el.value = gomNs.sessiondata.title; + $(_el).focusout(function() { + changeSessionTitle(this.value); + }).keypress(function(e) { + if (e.keyCode == 13) { + changeSessionTitle(this.value); + } + }); + $(this).html(_el); + _el.focus(); + _el.select(); + } + }); + $("#titrevue").keyup(function() { + var _curView = gomNs.sessiondata.views[gomNs.sessiondata.view]; + _curView.name = $(this).val(); + $("#view_" + gomNs.sessiondata.view + " a").html(_.escape(_curView.name)) + $("#bloc_gestvue h2").html('Gérer la vue "' + _.escape(_curView.name) + '"'); + debouncedSaveChanges(); + }); + $("#notes").click(function() { + if (gomNs.write_allowed) { + if (this.children[0].tagName == 'UL') { + var _el = document.createElement('textarea'), + _curView = gomNs.sessiondata.views[gomNs.sessiondata.view]; + _el.innerHTML = _curView.notes ? _curView.notes : ''; + $(_el).focusout(function() { + _curView.notes = this.value; + this.parentNode.innerHTML = ''; + debouncedSaveChanges(); + }); + $(this).html(_el); + _el.focus(); + _el.select(); + } + } + }); + gomNs.disChart = d3.select("#disciplines") + .append("svg:svg") + .attr("width", 475) + .attr("height", 120); + getInitialView(); + $(".barrebloc").click(function() { + $(this).next().slideToggle(); + }); + gomNs.widgetList = []; + var _html = ''; + $("#gestvue").append(_html); + $("#nouvellevue").click(function() { + $("#plusdevues").slideToggle(); + return false; + }) + $("#widgetlist input").change(function() { + var _newWL = []; + $("#widgetlist input").each(function(_k, _e) { + var _id = _e.id.substr(5); + if (!$(_e).prop("checked")) { + $("#" + _id).hide(); + _newWL.push(_id); + } else { + $("#" + _id).show(); + } + }); + gomNs.sessiondata.views[gomNs.sessiondata.view].hiddenWidgets = _newWL; + debouncedSaveChanges(); + }); + $("#nouv_resrech").click(function() { + var _txt = prompt("Comment souhaitez-vous nommer votre nouvelle vue ?","Nouveaux résultats de recherche"); + if (_txt !== null) { + addView('filter', _txt); + displayViewList(); + } + return false; + }); + $("#nouv_liste").click(function() { + var _txt = prompt("Comment souhaitez-vous nommer votre nouvelle vue ?","Nouvelle liste"); + if (_txt !== null) { + addView('list', _txt); + displayViewList(); + } + return false; + }); +});