/
util.js
120 lines (104 loc) · 3.23 KB
/
util.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
'use strict';
function getSingleElementByClassName(className) {
return document.getElementsByClassName(className)[0];
}
function replaceInnerInClass(className, text) {
var theClass = getSingleElementByClassName(className);
theClass.innerHTML = '';
theClass.appendChild(document.createTextNode(text));
}
function appendTextToClass(className, text) {
getSingleElementByClassName(className).appendChild(document.createTextNode(text));
}
function replaceTextInClass(className, text) {
replaceInnerInClass(className, '');
appendTextToClass(className, text);
}
function getQueryParam(paramName) {
var url = window.location.href;
var queryString = url.split('?');
queryString = queryString[queryString.length-1];
var params = queryString.split('&');
for (var i = 0; i < params.length; i++) {
var splitPair = params[i].split('=');
if (splitPair[0] === paramName) {
return splitPair[1];
}
}
};
function onSignIn (googleUser) {
var profile = googleUser.getBasicProfile();
replaceTextInClass('hs_loggedIn', 'Logged in as ' + profile.ig + ' (Google)');
getSingleElementByClassName('hs_logout').style.display = 'block';
}
function signOut () {
var auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then(function () {
replaceTextInClass('hs_loggedIn', 'Not logged in');
});
getSingleElementByClassName('hs_logout').style.display = 'none';
}
function prettifyTimeAgo(datetime) {
var timeDiff = Date.now().valueOf() - datetime.valueOf();
// milliseconds
if (timeDiff < 1000) {
return ' just now';
}
// seconds
timeDiff = Math.round(timeDiff / 1000);
if (timeDiff < 30) {
return timeDiff + ' secs ago';
}
// minutes
timeDiff = Math.round(timeDiff / 60);
if (timeDiff < 50) {
return timeDiff + ' mins ago';
}
// hours
timeDiff = Math.round(timeDiff / 60);
if (timeDiff < 24) {
return timeDiff + ' hours ago';
}
// days
timeDiff = Math.round(timeDiff / 24);
if (timeDiff < 7) {
return timeDiff + ' days ago';
}
// weeks
timeDiff = Math.round(timeDiff / 7);
if (timeDiff < 4) {
return timeDiff + ' weeks ago';
}
// months
timeDiff = Math.round(timeDiff / 4);
return timeDiff + ' months ago';
}
function googleAuthInit() {
// loads auth2 in order to show logout button if user is signed in
gapi.load('auth2', {
callback: function () {
gapi.auth2.init().then(function() {
if (gapi.auth2.getAuthInstance().isSignedIn.get()) {
getSingleElementByClassName('hs_logout').style.display = 'block';
}
}, function(obj) {
if (obj.error === "idpiframe_initialization_failed") {
console.log('You must replace YOUR-CLIENT-ID-HERE with your client id in stream.html and modal.html');
}
});
}
});
}
function getGeolocation() {
function success(pos) {
let coords = pos.coords;
let lngLat = coords.longitude + ' ' + coords.latitude;
replaceTextInClass('hs_showGeolocation', lngLat);
}
function error(err) {
replaceTextInClass('hs_showGeolocation', 'No location');
alert('Could not get Geolocation. Check your browser location privacy settings.', err);
}
replaceTextInClass('hs_showGeolocation', 'Loading...');
navigator.geolocation.getCurrentPosition(success, error);
}