-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
78 lines (65 loc) · 2.1 KB
/
script.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
$(document).ready(function() {
var languageOptions = {
fr: {
buttons: [{fr:'Français'}, {en:'Anglais'}],
text: 'Vive la révolution!',
cta: 'Voir la source du référentiel Github.'
},
en: {
buttons: [{fr:'French'}, {en:'English'}],
text: 'Long live the revolution!',
cta: 'View source for Github repository.'
}
};
var defaultLanguage = Object.keys(languageOptions)[0];
var selectedLanguage = getLangFromSearch() || defaultLanguage;
var body = $('body');
var stars = body.append('<div class="stars" />');
var mainHeading = $('h1');
var languageOptionContainer = $('div.languages');
var viewSourceMessage = $('small');
var languageButtons = $('a.lang');
var viewSourceTimer = null;
viewSourceMessage.fadeOut(0);
setLanguage();
$(document).on('mousemove', function(event) {
if(viewSourceTimer) {
clearTimeout(viewSourceTimer);
}
viewSourceMessage.fadeIn(300);
viewSourceTimer = setTimeout(function() {
viewSourceMessage.fadeOut(300);
clearTimeout(viewSourceTimer);
}, 3000);
});
function getLangFromSearch() {
var url = new URL(location.href);
var validLangs = Object.keys(languageOptions);
var lang = url.searchParams.get('lang');
if(validLangs.includes(lang)) {
return lang;
}
return null;
}
function setLanguage() {
var selectedLang = languageOptions[selectedLanguage];
body.removeClass().addClass(selectedLanguage);
mainHeading.html(selectedLang.text);
viewSourceMessage.html(selectedLang.cta);
languageOptionContainer.empty();
selectedLang.buttons.forEach(function(buttonName) {
var link = $('<a />');
var newSelectedLanguage = Object.keys(buttonName)[0];
link.appendTo(languageOptionContainer)
.html(buttonName[Object.keys(buttonName)[0]])
.attr('href', 'javascript:void(0)')
.on('click', function() {
selectedLanguage = newSelectedLanguage;
setLanguage();
});
if(selectedLanguage === newSelectedLanguage) {
link.addClass('active');
}
});
}
});