You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
161 lines
6.2 KiB
161 lines
6.2 KiB
$(function(){
|
|
var currentDate; // Holds the day clicked when adding a new event
|
|
var currentEvent; // Holds the event object when editing an event
|
|
var getUrlString = location.href;
|
|
var turl = new URL(getUrlString);
|
|
$('#color').colorpicker(); // Colopicker
|
|
$('#time').timepicker({
|
|
minuteStep: 5,
|
|
showInputs: false,
|
|
disableFocus: true,
|
|
showMeridian: false
|
|
}); // Timepicker
|
|
// Fullcalendar
|
|
$('#calendar').fullCalendar({
|
|
timeFormat: 'HH:mm',
|
|
header: {
|
|
left: 'prev, next, today',
|
|
center: 'title',
|
|
right: 'month, basicWeek, basicDay'
|
|
},
|
|
// Get all events stored in database
|
|
events: 'crud/getEvents.php?token='+turl.searchParams.get('token'),
|
|
// Handle Day Click
|
|
dayClick: function(date, event, view) {
|
|
currentDate = date.format();
|
|
// Open modal to add event
|
|
modal({
|
|
// Available buttons when adding
|
|
buttons: {
|
|
add: {
|
|
id: 'add-event', // Buttons id
|
|
css: 'btn-success', // Buttons class
|
|
label: '儲存' // Buttons label
|
|
}
|
|
},
|
|
title: '新增事件 (' + date.format() + ')' // Modal title
|
|
});
|
|
},
|
|
// Event Mouseover
|
|
eventMouseover: function(calEvent, jsEvent, view){
|
|
var tooltip = '<div class="event-tooltip">' + calEvent.description + '</div>';
|
|
$("body").append(tooltip);
|
|
$(this).mouseover(function(e) {
|
|
$(this).css('z-index', 10000);
|
|
$('.event-tooltip').fadeIn('500');
|
|
$('.event-tooltip').fadeTo('10', 1.9);
|
|
}).mousemove(function(e) {
|
|
$('.event-tooltip').css('top', e.pageY + 10);
|
|
$('.event-tooltip').css('left', e.pageX + 20);
|
|
});
|
|
},
|
|
eventMouseout: function(calEvent, jsEvent) {
|
|
$(this).css('z-index', 8);
|
|
$('.event-tooltip').remove();
|
|
},
|
|
// Handle Existing Event Click
|
|
eventClick: function(calEvent, jsEvent, view) {
|
|
// Set currentEvent variable according to the event clicked in the calendar
|
|
currentEvent = calEvent;
|
|
// Open modal to edit or delete event
|
|
modal({
|
|
// Available buttons when editing
|
|
buttons: {
|
|
delete: {
|
|
id: 'delete-event',
|
|
css: 'btn-danger',
|
|
label: '刪除'
|
|
},
|
|
update: {
|
|
id: 'update-event',
|
|
css: 'btn-success',
|
|
label: '修改'
|
|
}
|
|
},
|
|
title: '修改事件 > ' + calEvent.title + '',
|
|
event: calEvent
|
|
});
|
|
}
|
|
});
|
|
// Prepares the modal window according to data passed
|
|
function modal(data) {
|
|
// Set modal title
|
|
$('.modal-title').html(data.title);
|
|
// Clear buttons except Cancel
|
|
$('.modal-footer button:not(".btn-default")').remove();
|
|
// Set input values
|
|
$('#title').val(data.event ? data.event.title : '');
|
|
if( ! data.event) {
|
|
// When adding set timepicker to current time
|
|
var now = new Date();
|
|
var time = now.getHours() + ':' + (now.getMinutes() < 10 ? '0' + now.getMinutes() : now.getMinutes());
|
|
} else {
|
|
// When editing set timepicker to event's time
|
|
var time = data.event.date.split(' ')[1].slice(0, -3);
|
|
time = time.charAt(0) === '0' ? time.slice(1) : time;
|
|
}
|
|
$('#time').val(time);
|
|
$('#description').val(data.event ? data.event.description : '');
|
|
$('#color').val(data.event ? data.event.color : '#3a87ad');
|
|
// Create Butttons
|
|
$.each(data.buttons, function(index, button){
|
|
$('.modal-footer').prepend('<button type="button" id="' + button.id + '" class="btn ' + button.css + '">' + button.label + '</button>')
|
|
})
|
|
//Show Modal
|
|
$('.modal').modal('show');
|
|
//window.parent.parent.$('#calendarModal').modal('show');
|
|
}
|
|
// Handle Click on Add Button
|
|
$('.modal').on('click', '#add-event', function(e){
|
|
if(validator(['title', 'description'])) {
|
|
$.post('crud/addEvent.php', {
|
|
title: $('#title').val(),
|
|
description: $('#description').val(),
|
|
color: $('#color').val(),
|
|
date: currentDate + ' ' + getTime()
|
|
}, function(result){
|
|
$('.modal').modal('hide');
|
|
$('#calendar').fullCalendar("refetchEvents");
|
|
});
|
|
}
|
|
});
|
|
// Handle click on Update Button
|
|
$('.modal').on('click', '#update-event', function(e){
|
|
if(validator(['title', 'description'])) {
|
|
$.post('crud/updateEvent.php', {
|
|
id: currentEvent._id,
|
|
title: $('#title').val(),
|
|
description: $('#description').val(),
|
|
color: $('#color').val(),
|
|
date: currentEvent.date.split(' ')[0] + ' ' + getTime()
|
|
}, function(result){
|
|
$('.modal').modal('hide');
|
|
$('#calendar').fullCalendar("refetchEvents");
|
|
});
|
|
}
|
|
});
|
|
// Handle Click on Delete Button
|
|
$('.modal').on('click', '#delete-event', function(e){
|
|
$.get('crud/deleteEvent.php?id=' + currentEvent._id, function(result){
|
|
$('.modal').modal('hide');
|
|
$('#calendar').fullCalendar("refetchEvents");
|
|
});
|
|
});
|
|
// Get Formated Time From Timepicker
|
|
function getTime() {
|
|
var time = $('#time').val();
|
|
return (time.indexOf(':') == 1 ? '0' + time : time) + ':00';
|
|
}
|
|
// Dead Basic Validation For Inputs
|
|
function validator(elements) {
|
|
var errors = 0;
|
|
$.each(elements, function(index, element){
|
|
if($.trim($('#' + element).val()) == '') errors++;
|
|
});
|
|
if(errors) {
|
|
$('.error').html('請填寫標題與說明!');
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
});
|