287d9238425e3b0fd27a534ab7fa82d16d69c5d5
[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 linkedForm = $me.attr('form') || $me.attr('data-form') || null,
43 $form = linkedForm ? $('#' + linkedForm) : $me.closest('form'),
44 name = $me.data('name') || this.name,
45 value = $me.data('value') || this.value,
46 $elem = $('<input />').attr('type', 'hidden').attr('name', name).attr('value', value);
47
48 // Run any preSubmit callbacks
49 for (var i = 0; i < SplitButtons.preSubmitCallbacks.length; ++i) {
50 SplitButtons.preSubmitCallbacks[i](e);
51 }
52
53 $form.append($elem);
54
55 // Disable submit buttons
56 $form.on('submit', function() {
57 if ($form.find('.has-error').length > 0) {
58 return false;
59 }
60
61 var $affectedButton,
62 $splitButton = $me.closest('.t3js-splitbutton');
63
64 if ($splitButton.length > 0) {
65 $splitButton.find('button').prop('disabled', true);
66 $affectedButton = $splitButton.children().first();
67 } else {
68 $me.prop('disabled', true);
69 $affectedButton = $me;
70 }
71
72 Icons.getIcon('spinner-circle-dark', Icons.sizes.small).done(function(markup) {
73 $affectedButton.find('.t3js-icon').replaceWith(markup);
74 });
75 });
76
77 if (e.currentTarget.tagName === 'A' && !e.isDefaultPrevented()) {
78 $form.submit();
79 e.preventDefault();
80 }
81 });
82 };
83
84 /**
85 * Adds a callback being executed before submit
86 *
87 * @param {function} callback
88 */
89 SplitButtons.addPreSubmitCallback = function(callback) {
90 if (typeof callback !== 'function') {
91 throw 'callback must be a function.';
92 }
93
94 SplitButtons.preSubmitCallbacks.push(callback);
95 };
96
97 $(SplitButtons.initializeSaveHandling);
98
99 return SplitButtons;
100 });