[TASK] Move FieldControl/ResetSelection handling to module
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Resources / Private / TypeScript / FormEngine / FieldControl / ResetSelection.ts
1 /*
2 * This file is part of the TYPO3 CMS project.
3 *
4 * It is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU General Public License, either version 2
6 * of the License, or any later version.
7 *
8 * For the full copyright and license information, please read the
9 * LICENSE.txt file that was distributed with this source code.
10 *
11 * The TYPO3 project - inspiring people to share!
12 */
13
14 import * as $ from 'jquery';
15
16 /**
17 * This module is used for the field control "Reset selection" used for selectSingleBox
18 */
19 class ResetSelection {
20 private controlElement: HTMLElement = null;
21
22 constructor(controlElementId: string) {
23 $((): void => {
24 this.controlElement = <HTMLElement>document.querySelector(controlElementId);
25 if (this.controlElement !== null) {
26 this.controlElement.addEventListener('click', this.registerClickHandler);
27 }
28 });
29 }
30
31 /**
32 * @param {Event} e
33 */
34 private registerClickHandler = (e: Event): void => {
35 e.preventDefault();
36
37 const itemName: string = this.controlElement.dataset.itemName;
38 const selectedIndices: Array<number> = JSON.parse(this.controlElement.dataset.selectedIndices);
39 const field = (<HTMLSelectElement>document.forms.namedItem('editform').querySelector('[name="' + itemName + '[]"]'));
40
41 field.selectedIndex = -1;
42 for (let i of selectedIndices) {
43 field.options[i].selected = true;
44 }
45 }
46 }
47
48 export = ResetSelection;