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

127 lines
3.8 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');
var pushNotifications = require('./helpers/pushNotifications');
2013-10-11 18:40:42 -04:00
var Notify = require('notify.js');
2013-10-14 16:36:19 -04:00
var Desktop = require('./helpers/desktop');
var AppCache = require('./helpers/cache');
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-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-11-18 16:45:09 -05:00
config.useStreamManagement = true;
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
var profile = {};
2013-09-05 19:53:23 -04:00
async.series([
function (cb) {
2013-10-11 18:40:42 -04:00
app.notifications = new Notify();
2013-10-14 16:36:19 -04:00
app.desktop = new Desktop();
app.cache = new AppCache();
2013-09-05 19:53:23 -04:00
app.storage = new Storage();
app.storage.open(cb);
},
function (cb) {
app.storage.profiles.get(config.jid, function (err, res) {
if (res) {
profile = res;
profile.jid = {full: config.jid, bare: config.jid};
config.rosterVer = res.rosterVer;
2013-09-05 19:53:23 -04:00
}
cb();
});
},
function (cb) {
app.state = new AppState();
app.me = window.me = new MeModel(profile);
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);
client.use(pushNotifications);
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) {
function start() {
// start our router and show the appropriate page
app.history.start({pushState: true, root: '/'});
cb();
}
2013-09-05 19:53:23 -04:00
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();
if (me.contacts.length) {
start();
} else {
me.contacts.once('loaded', start);
}
2013-09-05 19:53:23 -04:00
}
]);
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();