snappymail/dev/Model/ContactProperty.js

59 lines
1.3 KiB
JavaScript
Raw Normal View History

2019-07-05 03:19:24 +08:00
import { ContactPropertyType } from 'Common/Enums';
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();
2019-07-05 03:19:24 +08:00
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
}
2019-07-05 03:19:24 +08:00
export { ContactPropertyModel, ContactPropertyModel as default };