import { Tracker } from ‘meteor/tracker’;
import { EventData } from ‘…/…/api/eventdata/eventdata’;
// Define a function that checks whether a moment has already passed.
let isPast = (date) => {
let today = moment().format();
return moment(today).isAfter(date);
};
Template.Calendar_Page.onCreated(() => {
Template.instance().subscribe(‘EventData’);
});
Template.Calendar_Page.helpers({
greeting: function() {
return Session.get(‘usernameaut’);
}
})
Template.Calendar_Page.onRendered(() => {
// Initialize the calendar.
$(’#event-calendar’).fullCalendar({
// Define the navigation buttons.
header: {
left: ‘title’,
center: ‘’,
right: ‘today prev,next’
},
// Add events to the calendar.
events(start, end, timezone, callback) {
let data = EventData.find().fetch().map((session) => {
// Don't allow already past study events to be editable.
session.editable = !isPast(session.start);
return session;
});
if (data) {
callback(data);
}
},
// Configure the information displayed for an "event."
eventRender(session, element) {
element.find('.fc-content').html(
`<h4 class="title">${session.title}</h4>
<p class="time">${session.startString}</p>
<p class="time">${session.endString}</p>
<p class="time">${session.pic}</p>
`
);
},
// Triggered when a day is clicked on.
dayClick(date, session) {
// Store the date so it can be used when adding an event to the EventData collection.
Session.set('eventModal', { type: 'add', date: date.format() });
// If the date has not already passed, show the create event modal.
if(moment(date.format()).isSameOrAfter(moment(), 'day')) {
var countdata = EventData.find({start:date.format()}).count();
console.log(countdata);
$('#create-event-modal').modal({ blurring: true }).modal('show');
}
},
// Delete an event if it is clicked on.
eventClick(event) {
var picUpdate = EventData.find({}, { _id :event._id}).fetch()[0].pic;
console.log(picUpdate);
if (event.pic==picUpdate ){
if (moment(event.start.format()).isSameOrAfter(moment(), 'day')){
Session.set("dateupdate",event.start.format());
Session.set("updateId",event._id);
Session.set("pic",event.pic);
$('#create-event-modal input[name="title"]').val(event.title);
$("#create-event-modal div.text:first").val(event.startString);
$("#create-event-modal div.text:first").text(event.startString);
$("#create-event-modal div.text:last").val(event.endString);
$("#create-event-modal div.text:last").text(event.endString);
$('#create-event-modal input[name="pic"]').val(event.pic);
$('#create-event-modal .ui.dividing.header').html('Update an event');
$('#create-event-modal button.ui.teal.button').html("Update Event");
$('#create-event-modal').modal({ blurring: true }).modal('show');
}
else
{
alert("Can not update, this is a past event");
return;
}
}
else
{
alert("Can not update, this schedule is belongs to "+event.pic);
return;
}
//EventData.remove({ _id: event._id });
},
// Allow events to be dragged and dropped.
eventDrop(session, delta, revert) {
var picUpdate = EventData.find({}, { _id : session._id}).fetch()[0].pic;
if (session.pic ==picUpdate)
{
let date = session.start.format();
if (!isPast(date)) {
let update = {
_id: session._id,
start: date,
end: date
};
// Update the date of the event.
Meteor.call('editEvent', update);
} else {
revert();
}
}
else
{
alert(“You can not change the schedule which is not belong to you”);
revert();
}
},
});
// Updates the calendar if there are changes.
Tracker.autorun(() => {
EventData.find().fetch();
$(’#event-calendar’).fullCalendar(‘refetchEvents’);
});
});