'use strict'; var checkNavWrapping = function() { var navMainCalendar = $('nav.main_calendar'); var wrapped = false; var lastTop; navMainCalendar.removeClass('wrapped'); navMainCalendar.first().children().each(function() { var thisTop = $(this).offset().top; if ((typeof lastTop !== 'undefined') && (Math.abs(thisTop - lastTop) > 5)) { wrapped = true; return false; } lastTop = thisTop; }); if (wrapped) { navMainCalendar.addClass('wrapped'); } }; var checkNav = function() { var nav = $('nav.main_calendar'); if (nav.eq(0).visible(true)) { nav.eq(1).hide(); } else { nav.eq(1).show(); } }; var replaceBody = function(response, href) { element. // That's because if we replace the whole HTML the browser will re-load the JavaScript and // CSS files which is unnecessary and will also cause problems if the CSS is not loaded in // time. // // Unfortunately, we can't use jQuery.replaceWith() on the body object as that doesn't work // properly. So we have to replace the body HTML and then update the attributes for the body // tag afterwards. ?> var matches = response.match(/(]*>)([^<]*(?:(?!<\/?body)<[^<]*)*)<\/body\s*>/i); var body = $('body'); body.html(matches[2]); $('').each(function() { $.each(this.attributes, function() { if(this.specified) { if (this.name.substring(0, 5).toLowerCase() === 'data-') { var value = this.value; if (value.charAt(0) === '[') { try { value = JSON.parse(value); } catch (e) { value = this.value; } } else if (value === 'true') { value = true; } else if (value === 'false') { value = false; } body.data(this.name.substring(5), value); } else { body.attr(this.name, this.value); } } }); }); $(document).trigger('page_ready'); var data = {csrf_token: getCSRFToken(), page: href}; if(args.site) { data.site = args.site; } $.post('ajax/update_page.php', data); history.pushState(null, '', href); }; element either via an Ajax call or using a pre-fetched response, // in order to avoid flickering of the screen as we move between pages in the calendar view. // // 'event' can either be an event object if the function is called from an 'on' // handler, or else it as an href string (eg when called from flatpickr). ?> var updateBody = function(event) { var href; if (typeof event === 'object') { href = $(this).attr('href'); event.preventDefault(); } else { href = event; } $('h2.date').text('') .addClass('loading'); if (updateBody.prefetched && updateBody.prefetched[href]) { replaceBody(updateBody.prefetched[href], href); } else { updateBody.lastRequest = href; refreshPage.disabled = true; $.get({url: href, dataType: 'html'}) .done(function(response) { if (href === updateBody.lastRequest) { updateBody.lastRequest = null; refreshPage.disabled = false; replaceBody(response, href); } }); } }; var prefetch = function() { return; if (updateBody.lastRequest || isMeteredConnection()) { return; } var activeConnections = 0; var delay = * 1000; var hrefs = []; $('a.prefetch').each(function() { var a = $(this); if (a.is(':visible')) { var href = a.attr('href'); if (typeof href !== 'undefined') { hrefs.push(href); } } }); updateBody.prefetched = {}; clearTimeout(prefetch.timeoutId); if (isHidden()) { prefetch.timeoutId = setTimeout(prefetch, delay); return; } hrefs.forEach(function(href) { activeConnections++; $.get({url: href, dataType: 'html'}) .fail(function() { }) .done(function(response) { updateBody.prefetched[href] = response; activeConnections--; if (activeConnections === 0) { prefetch.timeoutId = setTimeout(prefetch, delay); } }); }); }; $(document).on('page_ready', function() { $('.room_area_select').mrbsSelect(); $('nav.location').removeClass('js_hidden'); checkNavWrapping(); $(window).on('resize', checkNavWrapping); $('nav.main_calendar').removeClass('js_hidden'); checkNav(); $(window).on('scroll', checkNav); $(window).on('resize', checkNav); $('.color_key').removeClass('js_hidden'); $('nav.arrow a, nav.view a').on('click', updateBody); if (args.kiosk) { var dialog = $('
'); var timeout; $(document.body).on('click keypress', function(e) { function dialogClose() { dialog.dialog('close'); } e.preventDefault(); if (!dialog.dialog('instance')) { dialog.dialog({ buttons: [ {text: "", click: function() { var href = 'kiosk.php?kiosk=' + encodeURIComponent(args.kiosk); href += '&area=' + encodeURIComponent(args.area); href += '&room=' + encodeURIComponent(args.room); if (args.site) { href += '&site=' + encodeURIComponent(args.site); } $.redirect(href, {'csrf_token': getCSRFToken()}); } }, {text: "", click: function() { dialog.dialog('close'); } } ], close: function(event, ui) { clearTimeout(timeout); }, closeText: "", modal: true, open: function(event, ui){ timeout = setTimeout(dialogClose, *1000); }, title: "" }); } else if (!dialog.dialog('isOpen') && !$(e.target).parents('[role="dialog"]').length) { dialog.dialog('open'); } return false; }); } prefetch(); });