admin-galleries.js
8 years ago
admin-post.js
8 years ago
admin-widgets.js
8 years ago
admin.js
8 years ago
front-basicmasonry.js
8 years ago
front-basicslider.js
8 years ago
front.js
8 years ago
admin-post.js
234 lines
| 1 | ( function ( $ ) { |
| 2 | |
| 3 | $( document ).on( 'ready', function () { |
| 4 | var modal = $( '#rl-modal-gallery' ), |
| 5 | load_delayed_galleries = _.debounce( get_galleries, 500 ), |
| 6 | last_checked_gallery = 0 |
| 7 | galleries = {}; |
| 8 | |
| 9 | // search galleries |
| 10 | $( '#rl-media-search-input' ).on( 'keyup', function () { |
| 11 | load_delayed_galleries( modal, $( this ).val() ); |
| 12 | } ); |
| 13 | |
| 14 | // reload galleries in modal |
| 15 | modal.on( 'click', '.rl-reload-galleries', function ( e ) { |
| 16 | e.preventDefault(); |
| 17 | |
| 18 | // reset galleries |
| 19 | galleries = {}; |
| 20 | |
| 21 | // load galleries |
| 22 | get_galleries( modal ); |
| 23 | } ); |
| 24 | |
| 25 | // close gallery modal |
| 26 | modal.on( 'click', '.media-modal-close, .media-modal-backdrop, .rl-media-button-cancel-gallery', function ( e ) { |
| 27 | e.preventDefault(); |
| 28 | |
| 29 | modal.hide(); |
| 30 | } ); |
| 31 | |
| 32 | // select gallery in modal |
| 33 | modal.on( 'click', '.rl-galleries-list li .js--select-attachment, .rl-galleries-list li button', function ( e ) { |
| 34 | e.preventDefault(); |
| 35 | |
| 36 | var gallery = $( this ).closest( 'li' ), |
| 37 | current_checked_gallery = parseInt( gallery.data( 'id' ) ); |
| 38 | |
| 39 | if ( last_checked_gallery !== current_checked_gallery ) { |
| 40 | gallery.parent().find( 'li' ).removeClass( 'selected details' ); |
| 41 | |
| 42 | last_checked_gallery = current_checked_gallery; |
| 43 | |
| 44 | gallery.addClass( 'selected details' ); |
| 45 | |
| 46 | select_gallery( modal, current_checked_gallery, false ); |
| 47 | } else { |
| 48 | if ( gallery.hasClass( 'selected details' ) ) { |
| 49 | gallery.removeClass( 'selected details' ); |
| 50 | |
| 51 | select_gallery( modal, current_checked_gallery, true ); |
| 52 | } else { |
| 53 | gallery.addClass( 'selected details' ); |
| 54 | |
| 55 | select_gallery( modal, current_checked_gallery, false ); |
| 56 | } |
| 57 | } |
| 58 | } ); |
| 59 | |
| 60 | // insert gallery shortcode handler |
| 61 | modal.on( 'click', '.rl-media-button-insert-gallery', function ( e ) { |
| 62 | e.preventDefault(); |
| 63 | |
| 64 | if ( $( this ).attr( 'disabled' ) ) { |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | var shortcode = '[rl_gallery id="' + last_checked_gallery + '"]'; |
| 69 | editor = tinyMCE.get( 'content' ); |
| 70 | |
| 71 | if ( editor ) { |
| 72 | editor.execCommand( 'mceInsertContent', false, shortcode ); |
| 73 | } else { |
| 74 | wp.media.editor.insert( shortcode ); |
| 75 | } |
| 76 | |
| 77 | modal.hide(); |
| 78 | }); |
| 79 | |
| 80 | // add gallery button handler |
| 81 | $( document ).on( 'click', '#rl-insert-modal-gallery-button', function ( e ) { |
| 82 | e.preventDefault(); |
| 83 | |
| 84 | modal.show(); |
| 85 | |
| 86 | set_columns( modal ); |
| 87 | |
| 88 | get_galleries( modal ); |
| 89 | } ); |
| 90 | |
| 91 | $( window ).on( 'resize', function () { |
| 92 | set_columns( modal ); |
| 93 | } ); |
| 94 | } ); |
| 95 | |
| 96 | // set number of columns |
| 97 | function set_columns( element ) { |
| 98 | var list = element.find( '.rl-galleries-list' ), |
| 99 | list_width = list.width(), |
| 100 | content = element.find( '.media-frame-content' ), |
| 101 | columns = parseInt( content.attr( 'data-columns' ) ), |
| 102 | old_columns = new_columns = columns; |
| 103 | |
| 104 | if ( list_width ) { |
| 105 | var width = element.find( '.media-sidebar' ).outerWidth() + 'px'; |
| 106 | |
| 107 | list.css( 'right', width ); |
| 108 | element.find( '.attachments-browser .media-toolbar' ).css( 'right', width ); |
| 109 | new_columns = Math.min( Math.round( list_width / 170 ), 12 ) || 1; |
| 110 | |
| 111 | if ( ! old_columns || old_columns !== new_columns ) { |
| 112 | content.attr( 'data-columns', new_columns ); |
| 113 | } |
| 114 | } |
| 115 | }; |
| 116 | |
| 117 | // update gallery preview |
| 118 | function update_gallery_preview( element, gallery, animate ) { |
| 119 | // update gallery attachments |
| 120 | element.find( '.rl-attachments-list' ).append( gallery.attachments ).fadeOut( 0 ).delay( animate? 'fast' : 0 ).fadeIn( 0 ); |
| 121 | |
| 122 | // update number of images in gallery |
| 123 | element.find( '.rl-gallery-count' ).text( gallery.count ); |
| 124 | |
| 125 | // update gallery edit link |
| 126 | if ( gallery.edit_url !== '' ) |
| 127 | element.find( '.rl-edit-gallery-link' ).removeClass( 'hidden' ).attr( 'href', gallery.edit_url ); |
| 128 | else |
| 129 | element.find( '.rl-edit-gallery-link' ).addClass( 'hidden' ).attr( 'href', '' ); |
| 130 | } |
| 131 | |
| 132 | // select gallery |
| 133 | function select_gallery( element, gallery_id, toggle ) { |
| 134 | element.find( '.media-selection' ).toggleClass( 'empty', toggle ); |
| 135 | element.find( '.rl-media-button-insert-gallery' ).prop( 'disabled', toggle ); |
| 136 | |
| 137 | // load gallery preview images? |
| 138 | if ( ! toggle ) { |
| 139 | // clear images |
| 140 | element.find( '.rl-attachments-list' ).empty(); |
| 141 | |
| 142 | // load cached images |
| 143 | if ( typeof galleries[gallery_id] !== 'undefined' ) { |
| 144 | // update images |
| 145 | update_gallery_preview( element, galleries[gallery_id], false ); |
| 146 | // get images for the first time |
| 147 | } else { |
| 148 | var spinner = element.find( '.rl-gallery-images-spinner' ), |
| 149 | info = element.find( '.selection-info' ); |
| 150 | |
| 151 | // display spinner |
| 152 | spinner.fadeIn( 'fast' ).css( 'visibility', 'visible' ); |
| 153 | |
| 154 | // turn off info |
| 155 | info.addClass( 'rl-loading-content' ); |
| 156 | |
| 157 | $.post( ajaxurl, { |
| 158 | action: 'rl-post-gallery-preview', |
| 159 | post_id: rlArgs.post_id, |
| 160 | gallery_id: gallery_id, |
| 161 | nonce: rlArgs.nonce |
| 162 | } ).done( function ( response ) { |
| 163 | try { |
| 164 | if ( response.success ) { |
| 165 | // store gallery data |
| 166 | galleries[gallery_id] = response.data; |
| 167 | |
| 168 | // update gallery data |
| 169 | update_gallery_preview( element, galleries[gallery_id], true ); |
| 170 | } else { |
| 171 | //@TODO |
| 172 | } |
| 173 | } catch( e ) { |
| 174 | //@TODO |
| 175 | } |
| 176 | |
| 177 | // hide spinner |
| 178 | spinner.fadeOut( 'fast' ); |
| 179 | |
| 180 | // turn on info |
| 181 | info.removeClass( 'rl-loading-content' ); |
| 182 | } ).fail( function () { |
| 183 | // hide spinner |
| 184 | spinner.fadeOut( 'fast' ); |
| 185 | |
| 186 | // turn on info |
| 187 | info.removeClass( 'rl-loading-content' ); |
| 188 | } ); |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | // get galleries |
| 194 | function get_galleries( element, search = '' ) { |
| 195 | var spinner = $( '.rl-gallery-reload-spinner' ); |
| 196 | |
| 197 | // clear galleries |
| 198 | element.find( '.rl-galleries-list' ).empty(); |
| 199 | |
| 200 | // hide gallery info |
| 201 | element.find( '.media-selection' ).addClass( 'empty' ); |
| 202 | |
| 203 | // clear images |
| 204 | element.find( '.rl-attachments-list' ).empty(); |
| 205 | |
| 206 | // display spinner |
| 207 | spinner.fadeIn( 'fast' ); |
| 208 | |
| 209 | // get galleries |
| 210 | $.post( ajaxurl, { |
| 211 | action: 'rl-post-get-galleries', |
| 212 | post_id: rlArgs.post_id, |
| 213 | search: search, |
| 214 | nonce: rlArgs.nonce |
| 215 | } ).done( function ( response ) { |
| 216 | try { |
| 217 | if ( response.success ) { |
| 218 | element.find( '.rl-galleries-list' ).empty().append( response.data ); |
| 219 | } else { |
| 220 | |
| 221 | } |
| 222 | } catch( e ) { |
| 223 | |
| 224 | } |
| 225 | |
| 226 | // hide spinner |
| 227 | spinner.fadeOut( 'fast' ); |
| 228 | } ).fail( function () { |
| 229 | // hide spinner |
| 230 | spinner.fadeOut( 'fast' ); |
| 231 | } ); |
| 232 | } |
| 233 | |
| 234 | } )( jQuery ); |