-
Notifications
You must be signed in to change notification settings - Fork 194
/
index.js
147 lines (127 loc) · 3.65 KB
/
index.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
var Promise = require('es6-promise').Promise;
var flickr = require('./flickr');
var photosTemplate = require('./views/photos.hbs');
var utils = require('./utils');
// force https
if ((!location.port || location.port == "80") && location.protocol != 'https:') {
location.protocol = 'https:';
}
var photosEl = document.querySelector('.photos');
var refreshButton = document.querySelector('button.refresh');
var msgEl = document.querySelector('.msg-container');
var msgContentEl = document.querySelector('.msg');
var photoIDsDisplayed = null;
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/trained-to-thrill/sw.js', {
scope: '/trained-to-thrill/'
}).then(function(reg) {
console.log('◕‿◕', reg);
}, function(err) {
console.log('ಠ_ಠ', err);
});
}
function showSpinner(data) {
refreshButton.classList.add('loading');
}
function hideSpinner(data) {
refreshButton.classList.remove('loading');
}
function updatePage(data) {
var scrollHeight;
if (photoIDsDisplayed) {
scrollHeight = photosEl.scrollHeight;
data = data.filter(function(photo) {
if (photoIDsDisplayed.indexOf(photo.id) == -1) {
photoIDsDisplayed.push(photo.id);
return true;
}
return false;
});
photosEl.insertBefore(utils.strToEls(photosTemplate(data)), photosEl.firstChild);
photosEl.scrollTop += photosEl.scrollHeight - scrollHeight;
}
else {
photoIDsDisplayed = data.map(function(p) { return p.id; });
photosEl.insertBefore(utils.strToEls(photosTemplate(data)), photosEl.firstChild);
}
}
function getTrainPhotoData() {
return flickr.search('train station', {
headers: {}
}).catch(function() {
return null;
});
}
function getCachedTrainPhotoData() {
if ('serviceWorker' in navigator && navigator.serviceWorker.controller) {
return flickr.search('train station', {
headers: {'Accept': 'x-cache/only'}
}).catch(function() {
return null;
});
}
else {
return Promise.resolve(null);
}
}
function showMessage(msg, duration) {
msgContentEl.textContent = msg;
msgEl.style.display = 'block';
msgEl.offsetWidth;
msgEl.classList.add('show');
setTimeout(function() {
msgEl.classList.remove('show');
}, duration);
}
function showConnectionError() {
showMessage("Connectivity derailed!", 5000);
}
// Refresh button
refreshButton.addEventListener('click', function(event) {
this.blur();
event.preventDefault();
showSpinner();
getTrainPhotoData().then(function(data) {
var oldLen = photoIDsDisplayed && photoIDsDisplayed.length;
updatePage(data);
if (oldLen != photoIDsDisplayed.length) {
photosEl.scrollTop = 0;
}
}).catch(showConnectionError).then(hideSpinner);
});
// Initial load
var liveDataFetched = getTrainPhotoData().then(function(data) {
if (data) {
var alreadyRendered = !!photoIDsDisplayed;
var oldLen = photoIDsDisplayed && photoIDsDisplayed.length;
updatePage(data);
if (alreadyRendered && oldLen != photoIDsDisplayed.length) {
showMessage("▲ New trains ▲", 3000);
}
return true;
}
return false;
});
var cachedDataFetched = getCachedTrainPhotoData().then(function(data) {
if (data) {
if (!photoIDsDisplayed) {
updatePage(data);
}
return true;
}
return false;
});
liveDataFetched.then(function(fetched) {
return fetched || cachedDataFetched;
}).then(function(dataFetched) {
if (!dataFetched) {
showConnectionError();
}
hideSpinner();
});
// Add classes to fade-in images
document.addEventListener('load', function(event) {
if (event.target.classList.contains('main-photo-img')) {
event.target.parentNode.classList.add('loaded');
}
}, true);