PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.2.1
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.2.1
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 / learnpress.js

learnpress.js in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.2.1, at assets/src/js/admin/learnpress.js

912 lines 24.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 const $ = jQuery;
2 const $doc = $( document );
3 const $win = $( window );
4
5 const makePaymentsSortable = function makePaymentsSortable() {
6 // Make payments sortable
7 $( '.learn-press-payments.sortable tbody' ).sortable( {
8 handle: '.dashicons-menu',
9 helper( e, ui ) {
10 ui.children().each( function() {
11 $( this ).width( $( this ).width() );
12 } );
13 return ui;
14 },
15 axis: 'y',
16 start( event, ui ) {
17
18 },
19 stop( event, ui ) {
20
21 },
22 update( event, ui ) {
23 const order = $( this ).children().map( function() {
24 return $( this ).find( 'input[name="payment-order"]' ).val();
25 } ).get();
26
27 $.post( {
28 url: '',
29 data: {
30 'lp-ajax': 'update-payment-order',
31 order,
32 },
33 success( response ) {
34 },
35 } );
36 },
37 } );
38 };
39
40 /** Start Nhamdv code */
41
42 const lpMetaboxCustomFields = () => {
43 $( '.lp-metabox__custom-fields' ).on( 'click', '.lp-metabox-custom-field-button', function() {
44 const row = $( this ).data( 'row' ).replace( /lp_metabox_custom_fields_key/gi, Math.floor( Math.random() * 1000 ) + 1 );
45
46 $( this ).closest( 'table' ).find( 'tbody' ).append( row );
47 updateSort( $( this ).closest( '.lp-metabox__custom-fields' ) );
48 return false;
49 } );
50
51 $( '.lp-metabox__custom-fields' ).on( 'click', 'a.delete', function() {
52 $( this ).closest( 'tr' ).remove();
53 updateSort( $( this ).closest( '.lp-metabox__custom-fields' ) );
54 return false;
55 } );
56
57 $( '.lp-metabox__custom-fields tbody' ).sortable( {
58 items: 'tr',
59 cursor: 'move',
60 axis: 'y',
61 handle: 'td.sort',
62 scrollSensitivity: 40,
63 forcePlaceholderSize: true,
64 helper: 'clone',
65 opacity: 0.65,
66 update( event, ui ) {
67 updateSort( $( this ).closest( '.lp-metabox__custom-fields' ) );
68 },
69 } );
70
71 const updateSort = ( element ) => {
72 const items = element.find( 'tbody tr' );
73
74 items.each( function( i, item ) {
75 $( this ).find( '.sort .count' ).val( i );
76 } );
77 };
78 };
79
80 const lpMetaboxRepeaterField = () => {
81 const updateSort = ( element ) => {
82 const items = element.find( '.lp_repeater_meta_box__field' );
83
84 items.each( function( i, item ) {
85 $( this ).find( '.lp_repeater_meta_box__field__count' ).val( i );
86 $( this ).find( '.lp_repeater_meta_box__title__title > span' ).text( i + 1 );
87 } );
88 };
89
90 $( '.lp_repeater_meta_box__add' ).on( 'click', function() {
91 const row = $( this ).data( 'add' ).replace( /lp_metabox_repeater_key/gi, Math.floor( Math.random() * 1000 ) + 1 );
92 $( this ).closest( '.lp_repeater_meta_box__wrapper' ).find( '.lp_repeater_meta_box__fields' ).append( row );
93
94 updateSort( $( this ).closest( '.lp_repeater_meta_box__wrapper' ) );
95 $( this ).closest( '.lp_repeater_meta_box__wrapper' ).find( '.lp_repeater_meta_box__fields' ).last().find( 'input' ).trigger( 'focus' );
96
97 return false;
98 } );
99
100 $( '.lp_repeater_meta_box__wrapper' ).on( 'click', 'a.lp_repeater_meta_box__title__delete', function() {
101 $( this ).closest( '.lp_repeater_meta_box__field' ).remove();
102
103 updateSort( $( this ).closest( '.lp_repeater_meta_box__wrapper' ) );
104
105 return false;
106 } );
107
108 $( '.lp_repeater_meta_box__fields' ).on( 'click', '.lp_repeater_meta_box__title__toggle, .lp_repeater_meta_box__title__title', function() {
109 const field = $( this ).closest( '.lp_repeater_meta_box__field' );
110
111 if ( field.hasClass( 'lp_repeater_meta_box__field_active' ) ) {
112 field.removeClass( 'lp_repeater_meta_box__field_active' );
113 } else {
114 field.addClass( 'lp_repeater_meta_box__field_active' );
115 }
116
117 return false;
118 } );
119
120 $( '.lp_repeater_meta_box__fields' ).sortable( {
121 items: '.lp_repeater_meta_box__field',
122 cursor: 'grab',
123 axis: 'y',
124 handle: '.lp_repeater_meta_box__title__sort',
125 scrollSensitivity: 40,
126 forcePlaceholderSize: true,
127 helper: 'clone',
128 opacity: 0.65,
129 update( event, ui ) {
130 updateSort( $( this ).closest( '.lp_repeater_meta_box__wrapper' ) );
131 },
132 } );
133 };
134
135 const lpMetaboxExtraInfo = () => {
136 $( '.lp_course_extra_meta_box__add' ).on( 'click', function() {
137 $( this ).closest( '.lp_course_extra_meta_box__content' ).find( '.lp_course_extra_meta_box__fields' ).append( $( this ).data( 'add' ) );
138 $( this ).closest( '.lp_course_extra_meta_box__content' ).find( '.lp_course_extra_meta_box__field' ).last().find( 'input' ).trigger( 'focus' );
139
140 return false;
141 } );
142
143 document.querySelectorAll( '.lp_course_extra_meta_box__fields' ).forEach( ( ele ) => {
144 ele.addEventListener( 'keydown', ( e ) => {
145 const inputs = ele.querySelectorAll( '.lp_course_extra_meta_box__input' );
146
147 if ( e.keyCode === 13 ) {
148 e.preventDefault();
149 inputs.forEach( ( input ) => {
150 input.blur();
151 } );
152 return false;
153 }
154 } );
155 } );
156
157 $( '.lp_course_extra_meta_box__fields' ).on( 'click', 'a.delete', function() {
158 $( this ).closest( '.lp_course_extra_meta_box__field' ).remove();
159
160 return false;
161 } );
162
163 $( '.lp_course_extra_meta_box__fields' ).sortable( {
164 items: '.lp_course_extra_meta_box__field',
165 cursor: 'grab',
166 axis: 'y',
167 handle: '.sort',
168 scrollSensitivity: 40,
169 forcePlaceholderSize: true,
170 helper: 'clone',
171 opacity: 0.65,
172 } );
173
174 // FAQs metabox.
175 $( '.lp_course_faq_meta_box__add' ).on( 'click', function() {
176 $( this ).closest( '.lp_course_faq_meta_box__content' ).find( '.lp_course_faq_meta_box__fields' ).append( $( this ).data( 'add' ) );
177
178 return false;
179 } );
180
181 document.querySelectorAll( '.lp_course_faq_meta_box__fields' ).forEach( ( ele ) => {
182 ele.addEventListener( 'keydown', ( e ) => {
183 const inputs = ele.querySelectorAll( '.lp_course_faq_meta_box__field input' );
184 const textareas = ele.querySelectorAll( '.lp_course_faq_meta_box__field textarea' );
185
186 if ( e.keyCode === 13 ) {
187 e.preventDefault();
188 [ ...inputs, ...textareas ].forEach( ( input ) => {
189 input.blur();
190 } );
191 return false;
192 }
193 } );
194 } );
195
196 $( '.lp_course_faq_meta_box__fields' ).on( 'click', 'a.delete', function() {
197 $( this ).closest( '.lp_course_faq_meta_box__field' ).remove();
198
199 return false;
200 } );
201
202 $( '.lp_course_faq_meta_box__fields' ).sortable( {
203 items: '.lp_course_faq_meta_box__field',
204 cursor: 'grab',
205 axis: 'y',
206 handle: '.sort',
207 scrollSensitivity: 40,
208 forcePlaceholderSize: true,
209 helper: 'clone',
210 opacity: 0.65,
211 } );
212 };
213
214 // Nhamdv.
215 const lpGetFinalQuiz = () => {
216 const btns = document.querySelectorAll( '.lp-metabox-get-final-quiz' );
217
218 [ ...btns ].map( ( btn ) => {
219 btn.addEventListener( 'click', ( e ) => {
220 e.preventDefault();
221
222 const text = btn.textContent,
223 loading = btn.dataset.loading,
224 message = document.querySelector( '.lp-metabox-evaluate-final_quiz' );
225
226 if ( message ) {
227 message.remove();
228 }
229
230 btn.textContent = loading;
231
232 getResponse( btn )
233 .then( ( data ) => {
234 const { message, data: responseData } = data;
235
236 btn.textContent = text;
237
238 const newNode = document.createElement( 'div' );
239 newNode.className = 'lp-metabox-evaluate-final_quiz';
240 newNode.innerHTML = responseData || message;
241
242 btn.parentNode.insertBefore( newNode, btn.nextSibling );
243 } );
244 } );
245 } );
246
247 const getResponse = async ( btn ) => {
248 const response = await wp.apiFetch( {
249 path: 'lp/v1/admin/course/get_final_quiz',
250 method: 'POST',
251 data: {
252 courseId: btn.dataset.postid || '',
253 },
254 } );
255
256 return response;
257 };
258 };
259
260 const lpMetaboxColorPicker = () => {
261 $( '.lp-metabox__colorpick' )
262 .iris( {
263 change( event, ui ) {
264 $( this ).parent().find( '.colorpickpreview' ).css( { backgroundColor: ui.color.toString() } );
265 },
266 hide: true,
267 border: true,
268 } )
269
270 .on( 'click focus', function( event ) {
271 event.stopPropagation();
272 $( '.iris-picker' ).hide();
273 $( this ).closest( 'td' ).find( '.iris-picker' ).show();
274 $( this ).data( 'original-value', $( this ).val() );
275 } )
276
277 .on( 'change', function() {
278 if ( $( this ).is( '.iris-error' ) ) {
279 const originalValue = $( this ).data( 'original-value' );
280
281 if ( originalValue.match( /^\#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/ ) ) {
282 $( this ).val( $( this ).data( 'original-value' ) ).trigger( 'change' );
283 } else {
284 $( this ).val( '' ).trigger( 'change' );
285 }
286 }
287 } );
288
289 $( 'body' ).on( 'click', function() {
290 $( '.iris-picker' ).hide();
291 } );
292 };
293
294 const lpMetaboxImage = () => {
295 $( '.lp-metabox-field__image' ).each( ( i, ele ) => {
296 let lpImageFrame;
297
298 const addImage = $( ele ).find( '.lp-metabox-field__image--add' );
299 const delImage = $( ele ).find( '.lp-metabox-field__image--delete' );
300
301 const image = $( ele ).find( '.lp-metabox-field__image--image' );
302 const inputVal = $( ele ).find( '.lp-metabox-field__image--id' );
303
304 if ( ! inputVal.val() ) {
305 addImage.show();
306 delImage.hide();
307 } else {
308 addImage.hide();
309 delImage.show();
310 }
311
312 addImage.on( 'click', ( event ) => {
313 event.preventDefault();
314
315 if ( lpImageFrame ) {
316 lpImageFrame.open();
317 return;
318 }
319
320 lpImageFrame = wp.media( {
321 title: addImage.data( 'choose' ),
322 button: {
323 text: addImage.data( 'update' ),
324 },
325 multiple: false,
326 } );
327
328 lpImageFrame.on( 'select', function() {
329 const attachment = lpImageFrame.state().get( 'selection' ).first().toJSON();
330 const attachmentImage = attachment.sizes && attachment.sizes.thumbnail ? attachment.sizes.thumbnail.url : attachment.url;
331
332 image.append( '<div class="lp-metabox-field__image--inner"><img src="' + attachmentImage + '" alt="" style="max-width:100%;"/></div>' );
333
334 inputVal.val( attachment.id );
335
336 addImage.hide();
337
338 delImage.show();
339 } );
340
341 lpImageFrame.open();
342 } );
343
344 delImage.on( 'click', ( event ) => {
345 event.preventDefault();
346
347 image.html( '' );
348
349 addImage.show();
350
351 delImage.hide();
352
353 inputVal.val( '' );
354 } );
355 } );
356 };
357
358 const lpMetaboxImageAdvanced = () => {
359 $( '.lp-metabox-field__image-advanced' ).each( ( i, element ) => {
360 let lpImageFrame;
361
362 const imageGalleryIds = $( element ).find( '#lp-gallery-images-ids' );
363 const listImages = $( element ).find( '.lp-metabox-field__image-advanced-images' );
364 const btnUpload = $( element ).find( '.lp-metabox-field__image-advanced-upload > a' );
365
366 $( btnUpload ).on( 'click', ( event ) => {
367 event.preventDefault();
368
369 if ( lpImageFrame ) {
370 lpImageFrame.open();
371 return;
372 }
373
374 lpImageFrame = wp.media( {
375 title: btnUpload.data( 'choose' ),
376 button: {
377 text: btnUpload.data( 'update' ),
378 },
379 states: [
380 new wp.media.controller.Library( {
381 title: btnUpload.data( 'choose' ),
382 filterable: 'all',
383 multiple: true,
384 } ),
385 ],
386 } );
387
388 lpImageFrame.on( 'select', function() {
389 const selection = lpImageFrame.state().get( 'selection' );
390 let attachmentIds = imageGalleryIds.val();
391
392 selection.forEach( function( attachment ) {
393 attachment = attachment.toJSON();
394
395 if ( attachment.id ) {
396 attachmentIds = attachmentIds ? attachmentIds + ',' + attachment.id : attachment.id;
397 const attachmentImage = attachment.sizes && attachment.sizes.thumbnail ? attachment.sizes.thumbnail.url : attachment.url;
398
399 listImages.append(
400 '<li class="image" data-attachment_id="' + attachment.id + '"><img src="' + attachmentImage +
401 '" /><ul class="actions"><li><a href="#" class="delete" title="' + btnUpload.data( 'delete' ) + '">' +
402 btnUpload.data( 'text' ) + '</a></li></ul></li>'
403 );
404 }
405 } );
406
407 imageGalleryIds.val( attachmentIds );
408 } );
409
410 lpImageFrame.open();
411 } );
412
413 listImages.sortable( {
414 items: 'li.image',
415 cursor: 'move',
416 scrollSensitivity: 40,
417 forcePlaceholderSize: true,
418 forceHelperSize: false,
419 helper: 'clone',
420 opacity: 0.65,
421 placeholder: 'lp-metabox-sortable-placeholder',
422 start( event, ui ) {
423 ui.item.css( 'background-color', '#f6f6f6' );
424 },
425 stop( event, ui ) {
426 ui.item.removeAttr( 'style' );
427 },
428 update() {
429 let attachmentIds = '';
430
431 listImages.find( 'li.image' ).css( 'cursor', 'default' ).each( function() {
432 const attachmentId = $( this ).attr( 'data-attachment_id' );
433 attachmentIds = attachmentIds + attachmentId + ',';
434 } );
435
436 imageGalleryIds.val( attachmentIds );
437 },
438 } );
439
440 $( listImages ).find( 'li.image' ).each( ( i, ele ) => {
441 const del = $( ele ).find( 'a.delete' );
442
443 del.on( 'click', () => {
444 $( ele ).remove();
445
446 let attachmentIds = '';
447
448 $( listImages ).find( 'li.image' ).css( 'cursor', 'default' ).each( function() {
449 const attachmentId = $( this ).attr( 'data-attachment_id' );
450 attachmentIds = attachmentIds + attachmentId + ',';
451 } );
452
453 imageGalleryIds.val( attachmentIds );
454
455 return false;
456 } );
457 } );
458 } );
459 };
460
461 const lpMetaboxCourseTabs = () => {
462 $( document.body ).on( 'lp-metabox-course-tab-panels', function() {
463 $( 'ul.lp-meta-box__course-tab__tabs' ).show();
464
465 $( 'ul.lp-meta-box__course-tab__tabs a' ).on( 'click', function( e ) {
466 e.preventDefault();
467
468 const panelWrap = $( this ).closest( 'div.lp-meta-box__course-tab' );
469
470 $( 'ul.lp-meta-box__course-tab__tabs li', panelWrap ).removeClass( 'active' );
471
472 $( this ).parent().addClass( 'active' );
473
474 $( 'div.lp-meta-box-course-panels', panelWrap ).hide();
475
476 $( $( this ).attr( 'href' ) ).show();
477 } );
478
479 $( 'div.lp-meta-box__course-tab' ).each( function() {
480 $( this ).find( 'ul.lp-meta-box__course-tab__tabs li' ).eq( 0 ).find( 'a' ).trigger( 'click' );
481 } );
482 } ).trigger( 'lp-metabox-course-tab-panels' );
483 };
484
485 // use to show and hide field condition logic metabox.
486 const lpMetaboxCondition = () => {
487 const fields = document.querySelectorAll( '.lp-meta-box .form-field' );
488
489 fields.forEach( ( field ) => {
490 if ( field.hasAttribute( 'data-show' ) && field.dataset.show ) {
491 lpMetaboxConditionType( field, field.dataset.show, 'show' );
492 } else if ( field.hasAttribute( 'data-hide' ) && field.dataset.hide ) {
493 lpMetaboxConditionType( field, field.dataset.hide, 'hide' );
494 }
495 } );
496 };
497
498 const lpMetaboxConditionType = ( field, conditions, typeCondition = 'show' ) => {
499 const condition = JSON.parse( conditions ),
500 eles = document.querySelectorAll( `input[id^="${ condition[ 0 ] }"]` ),
501 logic = condition[ 1 ] === '=' ? '=' : '!=',
502 dataLogic = condition[ 2 ];
503
504 const switchCase = ( type, ele, target ) => {
505 switch ( type ) {
506 case 'checkbox':
507 let val = dataLogic;
508
509 if ( dataLogic === 'yes' || dataLogic === '1' || dataLogic === 1 || dataLogic === 'true' ) {
510 val = true;
511 } else if ( dataLogic === 'no' || dataLogic === '0' || dataLogic === 0 || dataLogic === 'false' ) {
512 val = false;
513 }
514
515 if ( logic == '!=' && val !== Boolean( target ? target.checked : ele.checked ) ) {
516 field.style.display = typeCondition === 'show' ? '' : 'none';
517 } else if ( logic == '=' && val == Boolean( target ? target.checked : ele.checked ) ) {
518 field.style.display = typeCondition === 'show' ? '' : 'none';
519 } else {
520 field.style.display = typeCondition === 'show' ? 'none' : '';
521 }
522 break;
523 }
524 };
525
526 eles.forEach( ( ele ) => {
527 const type = ele.getAttribute( 'type' );
528
529 switchCase( type, ele );
530
531 ele.addEventListener( 'change', ( e ) => {
532 const target = e.target;
533
534 switchCase( type, ele, target );
535 } );
536 } );
537 };
538
539 /** End Nhamdv code */
540
541 const initTooltips = function initTooltips() {
542 $( '.learn-press-tooltip' ).each( function() {
543 const $el = $( this ),
544 args = $.extend( { title: 'data-tooltip', offset: 10, gravity: 's' }, $el.data() );
545 $el.tipsy( args );
546 } );
547 };
548
549 const initSelect2 = function initSelect2() {
550 if ( $.fn.select2 ) {
551 const elSelect2 = $( '.lp-select-2 select' );
552 elSelect2.select2( {
553 placeholder: 'Select a value',
554 } );
555 elSelect2.on( 'change.select2', function( e ) {
556 const el = $( e.target );
557 const val = el.val();
558
559 if ( ! val.length ) {
560 el.val( null );
561 }
562 } );
563
564 $( '.lp_autocomplete_metabox_field' ).each( function() {
565 const dataAtts = $( this ).data( 'atts' );
566 let action = dataAtts.action;
567
568 if ( ! action ) {
569 switch ( dataAtts.data ) {
570 case 'users':
571 action = dataAtts.rest_url + 'wp/v2/users';
572 break;
573 default:
574 action = dataAtts.rest_url + 'wp/v2/' + dataAtts.data;
575 break;
576 }
577 }
578
579 $( this ).find( 'select' ).select2( {
580 placeholder: dataAtts.placeholder ? dataAtts.placeholder : 'Select',
581 ajax: {
582 url: action,
583 dataType: 'json',
584 delay: 250,
585 beforeSend( xhr ) {
586 xhr.setRequestHeader( 'X-WP-Nonce', dataAtts.nonce );
587 },
588 data( params ) {
589 return {
590 search: params.term,
591 };
592 },
593 processResults( data ) {
594 return {
595 results: data.map( ( item ) => {
596 return {
597 id: item.id,
598 text: item.title && item.title.rendered ? item.title.rendered : item.name,
599 };
600 } ),
601 };
602 },
603 cache: true,
604 },
605 minimumInputLength: 2,
606 } );
607 } );
608 }
609 };
610
611 const initSingleCoursePermalink = function initSingleCoursePermalink() {
612 $doc
613 .on( 'change', '.learn-press-single-course-permalink input[type="radio"]', function() {
614 const $check = $( this ),
615 $row = $check.closest( '.learn-press-single-course-permalink' );
616 if ( $row.hasClass( 'custom-base' ) ) {
617 $row.find( 'input[type="text"]' ).prop( 'readonly', false );
618 } else {
619 $row.siblings( '.custom-base' ).find( 'input[type="text"]' ).prop( 'readonly', true );
620 }
621 } )
622 .on( 'change', 'input.learn-press-course-base', function() {
623 $( '#course_permalink_structure' ).val( $( this ).val() );
624 } )
625 .on( 'focus', '#course_permalink_structure', function() {
626 $( '#learn_press_custom_permalink' ).click();
627 } )
628 .on( 'change', '#learn_press_courses_page_id', function() {
629 $( 'tr.learn-press-courses-page-id' ).toggleClass( 'hide-if-js', ! parseInt( this.value ) );
630 } );
631 };
632
633 const togglePaymentStatus = function togglePaymentStatus( e ) {
634 e.preventDefault();
635 const $row = $( this ).closest( 'tr' ),
636 $button = $( this ),
637 status = $row.find( '.status' ).hasClass( 'enabled' ) ? 'no' : 'yes';
638
639 $.ajax( {
640 url: '',
641 data: {
642 'lp-ajax': 'update-payment-status',
643 status,
644 id: $row.data( 'payment' ),
645 },
646 success( response ) {
647 response = LP.parseJSON( response );
648 for ( const i in response ) {
649 $( '#payment-' + i + ' .status' ).toggleClass( 'enabled', response[ i ] );
650 }
651 },
652 } );
653 };
654
655 const updateEmailStatus = function updateEmailStatus() {
656 ( function() {
657 $.post( {
658 url: window.location.href,
659 data: {
660 'lp-ajax': 'update_email_status',
661 status: $( this ).parent().hasClass( 'enabled' ) ? 'no' : 'yes',
662 id: $( this ).data( 'id' ),
663 },
664 dataType: 'text',
665 success: $.proxy( function( res ) {
666 res = LP.parseJSON( res );
667 for ( const i in res ) {
668 $( '#email-' + i + ' .status' ).toggleClass( 'enabled', res[ i ] );
669 }
670 }, this ),
671 } );
672 } ).apply( this );
673 };
674
675 const lpMetaboxsalePriceDate = () => {
676 // Don't run in LearnPress Frontend Editor Add-on.
677 if ( ! $( '#course-settings' ).length ) {
678 return;
679 }
680
681 $( '.lp_sale_dates_fields' ).each( function() {
682 const $this = $( this );
683 const $wrap = $this.closest( 'div.lp-meta-box-course-panels' );
684 let saleScheduleSet = false;
685
686 $this.find( 'input' ).each( function() {
687 if ( '' !== $( this ).val() ) {
688 saleScheduleSet = true;
689 }
690 } );
691
692 if ( saleScheduleSet ) {
693 $wrap.find( '.lp_sale_price_schedule' ).hide();
694 $wrap.find( '.lp_sale_dates_fields' ).show();
695 } else {
696 $wrap.find( '.lp_sale_price_schedule' ).show();
697 $wrap.find( '.lp_sale_dates_fields' ).hide();
698 }
699 } );
700
701 $( '.lp-meta-box-course-panels' ).on( 'click', '.lp_sale_price_schedule', function() {
702 const wrap = $( this ).closest( 'div.lp-meta-box-course-panels' );
703
704 $( this ).hide();
705
706 wrap.find( '.lp_cancel_sale_schedule' ).show();
707 wrap.find( '.lp_sale_dates_fields' ).show();
708
709 return false;
710 } );
711
712 $( '.lp-meta-box-course-panels' ).on( 'click', '.lp_cancel_sale_schedule', function() {
713 const wrap = $( this ).closest( 'div.lp-meta-box-course-panels' );
714
715 $( this ).hide();
716
717 wrap.find( '.lp_sale_price_schedule' ).show();
718 wrap.find( '.lp_sale_dates_fields' ).hide();
719 wrap.find( '.lp_sale_dates_fields' ).find( 'input' ).val( '' );
720
721 return false;
722 } );
723
724 $( document ).on( 'input', '#price_course_data', function( e ) {
725 const $this = $( this ),
726 regularPrice = $( '.lp_meta_box_regular_price' ),
727 salePrice = $( '.lp_meta_box_sale_price' ),
728 $target = $( e.target ).attr( 'id' );
729
730 $this.find( '.learn-press-tip-floating' ).remove();
731
732 if ( parseInt( salePrice.val() ) > parseInt( regularPrice.val() ) ) {
733 if ( $target === '_lp_price' ) {
734 regularPrice.parent( '.form-field' ).append( '<div class="learn-press-tip-floating">' + lpAdminCourseEditorSettings.i18n.notice_price + '</div>' );
735 } else if ( $target === '_lp_sale_price' ) {
736 salePrice.parent( '.form-field' ).append( '<div class="learn-press-tip-floating">' + lpAdminCourseEditorSettings.i18n.notice_sale_price + '</div>' );
737 }
738 }
739 } );
740
741 const datePickerSelect = function( datepicker ) {
742 const option = $( datepicker ).is( '#_lp_sale_start' ) ? 'minDate' : 'maxDate',
743 otherDateField = 'minDate' === option ? $( '#_lp_sale_end' ) : $( '#_lp_sale_start' ),
744 date = $( datepicker ).datetimepicker( 'getDate' );
745
746 $( otherDateField ).datetimepicker( 'option', option, date );
747 $( datepicker ).trigger( 'change' );
748 };
749
750 $( '.lp_sale_dates_fields' ).each( function() {
751 $( this ).find( 'input' ).datetimepicker( {
752 timeFormat: 'HH:mm',
753 separator: ' ',
754 dateFormat: 'yy-mm-dd',
755 showButtonPanel: true,
756 onSelect() {
757 datePickerSelect( $( this ) );
758 },
759 } );
760
761 $( this ).find( 'input' ).each( function() {
762 datePickerSelect( $( this ) );
763 } );
764 } );
765 };
766
767 const lpHidePassingGrade = () => {
768 const listHides = [ 'evaluate_final_quiz', 'evaluate_final_assignment' ];
769 const inputLists = document.querySelectorAll( 'input[type=radio][name=_lp_course_result]' );
770
771 [ ...inputLists ].map( ( ele, i ) => {
772 if ( ele.checked && listHides.includes( ele.value ) ) {
773 $( '._lp_passing_condition_field' ).hide();
774 }
775
776 return null;
777 } );
778
779 $( 'input[type=radio][name=_lp_course_result]' ).on( 'change', function( e ) {
780 if ( listHides.includes( e.target.value ) ) {
781 $( '._lp_passing_condition_field' ).hide();
782 } else {
783 $( '._lp_passing_condition_field' ).show();
784 }
785 } );
786 };
787
788 const callbackFilterTemplates = function callbackFilterTemplates() {
789 const $link = $( this );
790
791 if ( $link.hasClass( 'current' ) ) {
792 return false;
793 }
794
795 const $templatesList = $( '#learn-press-template-files' ),
796 $templates = $templatesList.find( 'tr[data-template]' ),
797 template = $link.data( 'template' ),
798 filter = $link.data( 'filter' );
799
800 $link.addClass( 'current' ).siblings( 'a' ).removeClass( 'current' );
801
802 if ( ! template ) {
803 if ( ! filter ) {
804 $templates.removeClass( 'hide-if-js' );
805 } else {
806 $templates.map( function() {
807 $( this ).toggleClass( 'hide-if-js', $( this ).data( 'filter-' + filter ) !== 'yes' );
808 } );
809 }
810 } else {
811 $templates.map( function() {
812 $( this ).toggleClass( 'hide-if-js', $( this ).data( 'template' ) !== template );
813 } );
814 }
815
816 $( '#learn-press-no-templates' ).toggleClass( 'hide-if-js', !! $templatesList.find( 'tr.template-row:not(.hide-if-js):first' ).length );
817
818 return false;
819 };
820
821 const toggleEmails = function toggleEmails( e ) {
822 e.preventDefault();
823 const $button = $( this ),
824 status = $button.data( 'status' );
825
826 $.ajax( {
827 url: '',
828 data: {
829 'lp-ajax': 'update_email_status',
830 status,
831 },
832 success( response ) {
833 response = LP.parseJSON( response );
834 for ( const i in response ) {
835 $( '#email-' + i + ' .status' ).toggleClass( 'enabled', response[ i ] );
836 }
837 },
838 } );
839 };
840
841 const importCourses = function importCourses() {
842 const $container = $( '#learn-press-install-sample-data-notice' ),
843 action = $( this ).attr( 'data-action' );
844 if ( ! action ) {
845 return;
846 }
847 e.preventDefault();
848
849 if ( action === 'yes' ) {
850 $container
851 .find( '.install-sample-data-notice' ).slideUp()
852 .siblings( '.install-sample-data-loading' ).slideDown();
853 } else {
854 $container.fadeOut();
855 }
856 $.ajax( {
857 url: ajaxurl,
858 dataType: 'html',
859 type: 'post',
860 data: {
861 action: 'learnpress_install_sample_data',
862 yes: action,
863 },
864 success( response ) {
865 response = LP.parseJSON( response );
866 if ( response.url ) {
867 $.ajax( {
868 url: response.url,
869 success() {
870 $container
871 .find( '.install-sample-data-notice' ).html( response.message ).slideDown()
872 .siblings( '.install-sample-data-loading' ).slideUp();
873 },
874 } );
875 } else {
876 $container
877 .find( '.install-sample-data-notice' ).html( response.message ).slideDown()
878 .siblings( '.install-sample-data-loading' ).slideUp();
879 }
880 },
881 } );
882 };
883
884 const onReady = function onReady() {
885 makePaymentsSortable();
886 initSelect2();
887 initTooltips();
888 initSingleCoursePermalink();
889
890 // lp Metabox in LP4.
891 lpMetaboxCourseTabs();
892 lpMetaboxCustomFields();
893 lpMetaboxColorPicker();
894 lpMetaboxImageAdvanced();
895 lpMetaboxImage();
896 lpMetaboxsalePriceDate();
897 lpMetaboxExtraInfo();
898 lpHidePassingGrade();
899 lpGetFinalQuiz();
900 lpMetaboxCondition();
901 lpMetaboxRepeaterField();
902
903 $( document )
904 .on( 'click', '.learn-press-payments .status .dashicons', togglePaymentStatus )
905 .on( 'click', '.change-email-status', updateEmailStatus )
906 .on( 'click', '.learn-press-filter-template', callbackFilterTemplates )
907 .on( 'click', '#learn-press-enable-emails, #learn-press-disable-emails', toggleEmails )
908 .on( 'click', '#learn-press-install-sample-data-notice a', importCourses );
909 };
910
911 $( document ).ready( onReady );
912