Skip to content

Instantly share code, notes, and snippets.

@chrisciampoli
Last active August 29, 2015 14:10
Show Gist options
  • Save chrisciampoli/4ae0f87510a696939593 to your computer and use it in GitHub Desktop.
Save chrisciampoli/4ae0f87510a696939593 to your computer and use it in GitHub Desktop.
Custom Calendar
sctr.addModule('calendar', function() {
/**
Creates a 7 day week
@param prev
@param next
TODO: move prev, next into their own methods to update curr
*/
var bindSortable, checkImage, checkWeek, initWeek, processResults, renderWeek, saveSchedule, updatePool;
bindSortable = function() {
$("#sortable1,#sortable2,#sortable3,#sortable4,#sortable5,#sortable6,#sortable7,#sortable8").sortable({
connectWith: ".connectedSortable",
cursor: "move",
placeholder: "sortable-placeholder",
forcePlaceHolderSize: true,
revert: true,
scroll: false,
tolerance: "pointer",
receive: function(event, ui) {
var theEvents, theParent, toPass;
if (this === ui.item.parent()[0]) {
theParent = ui.item.parent();
theEvents = theCalendar.find("li");
toPass = [];
console.log(ui.item.parent().attr("id"));
if (ui.item.parent().attr("id") === "sortable8") {
toPass.push({
release_date: "0000-00-00",
dvd_id: ui.item.attr("id"),
position: $(this).index()
});
saveSchedule(toPass);
return;
}
ui.item.attr("data-date", theParent.data("date"));
theEvents.each(function() {
toPass.push({
release_date: $(this).attr("data-date"),
dvd_id: $(this).attr("id"),
position: $(this).index()
});
});
saveSchedule(toPass);
}
},
stop: function(event, ui) {
var theEvents, theParent, toPass;
if (this === ui.item.parent()[0]) {
theParent = ui.item.parent();
ui.item.attr("data-date", theParent.data("date"));
theEvents = theCalendar.find("li");
toPass = [];
theEvents.each(function() {
toPass.push({
release_date: $(this).attr("data-date"),
dvd_id: $(this).attr("id"),
position: $(this).index()
});
});
saveSchedule(toPass);
}
}
});
};
/**
Check the db for selected weeks scheduled titles
@param from
@param to
*/
checkWeek = function(from, to, callback, gay) {
var e;
gay = gay || 0;
try {
$.ajax({
type: "POST",
url: "/calendar/search",
data: {
from: from,
to: to,
gay: gay
},
success: function(data) {
callback(data);
},
error: function(xhr, type, exception) {
alert("Issue saving week events to the database. Please contact your administrator.");
},
complete: function(xhr, type, exception) {
return $("#calender-nav-btns").children().prop('disabled', false);
}
});
} catch (_error) {
e = _error;
alert(e.toString());
}
};
/**
Creates the week dates
*/
initWeek = function(prev, next) {
var first, i, theWeek;
theWeek = [];
window.curr = window.curr || new Date;
if (next === true) {
window.curr.setDate(window.curr.getDate() + 7);
}
if (prev === true) {
window.curr.setDate(window.curr.getDate() - 7);
}
first = window.curr.getDate() - window.curr.getDay();
i = 0;
while (i < 7) {
next = new Date(curr.getTime());
next.setDate(first + i);
theWeek.push(next.toString());
i++;
}
$("#calendar-month-year").html(theWeek[0].split(" ")[1] + " " + theWeek[0].split(" ")[2] + " - " + theWeek[6].split(" ")[1] + " " + theWeek[6].split(" ")[2]);
return theWeek;
};
/**
Process returned AJAX week events (build up your events per day)
@param Json results
*/
processResults = function(results) {
console.log(results);
results.forEach(function(key, value) {
console.log('im here step 1 in results');
$(".calendar-row-container").each(function() {
var newDate, row, today;
console.log('im here step 2 in calendar row container each');
newDate = new Date;
today = newDate.getDate();
console.log($(this).data("date"));
console.log(key.release_date);
if ($(this).data("date") === key.release_date) {
console.log('im here step 3 matched to release date');
row = String("<li class=\"document-item\" data-order=\"" + key.display_order + "\" data-date=\"" + key.release_date + "\" id=\"" + key.dvd_id + "\">" + "<div class=\"title\" style=\"height:25px !important;\">" + ucwords(key.dvd_title) + "</div>" + "<div><img class=\"holder\" src=\"" + key.cover_src.src + "\" alt=\"\" /></div>" + "<div class=\"id\">" + key.dvd_id + " - HD</div>" + "</li>");
$(this).append(row);
}
});
Holder.run();
});
$(".calendar-row-container").each(function() {
var $modules, elements, order;
$modules = $(this).find("li");
order = [];
$modules.each(function() {
order.push($(this).data("order"));
});
order.sort();
elements = [];
$.each(order, function(i, position) {
elements.push($modules.filter("[data-order=\"" + position + "\"]"));
});
$(this).append(elements);
});
};
/**
Renders a pre-created 7 day week into DOM elements
@param week
@param target
*/
renderWeek = function(week, target) {
var dayNum;
target.html("");
dayNum = 0;
week.forEach(function(element) {
var dayName, leadingZero, matchDate, monthLookup, theBox, title;
dayNum += 1;
dayName = element.split(" ");
title = dayName[0] + " " + dayName[1] + " " + dayName[2] + " " + dayName[3] + " ";
monthLookup = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
if ((monthLookup.indexOf(dayName[1]) + 1) <= 9) {
leadingZero = "0";
} else {
leadingZero = "";
}
matchDate = dayName[3] + "-" + leadingZero + (monthLookup.indexOf(dayName[1]) + 1) + "-" + dayName[2];
theBox = String("<div class=\"document-list calendar-container\">" + "<div class=\"calendar-tag\" data-date=\"" + matchDate + "\">" + title + "</div>" + "<ul data-date=\"" + matchDate + "\" id=\"sortable" + dayNum + "\" class=\"list-inline connectedSortable calendar-row-container\"></ul>" + "</div>");
target.append(theBox);
});
};
/**
Updates the dvds in the db
*/
saveSchedule = function(data) {
var e;
try {
$.ajax({
type: "POST",
url: "/calendar/saveSchedule",
data: {
data: data
},
success: function(data) {},
error: function(xhr, type, exception) {
alert("ajax error response type " + type);
}
});
} catch (_error) {
e = _error;
alert(e.toString());
}
};
/**
Clears out the pool
*/
updatePool = function(results) {
$('#calendar-pool-container').empty();
$('#calendar-pool-container').append($('<ul id="sortable8" class="list-inline connectedSortable">'));
results.forEach(function(key, value) {
var row;
row = String("<li class=\"document-item\" data-order=\"" + key.display_order + "\" data-date=\"" + key.release_date + "\" id=\"" + key.dvd_id + "\">" + "<div class=\"title\" style=\"height:25px !important;\">" + ucwords(key.dvd_title) + "</div>" + "<div><img class=\"holder\" src=\"" + key.cover_src.src + "\" alt=\"\" /></div>" + "<div class=\"id\">" + key.dvd_id + " - HD</div>" + "</li>");
return $('#sortable8').append(row);
});
Holder.run();
bindSortable();
};
$(function() {
window.curr;
window.theWeek = initWeek();
window.theCalendar = $("#calendar");
renderWeek(window.theWeek, window.theCalendar);
$(this).trigger("weekLoaded", [window.theWeek]);
$("#prev_btn").on("click", function(e) {
e.preventDefault();
$("#calender-nav-btns").children().prop('disabled', true);
window.theWeek = initWeek(true, false);
renderWeek(window.theWeek, window.theCalendar);
$(this).trigger("weekLoaded", [window.theWeek, $('#hdselect').val()]);
});
$("#next_btn").on("click", function(e) {
e.preventDefault();
$("#calender-nav-btns").children().prop('disabled', true);
window.theWeek = initWeek(false, true);
renderWeek(window.theWeek, window.theCalendar);
$(this).trigger("weekLoaded", [window.theWeek, $('#hdselect').val()]);
});
$('#hdselect').on('change', function(e) {
checkWeek(window.theWeek[0], window.theWeek[6], processResults, $(this).val());
checkWeek("0000-00-00", "0000-00-00", updatePool, $(this).val());
renderWeek(window.theWeek, window.theCalendar);
return $("#calender-nav-btns").children().prop('disabled', true);
});
Holder.run();
});
$(document).on("weekLoaded", function(e, week, gay) {
try {
checkWeek(week[0], week[6], processResults, gay);
} catch (_error) {}
return bindSortable();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment