Rinser/html/js/app.js

131 lines
3.5 KiB
JavaScript
Raw Normal View History

2015-07-27 12:49:21 +00:00
var APP = {
refreshStep: 0,
preUrl: '/html/',
_storage: {
lastupdated: null,
feeds: {}
},
feeds: ['paleo','lifestyle'],
lastUpdated: null,
init: function() {
$('#refresh').on('click', $.proxy(this.refresh, this));
console.log('app starting...');
this.doLoad();
this.getLastUpdateDate();
console.log('Last updated: ' + this.lastUpdated);
},
getLastUpdateDate: function() {
var formatted, dt;
// this.lastUpdated = localStorage.getItem('lastUpdated') || null;
if (this.lastUpdated == null)
$('#lastupdate').empty().append('Never');
else
{
dt = new Date(this.lastUpdated);
formatted = moment(dt).startOf('minute').fromNow();
console.log(formatted);
$('#lastupdate').empty().append(formatted);
}
},
refresh: function() {
this.refreshStep = 0;
console.log('refresh');
console.log(this);
console.log('get ' + this.feeds[this.refreshStep]);
$('#lastupdate').empty().append('Refreshing...');
this.doRefresh();
},
doRefresh: function() {
var self = this;
if (this.refreshStep < this.feeds.length) {
var feedUrl = this.preUrl + this.feeds[this.refreshStep] + '.json';
$('#lastupdate').empty().append('Refreshing: ' + this.feeds[this.refreshStep]);
$.getJSON(feedUrl, function(data) {
console.log(self);
self.doUpdate(data);
});
} else {
console.log('Done');
this.doSave();
}
},
doUpdate: function(data) {
console.log(this);
console.log(data);
this._storage.feeds[this.feeds[this.refreshStep]] = data;
console.log(this._storage);
this.refreshStep++;
this.doRefresh();
},
doSave: function() {
console.log('Saving...');
this.lastUpdated = new Date();
this._storage.lastupdated = this.lastUpdated;
localStorage.setItem('lastUpdated',this.lastUpdated);
localStorage.setItem('_storage',JSON.stringify(this._storage));
this.getLastUpdateDate();
},
doLoad: function() {
2015-07-27 13:04:36 +00:00
var _load = localStorage.getItem('_storage');
2015-07-27 12:49:21 +00:00
$('#lastupdate').empty().append('Loading...');
2015-07-27 13:04:36 +00:00
if (_load != null)
this._storage = JSON.parse(_load);
else
this._storage = {lastupdated: null,feeds: {}};
2015-07-27 12:49:21 +00:00
this.lastUpdated = this._storage.lastupdated;
this.showList();
},
showList: function() {
var output,d={},list = [];
for(var key in this._storage.feeds)
{
console.log(key);
list.push({name:key});
}
d.list = list;
var output = new EJS({url: 'ejs/list.ejs'}).render(d);
$('#list').empty().append(output);
for(var key in this._storage.feeds)
{
$('#' + key).on('click', $.proxy(this.showFeed, this, key));
}
},
showFeed: function(opt) {
var output, d;
console.log('show feed ' + opt);
$('#feedcontent').empty();
d = {d:this._storage.feeds[opt]};
var output = new EJS({url: 'ejs/test.ejs'}).render(d);
$('#feedcontent').append(output);
}
};
Zepto(function($) {
console.log('Start app');
if (typeof(Storage) !== "undefined") {
APP.init();
} else {
// Sorry! No Web Storage support..
alert('No local storage');
}
});