snappymail/dev/Component/AbstractInput.js

71 lines
1.7 KiB
JavaScript
Raw Normal View History

2015-11-15 08:23:16 +08:00
import ko from 'ko';
2016-06-07 05:57:52 +08:00
import {isUnd, trim, pInt} from 'Common/Utils';
2015-11-15 08:23:16 +08:00
import {SaveSettingsStep} from 'Common/Enums';
import {AbstractComponent} from 'Component/Abstract';
class AbstractInput extends AbstractComponent
{
/**
* @param {Object} params
*/
constructor(params) {
super();
this.value = params.value || '';
this.size = params.size || 0;
this.label = params.label || '';
this.preLabel = params.preLabel || '';
2016-06-07 05:57:52 +08:00
this.enable = isUnd(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 || '';
2016-06-07 05:57:52 +08:00
this.labeled = !isUnd(params.label);
this.preLabeled = !isUnd(params.preLabel);
this.triggered = !isUnd(params.trigger) && !!this.trigger;
2015-11-15 08:23:16 +08:00
this.classForTrigger = ko.observable('');
this.className = ko.computed(() => {
const
2015-11-15 08:23:16 +08:00
size = ko.unwrap(this.size),
suffixValue = this.trigger ? ' ' + trim('settings-saved-trigger-input ' + this.classForTrigger()) : '';
2016-06-30 08:02:45 +08:00
return (0 < size ? 'span' + size : '') + suffixValue;
});
2015-11-15 08:23:16 +08:00
2016-06-07 05:57:52 +08:00
if (!isUnd(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);
if (this.trigger)
{
this.setTriggerState(this.trigger());
this.disposable.push(
this.trigger.subscribe(this.setTriggerState, this)
);
}
}
setTriggerState(value) {
2016-06-07 05:57:52 +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;
}
}
}
export {AbstractInput, AbstractInput as default};