ba70c781798bbbe6293dba5aaec73d22c6124937
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Resources / Public / JavaScript / QueryGenerator.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 this source code.
10 *
11 * The TYPO3 project - inspiring people to share!
12 */
13
14 /**
15 * This module handle the QueryGenerator forms.
16 */
17 define('TYPO3/CMS/Core/QueryGenerator', ['jquery', 'TYPO3/CMS/Backend/DateTimePicker', 'TYPO3/CMS/Backend/jquery.clearable'], function ($) {
18
19 /**
20 * The QueryGenerator AMD module
21 *
22 * @type {{form: null, limitField: null}}
23 */
24 var QueryGenerator = {
25 form: null,
26 limitField: null
27 };
28
29 /**
30 * Initialize the QueryGenerator object
31 */
32 QueryGenerator.initialize = function() {
33 QueryGenerator.form = $('form[name="queryform"]');
34 QueryGenerator.limitField = $('#queryLimit');
35 QueryGenerator.form.on('click', '.t3js-submit-click', function(e) {
36 e.preventDefault();
37 QueryGenerator.doSubmit();
38 });
39 QueryGenerator.form.on('change', '.t3js-submit-change', function(e) {
40 e.preventDefault();
41 QueryGenerator.doSubmit();
42 });
43 QueryGenerator.form.on('click', '.t3js-limit-submit button', function(e) {
44 e.preventDefault();
45 QueryGenerator.setLimit($(this).data('value'));
46 QueryGenerator.doSubmit();
47 });
48 QueryGenerator.form.on('click', '.t3js-addfield', function(e) {
49 e.preventDefault();
50 QueryGenerator.addValueToField($(this).data('field'), $(this).val());
51 });
52 QueryGenerator.form.find('.t3js-clearable').clearable({
53 onClear: function() {
54 QueryGenerator.doSubmit();
55 }
56 });
57 };
58
59 /**
60 * Submit the form
61 */
62 QueryGenerator.doSubmit = function() {
63 QueryGenerator.form.submit();
64 };
65
66 /**
67 * Set query limit
68 *
69 * @param {string} value
70 */
71 QueryGenerator.setLimit = function(value) {
72 QueryGenerator.limitField.val(value);
73 };
74
75 /**
76 * Add value to text field
77 *
78 * @param {string} field the name of the field
79 * @param {string} value the value to add
80 */
81 QueryGenerator.addValueToField = function(field, value) {
82 var $target = QueryGenerator.form.find('[name="' + field + '"]');
83 var currentValue = $target.val();
84 $target.val(currentValue + ',' + value);
85 };
86
87 // Initialize
88 QueryGenerator.initialize();
89 return QueryGenerator;
90 });