|
| 1 | +/* global customElements, HTMLElement, prplL10n, progressPlanner, progressPlannerAjaxRequest, prplSuggestedTask */ |
| 2 | +/* |
| 3 | + * Install Plugin |
| 4 | + * |
| 5 | + * A web component to install a plugin. |
| 6 | + * |
| 7 | + * Dependencies: progress-planner/l10n, progress-planner/ajax-request, progress-planner/suggested-task |
| 8 | + */ |
| 9 | + |
| 10 | +/** |
| 11 | + * Register the custom web component. |
| 12 | + */ |
| 13 | +customElements.define( |
| 14 | + 'prpl-install-plugin', |
| 15 | + class extends HTMLElement { |
| 16 | + constructor( |
| 17 | + pluginSlug, |
| 18 | + pluginName, |
| 19 | + action, |
| 20 | + providerId, |
| 21 | + className = 'prpl-button-link' |
| 22 | + ) { |
| 23 | + // Get parent class properties |
| 24 | + super(); |
| 25 | + |
| 26 | + this.pluginSlug = |
| 27 | + pluginSlug ?? this.getAttribute( 'data-plugin-slug' ); |
| 28 | + this.pluginName = |
| 29 | + pluginName ?? this.getAttribute( 'data-plugin-name' ); |
| 30 | + this.pluginName = this.pluginName ?? this.pluginSlug; |
| 31 | + this.action = action ?? this.getAttribute( 'data-action' ); |
| 32 | + this.providerId = |
| 33 | + providerId ?? this.getAttribute( 'data-provider-id' ); |
| 34 | + this.className = className ?? this.getAttribute( 'class' ); |
| 35 | + // If the plugin slug is empty, bail out. |
| 36 | + if ( ! this.pluginSlug ) { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + // Set the inner HTML. |
| 41 | + this.innerHTML = ` |
| 42 | + <button type="button" class="${ this.className }"> |
| 43 | + ${ prplL10n( |
| 44 | + 'install' === this.action |
| 45 | + ? 'installPlugin' |
| 46 | + : 'activatePlugin' |
| 47 | + ).replace( '%s', this.pluginName ) } |
| 48 | + </button> |
| 49 | + `; |
| 50 | + |
| 51 | + // Handle the click event. |
| 52 | + this.handleClick(); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Handle the click event. |
| 57 | + */ |
| 58 | + handleClick() { |
| 59 | + const button = this.querySelector( 'button' ); |
| 60 | + if ( ! button ) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + button.addEventListener( 'click', () => { |
| 65 | + button.disabled = true; |
| 66 | + if ( 'install' === this.action ) { |
| 67 | + this.installPlugin(); |
| 68 | + } else { |
| 69 | + this.activatePlugin(); |
| 70 | + } |
| 71 | + } ); |
| 72 | + } |
| 73 | + |
| 74 | + installPlugin() { |
| 75 | + const button = this.querySelector( 'button' ); |
| 76 | + const thisObj = this; |
| 77 | + |
| 78 | + button.innerHTML = ` |
| 79 | + <span class="prpl-install-button-loader"></span> |
| 80 | + ${ prplL10n( 'installing' ) } |
| 81 | + `; |
| 82 | + |
| 83 | + progressPlannerAjaxRequest( { |
| 84 | + url: progressPlanner.ajaxUrl, |
| 85 | + data: { |
| 86 | + action: 'progress_planner_install_plugin', |
| 87 | + plugin_slug: this.pluginSlug, |
| 88 | + plugin_name: this.pluginName, |
| 89 | + nonce: progressPlanner.nonce, |
| 90 | + }, |
| 91 | + } ) |
| 92 | + .then( () => thisObj.activatePlugin() ) |
| 93 | + .catch( ( error ) => console.error( error ) ); |
| 94 | + } |
| 95 | + |
| 96 | + activatePlugin() { |
| 97 | + const button = this.querySelector( 'button' ); |
| 98 | + const thisObj = this; |
| 99 | + button.innerHTML = ` |
| 100 | + <span class="prpl-install-button-loader"></span> |
| 101 | + ${ prplL10n( 'activating' ) } |
| 102 | + `; |
| 103 | + |
| 104 | + progressPlannerAjaxRequest( { |
| 105 | + url: progressPlanner.ajaxUrl, |
| 106 | + data: { |
| 107 | + action: 'progress_planner_activate_plugin', |
| 108 | + plugin_slug: thisObj.pluginSlug, |
| 109 | + plugin_name: thisObj.pluginName, |
| 110 | + nonce: progressPlanner.nonce, |
| 111 | + }, |
| 112 | + } ) |
| 113 | + .then( () => { |
| 114 | + button.innerHTML = prplL10n( 'activated' ); |
| 115 | + thisObj.completeTask(); |
| 116 | + } ) |
| 117 | + .catch( ( error ) => console.error( error ) ); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Complete the task. |
| 122 | + */ |
| 123 | + completeTask() { |
| 124 | + const tasks = document.querySelectorAll( |
| 125 | + '#prpl-suggested-tasks-list .prpl-suggested-task' |
| 126 | + ); |
| 127 | + const thisObj = this; |
| 128 | + |
| 129 | + tasks.forEach( ( taskElement ) => { |
| 130 | + if ( taskElement.dataset.taskId === thisObj.providerId ) { |
| 131 | + // Close popover. |
| 132 | + document |
| 133 | + .getElementById( 'prpl-popover-' + thisObj.providerId ) |
| 134 | + .hidePopover(); |
| 135 | + |
| 136 | + const postId = parseInt( taskElement.dataset.postId ); |
| 137 | + |
| 138 | + if ( postId ) { |
| 139 | + prplSuggestedTask.maybeComplete( postId ); |
| 140 | + } |
| 141 | + } |
| 142 | + } ); |
| 143 | + } |
| 144 | + } |
| 145 | +); |
0 commit comments