Rinser/html/js/app.v3.js

162 lines
3.7 KiB
JavaScript
Raw Normal View History

2016-03-15 11:33:52 +00:00
/**
* Created by Martin on 15/03/2016.
*/
2016-03-15 13:15:41 +00:00
"use strict";
2016-03-15 11:33:52 +00:00
var APP = function () {
2016-03-15 13:15:41 +00:00
var refreshStep = 0,
preUrl = 'https://pipes.silvrtree.co.uk/',
2016-03-15 11:33:52 +00:00
_storage = {
lastupdated: null,
feeds: {}
},
_list = null,
_feed = null,
2016-07-05 10:18:38 +00:00
feeds = ['paleo', 'lifestyle', 'tech', 'news', 'fit', 'bored', 'jobs-local'],
2016-03-15 11:33:52 +00:00
lastUpdated = null,
preCache = function () {
2016-03-15 13:15:41 +00:00
_list = new EJS({url: 'ejs/list.ejs'});
2016-03-15 11:33:52 +00:00
_feed = new EJS({
url: 'ejs/testcards.ejs'
});
},
getLastUpdateDate = function () {
var formatted, dt;
// this.lastUpdated = localStorage.getItem('lastUpdated') || null;
2016-03-15 13:15:41 +00:00
if (lastUpdated === null) {
2016-03-15 11:33:52 +00:00
$('#lastupdate').empty().append('Never');
2016-03-15 13:15:41 +00:00
}
2016-03-15 11:33:52 +00:00
else {
dt = new Date(lastUpdated);
formatted = moment(dt).startOf('minute').fromNow();
console.log(formatted);
$('#lastupdate').empty().append(formatted);
}
},
2016-03-15 13:15:41 +00:00
doSave = function () {
console.log('Saving...');
lastUpdated = new Date();
_storage.lastupdated = lastUpdated;
localStorage.setItem('lastUpdated', lastUpdated);
localStorage.setItem('_storage', JSON.stringify(_storage));
getLastUpdateDate();
// $.zprogress.done(true);
2016-03-15 11:33:52 +00:00
},
2016-03-15 13:15:41 +00:00
2016-03-15 11:33:52 +00:00
doRefresh = function () {
var self = this;
if (refreshStep < feeds.length) {
var feedUrl = preUrl + feeds[refreshStep] + '.json';
$('#lastupdate').empty().append('Refreshing: ' + feeds[refreshStep]);
$.getJSON(feedUrl, function (data) {
2016-03-15 14:36:04 +00:00
// console.log(self);
2016-03-15 11:33:52 +00:00
2016-03-15 14:36:04 +00:00
doUpdate(data);
2016-03-15 11:33:52 +00:00
});
} else {
console.log('Done');
// $.zprogress.inc(0.2);
doSave();
}
},
2016-03-15 13:15:41 +00:00
showFeed = function (opt) {
2016-03-16 13:43:57 +00:00
var output, d, $bodyContents ;
$bodyContents = $('#bodyContents');
2016-03-15 13:15:41 +00:00
console.log('show feed ' + opt);
// $('#feedcontent').empty();
d = {
d: _storage.feeds[opt]
};
output = _feed.render(d);
2016-03-16 13:43:57 +00:00
$bodyContents.empty();
$('BODY').scrollTop(0);
$bodyContents.html(output);
2016-03-16 13:46:17 +00:00
2016-03-15 13:15:41 +00:00
$('img').unveil();
},
2016-03-15 11:33:52 +00:00
doUpdate = function (data) {
console.log(this);
console.log(data);
_storage.feeds[feeds[refreshStep]] = data;
console.log(_storage);
refreshStep++;
// $.zprogress.inc(0.2);
doRefresh();
},
showList = function () {
2016-03-15 13:15:41 +00:00
var key, output, d = {},
2016-03-15 11:33:52 +00:00
list = [];
2016-03-15 13:15:41 +00:00
for (key in _storage.feeds) {
2016-03-15 11:33:52 +00:00
console.log(key);
list.push({
name: key
});
}
d.list = list;
2016-03-15 13:15:41 +00:00
output = _list.render(d);
2016-03-15 11:33:52 +00:00
$('#listContainer').empty().html(output);
2016-03-15 13:15:41 +00:00
for (key in _storage.feeds) {
2016-03-15 11:33:52 +00:00
$('#' + key).on('click', $.proxy(showFeed, this, key));
}
},
2016-03-15 13:15:41 +00:00
doLoad = function () {
2016-03-15 11:33:52 +00:00
2016-03-15 13:15:41 +00:00
var _load = localStorage.getItem('_storage');
$('#lastupdate').empty().append('Loading...');
if (_load !== null) {
_storage = JSON.parse(_load);
}
else {
_storage = {
lastupdated: null,
feeds: {}
};
}
lastUpdated = _storage.lastupdated || null;
showList();
},
refresh = function () {
refreshStep = 0;
console.log('refresh');
console.log(this);
console.log('get ' + feeds[refreshStep]);
$('#lastupdate').empty().append('Refreshing...');
// $.zprogress.start();
doRefresh();
}, init = function () {
$('#fnRefresh').on('click', $.proxy(refresh, this));
console.log('app starting...');
preCache();
doLoad();
getLastUpdateDate();
console.log('Last updated: ' + lastUpdated);
} ;
2016-03-15 11:33:52 +00:00
$('#fnRefresh').on('click', function () {
doLoad();
});
init();
};
MicroEvent.mixin(APP);
var app = new APP();