aboutsummaryrefslogtreecommitdiff
path: root/assets/js/ps-app.js
blob: 7bb4b9c8f853a7cdd83a9ce8f53ac75b7743a02d (plain)
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
(function($) {

    $(document).ready(function() {

	// Make each .media section clickable to reach ./<id>/
	[ 'gfx', 'txt', 'vdo' ].forEach(function(id) {
	    $('#' + id)
		.click(function(e) {
		    window.location = `/${id}/`;
		    return false
		})
		.mouseover(function(e) {
		    e.target.style.cursor = 'pointer'
		    return false
		})
		.mouseout(function(e) {
		    e.target.style.cursor = 'not-allowed'
		    return false
		})
	})
	$('.media').mouseout(function(e) {
	    e.target.style.cursor = 'auto'
	    return false
	})

	// EN/FR switch
	$('footer')
	    .append('<button id="lang-switch" title="Read in English">EN</button>')
	$('#lang-switch').click(function(e) {
	    var lang = $('html').attr('lang')
	    setLang(lang == 'fr' ? 'en' : 'fr')
	    return false
	})

	// Use ?lang=XX if available
	var URLParams = new URLSearchParams(window.location.search);
	if (URLParams.has('lang')) {
	    console.log('Setting lang from param to ' + URLParams.get('lang'))
	    setLang(URLParams.get('lang'))
	} else if (C.get('lang')) {
	    console.log('Setting lang to ' + C.get('lang'))
	    setLang(C.get('lang'))
	}

	// Translate to target lang
	function setLang(lang) {
	    if (lang == 'fr') {
		$('html').attr('lang', lang)
		$('#lang-switch').html('EN').attr('title', 'Read in English')
	    } else {
		lang = 'en'
		$('html').attr('lang', lang)
		$('#lang-switch').html('FR').attr('title', 'Lire en français')
	    }
	    C.set('lang', lang)
	}


    });

    // Basic cookie functions
    var C = {
	set: function(name, value) {
	    // Make the cookie expire at the end of session
	    var cookie = name + '=' + encodeURIComponent(value) + ';path=/;domain=.lesoiseaux.io;secure'
	    document.cookie = cookie
	    return cookie
	},
	get: function(name) {
	    try {
		var val = document.cookie.split(';')[0]
		var key = name + '='
		return (val.indexOf(key) == 0) ? decodeURIComponent(val.substr(key.length)) : null
	    } catch(e) {
		return null
	    }
	},
	has: function(name) {
	    try {
		return (document.cookie.split(';')[0].indexOf(name + '=') == 0)
	    } catch(e) {
		return false
	    }
	}
    }		

})($)