-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathswitchy.js
130 lines (108 loc) · 4.13 KB
/
switchy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
!function ($) {
"use strict"; // jshint ;_;
/* SWITCHY CLASS DEFINITION
* ====================== */
var Switchy = function (element, options) {
this.options = options;
this.$element = $(element);
this.$container = $("<div class='switchy-container'></div>");
this.$bar = $("<div class='switchy-bar'></div>");
this.$slider = $("<div class='switchy-slider' draggable='true'></div>");
this.$options = $(element).children('option');
this.numberOfOptions = this.$options.length;
this.initialOptionIndex = this.$options.filter('[value="'+$(element).val()+'"]').index();
this.init();
}
Switchy.prototype = {
constructor: Switchy,
lastSliderPosition: null,
init: function(){
var that = this;
// hide original select
this.$element.css({ position: 'absolute', left: '-9999px' })
// Prepare the slider for the DOM
this.$container.append(this.$bar.append(this.$slider));
// Append the slider to the DOM
this.$element.after(this.$container);
this.lastSliderPosition = this.initialOptionIndex;
var barGrid = this.$bar.innerWidth() / (this.numberOfOptions - 1);
// Position slider to initial value
this.$slider.css({
left: that.sliderPosition(barGrid, this.initialOptionIndex)
});
// When original select is updated
this.$element.on('change', function(e){
var nextOptionIndex = that.$options.filter('[value="'+that.$element.val()+'"]').index();
if (that.lastSliderPosition != nextOptionIndex){
that.moveSliderTo(barGrid, nextOptionIndex, false);
}
});
if (this.$slider.drag != undefined && this.options.draggable == true){
this.$slider.
drag('end', function(ev, dd){
var currentSliderPosition = that.$slider.position().left + (that.$slider.outerWidth(true) / 2),
currentOptionIndex = Math.round(currentSliderPosition / barGrid);
that.moveSliderTo(barGrid, currentOptionIndex, true);
}).
drag(function(ev, dd){
var limit = {
left: 0,
right: that.$bar.innerWidth() - that.$slider.outerWidth(true)
}
$(this).css({
left: Math.min(limit.right, Math.max(limit.left, dd.offsetX))
});
}, { relative: true });
}
this.$bar.on('click', function(e){
var currentSliderPosition = that.$slider.position().left,
currentOptionIndex = Math.ceil(currentSliderPosition / barGrid),
clickPosition = e.pageX - that.$bar.offset().left,
nextOptionIndex = Math.round(clickPosition / barGrid);
if (currentOptionIndex != nextOptionIndex){
// move slider position
that.moveSliderTo(barGrid, nextOptionIndex, true);
}
});
},
sliderPosition: function(barGrid, optionIndex){
var add = null;
if (optionIndex == 0){
add = 0;
} else if (optionIndex == this.numberOfOptions - 1){
add = -(this.$slider.outerWidth(true));
} else {
add = -(this.$slider.outerWidth(true) / 2);
}
return (barGrid * optionIndex) + add;
},
moveSliderTo: function(barGrid, nextOptionIndex, triggerChange){
var leftPosition = this.sliderPosition(barGrid, nextOptionIndex)
// move slider position
if (leftPosition != null){
this.$slider.animate({
left: leftPosition
}, "fast");
}
// update original select value
this.$options.removeAttr('selected');
this.$options.eq(nextOptionIndex).prop('selected', 'selected');
if (triggerChange == true)
this.$element.trigger('change');
this.lastSliderPosition = nextOptionIndex;
}
}
/* SWITCHY PLUGIN DEFINITION
* ======================= */
$.fn.switchy = function (option) {
return this.each(function () {
var $this = $(this),
options = $.extend({}, $.fn.switchy.defaults, typeof option == 'object' && option);
new Switchy(this, options);
})
}
$.fn.switchy.defaults = {
draggable: true
}
$.fn.switchy.Constructor = Switchy
}(window.jQuery);