-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemp2.html
258 lines (177 loc) · 6.04 KB
/
temp2.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
<!DOCTYPE html>
<htmllang="en">
<head>
<title># Month generation PERF tests</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="https://cdn.rawgit.com/necolas/normalize.css/master/normalize.css" />
<style>
html { box-sizing: border-box; font-size:62.5%; font-family: arial, helvetica, sans-serif; }
*, *:before, *:after { box-sizing: inherit; }
body { padding:40px 0; background: #fafafa; font-size:1.6rem; color:#333; }
i { font-style:normal; }
#r_dp {
width: 95%;
margin:0 auto;
position:relative;
backface-visibility:hidden;
transform:translateZ(0);
}
.r_month { width: 280px; float:left; margin-right:20px; margin-bottom:20px; background: #fff; }
table { width:100%; border-collapse:collapse; }
td {
width:auto;
padding: 5px; border:1px solid #ddd;
text-align:center;
}
.r_title {
padding:10px 5px;
text-align:center;
font-size:12px;
display:block;
font-style:normal;
width:100%;
background: #03A9F4;
color:#fff;
text-transform:uppercase;
}
button,h3 { margin:0 auto; position:relative; padding:10px; display: block; text-align:center; }
button { background:hotpink; color:#fff; border:0; outline:0; padding: 15px 20px }
h3 { margin:20px auto; }
</style>
</head>
<body id="body">
<button id="gen">Generate months</button>
<div id="r_dp">
</div><!-- /#r_dp -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
// helpers
function _id(e) { return document.getElementById(e); }
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); }
function isTouch() { return 'ontouchstart' in window || navigator.maxTouchPoints; };
/* temp button stuff */
var randBg = ['hotpink','#03A9F4','orange','#3c3','red','#333'];
randBg = randBg[Math.floor(Math.random() * randBg.length)];
var gen = _id('gen');
gen.style.background = randBg;
var touch = isTouch();
if(!touch){
$(gen).on('click', function(e){ e.preventDefault(); $(this).trigger('ontouchstart'); })
}
/*-----------------------------------------------------
GET DAYS FUNCTION
Should return a string
-----------------------------------------------------*/
function getMonth(month, year, monthArr, dayArr, dayString){
var days = [];
// set days 1
var date = new Date(year, month, 0),
totalDays = date.getDate(),
endDay = date.getDay();
// set days 2
date.setDate(0);
var startDay = date.getDay(0),
nextMonthStart = false,
prevMonthDays = 0;
// check if startdate isn't 0 (startdate of current month starts at first index)
if( this.startDay !== 0 ) { prevMonthDays = (new Date(year,month-1,0)).getDate() - startDay; }
// Begin looping through days
var count = 0,
day,
i=0,
out='';
for(i; i < 42; i++) {
day = {};
if(i < startDay) {
// prev months days
day.date = prevMonthDays = prevMonthDays + 1;
}
else if(i > totalDays + (startDay - 1)) {
// days after current month
day.date = count = count + 1;
if (!nextMonthStart) nextMonthStart = i
}
else {
// regular in-month days
day.date = i - startDay + 1;
}
days[days.length] = day.date
}; // end loop
// BEGIN OUTPUT
out += '<div class="r_month" rel="'+ month + '/' + year +'"><em class="r_title">' + monthArr[month-1] + ' <span>' + year + '</span></em><table><thead><tr>';
out += dayString; // add pre-generated day string
// end thead, begin tbody
out += '</tr></thead><tbody>';
// All days (generates ~90k elements @ 1000 months)
i=0;
for(var key in days){ // 42x / month
i++;
// START row
if(i === 1) out += '<tr>';
// not in current month
if( key < startDay || ( key >= nextMonthStart ) ) {
out += '<td class="r_nc"> </td>';
}
else {
out += '<td><i>'+days[key]+'</i></td>'; // stupid to set this at each <td>, set base @ base table instead then use BASE + this.innerText instead
}
// END row
if(i === 7) { out += '</tr>'; i=0; }
}
// push end
out += '</tbody></table></div>'; // end .r_table etc
// return string
return out;
}
// apply click handler
gen.ontouchstart = function(){
var monthsNum = 1000; // Number of months to generate
var totalNum = monthsNum;
var month = 1, // zero-based index
year = 2016,
out = '',
monthArr = 'January,February,March,April,May,June,Juli,August,September,October,November,December'.split(','),
dayArr = 'Mon,Tue,Wed,Thu,Fri,Sat,Sun'.split(','),
r_dp = _id('r_dp'),
days;
// Pre-process day output outside loop since it's static
// Note: this will run on each click as it is now since nothing of this is saved
var i=0, l=dayArr.length, dayString = '';
for(i;i<l;i++){ dayString += '<td>' + dayArr[i] + '</td>'; }
// Loop through each month
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; }
// run getMonths function
out += getMonth(month, year, monthArr, dayArr, dayString);
// pre-push output (split html for draw performance)
if( totalNum-monthsNum === 2 ) {
var pf = performance.now();
// output
r_dp.innerHTML = out;
out = ''; // reset
var pe = performance.now();
var l = 'getMonth perf @ 2 months: ' + Math.round((pe-pf)*100)/100 + 'ms';
console.log(l);
r_dp.innerHTML = '<h3>' + l + '</h3>' + r_dp.innerHTML;
}
} // end while-loop
// output out-array to #dp
// add to own stack and execute last
setTimeout(function(){
// Begin perf measure
var perfStart = performance.now();
r_dp.innerHTML = r_dp.innerHTML + out;
var perfEnd = performance.now();
var log = '@ '+ totalNum +' months: ' + Math.round((perfEnd-perfStart)*100)/100 + 'ms';
console.log(log);
r_dp.querySelector('h3').innerHTML = r_dp.querySelector('h3').innerHTML + ' | ' + log;
}, 4)
return false;
} // end event handler
</script>
</body>
</html>