Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TASK] Migrate SplitButtons to TypeScript
The SplitButtons module is migrated to TypeScript. Additionally, some parameters of the Icons module are declared as optional. Change-Id: I61f883e8e496e018a45f63c303de66274d071d94 Resolves: #82602 Releases: master Reviewed-on: https://review.typo3.org/55906 Tested-by: TYPO3com <no-reply@typo3.com> Reviewed-by: Mathias Schreiber <mathias.schreiber@typo3.com> Tested-by: Mathias Schreiber <mathias.schreiber@typo3.com> Reviewed-by: Benni Mack <benni@typo3.org> Tested-by: Benni Mack <benni@typo3.org>
- Loading branch information
1 parent
63d47c8
commit 92120c8
Showing
2 changed files
with
116 additions
and
104 deletions.
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
typo3/sysext/backend/Resources/Private/TypeScript/SplitButtons.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* 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 DocumentHeader source code. | ||
* | ||
* The TYPO3 project - inspiring people to share! | ||
*/ | ||
|
||
import * as $ from 'jquery'; | ||
import Icons = require('./Icons'); | ||
|
||
class SplitButtons { | ||
private preSubmitCallbacks: Array<Function> = []; | ||
|
||
constructor() { | ||
$((): void => { | ||
this.initializeSaveHandling(); | ||
}); | ||
} | ||
|
||
/** | ||
* Adds a callback being executed before submit | ||
* | ||
* @param {Function} callback | ||
*/ | ||
public addPreSubmitCallback(callback: Function): void { | ||
if (typeof callback !== 'function') { | ||
throw 'callback must be a function.'; | ||
} | ||
|
||
this.preSubmitCallbacks.push(callback); | ||
} | ||
|
||
/** | ||
* Initializes the save handling | ||
*/ | ||
private initializeSaveHandling(): void { | ||
let preventExec = false; | ||
const elements = [ | ||
'button[form]', | ||
'button[name^="_save"]', | ||
'a[data-name^="_save"]', | ||
'button[name="CMD"][value^="save"]', | ||
'a[data-name="CMD"][data-value^="save"]', | ||
'button[name^="_translation_save"]', | ||
'a[data-name^="_translation_save"]', | ||
'button[name="CMD"][value^="_translation_save"]', | ||
'a[data-name="CMD"][data-value^="_translation_save"]' | ||
].join(','); | ||
|
||
$('.t3js-module-docheader').on('click', elements, (e: JQueryEventObject): boolean => { | ||
// prevent doubleclick double submission bug in chrome, | ||
// see https://forge.typo3.org/issues/77942 | ||
if (!preventExec) { | ||
preventExec = true; | ||
const $me = $(e.currentTarget); | ||
const linkedForm = $me.attr('form') || $me.attr('data-form') || null; | ||
const $form = linkedForm ? $('#' + linkedForm) : $me.closest('form'); | ||
const name = $me.data('name') || e.currentTarget.getAttribute('name'); | ||
const value = $me.data('value') || e.currentTarget.getAttribute('value'); | ||
const $elem = $('<input />').attr('type', 'hidden').attr('name', name).attr('value', value); | ||
|
||
// Run any preSubmit callbacks | ||
for (let i = 0; i < this.preSubmitCallbacks.length; ++i) { | ||
this.preSubmitCallbacks[i](e); | ||
|
||
if (e.isPropagationStopped()) { | ||
preventExec = false; | ||
return false; | ||
} | ||
} | ||
$form.append($elem); | ||
// Disable submit buttons | ||
$form.on('submit', (): boolean => { | ||
if ($form.find('.has-error').length > 0) { | ||
preventExec = false; | ||
return false; | ||
} | ||
|
||
let $affectedButton: JQuery; | ||
const $splitButton = $me.closest('.t3js-splitbutton'); | ||
|
||
if ($splitButton.length > 0) { | ||
$splitButton.find('button').prop('disabled', true); | ||
$affectedButton = $splitButton.children().first(); | ||
} else { | ||
$me.prop('disabled', true); | ||
$affectedButton = $me; | ||
} | ||
|
||
Icons.getIcon('spinner-circle-dark', Icons.sizes.small).done((markup: string): void => { | ||
$affectedButton.find('.t3js-icon').replaceWith(markup); | ||
}); | ||
|
||
return true; | ||
}); | ||
|
||
if ((e.currentTarget.tagName === 'A' || $me.attr('form')) && !e.isDefaultPrevented()) { | ||
$form.submit(); | ||
e.preventDefault(); | ||
} | ||
} | ||
|
||
return true; | ||
}); | ||
} | ||
} | ||
|
||
export = new SplitButtons(); |
106 changes: 2 additions & 104 deletions
106
typo3/sysext/backend/Resources/Public/JavaScript/SplitButtons.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.