forked from ardani/jQuery.Simple-Event-Calendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplecalendar.js
138 lines (119 loc) · 5.47 KB
/
simplecalendar.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
var calendar = {
init: function() {
/**
* Get current date
*/
var d = new Date();
var strDate = d.getFullYear() + "/" + (d.getMonth() + 1) + "/" + d.getDate();
/**
* Get current month and set as '.current-month' in title
*/
var monthNumber = d.getMonth() + 1;
function GetMonthName(monthNumber) {
var months = ['Januari', 'Februari', 'Mars', 'April', 'Maj', 'Juni', 'Juli', 'Augusti', 'September', 'Oktober', 'November', 'December'];
return months[monthNumber - 1];
}
$('.month').text(GetMonthName(monthNumber));
/**
* Get current day and set as '.current-day'
*/
$('tbody td:contains("' + d.getDate() + '")').addClass('current-day');
/**
* Add class '.active' on calendar date
*/
$('tbody td').on('click', function(e) {
if ($(this).hasClass('event')) {
$('tbody td').removeClass('active');
$(this).addClass('active');
} else {
$('tbody td').removeClass('active');
};
});
/**
* Add '.event' class to all days that has an event
*/
$('.day-event').each(function(i) {
var eventMonth = $(this).attr('date-month');
var eventDay = $(this).attr('date-day');
$('tbody tr td[date-month="' + eventMonth + '"][date-day="' + eventDay + '"]').addClass('event');
});
/**
* Get current day on click in calendar
* and find day-event to display
*/
$('tbody td').on('click', function(e) {
$('.day-event').slideUp('fast');
var monthEvent = $(this).attr('date-month');
var dayEvent = $(this).text();
$('.day-event[date-month="' + monthEvent + '"][date-day="' + dayEvent + '"]').slideDown('fast');
});
/**
* Close day-event
*/
$('.close').on('click', function(e) {
$(this).parent().slideUp('fast');
});
/**
* Save & Remove to/from personal list
*/
$('.save').click(function() {
if (this.checked) {
$(this).next().text('Remove from personal list');
var eventHtml = $(this).closest('.day-event').html();
var eventMonth = $(this).closest('.day-event').attr('date-month');
var eventDay = $(this).closest('.day-event').attr('date-day');
var eventNumber = $(this).closest('.day-event').attr('data-number');
$('.person-list').append('<div class="day" date-month="' + eventMonth + '" date-day="' + eventDay + '" data-number="' + eventNumber + '" style="display:none;">' + eventHtml + '</div>');
$('.day[date-month="' + eventMonth + '"][date-day="' + eventDay + '"]').slideDown('fast');
$('.day').find('.close').remove();
$('.day').find('.save').removeClass('save').addClass('remove');
$('.day').find('.remove').next().addClass('hidden-print');
remove();
sortlist();
} else {
$(this).next().text('Save to personal list');
var eventMonth = $(this).closest('.day-event').attr('date-month');
var eventDay = $(this).closest('.day-event').attr('date-day');
var eventNumber = $(this).closest('.day-event').attr('data-number');
$('.day[date-month="' + eventMonth + '"][date-day="' + eventDay + '"][data-number="' + eventNumber + '"]').slideUp('slow');
setTimeout(function() {
$('.day[date-month="' + eventMonth + '"][date-day="' + eventDay + '"][data-number="' + eventNumber + '"]').remove();
}, 1500);
}
});
function remove() {
$('.remove').click(function() {
if (this.checked) {
$(this).next().text('Remove from personal list');
var eventMonth = $(this).closest('.day').attr('date-month');
var eventDay = $(this).closest('.day').attr('date-day');
var eventNumber = $(this).closest('.day').attr('data-number');
$('.day[date-month="' + eventMonth + '"][date-day="' + eventDay + '"][data-number="' + eventNumber + '"]').slideUp('slow');
$('.day-event[date-month="' + eventMonth + '"][date-day="' + eventDay + '"][data-number="' + eventNumber + '"]').find('.save').attr('checked', false);
$('.day-event[date-month="' + eventMonth + '"][date-day="' + eventDay + '"][data-number="' + eventNumber + '"]').find('span').text('Save to personal list');
setTimeout(function() {
$('.day[date-month="' + eventMonth + '"][date-day="' + eventDay + '"][data-number="' + eventNumber + '"]').remove();
}, 1500);
}
});
}
/**
* Sort personal list
*/
function sortlist() {
var personList = $('.person-list');
personList.find('.day').sort(function(a, b) {
return +a.getAttribute('date-day') - +b.getAttribute('date-day');
}).appendTo(personList);
}
/**
* Print button
*/
$('.print-btn').click(function() {
window.print();
});
},
};
$(document).ready(function() {
calendar.init();
});