diff options
author | Ola Bini <ola.bini@gmail.com> | 2014-07-31 19:29:33 -0300 |
---|---|---|
committer | Ola Bini <ola.bini@gmail.com> | 2014-07-31 19:29:33 -0300 |
commit | 04cf441c5ae18400c6b4865b0b37a71718dc9d46 (patch) | |
tree | dd0b0d049ec00389e2d4561b226c46eb1682b997 /web-ui/app/js/mail_view/data | |
parent | 639a663a4c37020003586438fdcd7ac529a00f10 (diff) |
Add web-ui based on previous code
Diffstat (limited to 'web-ui/app/js/mail_view/data')
-rw-r--r-- | web-ui/app/js/mail_view/data/mail_builder.js | 79 | ||||
-rw-r--r-- | web-ui/app/js/mail_view/data/mail_sender.js | 74 |
2 files changed, 153 insertions, 0 deletions
diff --git a/web-ui/app/js/mail_view/data/mail_builder.js b/web-ui/app/js/mail_view/data/mail_builder.js new file mode 100644 index 00000000..27820c1a --- /dev/null +++ b/web-ui/app/js/mail_view/data/mail_builder.js @@ -0,0 +1,79 @@ +/*global _ */ + +define(['services/model/mail'], function (mailModel) { + 'use strict'; + + var mail; + + function recipients(mail, place, v) { + if (v !== '' && !_.isUndefined(v)) { + if(_.isArray(v)) { + mail[place] = v; + } else { + mail[place] = v.split(' '); + } + } else { + mail[place] = []; + } + } + + return { + newMail: function(ident) { + ident = _.isUndefined(ident) ? '' : ident; + + mail = { + header: { + to: [], + cc: [], + bcc: [], + from: undefined, + subject: '' + }, + tags: [], + body: '', + ident: ident + }; + return this; + }, + + subject: function (subject) { + mail.header.subject = subject; + return this; + }, + + body: function(body) { + mail.body = body; + return this; + }, + + to: function (to) { + recipients(mail.header, 'to', to); + return this; + }, + + cc: function (cc) { + recipients(mail.header, 'cc', cc); + return this; + }, + + bcc: function (bcc) { + recipients(mail.header, 'bcc', bcc); + return this; + }, + + header: function(name, value) { + mail.header[name] = value; + return this; + }, + + tag: function(tag) { + if(_.isUndefined(tag)) { tag = 'drafts'; } + mail.tags.push(tag); + return this; + }, + + build: function() { + return mailModel.create(mail); + } + }; +}); diff --git a/web-ui/app/js/mail_view/data/mail_sender.js b/web-ui/app/js/mail_view/data/mail_sender.js new file mode 100644 index 00000000..7440f5a7 --- /dev/null +++ b/web-ui/app/js/mail_view/data/mail_sender.js @@ -0,0 +1,74 @@ +define( + [ + 'flight/lib/component', + 'mail_view/data/mail_builder', + 'page/events' + ], + function (defineComponent, mailBuilder, events) { + 'use strict'; + + return defineComponent(mailSender); + + function mailSender() { + function successSendMail(on){ + return function(result) { + on.trigger(document, events.mail.sent, result); + }; + } + + function successSaveDraft(on){ + return function(result){ + on.trigger(document, events.mail.draftSaved, result); + }; + } + + function failure(on) { + return function(xhr, status, error) { + on.trigger(events.ui.userAlerts.displayMessage, {message: 'Ops! something went wrong, try again later.'}); + }; + } + + this.defaultAttrs({ + mailsResource: '/mails' + }); + + this.sendMail = function(event, data) { + $.ajax(this.attr.mailsResource, { + type: 'POST', + dataType: 'json', + contentType: 'application/json; charset=utf-8', + data: JSON.stringify(data) + }).done(successSendMail(this)) + .fail(failure(this)); + }; + + this.saveMail = function(mail) { + var method = (mail.ident === '') ? 'POST' : 'PUT'; + + return $.ajax(this.attr.mailsResource, { + type: method, + dataType: 'json', + contentType: 'application/json; charset=utf-8', + data: JSON.stringify(mail) + }); + }; + + this.saveDraft = function(event, data) { + this.saveMail(data) + .done(successSaveDraft(this)) + .fail(failure(this)); + }; + + this.saveMailWithCallback = function(event, data) { + this.saveMail(data.mail) + .done(function(result) { return data.callback(result); }) + .fail(function(result) { return data.callback(result); }); + }; + + this.after('initialize', function () { + this.on(events.mail.send, this.sendMail); + this.on(events.mail.saveDraft, this.saveDraft); + this.on(document, events.mail.save, this.saveMailWithCallback); + }); + } + }); |