forked from jianganglu/school-time-table
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
506 lines (461 loc) · 15.1 KB
/
index.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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
<!DOCTYPE html>
<html>
<head>
<title>school-time-table-demo</title>
</head>
<link href="http://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet">
<style>
.school-time-table {
font-size: 14px;
color: #555;
border: 1px solid #868686;
border-collapse: collapse;
}
.school-time-table thead tr:first-child {
background-color: #e8e8e8;
}
.school-time-table thead th {
padding: 10px 15px;
font-weight: 400;
text-align: center;
}
.school-time-table thead .prev {
text-align: right;
}
.school-time-table thead .next {
text-align: left;
}
.school-time-table thead .date {
margin-top: 5px;
color: #d8d8d8;
}
.school-time-table thead .dow {
border: 1px solid #868686;
}
.school-time-table tbody td {
padding: 10px 15px;
border: 1px solid #868686;
cursor: pointer;
}
.school-time-table tbody .event {
background-color: #b1d1f1;
}
.school-time-table tbody .event-content {
height: 20px;
}
</style>
<body>
<!-- <table class="school-time-table">
<thead>
<tr>
<th class="prev" colspan="3">prev</th>
<th colspan="2">8月份,2016</th>
<th class="next" colspan="3">next</th>
</tr>
<tr>
<th class="dow"></th>
<th class="dow">
<div>周一</div>
<div class="date">2016/8/4</div>
</th>
<th class="dow">
<div>周二</div>
<div class="date">2016/8/5</div>
</th>
<th class="dow">
<div>周三</div>
<div class="date">2016/8/6</div>
</th>
<th class="dow">
<div>周四</div>
<div class="date">2016/8/7</div>
</th>
<th class="dow">
<div>周五</div>
<div class="date">2016/8/8</div>
</th>
<th class="dow">
<div>周六</div>
<div class="date">2016/8/9</div>
</th>
<th class="dow">
<div>周日</div>
<div class="date">2016/8/10</div>
</th>
</tr>
</thead>
<tbody>
<tr>
<td>第一节课</td>
<td></td>
<td class="event">
<div class="event-content" data-toggle="tooltip" data-placement="auto" data-html="true" data-original-title="开始时间:2016/8/1 9:00<br>开始时间:2016/8/1 12:00<br>课程:C语言设计<br>联系人:陈涛<br>电话:1383454663">
</div>
</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>第二节课</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>第三节课</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>第四节课</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>第五节课</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>第六节课</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>第七节课</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>第八节课</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table> -->
<div id="table-box"></div>
<div class="modal fade" id="modal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Modal title</h4>
</div>
<div class="modal-body">
<label class="">开始</label>
<input class="start_class" type="text" value=""/>
<label>结束</label>
<input class="over_class" type="text" value="">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary btn-save">Save changes</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<script src="http://cdn.bootcss.com/jquery/3.1.0/jquery.js"></script>
<script src="http://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.js"></script>
<script src="http://cdn.bootcss.com/underscore.js/1.8.3/underscore.js"></script>
<script>
;(function($) {
'use strict';
var TimeTable = function(element, options) {
this.options = options;
this.$element = $(element);
};
TimeTable.prototype = {
init: function() {
var self = this;
if(!self.options.defaultDate) {
self.options.defaultDate = new Date();
}
var weekFirstDay = self.showWeekFirstDay(self.options.defaultDate);
var weekLastDay = self.showWeekLastDay(self.options.defaultDate);
// console.log(self.getDates(weekFirstDay, weekLastDay));
// console.log(self.fill());
self.fill(weekFirstDay, weekLastDay);
self.click();
},
fill: function(start_date, end_date) {
var self = this;
var weekFirstDay = start_date ? start_date : self.showWeekFirstDay();
var weekLastDay = end_date ? end_date : self.showWeekFirstDay();
self.$element.data('weekFirstDay', weekFirstDay);
self.$element.data('weekLastDay', weekLastDay);
console.log(self);
var dates = self.getDates(weekFirstDay, weekLastDay);
var class_list= ['第一节课', '第二节课', '第三节课', '第四节课', '第五节课', '第六节课', '第七节课', '第八节课'];
var html = '';
html = '<table class="school-time-table">\
<thead>\
<tr>\
<th class="prev" colspan="3">prev</th>\
<th colspan="2">'+ (weekFirstDay.getMonth() + 1) +'月份,'+ weekFirstDay.getFullYear() +'</th>\
<th class="next" colspan="3">next</th>\
</tr>\
<tr>\
<th class="dow"></th>\
<th class="dow">\
<div>周一</div>\
<div class="date">'+ dates[0] +'</div>\
</th>\
<th class="dow">\
<div>周二</div>\
<div class="date">'+ dates[1] +'</div>\
</th>\
<th class="dow">\
<div>周三</div>\
<div class="date">'+ dates[2] +'</div>\
</th>\
<th class="dow">\
<div>周四</div>\
<div class="date">'+ dates[3] +'</div>\
</th>\
<th class="dow">\
<div>周五</div>\
<div class="date">'+ dates[4] +'</div>\
</th>\
<th class="dow">\
<div>周六</div>\
<div class="date">'+ dates[5] +'</div>\
</th>\
<th class="dow">\
<div>周日</div>\
<div class="date">'+ dates[6] +'</div>\
</th>\
</tr>\
</thead>\
<tbody>';
$.each(class_list, function(key, value) {
html += '<tr>\
<td>'+ value +'</td>';
$.each(dates, function(key1, value1) {
var is_event = false;
$.each(self.options.events, function(key2, value2) {
if((value2.date === value1) ) {
var begin = +value2.begin_class;
var end = +value2.over_class;
for(var i = begin; i <= end; i++) {
if(i === (key+1)) {
is_event = true;
html += '<td class="event" data-date="'+ value1 +'" data-index="'+ (key+1) +'" data-courseid="'+ value2.course_id +'">\
<div class="event-content" data-toggle="tooltip" data-placement="auto" data-html="true" data-original-title="开始时间:2016/8/1 9:00<br>开始时间:2016/8/1 12:00<br>课程:C语言设计<br>联系人:陈涛<br>电话:1383454663">\
</div>\
</td>';
return;
}
}
}
});
if(!is_event) {
html += '<td data-date="'+ value1 +'" data-index="'+ (key+1) +'"></td>';
}
})
html += '</tr>';
});
html += '</tbody>\
</table>';
this.$element.children().remove().end().append(html);
$('.event-content').tooltip();
},
click: function() {
var self = this;
self.$element.on('click', '.prev', function() {
var start_date = self.showDate(self.$element.data('weekFirstDay'), -7);
var end_date = self.showDate(self.$element.data('weekLastDay'), - 7);
self.$element.data('weekFirstDay', start_date);
self.$element.data('weekLastDay', end_date);
if(self.options.prev) {
self.options.prev(start_date, end_date);
}else {
self.fill(start_date, end_date);
}
}).on('click', '.next', function() {
var start_date = self.showDate(self.$element.data('weekFirstDay'), 7);
var end_date = self.showDate(self.$element.data('weekLastDay'), 7);
self.$element.data('weekFirstDay', start_date);
self.$element.data('weekLastDay', end_date);
if(self.options.next) {
self.options.next(start_date, end_date);
}else {
self.fill(start_date, end_date);
}
}).on('click', 'td', function(e) {
var elm = this;
if(self.options.click) {
self.options.click(elm);
}
});
},
//本周第一天日期
showWeekFirstDay: function(date) {
var now = new Date(date);
var weekFirstDay = new Date(now-(now.getDay()-1)*86400000);
return weekFirstDay;
},
//本周最后一天日期
showWeekLastDay: function(date) {
var nowDate = new Date(date);
var weekFirstDay = new Date(nowDate-(nowDate.getDay()-1)*86400000);
var weekLastDay = new Date((weekFirstDay/1000+6*86400)*1000);
return weekLastDay;
},
//获取前N天或后N天的日期
showDate: function(date, n) {
var current_date = date;
var uom = new Date(current_date - 0 + (n * 86400000));
return uom;
},
//返回某天到某天日期
getDates: function(start_date, end_date) {
var dateArray = [];
var current_date = start_date;
while(current_date <= end_date) {
dateArray.push(current_date.Format('yyyy-MM-dd'));
current_date = current_date.addDays(1);
}
return dateArray;
}
};
// 对Date的扩展,将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
// 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
// 例子:
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
Date.prototype.Format = function (fmt) { //author: meizz
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
Date.prototype.addDays = function(days) {
var dat = new Date(this.valueOf());
dat.setDate(dat.getDate() + days);
return dat;
};
window.TimeTable = TimeTable;
})(jQuery);
;(function($) {
'use strict';
$(function() {
var course_list = [];
var $modal = $('#modal');
var time_table;
$.ajax({
type: 'GET',
url: '/data/data.json'
}).done(function(resp) {
if(resp.code === 'ACK') {
time_table = new TimeTable($('#table-box'), {
// defaultDate: '2016-08-16',
events: resp.data,
prev: function(start_date, end_date) {
var self = this;
$.ajax({
type: 'GET',
url: '/data/data.json'
}).done(function(resp) {
if(resp.code === 'ACK') {
self.events = resp.data;
time_table.fill(start_date, end_date);
}
});
},
next: function(start_date, end_date) {
var self = this;
$.ajax({
type: 'GET',
url: '/data/data.json'
}).done(function(resp) {
if(resp.code === 'ACK') {
self.events = resp.data;
time_table.fill(start_date, end_date);
}
});
},
click: function(eml) {
var date = $(eml).data('date');
var classes = $(eml).data('index');
$modal.data('date', date);
$modal.find('.start_class').val(classes);
if($(eml).hasClass('event')) {
var index = _.findIndex(course_list, {date: date, begin_class: classes.toString()});
course_list.splice(index, 1);
}
var array = _.pluck(_.where(course_list, {date: date}), 'begin_class').sort();
$.each(array, function(key, value) {
if(+value > classes) {
$modal.find('.over_class').val(+value);
return false;
}
});
$modal.modal();
}
});
time_table.init();
}
});
$('.btn-save').on('click', function() {
var course = {};
course.date = $modal.data('date');
course.begin_class = $modal.find('.start_class').val();
course.over_class = $modal.find('.over_class').val();
course_list.push(course);
time_table.options.events = course_list;
time_table.fill($('#table-box').data('weekFirstDay'), $('#table-box').data('weekLastDay'));
$modal.modal('hide');
console.log('course_list', course_list);
});
});
})(jQuery);
</script>
</body>
</html>