-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest4.js
301 lines (204 loc) · 6.97 KB
/
test4.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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*-----------------------------------------------------
*******************************************************
*****************************************************
LIGHTRANGE
Simple date range selection
2016-01-28
Tommie Hansen
*****************************************************
*******************************************************
-----------------------------------------------------*/
// INIT
;(function($, window, document, undefined) {
'use strict';
/* Start performance measurement */
var perfStart = performance.now();
/* Helpers */
function _id(e) { return document.getElementById(e); }
//function _e(e) { return document.querySelector(e); }
//function _ee(e) { return document.querySelectorAll(e); }
//function _for(e,f) { var i, len=e.length; for(i=0;i<len;i++){ f(e[i]); }}
function _for(e,f) { var i, len=e.length; for(i=0;i<len;i++){ if(f(e[i]) === false) break; }}
function _data(e,attr) { return e.getAttribute('data-' + attr); }
// break-version for escaping loops via return false;
/* Base VARs */
var main = _id('cal'),
r_ovr = _id('r_ovr'),
r_dp = _id('r_dp'),
r_actions = _id('r_actions'),
r_nav = _id('r_nav'),
isInit = false,
curDate;
/* BASE conf VARs */
var dayLongArr,
monthArr,
dayArr;
/*-----------------------------------------------------
Begin rangeCal ns
-----------------------------------------------------*/
var rangeCal = {
/* Init */
init: function(field){
// set arrays
dayLongArr = _data(field, 'days').split(',');
monthArr = _data(field, 'months').split(',');
dayArr = [];
var monthsNum = _data(field, 'monthsNum').split(','),
offset = monthsNum[1],
monthsNum = monthsNum[0];
// create 3-letter days
_for(dayLongArr, function(e){
dayArr.push( e.substring(0, 3) );
});
// Bind field
field.onclick = function(){
rangeCal.show(monthsNum, offset);
}
},
/* Show */
show: function(monthsNum, offset){
// add classes
main.className = 'show';
r_ovr.className = 'on';
// use isInit to check if the calendar has already been triggered
// else just show the calendar without re-processing month and binds
if(!isInit){
var now = new Date(),
month = now.getMonth(), // note: getMonth = zero-based index
year = now.getFullYear();
// apply offset
month = month + parseInt(offset);
curDate = month + ',' + year;
this.getMonth(monthsNum, curDate); // get month
this.binds(monthsNum); // initiliaze all binds once
isInit = 1;
}
},
/* Hide */
hide: function(){
main.className = '';
},
/* Error */
err: function(){},
/* Hover Range */
hoverRange: function(){},
/* Navigation */
nav: function(){},
/* Binds */
binds: function(monthsNum){
// click day
$(main).on('click', 'tbody td', function(){
rangeCal.select(this);
});
// month navigation
$(r_nav).on('click', '.nav', function(){ rangeCal.nav(this, monthsNum); } )
// OK/Cancel buttons
$(r_actions).on('click', 'button', function(){
var ok = true;
// Cancel
if( this.className.indexOf('red') > -1 ){
// reset form and close (or just close)
}
// OK button
else {
// validate selections
}
// close if OK
if(ok) rangeCal.hide();
})
},
/* Navigation */
nav: function(e, monthsNum){
var curX,
curId = e.id;
var cur, month, year;
cur = curDate.split(',');
month = cur[0];
year = cur[1];
if( curId == 'next' ) {
month++;
if( month > 11 ) { month=0; year++; }
_id('prev').classList.remove('off');
}
else if( curId == 'prev' ) { // else if because people uses 'next' more then 'prev'
month--;
if( month < 0 ) { month=11; year--; }
}
curDate = month+','+year; // set new curdate
rangeCal.getMonth(monthsNum, curDate); // get next/prev months
},
/*-----------------------------------------------------
LARGE FUNCTIONS
-----------------------------------------------------*/
/* User select action */
select: function(){},
/* Set dates */
set: function(){},
/* Generate a month */
genMonth: function(year, month){
// month info, fork off of github
var monthData = function(){
var date=new Date(year,month,0);this.totalDays=date.getDate();this.endDay=date.getDay();date.setDate(0);this.startDay=date.getDay(0);this.days=[];this.nextMonthStart=false;var prevMonthDays=0;if(this.startDay!==0)prevMonthDays=(new Date(year,month-1,0)).getDate()-this.startDay;var count=0;for(var i=0;i<42;i++){var day={};if(i<this.startDay)day.date=prevMonthDays=prevMonthDays+1;else if(i>this.totalDays+(this.startDay-1)){day.date=count=count+1;if(!this.nextMonthStart)this.nextMonthStart=i}else day.date=i-this.startDay+1;this.days[this.days.length]=day.date}
};
var m = {};
monthData.call(m, year, month); // returns object to m, wants 1-12 index
return m;
},
/* Get month */
getMonth: function(monthsNum, curDate){
var perfStart = performance.now(); // Begin perf measure
var now = curDate.split(','),
month = now[0],
year = now[1],
out = [], // out array
i,
nextMonth,
nextYear;
// Loop through monthsNum
while(monthsNum--){
month++; // add 1 month for each iteration
// apply new year if month becomes 13, (jan = 1, dec = 12 etc)
if(month === 13) { year++; month = 1; }
// generate month
var m = this.genMonth(year, month);
/* Begin output */
// Month title/year + weekdays
out.push('<div class="r_month"><em class="r_title">' + monthArr[month-1] + ' <span>' + year + '</span></em><table><thead><tr>');
_for(dayArr, function(e){ out.push('<td>' + e + '</td>'); }) // push weekday names
// end thead, begin tbody
out.push('</tr></thead><tbody>');
// begin days
i=0;
var key;
for(key in m.days){
i++;
// START row
if(i === 1) out.push('<tr>');
// not in current month
if( key < m.startDay || (key >= m.nextMonthStart) ) {
out.push('<td class="notCurMonth" data-date="'+ '' +'"><i>'+m.days[key]+'</i></td>');
}
else {
out.push('<td data-date="'+ curDate +'"><i>'+m.days[key]+'</i></td>');
}
// END row
if(i === 7) { out.push('</tr>'); i=0; }
} // end for-loop
// push end
out.push('</tbody></table></div>'); // end .r_table etc
} // end loop
// output out-array to #dp
r_dp.innerHTML = out.join('');
// PERF: Output to log
var perfEnd = performance.now();
var str = 'getMonth perf: ' + (perfEnd-perfStart) + 'ms';
console.log(str);
} // end getMonth
};
/* INIT ON RUN */
rangeCal.init(_id('dateField'));
/* LOG PERFORMANCE */
var perfEnd = performance.now();
var str = 'init perf: ' + (perfEnd-perfStart) + 'ms';
console.log(str);
})(jQuery,window,document);