import{View}from"../../base.mjs";import{isEmpty}from"../../../base/helpers.mjs";class InputView extends View{static tagName="input";static inputType="text";static defaultValue=null;static placeholder=null;static autoComplete="off";constructor(t,i,e){super(t),this.fieldName=i,isEmpty(this.fieldName)&&(this.fieldName="unnamed"),this.fieldConfig=e||{},this.value=isEmpty(this.fieldConfig.value)?this.constructor.defaultValue:this.fieldConfig.value,this.errorMessage=null,this.autoComplete=isEmpty(this.fieldConfig.autoComplete)?this.constructor.autoComplete:this.fieldConfig.autoComplete,this.editable=!(!1===this.fieldConfig.editable),this.disabled=!!this.fieldConfig.disabled,this.readonly=!!this.fieldConfig.readonly,this.required=!!this.fieldConfig.required,this.placeholder=this.fieldConfig.placeholder||this.constructor.placeholder,this.tooltip=this.fieldConfig.tooltip||this.constructor.tooltip,this.onChangeCallbacks=[],this.onInputCallbacks=[],this.onFocusCallbacks=[],this.onBlurCallbacks=[],isEmpty(this.constructor.onChange)||this.onChange((t=>this.constructor.onChange(this,t))),isEmpty(this.constructor.onInput)||this.onInput((t=>this.constructor.onInput(this,t))),isEmpty(this.constructor.onFocus)||this.onFocus((t=>this.constructor.onFocus(this,t))),isEmpty(this.constructor.onBlur)||this.onBlur((t=>this.constructor.onBlur(this,t)))}onChange(t){this.onChangeCallbacks.push(t)}onInput(t){this.onInputCallbacks.push(t)}onFocus(t){this.onFocusCallbacks.push(t)}onBlur(t){this.onBlurCallbacks.push(t)}focus(){void 0!==this.node&&this.node.focus()}changed(t){isEmpty(t)||(t.stopPropagation(),t.preventDefault());let i=this.value,e=this.getValue();for(let t of this.onChangeCallbacks)t(this.fieldName,i,e);this.value=e}inputted(t){t.stopPropagation(),t.preventDefault();let i=this.getValue();for(let t of this.onInputCallbacks)t(i)}focused(t){t.stopPropagation(),t.preventDefault();for(let i of this.onFocusCallbacks)i(t)}blurred(t){t.stopPropagation(),t.preventDefault();for(let i of this.onBlurCallbacks)i(t)}disable(){return this.disabled=!0,void 0!==this.node&&this.node.disabled(!0),this}checkEnable(){return this.editable||isEmpty(this.value)?this.enable():this}enable(){return this.disabled=!1,void 0!==this.node&&this.node.disabled(!1),this}setValue(t,i){let e=this.value!==t;return this.value=t,void 0!==this.node&&this.node.val(this.value,!1),!1!==i&&e&&this.changed(),this.editable||this.disable(),this}getValue(){return void 0!==this.node&&void 0!==this.node.element?this.node.val():this.value}checkGetValue(t){let i=this.getValue();if(this.required&&isEmpty(i)&&!1!==t)throw"This field is required.";return i}labelClicked(){this.focus()}async build(){let t=await super.build();return t.name(this.fieldName).attr("disabled",this.disabled).attr("readonly",this.readonly).attr("required",this.required).attr("autocomplete",this.autoComplete).on("change",(t=>this.changed(t))).on("input",(t=>this.inputted(t))).on("blur",(t=>this.blurred(t))).on("focus",(t=>this.focused(t))).on("click,mouseup,mousedown",(t=>t.stopPropagation())),"input"==this.constructor.tagName&&t.type(this.constructor.inputType),isEmpty(this.value)||t.val(this.value),isEmpty(this.placeholder)||t.attr("placeholder",this.placeholder),isEmpty(this.tooltip)||t.data("tooltip",this.tooltip),t}}export{InputView};
