diff --git a/admin/admin.php b/admin/admin.php index f10b134..ab2308d 100644 --- a/admin/admin.php +++ b/admin/admin.php @@ -36,7 +36,7 @@ function bogo_admin_enqueue_scripts( $hook_suffix ) { } wp_enqueue_script( 'bogo-admin', - plugins_url( 'admin/includes/js/admin.js', BOGO_PLUGIN_BASENAME ), + plugins_url( 'admin/includes/js/admin.min.js', BOGO_PLUGIN_BASENAME ), array( 'jquery' ), BOGO_VERSION, true ); diff --git a/admin/includes/js/admin.js b/admin/includes/js/admin.js deleted file mode 100644 index 5e8b121..0000000 --- a/admin/includes/js/admin.js +++ /dev/null @@ -1,110 +0,0 @@ -( function( $ ) { - - 'use strict'; - - if ( typeof bogo === 'undefined' || bogo === null ) { - return; - } - - bogo.langName = function( locale ) { - return bogo.availableLanguages[ locale ] || ''; - }; - - $( function() { - $( 'body.options-general-php select#WPLANG' ).each( function() { - $( this ).find( 'option[selected="selected"]' ).removeAttr( 'selected' ); - var val = bogo.defaultLocale || 'en_US'; - val = ( 'en_US' == val ? '' : val ); - $( this ).find( 'option[value="' + val + '"]' ).first().attr( 'selected', 'selected' ); - } ); - } ); - - $( function() { - $( '#bogo-add-translation' ).click( function() { - if ( ! bogo.currentPost.postId ) { - return; - } - - var locale = $( '#bogo-translations-to-add' ).val(); - var rest_url = bogo.apiSettings.getRoute( - '/posts/' + bogo.currentPost.postId + '/translations/' + locale ); - $( '#bogo-add-translation' ).next( '.spinner' ) - .css( 'visibility', 'visible' ); - - $.ajax( { - type: 'POST', - url: rest_url, - beforeSend: function( xhr ) { - xhr.setRequestHeader( 'X-WP-Nonce', bogo.apiSettings.nonce ); - } - } ).done( function( response ) { - var post = response[ locale ]; - - if ( ! post ) { - return; - } - - var $added = $( '' ).attr( { - href: post.edit_link, - target: '_blank', - rel: 'noopener noreferrer' - } ).html( function() { - var output = post.title.rendered; - output += ' ' - + bogo.l10n.targetBlank + ''; - return output; - } ); - - $added = $( '
' ).append( $added ).append( - ' [' + bogo.availableLanguages[ locale ] + ']' ); - $( '#bogo-translations' ).append( $added ); - - $( '#bogo-translations-to-add option[value="' + locale + '"]' ).detach(); - - if ( $( '#bogo-translations-to-add option' ).length < 1 ) { - $( '#bogo-add-translation-actions' ).detach(); - } - } ).always( function() { - $( '#bogo-add-translation' ).next( '.spinner' ).css( 'visibility', 'hidden' ); - } ); - } ); - } ); - - $( function() { - if ( 'bogo-texts' == bogo.pagenow ) { - $( window ).on( 'beforeunload', function( event ) { - var changed = false; - - $( '#bogo-terms-translation :text' ).each( function() { - if ( this.defaultValue != $( this ).val() ) { - changed = true; - } - } ); - - if ( changed ) { - event.returnValue = bogo.l10n.saveAlert; - return bogo.l10n.saveAlert; - } - } ); - - $( '#bogo-terms-translation' ).submit( function() { - $( window ).off( 'beforeunload' ); - } ); - - $( '#select-locale' ).change( function() { - location = 'admin.php?page=bogo-texts&locale=' + $( this ).val(); - } ); - } - } ); - - bogo.apiSettings.getRoute = function( path ) { - var url = bogo.apiSettings.root; - - url = url.replace( - bogo.apiSettings.namespace, - bogo.apiSettings.namespace + path ); - - return url; - }; - -} )( jQuery ); diff --git a/admin/includes/js/admin.min.js b/admin/includes/js/admin.min.js new file mode 100644 index 0000000..4c2017f --- /dev/null +++ b/admin/includes/js/admin.min.js @@ -0,0 +1 @@ +!function(){var e,t;"undefined"!=typeof bogo&&null!==bogo&&(bogo.langName=function(e){return bogo.availableLanguages[e]||""},bogo.apiSettings.getRoute=function(e){return bogo.apiSettings.root.replace(bogo.apiSettings.namespace,bogo.apiSettings.namespace+e)},(e=document.querySelector("#bogo-add-translation"))&&e.addEventListener("click",function(){var r,e,i,l;bogo.currentPost.postId&&(r=document.querySelector("#bogo-translations-to-add").value,e=bogo.apiSettings.getRoute("/posts/"+bogo.currentPost.postId+"/translations/"+r),(i=document.querySelector("#bogo-add-translation").nextElementSibling).style.visibility="visible",(l=new XMLHttpRequest).onreadystatechange=function(e){if(l.readyState==XMLHttpRequest.DONE){if(200==l.status){var t=JSON.parse(l.response)[r];if(!t)return;var o=document.getElementById("bogo-translations"),n=document.createElement("LI"),a=t.title.rendered;a+=' '+bogo.l10n.targetBlank+"",n.innerHTML=''+a+" ["+bogo.availableLanguages[r]+"]",o.appendChild(n),document.querySelector('#bogo-translations-to-add option[value="'+r+'"]').remove(),document.querySelector("#bogo-translations-to-add").options.length||document.querySelector("#bogo-add-translation-actions").remove()}i.style.visibility="hidden"}},l.open("POST",e),l.setRequestHeader("Content-Type","application/x-www-form-urlencoded"),l.setRequestHeader("X-WP-Nonce",bogo.apiSettings.nonce),l.send())}),"bogo-texts"==bogo.pagenow&&(window.onbeforeunload=function(e){var t=!1;if(document.querySelectorAll("#bogo-terms-translation input[type=text]").forEach(function(e){e.defaultValue!=e.value&&(t=!0)}),t)return e.returnValue=bogo.l10n.saveAlert,bogo.l10n.saveAlert},document.querySelector("#bogo-terms-translation").addEventListener("submit",function(){window.onbeforeunload=function(){}}),(t=document.querySelector("#select-locale")).addEventListener("change",function(){window.location.href="admin.php?page=bogo-texts&locale="+t.value})))}(); \ No newline at end of file diff --git a/admin/includes/ts/admin.ts b/admin/includes/ts/admin.ts new file mode 100644 index 0000000..d1458a8 --- /dev/null +++ b/admin/includes/ts/admin.ts @@ -0,0 +1,121 @@ +interface bogoObject { + langName: any; + availableLanguages: any; + apiSettings: any; + currentPost: any; + l10n: any; + pagenow: any; +} + +declare var bogo: bogoObject; + +(function () { + + if (typeof bogo === 'undefined' || bogo === null) { + return; + } + + bogo.langName = function (locale: any) { + return bogo.availableLanguages[locale] || ''; + }; + + bogo.apiSettings.getRoute = function (path: any) { + let url = bogo.apiSettings.root; + url = url.replace(bogo.apiSettings.namespace, bogo.apiSettings.namespace + path); + + return url; + }; + + const bogo_add_translation = document.querySelector('#bogo-add-translation'); + + if (bogo_add_translation) { + + bogo_add_translation.addEventListener('click', function () { + + if (!bogo.currentPost.postId) { + return; + } + + const locale: any = (