| 1 |
aurelien |
1 |
var dictionnaire = new Array();
|
| 3 |
aurelien |
2 |
var motsAyantDefinition = null;
|
| 1 |
aurelien |
3 |
var mouseX = null;
|
|
|
4 |
var mouseY = null;
|
| 3 |
aurelien |
5 |
var tempsDepuisRequeteAjax = 0;
|
|
|
6 |
var active = false;
|
| 1 |
aurelien |
7 |
|
|
|
8 |
function getUrlBaseService() {
|
| 3 |
aurelien |
9 |
// provient de dictionnaire.config.js
|
|
|
10 |
return URL_BASE_SERVICE;
|
| 1 |
aurelien |
11 |
}
|
|
|
12 |
|
|
|
13 |
function supprimerAccents(str) {
|
|
|
14 |
var rExps=[
|
|
|
15 |
{re:/[\xC0-\xC6]/g, ch:'A'},
|
|
|
16 |
{re:/[\xE0-\xE6]/g, ch:'a'},
|
|
|
17 |
{re:/[\xC8-\xCB]/g, ch:'E'},
|
|
|
18 |
{re:/[\xE8-\xEB]/g, ch:'e'},
|
|
|
19 |
{re:/[\xCC-\xCF]/g, ch:'I'},
|
|
|
20 |
{re:/[\xEC-\xEF]/g, ch:'i'},
|
|
|
21 |
{re:/[\xD2-\xD6]/g, ch:'O'},
|
|
|
22 |
{re:/[\xF2-\xF6]/g, ch:'o'},
|
|
|
23 |
{re:/[\xD9-\xDC]/g, ch:'U'},
|
|
|
24 |
{re:/[\xF9-\xFC]/g, ch:'u'},
|
|
|
25 |
{re:/[\xD1]/g, ch:'N'},
|
|
|
26 |
{re:/[\xF1]/g, ch:'n'} ];
|
|
|
27 |
for(var i=0, len=rExps.length; i<len; i++)
|
|
|
28 |
str=str.replace(rExps[i].re, rExps[i].ch);
|
|
|
29 |
return str;
|
|
|
30 |
};
|
|
|
31 |
|
|
|
32 |
// c'est moche mais le mauvais support de l'unicode dans
|
|
|
33 |
// javascript ne me permet pas de faire mieux
|
|
|
34 |
function etendreChaineAccents(str) {
|
|
|
35 |
return str.replace('a',"(a|à|á|â|ã|ä|å)")
|
|
|
36 |
.replace('e',"(e|è|é|ê|ë)")
|
|
|
37 |
.replace('i',"(i|ì|í|î|ï)")
|
|
|
38 |
.replace('o',"(o|ò|ó|ô|õ|ö)")
|
|
|
39 |
.replace('u',"(u|ù|ú|û|ü)")
|
|
|
40 |
.replace('y',"(ýÿ)")
|
|
|
41 |
.replace('a',"(a|à|á|â|ã|ä|å)")
|
|
|
42 |
.replace('æ',"(ae|æ)")
|
|
|
43 |
.replace('ç',"(ç|c)")
|
|
|
44 |
.replace('ñ',"(ñ|n)")
|
|
|
45 |
.replace('œ',"(œ|oe)");
|
|
|
46 |
}
|
|
|
47 |
|
| 3 |
aurelien |
48 |
function afficherLienDefinitions() {
|
|
|
49 |
html = '<div id="conteneur_activation_definition"><a href="#">rechercher les définitions</a></div>';
|
|
|
50 |
$('#conteneur_activation_definition').live('click', function(event) {
|
|
|
51 |
event.preventDefault();
|
|
|
52 |
supprimerToutesDefinitions();
|
|
|
53 |
if(motsAyantDefinition == null) {
|
|
|
54 |
getMotsADefinitions();
|
|
|
55 |
} else {
|
|
|
56 |
ajouterDefinitions(motsAyantDefinition);
|
|
|
57 |
}
|
|
|
58 |
});
|
|
|
59 |
$('body').append(html);
|
|
|
60 |
}
|
|
|
61 |
|
| 1 |
aurelien |
62 |
function normaliserMotPourRecherche(str) {
|
|
|
63 |
str = supprimerAccents(str);
|
|
|
64 |
str = etendreChaineAccents(str);
|
|
|
65 |
return str;
|
|
|
66 |
}
|
|
|
67 |
|
| 3 |
aurelien |
68 |
$.fn.remplacerDefinitions = function(mots) {
|
| 1 |
aurelien |
69 |
this.each(function() {
|
|
|
70 |
$(this).contents().filter(function() {
|
| 3 |
aurelien |
71 |
return this.nodeType == 3;
|
| 1 |
aurelien |
72 |
}).each(function() {
|
| 3 |
aurelien |
73 |
element = $(this);
|
|
|
74 |
texte = element.text();
|
|
|
75 |
if(texte != "") {
|
|
|
76 |
$.each(mots, function(index, valeur) {
|
|
|
77 |
def = valeur['cle'];
|
|
|
78 |
texte = rechercherEtRemplacerMotParDefinition(texte, def);
|
|
|
79 |
});
|
|
|
80 |
element.replaceWith(texte);
|
|
|
81 |
}
|
| 1 |
aurelien |
82 |
});
|
|
|
83 |
});
|
|
|
84 |
return this;
|
|
|
85 |
}
|
|
|
86 |
|
| 3 |
aurelien |
87 |
function rechercherEtRemplacerMotParDefinition(texte, mot) {
|
|
|
88 |
motSimplifie = supprimerAccents(mot).toLowerCase();
|
|
|
89 |
mot = normaliserMotPourRecherche(mot);
|
|
|
90 |
exclureSpan = '[^(?:class="definition_term">)]';
|
|
|
91 |
regExp = new RegExp(exclureSpan+"[ |,|-|;|.]+("+mot+")[a-zA-Z]{1}", 'ig');
|
|
|
92 |
termeDansTexte = regExp.exec(texte);
|
|
|
93 |
if(termeDansTexte != null && termeDansTexte.length > 1) {
|
|
|
94 |
motOriginal = termeDansTexte[1];
|
|
|
95 |
templateMotADefinition = formaterTemplateMotADefinition(motSimplifie, motOriginal);
|
|
|
96 |
texte = texte.replace(motOriginal, templateMotADefinition);
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
return texte;
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
function getMotsADefinitions() {
|
| 1 |
aurelien |
103 |
$.ajax({
|
|
|
104 |
url: getUrlBaseService()+'mots/',
|
|
|
105 |
success: function(data) {
|
|
|
106 |
motsAyantDefinition = null;
|
|
|
107 |
motsAyantDefinition = data;
|
|
|
108 |
ajouterDefinitions(motsAyantDefinition);
|
|
|
109 |
},
|
|
|
110 |
dataType: "JSON",
|
|
|
111 |
global: false
|
|
|
112 |
});
|
|
|
113 |
|
|
|
114 |
ajouterListenerDefinitions();
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
function ajouterDefinitions(motsAvecDefinitions) {
|
| 3 |
aurelien |
118 |
set = 'p, span:not("definition_term"), td, pre';
|
|
|
119 |
$(set).remplacerDefinitions(motsAvecDefinitions);
|
| 1 |
aurelien |
120 |
}
|
|
|
121 |
|
|
|
122 |
function formaterTemplateMotADefinition(motSimplifie, motOriginal) {
|
|
|
123 |
definitionHtml = '<span rel="'+motSimplifie+'" class="definition_term">'
|
|
|
124 |
+motOriginal+
|
|
|
125 |
'</span>';
|
|
|
126 |
return definitionHtml;
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
function ajouterListenerDefinitions() {
|
|
|
130 |
$('.definition_term').live('mouseover mouseout', function(event) {
|
|
|
131 |
if (event.type == 'mouseover') {
|
|
|
132 |
event.preventDefault();
|
|
|
133 |
afficherDefinition($(this));
|
|
|
134 |
} else {
|
|
|
135 |
cacherPopupsDefinitions();
|
|
|
136 |
}
|
|
|
137 |
});
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
function afficherDefinition(element) {
|
|
|
141 |
mot = element.attr('rel');
|
|
|
142 |
if(dictionnaire[mot] != null) {
|
| 3 |
aurelien |
143 |
element.after(formaterDefinition(element));
|
| 1 |
aurelien |
144 |
afficherPopupDefinition();
|
|
|
145 |
} else {
|
|
|
146 |
chargerDefinitionDistante(element);
|
|
|
147 |
}
|
|
|
148 |
}
|
|
|
149 |
|
| 3 |
aurelien |
150 |
function chargerDefinitionDistante(element) {
|
|
|
151 |
date = new Date();
|
|
|
152 |
tempsDepuisRequeteAjax = date.getTime();
|
| 1 |
aurelien |
153 |
$.ajax({
|
|
|
154 |
url: getUrlBaseService()+'def/'+mot,
|
|
|
155 |
success: function(data) {
|
|
|
156 |
retour = data;
|
|
|
157 |
definition = retour.valeur;
|
|
|
158 |
dictionnaire[mot] = definition;
|
| 3 |
aurelien |
159 |
element.after(formaterDefinition(element));
|
| 1 |
aurelien |
160 |
afficherPopupDefinition();
|
|
|
161 |
},
|
|
|
162 |
dataType: "JSON",
|
|
|
163 |
global: false
|
|
|
164 |
});
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
function afficherPopupDefinition() {
|
|
|
168 |
$(".definition_container").css({'top':mouseY + 20,'left':mouseX - 10}).fadeIn('slow');
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
function cacherPopupsDefinitions() {
|
|
|
172 |
$(".definition_container").remove();
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
function formaterDefinition(element) {
|
|
|
176 |
mot = element.attr('rel');
|
|
|
177 |
data = dictionnaire[mot];
|
|
|
178 |
defHtml = '<div class="definition_container">'+
|
|
|
179 |
'<span class="definition_container_fleche"></span>'+
|
| 3 |
aurelien |
180 |
'<span class="definition">'+data+'</span>'+
|
| 1 |
aurelien |
181 |
'</div>';
|
|
|
182 |
return defHtml;
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
function supprimerToutesDefinitions() {
|
|
|
186 |
$('.definition_term').each(function() {
|
|
|
187 |
$(this).replaceWith($(this).html());
|
|
|
188 |
});
|
|
|
189 |
cacherPopupsDefinitions();
|
|
|
190 |
}
|
|
|
191 |
|
|
|
192 |
$(document).bind('mousemove', function(e){
|
|
|
193 |
mouseX = e.pageX;
|
|
|
194 |
mouseY = e.pageY - $(window).scrollTop();
|
|
|
195 |
});
|
|
|
196 |
|
|
|
197 |
$(document).ready(function() {
|
| 3 |
aurelien |
198 |
afficherLienDefinitions();
|
| 1 |
aurelien |
199 |
});
|
|
|
200 |
|