Rev 5 | Rev 7 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
var dictionnaire = new Array();
var motsAyantDefinition = null;
var mouseX = null;
var mouseY = null;
var active = false;
function getUrlBaseService() {
// provient de dictionnaire.config.js
return URL_BASE_SERVICE;
}
function supprimerAccents(str) {
var rExps=[
{re:/[\xC0-\xC6]/g, ch:'A'},
{re:/[\xE0-\xE6]/g, ch:'a'},
{re:/[\xC8-\xCB]/g, ch:'E'},
{re:/[\xE8-\xEB]/g, ch:'e'},
{re:/[\xCC-\xCF]/g, ch:'I'},
{re:/[\xEC-\xEF]/g, ch:'i'},
{re:/[\xD2-\xD6]/g, ch:'O'},
{re:/[\xF2-\xF6]/g, ch:'o'},
{re:/[\xD9-\xDC]/g, ch:'U'},
{re:/[\xF9-\xFC]/g, ch:'u'},
{re:/[\xD1]/g, ch:'N'},
{re:/[\xF1]/g, ch:'n'} ];
for(var i=0, len=rExps.length; i<len; i++)
str=str.replace(rExps[i].re, rExps[i].ch);
return str;
};
// c'est moche mais le mauvais support de l'unicode dans
// javascript ne me permet pas de faire mieux
function etendreChaineAccents(str) {
return str.replace('a',"(a|à|á|â|ã|ä|å)")
.replace('e',"(e|è|é|ê|ë)")
.replace('i',"(i|ì|í|î|ï)")
.replace('o',"(o|ò|ó|ô|õ|ö)")
.replace('u',"(u|ù|ú|û|ü)")
.replace('y',"(ýÿ)")
.replace('a',"(a|à|á|â|ã|ä|å)")
.replace('æ',"(ae|æ)")
.replace('ç',"(ç|c)")
.replace('ñ',"(ñ|n)")
.replace('œ',"(œ|oe)");
}
function afficherLienDefinitions() {
html = '<div id="conteneur_activation_definition"><a href="#">rechercher les définitions</a></div>';
$('#conteneur_activation_definition').live('click', function(event) {
event.preventDefault();
supprimerToutesDefinitions();
if(motsAyantDefinition == null) {
getMotsADefinitions();
} else {
ajouterDefinitions(motsAyantDefinition);
}
});
$('body').append(html);
}
function normaliserMotPourRecherche(str) {
str = supprimerAccents(str);
str = etendreChaineAccents(str);
return str;
}
$.fn.remplacerDefinitions = function(mots) {
this.each(function() {
$(this).contents().filter(function() {
return this.nodeType == 3;
}).each(function() {
element = $(this);
texte = element.text();
if(texte != "") {
$.each(mots, function(index, valeur) {
texte = rechercherEtRemplacerMotParDefinition(texte, valeur);
});
element.replaceWith(texte);
}
});
});
return this;
}
function rechercherEtRemplacerMotParDefinition(texte, mot) {
exclureSpan = '[^(?:class="definition_term">)]';
regExp = new RegExp(exclureSpan+"[ |,|-|;|.]+("+mot+")[a-zA-Z]{1}", 'ig');
termeDansTexte = regExp.exec(texte);
if(termeDansTexte != null && termeDansTexte.length > 1) {
motOriginal = termeDansTexte[1];
templateMotADefinition = formaterTemplateMotADefinition(motOriginal);
texte = texte.replace(motOriginal, templateMotADefinition);
}
return texte;
}
function getMotsADefinitions() {
$.ajax({
url: getUrlBaseService()+'mots/',
success: function(data) {
motsAyantDefinition = data;
ajouterDefinitions(motsAyantDefinition);
},
dataType: "JSON",
global: false
});
ajouterListenerDefinitions();
}
function ajouterDefinitions(motsAvecDefinitions) {
set = 'p, span, td, pre, div';
$(set).remplacerDefinitions(motsAvecDefinitions);
}
function formaterTemplateMotADefinition(motOriginal) {
motSimplifie = supprimerAccents(motOriginal);
definitionHtml = '<span rel="'+motSimplifie+'" class="definition_term">'
+motOriginal+
'</span>';
return definitionHtml;
}
function ajouterListenerDefinitions() {
$('.definition_term').live('mouseover mouseout', function(event) {
if (event.type == 'mouseover') {
event.preventDefault();
afficherDefinition($(this));
} else {
cacherPopupsDefinitions();
}
});
}
function afficherDefinition(element) {
mot = element.attr('rel');
if(dictionnaire[mot] != null) {
element.after(formaterDefinition(element));
afficherPopupDefinition();
} else {
chargerDefinitionDistante(element);
}
}
function chargerDefinitionDistante(element) {
date = new Date();
mot = element.attr('rel');
$.ajax({
url: getUrlBaseService()+'def/'+mot,
success: function(data) {
retour = data;
definition = retour.valeur;
dictionnaire[mot] = definition;
element.after(formaterDefinition(element));
afficherPopupDefinition();
},
dataType: "JSON",
global: false
});
}
function afficherPopupDefinition() {
$(".definition_container").css({'top':mouseY + 20,'left':mouseX - 10}).fadeIn('slow');
}
function cacherPopupsDefinitions() {
$(".definition_container").remove();
}
function formaterDefinition(element) {
mot = element.attr('rel');
data = dictionnaire[mot];
defHtml = '<div class="definition_container">'+
'<span class="definition_container_fleche"></span>'+
'<span class="definition">'+data+'</span>'+
'</div>';
return defHtml;
}
function supprimerToutesDefinitions() {
$('.definition_term').each(function() {
$(this).replaceWith($(this).html());
});
cacherPopupsDefinitions();
}
$(document).bind('mousemove', function(e){
mouseX = e.pageX;
mouseY = e.pageY - $(window).scrollTop();
});
$(document).ready(function() {
getMotsADefinitions();
afficherLienDefinitions();
});