kaiwa/clientapp/app.js

113 lines
3.2 KiB
JavaScript
Raw Normal View History

2013-08-29 23:38:28 -04:00
/*global $, app, me, client, XMPP*/
"use strict";
var _ = require('underscore');
var async = require('async');
var Backbone = require('backbone');
var MeModel = require('./models/me');
var MainView = require('./views/main');
var Router = require('./router');
var Storage = require('./storage');
var xmppEventHandlers = require('./helpers/xmppEventHandlers');
2013-09-12 14:18:44 -04:00
var notifier = require('./helpers/notifications');
2013-08-29 23:38:28 -04:00
module.exports = {
launch: function () {
2013-09-05 19:53:23 -04:00
var self = window.app = this;
var config = localStorage.config;
2013-09-12 14:18:44 -04:00
self.notifier = notifier;
2013-08-29 23:38:28 -04:00
2013-09-05 19:53:23 -04:00
if (!config) {
console.log('missing config');
window.location = '/login';
}
2013-08-29 23:38:28 -04:00
2013-09-17 14:22:42 -04:00
me.hasFocus = false;
$(window).blur(function () {
2013-09-17 14:22:42 -04:00
me.hasFocus = false;
});
$(window).focus(function () {
2013-09-17 14:22:42 -04:00
me.hasFocus = true;
});
2013-09-16 14:16:47 -04:00
window.onbeforeunload = function () {
if (client.sessionStarted) {
client.disconnect();
return "End active session?";
}
2013-09-16 14:16:47 -04:00
};
2013-09-05 19:53:23 -04:00
config = JSON.parse(config);
2013-08-29 23:38:28 -04:00
2013-09-05 19:53:23 -04:00
_.extend(this, Backbone.Events);
2013-08-29 23:38:28 -04:00
2013-09-05 19:53:23 -04:00
async.series([
function (cb) {
app.storage = new Storage();
app.storage.open(cb);
},
function (cb) {
app.storage.rosterver.get(config.jid, function (err, ver) {
if (ver) {
config.rosterVer = ver;
}
cb();
});
},
function (cb) {
window.me = new MeModel();
2013-08-29 23:38:28 -04:00
2013-09-05 19:53:23 -04:00
self.api = window.client = XMPP.createClient(config);
xmppEventHandlers(self.api, self);
2013-08-29 23:38:28 -04:00
2013-09-05 19:53:23 -04:00
self.api.connect();
2013-08-29 23:38:28 -04:00
2013-09-05 19:53:23 -04:00
self.api.once('session:started', function () {
app.hasConnected = true;
2013-08-29 23:38:28 -04:00
cb();
2013-09-05 19:53:23 -04:00
});
},
function (cb) {
new Router();
app.history = Backbone.history;
self.view = new MainView({
model: me,
el: document.body
});
self.view.render();
// we have what we need, we can now start our router and show the appropriate page
app.history.start({pushState: true, root: '/'});
cb();
}
]);
2013-08-29 23:38:28 -04:00
},
whenConnected: function (func) {
2013-09-05 19:53:23 -04:00
if (app.api.sessionStarted) {
2013-08-29 23:38:28 -04:00
func();
} else {
2013-09-05 19:53:23 -04:00
app.api.once('session:started', func);
2013-08-29 23:38:28 -04:00
}
},
navigate: function (page) {
var url = (page.charAt(0) === '/') ? page.slice(1) : page;
app.history.navigate(url, true);
},
renderPage: function (view, animation) {
var container = $('#pages');
if (app.currentPage) {
app.currentPage.hide(animation);
}
// we call render, but if animation is none, we want to tell the view
// to start with the active class already before appending to DOM.
container.append(view.render(animation === 'none').el);
view.show(animation);
}
};
module.exports.launch();