Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12694 anikendra 1
/* noUiSlider 3.2.1 */
2
(function ($) {
3
 
4
  $.fn.noUiSlider = function (options, flag) {
5
 
6
    // test for mouse, pointer or touch
7
    var EVENT = window.navigator.msPointerEnabled ? 2 : 'ontouchend' in document ? 3 : 1;
8
    if (window.debug && console) {
9
      console.log(EVENT);
10
    }
11
 
12
    // shorthand for test=function, calling
13
    function call(f, scope, args) {
14
      if (typeof f === "function") {
15
        f.call(scope, args);
16
      }
17
    }
18
 
19
    // function wrapper for calculating to and from range values
20
    var percentage = {
21
      to : function (range, value) {
22
        value = range[0] < 0 ? value + Math.abs(range[0]) : value - range[0];
23
        return (value * 100) / this._length(range);
24
      },
25
      from : function (range, value) {
26
        return (value * 100) / this._length(range);
27
      },
28
      is : function (range, value) {
29
        return ((value * this._length(range)) / 100) + range[0];
30
      },
31
      _length : function (range) {
32
        return (range[0] > range[1] ? range[0] - range[1] : range[1] - range[0]);
33
      }
34
    }
35
 
36
    // bounce handles of eachother, the edges of the slider
37
    function correct(proposal, slider, handle) {
38
 
39
      var
40
      setup = slider.data('setup'),
41
      handles = setup.handles,
42
      settings = setup.settings,
43
      pos = setup.pos;
44
 
45
      proposal = proposal < 0 ? 0 : proposal > 100 ? 100 : proposal;
46
 
47
      if (settings.handles == 2) {
48
        if (handle.is(':first-child')) {
49
          var other = parseFloat(handles[1][0].style[pos]) - settings.margin;
50
          proposal = proposal > other ? other : proposal;
51
        } else {
52
          var other = parseFloat(handles[0][0].style[pos]) + settings.margin;
53
          proposal = proposal < other ? other : proposal;
54
        }
55
      }
56
 
57
      if (settings.step) {
58
        var per = percentage.from(settings.range, settings.step);
59
        proposal = Math.round(proposal / per) * per;
60
      }
61
 
62
      return proposal;
63
 
64
    }
65
 
66
    // get standarised clientX and clientY
67
    function client(f) {
68
      try {
69
        return [(f.clientX || f.originalEvent.clientX || f.originalEvent.touches[0].clientX), (f.clientY || f.originalEvent.clientY || f.originalEvent.touches[0].clientY)];
70
      } catch (e) {
71
        return ['x', 'y'];
72
      }
73
    }
74
 
75
    // get native inline style value in %
76
    function place(handle, pos) {
77
      return parseFloat(handle[0].style[pos]);
78
    }
79
 
80
    // simplified defaults
81
    var defaults = {
82
      handles : 2,
83
      serialization : {
84
        to : ['', ''],
85
        resolution : 0.01
86
      }
87
    };
88
 
89
    // contains all methods
90
    methods = {
91
      create : function () {
92
 
93
        return this.each(function () {
94
 
95
          // set handle to position
96
          function setHandle(handle, to, slider) {
97
            handle.css(pos, to + '%').data('input').val(percentage.is(settings.range, to).toFixed(res));
98
          }
99
 
100
          var
101
          settings = $.extend(defaults, options),
102
          // handles
103
          handlehtml = '<a><div></div></a>',
104
          // save this to variable, // allows identification
105
          slider = $(this).data('_isnS_', true),
106
          // array of handles
107
          handles = [],
108
          // the way the handles are positioned for this slider, top/left
109
          pos,
110
          // for quick orientation testing and array matching
111
          orientation,
112
          // append classes
113
          classes = "",
114
          // tests numerical
115
          num = function (e) {
116
            return !isNaN(parseFloat(e)) && isFinite(e);
117
          },
118
          // counts decimals in serialization, sets default
119
          split = (settings.serialization.resolution = settings.serialization.resolution || 0.01).toString().split('.'),
120
          res = split[0] == 1 ? 0 : split[1].length;
121
 
122
          settings.start = num(settings.start) ? [settings.start, 0] : settings.start;
123
 
124
          // logs bad input values, if possible
125
          $.each(settings, function (a, b) {
126
 
127
            if (num(b)) {
128
              settings[a] = parseFloat(b);
129
            } else if (typeof b == "object" && num(b[0])) {
130
              b[0] = parseFloat(b[0]);
131
              if (num(b[1])) {
132
                b[1] = parseFloat(b[1]);
133
              }
134
            }
135
 
136
            var e = false;
137
            b = typeof b == "undefined" ? "x" : b;
138
 
139
            switch (a) {
140
            case 'range':
141
            case 'start':
142
              e = b.length != 2 || !num(b[0]) || !num(b[1]);
143
              break;
144
            case 'handles':
145
              e = (b < 1 || b > 2 || !num(b));
146
              break;
147
            case 'connect':
148
              e = b != "lower" && b != "upper" && typeof b != "boolean";
149
              break;
150
            case 'orientation':
151
              e = (b != "vertical" && b != "horizontal");
152
              break;
153
            case 'margin':
154
            case 'step':
155
              e = typeof b != "undefined" && !num(b);
156
              break;
157
            case 'serialization':
158
              e = typeof b != "object" || !num(b.resolution) || (typeof b.to == 'object' && b.to.length < settings.handles);
159
              break;
160
            case 'slide':
161
              e = typeof b != "function";
162
              break;
163
            }
164
 
165
            if (e && console) {
166
              console.error('Bad input for ' + a + ' on slider:', slider);
167
            }
168
 
169
          });
170
 
171
          settings.margin = settings.margin ? percentage.from(settings.range, settings.margin) : 0;
172
 
173
          // tests serialization to be strings or jQuery objects
174
          if (settings.serialization.to instanceof jQuery || typeof settings.serialization.to == 'string' || settings.serialization.to === false) {
175
            settings.serialization.to = [settings.serialization.to];
176
          }
177
 
178
          if (settings.orientation == "vertical") {
179
            classes += "vertical";
180
            pos = 'top';
181
            orientation = 1;
182
          } else {
183
            classes += "horizontal";
184
            pos = 'left';
185
            orientation = 0;
186
          }
187
 
188
          classes += settings.connect ? settings.connect == "lower" ? " connect lower" : " connect" : "";
189
 
190
          slider.addClass(classes);
191
 
192
          for (var i = 0; i < settings.handles; i++) {
193
 
194
            handles[i] = slider.append(handlehtml).children(':last');
195
            var setTo = percentage.to(settings.range, settings.start[i]);
196
            handles[i].css(pos, setTo + '%');
197
            if (setTo == 100 && handles[i].is(':first-child')) {
198
              handles[i].css('z-index', 2);
199
            }
200
 
201
            var bind = '.noUiSlider',
202
            onEvent = (EVENT === 1 ? 'mousedown' : EVENT === 2 ? 'MSPointerDown' : 'touchstart') + bind + 'X',
203
            moveEvent = (EVENT === 1 ? 'mousemove' : EVENT === 2 ? 'MSPointerMove' : 'touchmove') + bind,
204
            offEvent = (EVENT === 1 ? 'mouseup' : EVENT === 2 ? 'MSPointerUp' : 'touchend') + bind
205
 
206
            handles[i].find('div').on(onEvent, function (e) {
207
 
208
              $('body').bind('selectstart' + bind, function () {
209
                return false;
210
              });
211
 
212
              if (!slider.hasClass('disabled')) {
213
 
214
                $('body').addClass('TOUCH');
215
 
216
                var handle = $(this).addClass('active').parent(),
217
                unbind = handle.add($(document)).add('body'),
218
                originalPosition = parseFloat(handle[0].style[pos]),
219
                originalClick = client(e),
220
                previousClick = originalClick,
221
                previousProposal = false;
222
 
223
                $(document).on(moveEvent, function (f) {
224
 
225
                  f.preventDefault();
226
 
227
                  var currentClick = client(f);
228
 
229
                  if (currentClick[0] == "x") {
230
                    return;
231
                  }
232
 
233
                  currentClick[0] -= originalClick[0];
234
                  currentClick[1] -= originalClick[1];
235
 
236
                  var movement = [
237
                    previousClick[0] != currentClick[0], previousClick[1] != currentClick[1]
238
                  ],
239
                  proposal = originalPosition + ((currentClick[orientation] * 100) / (orientation ? slider.height() : slider.width()));
240
                  proposal = correct(proposal, slider, handle);
241
 
242
                  if (movement[orientation] && proposal != previousProposal) {
243
                    handle.css(pos, proposal + '%').data('input').val(percentage.is(settings.range, proposal).toFixed(res));
244
                    call(settings.slide, slider.data('_n', true));
245
                    previousProposal = proposal;
246
                    handle.css('z-index', handles.length == 2 && proposal == 100 && handle.is(':first-child') ? 2 : 1);
247
                  }
248
 
249
                  previousClick = currentClick;
250
 
251
                }).on(offEvent, function () {
252
 
253
                  unbind.off(bind);
254
                  $('body').removeClass('TOUCH');
255
                  if (slider.find('.active').removeClass('active').end().data('_n')) {
256
                    slider.data('_n', false).change();
257
                  }
258
                  // AKRANT
259
                  // REMOVE NEXT LINE TO RESTORE TO ORIGINAL
260
                  call(settings.slideEnd, slider.data('_n', true));
261
                });
262
 
263
              }
264
            }).on('click', function (e) {
265
              e.stopPropagation();
266
            });
267
 
268
          }
269
 
270
          if (EVENT == 1) {
271
            slider.on('click', function (f) {
272
              if (!slider.hasClass('disabled')) {
273
                var currentClick = client(f),
274
                proposal = ((currentClick[orientation] - slider.offset()[pos]) * 100) / (orientation ? slider.height() : slider.width()),
275
                handle = handles.length > 1 ? (currentClick[orientation] < (handles[0].offset()[pos] + handles[1].offset()[pos]) / 2 ? handles[0] : handles[1]) : handles[0];
276
                setHandle(handle, correct(proposal, slider, handle), slider);
277
                call(settings.slide, slider);
278
                slider.change();
279
              }
280
            });
281
          }
282
 
283
          for (var i = 0; i < handles.length; i++) {
284
            var val = percentage.is(settings.range, place(handles[i], pos)).toFixed(res);
285
            if (typeof settings.serialization.to[i] == 'string') {
286
              handles[i].data('input',
287
                slider.append('<input type="hidden" name="' + settings.serialization.to[i] + '">').find('input:last')
288
                .val(val)
289
                .change(function (a) {
290
                  a.stopPropagation();
291
                }));
292
            } else if (settings.serialization.to[i] == false) {
293
              handles[i].data('input', {
294
                val : function (a) {
295
                  if (typeof a != 'undefined') {
296
                    this.handle.data('noUiVal', a);
297
                  } else {
298
                    return this.handle.data('noUiVal');
299
                  }
300
                },
301
                handle : handles[i]
302
              });
303
            } else {
304
              handles[i].data('input', settings.serialization.to[i].data('handleNR', i).val(val).change(function () {
305
                  var arr = [null, null];
306
                  arr[$(this).data('handleNR')] = $(this).val();
307
                  slider.val(arr);
308
                }));
309
            }
310
          }
311
 
312
          $(this).data('setup', {
313
            settings : settings,
314
            handles : handles,
315
            pos : pos,
316
            res : res
317
          });
318
 
319
        });
320
      },
321
      val : function () {
322
 
323
        if (typeof arguments[0] !== 'undefined') {
324
 
325
          var val = typeof arguments[0] == 'number' ? [arguments[0]] : arguments[0];
326
 
327
          return this.each(function () {
328
 
329
            var setup = $(this).data('setup');
330
 
331
            for (var i = 0; i < setup.handles.length; i++) {
332
              if (val[i] != null) {
333
                var proposal = correct(percentage.to(setup.settings.range, val[i]), $(this), setup.handles[i]);
334
                setup.handles[i].css(setup.pos, proposal + '%').data('input').val(percentage.is(setup.settings.range, proposal).toFixed(setup.res));
335
              }
336
            }
337
          });
338
 
339
        } else {
340
 
341
          var handles = $(this).data('setup').handles,
342
          re = [];
343
          for (var i = 0; i < handles.length; i++) {
344
            re.push(parseFloat(handles[i].data('input').val()));
345
          }
346
          return re.length == 1 ? re[0] : re;
347
 
348
        }
349
      },
350
      disabled : function () {
351
        return flag ? $(this).addClass('disabled') : $(this).removeClass('disabled');
352
      }
353
    }
354
 
355
    // remap the native/current val function to noUiSlider
356
    var $_val = jQuery.fn.val;
357
 
358
    jQuery.fn.val = function () {
359
      return this.data('_isnS_') ? methods.val.apply(this, arguments) : $_val.apply(this, arguments);
360
    }
361
 
362
    return options == "disabled" ? methods.disabled.apply(this) : methods.create.apply(this);
363
 
364
  }
365
 
366
})(jQuery);