|
1
|
|
|
/*
|
|
2
|
|
|
* jQuery UI Slider Access
|
|
3
|
|
|
* By: Trent Richardson [http://trentrichardson.com]
|
|
4
|
|
|
* Version 0.2
|
|
5
|
|
|
* Last Modified: 12__regexoperators___/12/2011
|
|
6
|
|
|
*
|
|
7
|
|
|
* Copyright 2011 Trent Richardson
|
|
8
|
|
|
* Dual licensed under the MIT and GPL licenses.
|
|
9
|
|
|
* http://trentrichardson.com/Impromptu/GPL-LICENSE.txt
|
|
10
|
|
|
* http://trentrichardson.com/Impromptu/MIT-LICENSE.txt
|
|
11
|
|
|
*
|
|
12
|
|
|
*/
|
|
13
|
|
|
(function ($) {
|
|
14
|
|
|
|
|
15
|
|
|
$.fn.extend({
|
|
16
|
|
|
sliderAccess: function (options) {
|
|
17
|
|
|
options = options || {};
|
|
18
|
|
|
options.touchonly = options.touchonly !== undefined ? options.touchonly : true; // by default only show it if touch device
|
|
19
|
|
|
|
|
20
|
|
|
if (options.touchonly === true && !("ontouchend" in document))
|
|
21
|
|
|
return $(this);
|
|
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
return $(this).each(function (i, obj) {
|
|
|
|
|
|
|
24
|
|
|
var $t = $(this),
|
|
25
|
|
|
o = $.extend({}, {
|
|
26
|
|
|
where: 'after',
|
|
27
|
|
|
step: $t.slider('option', 'step'),
|
|
28
|
|
|
upIcon: 'ui-icon-plus',
|
|
29
|
|
|
downIcon: 'ui-icon-minus',
|
|
30
|
|
|
text: false,
|
|
31
|
|
|
upText: '+',
|
|
32
|
|
|
downText: '-',
|
|
33
|
|
|
buttonset: true,
|
|
34
|
|
|
buttonsetTag: 'span',
|
|
35
|
|
|
speed: 150
|
|
36
|
|
|
}, options),
|
|
37
|
|
|
$buttons = $('<' + o.buttonsetTag + ' class="ui-slider-access">' +
|
|
38
|
|
|
'<button data-icon="' + o.downIcon + '" data-step="-' + o.step + '">' + o.downText + '</button>' +
|
|
39
|
|
|
'<button data-icon="' + o.upIcon + '" data-step="' + o.step + '">' + o.upText + '</button>' +
|
|
40
|
|
|
'</' + o.buttonsetTag + '>');
|
|
41
|
|
|
|
|
42
|
|
|
$buttons.children('button').each(function (j, jobj) {
|
|
|
|
|
|
|
43
|
|
|
var $jt = $(this),
|
|
44
|
|
|
timeout = null,
|
|
45
|
|
|
increment = function($jt, $t, e) {
|
|
46
|
|
|
var step = $jt.data('step'),
|
|
47
|
|
|
curr = $t.slider('value'),
|
|
48
|
|
|
newval = curr += step * 1,
|
|
|
|
|
|
|
49
|
|
|
minval = $t.slider('option', 'min'),
|
|
50
|
|
|
maxval = $t.slider('option', 'max');
|
|
51
|
|
|
e.preventDefault();
|
|
52
|
|
|
if (newval < minval || newval > maxval)
|
|
53
|
|
|
return;
|
|
|
|
|
|
|
54
|
|
|
$t.slider('value', newval);
|
|
55
|
|
|
$t.slider("option", "slide").call($t, null, { value: newval });
|
|
56
|
|
|
};
|
|
57
|
|
|
|
|
58
|
|
|
$jt.button({
|
|
59
|
|
|
text: o.text,
|
|
60
|
|
|
icons: { primary: $jt.data('icon') }
|
|
61
|
|
|
})
|
|
62
|
|
|
.bind('touchstart mousedown', function (e) {
|
|
63
|
|
|
increment($jt, $t, e);
|
|
64
|
|
|
timeout = setInterval(function () {
|
|
65
|
|
|
increment($jt, $t, e);
|
|
66
|
|
|
}, o.speed);
|
|
67
|
|
|
});
|
|
68
|
|
|
|
|
69
|
|
|
$(document).bind('touchend mouseup', function () {
|
|
70
|
|
|
clearInterval(timeout);
|
|
71
|
|
|
return false;
|
|
72
|
|
|
});
|
|
73
|
|
|
|
|
74
|
|
|
});
|
|
75
|
|
|
|
|
76
|
|
|
// before or after
|
|
77
|
|
|
$t[o.where]($buttons);
|
|
78
|
|
|
|
|
79
|
|
|
if (o.buttonset) {
|
|
80
|
|
|
$buttons.removeClass('ui-corner-right').removeClass('ui-corner-left').buttonset();
|
|
81
|
|
|
$buttons.eq(0).addClass('ui-corner-left');
|
|
82
|
|
|
$buttons.eq(1).addClass('ui-corner-right');
|
|
83
|
|
|
}
|
|
84
|
|
|
|
|
85
|
|
|
// adjust the width so we don't break the original layout
|
|
86
|
|
|
var bOuterWidth = $buttons.css({
|
|
87
|
|
|
marginLeft: (o.where == 'after' ? 10 : 0),
|
|
88
|
|
|
marginRight: (o.where == 'before' ? 10 : 0)
|
|
89
|
|
|
}).outerWidth(true) + 5;
|
|
90
|
|
|
var tOuterWidth = $t.outerWidth(true);
|
|
91
|
|
|
$t.css('display', 'inline-block').width(tOuterWidth - bOuterWidth);
|
|
92
|
|
|
});
|
|
93
|
|
|
}
|
|
94
|
|
|
});
|
|
95
|
|
|
|
|
96
|
|
|
})(jQuery); |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.