Subversion-Projekte lars-tiefland.ci

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
776 lars 1
var ComponentsNoUiSliders = function() {
2
 
3
    var demo2 = function() {
4
        var connectSlider = document.getElementById('demo2');
5
 
6
        noUiSlider.create(connectSlider, {
7
            start: [20],
8
            connect: false,
9
            range: {
10
                'min': 0,
11
                'max': 100
12
            }
13
        });
14
    }
15
 
16
    var demo3 = function() {
17
        var connectSlider = document.getElementById('demo3');
18
 
19
        noUiSlider.create(connectSlider, {
20
            start: [20, 80],
21
            connect: false,
22
            range: {
23
                'min': 0,
24
                'max': 100
25
            }
26
        });
27
 
28
        var connectBar = document.createElement('div'),
29
            connectBase = connectSlider.getElementsByClassName('noUi-base')[0],
30
            connectHandles = connectSlider.getElementsByClassName('noUi-origin');
31
 
32
        // Give the bar a class for styling and add it to the slider.
33
        connectBar.className += 'connect';
34
        connectBase.appendChild(connectBar);
35
 
36
        connectSlider.noUiSlider.on('update', function( values, handle ) {
37
 
38
            // Pick left for the first handle, right for the second.
39
            var side = handle ? 'right' : 'left',
40
            // Get the handle position and trim the '%' sign.
41
                offset = (connectHandles[handle].style.left).slice(0, - 1);
42
 
43
            // Right offset is 100% - left offset
44
            if ( handle === 1 ) {
45
                offset = 100 - offset;
46
            }
47
 
48
            connectBar.style[side] = offset + '%';
49
        });
50
    }
51
 
52
    var demo4 = function() {
53
        //** init the select
54
        var select = document.getElementById('demo4_select');
55
 
56
        // Append the option elements
57
        for ( var i = -20; i <= 40; i++ ) {
58
            var option = document.createElement("option");
59
                option.text = i;
60
                option.value = i;
61
            select.appendChild(option);
62
        }
63
 
64
        //** init the slider
65
        var html5Slider = document.getElementById('demo4');
66
 
67
        noUiSlider.create(html5Slider, {
68
            start: [ 10, 30 ],
69
            connect: true,
70
            range: {
71
                'min': -20,
72
                'max': 40
73
            }
74
        });
75
 
76
        //** init the input
77
        var inputNumber = document.getElementById('demo4_input');
78
 
79
        html5Slider.noUiSlider.on('update', function( values, handle ) {
80
 
81
            var value = values[handle];
82
 
83
            if ( handle ) {
84
                inputNumber.value = value;
85
            } else {
86
                select.value = Math.round(value);
87
            }
88
        });
89
 
90
        select.addEventListener('change', function(){
91
            html5Slider.noUiSlider.set([this.value, null]);
92
        });
93
 
94
        inputNumber.addEventListener('change', function(){
95
            html5Slider.noUiSlider.set([null, this.value]);
96
        });
97
    }
98
 
99
    var demo5 = function() {
100
        var nonLinearSlider = document.getElementById('demo5');
101
 
102
        noUiSlider.create(nonLinearSlider, {
103
            connect: true,
104
            behaviour: 'tap',
105
            start: [ 500, 4000 ],
106
            range: {
107
                // Starting at 500, step the value by 500,
108
                // until 4000 is reached. From there, step by 1000.
109
                'min': [ 0 ],
110
                '10%': [ 500, 500 ],
111
                '50%': [ 4000, 1000 ],
112
                'max': [ 10000 ]
113
            }
114
        });
115
 
116
        // Write the CSS 'left' value to a span.
117
        function leftValue ( handle ) {
118
            return handle.parentElement.style.left;
119
        }
120
 
121
        var lowerValue = document.getElementById('demo5_lower-value'),
122
            upperValue = document.getElementById('demo5_upper-value'),
123
            handles = nonLinearSlider.getElementsByClassName('noUi-handle');
124
 
125
        // Display the slider value and how far the handle moved
126
        // from the left edge of the slider.
127
        nonLinearSlider.noUiSlider.on('update', function ( values, handle ) {
128
            if ( !handle ) {
129
                lowerValue.innerHTML = values[handle] + ', ' + leftValue(handles[handle]);
130
            } else {
131
                upperValue.innerHTML = values[handle] + ', ' + leftValue(handles[handle]);
132
            }
133
        });
134
    }
135
 
136
    var demo6 = function() {
137
        // Store the locked state and slider values.
138
        var lockedState = false,
139
            lockedSlider = false,
140
            lockedValues = [60, 80],
141
            slider1 = document.getElementById('demo6_slider1'),
142
            slider2 = document.getElementById('demo6_slider2'),
143
            lockButton = document.getElementById('demo6_lockbutton'),
144
            slider1Value = document.getElementById('demo6_slider1-span'),
145
            slider2Value = document.getElementById('demo6_slider2-span');
146
 
147
        // When the button is clicked, the locked
148
        // state is inverted.
149
        lockButton.addEventListener('click', function(){
150
            lockedState = !lockedState;
151
            this.textContent = lockedState ? 'unlock' : 'lock';
152
        });
153
 
154
        function crossUpdate ( value, slider ) {
155
 
156
            // If the sliders aren't interlocked, don't
157
            // cross-update.
158
            if ( !lockedState ) return;
159
 
160
            // Select whether to increase or decrease
161
            // the other slider value.
162
            var a = slider1 === slider ? 0 : 1, b = a ? 0 : 1;
163
 
164
            // Offset the slider value.
165
            value -= lockedValues[b] - lockedValues[a];
166
 
167
            // Set the value
168
            slider.noUiSlider.set(value);
169
        }
170
 
171
        noUiSlider.create(slider1, {
172
            start: 60,
173
 
174
            // Disable animation on value-setting,
175
            // so the sliders respond immediately.
176
            animate: false,
177
            range: {
178
                min: 50,
179
                max: 100
180
            }
181
        });
182
 
183
        noUiSlider.create(slider2, {
184
            start: 80,
185
            animate: false,
186
            range: {
187
                min: 50,
188
                max: 100
189
            }
190
        });
191
 
192
        slider1.noUiSlider.on('update', function( values, handle ){
193
            slider1Value.innerHTML = values[handle];
194
        });
195
 
196
        slider2.noUiSlider.on('update', function( values, handle ){
197
            slider2Value.innerHTML = values[handle];
198
        });
199
 
200
        function setLockedValues ( ) {
201
            lockedValues = [
202
                Number(slider1.noUiSlider.get()),
203
                Number(slider2.noUiSlider.get())
204
            ];
205
        }
206
 
207
        slider1.noUiSlider.on('change', setLockedValues);
208
        slider2.noUiSlider.on('change', setLockedValues);
209
 
210
        // The value will be send to the other slider,
211
        // using a custom function as the serialization
212
        // method. The function uses the global 'lockedState'
213
        // variable to decide whether the other slider is updated.
214
        slider1.noUiSlider.on('slide', function( values, handle ){
215
            crossUpdate(values[handle], slider2);
216
        });
217
 
218
        slider2.noUiSlider.on('slide', function( values, handle ){
219
            crossUpdate(values[handle], slider1);
220
        });
221
    }
222
 
223
    var demo7 = function() {
224
        var softSlider = document.getElementById('demo7');
225
 
226
        noUiSlider.create(softSlider, {
227
            start: 50,
228
            range: {
229
                min: 0,
230
                max: 100
231
            },
232
            pips: {
233
                mode: 'values',
234
                values: [20, 80],
235
                density: 4
236
            }
237
        });
238
 
239
        softSlider.noUiSlider.on('change', function ( values, handle ) {
240
            if ( values[handle] < 20 ) {
241
                softSlider.noUiSlider.set(20);
242
            } else if ( values[handle] > 80 ) {
243
                softSlider.noUiSlider.set(80);
244
            }
245
        });
246
    }
247
 
248
    var demo8 = function() {
249
        var tooltipSlider = document.getElementById('demo8');
250
 
251
        noUiSlider.create(tooltipSlider, {
252
            start: [40, 50],
253
            connect: true,
254
            range: {
255
                'min': 30,
256
                '30%': 40,
257
                'max': 50
258
            }
259
        });
260
 
261
        var tipHandles = tooltipSlider.getElementsByClassName('noUi-handle'),
262
            tooltips = [];
263
 
264
        // Add divs to the slider handles.
265
        for ( var i = 0; i < tipHandles.length; i++ ){
266
            tooltips[i] = document.createElement('div');
267
            tipHandles[i].appendChild(tooltips[i]);
268
        }
269
 
270
        // Add a class for styling
271
        tooltips[1].className += 'noUi-tooltip';
272
        // Add additional markup
273
        tooltips[1].innerHTML = '<strong>Value: </strong><span></span>';
274
        // Replace the tooltip reference with the span we just added
275
        tooltips[1] = tooltips[1].getElementsByTagName('span')[0];
276
 
277
        // Add a class for styling
278
        tooltips[0].className += 'noUi-tooltip';
279
        // Add additional markup
280
        tooltips[0].innerHTML = '<strong>Value: </strong><span></span>';
281
        // Replace the tooltip reference with the span we just added
282
        tooltips[0] = tooltips[0].getElementsByTagName('span')[0];
283
 
284
        // When the slider changes, write the value to the tooltips.
285
        tooltipSlider.noUiSlider.on('update', function( values, handle ){
286
            tooltips[handle].innerHTML = values[handle];
287
        });
288
    }
289
 
290
    return {
291
        //main function to initiate the module
292
        init: function() {
293
            demo2();
294
            demo3();
295
            demo4();
296
            demo5();
297
            demo6();
298
            demo7();
299
            demo8();
300
        }
301
 
302
    };
303
 
304
}();
305
 
306
jQuery(document).ready(function() {
307
   ComponentsNoUiSliders.init();
308
});