Improve webmail editor

- Fixed handling of saving drafts
- Fixed handling of message type (HTML/plain)
- Added primitive handling of attachments
This commit is contained in:
Francis Lachapelle
2014-12-16 15:47:34 -05:00
parent 27059ad4a8
commit 948a1f1c99
8 changed files with 137 additions and 61 deletions

View File

@@ -128,12 +128,39 @@
}
return mailbox;
};
mailbox = _find(this.mailboxes);
mailbox = _find(this.$mailboxes);
console.debug(mailbox);
console.debug(this.specialMailboxes);
};
/**
* @function $getMailboxByPath
* @memberof Account.prototype
* @desc Recursively find a mailbox using its path
* @returns a promise of the HTTP operation
*/
Account.prototype.$getMailboxByPath = function(path) {
var mailbox = null,
// Recursive find function
_find = function(mailboxes) {
var mailbox = _.find(mailboxes, function(o) {
return o.path == path;
});
if (!mailbox) {
angular.forEach(mailboxes, function(o) {
if (!mailbox && o.children && o.children.length > 0) {
mailbox = _find(o.children);
}
});
}
return mailbox;
};
mailbox = _find(this.$mailboxes);
return mailbox;
};
/**
* @function $newMessage
* @memberof Account.prototype
@@ -146,10 +173,11 @@
message;
// Query account for draft folder and draft UID
Account.$$resource.fetch(this.id, 'compose').then(function(data) {
message = new Account.$Message(data.accountId, data.mailboxPath, data);
Account.$$resource.fetch(this.id.toString(), 'compose').then(function(data) {
Account.$log.debug('New message: ' + JSON.stringify(data, undefined, 2));
message = new Account.$Message(data.accountId, _this.$getMailboxByPath(data.mailboxPath), data);
// Fetch draft initial data
Account.$$resource.fetch(message.id, 'edit').then(function(data) {
Account.$$resource.fetch(message.$absolutePath({asDraft: true}), 'edit').then(function(data) {
Account.$log.debug('New message: ' + JSON.stringify(data, undefined, 2));
message.editable = data;
deferred.resolve(message);

View File

@@ -277,7 +277,7 @@
// Build map of UID <=> index
_this.uidsMap[data.uid] = i;
msgs.push(new Mailbox.$Message(_this.$account.id, _this.path, data));
msgs.push(new Mailbox.$Message(_this.$account.id, _this, data));
return msgs;
}, _this.$messages);

View File

@@ -10,9 +10,9 @@
* @param {string} mailboxPath - an array of the mailbox path components
* @param {object} futureAddressBookData - either an object literal or a promise
*/
function Message(accountId, mailboxPath, futureMessageData) {
function Message(accountId, mailbox, futureMessageData) {
this.accountId = accountId;
this.mailboxPath = mailboxPath;
this.$mailbox = mailbox;
// Data is immediately available
if (typeof futureMessageData.then !== 'function') {
//console.debug(JSON.stringify(futureMessageData, undefined, 2));
@@ -56,7 +56,7 @@
Message.prototype.$absolutePath = function(options) {
var path;
path = _.map(this.mailboxPath.split('/'), function(component) {
path = _.map(this.$mailbox.path.split('/'), function(component) {
return 'folder' + component.asCSSIdentifier();
});
path.splice(0, 0, this.accountId); // insert account ID
@@ -70,6 +70,25 @@
return path.join('/');
};
/**
* @function $setUID
* @memberof Message.prototype
* @desc Change the UID of the message. This happens when saving a draft.
* @param {number} uid - the new message UID
*/
Message.prototype.$setUID = function(uid) {
var oldUID = this.uid || -1;
if (oldUID != uid) {
this.uid = uid;
this.id = this.$absolutePath();
if (oldUID > -1) {
this.$mailbox.uidsMap[uid] = this.$mailbox.uidsMap[oldUID];
this.$mailbox.uidsMap[oldUID] = null;
}
}
};
/**
* @function $formatFullAddresses
* @memberof Message.prototype
@@ -117,20 +136,22 @@
/**
* @function $editableContent
* @memberof Message.prototype
* @desc Fetch the editable message body along with other metadat such as the recipients.
* @desc First, fetch the draft ID that corresponds to the temporary draft object on the SOGo server.
* Secondly, fetch the editable message body along with other metadata such as the recipients.
* @returns the HTML representation of the body
*/
Message.prototype.$editableContent = function() {
var _this = this,
deferred = Message.$q.defer();
Message.$$resource.fetch(this.$absolutePath({asDraft: true}), 'edit').then(function(data) {
Message.$log.debug('editable = ' + JSON.stringify(data, undefined, 2));
_this.editable = data;
deferred.resolve(data.text);
}, function(data) {
deferred.reject();
});
Message.$$resource.fetch(this.id, 'edit').then(function(data) {
angular.extend(_this, data);
Message.$$resource.fetch(_this.$absolutePath({asDraft: true}), 'edit').then(function(data) {
Message.$log.debug('editable = ' + JSON.stringify(data, undefined, 2));
_this.editable = data;
deferred.resolve(data.text);
}, deferred.reject);
}, deferred.reject);
return deferred.promise;
};
@@ -156,7 +177,8 @@
* @returns a promise of the HTTP operation
*/
Message.prototype.$save = function() {
var data = this.editable;
var _this = this,
data = this.editable;
// Flatten recipient addresses
_.each(['to', 'cc', 'bcc', 'reply-to'], function(type) {
@@ -166,7 +188,11 @@
});
Message.$log.debug('save = ' + JSON.stringify(data, undefined, 2));
return Message.$$resource.save(this.$absolutePath({asDraft: true}), data);
return Message.$$resource.save(this.$absolutePath({asDraft: true}), data).then(function(response) {
Message.$log.debug('save = ' + JSON.stringify(response, undefined, 2));
_this.$setUID(response.uid);
_this.$update(); // fetch a new viewable version of the message
});
};
/**