jquery.dateField_wev8.js
7.24 KB
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
/*
Copyright (c) 2009 Open Lab
Written by Roberto Bicchierai http://roberto.open-lab.com
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
jQuery.fn.dateField = function(options) {
//check if the input field is passed correctly
if (!options.inputField){
console.error("You must supply an input field");
return false;
}
// -------------------------- start default option values --------------------------
if (typeof(options.firstDayOfWeek) == "undefined")
options.firstDayOfWeek=Date.firstDayOfWeek;
if (typeof(options.useWheel) == "undefined")
options.useWheel=true;
if (typeof(options.dateFormat) == "undefined")
options.dateFormat=Date.defaultFormat;
// -------------------------- end default option values --------------------------
// ------------------ start
if(options.inputField.is("[readonly]") || options.inputField.is("[disabled]"))
return;
var calendar = {currentDate: new Date()};
calendar.options = options;
//build the calendar on the first element in the set of matched elements.
var theOpener = this.eq(0);
var theDiv=$("<div>").addClass("calBox");
//create calendar elements elements
var divNavBar = $("<div>").addClass("calNavBar");
var divDays = $("<div>").addClass("calDay");
divDays.addClass("calFullMonth");
theDiv.append(divNavBar).append(divDays);
if (options.isSearchField){
var divShortcuts=$("<div>").addClass("shortCuts").html("<span title='last quarter'>LQ</span> <span title='last month'>LM</span> <span title='this month'>M</span> <span title='last week'>LW</span> <span title='this week'>W</span> <span title='yesterday'>Y</span> <span title='today'>T</span><span title='tomorrow'>TO</span> <span title='next week'>NW</span> <span title='next month'>NM</span> <span title='this quarter'>Q</span> <span title='next quarter'>NQ</span>");
divShortcuts.click(function(ev){
var el=$(ev.target);
if(el.is("span")){
if (!options.isSearchField)
options.inputField.val(Date.parseString(el.text().trim(),options.dateFormat).format(options.dateFormat),true);
else
options.inputField.val(el.text().trim());
theDiv.remove();
}
});
theDiv.append(divShortcuts);
}
$("body").append(theDiv);
nearBestPosition(theOpener,theDiv);
theDiv.bringToFront();
//register for click outside. Delayed to avoid it run immediately
$("body").oneTime(100, "regclibodcal", function() {
$("body").bind("click.dateField", function() {
$(this).unbind("click.dateField");
theDiv.remove();
});
});
calendar.drawCalendar = function(date) {
calendar.currentDate = date;
var fillNavBar = function(date) {
var t = new Date(date.getTime());
divNavBar.empty();
t.setMonth(t.getMonth()-1);
var spanPrev = $("<span>").addClass("calElement noCallback prev").attr("millis", t.getTime());
t.setMonth(t.getMonth()+1);
var spanMonth = $("<span>").html(t.format("MMMM yyyy"));
t.setMonth(t.getMonth()+1);
var spanNext = $("<span>").addClass("calElement noCallback next").attr("millis", t.getTime());
divNavBar.append(spanPrev).append(spanMonth).append(spanNext);
};
var fillDaysFullMonth = function(date) {
divDays.empty();
var t = new Date();//today
var w = parseInt((theDiv.width()-4-(4*7))/7)+"px";
// draw day headers
var d = new Date(date);
d.setFirstDayOfThisWeek(options.firstDayOfWeek);
for (var i = 0; i < 7; i++) {
var span = $("<span>").addClass("calDayHeader").attr("day", d.getDay());
span.css("width",w);
span.html(Date.dayAbbreviations[d.getDay()]);
//call the dayHeaderRenderer
if (typeof(options.dayHeaderRenderer) == "function")
options.dayHeaderRenderer(span,d.getDay());
divDays.append(span);
d.setDate(d.getDate()+1);
}
//draw cells
d = new Date(date);
d.setDate(1); // set day to start of month
d.setFirstDayOfThisWeek(options.firstDayOfWeek);//go to first day of week
var i=0;
while ((d.getMonth()<=date.getMonth() && d.getFullYear()<=date.getFullYear()) || d.getFullYear()<date.getFullYear() || (i%7!=0)) {
var span = $("<span>").addClass("calElement day").attr("millis", d.getTime());
span.html("<span class=dayNumber>" + d.getDate() + "</span>").css("width",w);
if (d.getYear() == t.getYear() && d.getMonth() == t.getMonth() && d.getDate() == t.getDate())
span.addClass("today");
if (d.getYear() == date.getYear() && d.getMonth() == date.getMonth() && d.getDate() == date.getDate())
span.addClass("selected");
if(d.getMonth()!=date.getMonth())
span.addClass("calOutOfScope");
//call the dayRenderer
if (typeof(options.dayRenderer) == "function")
options.dayRenderer(span,d);
divDays.append(span);
d.setDate(d.getDate()+1);
i++;
}
};
fillNavBar(date);
fillDaysFullMonth(date);
};
theDiv.click(function(ev) {
var el = $(ev.target).closest(".calElement");
if (el.size() > 0) {
var date = new Date(parseInt(el.attr("millis")));
if (el.hasClass("day")) {
theDiv.remove();
if (!el.is(".noCallback")) {
options.inputField.val(date.format(options.dateFormat)).attr("millis", date.getTime()).focus();
if (typeof(options.callback) == "function")
options.callback(date);
}
} else {
calendar.drawCalendar(date);
}
}
ev.stopPropagation();
});
//if mousewheel
if ($.event.special.mousewheel && options.useWheel) {
divDays.mousewheel(function(event, delta) {
var d = new Date(calendar.currentDate.getTime());
d.setMonth(d.getMonth() + delta);
calendar.drawCalendar(d);
return false;
});
}
// start calendar to the date in the input
var dateStr=options.inputField.val();
if (!dateStr || !Date.isValid(dateStr,options.dateFormat,true)){
calendar.drawCalendar(new Date());
} else {
var date = Date.parseString(dateStr,options.dateFormat,true);
//set date string formatted
if (!options.isSearchField)
options.inputField.val(date.format(options.dateFormat)).attr("millis",date.getTime());
calendar.drawCalendar(date);
}
return calendar;
};