'use strict';
var refreshListenerAdded = false;
var intervalId;
function scrollToCurrentSlot() {
var table = $('.dwm_main');
var timelineVertical = table.find('thead').data('timeline-vertical');
var container = table.parent();
var scrollTo, scrollable;
scrollable = (timelineVertical) ? container.isHScrollable() : container.isVScrollable();
if (scrollable)
{
var slots = table.find('thead').data('slots');
var nowSlotIndices = Timeline.search(slots);
if (nowSlotIndices.length > 1)
{
var index = Math.max(0, nowSlotIndices[0] - 1);
}
if (index > 0)
{
if (timelineVertical)
{
var cols = table.find('thead th:not(.first_last)');
scrollTo = cols.eq(index).offset().left - cols.eq(0).offset().left;
container.scrollLeft(scrollTo);
}
else
{
var rows = table.find('tbody tr');
scrollTo = rows.eq(index).offset().top - rows.eq(0).offset().top;
container.scrollTop(scrollTo);
}
}
}
}
var sizeColumns = function() {
var mainCols = $('.dwm_main thead tr:first-child th:visible').not('th.first_last, th.hidden_day');
mainCols.css('width', 100/mainCols.length + '%');
};
var refreshPage = function refreshPage() {
var table = $('table.dwm_main');
if (!isHidden() &&
!table.hasClass('resizing') &&
(args.kiosk || !isMeteredConnection()) &&
!refreshPage.disabled)
{
var url = 'index.php';
var data = {
refresh: 1,
view: args.view,
view_all: args.view_all,
page_date: args.pageDate,
area: args.area,
room: args.room
};
if (args.timetohighlight !== undefined)
{
data.timetohighlight = args.timetohighlight;
}
table.addClass('refreshable');
if(args.site)
{
data.site = args.site;
}
// If we're in kiosk mode add the kiosk parameter in the query string rather than as a POST
// parameter to avoid an unnecessary redirection by the server (the server checks for a kiosk
// in the query string and, if not found, will redirect to the kiosk URL).
if (args.kiosk !== undefined)
{
url += '?kiosk=' + args.kiosk;
}
return $.post(
url,
data,
function(result){
if (result && !isHidden() && !refreshPage.disabled)
{
var table = $('table.dwm_main');
if (!table.hasClass('resizing') && table.hasClass('refreshable'))
{
var dateHeading = $('.date_heading');
dateHeading.empty().html(result.date_heading);
table.empty().html(result.inner_html);
window.clearInterval(intervalId);
intervalId = undefined;
table.trigger('tableload');
}
}
},
'json'
);
}
};
var refreshVisChanged = function refreshVisChanged() {
var pageHidden = isHidden();
if (pageHidden !== null)
{
if (typeof intervalId !== 'undefined')
{
window.clearInterval(intervalId);
intervalId = undefined;
}
if (!pageHidden)
{
refreshPage().done(function() {
prefetch();
});
}
}
};
var Timeline = {
timerRunning: null,
clear: function() {
$('.timeline').remove();
if (Timeline.timerRunning)
{
window.clearInterval(Timeline.timerRunning);
Timeline.timerRunning = null;
}
},
getFirstNonZeroSlotSize: function() {
var slotSize;
var table = $('#day_main');
table.find('thead th').not('.first_last').each(function () {
slotSize = $(this).outerWidth();
if (slotSize)
{
return false;
}
});
table.find('tbody tr').each(function () {
slotSize = $(this).outerHeight();
if (slotSize)
{
return false;
}
});
return slotSize;
},
search: function(slots, time) {
function within(slots, time)
{
function getFirst(arr)
{
if (Array.isArray(arr))
{
return getFirst(arr[0]);
}
return arr;
}
function getLast(arr)
{
if (Array.isArray(arr))
{
return getLast(arr[arr.length - 1]);
}
return arr;
}
return ((getFirst(slots) <= time) && (getLast(slots) > time));
}
function getIndices(slots, time) {
var element;
for (var i=slots.length - 1; i>=0; i--) {
element = slots[i];
if (within(element, time))
{
if (Array.isArray(element[0]))
{
getIndices(element, time);
}
result.push(i);
break;
}
}
}
var result = [];
if ((typeof time === 'undefined'))
{
time = Math.floor(Date.now() / 1000);
}
if ((typeof slots !== 'undefined') && within(slots, time))
{
getIndices(slots, time);
}
return result;
},
show: function () {
if (isHidden())
{
return;
}
$('.timeline').remove();
var now = Math.floor(Date.now() / 1000);
var table = $('.dwm_main');
var container = table.parent();
var thead = table.find('thead');
var slots = thead.data('slots');
var timelineVertical = thead.data('timeline-vertical');
var timelineFull = thead.data('timeline-full');
var nowSlotIndices, slot, fraction, row, element;
var slotSize, delay, timeline;
var top, left, borderLeftWidth, borderRightWidth, width, height;
var headers, headersFirstLast, headersNormal, headerFirstSize, headerLastSize;
nowSlotIndices = Timeline.search(slots);
if (nowSlotIndices.length > 1)
{
slot = slots;
for (var i=nowSlotIndices.length - 1; i>=0; i--)
{
slot = slot[nowSlotIndices[i]];
}
fraction = (now-slot[0]) / (slot[1]-slot[0]);
header cells in because they are useful for working out the
// dimensions of slots in the table. We can't rely on the cells in the