992382392c3152035514b8faac73c4c92262930f
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Resources / Public / JavaScript / ValueSlider.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 * Implementation of the value slider for input text elements.
16 *
17 * Module: TYPO3/CMS/Backend/ValueSlider
18 */
19 define(['jquery', 'twbs/bootstrap-slider'], function($) {
20 /**
21 * ValueSlider object
22 *
23 * @type {{selector: string}}
24 * @exports TYPO3/CMS/Backend/ValueSlider
25 */
26 var ValueSlider = {
27 selector: '[data-slider-id]'
28 };
29
30 /**
31 * Initialize all slider elements
32 */
33 ValueSlider.initializeSlider = function() {
34 var $sliders = $(ValueSlider.selector);
35 if ($sliders.length > 0) {
36 $sliders.slider({
37 formatter: ValueSlider.renderTooltipValue
38 });
39 $sliders.on('change', ValueSlider.updateValue);
40 }
41 };
42
43 /**
44 * Update value of slider element
45 *
46 * @param {Event} e
47 */
48 ValueSlider.updateValue = function(e) {
49 var $slider = $(e.currentTarget),
50 $foreignField = $('[data-formengine-input-name="' + $slider.data('sliderItemName') + '"]'),
51 sliderField = $slider.data('sliderField'),
52 sliderCallbackParams = $slider.data('sliderCallbackParams');
53
54 $foreignField.val(e.value.newValue);
55 TBE_EDITOR.fieldChanged.apply(TBE_EDITOR, sliderCallbackParams);
56 };
57
58 /**
59 *
60 * @param {Number} value
61 * @returns {*}
62 */
63 ValueSlider.renderTooltipValue = function(value) {
64 var renderedValue,
65 $slider = $('[data-slider-id="' + this.id + '"]'),
66 data = $slider.data();
67 switch (data.sliderValueType) {
68 case 'double':
69 renderedValue = parseFloat(value).toFixed(2);
70 break;
71 case 'int':
72 default:
73 renderedValue = parseInt(value);
74 }
75
76 return renderedValue;
77 };
78
79 // init if document is ready
80 $(document).ready(function() {
81 ValueSlider.initializeSlider();
82 });
83
84 return ValueSlider;
85 });