PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7
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.1.7, at assets/src/js/admin/learnpress.js

916 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 if ( ! lpGlobalSettings.root ) {
249 return;
250 }
251
252 const response = await wp.apiFetch( {
253 path: 'lp/v1/admin/course/get_final_quiz',
254 method: 'POST',
255 data: {
256 courseId: btn.dataset.postid || '',
257 },
258 } );
259
260 return response;
261 };
262 };
263
264 const lpMetaboxColorPicker = () => {
265 $( '.lp-metabox__colorpick' )
266 .iris( {
267 change( event, ui ) {
268 $( this ).parent().find( '.colorpickpreview' ).css( { backgroundColor: ui.color.toString() } );
269 },
270 hide: true,
271 border: true,
272 } )
273
274 .on( 'click focus', function( event ) {
275 event.stopPropagation();
276 $( '.iris-picker' ).hide();
277 $( this ).closest( 'td' ).find( '.iris-picker' ).show();
278 $( this ).data( 'original-value', $( this ).val() );
279 } )
280
281 .on( 'change', function() {
282 if ( $( this ).is( '.iris-error' ) ) {
283 const originalValue = $( this ).data( 'original-value' );
284
285 if ( originalValue.match( /^\#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/ ) ) {
286 $( this ).val( $( this ).data( 'original-value' ) ).trigger( 'change' );
287 } else {
288 $( this ).val( '' ).trigger( 'change' );
289 }
290 }
291 } );
292
293 $( 'body' ).on( 'click', function() {
294 $( '.iris-picker' ).hide();
295 } );
296 };
297
298 const lpMetaboxImage = () => {
299 $( '.lp-metabox-field__image' ).each( ( i, ele ) => {
300 let lpImageFrame;
301
302 const addImage = $( ele ).find( '.lp-metabox-field__image--add' );
303 const delImage = $( ele ).find( '.lp-metabox-field__image--delete' );
304
305 const image = $( ele ).find( '.lp-metabox-field__image--image' );
306 const inputVal = $( ele ).find( '.lp-metabox-field__image--id' );
307
308 if ( ! inputVal.val() ) {
309 addImage.show();
310 delImage.hide();
311 } else {
312 addImage.hide();
313 delImage.show();
314 }
315
316 addImage.on( 'click', ( event ) => {
317 event.preventDefault();
318
319 if ( lpImageFrame ) {
320 lpImageFrame.open();
321 return;
322 }
323
324 lpImageFrame = wp.media( {
325 title: addImage.data( 'choose' ),
326 button: {
327 text: addImage.data( 'update' ),
328 },
329 multiple: false,
330 } );
331
332 lpImageFrame.on( 'select', function() {
333 const attachment = lpImageFrame.state().get( 'selection' ).first().toJSON();
334 const attachmentImage = attachment.sizes && attachment.sizes.thumbnail ? attachment.sizes.thumbnail.url : attachment.url;
335
336 image.append( '<div class="lp-metabox-field__image--inner"><img src="' + attachmentImage + '" alt="" style="max-width:100%;"/></div>' );
337
338 inputVal.val( attachment.id );
339
340 addImage.hide();
341
342 delImage.show();
343 } );
344
345 lpImageFrame.open();
346 } );
347
348 delImage.on( 'click', ( event ) => {
349 event.preventDefault();
350
351 image.html( '' );
352
353 addImage.show();
354
355 delImage.hide();
356
357 inputVal.val( '' );
358 } );
359 } );
360 };
361
362 const lpMetaboxImageAdvanced = () => {
363 $( '.lp-metabox-field__image-advanced' ).each( ( i, element ) => {
364 let lpImageFrame;
365
366 const imageGalleryIds = $( element ).find( '#lp-gallery-images-ids' );
367 const listImages = $( element ).find( '.lp-metabox-field__image-advanced-images' );
368 const btnUpload = $( element ).find( '.lp-metabox-field__image-advanced-upload > a' );
369
370 $( btnUpload ).on( 'click', ( event ) => {
371 event.preventDefault();
372
373 if ( lpImageFrame ) {
374 lpImageFrame.open();
375 return;
376 }
377
378 lpImageFrame = wp.media( {
379 title: btnUpload.data( 'choose' ),
380 button: {
381 text: btnUpload.data( 'update' ),
382 },
383 states: [
384 new wp.media.controller.Library( {
385 title: btnUpload.data( 'choose' ),
386 filterable: 'all',
387 multiple: true,
388 } ),
389 ],
390 } );
391
392 lpImageFrame.on( 'select', function() {
393 const selection = lpImageFrame.state().get( 'selection' );
394 let attachmentIds = imageGalleryIds.val();
395
396 selection.forEach( function( attachment ) {
397 attachment = attachment.toJSON();
398
399 if ( attachment.id ) {
400 attachmentIds = attachmentIds ? attachmentIds + ',' + attachment.id : attachment.id;
401 const attachmentImage = attachment.sizes && attachment.sizes.thumbnail ? attachment.sizes.thumbnail.url : attachment.url;
402
403 listImages.append(
404 '<li class="image" data-attachment_id="' + attachment.id + '"><img src="' + attachmentImage +
405 '" /><ul class="actions"><li><a href="#" class="delete" title="' + btnUpload.data( 'delete' ) + '">' +
406 btnUpload.data( 'text' ) + '</a></li></ul></li>'
407 );
408 }
409 } );
410
411 imageGalleryIds.val( attachmentIds );
412 } );
413
414 lpImageFrame.open();
415 } );
416
417 listImages.sortable( {
418 items: 'li.image',
419 cursor: 'move',
420 scrollSensitivity: 40,
421 forcePlaceholderSize: true,
422 forceHelperSize: false,
423 helper: 'clone',
424 opacity: 0.65,
425 placeholder: 'lp-metabox-sortable-placeholder',
426 start( event, ui ) {
427 ui.item.css( 'background-color', '#f6f6f6' );
428 },
429 stop( event, ui ) {
430 ui.item.removeAttr( 'style' );
431 },
432 update() {
433 let attachmentIds = '';
434
435 listImages.find( 'li.image' ).css( 'cursor', 'default' ).each( function() {
436 const attachmentId = $( this ).attr( 'data-attachment_id' );
437 attachmentIds = attachmentIds + attachmentId + ',';
438 } );
439
440 imageGalleryIds.val( attachmentIds );
441 },
442 } );
443
444 $( listImages ).find( 'li.image' ).each( ( i, ele ) => {
445 const del = $( ele ).find( 'a.delete' );
446
447 del.on( 'click', () => {
448 $( ele ).remove();
449
450 let attachmentIds = '';
451
452 $( listImages ).find( 'li.image' ).css( 'cursor', 'default' ).each( function() {
453 const attachmentId = $( this ).attr( 'data-attachment_id' );
454 attachmentIds = attachmentIds + attachmentId + ',';
455 } );
456
457 imageGalleryIds.val( attachmentIds );
458
459 return false;
460 } );
461 } );
462 } );
463 };
464
465 const lpMetaboxCourseTabs = () => {
466 $( document.body ).on( 'lp-metabox-course-tab-panels', function() {
467 $( 'ul.lp-meta-box__course-tab__tabs' ).show();
468
469 $( 'ul.lp-meta-box__course-tab__tabs a' ).on( 'click', function( e ) {
470 e.preventDefault();
471
472 const panelWrap = $( this ).closest( 'div.lp-meta-box__course-tab' );
473
474 $( 'ul.lp-meta-box__course-tab__tabs li', panelWrap ).removeClass( 'active' );
475
476 $( this ).parent().addClass( 'active' );
477
478 $( 'div.lp-meta-box-course-panels', panelWrap ).hide();
479
480 $( $( this ).attr( 'href' ) ).show();
481 } );
482
483 $( 'div.lp-meta-box__course-tab' ).each( function() {
484 $( this ).find( 'ul.lp-meta-box__course-tab__tabs li' ).eq( 0 ).find( 'a' ).trigger( 'click' );
485 } );
486 } ).trigger( 'lp-metabox-course-tab-panels' );
487 };
488
489 // use to show and hide field condition logic metabox.
490 const lpMetaboxCondition = () => {
491 const fields = document.querySelectorAll( '.lp-meta-box .form-field' );
492
493 fields.forEach( ( field ) => {
494 if ( field.hasAttribute( 'data-show' ) && field.dataset.show ) {
495 lpMetaboxConditionType( field, field.dataset.show, 'show' );
496 } else if ( field.hasAttribute( 'data-hide' ) && field.dataset.hide ) {
497 lpMetaboxConditionType( field, field.dataset.hide, 'hide' );
498 }
499 } );
500 };
501
502 const lpMetaboxConditionType = ( field, conditions, typeCondition = 'show' ) => {
503 const condition = JSON.parse( conditions ),
504 eles = document.querySelectorAll( `input[id^="${ condition[ 0 ] }"]` ),
505 logic = condition[ 1 ] === '=' ? '=' : '!=',
506 dataLogic = condition[ 2 ];
507
508 const switchCase = ( type, ele, target ) => {
509 switch ( type ) {
510 case 'checkbox':
511 let val = dataLogic;
512
513 if ( dataLogic === 'yes' || dataLogic === '1' || dataLogic === 1 || dataLogic === 'true' ) {
514 val = true;
515 } else if ( dataLogic === 'no' || dataLogic === '0' || dataLogic === 0 || dataLogic === 'false' ) {
516 val = false;
517 }
518
519 if ( logic == '!=' && val !== Boolean( target ? target.checked : ele.checked ) ) {
520 field.style.display = typeCondition === 'show' ? '' : 'none';
521 } else if ( logic == '=' && val == Boolean( target ? target.checked : ele.checked ) ) {
522 field.style.display = typeCondition === 'show' ? '' : 'none';
523 } else {
524 field.style.display = typeCondition === 'show' ? 'none' : '';
525 }
526 break;
527 }
528 };
529
530 eles.forEach( ( ele ) => {
531 const type = ele.getAttribute( 'type' );
532
533 switchCase( type, ele );
534
535 ele.addEventListener( 'change', ( e ) => {
536 const target = e.target;
537
538 switchCase( type, ele, target );
539 } );
540 } );
541 };
542
543 /** End Nhamdv code */
544
545 const initTooltips = function initTooltips() {
546 $( '.learn-press-tooltip' ).each( function() {
547 const $el = $( this ),
548 args = $.extend( { title: 'data-tooltip', offset: 10, gravity: 's' }, $el.data() );
549 $el.tipsy( args );
550 } );
551 };
552
553 const initSelect2 = function initSelect2() {
554 if ( $.fn.select2 ) {
555 const elSelect2 = $( '.lp-select-2 select' );
556 elSelect2.select2( {
557 placeholder: 'Select a value',
558 } );
559 elSelect2.on( 'change.select2', function( e ) {
560 const el = $( e.target );
561 const val = el.val();
562
563 if ( ! val.length ) {
564 el.val( null );
565 }
566 } );
567
568 $( '.lp_autocomplete_metabox_field' ).each( function() {
569 const dataAtts = $( this ).data( 'atts' );
570 let action = dataAtts.action;
571
572 if ( ! action ) {
573 switch ( dataAtts.data ) {
574 case 'users':
575 action = dataAtts.rest_url + 'wp/v2/users';
576 break;
577 default:
578 action = dataAtts.rest_url + 'wp/v2/' + dataAtts.data;
579 break;
580 }
581 }
582
583 $( this ).find( 'select' ).select2( {
584 placeholder: dataAtts.placeholder ? dataAtts.placeholder : 'Select',
585 ajax: {
586 url: action,
587 dataType: 'json',
588 delay: 250,
589 beforeSend( xhr ) {
590 xhr.setRequestHeader( 'X-WP-Nonce', dataAtts.nonce );
591 },
592 data( params ) {
593 return {
594 search: params.term,
595 };
596 },
597 processResults( data ) {
598 return {
599 results: data.map( ( item ) => {
600 return {
601 id: item.id,
602 text: item.title && item.title.rendered ? item.title.rendered : item.name,
603 };
604 } ),
605 };
606 },
607 cache: true,
608 },
609 minimumInputLength: 2,
610 } );
611 } );
612 }
613 };
614
615 const initSingleCoursePermalink = function initSingleCoursePermalink() {
616 $doc
617 .on( 'change', '.learn-press-single-course-permalink input[type="radio"]', function() {
618 const $check = $( this ),
619 $row = $check.closest( '.learn-press-single-course-permalink' );
620 if ( $row.hasClass( 'custom-base' ) ) {
621 $row.find( 'input[type="text"]' ).prop( 'readonly', false );
622 } else {
623 $row.siblings( '.custom-base' ).find( 'input[type="text"]' ).prop( 'readonly', true );
624 }
625 } )
626 .on( 'change', 'input.learn-press-course-base', function() {
627 $( '#course_permalink_structure' ).val( $( this ).val() );
628 } )
629 .on( 'focus', '#course_permalink_structure', function() {
630 $( '#learn_press_custom_permalink' ).click();
631 } )
632 .on( 'change', '#learn_press_courses_page_id', function() {
633 $( 'tr.learn-press-courses-page-id' ).toggleClass( 'hide-if-js', ! parseInt( this.value ) );
634 } );
635 };
636
637 const togglePaymentStatus = function togglePaymentStatus( e ) {
638 e.preventDefault();
639 const $row = $( this ).closest( 'tr' ),
640 $button = $( this ),
641 status = $row.find( '.status' ).hasClass( 'enabled' ) ? 'no' : 'yes';
642
643 $.ajax( {
644 url: '',
645 data: {
646 'lp-ajax': 'update-payment-status',
647 status,
648 id: $row.data( 'payment' ),
649 },
650 success( response ) {
651 response = LP.parseJSON( response );
652 for ( const i in response ) {
653 $( '#payment-' + i + ' .status' ).toggleClass( 'enabled', response[ i ] );
654 }
655 },
656 } );
657 };
658
659 const updateEmailStatus = function updateEmailStatus() {
660 ( function() {
661 $.post( {
662 url: window.location.href,
663 data: {
664 'lp-ajax': 'update_email_status',
665 status: $( this ).parent().hasClass( 'enabled' ) ? 'no' : 'yes',
666 id: $( this ).data( 'id' ),
667 },
668 dataType: 'text',
669 success: $.proxy( function( res ) {
670 res = LP.parseJSON( res );
671 for ( const i in res ) {
672 $( '#email-' + i + ' .status' ).toggleClass( 'enabled', res[ i ] );
673 }
674 }, this ),
675 } );
676 } ).apply( this );
677 };
678
679 const lpMetaboxsalePriceDate = () => {
680 // Don't run in LearnPress Frontend Editor Add-on.
681 if ( ! $( '#course-settings' ).length ) {
682 return;
683 }
684
685 $( '.lp_sale_dates_fields' ).each( function() {
686 const $this = $( this );
687 const $wrap = $this.closest( 'div.lp-meta-box-course-panels' );
688 let saleScheduleSet = false;
689
690 $this.find( 'input' ).each( function() {
691 if ( '' !== $( this ).val() ) {
692 saleScheduleSet = true;
693 }
694 } );
695
696 if ( saleScheduleSet ) {
697 $wrap.find( '.lp_sale_price_schedule' ).hide();
698 $wrap.find( '.lp_sale_dates_fields' ).show();
699 } else {
700 $wrap.find( '.lp_sale_price_schedule' ).show();
701 $wrap.find( '.lp_sale_dates_fields' ).hide();
702 }
703 } );
704
705 $( '.lp-meta-box-course-panels' ).on( 'click', '.lp_sale_price_schedule', function() {
706 const wrap = $( this ).closest( 'div.lp-meta-box-course-panels' );
707
708 $( this ).hide();
709
710 wrap.find( '.lp_cancel_sale_schedule' ).show();
711 wrap.find( '.lp_sale_dates_fields' ).show();
712
713 return false;
714 } );
715
716 $( '.lp-meta-box-course-panels' ).on( 'click', '.lp_cancel_sale_schedule', function() {
717 const wrap = $( this ).closest( 'div.lp-meta-box-course-panels' );
718
719 $( this ).hide();
720
721 wrap.find( '.lp_sale_price_schedule' ).show();
722 wrap.find( '.lp_sale_dates_fields' ).hide();
723 wrap.find( '.lp_sale_dates_fields' ).find( 'input' ).val( '' );
724
725 return false;
726 } );
727
728 $( document ).on( 'input', '#price_course_data', function( e ) {
729 const $this = $( this ),
730 regularPrice = $( '.lp_meta_box_regular_price' ),
731 salePrice = $( '.lp_meta_box_sale_price' ),
732 $target = $( e.target ).attr( 'id' );
733
734 $this.find( '.learn-press-tip-floating' ).remove();
735
736 if ( parseInt( salePrice.val() ) > parseInt( regularPrice.val() ) ) {
737 if ( $target === '_lp_price' ) {
738 regularPrice.parent( '.form-field' ).append( '<div class="learn-press-tip-floating">' + lpAdminCourseEditorSettings.i18n.notice_price + '</div>' );
739 } else if ( $target === '_lp_sale_price' ) {
740 salePrice.parent( '.form-field' ).append( '<div class="learn-press-tip-floating">' + lpAdminCourseEditorSettings.i18n.notice_sale_price + '</div>' );
741 }
742 }
743 } );
744
745 const datePickerSelect = function( datepicker ) {
746 const option = $( datepicker ).is( '#_lp_sale_start' ) ? 'minDate' : 'maxDate',
747 otherDateField = 'minDate' === option ? $( '#_lp_sale_end' ) : $( '#_lp_sale_start' ),
748 date = $( datepicker ).datetimepicker( 'getDate' );
749
750 $( otherDateField ).datetimepicker( 'option', option, date );
751 $( datepicker ).trigger( 'change' );
752 };
753
754 $( '.lp_sale_dates_fields' ).each( function() {
755 $( this ).find( 'input' ).datetimepicker( {
756 timeFormat: 'HH:mm',
757 separator: ' ',
758 dateFormat: 'yy-mm-dd',
759 showButtonPanel: true,
760 onSelect() {
761 datePickerSelect( $( this ) );
762 },
763 } );
764
765 $( this ).find( 'input' ).each( function() {
766 datePickerSelect( $( this ) );
767 } );
768 } );
769 };
770
771 const lpHidePassingGrade = () => {
772 const listHides = [ 'evaluate_final_quiz', 'evaluate_final_assignment' ];
773 const inputLists = document.querySelectorAll( 'input[type=radio][name=_lp_course_result]' );
774
775 [ ...inputLists ].map( ( ele, i ) => {
776 if ( ele.checked && listHides.includes( ele.value ) ) {
777 $( '._lp_passing_condition_field' ).hide();
778 }
779
780 return null;
781 } );
782
783 $( 'input[type=radio][name=_lp_course_result]' ).on( 'change', function( e ) {
784 if ( listHides.includes( e.target.value ) ) {
785 $( '._lp_passing_condition_field' ).hide();
786 } else {
787 $( '._lp_passing_condition_field' ).show();
788 }
789 } );
790 };
791
792 const callbackFilterTemplates = function callbackFilterTemplates() {
793 const $link = $( this );
794
795 if ( $link.hasClass( 'current' ) ) {
796 return false;
797 }
798
799 const $templatesList = $( '#learn-press-template-files' ),
800 $templates = $templatesList.find( 'tr[data-template]' ),
801 template = $link.data( 'template' ),
802 filter = $link.data( 'filter' );
803
804 $link.addClass( 'current' ).siblings( 'a' ).removeClass( 'current' );
805
806 if ( ! template ) {
807 if ( ! filter ) {
808 $templates.removeClass( 'hide-if-js' );
809 } else {
810 $templates.map( function() {
811 $( this ).toggleClass( 'hide-if-js', $( this ).data( 'filter-' + filter ) !== 'yes' );
812 } );
813 }
814 } else {
815 $templates.map( function() {
816 $( this ).toggleClass( 'hide-if-js', $( this ).data( 'template' ) !== template );
817 } );
818 }
819
820 $( '#learn-press-no-templates' ).toggleClass( 'hide-if-js', !! $templatesList.find( 'tr.template-row:not(.hide-if-js):first' ).length );
821
822 return false;
823 };
824
825 const toggleEmails = function toggleEmails( e ) {
826 e.preventDefault();
827 const $button = $( this ),
828 status = $button.data( 'status' );
829
830 $.ajax( {
831 url: '',
832 data: {
833 'lp-ajax': 'update_email_status',
834 status,
835 },
836 success( response ) {
837 response = LP.parseJSON( response );
838 for ( const i in response ) {
839 $( '#email-' + i + ' .status' ).toggleClass( 'enabled', response[ i ] );
840 }
841 },
842 } );
843 };
844
845 const importCourses = function importCourses() {
846 const $container = $( '#learn-press-install-sample-data-notice' ),
847 action = $( this ).attr( 'data-action' );
848 if ( ! action ) {
849 return;
850 }
851 e.preventDefault();
852
853 if ( action === 'yes' ) {
854 $container
855 .find( '.install-sample-data-notice' ).slideUp()
856 .siblings( '.install-sample-data-loading' ).slideDown();
857 } else {
858 $container.fadeOut();
859 }
860 $.ajax( {
861 url: ajaxurl,
862 dataType: 'html',
863 type: 'post',
864 data: {
865 action: 'learnpress_install_sample_data',
866 yes: action,
867 },
868 success( response ) {
869 response = LP.parseJSON( response );
870 if ( response.url ) {
871 $.ajax( {
872 url: response.url,
873 success() {
874 $container
875 .find( '.install-sample-data-notice' ).html( response.message ).slideDown()
876 .siblings( '.install-sample-data-loading' ).slideUp();
877 },
878 } );
879 } else {
880 $container
881 .find( '.install-sample-data-notice' ).html( response.message ).slideDown()
882 .siblings( '.install-sample-data-loading' ).slideUp();
883 }
884 },
885 } );
886 };
887
888 const onReady = function onReady() {
889 makePaymentsSortable();
890 initSelect2();
891 initTooltips();
892 initSingleCoursePermalink();
893
894 // lp Metabox in LP4.
895 lpMetaboxCourseTabs();
896 lpMetaboxCustomFields();
897 lpMetaboxColorPicker();
898 lpMetaboxImageAdvanced();
899 lpMetaboxImage();
900 lpMetaboxsalePriceDate();
901 lpMetaboxExtraInfo();
902 lpHidePassingGrade();
903 lpGetFinalQuiz();
904 lpMetaboxCondition();
905 lpMetaboxRepeaterField();
906
907 $( document )
908 .on( 'click', '.learn-press-payments .status .dashicons', togglePaymentStatus )
909 .on( 'click', '.change-email-status', updateEmailStatus )
910 .on( 'click', '.learn-press-filter-template', callbackFilterTemplates )
911 .on( 'click', '#learn-press-enable-emails, #learn-press-disable-emails', toggleEmails )
912 .on( 'click', '#learn-press-install-sample-data-notice a', importCourses );
913 };
914
915 $( document ).ready( onReady );
916