| 1 |
import { initElsTomSelect, searchUserOnListPost } from './init-tom-select.js'; |
| 2 |
import { AdminUtilsFunctions, Api, Utils } from './utils-admin.js'; |
| 3 |
import { DropdownPages } from './share/dropdown-pages.js'; |
| 4 |
|
| 5 |
( function( $ ) { |
| 6 |
/** |
| 7 |
* Callback event for button to creating pages inside error message. |
| 8 |
* |
| 9 |
* @param {Event} e |
| 10 |
*/ |
| 11 |
|
| 12 |
const createPages = function createPages( e ) { |
| 13 |
const $button = $( this ).addClass( 'disabled' ); |
| 14 |
e.preventDefault(); |
| 15 |
$.post( { |
| 16 |
url: $button.attr( 'href' ), |
| 17 |
data: { |
| 18 |
'lp-ajax': 'create-pages', |
| 19 |
}, |
| 20 |
dataType: 'text', |
| 21 |
success: function success( res ) { |
| 22 |
const $message = $button.closest( '.lp-notice' ).html( '<p>' + res + '</p>' ); |
| 23 |
setTimeout( function() { |
| 24 |
$message.fadeOut(); |
| 25 |
}, 2000 ); |
| 26 |
}, |
| 27 |
} ); |
| 28 |
}; |
| 29 |
|
| 30 |
const lpMetaboxFileInput = () => { |
| 31 |
$( '.lp-meta-box__file' ).each( ( i, element ) => { |
| 32 |
let lpImageFrame; |
| 33 |
|
| 34 |
const imageGalleryIds = $( element ).find( '.lp-meta-box__file_input' ); |
| 35 |
const listImages = $( element ).find( '.lp-meta-box__file_list' ); |
| 36 |
const btnUpload = $( element ).find( '.btn-upload' ); |
| 37 |
const isMultil = !! $( element ).data( 'multil' ); |
| 38 |
|
| 39 |
$( btnUpload ).on( 'click', ( event ) => { |
| 40 |
event.preventDefault(); |
| 41 |
|
| 42 |
if ( lpImageFrame ) { |
| 43 |
lpImageFrame.open(); |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
lpImageFrame = wp.media( { |
| 48 |
states: [ |
| 49 |
new wp.media.controller.Library( { |
| 50 |
filterable: 'all', |
| 51 |
multiple: isMultil, |
| 52 |
} ), |
| 53 |
], |
| 54 |
} ); |
| 55 |
|
| 56 |
lpImageFrame.on( 'select', function() { |
| 57 |
const selection = lpImageFrame.state().get( 'selection' ); |
| 58 |
let attachmentIds = imageGalleryIds.val(); |
| 59 |
|
| 60 |
selection.forEach( function( attachment ) { |
| 61 |
attachment = attachment.toJSON(); |
| 62 |
|
| 63 |
if ( attachment.id ) { |
| 64 |
if ( ! isMultil ) { |
| 65 |
attachmentIds = attachment.id; |
| 66 |
listImages.empty(); |
| 67 |
} else { |
| 68 |
attachmentIds = attachmentIds ? attachmentIds + ',' + attachment.id : attachment.id; |
| 69 |
} |
| 70 |
|
| 71 |
listImages.append( |
| 72 |
'<li class="lp-meta-box__file_list-item image" data-attachment_id="' + attachment.id + '"><img class="is_file" src="' + attachment.icon + |
| 73 |
'" /><span>' + attachment.filename + '</span><ul class="actions"><li><a href="#" class="delete"></a></li></ul></li>' |
| 74 |
); |
| 75 |
} |
| 76 |
} ); |
| 77 |
|
| 78 |
delImage(); |
| 79 |
|
| 80 |
imageGalleryIds.val( attachmentIds ); |
| 81 |
} ); |
| 82 |
|
| 83 |
lpImageFrame.open(); |
| 84 |
} ); |
| 85 |
|
| 86 |
if ( isMultil ) { |
| 87 |
listImages.sortable( { |
| 88 |
items: 'li.image', |
| 89 |
cursor: 'move', |
| 90 |
scrollSensitivity: 40, |
| 91 |
forcePlaceholderSize: true, |
| 92 |
forceHelperSize: false, |
| 93 |
helper: 'clone', |
| 94 |
opacity: 0.65, |
| 95 |
placeholder: 'lp-metabox-sortable-placeholder', |
| 96 |
start( event, ui ) { |
| 97 |
ui.item.css( 'background-color', '#f6f6f6' ); |
| 98 |
}, |
| 99 |
stop( event, ui ) { |
| 100 |
ui.item.removeAttr( 'style' ); |
| 101 |
}, |
| 102 |
update() { |
| 103 |
let attachmentIds = ''; |
| 104 |
|
| 105 |
listImages.find( 'li.image' ).css( 'cursor', 'default' ).each( function() { |
| 106 |
const attachmentId = $( this ).attr( 'data-attachment_id' ); |
| 107 |
attachmentIds = attachmentIds + attachmentId + ','; |
| 108 |
} ); |
| 109 |
|
| 110 |
delImage(); |
| 111 |
|
| 112 |
imageGalleryIds.val( attachmentIds ); |
| 113 |
}, |
| 114 |
} ); |
| 115 |
} |
| 116 |
|
| 117 |
const delImage = () => { |
| 118 |
$( listImages ).find( 'li.image' ).each( ( i, ele ) => { |
| 119 |
const del = $( ele ).find( 'a.delete' ); |
| 120 |
|
| 121 |
del.on( 'click', function() { |
| 122 |
$( ele ).remove(); |
| 123 |
|
| 124 |
if ( isMultil ) { |
| 125 |
let attachmentIds = ''; |
| 126 |
|
| 127 |
$( listImages ).find( 'li.image' ).css( 'cursor', 'default' ).each( function() { |
| 128 |
const attachmentId = $( this ).attr( 'data-attachment_id' ); |
| 129 |
attachmentIds = attachmentIds + attachmentId + ','; |
| 130 |
} ); |
| 131 |
|
| 132 |
imageGalleryIds.val( attachmentIds ); |
| 133 |
} else { |
| 134 |
imageGalleryIds.val( '' ); |
| 135 |
} |
| 136 |
|
| 137 |
return false; |
| 138 |
} ); |
| 139 |
} ); |
| 140 |
}; |
| 141 |
|
| 142 |
delImage(); |
| 143 |
} ); |
| 144 |
}; |
| 145 |
|
| 146 |
const onReady = function onReady() { |
| 147 |
lpMetaboxFileInput(); |
| 148 |
//updateDb(); |
| 149 |
const dropdownPages = new DropdownPages(); |
| 150 |
dropdownPages.init(); |
| 151 |
//$( '.learn-press-advertisement-slider' ).LP( 'Advertisement', 'a', 's' ).appendTo( $( '#wpbody-content' ) ); |
| 152 |
//$( '.learn-press-toggle-item-preview' ).on( 'change', updateItemPreview ); |
| 153 |
$( '.learn-press-tip' ).LP( 'QuickTip' ); //$('.learn-press-tabs').LP('AdminTab'); |
| 154 |
|
| 155 |
$( document ).on( 'click', '#learn-press-create-pages', createPages ) |
| 156 |
//.on( 'click', '.lp-upgrade-notice .close-notice', hideUpgradeMessage ) |
| 157 |
//.on( 'click', '.plugin-action-buttons a', pluginActions ) |
| 158 |
//.on( 'click', '[data-remove-confirm]', preventDefault ) |
| 159 |
.on( 'mousedown', '.lp-sortable-handle', function( e ) { |
| 160 |
$( 'html, body' ).addClass( 'lp-item-moving' ); |
| 161 |
$( e.target ).closest( '.lp-sortable-handle' ).css( 'cursor', 'inherit' ); |
| 162 |
} ).on( 'mouseup', function( e ) { |
| 163 |
$( 'html, body' ).removeClass( 'lp-item-moving' ); |
| 164 |
$( '.lp-sortable-handle' ).css( 'cursor', '' ); |
| 165 |
} ); |
| 166 |
|
| 167 |
// Scroll to Passing grade when click link final Quiz in Course Setting. |
| 168 |
if ( window.location.hash ) { |
| 169 |
const hash = window.location.hash; |
| 170 |
|
| 171 |
if ( hash === '#_lp_passing_grade' ) { |
| 172 |
const ele = document.querySelector( hash ); |
| 173 |
|
| 174 |
$( 'html, body' ).animate( { |
| 175 |
scrollTop: $( hash ).offset().top, |
| 176 |
}, 900, 'swing' ); |
| 177 |
|
| 178 |
ele.parentNode.style.border = '2px solid orangered'; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
// Show/hide meta-box field with type checkbox |
| 183 |
/*$( 'input' ).on( 'click', function( e ) { |
| 184 |
const el = $( e.target ); |
| 185 |
if ( ! el.length ) { |
| 186 |
return; |
| 187 |
} |
| 188 |
|
| 189 |
const id = el.attr( 'id' ); |
| 190 |
if ( ! id ) { |
| 191 |
return; |
| 192 |
} |
| 193 |
|
| 194 |
const classHide = id.replace( 'learn_press_', '' ); |
| 195 |
const elHide = $( `.show_if_${ classHide }` ); |
| 196 |
|
| 197 |
if ( el.is( ':checked' ) ) { |
| 198 |
elHide.show(); |
| 199 |
} else { |
| 200 |
elHide.hide(); |
| 201 |
} |
| 202 |
} );*/ |
| 203 |
}; |
| 204 |
|
| 205 |
$( document ).ready( onReady ); |
| 206 |
}( jQuery ) ); |
| 207 |
|
| 208 |
const showHideOptionsDependency = ( e, target ) => { |
| 209 |
if ( target.tagName === 'INPUT' ) { |
| 210 |
if ( target.closest( '.forminp ' ) ) { |
| 211 |
const nameInput = target.name; |
| 212 |
const classDependency = nameInput.replace( 'learn_press_', '' ); |
| 213 |
|
| 214 |
const elClassDependency = document.querySelectorAll( `.show_if_${ classDependency }` ); |
| 215 |
if ( elClassDependency ) { |
| 216 |
elClassDependency.forEach( ( el ) => { |
| 217 |
el.classList.toggle( 'lp-option-disabled' ); |
| 218 |
} ); |
| 219 |
} |
| 220 |
} else if ( target.closest( '.lp-meta-box' ) ) { |
| 221 |
const elLPMetaBox = target.closest( '.lp-meta-box' ); |
| 222 |
const nameInput = target.name; |
| 223 |
|
| 224 |
const elClassDependency = elLPMetaBox.querySelectorAll( `[data-dependency="${ nameInput }"]` ); |
| 225 |
if ( elClassDependency ) { |
| 226 |
elClassDependency.forEach( ( el ) => { |
| 227 |
el.classList.toggle( 'lp-option-disabled' ); |
| 228 |
} ); |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
}; |
| 233 |
|
| 234 |
// Events |
| 235 |
document.addEventListener( 'click', ( e ) => { |
| 236 |
const target = e.target; |
| 237 |
showHideOptionsDependency( e, target ); |
| 238 |
// For case click add on Widgets of WordPress. |
| 239 |
initElsTomSelect(); |
| 240 |
} ); |
| 241 |
|
| 242 |
document.addEventListener( 'DOMContentLoaded', () => { |
| 243 |
searchUserOnListPost(); |
| 244 |
// Sure that the TomSelect is loaded if listen can't find elements. |
| 245 |
initElsTomSelect(); |
| 246 |
} ); |
| 247 |
|
| 248 |
// Listen element select created on DOM. |
| 249 |
Utils.lpOnElementReady( 'select.lp-tom-select', ( e ) => { |
| 250 |
initElsTomSelect(); |
| 251 |
} ); |
| 252 |
|
| 253 |
Utils.lpOnElementReady( '#posts-filter', ( e ) => { |
| 254 |
searchUserOnListPost(); |
| 255 |
} ); |
| 256 |
|
| 257 |
window.lpFindTomSelect = initElsTomSelect; |
| 258 |
|