Subversion Repositories Applications.dictionnaire

Rev

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

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