Rinser/html/js/app.prod.js
2015-08-12 13:04:17 +01:00

147 lines
3.9 KiB
JavaScript

var APP = {
refreshStep: 0,
preUrl: '/',
_storage: {
lastupdated: null,
feeds: {}
},
_list : null,
_feed : null,
feeds: ['paleo', 'lifestyle', 'tech'],
lastUpdated: null,
preCache: function() {
this._list = new EJS({url: 'ejs/list.ejs'});
this._feed = new EJS({
url: 'ejs/testcards.ejs'
});
},
init: function() {
$('#refresh').on('click', $.proxy(this.refresh, this));
console.log('app starting...');
this.preCache();
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...');
$.zprogress.start();
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');
$.zprogress.inc(0.2);
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++;
$.zprogress.inc(0.2);
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();
$.zprogress.done(true);
},
doLoad: function() {
var _load = localStorage.getItem('_storage');
$('#lastupdate').empty().append('Loading...');
if (_load != null)
this._storage = JSON.parse(_load);
else
this._storage = {
lastupdated: null,
feeds: {}
};
this.lastUpdated = this._storage.lastupdated || null;
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 = this._list.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 = this._feed.render(d);
$('#feedcontent').append(output);
$("img").unveil();
}
};
Zepto(function($) {
console.log('Start app');
if (typeof(Storage) !== "undefined") {
APP.init();
} else {
// Sorry! No Web Storage support..
alert('No local storage');
}
});