1
0
mirror of https://github.com/moparisthebest/kaiwa synced 2024-11-14 21:45:08 -05:00
kaiwa/clientapp/app.js

109 lines
3.2 KiB
JavaScript
Raw Normal View History

2013-09-27 01:52:54 -04:00
/*global $, app, me, client*/
2013-08-29 23:38:28 -04:00
"use strict";
var _ = require('underscore');
var async = require('async');
var Backbone = require('backbone');
2013-09-27 01:52:54 -04:00
var StanzaIO = require('stanza.io');
var AppState = require('./models/state');
2013-08-29 23:38:28 -04:00
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-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) {
app.state = new AppState();
app.me = window.me = new MeModel();
2013-08-29 23:38:28 -04:00
2013-09-17 14:25:33 -04:00
window.onbeforeunload = function () {
if (app.api.sessionStarted) {
app.api.disconnect();
2013-09-17 14:25:33 -04:00
}
};
2013-09-27 01:52:54 -04:00
self.api = window.client = StanzaIO.createClient(config);
2013-09-05 19:53:23 -04:00
xmppEventHandlers(self.api, self);
2013-08-29 23:38:28 -04:00
2013-09-05 19:53:23 -04:00
self.api.once('session:started', function () {
app.state.hasConnected = true;
2013-08-29 23:38:28 -04:00
cb();
2013-09-05 19:53:23 -04:00
});
self.api.connect();
2013-09-05 19:53:23 -04:00
},
function (cb) {
new Router();
app.history = Backbone.history;
self.view = new MainView({
model: app.state,
2013-09-05 19:53:23 -04:00
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.state.markActive();
2013-08-29 23:38:28 -04:00
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();