Subversion Repositories Applications.dictionnaire

Rev

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

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