snappymail/dev/Model/ContactProperty.js

87 lines
1.5 KiB
JavaScript
Raw Normal View History

2019-07-05 03:19:24 +08:00
import { pInt, pString } from 'Common/Utils';
import { i18n } from 'Common/Translator';
2016-07-07 05:03:30 +08:00
2019-07-05 03:19:24 +08:00
import { AbstractModel } from 'Knoin/AbstractModel';
2016-07-07 05:03:30 +08:00
const trim = text => null == text ? "" : (text + "").trim();
2021-01-25 05:58:06 +08:00
/**
* @enum {number}
*/
export const ContactPropertyType = {
Unknown: 0,
FullName: 10,
FirstName: 15,
LastName: 16,
MiddleName: 17,
Nick: 18,
NamePrefix: 20,
NameSuffix: 21,
Email: 30,
Phone: 31,
Web: 32,
Birthday: 40,
Facebook: 90,
Skype: 91,
GitHub: 92,
Note: 110,
Custom: 250
};
2021-01-22 23:32:08 +08:00
export class ContactPropertyModel extends AbstractModel {
2016-07-07 05:03:30 +08:00
/**
* @param {number=} type = Enums.ContactPropertyType.Unknown
* @param {string=} typeStr = ''
* @param {string=} value = ''
* @param {boolean=} focused = false
* @param {string=} placeholder = ''
*/
2019-07-05 03:19:24 +08:00
constructor(type = ContactPropertyType.Unknown, typeStr = '', value = '', focused = false, placeholder = '') {
2020-10-19 01:19:45 +08:00
super();
2016-07-07 05:03:30 +08:00
2020-10-25 18:46:58 +08:00
this.addObservables({
type: pInt(type),
typeStr: pString(typeStr),
focused: !!focused,
value: pString(value),
2016-07-07 05:03:30 +08:00
2020-10-25 18:46:58 +08:00
placeholder: placeholder
});
2016-07-07 05:03:30 +08:00
2020-10-25 21:14:14 +08:00
this.addComputables({
placeholderValue: () => {
const v = this.placeholder();
return v ? i18n(v) : '';
},
2016-07-07 05:03:30 +08:00
2020-10-25 21:14:14 +08:00
largeValue: () => ContactPropertyType.Note === this.type()
});
2016-07-07 05:03:30 +08:00
}
isType(type) {
return this.type && type === this.type();
}
isValid() {
return this.value && !!trim(this.value());
}
toJSON() {
return {
type: this.type(),
typeStr: this.typeStr(),
value: this.value()
};
}
// static reviveFromJson(json) {}
2016-07-07 05:03:30 +08:00
}