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-widgets.js
246 lines
| 1 | ( function ( window, $ ) { |
| 2 | |
| 3 | /* Gallery widget */ |
| 4 | |
| 5 | var rl_galleries = [ ], |
| 6 | rl_gallery_frames = [ ], |
| 7 | rl_gallery_ids = [ ], |
| 8 | rl_gallery_images = [ ]; |
| 9 | |
| 10 | $( document ).on( 'ready widget-updated widget-added', function () { |
| 11 | |
| 12 | rl_galleries = $( '.rl-gallery-widget' ); |
| 13 | |
| 14 | if ( rl_galleries.length > 0 ) { |
| 15 | $.each( rl_galleries, function ( index, gallery ) { |
| 16 | var gallery_id_attr = $( gallery ).attr( 'id' ), |
| 17 | gallery_id_arr = gallery_id_attr.match( /\-\d*\-/ ); |
| 18 | |
| 19 | if ( gallery_id_arr != null ) { |
| 20 | var gallery_id = gallery_id_arr.shift().replace( /-/g, '' ); |
| 21 | |
| 22 | rl_gallery_frames[gallery_id] = [ ]; |
| 23 | rl_gallery_ids[gallery_id] = $( gallery ).find( '.rl-gallery-ids' ); |
| 24 | rl_gallery_images[gallery_id] = $( gallery ).find( '.rl-gallery-images' ); |
| 25 | |
| 26 | // image ordering |
| 27 | rl_gallery_images[gallery_id].sortable( { |
| 28 | items: 'li.rl-gallery-image', |
| 29 | cursor: 'move', |
| 30 | scrollSensitivity: 40, |
| 31 | forcePlaceholderSize: true, |
| 32 | forceHelperSize: false, |
| 33 | helper: 'clone', |
| 34 | opacity: 0.65, |
| 35 | placeholder: 'rl-gallery-sortable-placeholder', |
| 36 | start: function ( event, ui ) { |
| 37 | ui.item.css( 'border-color', '#f6f6f6' ); |
| 38 | }, |
| 39 | stop: function ( event, ui ) { |
| 40 | ui.item.removeAttr( 'style' ); |
| 41 | }, |
| 42 | update: function ( event, ui ) { |
| 43 | var attachment_ids = [ ]; |
| 44 | |
| 45 | $( rl_gallery_images[gallery_id] ).find( 'li.rl-gallery-image' ).each( function () { |
| 46 | var attachment_id = jQuery( this ).attr( 'data-attachment_id' ); |
| 47 | attachment_ids.push( attachment_id ); |
| 48 | } ); |
| 49 | |
| 50 | rl_gallery_ids[gallery_id].val( attachment_ids.join( ',' ) ); |
| 51 | } |
| 52 | } ); |
| 53 | |
| 54 | // removing images |
| 55 | $( rl_gallery_images[gallery_id] ).on( 'click', '.rl-gallery-image-remove', function () { |
| 56 | var li = $( this ).closest( 'li.rl-gallery-image' ), |
| 57 | attachment_ids = rl_gallery_ids[gallery_id].val().split( ',' ).map( function ( el ) { |
| 58 | return parseInt( el ) |
| 59 | } ); |
| 60 | |
| 61 | // remove id |
| 62 | attachment_ids = _.without( attachment_ids, parseInt( li.data( 'attachment_id' ) ) ); |
| 63 | |
| 64 | // remove attachment |
| 65 | li.remove(); |
| 66 | |
| 67 | // update attachment ids |
| 68 | rl_gallery_ids[gallery_id].val( attachment_ids.join( ',' ) ); |
| 69 | |
| 70 | return false; |
| 71 | } ); |
| 72 | } |
| 73 | } ); |
| 74 | } |
| 75 | ; |
| 76 | |
| 77 | // open the modal on click |
| 78 | $( '.rl-gallery-widget-select' ).on( 'click', function ( event ) { |
| 79 | event.preventDefault(); |
| 80 | |
| 81 | var gallery = $( this ).closest( '.rl-gallery-widget' ), |
| 82 | gallery_id = 0, |
| 83 | gallery_id_attr = $( gallery ).attr( 'id' ), |
| 84 | gallery_id_arr = gallery_id_attr.match( /\-\d*\-/ ); |
| 85 | |
| 86 | if ( gallery_id_arr != null ) { |
| 87 | gallery_id = gallery_id_arr.shift().replace( /-/g, '' ); |
| 88 | |
| 89 | var attachment_ids = rl_gallery_ids[gallery_id].val(); |
| 90 | |
| 91 | // if the media frame already exists, reopen it. |
| 92 | if ( rl_gallery_frames[gallery_id].length != 0 ) { |
| 93 | rl_gallery_frames[gallery_id].open(); |
| 94 | |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | // create the media frame. |
| 99 | rl_gallery_frames[gallery_id] = wp.media( { |
| 100 | // Set the title of the modal. |
| 101 | title: rlArgs.textSelectImages, |
| 102 | multiple: true, |
| 103 | button: { |
| 104 | text: rlArgs.textUseImages |
| 105 | }, |
| 106 | library: { type: 'image' }, |
| 107 | multiple: true |
| 108 | } ); |
| 109 | |
| 110 | rl_gallery_frames[gallery_id].on( 'open', function () { |
| 111 | var selection = rl_gallery_frames[gallery_id].state().get( 'selection' ), |
| 112 | attachment_ids = rl_gallery_ids[gallery_id].val().split( ',' ); |
| 113 | |
| 114 | $.each( attachment_ids, function () { |
| 115 | if ( $.isNumeric( this ) ) { |
| 116 | attachment = wp.media.attachment( this ); |
| 117 | attachment.fetch(); |
| 118 | selection.add( attachment ? [ attachment ] : [ ] ); |
| 119 | } |
| 120 | } ); |
| 121 | } ); |
| 122 | |
| 123 | // when an image is selected, run a callback. |
| 124 | rl_gallery_frames[gallery_id].on( 'select', function () { |
| 125 | var selection = rl_gallery_frames[gallery_id].state().get( 'selection' ), |
| 126 | selected_ids = [ ], |
| 127 | attachment_ids = rl_gallery_ids[gallery_id].val().split( ',' ).map( function ( el ) { |
| 128 | return parseInt( el ) |
| 129 | } ); |
| 130 | |
| 131 | if ( selection ) { |
| 132 | selection.map( function ( attachment ) { |
| 133 | if ( attachment.id ) { |
| 134 | selected_ids.push( attachment.id ); |
| 135 | |
| 136 | // is image already in gallery? |
| 137 | if ( $.inArray( attachment.id, attachment_ids ) !== -1 ) { |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | attachment_ids.push( attachment.id ); |
| 142 | attachment = attachment.toJSON(); |
| 143 | |
| 144 | // is preview size available? |
| 145 | if ( attachment.sizes && attachment.sizes['thumbnail'] ) { |
| 146 | attachment.url = attachment.sizes['thumbnail'].url; |
| 147 | } |
| 148 | |
| 149 | rl_gallery_images[gallery_id].append( '\ |
| 150 | <li class="rl-gallery-image" data-attachment_id="' + attachment.id + '">\ |
| 151 | <div class="rl-gallery-inner"><img src="' + attachment.url + '" /></div>\ |
| 152 | <div class="rl-gallery-actions"><a href="#" class="rl-gallery-image-remove dashicons dashicons-no" title="' + rlArgs.textRemoveImage + '"></a></div>\ |
| 153 | </li>' |
| 154 | ); |
| 155 | } |
| 156 | } ); |
| 157 | } |
| 158 | |
| 159 | // assign copy of attachments ids |
| 160 | var copy = attachment_ids; |
| 161 | |
| 162 | for ( var i = 0; i < attachment_ids.length; i++ ) { |
| 163 | // unselected image? |
| 164 | if ( $.inArray( attachment_ids[i], selected_ids ) === -1 ) { |
| 165 | $( rl_gallery_images[gallery_id] ).find( 'li.rl-gallery-image[data-attachment_id="' + attachment_ids[i] + '"]' ).remove(); |
| 166 | |
| 167 | copy = _.without( copy, attachment_ids[i] ) |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | rl_gallery_ids[gallery_id].val( $.unique( copy ).join( ',' ) ); |
| 172 | } ); |
| 173 | |
| 174 | // finally, open the modal. |
| 175 | rl_gallery_frames[gallery_id].open(); |
| 176 | } |
| 177 | ; |
| 178 | } ); |
| 179 | |
| 180 | } ); |
| 181 | |
| 182 | /* Single image widget */ |
| 183 | |
| 184 | var rl_image_frame, |
| 185 | rl_image_container, |
| 186 | rl_image_input; |
| 187 | |
| 188 | $( document ).on( 'ready widget-updated widget-added', function () { |
| 189 | |
| 190 | $( '.rl-image-widget-select' ).on( 'click', function ( event ) { |
| 191 | |
| 192 | var $this = $( this ); |
| 193 | rl_image_container = $this.parent().find( '.rl-image-widget-content' ); |
| 194 | rl_image_input = $this.parent().find( '.rl-image-widget-image-id' ); |
| 195 | |
| 196 | event.preventDefault(); |
| 197 | |
| 198 | // create a new media frame |
| 199 | rl_image_frame = wp.media( { |
| 200 | title: rlArgs.textSelectImage, |
| 201 | button: { |
| 202 | text: rlArgs.textUseImage |
| 203 | }, |
| 204 | library: { type: 'image' }, |
| 205 | multiple: false |
| 206 | } ); |
| 207 | |
| 208 | // when an image is selected in the media frame... |
| 209 | rl_image_frame.on( 'select', function () { |
| 210 | // get media attachment details from the frame state |
| 211 | var attachment = rl_image_frame.state().get( 'selection' ).first().toJSON(); |
| 212 | |
| 213 | // send the attachment URL to our custom image input field. |
| 214 | rl_image_container.html( '<img src="' + attachment.url + '" alt="" />' ); |
| 215 | |
| 216 | // send the attachment id to our hidden input |
| 217 | rl_image_input.val( attachment.id ); |
| 218 | } ); |
| 219 | |
| 220 | // when an image is already selected in the media frame... |
| 221 | rl_image_frame.on( 'open', function () { |
| 222 | var selection = rl_image_frame.state().get( 'selection' ); |
| 223 | |
| 224 | // get current image |
| 225 | var attachment = wp.media.attachment( rl_image_input.val() ); |
| 226 | attachment.fetch(); |
| 227 | |
| 228 | // preselect in media frame |
| 229 | selection.add( attachment ? [ attachment ] : [ ] ); |
| 230 | } ); |
| 231 | |
| 232 | rl_image_frame.open(); |
| 233 | |
| 234 | } ); |
| 235 | |
| 236 | $( '.rl-image-link-to' ).on( 'change', function() { |
| 237 | if ( $( this ).val() == 'custom' ) { |
| 238 | $( this ).parent().next().slideDown( 'fast' ); |
| 239 | } else { |
| 240 | $( this ).parent().next().slideUp( 'fast' ); |
| 241 | } |
| 242 | } ); |
| 243 | |
| 244 | } ); |
| 245 | |
| 246 | } )( this, jQuery ); |