snappymail/dev/Component/AbstractInput.js

62 lines
1.7 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';
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.size = params.size || 0;
this.label = params.label || '';
this.preLabel = params.preLabel || '';
this.enable = undefined === 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 || '';
this.labeled = undefined !== params.label;
this.preLabeled = undefined !== params.preLabel;
this.triggered = undefined !== params.trigger && !!this.trigger;
2015-11-15 08:23:16 +08:00
this.classForTrigger = ko.observable('');
this.className = ko.computed(() => {
2019-07-05 03:19:24 +08:00
const size = ko.unwrap(this.size),
suffixValue = this.trigger ? ' ' + ('settings-saved-trigger-input ' + this.classForTrigger()).trim() : '';
2016-06-30 08:02:45 +08:00
return (0 < size ? 'span' + size : '') + suffixValue;
});
2015-11-15 08:23:16 +08:00
if (undefined !== params.width && params.element) {
2015-11-15 08:23:16 +08:00
params.element.find('input,select,textarea').css('width', params.width);
}
this.disposable.push(this.className);
2019-07-05 03:19:24 +08:00
if (this.trigger) {
2015-11-15 08:23:16 +08:00
this.setTriggerState(this.trigger());
2019-07-05 03:19:24 +08:00
this.disposable.push(this.trigger.subscribe(this.setTriggerState, this));
2015-11-15 08:23:16 +08:00
}
}
setTriggerState(value) {
2019-07-05 03:19:24 +08:00
switch (pInt(value)) {
2015-11-15 08:23:16 +08:00
case SaveSettingsStep.TrueResult:
this.classForTrigger('success');
break;
case SaveSettingsStep.FalseResult:
this.classForTrigger('error');
break;
default:
this.classForTrigger('');
break;
}
}
}
2019-07-05 03:19:24 +08:00
export { AbstractInput, AbstractInput as default };