2016-07-07 05:03:30 +08:00
|
|
|
|
2017-09-28 01:58:15 +08:00
|
|
|
import _ from '_';
|
|
|
|
import addressparser from 'emailjs-addressparser';
|
|
|
|
import {trim, encodeHtml, isNonEmptyArray} from 'Common/Utils';
|
2016-07-07 05:03:30 +08:00
|
|
|
|
|
|
|
class EmailModel
|
|
|
|
{
|
2016-09-10 06:38:16 +08:00
|
|
|
email = '';
|
|
|
|
name = '';
|
|
|
|
dkimStatus = '';
|
|
|
|
dkimValue = '';
|
|
|
|
|
2016-07-07 05:03:30 +08:00
|
|
|
/**
|
|
|
|
* @param {string=} email = ''
|
|
|
|
* @param {string=} name = ''
|
|
|
|
* @param {string=} dkimStatus = 'none'
|
|
|
|
* @param {string=} dkimValue = ''
|
|
|
|
*/
|
|
|
|
constructor(email = '', name = '', dkimStatus = 'none', dkimValue = '')
|
|
|
|
{
|
|
|
|
this.email = email;
|
|
|
|
this.name = name;
|
|
|
|
this.dkimStatus = dkimStatus;
|
|
|
|
this.dkimValue = dkimValue;
|
|
|
|
|
|
|
|
this.clearDuplicateName();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @static
|
|
|
|
* @param {AjaxJsonEmail} json
|
|
|
|
* @returns {?EmailModel}
|
|
|
|
*/
|
|
|
|
static newInstanceFromJson(json) {
|
|
|
|
const email = new EmailModel();
|
|
|
|
return email.initByJson(json) ? email : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
clear() {
|
|
|
|
this.email = '';
|
|
|
|
this.name = '';
|
|
|
|
|
|
|
|
this.dkimStatus = 'none';
|
|
|
|
this.dkimValue = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
validate() {
|
|
|
|
return '' !== this.name || '' !== this.email;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {boolean} withoutName = false
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
hash(withoutName = false) {
|
|
|
|
return '#' + (withoutName ? '' : this.name) + '#' + this.email + '#';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
clearDuplicateName() {
|
|
|
|
if (this.name === this.email)
|
|
|
|
{
|
|
|
|
this.name = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} query
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
search(query) {
|
|
|
|
return -1 < (this.name + ' ' + this.email).toLowerCase().indexOf(query.toLowerCase());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {AjaxJsonEmail} oJsonEmail
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
initByJson(json) {
|
|
|
|
let result = false;
|
|
|
|
if (json && 'Object/Email' === json['@Object'])
|
|
|
|
{
|
|
|
|
this.name = trim(json.Name);
|
|
|
|
this.email = trim(json.Email);
|
|
|
|
this.dkimStatus = trim(json.DkimStatus || '');
|
|
|
|
this.dkimValue = trim(json.DkimValue || '');
|
|
|
|
|
|
|
|
result = '' !== this.email;
|
|
|
|
this.clearDuplicateName();
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {boolean} friendlyView
|
|
|
|
* @param {boolean=} wrapWithLink = false
|
|
|
|
* @param {boolean=} useEncodeHtml = false
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
toLine(friendlyView, wrapWithLink = false, useEncodeHtml = false) {
|
|
|
|
let result = '';
|
|
|
|
if ('' !== this.email)
|
|
|
|
{
|
|
|
|
if (friendlyView && '' !== this.name)
|
|
|
|
{
|
2017-10-07 02:52:00 +08:00
|
|
|
result = wrapWithLink ? '<a href="mailto:' + encodeHtml(this.email) + '?to=' + encodeHtml('"' + this.name + '" <' + this.email + '>') +
|
2016-07-30 03:14:51 +08:00
|
|
|
'" target="_blank" tabindex="-1">' + encodeHtml(this.name) + '</a>' : (useEncodeHtml ? encodeHtml(this.name) : this.name);
|
2017-10-07 02:52:00 +08:00
|
|
|
// result = wrapWithLink ? '<a href="mailto:' + encodeHtml('"' + this.name + '" <' + this.email + '>') +
|
|
|
|
// '" target="_blank" tabindex="-1">' + encodeHtml(this.name) + '</a>' : (useEncodeHtml ? encodeHtml(this.name) : this.name);
|
2016-07-07 05:03:30 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = this.email;
|
|
|
|
if ('' !== this.name)
|
|
|
|
{
|
|
|
|
if (wrapWithLink)
|
|
|
|
{
|
|
|
|
result = encodeHtml('"' + this.name + '" <') + '<a href="mailto:' +
|
2017-10-07 02:52:00 +08:00
|
|
|
encodeHtml(this.email) + '?to=' + encodeHtml('"' + this.name + '" <' + this.email + '>') +
|
2016-07-07 05:03:30 +08:00
|
|
|
'" target="_blank" tabindex="-1">' +
|
|
|
|
encodeHtml(result) +
|
|
|
|
'</a>' +
|
|
|
|
encodeHtml('>');
|
2017-10-07 02:52:00 +08:00
|
|
|
// result = encodeHtml('"' + this.name + '" <') + '<a href="mailto:' +
|
|
|
|
// encodeHtml('"' + this.name + '" <' + this.email + '>') +
|
|
|
|
// '" target="_blank" tabindex="-1">' +
|
|
|
|
// encodeHtml(result) +
|
|
|
|
// '</a>' +
|
|
|
|
// encodeHtml('>');
|
2016-07-07 05:03:30 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = '"' + this.name + '" <' + result + '>';
|
|
|
|
if (useEncodeHtml)
|
|
|
|
{
|
|
|
|
result = encodeHtml(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (wrapWithLink)
|
|
|
|
{
|
|
|
|
result = '<a href="mailto:' + encodeHtml(this.email) + '" target="_blank" tabindex="-1">' + encodeHtml(this.email) + '</a>';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-09-28 01:58:15 +08:00
|
|
|
static splitEmailLine(line) {
|
2018-02-28 03:10:26 +08:00
|
|
|
const parsedResult = addressparser(line);
|
2017-09-28 01:58:15 +08:00
|
|
|
if (isNonEmptyArray(parsedResult))
|
2016-07-07 05:03:30 +08:00
|
|
|
{
|
2017-09-28 01:58:15 +08:00
|
|
|
const result = [];
|
|
|
|
let exists = false;
|
|
|
|
parsedResult.forEach((item) => {
|
|
|
|
const address = item.address ? new EmailModel(
|
|
|
|
item.address.replace(/^[<]+(.*)[>]+$/g, '$1'),
|
|
|
|
item.name || ''
|
|
|
|
) : null;
|
|
|
|
|
|
|
|
if (address && address.email) {
|
|
|
|
exists = true;
|
|
|
|
}
|
2016-07-07 05:03:30 +08:00
|
|
|
|
2017-09-28 01:58:15 +08:00
|
|
|
result.push(address ? address.toLine(false) : item.name);
|
|
|
|
});
|
2016-07-07 05:03:30 +08:00
|
|
|
|
2017-09-28 01:58:15 +08:00
|
|
|
return exists ? result : null;
|
|
|
|
}
|
2016-07-07 05:03:30 +08:00
|
|
|
|
2017-09-28 01:58:15 +08:00
|
|
|
return null;
|
|
|
|
}
|
2016-07-07 05:03:30 +08:00
|
|
|
|
2017-09-28 01:58:15 +08:00
|
|
|
static parseEmailLine(line) {
|
2018-02-28 03:10:26 +08:00
|
|
|
const parsedResult = addressparser(line);
|
2017-09-28 01:58:15 +08:00
|
|
|
if (isNonEmptyArray(parsedResult))
|
2016-07-07 05:03:30 +08:00
|
|
|
{
|
2017-09-28 01:58:15 +08:00
|
|
|
return _.compact(parsedResult.map(
|
|
|
|
(item) => (item.address ? new EmailModel(
|
|
|
|
item.address.replace(/^[<]+(.*)[>]+$/g, '$1'),
|
|
|
|
item.name || ''
|
|
|
|
) : null)
|
|
|
|
));
|
2016-07-07 05:03:30 +08:00
|
|
|
}
|
|
|
|
|
2017-09-28 01:58:15 +08:00
|
|
|
return [];
|
|
|
|
}
|
2016-07-07 05:03:30 +08:00
|
|
|
|
2017-09-28 01:58:15 +08:00
|
|
|
/**
|
|
|
|
* @param {string} emailAddress
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
parse(emailAddress) {
|
|
|
|
emailAddress = trim(emailAddress);
|
|
|
|
if ('' === emailAddress)
|
2016-07-07 05:03:30 +08:00
|
|
|
{
|
2017-09-28 01:58:15 +08:00
|
|
|
return false;
|
2016-07-07 05:03:30 +08:00
|
|
|
}
|
|
|
|
|
2018-02-28 03:10:26 +08:00
|
|
|
const result = addressparser(emailAddress);
|
2017-09-28 01:58:15 +08:00
|
|
|
if (isNonEmptyArray(result) && result[0])
|
|
|
|
{
|
|
|
|
this.name = result[0].name || '';
|
|
|
|
this.email = result[0].address || '';
|
|
|
|
this.clearDuplicateName();
|
2016-07-07 05:03:30 +08:00
|
|
|
|
2017-09-28 01:58:15 +08:00
|
|
|
return true;
|
|
|
|
}
|
2016-07-07 05:03:30 +08:00
|
|
|
|
2017-09-28 01:58:15 +08:00
|
|
|
return false;
|
2016-07-07 05:03:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export {EmailModel, EmailModel as default};
|