-
Notifications
You must be signed in to change notification settings - Fork 1
/
the.js
230 lines (172 loc) · 5.38 KB
/
the.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
var DEFAULT_LOCALE = 'fr';
var localStorageLocale = '';
var HEMISPHERE_GREEN = '#8ACDA3';
var currentLocale = '';
var urlParamsHM = {};
var NO_LOCAL_STRG = false;
$("document").ready(function (){
start();
});
function start(){
// before anything else we test the availables features :
if(!Modernizr.canvas){
console.log('no canvas');
}
if(!Modernizr.localstorage){
console.log('no localStorage');
NO_LOCAL_STRG = true;
}
var localStorageLocale = localStorage.getItem('hemisp_lng');
if((localStorageLocale === undefined) || (localStorageLocale === null))
$('#menu_opts').load('menu_'+DEFAULT_LOCALE+'.html');
else
$('#menu_opts').load('menu_'+localStorageLocale+'.html');
$(window).bind('hashchange',onHashChange);
$(window).hashchange();
}
function menuClick(event){
event.preventDefault();
//loadContent(event.currentTarget.href);
trimUSCandSCParams();
var urlParamsString = urlParamsHMToString();
if(urlParamsString != '')
urlParamsString = '?'+urlParamsString;
//console.log("***FF DEBUG*** ",event.currentTarget.href," ",event.currentTarget.baseURI.length," ",urlParamsString);
//window.location.hash = '!'+event.currentTarget.href.substring(event.currentTarget.baseURI.length)+urlParamsString;
if(window.location.href.indexOf("#!")!=-1)
window.location.hash = '!'+event.currentTarget.href.substring(window.location.href.indexOf("#!"))+urlParamsString;
else
window.location.hash = '!'+event.currentTarget.href.substring(window.location.href.length)+urlParamsString;
}
function langToggleClick(event){
event.preventDefault();
switch (event.currentTarget.id) {
case 'langToggle_fr' :
setLocale('fr');
break;
case 'langToggle_en' :
setLocale('en');
break;
default:
console.log('err#1 unexpected langToggle id');
break;
}
}
//change the url's locale parameter according to the input value
function setLocale(value){
if(!NO_LOCAL_STRG){
localStorage.hemisp_lng = value;
}
urlParamsHM['lng'] = value;
updateParamsInURL();
}
// takes the urlParamsHM HashMap and write the params in the url
function updateParamsInURL(){
var paramsIndex = window.location.href.indexOf('?')+1;
var paramsString = urlParamsHMToString();
if(paramsIndex != 0){// we already have params in the url
window.location.href = window.location.href.substring(0,paramsIndex)+paramsString;
}else{//we don't have any params in the url
var hashIndex = window.location.href.indexOf('#!');
if(hashIndex != -1){//we have a hashbang in the url
window.location.href += '?'+paramsString;
}else{// we don't have any hashbang in the url
window.location.href += '#!?'+paramsString;
}
}
}
function urlParamsHMToString(){
var string = '';
for(var param in urlParamsHM){
string += param + '=' + urlParamsHM[param] + '&';
}
string = string.substring(0,string.length - 1);
return string;
}
function onHashChange(){
//console.log(window.location.hash);
urlParamsHM = extractUrlParams();
updateLangIfRequiered();
loadContent(window.location.href);
}
function trimUSCandSCParams(){
if(urlParamsHM['usc']){
delete urlParamsHM['usc'];
//updateParamsInURL();
//return;
}
if(urlParamsHM['sc']){
delete urlParamsHM['sc'];
//updateParamsInURL();
//return;
}
}
// loadContent is designed to load content inside the #content element
function loadContent(url){
//console.log(url);
var hashPos = url.indexOf('#!');
if(hashPos != -1){
if((url.indexOf('?') !=-1 && url.indexOf('?') == hashPos+2) || url.length <= hashPos+2){
// in this case we have nohin to load in #content
$("#content").html('');
return;
}
var newUrl = url.substring(0,hashPos) + url.substring(hashPos+2);
url = newUrl;
}else{
//nohin to load in #content
$("#content").html('');
return;
}
if(!NO_LOCAL_STRG && !urlParamsHM['lng'] && localStorage.hemisp_lng){ //if we have a lng stored locally and the url doesn't specify a lng
// we add the lng to the url for this request
if(url.indexOf('?') != -1){// we already have params
url += '&lng='+localStorage.hemisp_lng;
}else{// we have no preexisting params
url += '?lng='+localStorage.hemisp_lng;
}
}
$("#content").load(url);
}
function updateLangIfRequiered(){
if(urlParamsHM['lng']){
if(currentLocale != urlParamsHM['lng']){
currentLocale = urlParamsHM['lng'];
$('#menu_opts').load('menu_'+currentLocale+'.html');
switch (currentLocale){
case 'en':
$('#langToggle_en').css('color',HEMISPHERE_GREEN);
$('#langToggle_fr').css('color','');
break;
case 'fr':
$('#langToggle_fr').css('color',HEMISPHERE_GREEN);
$('#langToggle_en').css('color','');
break;
default:
console.log('err#2 locale not supported');
}
}
}else{
$('#langToggle_fr').css('color','');
$('#langToggle_en').css('color','');
}
}
//toggleFolded is called to fold and unfold subcontent
function toggleFold(elmnt){
$(elmnt).next(".subcontent").slideToggle(300, function() {
// Animation complete.
});
}
// extractUrlParams takes all url variables and puts them in an hashmap (object)
// we can't use here window.location.search because of the #!
function extractUrlParams(){
var t = [];
if(window.location.href.indexOf('?') != -1)
t = window.location.href.substring(window.location.href.indexOf('?')+1).split('&');
var f = {};
for (var i=0; i<t.length; i++){
var x = t[ i ].split('=');
f[x[0]]=x[1];
}
return f;
}