-
Notifications
You must be signed in to change notification settings - Fork 147
/
jQRuler.js
128 lines (103 loc) · 2.81 KB
/
jQRuler.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
/**
* jQRangeSlider
* A javascript slider selector that supports dates
*
* Copyright (C) Guillaume Gautreau 2013
* Dual licensed under the MIT or GPL Version 2 licenses.
*/
(function($){
"use strict";
var scaleDefaults = {
first: function(start){
return start;
},
next: function(value){
return value + 1;
},
format: function () {
return;
},
label: function(tick){
return Math.round(tick);
},
stop: function(){
return false;
}
};
$.widget("ui.ruler", {
options: {
min: 0,
max: 100,
scales: []
},
_create: function(){
this.element.addClass("ui-ruler");
this._createScales();
},
destroy: function(){
this.element.removeClass("ui-ruler");
this.element.empty();
this._super();
},
_regenerate: function(){
this.element.empty();
this._createScales();
},
_setOption: function(key, value){
if (key === "min" || key === "max" && value !== this.options[key]){
this.options[key] = value;
this._regenerate();
return;
}
if (key === "scales" && value instanceof Array){
this.options.scales = value;
this._regenerate();
return;
}
},
_createScales: function(){
if (this.options.max === this.options.min){
return;
}
for (var i = 0; i < this.options.scales.length; i++){
this._createScale(this.options.scales[i], i);
}
},
_createScale: function(opt, index){
var options = $.extend({}, scaleDefaults, opt),
container = $("<div class='ui-ruler-scale' />").appendTo(this.element);
container.addClass("ui-ruler-scale" + index);
this._createTicks(container, options);
},
_createTicks: function(container, scaleOptions){
var start,
end = scaleOptions.first(this.options.min, this.options.max),
difference = this.options.max - this.options.min,
first = true,
width, tick;
do{
start = end;
end = scaleOptions.next(start);
width = (Math.min(end, this.options.max) - Math.max(start, this.options.min)) / difference;
tick = this._createTick(start, end, scaleOptions);
container.append(tick);
tick.css("width", 100 * width + "%");
if (first && start > this.options.min){
tick.css("margin-left", 100 * (start - this.options.min) / difference + "%");
}
first = false;
}while(!this._stop(scaleOptions, end));
},
_stop: function(scaleOptions, value){
return scaleOptions.stop(value) || value >= this.options.max;
},
_createTick: function(start, end, scaleOptions){
var container = $("<div class='ui-ruler-tick' style='display:inline-block' />"),
inner = $("<div class='ui-ruler-tick-inner' />").appendTo(container),
label = $("<span class='ui-ruler-tick-label' />").appendTo(inner);
label.text(scaleOptions.label(start, end));
scaleOptions.format(container, start, end);
return container;
}
});
}(jQuery));