/** * UI scripts for admin settings page */ import $ from 'jquery'; import 'select2'; import Config from './config'; import Common from './common'; const Utils = { alert_unsaved: function() { $( '.wrap form' ) .on('change.unsaved', ':input:not(#date-range)', function(){ Common.set_unsaved(); }) // Prevent alert if user submitted form .on( 'submit.unsaved', function() { Common.set_saved(); }); window.onbeforeunload = function(){ if ( Common.unsaved ) { return aepc_admin.unsaved; } }; }, unset_alert_unsaved: function() { $( '.wrap form' ).off('change.unsaved submit.unsaved'); window.onbeforeunload = function(){}; }, apply_autocomplete: function( el, data ) { if ( el.is('select') ) { el.select2({ data: { results: data } }); } else { el.select2({ tags: data }); } }, addLoader: function( el ) { if ( typeof el.data('select2') !== 'undefined' ) { let select2 = el.data('select2'), select2container = select2.container; select2container.addClass( 'loading-data' ); } else if ( el.is( 'div, form' ) ) { el.addClass( 'loading-data loading-box' ); } else if ( el.is( 'a' ) ) { el.addClass( 'loading-data' ); } }, removeLoader: function( el ) { if ( typeof el.data('select2') !== 'undefined' ) { let select2 = el.data('select2'), select2container = select2.container; select2container.removeClass( 'loading-data' ); } else if ( el.is( 'div, form' ) ) { el.removeClass( 'loading-data loading-box' ); } else if ( el.is( 'a' ) ) { el.removeClass( 'loading-data' ); } }, removeMessage: function( el, type ) { if ( 'error' === type ) { type = 'danger'; } if ( el.find( '.alert-' + type ).length ) { el.find( '.alert-' + type ).remove(); } }, removeMainMessages: function( type ) { Utils.removeMessage( $('.plugin-content'), type ); }, removeAllMainMessages: function() { Utils.removeMainMessages( 'success' ); Utils.removeMainMessages( 'error' ); }, addMessage: function( el, type, msg ) { if ( 'error' === type ) { type = 'danger'; } const { text: text, dismiss_action: dismissAction = false } = typeof msg === 'object' ? msg : { text: msg }; let dismissButton = $( '