Rinser/html/js/app.prod.js

147 lines
3.9 KiB
JavaScript
Raw Normal View History

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