Files
cms-backend/Resources/Public/JavaScript/global-event-handler.js
T

14 lines
3.9 KiB
JavaScript

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
import c from"@typo3/core/document-service.js";import r from"@typo3/core/event/regular-event.js";import u from"@typo3/backend/viewport.js";class f{constructor(){this.options={onChangeSelector:'[data-global-event="change"]',onClickSelector:'[data-global-event="click"]',onSubmitSelector:'form[data-global-event="submit"]'},c.ready().then(()=>this.registerEvents())}registerEvents(){new r("change",this.handleChangeEvent.bind(this)).delegateTo(document,this.options.onChangeSelector),new r("click",this.handleClickEvent.bind(this)).delegateTo(document,this.options.onClickSelector),new r("submit",this.handleSubmitEvent.bind(this)).delegateTo(document,this.options.onSubmitSelector)}handleChangeEvent(e,t){e.preventDefault(),this.handleFormChildAction(e,t)||this.handleFormChildNavigateAction(e,t)}handleClickEvent(e,t){e.preventDefault(),this.handleFormChildAction(e,t)}handleSubmitEvent(e,t){e.preventDefault(),this.handleFormNavigateAction(e,t)}handleFormChildAction(e,t){const n=t.dataset.actionSubmit,a=t.dataset.actionFocus;if(!n&&!a)return!1;let i=null;const l=t.closest("form");if(n){const o=n!=="$form"?document.querySelector(n):null;if(n==="$form"&&this.isHTMLFormChildElement(t)?i=t.form:n==="$form"&&l?i=l:o instanceof HTMLFormElement&&(i=o),!(i instanceof HTMLFormElement))return!1;this.assignFormValues(i,t),i.submit()}if(a&&l){if(!(l instanceof HTMLFormElement))return!1;const o=l.querySelector(a);if(o===null)return!1;o.focus()}return!0}assignFormValues(e,t){const n=t.dataset.formValues,a=n?JSON.parse(n):null;return a===null||!(a instanceof Object)?!1:(Object.entries(a).forEach(([i,l])=>{let o=e.querySelector("[name="+CSS.escape(i)+"]");o instanceof HTMLElement?this.assignHTMLFormChildElementValue(o,l.toString()):(o=document.createElement("input"),o.setAttribute("type","hidden"),o.setAttribute("name",i),o.setAttribute("value",l.toString()),e.appendChild(o))}),!0)}handleFormChildNavigateAction(e,t){const n=t.dataset.actionNavigate;if(!n)return!1;const a=this.resolveHTMLFormChildElementValue(t),i=t.dataset.navigateValue;let l=null;return n==="$data=~s/$value/"&&i&&a!==null?l=this.substituteValueVariable(i,a):n==="$data"&&i?l=i:n==="$value"&&a&&(l=a),l===null?!1:(u.ContentContainer.setUrl(l),!0)}handleFormNavigateAction(e,t){const n=t.action,a=t.dataset.actionNavigate;if(!n||!a)return!1;const i=t.dataset.navigateValue,l=t.dataset.valueSelector,o=this.resolveHTMLFormChildElementValue(t.querySelector(l));let s=null;return a==="$form=~s/$value/"&&i&&o!==null?s=this.substituteValueVariable(i,o):a==="$form"&&(s=n),s===null?!1:(u.ContentContainer.setUrl(s),!0)}substituteValueVariable(e,t){return e.replace(/(\$\{value\}|%24%7Bvalue%7D|\$\[value\]|%24%5Bvalue%5D)/gi,t)}isHTMLFormChildElement(e){return e instanceof HTMLSelectElement||e instanceof HTMLInputElement||e instanceof HTMLTextAreaElement}resolveHTMLFormChildElementValue(e){const t=e.getAttribute("type");if(e instanceof HTMLSelectElement)return e.options[e.selectedIndex].value;if(e instanceof HTMLInputElement&&t==="checkbox"){const n=e.dataset.emptyValue;return e.checked?e.value:typeof n<"u"?n:""}else if(e instanceof HTMLInputElement)return e.value;return null}assignHTMLFormChildElementValue(e,t){const n=e.getAttribute("type");if(e instanceof HTMLSelectElement)Array.from(e.options).some((a,i)=>a.value===t?(e.selectedIndex=i,!0):!1);else if(e instanceof HTMLInputElement&&n==="checkbox"){const a=e.dataset.emptyValue;typeof a<"u"&&a===t?e.checked=!1:e.value===t&&(e.checked=!0)}else e instanceof HTMLInputElement&&(e.value=t)}}var m=new f;export{m as default};