[TASK] Migrate Modal to TypeScript
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Resources / Private / TypeScript / RenameFile.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 {SeverityEnum} from './Enum/Severity';
15 import * as $ from 'jquery';
16 import Modal = require('./Modal');
17
18 /**
19  * Module: TYPO3/CMS/Backend/RenameFile
20  * Modal to pick the required conflict strategy for colliding filenames
21  * @exports TYPO3/CMS/Backend/RenameFile
22  */
23 class RenameFile {
24
25   constructor() {
26     this.initialize();
27   }
28
29   public initialize(): void {
30     $('.t3js-submit-file-rename').on('click', this.checkForDuplicate);
31   }
32
33   private checkForDuplicate(e: any): void {
34     e.preventDefault();
35
36     const form: any = $('#' + $(e.currentTarget).attr('form'));
37     const fileNameField: any = form.find('input[name="data[rename][0][target]"]');
38     const conflictModeField: any = form.find('input[name="data[rename][0][conflictMode]"]');
39     const ajaxUrl: string = TYPO3.settings.ajaxUrls.file_exists;
40
41     $.ajax({
42       cache: false,
43       data: {
44         fileName: fileNameField.val(),
45         fileTarget: form.find('input[name="data[rename][0][destination]"]').val(),
46       },
47       success: (response: any): void => {
48         const fileExists: boolean = typeof response.uid !== 'undefined';
49         const originalFileName: string = fileNameField.data('original');
50         const newFileName: string = fileNameField.val();
51
52         if (fileExists && originalFileName !== newFileName) {
53           const description: string = TYPO3.lang['file_rename.exists.description']
54                                            .replace('{0}', originalFileName).replace('{1}', newFileName);
55
56           const modal: JQuery = Modal.confirm(
57             TYPO3.lang['file_rename.exists.title'],
58             description,
59             SeverityEnum.warning,
60             [
61               {
62                 active: true,
63                 btnClass: 'btn-default',
64                 name: 'cancel',
65                 text: TYPO3.lang['file_rename.actions.cancel'],
66               },
67               {
68                 btnClass: 'btn-primary',
69                 name: 'rename',
70                 text: TYPO3.lang['file_rename.actions.rename'],
71               },
72               {
73                 btnClass: 'btn-default',
74                 name: 'replace',
75                 text: TYPO3.lang['file_rename.actions.override'],
76               },
77             ]);
78
79           modal.on('button.clicked', (event: any): void => {
80             if (event.target.name !== 'cancel') {
81               conflictModeField.val(event.target.name);
82               form.submit();
83             }
84             Modal.dismiss();
85           });
86         } else {
87           form.submit();
88         }
89       },
90       url: ajaxUrl,
91     });
92   }
93 }
94
95 export = new RenameFile();