Subversion Repositories Applications.dictionnaire

Rev

Rev 3 | Go to most recent revision | Details | Last modification | View Log | RSS feed

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