/** * Backing JS for Special:CentralNoticeBanners/edit, the form that allows * editing of banner content and changing of banner settings. * * This file is part of the CentralNotice Extension to MediaWiki * https://www.mediawiki.org/wiki/Extension:CentralNotice * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * http://www.gnu.org/copyleft/gpl.html */ ( function () { var bannerEditor, bannerName, $previewFieldSet, $previewContent, $bannerMessages, fileScopedOpenExternalPreview, // Prefix for key used to store banner preview content for external preview. // Coordinate with PREVIEW_STORAGE_KEY_PREFIX in ext.centralNotice.display.js PREVIEW_STORAGE_KEY_PREFIX = 'cn-banner-preview-'; function doPurgeCache() { var language = $( '#cn-cdn-cache-language' ).val(), messageId = 'centralnotice-purge-cache-' + language, waiting; // Do nothing if the button was disabled (from lack of CN admin rights) if ( $( '#cn-cdn-cache-purge' ).prop( 'disabled' ) ) { return; } // Show notification with info if the background call takes a while to return waiting = setTimeout( function () { mw.notify( mw.message( 'centralnotice-banner-cdn-dialog-waiting-text' ).text(), { autoHide: false, tag: messageId } ); }, 300 ); new mw.Api().postWithToken( 'csrf', { action: 'centralnoticecdncacheupdatebanner', banner: bannerName, language: language }, { timeout: 2000 } ).always( function () { clearTimeout( waiting ); } ).fail( function ( code, result ) { var text = mw.message( 'centralnotice-banner-cdn-dialog-error' ).text(); if ( result && result.error && result.error.info ) { text += ' (' + result.error.info + ')'; } else if ( result && result.exception ) { text += ' (' + result.exception + ')'; } mw.notify( text, { type: 'error', tag: messageId } ); } ).done( function () { mw.notify( mw.message( 'centralnotice-banner-cdn-dialog-success' ).text(), { type: 'success', tag: messageId } ); } ); } /** * Collects unsaved messages values from banner editing form (if any) * * @return {Object} */ function getUnsavedMessagesValues() { var bannerMessagesCache = {}; if ( $bannerMessages.length ) { $bannerMessages.each( function ( i, message ) { var label = $( message ).find( 'label' ).text(), value = $( message ).find( 'textarea' ).val(); bannerMessagesCache[ label ] = value; } ); } return bannerMessagesCache; } /** * Renders banner content preview in live preview section. * * @param {boolean} openExternalPreview */ function fetchAndUpdateBannerPreview( openExternalPreview ) { var $bannerContentTextArea = $( '#mw-input-wpbanner-body' ), bannerMessagesCache = getUnsavedMessagesValues(), url = new mw.Uri( mw.config.get( 'wgCentralNoticeActiveBannerDispatcher' ) ); // Set this file-scoped variable so the callback knows whether to open the // external preview. fileScopedOpenExternalPreview = openExternalPreview; // Activate the barbershop "loading" animation $previewFieldSet.attr( 'disabled', true ); // Send the raw unsaved banner content and messages for server-side rendering. // This will call bannerEditor.updateBannerPreview(). $.post( url.toString(), { banner: bannerName, previewcontent: $bannerContentTextArea.val(), previewmessages: bannerMessagesCache, token: mw.user.tokens.get( 'csrfToken' ) } ).fail( function ( jqXHR, status, error ) { bannerEditor.handleBannerLoaderError( status + ': ' + error ); } ).always( function () { // De-activate the barbershop "loading" animation $previewFieldSet.attr( 'disabled', false ); } ); } // TODO Several functions exposed aren't used elsewhere, so they should be private mw.centralNotice.adminUi.bannerEditor = bannerEditor = { /** * Display the 'Create Banner' dialog * * @return {boolean} */ doCloneBannerDialog: function () { var buttons = {}, okButtonText = mw.message( 'centralnotice-clone' ).text(), cancelButtonText = mw.message( 'centralnotice-clone-cancel' ).text(), $dialogObj = $( '