');\n var $preparedElementToDisplay = typeof $elementToDisplay === 'string' ? utils.generateDomElementFromString($elementToDisplay) : $elementToDisplay;\n var resetElementsFunctionList = [];\n var closeCallbacks = [];\n $dialog.appendChild($preparedElementToDisplay);\n if (!utils.hasClass('html', 'noScroll')) {\n utils.addClass('html', 'noScroll');\n resetElementsFunctionList.push(function () {\n utils.removeClass('html', 'noScroll');\n });\n }\n document.body.appendChild($dialog);\n document.body.appendChild($overlay);\n resetElementsFunctionList.push(function () {\n utils.removeElement($dialog);\n utils.removeElement($overlay);\n });\n var setupFocusHandlerAndFocusDialog = function setupFocusHandlerAndFocusDialog() {\n function keepFocus(event) {\n var modalFocus = document.getElementById('hmrc-timeout-dialog');\n if (modalFocus) {\n if (event.target !== modalFocus && !modalFocus.contains(event.target)) {\n event.stopPropagation();\n modalFocus.focus();\n }\n }\n }\n var elemToFocusOnReset = document.activeElement;\n $dialog.focus();\n document.addEventListener('focus', keepFocus, true);\n resetElementsFunctionList.push(function () {\n document.removeEventListener('focus', keepFocus);\n elemToFocusOnReset.focus();\n });\n };\n\n // disable the non-dialog page to prevent confusion for VoiceOver users\n var selectors = ['#skiplink-container', 'body > header', '#global-cookie-message', 'main', 'body > footer', 'body > .govuk-skip-link', '.cbanner-govuk-cookie-banner', 'body > .govuk-width-container'];\n var elements = document.querySelectorAll(selectors.join(', '));\n var close = function close() {\n while (resetElementsFunctionList.length > 0) {\n var fn = resetElementsFunctionList.shift();\n fn();\n }\n };\n var closeAndInform = function closeAndInform() {\n closeCallbacks.forEach(function (fn) {\n fn();\n });\n close();\n };\n var setupKeydownHandler = function setupKeydownHandler() {\n function keydownListener(e) {\n if (e.keyCode === 27) {\n closeAndInform();\n }\n }\n document.addEventListener('keydown', keydownListener);\n resetElementsFunctionList.push(function () {\n document.removeEventListener('keydown', keydownListener);\n });\n };\n var preventMobileScrollWhileAllowingPinchZoom = function preventMobileScrollWhileAllowingPinchZoom() {\n var handleTouch = function handleTouch(e) {\n var touches = e.touches || e.changedTouches || [];\n if (touches.length === 1) {\n e.preventDefault();\n }\n };\n document.addEventListener('touchmove', handleTouch, true);\n resetElementsFunctionList.push(function () {\n document.removeEventListener('touchmove', handleTouch, true);\n });\n };\n elements.forEach(function ($elem) {\n var value = $elem.getAttribute('aria-hidden');\n $elem.setAttribute('aria-hidden', 'true');\n resetElementsFunctionList.push(function () {\n if (value) {\n $elem.setAttribute('aria-hidden', value);\n } else {\n $elem.removeAttribute('aria-hidden');\n }\n });\n });\n\n //\n setupFocusHandlerAndFocusDialog();\n setupKeydownHandler();\n preventMobileScrollWhileAllowingPinchZoom();\n return {\n closeDialog: function closeDialog() {\n close();\n },\n setAriaLabelledBy: function setAriaLabelledBy(value) {\n if (value) {\n $dialog.setAttribute('aria-labelledby', value);\n } else {\n $dialog.removeAttribute('aria-labelledby');\n }\n },\n addCloseHandler: function addCloseHandler(closeHandler) {\n closeCallbacks.push(closeHandler);\n }\n };\n }\n var dialog = {\n displayDialog: displayDialog\n };\n\n function ValidateInput() {}\n ValidateInput[\"int\"] = function (stringToValidate) {\n var parsedInt = parseInt(stringToValidate, 10);\n return Number.isNaN(parsedInt) ? undefined : parsedInt;\n };\n ValidateInput.string = function (stringToValidate) {\n return typeof stringToValidate === 'string' ? stringToValidate : undefined;\n };\n ValidateInput[\"boolean\"] = function (stringToValidate) {\n return String(stringToValidate).toLowerCase() === 'true';\n };\n\n function RedirectHelper() {}\n RedirectHelper.redirectToUrl = function (url) {\n // This exists to make redirects more testable\n window.location.href = url;\n };\n\n // TODO: rewrite this to follow govuk-frontend prototype module pattern\n\n function TimeoutDialog($module, $sessionActivityService) {\n var options = {};\n var settings = {};\n var cleanupFunctions = [];\n var currentTimer;\n var sessionActivityService = $sessionActivityService;\n function init() {\n var validate = ValidateInput;\n function lookupData(key) {\n return ($module.attributes.getNamedItem(key) || {}).value;\n }\n var localisedDefaults = validate.string(lookupData('data-language')) === 'cy' ? {\n title: 'Rydych ar fin cael eich allgofnodi',\n message: 'Er eich diogelwch, byddwn yn eich allgofnodi cyn pen',\n keepAliveButtonText: 'Parhau i fod wedi’ch mewngofnodi',\n signOutButtonText: 'Allgofnodi',\n properties: {\n minutes: 'funud',\n minute: 'funud',\n seconds: 'eiliad',\n second: 'eiliad'\n }\n } : {\n title: 'You’re about to be signed out',\n message: 'For your security, we will sign you out in',\n keepAliveButtonText: 'Stay signed in',\n signOutButtonText: 'Sign out',\n properties: {\n minutes: 'minutes',\n minute: 'minute',\n seconds: 'seconds',\n second: 'second'\n }\n };\n options = {\n timeout: validate[\"int\"](lookupData('data-timeout')),\n countdown: validate[\"int\"](lookupData('data-countdown')),\n keepAliveUrl: validate.string(lookupData('data-keep-alive-url')),\n signOutUrl: validate.string(lookupData('data-sign-out-url')),\n timeoutUrl: validate.string(lookupData('data-timeout-url')),\n title: validate.string(lookupData('data-title')),\n message: validate.string(lookupData('data-message')),\n messageSuffix: validate.string(lookupData('data-message-suffix')),\n keepAliveButtonText: validate.string(lookupData('data-keep-alive-button-text')),\n signOutButtonText: validate.string(lookupData('data-sign-out-button-text')),\n synchroniseTabs: validate[\"boolean\"](lookupData('data-synchronise-tabs') || false),\n hideSignOutButton: validate[\"boolean\"](lookupData('data-hide-sign-out-button') || false)\n };\n\n // Default timeoutUrl to signOutUrl if not set\n options.timeoutUrl = options.timeoutUrl || options.signOutUrl;\n validateInput(options);\n settings = _mergeOptionsWithDefaults(options, localisedDefaults);\n setupDialogTimer();\n listenForSessionActivityAndResetDialogTimer();\n }\n var broadcastSessionActivity = function broadcastSessionActivity() {\n sessionActivityService.logActivity();\n };\n var listenForSessionActivityAndResetDialogTimer = function listenForSessionActivityAndResetDialogTimer() {\n if (settings.synchroniseTabs) {\n sessionActivityService.onActivity(function (event) {\n var timeOfActivity = event.timestamp;\n cleanup();\n setupDialogTimer(timeOfActivity);\n });\n }\n };\n var validateInput = function validateInput(config) {\n var requiredConfig = ['timeout', 'countdown', 'keepAliveUrl', 'signOutUrl'];\n var missingRequiredConfig = [];\n requiredConfig.forEach(function (item) {\n if (!config[item]) {\n missingRequiredConfig.push(\"data-\".concat(item.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase()));\n }\n });\n if (missingRequiredConfig.length > 0) {\n throw new Error(\"Missing config item(s): [\".concat(missingRequiredConfig.join(', '), \"]\"));\n }\n };\n var _mergeOptionsWithDefaults = function mergeOptionsWithDefaults(theOptions, localisedDefaults) {\n var clone = _objectSpread2({}, theOptions);\n Object.keys(localisedDefaults).forEach(function (key) {\n if (_typeof(clone[key]) === 'object') {\n clone[key] = _mergeOptionsWithDefaults(theOptions[key], localisedDefaults[key]);\n }\n if (clone[key] === undefined || clone[key] === '') {\n clone[key] = localisedDefaults[key];\n }\n });\n return clone;\n };\n var setupDialogTimer = function setupDialogTimer() {\n var timeOfLastActivity = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : getDateNow();\n var signoutTime = timeOfLastActivity + settings.timeout * 1000;\n var delta = getDateNow() - timeOfLastActivity;\n var secondsUntilTimeoutDialog = settings.timeout - settings.countdown;\n var timeout = window.setTimeout(function () {\n setupDialog(signoutTime);\n }, secondsUntilTimeoutDialog * 1000 - delta);\n cleanupFunctions.push(function () {\n window.clearTimeout(timeout);\n if (currentTimer) {\n window.clearTimeout(currentTimer);\n }\n });\n };\n var setupDialog = function setupDialog(signoutTime) {\n var $element = utils.generateDomElementFromString('
');\n if (settings.title) {\n var $tmp = utils.generateDomElementFromStringAndAppendText('