2020-09-16 22:33:53 +08:00
|
|
|
import { AbstractCollectionModel } from 'Model/AbstractCollection';
|
2023-02-13 23:15:26 +08:00
|
|
|
import { EmailModel, addressparser } from 'Model/Email';
|
|
|
|
import { forEachObjectValue } from 'Common/Utils';
|
2020-09-15 15:43:53 +08:00
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2020-09-16 22:33:53 +08:00
|
|
|
export class EmailCollectionModel extends AbstractCollectionModel
|
2020-09-15 15:43:53 +08:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param {?Array} json
|
2020-09-15 21:08:08 +08:00
|
|
|
* @returns {EmailCollectionModel}
|
2020-09-15 15:43:53 +08:00
|
|
|
*/
|
|
|
|
static reviveFromJson(items) {
|
2020-10-23 21:15:54 +08:00
|
|
|
return super.reviveFromJson(items, email => EmailModel.reviveFromJson(email));
|
2020-09-15 15:43:53 +08:00
|
|
|
}
|
|
|
|
|
2023-02-13 23:15:26 +08:00
|
|
|
/**
|
|
|
|
* @param {string} text
|
|
|
|
* @returns {EmailCollectionModel}
|
|
|
|
*/
|
|
|
|
static fromString(str) {
|
|
|
|
let list = new this();
|
|
|
|
list.fromString(str);
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2020-09-15 15:43:53 +08:00
|
|
|
/**
|
|
|
|
* @param {boolean=} friendlyView = false
|
|
|
|
* @param {boolean=} wrapWithLink = false
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2022-09-30 17:38:51 +08:00
|
|
|
toString(friendlyView, wrapWithLink) {
|
|
|
|
return this.map(email => email.toLine(friendlyView, wrapWithLink)).join(', ');
|
2020-09-15 15:43:53 +08:00
|
|
|
}
|
2023-02-13 23:15:26 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} text
|
|
|
|
*/
|
|
|
|
fromString(str) {
|
|
|
|
if (str) {
|
|
|
|
let items = {}, key;
|
|
|
|
addressparser(str).forEach(item => {
|
2023-02-14 02:47:24 +08:00
|
|
|
item = new EmailModel(item.email, item.name);
|
2023-02-13 23:15:26 +08:00
|
|
|
// Make them unique
|
|
|
|
key = item.email || item.name;
|
|
|
|
if (key && (item.name || !items[key])) {
|
|
|
|
items[key] = item;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
forEachObjectValue(items, item => this.push(item));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-15 15:43:53 +08:00
|
|
|
}
|