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