Skip to content

Commit af6ebeb

Browse files
author
Tero Piirainen
committed
datepicker cleanup while making it's documentation
1 parent 07693af commit af6ebeb

8 files changed

+478
-249
lines changed

src/form/form.datepicker.js

+172-88
Large diffs are not rendered by default.

src/form/form.slider.js

+20-22
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
max: 100,
2727
step: 0, // Specifies the value granularity of the element's value (onSlide callbacks)
2828
value: 0,
29-
decimals: 2,
29+
accuracy: 2,
3030
vertical: false,
3131
keyboard: true,
3232

@@ -46,8 +46,8 @@
4646
return Math.round(value / step) * step;
4747
}
4848

49-
function round(value, decimals) {
50-
var n = Math.pow(10, decimals);
49+
function round(value, accuracy) {
50+
var n = Math.pow(10, accuracy);
5151
return Math.round(value * n) / n;
5252
}
5353

@@ -75,23 +75,16 @@
7575
// get (HTML5) attributes into configuration
7676
$.each("min,max,step,value".split(","), function(i, key) {
7777
var val = input.attr(key);
78-
if (val || val === 0) {;
78+
if (val || val === 0) {
7979
conf[key] = parseFloat(val, 10);
8080
}
8181
});
8282

8383
var range = conf.max - conf.min;
8484

85-
/* replace build-in range element for cross browser consistency
86-
NOTE: input.attr("type", "text") throws exception by the browser */
87-
85+
// Replace built-in date input: NOTE: input.attr("type", "text") throws exception by the browser
8886
if (input[0].getAttribute("type") == 'range') {
89-
var tmp = $('<input/>')
90-
.attr("type", "text")
91-
.addClass(input.attr("className"))
92-
.attr("name", input.attr("name"))
93-
.attr("disabled", input.attr("disabled")).val(input.val());
94-
87+
var tmp = input.clone().attr("type", "text");
9588
input.replaceWith(tmp);
9689
input = tmp;
9790
}
@@ -106,15 +99,15 @@
10699

107100
// fit inside the slider
108101
x = Math.min(Math.max(0, x), len);
109-
102+
110103
// increment in steps
111104
if (conf.step) {
112105
x = toSteps(x, conf.step, len);
113106
}
114107

115108
// calculate value
116109
var isClick = e && e.originalEvent && e.originalEvent.type == "click",
117-
v = round(x / len * range + conf.min, conf.decimals);
110+
v = round(x / len * range + conf.min, conf.accuracy);
118111

119112
// onSlide
120113
if (value !== undefined && !isClick) {
@@ -154,7 +147,7 @@
154147

155148
setValue: function(val, e) {
156149
val = parseFloat(val);
157-
if (val === NaN || val == value) { return self; }
150+
if (isNaN(val) || val == value) { return self; }
158151
var x = (val - conf.min) * (len / range);
159152
return seek(x, e);
160153
},
@@ -180,8 +173,8 @@
180173
},
181174

182175
step: function(am, e) {
183-
var x = Math.max(len / conf.step || conf.size || 10, 2);
184-
return seek(pos + x * am, e);
176+
if (conf.max <= 1) { am /= 10; }
177+
return self.setValue(value + am);
185178
},
186179

187180
next: function() {
@@ -270,11 +263,11 @@
270263

271264
// UP: k=75, l=76, up=38, pageup=33, right=39
272265
if (up) {
273-
slider.step(e.ctrlKey || key == 33 ? 5 : 1, e);
266+
slider.step(e.ctrlKey || key == 33 ? 10 : 1, e);
274267

275268
// DOWN: j=74, h=72, down=40, pagedown=34, left=37
276269
} else if (down) {
277-
slider.step(e.ctrlKey || key == 34 ? -5 : -1, e);
270+
slider.step(e.ctrlKey || key == 34 ? -10 : -1, e);
278271
}
279272
return e.preventDefault();
280273
}
@@ -286,7 +279,12 @@
286279
});
287280
}
288281

289-
282+
$.expr[':'].range = function(el) {
283+
var type = el.getAttribute("type");
284+
return type && type == 'range' || !!$(el).filter("input").data("slider");
285+
};
286+
287+
290288
// jQuery plugin implementation
291289
$.fn.slider = function(conf) {
292290

@@ -304,7 +302,7 @@
304302
els = els ? els.add(input) : input;
305303
});
306304

307-
return els;
305+
return els ? els : this;
308306
};
309307

310308

test/form/datepicker.htm

+27-132
Original file line numberDiff line numberDiff line change
@@ -4,150 +4,32 @@
44
<script src="../js/jquery-1.3.2.js"></script>
55
<script src="../../src/form/form.datepicker.js"></script>
66

