forked from RubyLouvre/avalon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index15datepicker.html
259 lines (243 loc) · 12.2 KB
/
index15datepicker.html
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>datepicker</title>
<script src="avalon.js"></script>
<script>
avalon.define("datepicker", function(vm) {
//配置
vm.changeYear = false
vm.changeMonth = false
vm.minDate = new Date(2013, 3, 25);
//vm.maxDate
vm.showOtherMonths = false;
vm.showButtonPanel = false;
//当前时间
vm.selectedDate = new Date;
vm.currentDate = new Date;
vm.currentMonth = vm.currentDate.getMonth();
vm.currentYear = vm.currentDate.getFullYear();
vm.currentWeeks = getWeeks(vm.currentDate);
//显示顶部的年份与月份
vm.title = {
get: function() {
var format = "";
if (!this.changeYear && this.changeMonth) {
format = "yyyy年";
} else if (this.changeYear && !this.changeMonth) {
format = "MMMM";
} else if (!this.changeYear && !this.changeMonth) {
format = "MMMM yyyy年";
}
return format && avalon.filters.date(this.currentDate, format);
}
};
//星期显示
vm.$weeks = "日一二三四五六".split("");
//月份下拉菜单
vm.$months = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
//当月的日期
function isDisabled(time) {
var disabled = false;
if (vm.minDate) {
disabled = time < vm.minDate;
}
if (disabled && vm.maxDate) {
disabled = time > vm.maxDate;
}
return disabled - 0;
}
function getWeeks(cur) {
vm.currentDate = cur;
var year = cur.getFullYear();
var month = cur.getMonth();//得到今天是几月(0 ~ 11)
var date = cur.getDate(); //得到今天是几号 (1 ~ 31)
cur.setMonth(month + 1);//改为下一个月,
//由于日期是1 ~ 31, 0则是退到上一个月的最后一天,于是得到这个月的总天数
cur.setDate(0);
var num = cur.getDate();
var next = 6 - cur.getDay();
var dates = avalon.range(1, num + 1);
dates = dates.map(function(d) {
var time = new Date(year, month, d)
return [year, month, d, isDisabled(time)].join("-");
});
cur.setMonth(month);
cur.setDate(1);//得到当月的第一天
var prev = cur.getDay();//0 ~ 6
cur.setDate(date);//还原
for (var i = 0; i < prev; i++) {//补上上一个月的日期
cur = new Date(year, month, -1 * i)
dates.unshift([year, cur.getMonth(), cur.getDate(), isDisabled(cur)].join("-"))
}
for (var i = 0; i < next; i++) {//补上下一个月的日期
cur = new Date(year, month + 1, i + 1)
dates.push([year, cur.getMonth(), cur.getDate(), isDisabled(cur)].join("-"))
}
var ret = [];
while (dates.length) {//每行七个分组
ret.push(dates.splice(0, 7));
}
return ret;//一个三维数组
}
//取得当年的前后20年
function getYears() {
var y = vm.currentYear;
return avalon.range(y - 10, y + 10);
}
vm.candidateYears = getYears();
//点击事件
vm.theme = "start";
vm.nextMonth = function(e) {
e.preventDefault()
var d = vm.currentDate;
var m = d.getMonth();
d.setMonth(m + 1);
m = d.getMonth();
if (m === 0) {
var y = d.getFullYear();
vm.currentYear = y;
}
vm.currentMonth = m;
};
//点击事件
vm.prevMonth = function(e) {
e.preventDefault();
var d = vm.currentDate;
var m = d.getMonth();
d.setMonth(m - 1);
m = d.getMonth();
if (m === 11) {
var y = d.getFullYear();
vm.currentYear = y;
}
vm.currentMonth = m;
};
//侦听
vm.$watch("currentMonth", function(val) {
var d = vm.currentDate;
d.setMonth(val);
vm.currentWeeks = getWeeks(d);
vm.title = NaN;
});
vm.$watch("currentYear", function(val) {
var d = vm.currentDate;
val = ~~val
d.setFullYear(val)
vm.currentWeeks = getWeeks(d);
vm.title = NaN;
});
//高亮当前选中的日期
vm.selectDay = function(e) {
e.preventDefault()
var el = e.target;
if (el.tagName === "A" && el.parentNode.tagName === "TD" && !/disabled/.test(el.className)) {
var d = el.$vmodel.day.split('-')
vm.selectedDate = new Date(d[0], d[1], d[2]);
}
};
//高亮今天的日期
var today = new Date;
var atoday = [today.getFullYear(), today.getMonth(), today.getDate()];
vm.isToday = function() {
var day = this.$vmodel.day;
return day.slice(0, day.lastIndexOf("-")) === atoday.join("-");
};
vm.backToday = function() {
vm.currentMonth = atoday[1];
vm.currentYear = atoday[0];
}
});
</script>
</head>
<body ms-controller="datepicker">
<h3 style="text-align: center">datepicker</h3>
<div ms-controller="datepicker">
<div id="ui-datepicker-div" class="ui-datepicker ui-widget ui-widget-content ui-helper-clearfix ui-corner-all" style="display:block">
<div class="ui-datepicker-header ui-widget-header ui-helper-clearfix ui-corner-all">
<a class="ui-datepicker-prev ui-corner-all" title="Prev"
ms-click="prevMonth"
ms-hover="ui-state-hover"
ms-hover-1="ui-datepicker-prev-hover"
>
<span class="ui-icon ui-icon-circle-triangle-w">Prev</span></a>
<a class="ui-datepicker-next ui-corner-all" title="Next"
ms-click="nextMonth"
ms-hover="ui-state-hover"
ms-hover-1="ui-datepicker-next-hover"
>
<span class="ui-icon ui-icon-circle-triangle-e">Next</span></a>
<div class="ui-datepicker-title">
<select ms-each-month="$months" ms-if="changeMonth" ms-duplex="currentMonth" >
<option ms-value="{{month}}" ms-selected="currentMonth == month">{{month+1}}月</option>
</select>
<select ms-each-year="candidateYears" ms-if="changeYear" ms-duplex="currentYear" >
<option ms-value="{{year}}" ms-selected="currentYear == year">{{year}}年</option>
</select>
{{title}}
</div>
</div>
<table class="ui-datepicker-calendar" >
<thead>
<tr ms-each-date="$weeks">
<th ms-class-ui-datepicker-week-end="$first">
<span title="星期{{date}}">{{date}}</span>
</th>
</tr>
</thead>
<tbody ms-each-week="currentWeeks" ms-click="selectDay">
<tr ms-each-day="week">
<td ms-class-ui-datepicker-other-month="Number(day.split('-')[1]) != currentMonth"
ms-class-ui-datepicker-week-end="$first || $last"
ms-class-ui-state-disabled="day.split('-')[3] == 1"
ms-class-ui-datepicker-unselectable="day.split('-')[3] == 1"
>
<a class="ui-state-default" ms-title='currentMonth' href="#"
ms-if="showOtherMonths || Number(day.split('-')[1]) == currentMonth"
ms-hover="ui-state-hover"
ms-class-ui-state-highlight="isToday"
>{{day.split('-')[2]}} </a>
</td>
</tbody>
</table>
<div class="ui-datepicker-buttonpane ui-widget-content" ms-if="showButtonPanel">
<button type="button" class="ui-datepicker-current ui-state-default ui-priority-secondary ui-corner-all"
ms-hover="ui-state-hover"
ms-click="backToday"
>Today</button>
<button type="button" class="ui-datepicker-close ui-state-default ui-priority-primary ui-corner-all"
ms-hover="ui-state-hover"
>Done</button>
</div>
</div>
<link rel="stylesheet" ms-href="http://code.jquery.com/ui/1.10.3/themes/{{theme}}/jquery-ui.css" >
<div><input type="radio" ms-model="changeMonth" />可选择月份</div>
<div><input type="radio" ms-model="changeYear" />可选择年份</div>
<div><input type="radio" ms-model="showButtonPanel" />显示按钮面板</div>
<div><input type="radio" ms-model="showOtherMonths" />显示其他月份的日期</div>
<div><select ms-model="theme">
<option value='start'>start</option>
<option value='smoothness'>smoothness</option>
<option value='flick'>flick</option>
<option value='sunny'>sunny</option>
<option value='excite-bike'>excite bike</option>
<option value='black-tie'>black tie</option>
<option value='trontastic'>trontastic</option>
<option value='swanky-purse'>swanky purse</option>
<option value='le-frog'>le frog</option>
<option value='blitzer'>blitzer</option>
<option value='dot-luv'>dot luv</option>
<option value='mint-choc'>mint-choc</option>
<option value='hot-sneaks'>hot sneaks</option>
<option value='south-street'> south street</option>
<option value='humanity'>humanity</option>
<option value='vader'>vader</option>
<option value='eggplant'>eggplant</option>
<option value='cupertino'>cupertino</option>
<option value='overcast'>overcast</option>
</select>你喜欢的皮肤</div>
<p>你选择的日期为 {{selectedDate | date('yyyy-MM-dd') }}</p>
</div>
</body>
</html>