PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.6.9.2
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.6.9.2
4.4.8 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 All 139 releases
learnpress / assets / src / js / admin / learnpress.js

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

860 lines 23.6 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' ? 'flex' : 'none';
521 } else if ( logic == '=' && val == Boolean( target ? target.checked : ele.checked ) ) {
522 field.style.display = typeCondition === 'show' ? 'flex' : 'none';
523 } else {
524 field.style.display = typeCondition === 'show' ? 'none' : 'flex';
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 $( '.lp-select-2 select' ).select2();
556 }
557 };
558
559 const initSingleCoursePermalink = function initSingleCoursePermalink() {
560 $doc
561 .on( 'change', '.learn-press-single-course-permalink input[type="radio"]', function() {
562 const $check = $( this ),
563 $row = $check.closest( '.learn-press-single-course-permalink' );
564 if ( $row.hasClass( 'custom-base' ) ) {
565 $row.find( 'input[type="text"]' ).prop( 'readonly', false );
566 } else {
567 $row.siblings( '.custom-base' ).find( 'input[type="text"]' ).prop( 'readonly', true );
568 }
569 } )
570 .on( 'change', 'input.learn-press-course-base', function() {
571 $( '#course_permalink_structure' ).val( $( this ).val() );
572 } )
573 .on( 'focus', '#course_permalink_structure', function() {
574 $( '#learn_press_custom_permalink' ).click();
575 } )
576 .on( 'change', '#learn_press_courses_page_id', function() {
577 $( 'tr.learn-press-courses-page-id' ).toggleClass( 'hide-if-js', ! parseInt( this.value ) );
578 } );
579 };
580
581 const togglePaymentStatus = function togglePaymentStatus( e ) {
582 e.preventDefault();
583 const $row = $( this ).closest( 'tr' ),
584 $button = $( this ),
585 status = $row.find( '.status' ).hasClass( 'enabled' ) ? 'no' : 'yes';
586
587 $.ajax( {
588 url: '',
589 data: {
590 'lp-ajax': 'update-payment-status',
591 status,
592 id: $row.data( 'payment' ),
593 },
594 success( response ) {
595 response = LP.parseJSON( response );
596 for ( const i in response ) {
597 $( '#payment-' + i + ' .status' ).toggleClass( 'enabled', response[ i ] );
598 }
599 },
600 } );
601 };
602
603 const updateEmailStatus = function updateEmailStatus() {
604 ( function() {
605 $.post( {
606 url: window.location.href,
607 data: {
608 'lp-ajax': 'update_email_status',
609 status: $( this ).parent().hasClass( 'enabled' ) ? 'no' : 'yes',
610 id: $( this ).data( 'id' ),
611 },
612 dataType: 'text',
613 success: $.proxy( function( res ) {
614 res = LP.parseJSON( res );
615 for ( const i in res ) {
616 $( '#email-' + i + ' .status' ).toggleClass( 'enabled', res[ i ] );
617 }
618 }, this ),
619 } );
620 } ).apply( this );
621 };
622
623 const lpMetaboxsalePriceDate = () => {
624 // Don't run in LearnPress Frontend Editor Add-on.
625 if ( ! $( '#course-settings' ).length ) {
626 return;
627 }
628
629 $( '.lp_sale_dates_fields' ).each( function() {
630 const $this = $( this );
631 const $wrap = $this.closest( 'div.lp-meta-box-course-panels' );
632 let saleScheduleSet = false;
633
634 $this.find( 'input' ).each( function() {
635 if ( '' !== $( this ).val() ) {
636 saleScheduleSet = true;
637 }
638 } );
639
640 if ( saleScheduleSet ) {
641 $wrap.find( '.lp_sale_price_schedule' ).hide();
642 $wrap.find( '.lp_sale_dates_fields' ).show();
643 } else {
644 $wrap.find( '.lp_sale_price_schedule' ).show();
645 $wrap.find( '.lp_sale_dates_fields' ).hide();
646 }
647 } );
648
649 $( '.lp-meta-box-course-panels' ).on( 'click', '.lp_sale_price_schedule', function() {
650 const wrap = $( this ).closest( 'div.lp-meta-box-course-panels' );
651
652 $( this ).hide();
653
654 wrap.find( '.lp_cancel_sale_schedule' ).show();
655 wrap.find( '.lp_sale_dates_fields' ).show();
656
657 return false;
658 } );
659
660 $( '.lp-meta-box-course-panels' ).on( 'click', '.lp_cancel_sale_schedule', function() {
661 const wrap = $( this ).closest( 'div.lp-meta-box-course-panels' );
662
663 $( this ).hide();
664
665 wrap.find( '.lp_sale_price_schedule' ).show();
666 wrap.find( '.lp_sale_dates_fields' ).hide();
667 wrap.find( '.lp_sale_dates_fields' ).find( 'input' ).val( '' );
668
669 return false;
670 } );
671
672 $( document ).on( 'input', '#price_course_data', function( e ) {
673 const $this = $( this ),
674 regularPrice = $( '.lp_meta_box_regular_price' ),
675 salePrice = $( '.lp_meta_box_sale_price' ),
676 $target = $( e.target ).attr( 'id' );
677
678 $this.find( '.learn-press-tip-floating' ).remove();
679
680 if ( parseInt( salePrice.val() ) > parseInt( regularPrice.val() ) ) {
681 if ( $target === '_lp_price' ) {
682 regularPrice.parent( '.form-field' ).append( '<div class="learn-press-tip-floating">' + lpAdminCourseEditorSettings.i18n.notice_price + '</div>' );
683 } else if ( $target === '_lp_sale_price' ) {
684 salePrice.parent( '.form-field' ).append( '<div class="learn-press-tip-floating">' + lpAdminCourseEditorSettings.i18n.notice_sale_price + '</div>' );
685 }
686 }
687 } );
688
689 const datePickerSelect = function( datepicker ) {
690 const option = $( datepicker ).is( '#_lp_sale_start' ) ? 'minDate' : 'maxDate',
691 otherDateField = 'minDate' === option ? $( '#_lp_sale_end' ) : $( '#_lp_sale_start' ),
692 date = $( datepicker ).datetimepicker( 'getDate' );
693
694 $( otherDateField ).datetimepicker( 'option', option, date );
695 $( datepicker ).trigger( 'change' );
696 };
697
698 $( '.lp_sale_dates_fields' ).each( function() {
699 $( this ).find( 'input' ).datetimepicker( {
700 timeFormat: 'HH:mm',
701 separator: ' ',
702 dateFormat: 'yy-mm-dd',
703 showButtonPanel: true,
704 onSelect() {
705 datePickerSelect( $( this ) );
706 },
707 } );
708
709 $( this ).find( 'input' ).each( function() {
710 datePickerSelect( $( this ) );
711 } );
712 } );
713 };
714
715 const lpHidePassingGrade = () => {
716 const listHides = [ 'evaluate_final_quiz', 'evaluate_final_assignment' ];
717 const inputLists = document.querySelectorAll( 'input[type=radio][name=_lp_course_result]' );
718
719 [ ...inputLists ].map( ( ele, i ) => {
720 if ( ele.checked && listHides.includes( ele.value ) ) {
721 $( '._lp_passing_condition_field' ).hide();
722 }
723
724 return null;
725 } );
726
727 $( 'input[type=radio][name=_lp_course_result]' ).on( 'change', function( e ) {
728 if ( listHides.includes( e.target.value ) ) {
729 $( '._lp_passing_condition_field' ).hide();
730 } else {
731 $( '._lp_passing_condition_field' ).show();
732 }
733 } );
734 };
735
736 const callbackFilterTemplates = function callbackFilterTemplates() {
737 const $link = $( this );
738
739 if ( $link.hasClass( 'current' ) ) {
740 return false;
741 }
742
743 const $templatesList = $( '#learn-press-template-files' ),
744 $templates = $templatesList.find( 'tr[data-template]' ),
745 template = $link.data( 'template' ),
746 filter = $link.data( 'filter' );
747
748 $link.addClass( 'current' ).siblings( 'a' ).removeClass( 'current' );
749
750 if ( ! template ) {
751 if ( ! filter ) {
752 $templates.removeClass( 'hide-if-js' );
753 } else {
754 $templates.map( function() {
755 $( this ).toggleClass( 'hide-if-js', $( this ).data( 'filter-' + filter ) !== 'yes' );
756 } );
757 }
758 } else {
759 $templates.map( function() {
760 $( this ).toggleClass( 'hide-if-js', $( this ).data( 'template' ) !== template );
761 } );
762 }
763
764 $( '#learn-press-no-templates' ).toggleClass( 'hide-if-js', !! $templatesList.find( 'tr.template-row:not(.hide-if-js):first' ).length );
765
766 return false;
767 };
768
769 const toggleEmails = function toggleEmails( e ) {
770 e.preventDefault();
771 const $button = $( this ),
772 status = $button.data( 'status' );
773
774 $.ajax( {
775 url: '',
776 data: {
777 'lp-ajax': 'update_email_status',
778 status,
779 },
780 success( response ) {
781 response = LP.parseJSON( response );
782 for ( const i in response ) {
783 $( '#email-' + i + ' .status' ).toggleClass( 'enabled', response[ i ] );
784 }
785 },
786 } );
787 };
788
789 const importCourses = function importCourses() {
790 const $container = $( '#learn-press-install-sample-data-notice' ),
791 action = $( this ).attr( 'data-action' );
792 if ( ! action ) {
793 return;
794 }
795 e.preventDefault();
796
797 if ( action === 'yes' ) {
798 $container
799 .find( '.install-sample-data-notice' ).slideUp()
800 .siblings( '.install-sample-data-loading' ).slideDown();
801 } else {
802 $container.fadeOut();
803 }
804 $.ajax( {
805 url: ajaxurl,
806 dataType: 'html',
807 type: 'post',
808 data: {
809 action: 'learnpress_install_sample_data',
810 yes: action,
811 },
812 success( response ) {
813 response = LP.parseJSON( response );
814 if ( response.url ) {
815 $.ajax( {
816 url: response.url,
817 success() {
818 $container
819 .find( '.install-sample-data-notice' ).html( response.message ).slideDown()
820 .siblings( '.install-sample-data-loading' ).slideUp();
821 },
822 } );
823 } else {
824 $container
825 .find( '.install-sample-data-notice' ).html( response.message ).slideDown()
826 .siblings( '.install-sample-data-loading' ).slideUp();
827 }
828 },
829 } );
830 };
831
832 const onReady = function onReady() {
833 makePaymentsSortable();
834 initSelect2();
835 initTooltips();
836 initSingleCoursePermalink();
837
838 // lp Metabox in LP4.
839 lpMetaboxCourseTabs();
840 lpMetaboxCustomFields();
841 lpMetaboxColorPicker();
842 lpMetaboxImageAdvanced();
843 lpMetaboxImage();
844 lpMetaboxsalePriceDate();
845 lpMetaboxExtraInfo();
846 lpHidePassingGrade();
847 lpGetFinalQuiz();
848 lpMetaboxCondition();
849 lpMetaboxRepeaterField();
850
851 $( document )
852 .on( 'click', '.learn-press-payments .status .dashicons', togglePaymentStatus )
853 .on( 'click', '.change-email-status', updateEmailStatus )
854 .on( 'click', '.learn-press-filter-template', callbackFilterTemplates )
855 .on( 'click', '#learn-press-enable-emails, #learn-press-disable-emails', toggleEmails )
856 .on( 'click', '#learn-press-install-sample-data-notice a', importCourses );
857 };
858
859 $( document ).ready( onReady );
860