/* exported ampValidatedUrlPostEditScreen */ const ampValidatedUrlPostEditScreen = ( function() { // eslint-disable-line no-unused-vars let component = { data: { l10n: { unsaved_changes: '', showing_number_errors: '', page_heading: '', show_all: '', amp_enabled: false } } }; /** * The id for the 'Showing x of y errors' notice. * * @var {string} */ component.idNumberErrors = 'number-errors'; /** * The id for the 'Show all' button. * * @var {string} */ component.showAllId = 'show-all-errors'; /** * Boot. * * @param {Object} data Data. * @param {Object} data.l10n Translations. */ component.boot = function boot( data ) { Object.assign( component.data, data ); component.handleShowAll(); component.handleFiltering(); component.handleSearching(); component.handleStatusChange(); component.handleBulkActions(); component.changeHeading(); component.watchForUnsavedChanges(); component.showAMPIconIfEnabled(); }; /** * Add prompt when leaving page due to unsaved changes. */ component.addBeforeUnloadPrompt = function addBeforeUnloadPrompt() { if ( component.beforeUnloadPromptAdded ) { return; } window.addEventListener( 'beforeunload', component.onBeforeUnload ); // Remove prompt when clicking trash or update. document.querySelector( '#major-publishing-actions' ).addEventListener( 'click', function() { window.removeEventListener( 'beforeunload', component.onBeforeUnload ); } ); component.beforeUnloadPromptAdded = true; }; /** * Watch for unsaved changes. * * Add an beforeunload warning when attempting to leave the page when there are unsaved changes, * unless the user is pressing the trash link or update button. */ component.watchForUnsavedChanges = function watchForUnsavedChanges() { const onChange = function( event ) { if ( event.target.matches( 'select' ) ) { document.getElementById( 'post' ).removeEventListener( 'change', onChange ); component.addBeforeUnloadPrompt(); } }; document.getElementById( 'post' ).addEventListener( 'change', onChange ); }; /** * Show message at beforeunload. * * @param {Event} event - The beforeunload event. * @return {string} Message. */ component.onBeforeUnload = function onBeforeUnload( event ) { event.preventDefault(); event.returnValue = component.data.l10n.unsaved_changes; return component.data.l10n.unsaved_changes; }; /** * Updates the with 'Showing x of y validation errors' at the top of the list table with the current count. * If this does not exist yet, it creates the element. * * @param {number} numberErrorsDisplaying - The number of errors displaying. * @param {number} totalErrors - The total number of errors, displaying or not. */ component.updateShowingErrorsRow = function updateShowingErrorsRow( numberErrorsDisplaying, totalErrors ) { const showAllButton = document.getElementById( component.showAllId ); let thead, th, tr = document.getElementById( component.idNumberErrors ); const theadQuery = document.getElementsByTagName( 'thead' ); // Only create the if it does not exist yet. if ( theadQuery[ 0 ] && ! tr ) { thead = theadQuery[ 0 ]; tr = document.createElement( 'tr' ); th = document.createElement( 'th' ); th.setAttribute( 'id', component.idNumberErrors ); th.setAttribute( 'colspan', '6' ); tr.appendChild( th ); thead.appendChild( tr ); } // If all of the errors are displaying, hide the 'Show all' button and the count notice. if ( showAllButton && numberErrorsDisplaying === totalErrors ) { showAllButton.classList.add( 'hidden' ); tr.classList.add( 'hidden' ); } else if ( null !== numberErrorsDisplaying ) { // Update the number of errors displaying and create a 'Show all' button if it does not exist yet. document.getElementById( component.idNumberErrors ).innerText = component.data.l10n.showing_number_errors.replace( '%1$s', numberErrorsDisplaying ); document.getElementById( component.idNumberErrors ).classList.remove( 'hidden' ); component.conditionallyCreateShowAllButton(); if ( document.getElementById( component.showAllId ) ) { document.getElementById( component.showAllId ).classList.remove( 'hidden' ); } } }; /** * Conditionally creates and appends a 'Show all' button. */ component.conditionallyCreateShowAllButton = function conditionallyCreateShowAllButton() { const buttonContainer = document.getElementById( 'url-post-filter' ); let showAllButton = document.getElementById( component.showAllId ); // There is no 'Show all'