1
0
mirror of https://github.com/moparisthebest/kaiwa synced 2024-11-27 19:52:23 -05:00
kaiwa/clientapp/app.js

170 lines
5.5 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 Backbone = require('backbone');
2015-02-09 09:22:17 -05:00
Backbone.$ = $;
var async = require('async');
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');
2015-03-19 11:41:32 -04:00
var LdapUsers = require('./models/ldapUsers');
2013-08-29 23:38:28 -04:00
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
2015-01-25 14:21:57 -05:00
var SoundEffectManager = require('sound-effect-manager');
2013-08-29 23:38:28 -04:00
module.exports = {
launch: function () {
2015-02-09 09:22:17 -05:00
2013-09-05 19:53:23 -04:00
var self = window.app = this;
var config = localStorage.config;
2013-09-05 19:53:23 -04:00
if (!config) {
console.log('missing config');
window.location = '/login';
2015-02-09 09:22:17 -05:00
return;
2013-09-05 19:53:23 -04:00
}
2013-08-29 23:38:28 -04:00
2015-03-19 11:41:32 -04:00
app.config = JSON.parse(config);
app.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();
2015-01-25 14:21:57 -05:00
app.soundManager = new SoundEffectManager();
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);
2015-02-09 09:27:16 -05:00
app.composing = {};
app.timeInterval = 0;
app.mucInfos = [];
2013-09-05 19:53:23 -04:00
},
function (cb) {
2015-03-19 11:41:32 -04:00
app.storage.profiles.get(app.config.jid, function (err, res) {
if (res) {
profile = res;
2015-03-19 11:41:32 -04:00
profile.jid = {full: app.config.jid, bare: app.config.jid};
app.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
}
};
2015-03-19 11:41:32 -04:00
self.api = window.client = StanzaIO.createClient(app.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
},
2015-01-25 14:21:57 -05:00
function (cb) {
app.soundManager.loadFile('/sounds/ding.wav', 'ding');
app.soundManager.loadFile('/sounds/threetone-alert.wav', 'threetone-alert');
cb();
},
function (cb) {
app.whenConnected(function () {
function getInterval() {
if (client.sessionStarted) {
client.getTime(self.id, function (err, res) {
if (err) return;
self.timeInterval = res.time.utc - Date.now();
});
setTimeout(getInterval, 600000);
}
}
getInterval();
});
cb();
},
2013-09-05 19:53:23 -04:00
function (cb) {
2015-03-20 15:36:40 -04:00
app.whenConnected(function () {
me.publishAvatar();
});
function start() {
// start our router and show the appropriate page
app.history.start({pushState: true, root: '/'});
if (app.history.fragment == '' && SERVER_CONFIG.startup)
app.navigate(SERVER_CONFIG.startup);
cb();
}
2013-09-05 19:53:23 -04:00
new Router();
app.history = Backbone.history;
2015-02-23 12:24:16 -05:00
app.history.on("route", function(route, params) {
app.state.pageChanged = params;
});
2013-09-05 19:53:23 -04:00
self.view = new MainView({
model: app.state,
2013-09-05 19:53:23 -04:00
el: document.body
});
self.view.render();
2015-03-19 11:41:32 -04:00
app.ldapUsers = new LdapUsers();
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);
2015-02-09 10:20:28 -05:00
},
serverConfig: function () {
return SERVER_CONFIG;
2013-08-29 23:38:28 -04:00
}
};
2015-02-09 09:22:17 -05:00
$(function () {
module.exports.launch();
});