Subversion Repositories Applications.dictionnaire

Rev

Rev 3 | Rev 6 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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