PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / trunk
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses vtrunk
4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 4.2.1 All 138 releases
learnpress / assets / src / js / admin / setup-wizard.js

setup-wizard.js in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses trunk, at assets/src/js/admin/setup-wizard.js

258 lines 7.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Setup Wizard Script
3 *
4 * @since 4.2.8.6
5 */
6 import * as lpUtils from 'lpAssetsJsPath/utils.js';
7
8 class SetupWizard {
9 static selectors = {
10 elSetupForm: '.lp-setup-wizard-form',
11 elPreviewPrice: '#preview-price',
12 elCurrency: 'select[name="settings[currency][currency]"]',
13 };
14
15 init() {
16 this.elSetupForm = document.querySelector( SetupWizard.selectors.elSetupForm );
17 if ( ! this.elSetupForm ) {
18 return;
19 }
20
21 this.resetNextButtons();
22 this.events();
23 this.initToggles();
24 this.syncEmailMaster();
25 this.syncCurrencyPreset();
26 this.initDemoCourseImporter();
27 }
28
29 events() {
30 window.addEventListener( 'pageshow', () => this.resetNextButtons() );
31
32 this.elSetupForm.querySelector( SetupWizard.selectors.elCurrency )?.addEventListener( 'change', () => {
33 this.syncCurrencyPreset();
34 } );
35
36 document.addEventListener( 'click', ( e ) => {
37 const nextButton = e.target.closest( '.button-next' );
38 if ( nextButton ) {
39 e.preventDefault();
40 this.saveStep( nextButton );
41 return;
42 }
43
44 const preset = e.target.closest( '[data-currency-preset]' );
45 if ( ! preset ) {
46 return;
47 }
48
49 const currency = this.elSetupForm.querySelector( SetupWizard.selectors.elCurrency );
50 if ( ! currency ) {
51 return;
52 }
53
54 currency.value = preset.dataset.currencyPreset;
55 currency.dispatchEvent( new Event( 'change', { bubbles: true } ) );
56 } );
57
58 }
59
60 resetNextButtons() {
61 this.elSetupForm.querySelectorAll( '.button-next' ).forEach( ( button ) => {
62 button.removeAttribute( 'aria-disabled' );
63 lpUtils.lpSetLoadingEl( button, 0 );
64 } );
65 }
66
67 initToggles() {
68 lpUtils.toggleEnable( ( toggle, isEnabled ) => {
69 const input = toggle.querySelector( '.lp-toggle-enable__input' );
70 if ( input?.classList.contains( 'lp-setup-email-master' ) ) {
71 this.elSetupForm.querySelectorAll( '.lp-setup-email-notification' ).forEach( ( notification ) => {
72 this.setToggleState( notification, isEnabled );
73 } );
74 } else if ( input?.classList.contains( 'lp-setup-email-notification' ) ) {
75 this.syncEmailMaster();
76 }
77 } );
78 }
79
80 setToggleState( input, isEnabled ) {
81 input.checked = isEnabled;
82 input.value = isEnabled ? '1' : '0';
83 input.closest( '.lp-toggle-enable' )?.classList.toggle( 'is-enabled', isEnabled );
84 }
85
86 initDemoCourseImporter() {
87 this.demoCourse = this.elSetupForm.querySelector( '#lp-setup-demo-course' );
88 if ( ! this.demoCourse ) {
89 return;
90 }
91
92 this.demoCourseIndex = 0;
93 this.demoCourse.querySelector( '#install-sample-course' )?.addEventListener( 'click', () => this.importDemoCourse() );
94 this.demoCourse.querySelector( '.lp-setup-demo-course__retry' )?.addEventListener( 'click', () => this.importDemoCourse() );
95 }
96
97 importDemoCourse() {
98 if ( ! this.demoCourse || ! window.lpAJAXG ) {
99 return;
100 }
101
102 this.demoCourse.dataset.state = 'importing';
103 this.demoCourse.querySelectorAll( 'button' ).forEach( ( button ) => {
104 button.disabled = true;
105 lpUtils.lpSetLoadingEl( button, 1 );
106 } );
107
108 window.lpAJAXG.fetchAJAX(
109 {
110 action: this.demoCourse.dataset.action,
111 index: this.demoCourseIndex,
112 id_url: 'setup-demo-course',
113 },
114 {
115 success: ( response ) => {
116 if ( 'success' !== response.status ) {
117 throw new Error( response.message );
118 }
119
120 this.updateDemoCourseProgress( response.data );
121 if ( response.data.complete ) {
122 this.completeDemoCourseImport( response.data );
123 } else {
124 this.demoCourseIndex = response.data.index;
125 this.importDemoCourse();
126 }
127 },
128 error: ( error ) => {
129 this.demoCourse.dataset.state = 'error';
130 const message = this.demoCourse.querySelector( '.lp-setup-demo-course__error-message' );
131 if ( message ) {
132 message.textContent = error?.message || String( error );
133 }
134 },
135 completed: () => {
136 if ( 'importing' === this.demoCourse.dataset.state ) {
137 return;
138 }
139
140 this.demoCourse.querySelectorAll( 'button' ).forEach( ( button ) => {
141 button.disabled = false;
142 lpUtils.lpSetLoadingEl( button, 0 );
143 } );
144 },
145 }
146 );
147 }
148
149 updateDemoCourseProgress( data ) {
150 const status = this.demoCourse.querySelector( '.lp-setup-demo-course__status-text' );
151 const percent = this.demoCourse.querySelector( '.lp-setup-demo-course__progress .lp-setup-demo-course__percent' );
152 const bar = this.demoCourse.querySelector( '.lp-setup-demo-course__progress .lp-setup-demo-course__bar span' );
153 if ( status ) {
154 status.textContent = `${ this.demoCourse.dataset.importingLabel } (${ data.index }/${ data.total }): ${ data.title }`;
155 }
156 if ( percent ) {
157 percent.textContent = `${ data.percent }%`;
158 }
159 if ( bar ) {
160 bar.style.width = `${ data.percent }%`;
161 }
162 }
163
164 completeDemoCourseImport( data ) {
165 this.demoCourse.dataset.state = 'complete';
166 const count = this.demoCourse.querySelector( '.lp-setup-demo-course__complete-count' );
167 if ( count ) {
168 count.textContent = data.total;
169 }
170 const viewCourses = this.demoCourse.querySelector( '.lp-setup-demo-course__complete a' );
171 if ( viewCourses && data.view_url ) {
172 viewCourses.href = data.view_url;
173 }
174 const finishButton = this.elSetupForm.querySelector( '.lp-setup-footer-bar .button-next' );
175 if ( finishButton ) {
176 const finishButtonLabel = finishButton.querySelector( '.lp-setup-button__label' );
177 if ( finishButtonLabel ) {
178 finishButtonLabel.textContent = this.demoCourse.dataset.finishLabel;
179 }
180 }
181 }
182
183 syncEmailMaster() {
184 const master = this.elSetupForm.querySelector( '.lp-setup-email-master' );
185 const notifications = [ ...this.elSetupForm.querySelectorAll( '.lp-setup-email-notification' ) ];
186 if ( ! master || ! notifications.length ) {
187 return;
188 }
189
190 const enabledCount = notifications.filter( ( input ) => input.checked ).length;
191 this.setToggleState( master, enabledCount === notifications.length );
192 }
193
194 syncCurrencyPreset() {
195 const currency = this.elSetupForm.querySelector( SetupWizard.selectors.elCurrency );
196 if ( ! currency ) {
197 return;
198 }
199
200 this.elSetupForm.querySelectorAll( '[data-currency-preset]' ).forEach( ( preset ) => {
201 preset.classList.toggle( 'is-active', preset.dataset.currencyPreset === currency.value );
202 } );
203 }
204
205 saveStep( button ) {
206 if ( ! window.lpAJAXG || 'true' === button.getAttribute( 'aria-disabled' ) ) {
207 return;
208 }
209
210 const loadingStartedAt = Date.now();
211 const minimumLoadingDuration = 200;
212 let isNavigating = false;
213
214 lpUtils.lpSetLoadingEl( button, 1 );
215 button.setAttribute( 'aria-disabled', 'true' );
216 const dataSend = lpUtils.getDataOfForm( this.elSetupForm );
217 dataSend.action = 'lp_setup_wizard_save_step';
218 dataSend.id_url = 'setup-wizard-save-step';
219
220 try {
221 window.lpAJAXG.fetchAJAX( dataSend, {
222 success: ( response ) => {
223 if ( 'success' !== response.status ) {
224 throw new Error( response.message );
225 }
226
227 isNavigating = true;
228 const loadingDuration = Date.now() - loadingStartedAt;
229 const redirectDelay = Math.max( 0, minimumLoadingDuration - loadingDuration );
230
231 window.setTimeout( () => {
232 window.location.href = button.dataset.nextUrl;
233 }, redirectDelay );
234 },
235 error: ( error ) => {
236 window.alert( error?.message || String( error ) );
237 },
238 completed: () => {
239 if ( isNavigating ) {
240 return;
241 }
242
243 button.removeAttribute( 'aria-disabled' );
244 lpUtils.lpSetLoadingEl( button, 0 );
245 },
246 } );
247 } catch ( error ) {
248 button.removeAttribute( 'aria-disabled' );
249 lpUtils.lpSetLoadingEl( button, 0 );
250 window.alert( error?.message || String( error ) );
251 }
252 }
253 }
254
255 document.addEventListener( 'DOMContentLoaded', () => {
256 new SetupWizard().init();
257 } );
258