7-
<style>
8-
/*{{{ style */
9-
#picker {
10-
width:198px;
11-
padding:2px;
12-
background-color:#ddd;
13-
border:1px solid #ccc;
14-
-moz-border-radius:3px;
15-
position:relative;
16-
font-family:"Lucida Grande","bitstream vera sans";
17-
font-size:11px;
18-
overflow:hidden;
19-
}
20-
21-
#head {
22-
background:#789;
23-
padding:2px;
24-
height:22px;
25-
}
26-
27-
#pickertitle {
28-
font-size:12px;
29-
color:#fff;
30-
float:left;
31-
text-align:center;
32-
width:150px;
33-
line-height:2px;
34-
}
35-
36-
#pm, #nm {
37-
display:block;
38-
width:20px;
39-
height:20px;
40-
border:1px solid #ccc;
41-
float:left;
42-
}
43-
44-
#nm {
45-
float:right;
46-
}
47-
48-
#days {
49-
height:14px;
50-
background-color:#456;
51-
color:#fff;
52-
}
53-
54-
#days span {
55-
display:block;
56-
float:left;
57-
width:28px;
58-
text-align:center;
59-
}
60-
61-
#weeks {
62-
background-color:#fff;
63-
margin-top:4px;
64-
}
65-
66-
.week {
67-
clear:left;
68-
height:24px;
69-
}
70-
71-
.week:hover {
72-
background-color:#efefef;
73-
}
74-
.week a {
75-
display:block;
76-
float:left;
77-
width:25px;
78-
height:20px;
79-
text-decoration:none;
80-
font-size:11px;
81-
border:1px solid #ddd;
82-
margin-left:1px;
83-
text-align:center;
84-
line-height:20px;
85-
}
86-
87-
.week a:hover, .focus {
88-
background-color:#789;
89-
color:#fff;
90-
}
917

92-
.sun {
93-
color:red;
94-
}
95-
96-
.off {
97-
color:#666;
98-
}
99-
100-
.disabled {
101-
background-color:#efefef !important;
102-
color:#ccc !important;
103-
cursor:default;
104-
}
105-
106-
.week a.off:hover {
107-
background-color:#ddd;
108-
}
109-
110-
#pickertitle select {
111-
font-size:10px;
112-
}
113-
114-
#today {
115-
background-color:blue;
116-
color:#fff;
117-
}
118-
119-
/* other stuff */
120-
input {
121-
width:200px;
122-
font-size:11px;
123-
font-family:"verdana";
8+
<style>
9+
body {
10+
font-family:"lucida grande","verdana","sans serif";
11+
padding:50px 100px;
12412
}
12513

126-
.trigger {
14+
.picktrigger {
12715
width:30px;
12816
height:30px;
12917
border:1px solid blue;
13018
}
19+
</style>
13120

132-
/*}}}*/
21+
<link rel="stylesheet" type="text/css" href="picker.css"/>
13322

134-
</style>
13523

136-
<h2>Minimal setup</h2>
24+
<h2>Datepicker test page</h2>
13725

138-
<p>Here is the minimal setup for datepicker. Just an <samp>INPUT</samp> with the <samp>type</samp> attribute fixed</p>
13926

14027
<!-- http://www.apple.com/iphone/iphone-3gs/more-features.html -->
14128
<input type="date" class="date" name="joku" />
14229

143-
<h3>HTML coding</h3>
144-
145-
The demo only uses the following <samp>INPUT</samp> element for datepicker.
14630

147-
<h3>Another one</h3>
148-
149-
<input type="date" class="date" data-value="2004-03-20" min="2004-02-20"/>
150-
<div class="trigger"></div>
31+
<input type="date" class="date" data-value="2004-03-20" min="2004-02-20" readonly="true" />
32+
<div class="picktrigger"></div>
15133

15234
<script>
15335
$(function() {
@@ -156,21 +38,34 @@ <h3>Another one</h3>
15638
selectors: true,
15739
// offset: [10, 10],
15840
yearRange: [-3, 3],
159-
easing: 'linear',
16041
speed: 0,
16142
firstDay: 0,
16243
min: 365 * 1,
16344
max: 365 * 2,
16445

16546
change: function(e, date) {
166-
console.info(date, "picked");
47+
console.info(date, "picked", this.getDate("yy-mm-dd"));
48+
},
49+
50+
onShow: function(e) {
51+
console.info("onShow", this, e);
52+
},
53+
54+
onHide: function(e) {
55+
console.info("onHide", this.getDate(), e, this);
16756
}
16857

16958
}).one("change", function() {
170-
console.info("binded event");
59+
console.info("binded event");
60+
61+
}).one("onBeforeShow", function(e) {
62+
console.info("before show", e);
63+
return true;
17164
});
17265

173-
66+
$(":date:first").change(function() {
67+
console.info("JOOOOOOOOOOOO", this, arguments);
68+
});
17469
});
17570
</script>
17671

0 commit comments

Comments
 (0)