admin-folders.js
7 months ago
admin-galleries.js
3 years ago
admin-gallery.js
4 years ago
admin-media.js
1 year ago
admin-plugins.js
3 years ago
admin-widgets.js
3 years ago
admin.js
3 years ago
front-basicmasonry.js
3 years ago
front-basicslider.js
1 year ago
front.js
7 months ago
gutenberg.min.js
7 months ago
admin-gallery.js
458 lines
| 1 | ( function( $ ) { |
| 2 | |
| 3 | ResponsiveLightboxGallery = { |
| 4 | modal: null, |
| 5 | lastGalleryID: 0, |
| 6 | lastGalleryImage: '', |
| 7 | currentGalleryID: 0, |
| 8 | resetFilters: false, |
| 9 | galleries: {}, |
| 10 | gutenberg: false, |
| 11 | primaryButtonClass: '', |
| 12 | secondaryButtonClass: '', |
| 13 | |
| 14 | /** |
| 15 | * Initialize galleries. |
| 16 | */ |
| 17 | init: function() { |
| 18 | this.gutenberg = typeof rlBlockEditor !== 'undefined'; |
| 19 | this.searchGalleries = _.debounce( this.getGalleries, 500 ), |
| 20 | this.bindEvents(); |
| 21 | this.setButtons(); |
| 22 | }, |
| 23 | |
| 24 | /** |
| 25 | * Search galleries. |
| 26 | */ |
| 27 | searchGalleries: function() {}, |
| 28 | |
| 29 | /** |
| 30 | * Set modal buttons. |
| 31 | */ |
| 32 | setButtons: function() { |
| 33 | if ( this.gutenberg ) { |
| 34 | this.primaryButtonClass = '.rl-media-button-select-gallery'; |
| 35 | this.secondaryButtonClass = '.rl-media-button-insert-gallery'; |
| 36 | } else { |
| 37 | this.primaryButtonClass = '.rl-media-button-insert-gallery'; |
| 38 | this.secondaryButtonClass = '.rl-media-button-select-gallery'; |
| 39 | } |
| 40 | }, |
| 41 | |
| 42 | /** |
| 43 | * Get modal primary button. |
| 44 | */ |
| 45 | getModalButton: function() { |
| 46 | return this.modal[0].getElementsByClassName( 'rl-media-button-select-gallery' )[0]; |
| 47 | }, |
| 48 | |
| 49 | /** |
| 50 | * Open modal. |
| 51 | */ |
| 52 | open: function( galleryID ) { |
| 53 | if ( typeof galleryID === 'undefined' ) |
| 54 | var galleryID = 0; |
| 55 | |
| 56 | var phrase = ''; |
| 57 | |
| 58 | $( this.primaryButtonClass ).show(); |
| 59 | $( this.secondaryButtonClass ).hide(); |
| 60 | |
| 61 | // reset filters? |
| 62 | if ( this.resetFilters ) { |
| 63 | phrase = ''; |
| 64 | |
| 65 | // clear searh input |
| 66 | $( '#rl-media-search-input' ).val( '' ); |
| 67 | |
| 68 | // reset categories |
| 69 | this.modal.find( '#rl-media-attachment-categories' ).val( 0 ); |
| 70 | } else |
| 71 | phrase = $( '#rl-media-search-input' ).val(); |
| 72 | |
| 73 | // display modal |
| 74 | this.modal.show(); |
| 75 | |
| 76 | // fix columns |
| 77 | this.setColumns(); |
| 78 | |
| 79 | // get galleries |
| 80 | this.getGalleries( phrase, galleryID ); |
| 81 | }, |
| 82 | |
| 83 | /** |
| 84 | * Close modal. |
| 85 | */ |
| 86 | close: function( event ) { |
| 87 | event.preventDefault(); |
| 88 | |
| 89 | this.modal.hide(); |
| 90 | }, |
| 91 | |
| 92 | /** |
| 93 | * Calculate column width. |
| 94 | */ |
| 95 | setColumns: function() { |
| 96 | var list = this.modal.find( '.rl-galleries-list' ); |
| 97 | var listWidth = list.width(); |
| 98 | var content = this.modal.find( '.media-frame-content' ); |
| 99 | var columns = parseInt( content.attr( 'data-columns' ) ); |
| 100 | var oldColumns = newColumns = columns; |
| 101 | |
| 102 | if ( listWidth ) { |
| 103 | // get sidebar width |
| 104 | var width = this.modal.find( '.media-sidebar' ).outerWidth() + 'px'; |
| 105 | |
| 106 | // set attachment list new width |
| 107 | list.css( 'right', width ); |
| 108 | |
| 109 | // do the same for primary toolbar |
| 110 | this.modal.find( '.attachments-browser .media-toolbar' ).css( 'right', width ); |
| 111 | |
| 112 | // calculate new columns number |
| 113 | newColumns = Math.min( Math.round( listWidth / 170 ), 12 ) || 1; |
| 114 | |
| 115 | // set new columns number |
| 116 | if ( ! oldColumns || oldColumns !== newColumns ) |
| 117 | content.attr( 'data-columns', newColumns ); |
| 118 | } |
| 119 | }, |
| 120 | |
| 121 | /** |
| 122 | * Click gallery event handler. |
| 123 | */ |
| 124 | handleClickGallery: function( event ) { |
| 125 | event.preventDefault(); |
| 126 | |
| 127 | var gallery = $( event.target ).closest( 'li' ); |
| 128 | |
| 129 | // set current gallery id |
| 130 | this.currentGalleryID = parseInt( gallery.data( 'id' ) ); |
| 131 | |
| 132 | // clicked different gallery? |
| 133 | if ( this.lastGalleryID !== this.currentGalleryID ) { |
| 134 | gallery.parent().find( 'li' ).removeClass( 'selected details' ); |
| 135 | |
| 136 | this.lastGalleryID = this.currentGalleryID; |
| 137 | |
| 138 | // get full source image |
| 139 | var fullSource = gallery.find( '.centered' ).data( 'full-src' ); |
| 140 | |
| 141 | // invalid full source image? |
| 142 | if ( fullSource === '' ) |
| 143 | this.lastGalleryImage = gallery.find( 'img' ).first().attr( 'src' ); |
| 144 | else |
| 145 | this.lastGalleryImage = fullSource; |
| 146 | |
| 147 | gallery.addClass( 'selected details' ); |
| 148 | |
| 149 | this.clickGallery( this.currentGalleryID, false ); |
| 150 | } else { |
| 151 | // already selected? |
| 152 | if ( gallery.hasClass( 'selected details' ) ) { |
| 153 | // unselect gallery |
| 154 | this.currentGalleryID = 0; |
| 155 | |
| 156 | gallery.removeClass( 'selected details' ); |
| 157 | |
| 158 | this.clickGallery( this.currentGalleryID, true ); |
| 159 | } else { |
| 160 | gallery.addClass( 'selected details' ); |
| 161 | |
| 162 | this.clickGallery( this.currentGalleryID, false ); |
| 163 | } |
| 164 | } |
| 165 | }, |
| 166 | |
| 167 | /** |
| 168 | * Load gallery thumbnails from cache or via AJAX. |
| 169 | */ |
| 170 | clickGallery: function( gallery_id, toggle ) { |
| 171 | var _this = this; |
| 172 | |
| 173 | _this.modal.find( '.media-selection' ).toggleClass( 'empty', toggle ); |
| 174 | _this.modal.find( this.primaryButtonClass ).prop( 'disabled', toggle ); |
| 175 | |
| 176 | // load gallery preview images? |
| 177 | if ( ! toggle ) { |
| 178 | // clear images |
| 179 | _this.modal.find( '.rl-attachments-list' ).empty(); |
| 180 | |
| 181 | if ( _this.galleries[gallery_id].inProgress ) { |
| 182 | // display spinner |
| 183 | _this.toggleSpinner( true ); |
| 184 | |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | // load cached images |
| 189 | if ( _this.galleries[gallery_id].ready ) { |
| 190 | // hide spinner |
| 191 | _this.toggleSpinner( false ); |
| 192 | |
| 193 | // update images |
| 194 | _this.updateGalleryPreview( _this.galleries[gallery_id].data, false ); |
| 195 | // get images for the first time |
| 196 | } else { |
| 197 | // display spinner |
| 198 | _this.toggleSpinner( true ); |
| 199 | |
| 200 | // set in progress flag |
| 201 | _this.galleries[gallery_id].inProgress = true; |
| 202 | |
| 203 | $.post( ajaxurl, { |
| 204 | action: 'rl-post-gallery-preview', |
| 205 | post_id: rlArgsGallery.post_id, |
| 206 | gallery_id: gallery_id, |
| 207 | page: rlArgsGallery.page, |
| 208 | nonce: rlArgsGallery.nonce |
| 209 | } ).done( function( response ) { |
| 210 | try { |
| 211 | if ( response.success ) { |
| 212 | // store gallery data |
| 213 | _this.galleries[gallery_id].data = response.data; |
| 214 | |
| 215 | // set ready flag |
| 216 | _this.galleries[gallery_id].ready = true; |
| 217 | |
| 218 | // same gallery? |
| 219 | if ( _this.currentGalleryID === gallery_id ) { |
| 220 | // update gallery data |
| 221 | _this.updateGalleryPreview( _this.galleries[gallery_id].data, true ); |
| 222 | } |
| 223 | } else { |
| 224 | // set ready flag |
| 225 | _this.galleries[gallery_id].ready = false; |
| 226 | } |
| 227 | } catch( e ) { |
| 228 | // set ready flag |
| 229 | _this.galleries[gallery_id].ready = false; |
| 230 | } |
| 231 | } ).fail( function() { |
| 232 | // set ready flag |
| 233 | _this.galleries[gallery_id].ready = false; |
| 234 | } ).always( function() { |
| 235 | // set in progress flag |
| 236 | _this.galleries[gallery_id].inProgress = false; |
| 237 | |
| 238 | // same gallery? |
| 239 | if ( _this.currentGalleryID === gallery_id ) { |
| 240 | // hide spinner |
| 241 | _this.toggleSpinner( false ); |
| 242 | } |
| 243 | } ); |
| 244 | } |
| 245 | } |
| 246 | }, |
| 247 | |
| 248 | /** |
| 249 | * Select gallery (block editor). |
| 250 | */ |
| 251 | selectGallery: function( event ) { |
| 252 | event.preventDefault(); |
| 253 | |
| 254 | if ( $( this ).attr( 'disabled' ) ) |
| 255 | return; |
| 256 | |
| 257 | this.modal.hide(); |
| 258 | }, |
| 259 | |
| 260 | /** |
| 261 | * Insert gallery (classic editor). |
| 262 | */ |
| 263 | insertGallery: function( event ) { |
| 264 | event.preventDefault(); |
| 265 | |
| 266 | if ( $( this ).attr( 'disabled' ) ) |
| 267 | return; |
| 268 | |
| 269 | var shortcode = '[rl_gallery id="' + this.lastGalleryID + '"]'; |
| 270 | var editor = tinyMCE.get( 'content' ); |
| 271 | |
| 272 | if ( editor && ! editor.isHidden() ) |
| 273 | editor.execCommand( 'mceInsertContent', false, shortcode ); |
| 274 | else |
| 275 | wp.media.editor.insert( shortcode ); |
| 276 | |
| 277 | this.modal.hide(); |
| 278 | }, |
| 279 | |
| 280 | /** |
| 281 | * Load galleries. |
| 282 | */ |
| 283 | getGalleries: function( search, galleryID ) { |
| 284 | var modal = this.modal; |
| 285 | var spinner = $( '.rl-gallery-reload-spinner' ); |
| 286 | var galleries = modal.find( '.rl-galleries-list' ); |
| 287 | var _this = this; |
| 288 | |
| 289 | // clear galleries |
| 290 | galleries.empty(); |
| 291 | |
| 292 | // hide gallery info |
| 293 | modal.find( '.media-selection' ).addClass( 'empty' ); |
| 294 | |
| 295 | // clear images |
| 296 | modal.find( '.rl-attachments-list' ).empty(); |
| 297 | |
| 298 | // display spinner |
| 299 | spinner.fadeIn( 'fast' ); |
| 300 | |
| 301 | // get galleries |
| 302 | $.post( ajaxurl, { |
| 303 | action: 'rl-post-get-galleries', |
| 304 | post_id: rlArgsGallery.post_id, |
| 305 | search: search, |
| 306 | page: rlArgsGallery.page, |
| 307 | nonce: rlArgsGallery.nonce, |
| 308 | category: _this.resetFilters ? 0 : modal.find( '#rl-media-attachment-categories' ).val() |
| 309 | } ).done( function( response ) { |
| 310 | try { |
| 311 | if ( response.success ) { |
| 312 | if ( response.data.html !== '' ) { |
| 313 | modal.find( '.rl-no-galleries' ).hide(); |
| 314 | modal.find( '.rl-galleries-list' ).empty().append( response.data.html ); |
| 315 | |
| 316 | // set up galleries |
| 317 | response.data.galleries.forEach( function( gallery_id ) { |
| 318 | _this.galleries[gallery_id] = { |
| 319 | 'inProgress': false, |
| 320 | 'ready': false, |
| 321 | 'data': {} |
| 322 | }; |
| 323 | } ); |
| 324 | |
| 325 | // select gallery |
| 326 | if ( galleryID !== 0 ) |
| 327 | galleries.find( 'li[data-id="' + galleryID + '"] .js--select-attachment' ).trigger( 'click' ); |
| 328 | } else |
| 329 | modal.find( '.rl-no-galleries' ).show(); |
| 330 | } else { |
| 331 | // |
| 332 | } |
| 333 | } catch( e ) { |
| 334 | // |
| 335 | } |
| 336 | } ).always( function() { |
| 337 | // hide spinner |
| 338 | spinner.fadeOut( 'fast' ); |
| 339 | } ); |
| 340 | }, |
| 341 | |
| 342 | /** |
| 343 | * Toggle spinner. |
| 344 | */ |
| 345 | toggleSpinner: function( display ) { |
| 346 | var spinner = this.modal.find( '.rl-gallery-images-spinner' ); |
| 347 | var info = this.modal.find( '.selection-info' ); |
| 348 | |
| 349 | if ( display ) { |
| 350 | // display spinner |
| 351 | spinner.fadeIn( 'fast' ).css( 'visibility', 'visible' ); |
| 352 | |
| 353 | // turn off info |
| 354 | info.addClass( 'rl-loading-content' ); |
| 355 | } else { |
| 356 | // hide spinner |
| 357 | spinner.fadeOut( 'fast' ); |
| 358 | |
| 359 | // turn on info |
| 360 | info.removeClass( 'rl-loading-content' ); |
| 361 | } |
| 362 | }, |
| 363 | |
| 364 | /** |
| 365 | * Clear and load maximum 20 gallery thumbnails. |
| 366 | */ |
| 367 | updateGalleryPreview: function( gallery, animate ) { |
| 368 | // update gallery attachments |
| 369 | this.modal.find( '.rl-attachments-list' ).empty().append( gallery.attachments ).fadeOut( 0 ).delay( animate? 'fast' : 0 ).fadeIn( 0 ); |
| 370 | |
| 371 | // update number of images in gallery |
| 372 | this.modal.find( '.rl-gallery-count' ).text( gallery.count ); |
| 373 | |
| 374 | // update gallery edit link |
| 375 | if ( gallery.edit_url !== '' ) |
| 376 | this.modal.find( '.rl-edit-gallery-link' ).removeClass( 'hidden' ).attr( 'href', gallery.edit_url ); |
| 377 | else |
| 378 | this.modal.find( '.rl-edit-gallery-link' ).addClass( 'hidden' ).attr( 'href', '' ); |
| 379 | }, |
| 380 | |
| 381 | /** |
| 382 | * Reload galleries. |
| 383 | */ |
| 384 | reloadGalleries: function( event ) { |
| 385 | event.preventDefault(); |
| 386 | |
| 387 | // hide "no galleries" box |
| 388 | this.modal.find( '.rl-no-galleries' ).hide(); |
| 389 | |
| 390 | // reset galleries |
| 391 | this.galleries = {}; |
| 392 | |
| 393 | // reset filters |
| 394 | this.resetFilters = false; |
| 395 | |
| 396 | // load galleries |
| 397 | this.getGalleries( $( '#rl-media-search-input' ).val(), 0 ); |
| 398 | }, |
| 399 | |
| 400 | /** |
| 401 | * Bind all events. |
| 402 | */ |
| 403 | bindEvents: function() { |
| 404 | var _this = this; |
| 405 | |
| 406 | // add gallery |
| 407 | $( document ).on( 'click', '#rl-insert-modal-gallery-button', function( e ) { _this.open( 0 ); } ); |
| 408 | |
| 409 | // ready event |
| 410 | $( function() { |
| 411 | _this.modal = $( '#rl-modal-gallery' ); |
| 412 | |
| 413 | // search galleries |
| 414 | _this.modal.on( 'keyup', '#rl-media-search-input', function() { |
| 415 | _this.searchGalleries( $( this ).val() ); |
| 416 | } ); |
| 417 | |
| 418 | // reload galleries |
| 419 | _this.modal.on( 'click', '.rl-reload-galleries', function( e ) { |
| 420 | _this.reloadGalleries( e ); |
| 421 | } ); |
| 422 | |
| 423 | // change category |
| 424 | _this.modal.on( 'change', '#rl-media-attachment-categories', function( e ) { |
| 425 | _this.reloadGalleries( e ); |
| 426 | } ); |
| 427 | |
| 428 | // close gallery |
| 429 | _this.modal.on( 'click', '.media-modal-close, .media-modal-backdrop, .rl-media-button-cancel-gallery', function( e ) { |
| 430 | _this.close( e ); |
| 431 | } ); |
| 432 | |
| 433 | // click gallery |
| 434 | _this.modal.on( 'click', '.rl-galleries-list li .js--select-attachment, .rl-galleries-list li button', function( e ) { |
| 435 | _this.handleClickGallery( e ); |
| 436 | } ); |
| 437 | |
| 438 | // insert gallery (classic editor) |
| 439 | _this.modal.on( 'click', '.rl-media-button-insert-gallery', function( e ) { |
| 440 | _this.insertGallery( e ); |
| 441 | } ); |
| 442 | |
| 443 | // select gallery (block editor) |
| 444 | _this.modal.on( 'click', '.rl-media-button-select-gallery', function( e ) { |
| 445 | _this.selectGallery( e ); |
| 446 | } ); |
| 447 | |
| 448 | // resize window |
| 449 | $( window ).on( 'resize', function() { |
| 450 | _this.setColumns(); |
| 451 | } ); |
| 452 | } ); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | ResponsiveLightboxGallery.init(); |
| 457 | |
| 458 | } )( jQuery ); |