mirror of
https://github.com/the-djmaze/snappymail.git
synced 2025-01-01 04:22:15 +08:00
54 lines
1.2 KiB
JavaScript
54 lines
1.2 KiB
JavaScript
import { AbstractCollectionModel } from 'Model/AbstractCollection';
|
|
import { EmailModel, addressparser } from 'Model/Email';
|
|
import { forEachObjectValue } from 'Common/Utils';
|
|
|
|
'use strict';
|
|
|
|
export class EmailCollectionModel extends AbstractCollectionModel
|
|
{
|
|
/**
|
|
* @param {?Array} json
|
|
* @returns {EmailCollectionModel}
|
|
*/
|
|
static reviveFromJson(items) {
|
|
return super.reviveFromJson(items, email => EmailModel.reviveFromJson(email));
|
|
}
|
|
|
|
/**
|
|
* @param {string} text
|
|
* @returns {EmailCollectionModel}
|
|
*/
|
|
static fromString(str) {
|
|
let list = new this();
|
|
list.fromString(str);
|
|
return list;
|
|
}
|
|
|
|
/**
|
|
* @param {boolean=} friendlyView = false
|
|
* @param {boolean=} wrapWithLink = false
|
|
* @returns {string}
|
|
*/
|
|
toString(friendlyView, wrapWithLink) {
|
|
return this.map(email => email.toLine(friendlyView, wrapWithLink)).join(', ');
|
|
}
|
|
|
|
/**
|
|
* @param {string} text
|
|
*/
|
|
fromString(str) {
|
|
if (str) {
|
|
let items = {}, key;
|
|
addressparser(str).forEach(item => {
|
|
item = new EmailModel(item.email, item.name);
|
|
// Make them unique
|
|
key = item.email || item.name;
|
|
if (key && (item.name || !items[key])) {
|
|
items[key] = item;
|
|
}
|
|
});
|
|
forEachObjectValue(items, item => this.push(item));
|
|
}
|
|
}
|
|
|
|
}
|