snappymail/dev/Component/AbstractInput.js

56 lines
1.4 KiB
JavaScript
Raw Normal View History

2015-11-15 08:23:16 +08:00
import ko from 'ko';
import { pInt } from 'Common/Utils';
2019-07-05 03:19:24 +08:00
import { SaveSettingsStep } from 'Common/Enums';
import { AbstractComponent } from 'Component/Abstract';
import { koComputable } from 'External/ko';
2015-11-15 08:23:16 +08:00
2019-07-05 03:19:24 +08:00
class AbstractInput extends AbstractComponent {
2015-11-15 08:23:16 +08:00
/**
* @param {Object} params
*/
constructor(params) {
super();
this.value = params.value || '';
this.label = params.label || '';
2021-09-14 22:11:50 +08:00
this.enable = null == params.enable ? true : params.enable;
2015-11-15 08:23:16 +08:00
this.trigger = params.trigger && params.trigger.subscribe ? params.trigger : null;
this.placeholder = params.placeholder || '';
2021-09-14 22:11:50 +08:00
this.labeled = null != params.label;
2015-11-15 08:23:16 +08:00
let size = 0 < params.size ? 'span' + params.size : '';
2019-07-05 03:19:24 +08:00
if (this.trigger) {
2021-09-14 22:11:50 +08:00
const
classForTrigger = ko.observable(''),
setTriggerState = value => {
switch (pInt(value)) {
case SaveSettingsStep.TrueResult:
classForTrigger('success');
break;
case SaveSettingsStep.FalseResult:
classForTrigger('error');
break;
default:
classForTrigger('');
break;
}
};
setTriggerState(this.trigger());
this.className = koComputable(() =>
(size + ' settings-saved-trigger-input ' + classForTrigger()).trim()
2021-09-14 22:11:50 +08:00
);
this.disposable.push(this.trigger.subscribe(setTriggerState, this));
} else {
this.className = size;
2015-11-15 08:23:16 +08:00
}
2021-09-14 22:11:50 +08:00
this.disposable.push(this.className);
2015-11-15 08:23:16 +08:00
}
}
2021-01-22 23:32:08 +08:00
export { AbstractInput };