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
|
|
|
|
2020-10-30 18:49:05 +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
|
|
|
}
|
2020-10-20 16:27:00 +08:00
|
|
|
|
2020-10-30 18:49:05 +08:00
|
|
|
isType(type) {
|
|
|
|
return this.type && type === this.type();
|
|
|
|
}
|
|
|
|
|
|
|
|
isValid() {
|
|
|
|
return this.value && !!trim(this.value());
|
|
|
|
}
|
|
|
|
|
2020-10-20 21:37:06 +08:00
|
|
|
toJSON() {
|
|
|
|
return {
|
|
|
|
type: this.type(),
|
|
|
|
typeStr: this.typeStr(),
|
|
|
|
value: this.value()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-10-20 23:39:00 +08:00
|
|
|
// static reviveFromJson(json) {}
|
2016-07-07 05:03:30 +08:00
|
|
|
}
|
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
export { ContactPropertyModel, ContactPropertyModel as default };
|