/
events.js
109 lines (91 loc) · 2.77 KB
/
events.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
/* -*- Mode: JavaScript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
this.events = (function() {
function list() {
model.count('event',function(c) {
if(c > 0) {
model.list('event',1,0,function(data) {
if(data) {
show_list_grouping(data);
}
})
}
else {
if(window.navigator.onLine) {
api.items('', function(data) {
if(data) {
show_list_grouping(data);
//add(data);
}
});
}
else {
alert('Offline!');
}
}
})
}
function add(data) {
$.each(data, function() {
model.set(this);
})
}
function clearAllForm() {
$('#view-event-add').find('input,textarea').each(function() {
$(this).val('');
})
}
function show_list_grouping(data) {
$.each(data, function() {
var group = groupingdate(this.start);
var groupid = group.toLowerCase().replace(' ','');
if($('#'+group).length == 0) {
$('<header />', {text: group}).appendTo('#event-list-content');
$('<ul />', {id: group}).appendTo('#event-list-content');
}
var img = $('<img />', {'src' : this.banner, 'alt': 'placeholder'}).wrap('<aside />').parent().addClass('pack-end');
var link = $('<p />', {text: this.title}).wrap('<a />').parent().attr('href','#event-detail').attr('data-eventid',this.id);
$('<li />').append(img).append(link).appendTo('#'+group);
})
}
function detail(id) {
if(window.navigator.onLine) {
api.detail(id, function(data) {
if(data) {
model.set(this);
}
})
}
}
function groupingdate(time) {
var date = new Date(new Date().getFullYear(),new Date().getMonth(),new Date().getDate(),0,0,0);
var tdate = new Date(time*1000);
var now = Math.round(date.getTime() / 1000);
var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
var currdate = date.getDate();
var itemdate = tdate.getDate();
var firstdate = currdate - date.getDay();
var lastdate = firstdate + 6;
var diff = time - now;
var daydiff = firstdate - itemdate;
if(diff <= 86400) {
return 'Today';
}
else if(diff > 86400 && diff <= (86400*2)) {
return 'Tomorrow';
}
else if(diff >= (86400*3) && diff <= (86400*daydiff)) {
return 'This Week';
}
else if(daydiff < -8 && (tdate.getFullYear() <= date.getFullYear())) {
return months[tdate.getMonth()];
}
else {
return 'Next Year';
}
}
return {
list: list,
add: add,
clearAllForm: clearAllForm
};
}());