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');
|
|
|
|
|
2013-09-26 23:19:46 -04:00
|
|
|
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');
|
2014-01-23 19:34:21 -05:00
|
|
|
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');
|
2014-01-07 01:01:17 -05:00
|
|
|
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-09-26 23:19:46 -04:00
|
|
|
|
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-10-18 10:17:13 -04:00
|
|
|
config.resource = 'otalk';
|
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-10-15 17:48:39 -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();
|
2014-01-07 01:01:17 -05:00
|
|
|
app.cache = new AppCache();
|
2013-09-05 19:53:23 -04:00
|
|
|
app.storage = new Storage();
|
|
|
|
app.storage.open(cb);
|
|
|
|
},
|
|
|
|
function (cb) {
|
2013-10-15 17:48:39 -04:00
|
|
|
app.storage.profiles.get(config.jid, function (err, res) {
|
|
|
|
if (res) {
|
|
|
|
profile = res;
|
2014-01-05 07:19:46 -05:00
|
|
|
profile.jid = {full: config.jid, bare: config.jid};
|
2013-10-15 17:48:39 -04:00
|
|
|
config.rosterVer = res.rosterVer;
|
2013-09-05 19:53:23 -04:00
|
|
|
}
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function (cb) {
|
2013-09-26 23:19:46 -04:00
|
|
|
app.state = new AppState();
|
2013-10-15 17:48:39 -04:00
|
|
|
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 () {
|
2013-09-26 23:19:46 -04:00
|
|
|
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);
|
2014-01-23 19:34:21 -05:00
|
|
|
client.use(pushNotifications);
|
2013-09-05 19:53:23 -04:00
|
|
|
xmppEventHandlers(self.api, self);
|
2013-08-29 23:38:28 -04:00
|
|
|
|
2013-12-17 01:22:53 -05:00
|
|
|
if (self.api.jingle.capabilities.length > 1) {
|
|
|
|
self.api.jingle.startLocalMedia();
|
|
|
|
}
|
2013-10-16 16:02:03 -04:00
|
|
|
|
2013-09-05 19:53:23 -04:00
|
|
|
self.api.once('session:started', function () {
|
2013-09-26 23:19:46 -04:00
|
|
|
app.state.hasConnected = true;
|
2013-08-29 23:38:28 -04:00
|
|
|
cb();
|
2013-09-05 19:53:23 -04:00
|
|
|
});
|
2013-09-26 23:19:46 -04:00
|
|
|
self.api.connect();
|
2013-09-05 19:53:23 -04:00
|
|
|
},
|
|
|
|
function (cb) {
|
2013-10-14 19:45:31 -04:00
|
|
|
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({
|
2013-09-26 23:19:46 -04:00
|
|
|
model: app.state,
|
2013-09-05 19:53:23 -04:00
|
|
|
el: document.body
|
|
|
|
});
|
|
|
|
self.view.render();
|
|
|
|
|
2013-10-14 19:45:31 -04:00
|
|
|
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;
|
2013-09-26 23:19:46 -04:00
|
|
|
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();
|