/** * ActivityPub Connected Applications JavaScript. * * Handles registering OAuth clients, deleting clients, and revoking * OAuth tokens from the user profile, following the WordPress core * Application Passwords UI pattern. */ /* global activitypubConnectedApps, jQuery, ClipboardJS */ ( function( $ ) { var $section = $( '#activitypub-connected-apps-section' ), $newAppForm = $section.find( '.create-application-password' ), $newAppFields = $newAppForm.find( '.input' ), $newAppButton = $newAppForm.find( '.button' ), $appsWrapper = $section.find( '#activitypub-registered-apps-wrapper' ), $appsTbody = $section.find( '#activitypub-registered-apps-tbody' ), $tokensWrapper = $section.find( '.activitypub-connected-apps-list-table-wrapper' ), $tokensTbody = $section.find( '#activitypub-connected-apps-tbody' ), $revokeAll = $section.find( '#activitypub-revoke-all-tokens' ), $deleteAll = $section.find( '#activitypub-delete-all-clients' ); // Register a new application. $newAppButton.on( 'click', function( e ) { e.preventDefault(); if ( $newAppButton.prop( 'aria-disabled' ) ) { return; } var $name = $( '#activitypub-new-app-name' ); var $redirectUri = $( '#activitypub-new-app-redirect-uri' ); if ( 0 === $name.val().trim().length ) { $name.trigger( 'focus' ); return; } if ( 0 === $redirectUri.val().trim().length ) { $redirectUri.trigger( 'focus' ); return; } clearNotices(); $newAppButton.prop( 'aria-disabled', true ).addClass( 'disabled' ); $.ajax( { url: activitypubConnectedApps.ajaxUrl, method: 'POST', data: { action: 'activitypub_register_oauth_client', name: $name.val().trim(), redirect_uri: $redirectUri.val().trim(), _wpnonce: activitypubConnectedApps.nonce } } ).always( function() { $newAppButton.removeProp( 'aria-disabled' ).removeClass( 'disabled' ); } ).done( function( response ) { if ( ! response.success ) { addNotice( response.data && response.data.message ? response.data.message : activitypubConnectedApps.registerError, 'error' ); return; } // Build credential notice (matches core's tmpl-new-application-password). var $notice = $( '
' ) .attr( 'role', 'alert' ) .attr( 'tabindex', '-1' ) .addClass( 'notice notice-success is-dismissible new-application-password-notice' ); // Client ID row. var $clientIdRow = $( '

' ).addClass( 'application-password-display' ) .append( $( '' ).text( activitypubConnectedApps.clientIdLabel ) ) .append( $( '' ).attr( { type: 'text', readonly: 'readonly' } ).addClass( 'code' ).val( response.data.client_id ) ) .append( $( '' ) .attr( 'type', 'button' ) .addClass( 'notice-dismiss' ) .append( $( '' ).addClass( 'screen-reader-text' ).text( activitypubConnectedApps.dismiss ) ) ); $newAppForm.after( $notice ); return $notice; } /** * Clears notice messages from the Connected Applications section. */ function clearNotices() { $( '.notice', $section ).remove(); } }( jQuery ) );