From b9f1aaff9f7c2da364ab0a7c8ef89e1591e752ea Mon Sep 17 00:00:00 2001 From: Bruno Tavares Date: Thu, 9 Apr 2015 10:05:16 -0300 Subject: Makes the mail model creation more linear I was surfing the code and I got quite confused with the closures and redefinitions of `this` when creating the mail model. I tried to keep the style of grouping together the functions assignment on the model on the end. --- web-ui/app/js/services/model/mail.js | 176 +++++++++++++++++------------------ 1 file changed, 83 insertions(+), 93 deletions(-) (limited to 'web-ui') diff --git a/web-ui/app/js/services/model/mail.js b/web-ui/app/js/services/model/mail.js index 34cba610..c41bcff9 100644 --- a/web-ui/app/js/services/model/mail.js +++ b/web-ui/app/js/services/model/mail.js @@ -16,121 +16,111 @@ */ 'use strict'; -define(['helpers/contenttype'], - function (contentType) { - - var asMail = (function () { - - function isSentMail() { - return _.has(this, 'mailbox') && this.mailbox.toUpperCase() === 'SENT'; - } - - function isDraftMail() { - return _.has(this, 'mailbox') && this.mailbox.toUpperCase() === 'DRAFTS'; - } - - function isInTrash() { - return _.has(this, 'mailbox') && this.mailbox.toUpperCase() === 'TRASH'; - } - - function setDraftReplyFor(ident) { - this.draft_reply_for = ident; - } - - function replyToAddress() { - return { - to: [this.replying.single], - cc: [] - }; - } - - function replyToAllAddress() { - return { - to: this.replying.all['to-field'], - cc: this.replying.all['cc-field'] - }; - } +define(['helpers/contenttype'], function (contentType) { + function isSentMail() { + return _.has(this, 'mailbox') && this.mailbox.toUpperCase() === 'SENT'; + } + + function isDraftMail() { + return _.has(this, 'mailbox') && this.mailbox.toUpperCase() === 'DRAFTS'; + } + + function isInTrash() { + return _.has(this, 'mailbox') && this.mailbox.toUpperCase() === 'TRASH'; + } + + function setDraftReplyFor(ident) { + this.draft_reply_for = ident; + } + + function replyToAddress() { + return { + to: [this.replying.single], + cc: [] + }; + } - function getHeadersFromMailPart (rawBody) { - var lines, headerLines, endOfHeaders, headers; + function replyToAllAddress() { + return { + to: this.replying.all['to-field'], + cc: this.replying.all['cc-field'] + }; + } - lines = rawBody.split('\n'); - endOfHeaders = _.indexOf(lines, ''); - headerLines = lines.slice(0, endOfHeaders); + function getHeadersFromMailPart (rawBody) { + var lines, headerLines, endOfHeaders, headers; - headers = _.map(headerLines, function (headerLine) { - return _.map(headerLine.split(':'), function(elem){return elem.trim();}); - }); + lines = rawBody.split('\n'); + endOfHeaders = _.indexOf(lines, ''); + headerLines = lines.slice(0, endOfHeaders); - return _.object(headers); - } + headers = _.map(headerLines, function (headerLine) { + return _.map(headerLine.split(':'), function(elem){return elem.trim();}); + }); - function getBodyFromMailPart (rawBody) { - var lines, endOfHeaders; + return _.object(headers); + } - lines = rawBody.split('\n'); - endOfHeaders = _.indexOf(lines, ''); + function getBodyFromMailPart (rawBody) { + var lines, endOfHeaders; - return lines.slice(endOfHeaders + 1).join('\n'); - } + lines = rawBody.split('\n'); + endOfHeaders = _.indexOf(lines, ''); - function parseWithHeaders(rawBody) { - return {headers: getHeadersFromMailPart(rawBody), body: getBodyFromMailPart(rawBody)}; - } + return lines.slice(endOfHeaders + 1).join('\n'); + } - function getMailMultiParts () { - var mediaType = this.getMailMediaType(); - var boundary = '--' + mediaType.params.boundary + '\n'; - var finalBoundary = '--' + mediaType.params.boundary + '--'; + function parseWithHeaders(rawBody) { + return {headers: getHeadersFromMailPart(rawBody), body: getBodyFromMailPart(rawBody)}; + } - var bodyParts = this.body.split(finalBoundary)[0].split(boundary); + function getMailMultiParts () { + var mediaType = this.getMailMediaType(); + var boundary = '--' + mediaType.params.boundary + '\n'; + var finalBoundary = '--' + mediaType.params.boundary + '--'; - bodyParts = _.reject(bodyParts, function(bodyPart) { return _.isEmpty(bodyPart.trim()); }); + var bodyParts = this.body.split(finalBoundary)[0].split(boundary); - return _.map(bodyParts, parseWithHeaders); - } + bodyParts = _.reject(bodyParts, function(bodyPart) { return _.isEmpty(bodyPart.trim()); }); - function getMailMediaType () { - return new contentType.MediaType(this.header.content_type); - } + return _.map(bodyParts, parseWithHeaders); + } - function isMailMultipartAlternative () { - return this.getMailMediaType().type === 'multipart/alternative'; - } + function getMailMediaType () { + return new contentType.MediaType(this.header.content_type); + } - function availableBodyPartsContentType () { - var bodyParts = this.getMailMultiParts(); + function isMailMultipartAlternative () { + return this.getMailMediaType().type === 'multipart/alternative'; + } - return _.pluck(_.pluck(bodyParts, 'headers'), 'Content-Type'); - } + function availableBodyPartsContentType () { + var bodyParts = this.getMailMultiParts(); - function getMailPartByContentType (contentType) { - var bodyParts = this.getMailMultiParts(); + return _.pluck(_.pluck(bodyParts, 'headers'), 'Content-Type'); + } - return _.findWhere(bodyParts, {headers: {'Content-Type': contentType}}); - } + function getMailPartByContentType (contentType) { + var bodyParts = this.getMailMultiParts(); - return function () { - this.isSentMail = isSentMail; - this.isDraftMail = isDraftMail; - this.isInTrash = isInTrash; - this.setDraftReplyFor = setDraftReplyFor; - this.replyToAddress = replyToAddress; - this.replyToAllAddress = replyToAllAddress; - this.getMailMediaType = getMailMediaType; - this.isMailMultipartAlternative = isMailMultipartAlternative; - this.getMailMultiParts = getMailMultiParts; - this.availableBodyPartsContentType = availableBodyPartsContentType; - this.getMailPartByContentType = getMailPartByContentType; - return this; - }; - }()); + return _.findWhere(bodyParts, {headers: {'Content-Type': contentType}}); + } return { create: function (mail) { - if (mail) { - asMail.apply(mail); - } + if (!mail) { return; } + + mail.isSentMail = isSentMail; + mail.isDraftMail = isDraftMail; + mail.isInTrash = isInTrash; + mail.setDraftReplyFor = setDraftReplyFor; + mail.replyToAddress = replyToAddress; + mail.replyToAllAddress = replyToAllAddress; + mail.getMailMediaType = getMailMediaType; + mail.isMailMultipartAlternative = isMailMultipartAlternative; + mail.getMailMultiParts = getMailMultiParts; + mail.availableBodyPartsContentType = availableBodyPartsContentType; + mail.getMailPartByContentType = getMailPartByContentType; return mail; } }; -- cgit v1.2.3