speechkit
/
src
/
Compatibility
/
Elementor
/
js
/
components
/
beyondwords
/
hooks
/
init-players-after-document-save.js
init-players-after-document-save.js in BeyondWords – AI audio for publishers 4.0.1, at src/Compatibility/Elementor/js/components/beyondwords/hooks/init-players-after-document-save.js
| 1 | /* global jQuery, $e, elementor */ |
| 2 | |
| 3 | // UI hook, fired after the command runs. |
| 4 | // Important: Available to run in the console but depends on $e.components example#1. |
| 5 | |
| 6 | export class InitPlayersAfterDocumentSave extends $e.modules.hookData.After { |
| 7 | getCommand() { |
| 8 | // Command to hook. |
| 9 | return 'document/save/save'; |
| 10 | } |
| 11 | |
| 12 | getId() { |
| 13 | // Unique id for the hook. |
| 14 | return 'beyondwords-init-players-after-document-save'; |
| 15 | } |
| 16 | |
| 17 | /* |
| 18 | * The actual hook logic. |
| 19 | */ |
| 20 | apply( args ) { |
| 21 | const { document = elementor.documents.getCurrent(), status } = args; |
| 22 | |
| 23 | if ( status === 'autosave' ) { |
| 24 | return; |
| 25 | } |
| 26 | |
| 27 | window.elementorCommon.ajax |
| 28 | .addRequest( 'get_beyondwords_data', { |
| 29 | error: ( data ) => this.onRequestError( data, document ), |
| 30 | } ) |
| 31 | .then( ( data ) => this.onRequestSuccess( data, document ) ); |
| 32 | } |
| 33 | |
| 34 | onRequestSuccess( data, document ) { |
| 35 | // console.log( 'onRequestSuccess document', document ); |
| 36 | // console.log( 'onRequestSuccess data', data ); |
| 37 | |
| 38 | const { beyondwords_project_id, beyondwords_content_id } = data; |
| 39 | |
| 40 | // console.log( |
| 41 | // 'onRequestSuccess beyondwords_project_id', |
| 42 | // beyondwords_project_id |
| 43 | // ); |
| 44 | |
| 45 | // console.log( |
| 46 | // 'onRequestSuccess beyondwords_content_id', |
| 47 | // beyondwords_content_id |
| 48 | // ); |
| 49 | |
| 50 | // Remove document cache. |
| 51 | elementor.documents.invalidateCache( document.id ); |
| 52 | |
| 53 | // Update Elementor controls |
| 54 | $e.run( 'document/elements/settings', { |
| 55 | container: elementor.settings.page.getEditedView().getContainer(), |
| 56 | settings: { |
| 57 | control_beyondwords_project_id: beyondwords_project_id, |
| 58 | control_beyondwords_content_id: beyondwords_content_id, |
| 59 | }, |
| 60 | options: { |
| 61 | external: true, |
| 62 | }, |
| 63 | } ); |
| 64 | |
| 65 | // @todo Replace jQuery |
| 66 | jQuery( '#beyondwords-elementor-editor-player' ).attr( |
| 67 | 'data-beyondwords-project-id', |
| 68 | beyondwords_project_id |
| 69 | ); |
| 70 | |
| 71 | // @todo Replace jQuery |
| 72 | jQuery( '#beyondwords-elementor-editor-player' ).attr( |
| 73 | 'data-beyondwords-content-id', |
| 74 | beyondwords_content_id |
| 75 | ); |
| 76 | |
| 77 | setTimeout( function () { |
| 78 | window.beyondwordsElementorCompatibility.initPlayer(); |
| 79 | }, 250 ); |
| 80 | |
| 81 | return { |
| 82 | data, |
| 83 | }; |
| 84 | } |
| 85 | |
| 86 | onRequestError() { |
| 87 | elementor.notifications.showToast( { |
| 88 | message: |
| 89 | 'Unable to retrieve BeyondWords data. Please refresh the page to see the player.', |
| 90 | } ); |
| 91 | } |
| 92 | } |
| 93 |