/**
* Counsellor Layout
*
* Date: Dec 27 2012
*/
var defaults = {
monthNames: ['January','February','March','April','May','June','July','August','September','October','November','December'],
monthNamesShort: ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],
dayNames: ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],
dayNamesShort: ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'],
timeFormat: 'h:mmtt'
};
(function($, undefined) {
$.fn.dayLayout = function(options) {
options = $.extend(true, {},
defaults,
options
);
$.ajaxSetup({
data: window.csrf,
cache: false,
headers: {
'X-CSRF-TOKEN': ''
}
});
var csrfToken = '';
// Global values
var startHour = parseTime($("#template_start_time").val()) / 60;
var endHour = parseTime($("#template_end_time").val()) / 60;
var Minutes = $("#minutes").val();
var numCells = 60 / Minutes;
var minutePixels = 30 / Minutes;
var rowHeight = Minutes * minutePixels - 1;
var printBlock = options.showPrintIcon;
// Define layout based on parameters
switch (options.layoutMode) {
case 'edit-template':
var numCols = 1;
var blockClass = 'block';
var apptBlockLeft = '300px';
var apptBlockWidth = '297px';
break;
case 'view-template':
var numCols = 1;
var blockClass = 'block';
var apptBlockLeft = '300px';
var apptBlockWidth = '297px';
break;
case 'booking-template':
var numCols = 1;
var blockClass = 'block';
var apptBlockLeft = '144px';
var apptBlockWidth = '450px';
break;
}
// Display time grid
content = buildSkeleton(options);
this.html(content);
// Get and display the time blocks
$.getJSON("ajax.Block.Fetch.php",
{
template_num: options.template_num,
calendar_date: opt('calendar_date'),
calendar_num: opt('calendar_num'),
day_num: opt('day_num'),
record_num: 0,
auth: opt('auth')
},
function(results) {
var tcontent = positionBlocks(results);
$("#layout").append(tcontent);
}
);
function buildSkeleton(options) {
var s;
var currDate;
var i;
$("#hourMin").val(startHour);
$("#hourMax").val(endHour);
s = "
";
for (h = startHour; h < endHour; h++) {
currDate = zeroDate();
addMinutes(currDate, h * 60);
s += "" +
"" +
"" +
formatDate(currDate, options.timeFormat, options) +
"" +
" | " +
"
";
for (i = 1; i < numCells; i++) {
currDate = zeroDate();
addMinutes(currDate, h * 60 + Minutes * i);
if (numCells > 4) {
s += "" +
"" +
"" +
formatDate(currDate, options.timeFormat, options) +
"" +
" | " +
"
";
}
else {
s += "" +
"" +
" " +
" | " +
"
";
}
}
}
s += "
";
return s;
}
function positionBlocks(blocks) {
var i;
var s = '';
var len = blocks.length;
var block, rnum;
var posTop, posBottom, posLeft, height;
var title, description, title_text;
var appt_count, student_name, student_num, appt_note, title_note, appt_staff;
var startTime, endTime;
var editICON, deleteICON;
var layoutMode = opt('layoutMode');
var rule_class = '';
for (i = 0; i < len; i++) {
block = blocks[i];
// Calculate the block dimensions
posTop = (parseTime(block.block_start_time) - parseTime(startHour)) * minutePixels;
posBottom = (parseTime(block.block_end_time) - parseTime(startHour)) * minutePixels - 2;
height = posBottom - posTop;
// Format title
startTime = formatDate(addMinutes(zeroDate(), parseTime(block.block_start_time)), opt('timeFormat'));
endTime = formatDate(addMinutes(zeroDate(), parseTime(block.block_end_time)), opt('timeFormat'));
// Display blocks
if (block.type == 'block') {
// Format description if there is one
description = '';
// Format icons for editing
rnum = block.record_num;
blockIcons = "";
title = "";
btitle = "";
if (layoutMode == 'edit-template') {
editICON = "";
deleteICON = "";
addICON = "";
blockIcons = "
" + editICON + " " + deleteICON + " " + addICON;
btitle = block.block_title;
title = block.block_title + ": " + startTime + " - " + endTime;
if (block.description.length > 0) {
description = " - "+block.description;
btitle = btitle+"\n"+description;
}
}
else {
if (layoutMode == 'view-template') {
btitle = block.block_title;
title = block.block_title;
if (block.description.length > 0) {
description = " - "+block.description;
btitle = btitle+"\n"+description;
}
}
else {
btitle = block.block_title;
if (block.num_appointment_times == 0) {
title = block.block_title + ": " + startTime + " - " + endTime;
if (block.description.length > 0) {
description = " - "+block.description;
btitle = btitle+"\n"+description;
}
}
else {
if (block.description.length > 0) {
btitle = btitle+"\n - "+block.description;
}
}
if ((opt('userType') == 'Admin' || opt('userType') == 'Staff') && layoutMode == 'booking-template') {
var calendar_notes = block.calendar_notes;
var notes_icon = "/images/icons/textfile16.png";
var print_icon = "/images/icons/printer16.png";
var user_icon = "/images/icons/usere16.png";
if (calendar_notes.length > 0) notes_icon = "/images/icons/textfile16.png";
noteICON = "";
printICON = '';
if (printBlock == 'Y') printICON = "";
blockIcons = "" + noteICON + printICON + "";
}
rule_class = '';
if (block.rule_class.length > 0) {
rule_class = block.rule_class;
if (rule_class.indexOf('rule-block-data') !== -1) {
title = "" + block.block_title + "";
if (block.description.length > 0) description = "
- "+block.description;
}
}
}
}
// Blocks
s += "" +
blockIcons + "
" + title + "" + description + "
" +
"
" +
"
" +
"
" +
"
" +
"
" +
"
" +
"
" +
"
" +
"
" +
"
" +
"
";
}
else {
// Display appointment times
title = startTime + " - " + endTime;
if (block.appt_title && block.appt_title.length) title += ' - ' + block.appt_title;
// Format icons for editing
rnum = block.record_num;
bnum = block.block_num;
blockIcons = "";
if (layoutMode == 'edit-template') {
editICON = "";
deleteICON = "";
blockIcons = "" + deleteICON + editICON + "";
}
// Format appointment count for display
appt_count = '';
if (block.appt_count) appt_count = block.appt_count + " ";
// Format student name for display
student_name = '';
student_num = 0;
if (block.student_name) {
student_name = block.student_name;
student_num = block.student_num;
title_text = '';
if (block.appt_class != 'Unavailable') {
if (block.appt_by && block.appt_by.length > 0) student_name = student_name+" ("+block.appt_by+")";
}
}
else {
title_text = "";
title_text = block.block_title;
// if (block.description.length > 0) {
// title_text = title_text+" - "+block.description;
// }
}
if (block.phone) {
phone = block.phone;
}
else phone = '';
if (block.email) {
email = block.email;
}
else email = '';
appt_note = '';
if (block.appt_note && block.appt_note.length) appt_note = " - " + block.appt_note;
title_note = appt_note;
if (block.contact_info) title_note = title_note + block.contact_info;
var note_div = "";
if (opt('showNotes') == 'Y') note_div = "" + email + ' ' + phone + ' ' + block.appt_note_text + "
";
else note_div = "" + email + ' ' + phone + "
";
var extraStyle = '';
if (block.custom_color !== null && block.custom_color.length > 0) {
extraStyle = "background-color: #" + block.custom_color + "; color: #" + block.custom_text_color + ";";
}
s += "" +
"" + title + "" + blockIcons +
"" + appt_count + student_name + "
" +
note_div +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"
";
}
}
return s;
}
function opt(name) {
var v = options[name];
return v;
}
}
})(jQuery);
function formatDate(date, format, options) {
return formatDates(date, null, format, options);
}
function formatDates(date1, date2, format, options) {
// options = options || defaults;
var date = date1,
otherDate = date2,
i, len = format.length, c,
i2, formatter,
res = '';
for (i=0; ii; i2--) {
if (formatter = dateFormatters[format.substring(i, i2)]) {
if (date) {
res += formatter(date, options);
}
i = i2 - 1;
break;
}
}
if (i2 == i) {
if (date) {
res += c;
}
}
}
}
return res;
};
function zeroPad(n) {
return (n < 10 ? '0' : '') + n;
}
var dateFormatters = {
s : function(d) { return d.getSeconds() },
ss : function(d) { return zeroPad(d.getSeconds()) },
m : function(d) { return d.getMinutes() },
mm : function(d) { return zeroPad(d.getMinutes()) },
h : function(d) { return d.getHours() % 12 || 12 },
hh : function(d) { return zeroPad(d.getHours() % 12 || 12) },
H : function(d) { return d.getHours() },
HH : function(d) { return zeroPad(d.getHours()) },
d : function(d) { return d.getDate() },
dd : function(d) { return zeroPad(d.getDate()) },
ddd : function(d,o) { return o.dayNamesShort[d.getDay()] },
dddd: function(d,o) { return o.dayNames[d.getDay()] },
M : function(d) { return d.getMonth() + 1 },
MM : function(d) { return zeroPad(d.getMonth() + 1) },
MMM : function(d,o) { return o.monthNamesShort[d.getMonth()] },
MMMM: function(d,o) { return o.monthNames[d.getMonth()] },
yy : function(d) { return (d.getFullYear()+'').substring(2) },
yyyy: function(d) { return d.getFullYear() },
t : function(d) { return d.getHours() < 12 ? 'a' : 'p' },
tt : function(d) { return d.getHours() < 12 ? 'am' : 'pm' },
T : function(d) { return d.getHours() < 12 ? 'A' : 'P' },
TT : function(d) { return d.getHours() < 12 ? 'AM' : 'PM' },
u : function(d) { return formatDate(d, "yyyy-MM-dd'T'HH:mm:ss'Z'") },
S : function(d) {
var date = d.getDate();
if (date > 10 && date < 20) {
return 'th';
}
return ['st', 'nd', 'rd'][date%10-1] || 'th';
}
};
function zeroDate() { // returns a Date with time 00:00:00 and dateOfMonth=1
var i=0, d;
do {
d = new Date(1970, i++, 1);
} while (d.getHours()); // != 0
return d;
}
function addMinutes(d, n) {
d.setMinutes(d.getMinutes() + n);
return d;
}
function parseTime(s) { // returns minutes since start of day
if (typeof s == 'number') { // an hour
return s * 60;
}
if (typeof s == 'object') { // a Date object
return s.getHours() * 60 + s.getMinutes();
}
var m = s.match(/(\d+)(?::(\d+))?\s*(\w+)?/);
if (m) {
var h = parseInt(m[1], 10);
if (m[3]) {
h %= 12;
if (m[3].toLowerCase().charAt(0) == 'p') {
h += 12;
}
}
return h * 60 + (m[2] ? parseInt(m[2], 10) : 0);
}
}