Created
March 28, 2014 21:42
-
-
Save benneuman/9843581 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<link rel='stylesheet' href='fullcalendar/fullcalendar.css' /> | |
</head> | |
<body> | |
<div id='calendar'> | |
</div> | |
<script src='lib/jquery.min.js'></script> | |
<script src='lib/jquery-ui.custom.min.js'></script> | |
<script src='fullcalendar/fullcalendar.js'></script> | |
<script> | |
$(document).ready(function(){ | |
var date = new Date(); | |
var d = date.getDate(); | |
var m = date.getMonth(); | |
var y = date.getFullYear(); | |
$("#calendar").fullCalendar({ | |
selectable: true, | |
allDayDefault: false, | |
allDaySlot: false, | |
slotEventOverlap: false, | |
defaultView: 'agendaWeek', | |
events: [ | |
new ReservedSpot(new Date(y, m, d - 4, 0, 0), new Date(y, m,d - 4, 9, 0)), | |
{ | |
start: new Date(y, m, d - 4, 9, 0), | |
end: new Date(y, m, d - 4, 17, 0), | |
editable: true, | |
backgroundColor: 'blue', | |
textColor: 'red', | |
} | |
], | |
eventRender: function(event, element, view) { | |
if (event.busySpot) { | |
$(element).find(".fc-event-time").html(''); | |
} | |
} | |
}); | |
}); | |
function ReservedSpot(start, end) { | |
this.start = start; | |
this.end = end; | |
this.backgroundColor = 'red'; | |
this.editable = false; | |
this.busySpot = true; | |
} | |
function Availability(start, end, day) { | |
this.start = start; | |
this.end = end; | |
this.day = day; | |
} | |
function getUnavailableRanges(availabilities) { | |
var sorted_avails = sort_availabilities(day_availabilities); | |
} | |
function selectAvailabilitiessByDay(availabilities){} | |
function Availabilities(availabilities) { | |
this.availabilities = availabilities.sort(this._sort); | |
this.startTime = this._defineStarttime(); | |
this.endTime = this._defineEndtime(); | |
this.unavailabilities = this.getUnavailabilities(); | |
} | |
Availabilities.prototype.getUnavailabilities = function() { | |
var unavails = {}; | |
for (var day = 1; day < 8; day++) { | |
unavails[day] = this.getUnavailableRangesForDay(day); | |
} | |
return unavails; | |
} | |
Availabilities.prototype.getUnavailableRangesForDay = function(day) | |
{ | |
var day_avails = this.select_by_day(day); | |
var ranges = [[this.startTime, this.endTime]]; | |
for (var i = 0; i < day_avails.length; i++) { | |
var last_range = ranges.pop(); | |
last_range[1] = day_avails[i].start; | |
var new_range = [day_avails[i].end, this.endTime]; | |
if (last_range[0] != last_range[1]) { | |
ranges.push(last_range); | |
} | |
if (new_range[0] != new_range [1]) { | |
ranges.push(new_range); | |
} | |
} | |
return ranges; | |
} | |
Availabilities.prototype._defineStarttime = function() { | |
var start_time = this.availabilities[0].start; | |
for (var i = 1; i < this.availabilities.length; i++) { | |
if (this.availabilities[i].start < start_time) { | |
start_time = this.availabilities[i].start; | |
} | |
} | |
return start_time; | |
} | |
Availabilities.prototype._defineEndtime = function() { | |
var end_time = this.availabilities[0].end; | |
for (var i = 1; i < this.availabilities.length; i++) { | |
if (this.availabilities[i].end > end_time) { | |
end_time = this.availabilities[i].end; | |
} | |
} | |
return end_time; | |
} | |
Availabilities.prototype.select_by_day = function(day) { | |
var selection = [] | |
for (var i = 0; i < this.availabilities.length; i++) { | |
var availability = this.availabilities[i]; | |
if (availability.day == day) { | |
selection.push(availability) | |
}; | |
} | |
return selection; | |
}; | |
Availabilities.prototype._sort = function(a, b) { | |
if (a.day != b.day) { | |
return this.sortByDay(a, b); | |
} | |
else if (a.start != b.start) { | |
return this.sortByTime(a, b); | |
} | |
}; | |
Availabilities.prototype._sortByTime = function(a, b) { | |
if (a.start < b.start) { | |
return -1; | |
} | |
else if (a.start > b.start) { | |
return 1; | |
} | |
else { | |
return 0; | |
} | |
}; | |
Availabilities.prototype._sortByDay = function(a, b) { | |
if (a.day < b.day) { | |
return -1; | |
} | |
else if (a.day > b.day) { | |
return 1; | |
} | |
} | |
function test_unavails() { | |
var avail1 = new Availability(360, 720, 2); | |
var avail2 = new Availability(730, 750, 2); | |
var avail3 = new Availability(900, 950, 2); | |
var avail4 = new Availability(320, 640, 3); | |
var avail5 = new Availability(730, 750, 3); | |
var avail6 = new Availability(450, 640, 5); | |
var avails = new Availabilities([avail3, avail5, avail6, avail4, avail2, avail1]); | |
var expected = { | |
1: [[320, 950]], | |
2: [[320, 360], [720, 730], [750, 900]], | |
3: [[640, 730], [750, 950]], | |
4: [[320, 950]], | |
5: [[320, 450], [640, 950]], | |
6: [[320, 950]], | |
7: [[320, 950]], | |
} | |
console.log(avails.unavailabilities); | |
return check_array_eq(avails.unavailabilities, expected); | |
} | |
function test_dayUnavails2() { | |
var avail1 = new Availability(360, 720, 2); | |
var avail2 = new Availability(730, 750, 2); | |
var avail3 = new Availability(900, 950, 2); | |
var avails = new Availabilities([avail1, avail2, avail3]); | |
var uns = avails.getUnavailableRangesForDay(2); | |
return check_array_eq([[720, 730], [750, 900]], uns); | |
} | |
function test_dayUnavails() { | |
var avail1 = new Availability(360, 720, 2); | |
var avail2 = new Availability(730, 750, 2); | |
var avail3 = new Availability(900, 950, 2); | |
var avails = new Availabilities([avail1, avail2, avail3]); | |
var uns = avails.getUnavailableRangesForDay(3); | |
return check_array_eq([[360, 950]], uns); | |
} | |
function test_selectByDay() { | |
var avail1 = new Availability(360, 720, 2); | |
var avail2 = new Availability(730, 750, 2); | |
var avail3 = new Availability(900, 950, 2); | |
var avail4 = new Availability(320, 640, 3); | |
var avail5 = new Availability(730, 750, 3); | |
var avail6 = new Availability(450, 640, 5); | |
var avails = new Availabilities([avail3, avail5, avail6, avail4, avail2, avail1]); | |
var selection = avails.select_by_day(3); | |
return check_array_eq(selection, [avail4, avail5]); | |
} | |
function test_Availabilities() { | |
var avail1 = new Availability(360, 720, 2); | |
var avail2 = new Availability(730, 750, 2); | |
var avail3 = new Availability(900, 950, 2); | |
var avail4 = new Availability(320, 640, 3); | |
var avail5 = new Availability(730, 750, 3); | |
var avail6 = new Availability(450, 640, 5); | |
var avails = new Availabilities([avail3, avail5, avail6, avail4, avail2, avail1]); | |
return check_array_eq(avails.availabilities, [avail1, avail2, avail3, avail4, avail5, avail6]); | |
} | |
function test_startTime() { | |
var avails = [ | |
new Availability(360, 720, 1), | |
new Availability(360, 720, 2), | |
new Availability(380, 700, 3), | |
new Availability(300, 400, 3), | |
new Availability(400, 800, 4), | |
]; | |
var avail = new Availabilities(avails); | |
return (avail.startTime == 300); | |
} | |
function test_endTime() { | |
var avails = [ | |
new Availability(360, 720, 1), | |
new Availability(360, 720, 2), | |
new Availability(380, 700, 3), | |
new Availability(300, 400, 3), | |
new Availability(400, 800, 4), | |
new Availability(500, 800, 5) | |
]; | |
var avail = new Availabilities(avails); | |
return (avail.endTime == 800); | |
} | |
function sort_availabilities(availabilities) { | |
return availabilities.sort(availabilitySort); | |
} | |
function availabilitySort(a, b) { | |
if (a.day != b.day) { | |
return sortByDay(a, b); | |
} | |
else if (a.start != b.start) { | |
return sortByTime(a, b); | |
} | |
} | |
function sortByTime(a, b) { | |
if (a.start < b.start) { | |
return -1; | |
} | |
else if (a.start > b.start) { | |
return 1; | |
} | |
else { | |
return 0; | |
} | |
} | |
function sortByDay(a, b) { | |
if (a.day < b.day) { | |
return -1; | |
} | |
else if (a.day > b.day) { | |
return 1; | |
} | |
} | |
function test_sorter2() { | |
var avail1 = new Availability(360, 720, 2); | |
var avail2 = new Availability(730, 750, 2); | |
var avail3 = new Availability(900, 950, 2); | |
var avail4 = new Availability(320, 640, 3); | |
var avail5 = new Availability(730, 750, 3); | |
var avail6 = new Availability(450, 640, 5); | |
var sorted = sort_availabilities([avail3, avail5, avail6, avail4, avail2, avail1]); | |
return check_array_eq(sorted, [avail1, avail2, avail3, avail4, avail5, avail6]); | |
} | |
function test_sorter() { | |
var avail1 = new Availability(360, 720, 1); | |
var avail2 = new Availability(730, 750, 1); | |
var avail3 = new Availability(900, 950, 1); | |
var sorted = sort_availabilities([avail2, avail1, avail3]); | |
return (sorted[0] == avail1 && sorted[1] == avail2 && sorted[2] == avail3); | |
} | |
function test_sortByDay() { | |
var avail1 = new Availability(360, 720, 2); | |
var avail2 = new Availability(360, 720, 4); | |
var check = sortByDay(avail1, avail2); | |
return (check == -1); | |
} | |
function test_sortByDay2() { | |
var avail1 = new Availability(360, 720, 2); | |
var avail2 = new Availability(360, 720, 4); | |
var check = sortByDay(avail2, avail1); | |
return (check == 1); | |
} | |
function test_sortByTime() { | |
var avail1 = new Availability(360, 720, 1); | |
var avail2 = new Availability(730, 750, 1); | |
var check = sortByTime(avail1, avail2); | |
return (check == -1); | |
} | |
function test_sortByTime2() { | |
var avail1 = new Availability(360, 720, 1); | |
var avail2 = new Availability(730, 750, 1); | |
var check = sortByTime(avail2, avail1); | |
return (check == 1); | |
} | |
function test_start_time() { | |
var avails = [ | |
new Availability(360, 720, 1), | |
new Availability(360, 720, 2), | |
new Availability(380, 700, 3), | |
new Availability(300, 400, 3), | |
new Availability(400, 800, 4), | |
]; | |
var start_time = DefineStartTime(avails); | |
return (start_time == 300); | |
} | |
function test_end_time() { | |
var avails = [ | |
new Availability(360, 720, 1), | |
new Availability(360, 720, 2), | |
new Availability(380, 700, 3), | |
new Availability(300, 400, 3), | |
new Availability(400, 800, 4), | |
new Availability(500, 800, 5) | |
]; | |
var end_time = DefineEndTime(avails); | |
return (end_time == 800); | |
} | |
function test_ArrayEq() { | |
a = [1,2,3]; | |
b = ["foo", "bar", "me"]; | |
var eq = check_array_eq(a, b); | |
return (eq == false); | |
} | |
function test_ArrayEq2() { | |
a = ['foo', 'bar', 'me']; | |
b = ["foo", "bar", "me"]; | |
var eq = check_array_eq(a, b); | |
return (eq == true); | |
} | |
function test_ArrayEq3() { | |
a = ['foo', 'bar', 'me', 'grew']; | |
b = ["foo", "bar", "me"]; | |
var eq = check_array_eq(a, b); | |
return (eq == false); | |
} | |
function test_ArrayEq4() { | |
a = []; | |
b = []; | |
var eq = check_array_eq(a, b); | |
return (eq == true); | |
} | |
function test_ArrayEq5() { | |
a = [[3,2],4]; | |
b = [[3,2],4]; | |
var eq = check_array_eq(a, b); | |
return (eq == true); | |
} | |
function test_ArrayEq6() { | |
a = [[3,2],4]; | |
b = [[2],4]; | |
var eq = check_array_eq(a, b); | |
return (eq == false); | |
} | |
function run_tests() { | |
var tests = [ | |
test_start_time, | |
test_end_time, | |
test_sortByTime, | |
test_sortByDay, | |
test_sortByTime2, | |
test_sortByDay2, | |
test_sorter, | |
test_sorter2, | |
test_Availabilities, | |
test_startTime, | |
test_endTime, | |
test_selectByDay, | |
test_dayUnavails, | |
test_ArrayEq, | |
test_ArrayEq2, | |
test_ArrayEq3, | |
test_ArrayEq4, | |
test_ArrayEq5, | |
test_ArrayEq6, | |
test_dayUnavails2, | |
test_unavails | |
]; | |
for (var i = 0; i < tests.length; i++) { | |
console.log(tests[i].call()); | |
} | |
} | |
function check_array_eq(a, b) { | |
if (a.length != b.length) { | |
return false; | |
} | |
else { | |
for (i in a) { | |
if (a.hasOwnProperty(i)) { | |
if (a[i].constructor == Array && b[i].constructor == Array) { | |
var eq = check_array_eq(a[i], b[i]); | |
if (!eq) { | |
return false; | |
} | |
} | |
else if (a[i] != b[i]) { | |
return false; | |
} | |
} | |
} | |
return true; | |
} | |
} | |
function DefineStartTime(availabilities) { | |
var start_time = availabilities[0].start; | |
for (var i = 1; i < availabilities.length; i++) { | |
if (availabilities[i].start < start_time) { | |
start_time = availabilities[i].start; | |
} | |
} | |
return start_time; | |
} | |
function DefineEndTime(availabilities) { | |
var end_time = availabilities[0].end; | |
for (var i = 1; i < availabilities.length; i++) { | |
if (availabilities[i].end > end_time) { | |
end_time = availabilities[i].end; | |
} | |
} | |
return end_time; | |
} | |
run_tests(); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment