diff --git a/src/index.html b/src/index.html
index 27ebcaa..549412a 100755
--- a/src/index.html
+++ b/src/index.html
@@ -39,6 +39,7 @@
+
diff --git a/src/js/app-config.js b/src/js/app-config.js
index 67e591f..5b1e1a7 100644
--- a/src/js/app-config.js
+++ b/src/js/app-config.js
@@ -30,8 +30,28 @@ var app; // container for the application namespace
*/
app.util.tpl = {
templates: {},
+
get: function(name) {
return this.templates[name];
+ },
+
+ loadTemplates: function(names, callback) {
+ var that = this;
+
+ var loadTemplate = function(index) {
+ var name = names[index];
+ console.log('Loading template: ' + name);
+ $.get('tpl/' + name + '.html', function(data) {
+ that.templates[name] = data;
+ index++;
+ if (index < names.length) {
+ loadTemplate(index);
+ } else {
+ callback();
+ }
+ });
+ };
+ loadTemplate(0);
}
};
diff --git a/src/js/app-controller.js b/src/js/app-controller.js
new file mode 100644
index 0000000..f2c27d7
--- /dev/null
+++ b/src/js/app-controller.js
@@ -0,0 +1,98 @@
+/**
+ * The main application controller
+ */
+app.Controller = function() {
+ 'use strict';
+
+ var emailDao; // local variable for the main email data access object
+
+ /**
+ * Initializes modules through dependecy injection
+ */
+ this.init = function(callback) {
+ var util = new cryptoLib.Util(window, uuid);
+ var crypto = new app.crypto.Crypto(window, util);
+ var cloudstorage = new app.dao.CloudStorage(window, $);
+ var jsonDao = new app.dao.LawnchairDAO(Lawnchair);
+ var devicestorage = new app.dao.DeviceStorage(util, crypto, jsonDao, null);
+ var keychain = new app.dao.KeychainDAO(jsonDao, cloudstorage);
+ emailDao = new app.dao.EmailDAO(jsonDao, crypto, devicestorage, cloudstorage, util, keychain);
+
+ callback();
+ };
+
+ /**
+ * Executes a number of commands
+ */
+ this.execute = function(cmd, args, callback) {
+ if (cmd === 'login') {
+ // login user
+ login(args.userId, args.password, function(err) {
+ callback({
+ err: err
+ });
+ });
+
+ } else if (cmd === 'syncEmails') {
+ // list emails from folder
+ emailDao.syncFromCloud(args.folder, function(err) {
+ callback({
+ err: err
+ });
+ });
+
+ } else if (cmd === 'listEmails') {
+ // list emails from folder
+ emailDao.listItems(args.folder, args.offset, args.num, function(err, collection) {
+ callback({
+ err: err,
+ collection: collection.toJSON()
+ });
+ });
+
+ } else if (cmd === 'getEmail') {
+ // list emails from folder
+ var mail = emailDao.getItem(args.folder, args.messageId);
+ callback({
+ err: null,
+ email: mail.toJSON()
+ });
+
+ } else if (cmd === 'sendEmail') {
+ // list emails from folder
+ sendEmail(args.email, function(err) {
+ callback({
+ err: err
+ });
+ });
+
+ } else {
+ // error: invalid message from sandbox
+ callback({
+ err: {
+ errMsg: 'Invalid message posted from sandbox!'
+ }
+ });
+ }
+ };
+
+ //
+ // Helper methods
+ //
+
+ function login(userId, password, callback) {
+ var account = new app.model.Account({
+ emailAddress: userId,
+ symKeySize: app.config.symKeySize,
+ symIvSize: app.config.symIvSize,
+ asymKeySize: app.config.asymKeySize
+ });
+ emailDao.init(account, password, callback);
+ }
+
+ function sendEmail(email, callback) {
+ var em = new app.model.Email(email);
+ emailDao.sendEmail(em, callback);
+ }
+
+};
\ No newline at end of file
diff --git a/src/js/window-loader.js b/src/js/window-loader.js
index 49862b7..fb2c39d 100644
--- a/src/js/window-loader.js
+++ b/src/js/window-loader.js
@@ -1,29 +1,14 @@
(function() {
'use strict';
- var emailDao; // local variable for main DAO
-
- /**
- * The Template Loader. Used to asynchronously load templates located in separate .html files
- */
- app.util.tpl.loadTemplates = function(names, callback) {
- var that = this;
-
- var loadTemplate = function(index) {
- var name = names[index];
- console.log('Loading template: ' + name);
- $.get('tpl/' + name + '.html', function(data) {
- that.templates[name] = data;
- index++;
- if (index < names.length) {
- loadTemplate(index);
- } else {
- callback();
- }
- });
- };
- loadTemplate(0);
- };
+ var views = [
+ 'login',
+ 'compose',
+ 'folderlist',
+ 'messagelist',
+ 'messagelistitem',
+ 'read'
+ ];
/**
* Load templates and start the application
@@ -43,39 +28,22 @@
function onDeviceReady() {
console.log('Starting in Browser: ' + isBrowser);
- app.util.tpl.loadTemplates([
- 'login',
- 'compose',
- 'folderlist',
- 'messagelist',
- 'messagelistitem',
- 'read'
- ], startApp);
+ app.util.tpl.loadTemplates(views, startApp);
}
});
- function initDAO() {
- var util = new cryptoLib.Util(window, uuid);
- var crypto = new app.crypto.Crypto(window, util);
- var cloudstorage = new app.dao.CloudStorage(window, $);
- var jsonDao = new app.dao.LawnchairDAO(Lawnchair);
- var devicestorage = new app.dao.DeviceStorage(util, crypto, jsonDao, null);
- var keychain = new app.dao.KeychainDAO(jsonDao, cloudstorage);
- emailDao = new app.dao.EmailDAO(jsonDao, crypto, devicestorage, cloudstorage, util, keychain);
- }
-
function startApp() {
- // init email dao and dependencies
- initDAO();
// sandboxed ui in iframe
var sandbox = document.getElementById('sandboxFrame').contentWindow;
+ var controller = new app.Controller();
// set global listener for events from sandbox
window.onmessage = function(e) {
var cmd = e.data.cmd;
var args = e.data.args;
+
// handle the workload in the main window
- handleCommand(cmd, args, function(resArgs) {
+ controller.execute(cmd, args, function(resArgs) {
// send reponse to sandbox
sandbox.postMessage({
cmd: cmd,
@@ -84,78 +52,14 @@
});
};
- // init sandbox ui
- sandbox.postMessage({
- cmd: 'init',
- args: app.util.tpl.templates
- }, '*');
- }
-
- function handleCommand(cmd, args, callback) {
- if (cmd === 'login') {
- // login user
- login(args.userId, args.password, function(err) {
- callback({
- err: err
- });
- });
-
- } else if (cmd === 'syncEmails') {
- // list emails from folder
- emailDao.syncFromCloud(args.folder, function(err) {
- callback({
- err: err
- });
- });
-
- } else if (cmd === 'listEmails') {
- // list emails from folder
- emailDao.listItems(args.folder, args.offset, args.num, function(err, collection) {
- callback({
- err: err,
- collection: collection.toJSON()
- });
- });
-
- } else if (cmd === 'getEmail') {
- // list emails from folder
- var mail = emailDao.getItem(args.folder, args.messageId);
- callback({
- err: null,
- email: mail.toJSON()
- });
-
- } else if (cmd === 'sendEmail') {
- // list emails from folder
- sendEmail(args.email, function(err) {
- callback({
- err: err
- });
- });
-
- } else {
- // error: invalid message from sandbox
- callback({
- err: {
- errMsg: 'Invalid message posted from sandbox!'
- }
- });
- }
- }
-
- function login(userId, password, callback) {
- var account = new app.model.Account({
- emailAddress: userId,
- symKeySize: app.config.symKeySize,
- symIvSize: app.config.symIvSize,
- asymKeySize: app.config.asymKeySize
+ // init controller
+ controller.init(function() {
+ // init sandbox ui
+ sandbox.postMessage({
+ cmd: 'init',
+ args: app.util.tpl.templates
+ }, '*');
});
- emailDao.init(account, password, callback);
- }
-
- function sendEmail(email, callback) {
- var em = new app.model.Email(email);
- emailDao.sendEmail(em, callback);
}
}());
\ No newline at end of file