Subversion Repositories Applications.dictionnaire

Rev

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

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