79e23a10f559b6c3a42d251001ab4a0a0d954bf6
[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'], function($) {
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 if (e.currentTarget.tagName === 'A' && !e.isDefaultPrevented()) {
55 $form.submit();
56 e.preventDefault();
57 }
58 });
59 };
60
61 /**
62 * Adds a callback being executed before submit
63 *
64 * @param {function} callback
65 */
66 SplitButtons.addPreSubmitCallback = function(callback) {
67 if (typeof callback !== 'function') {
68 throw 'callback must be a function.';
69 }
70
71 SplitButtons.preSubmitCallbacks.push(callback);
72 };
73
74 $(SplitButtons.initializeSaveHandling);
75
76 return SplitButtons;
77 });