Skip to content

Instantly share code, notes, and snippets.

@roseg43
Created August 30, 2016 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roseg43/f9366daf4037faf802d54e7affdfe8e0 to your computer and use it in GitHub Desktop.
Save roseg43/f9366daf4037faf802d54e7affdfe8e0 to your computer and use it in GitHub Desktop.
Custom calendar schedule manager that manages selection states and passes selected objects as JSON to a PDF generator.
(function ($) {
// An array of presentationTimeslots
var slots = [];
function presentationTimeslot(el) {
// So that we always have a global object scope that can't be tainted by local scopes
var self = this;
this.$el = $(el);
this.$parentTable = this.$el.parents('table');
this.title = this.$el.find('.schedule-event').text();
this.detail = this.$el.find('.schedule-event-detail');
this.timeslot = this.$el.find('.schedule-time').text();
this.$tracks = this.$el.find('td:not(.schedule-time)');
this.selectedTrack = this.determineSelectedTrack();
this.events = {
scheduleUpdated: new CustomEvent("scheduleUpdated", {bubbles: true})
};
// Update the active track / prompt the user, then fire the scheduleUpdated event.
this.$tracks.click(function (e) {
e.preventDefault();
if (!self.trackIsEmpty($(this))) {
self.promptOverwrite($(this), e);
}
});
};
presentationTimeslot.prototype.trackIsEmpty = function ($el) {
return ($el.hasClass('empty'));
}
presentationTimeslot.prototype.determineSelectedTrack = function () {
var filtered_tracks = this.$tracks.filter('.active');
if (filtered_tracks.length > 0) {
return filtered_tracks;
} else {
return false;
}
};
/**
* This method handles the actual front-end display of presentation items and fires the scheduleUpdated event
* if an event object has been passed into it.
*/
presentationTimeslot.prototype.setSelectedTrack = function ($el, e) {
if (this.selectedTrack) {
this.selectedTrack.removeClass('active');
this.selectedTrack.find('.favorite-toggle').addClass('inactive');
}
this.selectedTrack = $el;
$el.addClass('active');
$el.find('.favorite-toggle').removeClass('inactive');
if (typeof e != undefined) {
e.target.dispatchEvent(this.events.scheduleUpdated);
}
};
/**
* Prompts the user if a track in a timeslot is already selected. Switches the selected track if the user confirms.
*/
presentationTimeslot.prototype.promptOverwrite = function ($el, e) {
if (this.selectedTrack) {
if (window.confirm('This will remove the presentation from this time slot you\'ve previously selected from your schedule. Is this okay?')) {
this.setSelectedTrack($el, e);
} else {
return false
}
} else {
this.setSelectedTrack($el, e);
}
};
/**
* Handles all of the active presentation slots, so that we can organize our data before sending it to a handlebars
* template.
*/
function scheduleManager(slots) {
var self = this;
this.slots = slots;
this.activeSlots = [];
$(window).on('scheduleUpdated', function() {
self.updateActiveSlots();
});
};
/**
* Is called whenever a user updates their schedule. Event is registered in presentationTimeslot.events
* Checks every slot to see if it has a selectedTrack. If so, add it to the activeSlots property.
*/
scheduleManager.prototype.updateActiveSlots = function() {
var filtered_slots = [];
this.slots.forEach(function(value, index, arr) {
if (value.selectedTrack != false) {
filtered_slots.push(value);
}
});
this.activeSlots = filtered_slots;
};
/**
* Returns a JSON object for use with Handlebars to render the custom print schedule
*/
scheduleManager.prototype.outputJSON = function() {
return JSON.stringify(this.activeSlots);
};
// Object initialization
$('table.table tr').each(function () {
slots.push(new presentationTimeslot($(this)));
});
var scheduleManager = new scheduleManager(slots);
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment