Subversion Repositories Applications.dictionnaire

Rev

Rev 3 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

var dictionnaire = new Array();
var motsAyantDefinition = new Array();
var noeudAyantsDefinition = new Array();
var mouseX = null;
var mouseY = null;

function getUrlBaseService() {
        return 'http://localhost/dictionnaire/services/0.1/dictionnaire/';
}

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 normaliserMotPourRecherche(str) {
        str = supprimerAccents(str);
        str = etendreChaineAccents(str);
        return str;
}

$.fn.remplacerDefinition = function(mot) {
        
        motSimplifie = supprimerAccents(mot).toLowerCase();
        mot = normaliserMotPourRecherche(mot);
    
    var regExp = new RegExp("("+mot+")", 'ig');
    this.each(function() {      
          $(this).contents().filter(function() { 
                  return this.nodeType == Node.TEXT_NODE;          
          }).each(function() {
                        termeDansTexte = regExp.exec($(this).text());   
                        if(termeDansTexte != null && termeDansTexte.length > 0) {
                        motOriginal = termeDansTexte[0];
                        templateMotADefinition = formaterTemplateMotADefinition(motSimplifie, motOriginal);
                    output = $(this).text().replace(regExp, definitionHtml);
                    $(this).replaceWith(output);
                    noeudAyantsDefinition.push($(this));
                        }
          });
    });
    return this;
}

function getMotADefinitions() {
        $.ajax({
                url: getUrlBaseService()+'mots/',
                success: function(data) {
                        motsAyantDefinition = null;
                        motsAyantDefinition = data;
                        ajouterDefinitions(motsAyantDefinition);
                },
                dataType: "JSON",
                global: false
        });
        
        ajouterListenerDefinitions();
}

function ajouterDefinitions(motsAvecDefinitions) {
        jQuery.each(motsAvecDefinitions, function(index) {      
                def = motsAvecDefinitions[index]['cle'];
                $('p, span, td, th, div').remplacerDefinition(def);
        });
}

function formaterTemplateMotADefinition(motSimplifie, 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.append(formaterDefinition(element));
                afficherPopupDefinition();
        } else {
                chargerDefinitionDistante(element);
        }
}

function chargerDefinitionDistante(element) {   
        $.ajax({
                url: getUrlBaseService()+'def/'+mot,
                success: function(data) {
                        retour = data;
                        definition = retour.valeur;
                        dictionnaire[mot] = definition;
                        element.append(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>'+
                                        data+
                                '</div>';
        return defHtml;
}

function supprimerToutesDefinitions() {
        $('.definition_term').each(function() {
                $(this).replaceWith($(this).html());
        });
        cacherPopupsDefinitions();
}

function surFinRequeteAjax() {
        supprimerToutesDefinitions();
        ajouterDefinitions(motsAyantDefinition);
}

$(document).bind('mousemove', function(e){
        mouseX = e.pageX;
    mouseY = e.pageY - $(window).scrollTop();
});

$(document).ready(function() {
        getMotADefinitions();  
});

$(document).ajaxStop(function() {
        t = setTimeout(function(){surFinRequeteAjax()},800)
});