3d7963a9672b1df7cf084c7b0f1f768ad30768d4
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Resources / Public / JavaScript / SplitButtons.js
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 DocumentHeader source code.
10 *
11 * The TYPO3 project - inspiring people to share!
12 */
13
14 /**
15 * Module: TYPO3/CMS/Backend/SplitButtons
16 * Initializes global handling of split buttons.
17 */
18 define(['jquery', 'TYPO3/CMS/Backend/Icons'], function($, Icons) {
19 'use strict';
20
21 /**
22 *
23 * @type {{preSubmitCallbacks: Array}}
24 * @exports TYPO3/CMS/Backend/SplitButtons
25 */
26 var SplitButtons = {
27 preSubmitCallbacks: []
28 };
29
30 /**
31 * Initializes the save handling
32 */
33 SplitButtons.initializeSaveHandling = function() {
34 var elements = [
35 'button[name^="_save"]',
36 'a[data-name^="_save"]',
37 'button[name="CMD"][value^="save"]',
38 'a[data-name="CMD"][data-value^="save"]'
39 ].join(',');
40 $(document).on('click', elements, function(e) {
41 var $me = $(this),
42 $form = $me.closest('form'),
43 name = $me.data('name') || this.name,
44 value = $me.data('value') || this.value,
45 $elem = $('<input />').attr('type', 'hidden').attr('name', name).attr('value', value);
46
47 // Run any preSubmit callbacks
48 for (var i = 0; i < SplitButtons.preSubmitCallbacks.length; ++i) {
49 SplitButtons.preSubmitCallbacks[i](e);
50 }
51
52 $form.append($elem);
53
54 // Disable submit buttons
55 $form.on('submit', function() {
56 var $affectedButton,
57 $splitButton = $me.closest('.t3js-splitbutton');
58
59 if ($splitButton.length > 0) {
60 $splitButton.find('button').prop('disabled', true);
61 $affectedButton = $splitButton.children().first();
62 } else {
63 $me.prop('disabled', true);
64 $affectedButton = $me;
65 }
66
67 Icons.getIcon('spinner-circle-dark', Icons.sizes.small).done(function(markup) {
68 $affectedButton.find('.t3js-icon').replaceWith(markup);
69 });
70 });
71
72 if (e.currentTarget.tagName === 'A' && !e.isDefaultPrevented()) {
73 $form.submit();
74 e.preventDefault();
75 }
76 });
77 };
78
79 /**
80 * Adds a callback being executed before submit
81 *
82 * @param {function} callback
83 */
84 SplitButtons.addPreSubmitCallback = function(callback) {
85 if (typeof callback !== 'function') {
86 throw 'callback must be a function.';
87 }
88
89 SplitButtons.preSubmitCallbacks.push(callback);
90 };
91
92 $(SplitButtons.initializeSaveHandling);
93
94 return SplitButtons;
95 });