import{View}from"../../view/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,e,s){super(t),this.fieldName=e,isEmpty(this.fieldName)&&(this.fieldName="unnamed"),this.fieldConfig=s||{},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=[],this.onClickCallbacks=[],this.onMouseUpCallbacks=[],this.onMouseDownCallbacks=[],this.onKeyPressCallbacks=[],this.onKeyDownCallbacks=[],this.onKeyUpCallbacks=[]}onChange(t){this.onChangeCallbacks.push(t)}onInput(t){this.onInputCallbacks.push(t)}onFocus(t){this.onFocusCallbacks.push(t)}onBlur(t){this.onBlurCallbacks.push(t)}onClick(t){this.onClickCallbacks.push(t)}onMouseDown(t){this.onMouseDownCallbacks.push(t)}onMouseUp(t){this.onMouseUpCallbacks.push(t)}onKeyPress(t){this.onKeyPressCallbacks.push(t)}onKeyDown(t){this.onKeyDownCallbacks.push(t)}onKeyUp(t){this.onKeyUpCallbacks.push(t)}focus(){void 0!==this.node&&this.node.focus()}changed(t){t&&(t.stopPropagation(),t.preventDefault());let e=this.value,s=this.getValue();for(let t of this.onChangeCallbacks)t(this.fieldName,e,s);this.value=s}inputted(t){t&&(t.stopPropagation(),t.preventDefault());let e=this.getValue();for(let t of this.onInputCallbacks)t(e)}focused(t){t&&(t.stopPropagation(),t.preventDefault());for(let e of this.onFocusCallbacks)e(t)}blurred(t){t&&(t.stopPropagation(),t.preventDefault());for(let e of this.onBlurCallbacks)e(t)}clicked(t){t&&t.stopPropagation();for(let e of this.onClickCallbacks)e(t)}mouseUpped(t){t&&t.stopPropagation();for(let e of this.onMouseUpCallbacks)e(t)}mouseDowned(t){t&&t.stopPropagation();for(let e of this.onMouseDownCallbacks)e(t)}keyPressed(t){t&&t.stopPropagation();for(let e of this.onKeyPressCallbacks)e(t)}keyDowned(t){t&&t.stopPropagation();for(let e of this.onKeyDownCallbacks)e(t)}keyUpped(t){t&&t.stopPropagation();for(let e of this.onKeyUpCallbacks)e(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,e){let s=this.value!==t;return this.value=t,void 0!==this.node&&this.node.val(this.value,!1),!1!==e&&s&&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 e=this.getValue();if(this.required&&isEmpty(e)&&!1!==t)throw"This field is required.";return e}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",(t=>this.clicked(t))).on("mouseup",(t=>this.mouseUpped(t))).on("mousedown",(t=>this.mouseDowned(t))).on("keypress",(t=>this.keyPressed(t))).on("keyup",(t=>this.keyUpped(t))).on("keydown",(t=>this.keyDowned(t))),"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};
