var CaretakerMessage = function() { //Variables //Date to Start displaying caretaker message var startDate = null; //Date to stop displaying caretaker message var endDate = null; //location of json message //https://caretaker-vic-gov.s3-ap-southeast-2.amazonaws.com/message.json var messageFile = 'https://drwgdblqzrfiz.cloudfront.net/message.json'; //location of css file //https://caretaker-vic-gov.s3-ap-southeast-2.amazonaws.com/message.css var caretakerCSS = 'https://drwgdblqzrfiz.cloudfront.net/message.css'; //External link timer in seconds var linkTimer = 5; var jsonData = null; var currentDate = new Date(); var visited = loadStoredData('visited'); var messageType = (typeof ctMessageType !== 'undefined' ? ctMessageType : 'default'); /** * Create message box * * @param data Data supplied from json file * @param contentField field to use for content within json * @param boxClass class to be added to caretaker box * @return messageBox */ function createBox(contentName, boxClass) { data = jsonData.default; if (typeof jsonData[messageType] !== 'undefined') { data = jsonData[messageType]; } removeElement('ct_box'); var messageBox = document.createElement('div'); messageBox.id = 'ct_box'; messageBox.className = 'boxClass'; if (data.image) { var messageImgWrapper = document.createElement('div'); messageImgWrapper.className = 'ct_image'; var messageImg = document.createElement('img'); messageImg.src = data.image; messageImg.alt = (data.imageAlt ? data.imageAlt : ''); messageImgWrapper.appendChild(messageImg); messageBox.appendChild(messageImgWrapper); } var messageBoxCloseButton = document.createElement('a'); messageBoxCloseButton.href = '#'; messageBoxCloseButton.className = 'ct_close'; messageBoxCloseButton.id = 'ct_close'; messageBoxCloseButton.textContent = 'Close'; messageBoxCloseButton.rel = 'noopener noreferrer'; messageBoxCloseButton.title = 'Close the Victorian caretaker notification message. This will store your preference to close the message for this browser session.'; var messageBoxClose = document.createElement('div'); messageBoxClose.className = 'ct_close_box'; messageBoxClose.role = 'button'; messageBoxClose.tabIndex = 0; messageBoxClose.addEventListener('click', function() { removeElement('ct_box'); document.body.style.paddingTop = 0; storeData('visited', true); }); messageBoxClose.appendChild(messageBoxCloseButton); var messageBoxContentWrapper = document.createElement('div'); messageBoxContentWrapper.className = 'ct_body'; messageBoxContentWrapper.innerHTML = data[contentName]; messageBox.appendChild(messageBoxContentWrapper); messageBox.appendChild(messageBoxClose); document.body.appendChild(messageBox); document.body.style.paddingTop = messageBox.clientHeight + 'px'; messageBoxCloseButton.focus(); return messageBox; } /** * Load Caretaker Message * Grabs json file and passes to function to create message box */ function loadCaretakerMessage() { if (visited === false || visited === 'false') { createBox('content', 'ct_box_middle'); window.scrollTo(0,0); } } function loadJSON(sucessCallback, errorCallback) { var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState === XMLHttpRequest.DONE) { if (xhr.status === 200) { jsonData = JSON.parse(xhr.responseText); startDate = new Date(Date.parse(jsonData.startDate)); endDate = new Date(Date.parse(jsonData.endDate)); sucessCallback(); } else { errorCallback(); } } }; xhr.open('GET', messageFile, true); xhr.send(); } /** * Load Caretaker Link Message * Loops over all links on a page and adds a popup to external links * Optional class can be applied to links to skip popup message */ function loadCaretakerLinkMessage() { var data = jsonData.default; if (typeof jsonData[messageType] !== 'undefined') { data = jsonData[messageType]; } var links = document.getElementsByTagName('a'); for (var i = 0; i < links.length; i++) { var link = links[i]; if (link.hostname && link.hostname !== location.hostname && link.hostname.indexOf('vic.gov.au') < 0) { // Update Title Links if (link.title === null || link.title === '') { link.title = link.href; } link.title = link.title + ' | ' + data.linkContent; var showCareLinkMessage = function(e) { e.preventDefault(); var messageBox = createBox('linkContent', 'ct_box_middle'); var bodyText = document.getElementsByClassName('ct_body'); bodyText[0].innerHTML = bodyText[0].innerHTML + "
You will be redirected in " + linkTimer + " seconds…"; setTimeout(function() { e.target.click(); }, linkTimer * 1000); e.target.removeEventListener(e.type, arguments.callee); }; // Add Click Event link.addEventListener('click', showCareLinkMessage, false); } } } function getUrlVars() { var vars = {}; var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m, key, value) { vars[key] = value; }); return vars; } function getCookie(cname) { var name = cname + '='; var decodedCookie = decodeURIComponent(document.cookie); var ca = decodedCookie.split(';'); for (var i = 0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0) == ' ') { c = c.substring(1); } if (c.indexOf(name) == 0) { return c.substring(name.length, c.length); } } return ''; } function setCookie(cname, cvalue, exdays) { var d = new Date(); d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000)); var expires = 'expires=' + d.toUTCString(); document.cookie = cname + '=' + cvalue + ';' + expires + ';path=/'; } function storeData(type, obj) { var data = obj; try { if (typeof sessionStorage !== undefined && sessionStorage !== null) { sessionStorage[type] = data; } else { //jQuery.cookie(type, data, {expires: 1, path: '/'}); setCookie(type, data, 365); } } catch (err) { ; } } function loadStoredData(type) { var data; if (typeof sessionStorage !== undefined && sessionStorage !== null) { data = sessionStorage[type]; } else { data = getCookie(type); } if (data && data.length > 0) { return data; } return false; } function removeElement(elementId) { // Removes an element from the document var element = document.getElementById(elementId); if (element !== null) { element.parentNode.removeChild(element); } } function loadStyleSheet() { var fileRef = document.createElement('link'); fileRef.setAttribute('rel', 'stylesheet'); fileRef.setAttribute('type', 'text/css'); fileRef.setAttribute('href', caretakerCSS); document.getElementsByTagName('head')[0].appendChild(fileRef); } this.init = function() { loadJSON(function() { if ((currentDate.getTime() < startDate.getTime() || currentDate.getTime() > endDate.getTime())) { return false; } loadStyleSheet(); loadCaretakerMessage(); loadCaretakerLinkMessage(); }, function() { }); }(); } ; function ready(callback) { // in case the document is already rendered if (document.readyState != 'loading') { callback(); }// modern browsers else if (document.addEventListener) { document.addEventListener( 'DOMContentLoaded', callback); }// IE <= 8 else { document.attachEvent('onreadystatechange', function() { if (document.readyState == 'complete') { callback(); } }); } } function initCaretakerScript() { ready(function() { CaretakerMessage(); }); } function loadjQuery(url, success) { var script = document.createElement('script'); script.src = url; var head = document.getElementsByTagName('head')[0], done = false; head.appendChild(script); // Attach handlers for all browsers script.onload = script.onreadystatechange = function() { if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) { done = true; success(); script.onload = script.onreadystatechange = null; head.removeChild(script); } }; } initCaretakerScript();