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-galleries.js
511 lines
| 1 | ( function ( $ ) { |
| 2 | |
| 3 | $( document ).on( 'ready', function () { |
| 4 | |
| 5 | var gallery_frame = null, |
| 6 | attachment_frame = null, |
| 7 | gallery_container = $( '.rl-gallery-images' ), |
| 8 | gallery_ids = $( '.rl-gallery-ids' ); |
| 9 | |
| 10 | media_gallery_sortable( gallery_container, gallery_ids, $( 'input[name="rl_gallery[images][menu_item]"]:checked' ).val() ); |
| 11 | |
| 12 | // color picker |
| 13 | $( '.rl-gallery-tab-content .color-picker' ).wpColorPicker(); |
| 14 | |
| 15 | // make sure HTML5 validation is turned off |
| 16 | $( 'form#post' ).attr( 'novalidate', 'novalidate' ); |
| 17 | |
| 18 | // make sure to dispay images metabox at start |
| 19 | $( '#responsive-gallery-images' ).show(); |
| 20 | |
| 21 | // move navigation tabs and metaboxes to second postbox container to fix mobile devices problem |
| 22 | $( '.rl-display-metabox, .rl-hide-metabox, h2.nav-tab-wrapper' ).prependTo( '#postbox-container-2' ); |
| 23 | |
| 24 | // change navigation menu |
| 25 | $( document ).on( 'change', '.rl-gallery-tab-menu-item', function () { |
| 26 | var tab = $( this ).closest( '.postbox' ).attr( 'id' ).replace( 'responsive-gallery-', '' ), |
| 27 | source = $( this ).closest( '.rl-gallery-tab-menu' ), |
| 28 | container = $( this ).closest( '.inside' ).find( '.rl-gallery-tab-content' ), |
| 29 | spinner = source.find( '.spinner' ), |
| 30 | menu_item = $( this ).val(); |
| 31 | |
| 32 | // disable nav on ajax |
| 33 | container.addClass( 'rl-loading-content' ); |
| 34 | source.addClass( 'rl-loading-content' ); |
| 35 | |
| 36 | // display spinner |
| 37 | spinner.fadeIn( 'fast' ).css( 'visibility', 'visible' ); |
| 38 | |
| 39 | // post ajax request |
| 40 | $.post( ajaxurl, { |
| 41 | action: 'rl-get-menu-content', |
| 42 | post_id: rlArgs.post_id, |
| 43 | tab: tab, |
| 44 | menu_item: menu_item, |
| 45 | nonce: rlArgs.nonce |
| 46 | } ).done( function ( response ) { |
| 47 | try { |
| 48 | if ( response.success ) { |
| 49 | // replace HTML |
| 50 | container.html( response.data ); |
| 51 | |
| 52 | // enable nav after ajax |
| 53 | container.removeClass( 'rl-loading-content' ); |
| 54 | source.removeClass( 'rl-loading-content' ); |
| 55 | |
| 56 | // update gallery data |
| 57 | gallery_frame = null; |
| 58 | gallery_container = $( '.rl-gallery-images' ); |
| 59 | gallery_ids = $( '.rl-gallery-ids' ); |
| 60 | |
| 61 | // refresh sortable only for media library |
| 62 | media_gallery_sortable( gallery_container, gallery_ids, menu_item ); |
| 63 | |
| 64 | // color picker |
| 65 | container.find( '.color-picker' ).wpColorPicker(); |
| 66 | } else { |
| 67 | // @todo |
| 68 | } |
| 69 | } catch ( e ) { |
| 70 | // @todo |
| 71 | } |
| 72 | |
| 73 | // hide spinner |
| 74 | spinner.fadeOut( 'fast' ); |
| 75 | } ).fail( function () { |
| 76 | // hide spinner |
| 77 | spinner.fadeOut( 'fast' ); |
| 78 | } ); |
| 79 | } ); |
| 80 | |
| 81 | // change navigation menu |
| 82 | $( document ).on( 'click', '.nav-tab', function ( e ) { |
| 83 | e.preventDefault(); |
| 84 | |
| 85 | var anchor = $( this ).attr( 'href' ).substr( 1 ); |
| 86 | |
| 87 | // remove active class |
| 88 | $( '.nav-tab' ).removeClass( 'nav-tab-active' ); |
| 89 | |
| 90 | // add active class |
| 91 | $( this ).addClass( 'nav-tab-active' ); |
| 92 | |
| 93 | // hide all normal metaboxes |
| 94 | $( '#postbox-container-2 div[id^="responsive-gallery-"]' ).removeClass( 'rl-display-metabox' ).addClass( 'rl-hide-metabox' ); |
| 95 | |
| 96 | // display needed metabox |
| 97 | if ( anchor === '' ) { |
| 98 | $( '#responsive-gallery-images' ).addClass( 'rl-display-metabox' ).removeClass( 'rl-hide-metabox' ); |
| 99 | } else { |
| 100 | $( '#responsive-gallery-' + anchor ).addClass( 'rl-display-metabox' ).removeClass( 'rl-hide-metabox' ); |
| 101 | } |
| 102 | |
| 103 | $( 'input[name="rl_active_tab"]' ).val( anchor ); |
| 104 | } ); |
| 105 | |
| 106 | $( '.rl-shortcode' ).on( 'click', function () { |
| 107 | $( this ).select(); |
| 108 | } ); |
| 109 | |
| 110 | // remove image |
| 111 | $( document ).on( 'click', '.rl-gallery-image-remove', function ( e ) { |
| 112 | e.preventDefault(); |
| 113 | |
| 114 | // prevent featured images being removed |
| 115 | if ( $( this ).closest( '.rl-gallery-images-featured' ).length === 1 ) { |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | var li = $( this ).closest( 'li.rl-gallery-image' ), |
| 120 | attachment_ids = get_current_attachments( gallery_ids ); |
| 121 | |
| 122 | // remove id |
| 123 | attachment_ids = _.without( attachment_ids, parseInt( li.data( 'attachment_id' ) ) ); |
| 124 | |
| 125 | // remove attachment |
| 126 | li.remove(); |
| 127 | |
| 128 | // update attachment ids |
| 129 | gallery_ids.val( $.unique( attachment_ids ).join( ',' ) ); |
| 130 | |
| 131 | return false; |
| 132 | } ); |
| 133 | |
| 134 | // edit image |
| 135 | $( document ).on( 'click', '.rl-gallery-image-edit', function ( e ) { |
| 136 | e.preventDefault(); |
| 137 | |
| 138 | var li = $( this ).closest( 'li.rl-gallery-image' ), |
| 139 | attachment_id = parseInt( li.data( 'attachment_id' ) ), |
| 140 | attachment_changed = false; |
| 141 | |
| 142 | // frame already exists? |
| 143 | if ( attachment_frame !== null ) { |
| 144 | attachment_frame.detach(); |
| 145 | attachment_frame.dispose(); |
| 146 | attachment_frame = null; |
| 147 | } |
| 148 | |
| 149 | // create new frame |
| 150 | attachment_frame = wp.media( { |
| 151 | id: 'rl-edit-attachment-modal', |
| 152 | frame: 'select', |
| 153 | uploader: false, |
| 154 | multiple: false, |
| 155 | title: rlArgs.editTitle, |
| 156 | library: { |
| 157 | post__in: attachment_id |
| 158 | }, |
| 159 | button: { |
| 160 | text: rlArgs.buttonEditFile |
| 161 | } |
| 162 | } ).on( 'open', function () { |
| 163 | var attachment = wp.media.attachment( attachment_id ), |
| 164 | selection = attachment_frame.state().get( 'selection' ); |
| 165 | |
| 166 | attachment_frame.$el.closest( '.media-modal' ).addClass( 'rl-edit-modal' ); |
| 167 | |
| 168 | // get attachment |
| 169 | attachment.fetch(); |
| 170 | |
| 171 | // reset selection |
| 172 | //selection.reset(); |
| 173 | |
| 174 | // add attachment |
| 175 | selection.add( attachment ); |
| 176 | } ); |
| 177 | |
| 178 | attachment_frame.open(); |
| 179 | |
| 180 | return false; |
| 181 | } ); |
| 182 | |
| 183 | // change image status |
| 184 | $( document ).on( 'click', '.rl-gallery-image-status', function ( e ) { |
| 185 | e.preventDefault(); |
| 186 | |
| 187 | var li = $( this ).closest( 'li.rl-gallery-image' ), |
| 188 | input = li.find( '.rl-gallery-exclude' ), |
| 189 | status = li.hasClass( 'rl-status-active' ), |
| 190 | id = parseInt( li.data( 'attachment_id' ) ), |
| 191 | item = ''; |
| 192 | |
| 193 | if ( id > 0 ) |
| 194 | item = id; |
| 195 | else |
| 196 | item = li.find( '.rl-gallery-inner img' ).attr( 'src' ); |
| 197 | |
| 198 | // exclude? |
| 199 | if ( status ) { |
| 200 | li.addClass( 'rl-status-inactive' ).removeClass( 'rl-status-active' ); |
| 201 | |
| 202 | // add item |
| 203 | input.val( item ); |
| 204 | } else { |
| 205 | li.addClass( 'rl-status-active' ).removeClass( 'rl-status-inactive' ); |
| 206 | |
| 207 | // remove item |
| 208 | input.val( '' ); |
| 209 | } |
| 210 | |
| 211 | return false; |
| 212 | } ); |
| 213 | |
| 214 | // open the modal on click |
| 215 | $( document ).on( 'click', '.rl-gallery-select', function ( e ) { |
| 216 | e.preventDefault(); |
| 217 | |
| 218 | // open media frame if already exists |
| 219 | if ( gallery_frame !== null ) { |
| 220 | gallery_frame.open(); |
| 221 | |
| 222 | return; |
| 223 | } |
| 224 | |
| 225 | // create the media frame |
| 226 | gallery_frame = wp.media( { |
| 227 | title: rlArgs.textSelectImages, |
| 228 | multiple: 'add', |
| 229 | autoSelect: true, |
| 230 | library: { |
| 231 | type: 'image' |
| 232 | }, |
| 233 | button: { |
| 234 | text: rlArgs.textUseImages |
| 235 | } |
| 236 | } ).on( 'open', function () { |
| 237 | var selection = gallery_frame.state().get( 'selection' ), |
| 238 | attachment_ids = get_current_attachments( gallery_ids ); |
| 239 | |
| 240 | // deselect all attachments |
| 241 | selection.reset(); |
| 242 | |
| 243 | $.each( attachment_ids, function () { |
| 244 | // prepare attachment |
| 245 | attachment = wp.media.attachment( this ); |
| 246 | |
| 247 | // select attachment |
| 248 | selection.add( attachment ? [ attachment ] : [ ] ); |
| 249 | } ); |
| 250 | } ).on( 'select', function () { |
| 251 | var selection = gallery_frame.state().get( 'selection' ), |
| 252 | attachment_ids = get_current_attachments( gallery_ids ), |
| 253 | selected_ids = [ ]; |
| 254 | |
| 255 | if ( selection ) { |
| 256 | selection.map( function ( attachment ) { |
| 257 | if ( attachment.id ) { |
| 258 | // add attachment |
| 259 | selected_ids.push( attachment.id ); |
| 260 | |
| 261 | // is image already in gallery? |
| 262 | if ( $.inArray( attachment.id, attachment_ids ) !== -1 ) { |
| 263 | return; |
| 264 | } |
| 265 | |
| 266 | // add attachment |
| 267 | attachment_ids.push( attachment.id ); |
| 268 | attachment = attachment.toJSON(); |
| 269 | |
| 270 | // is preview size available? |
| 271 | if ( attachment.sizes && attachment.sizes['thumbnail'] ) { |
| 272 | attachment.url = attachment.sizes['thumbnail'].url; |
| 273 | } |
| 274 | |
| 275 | // append new image |
| 276 | gallery_container.append( rlArgs.mediaItemTemplate.replace( /__IMAGE_ID__/g, attachment.id ).replace( /__IMAGE__/g, '<img width="150" height="150" src="' + attachment.url + '" class="attachment-thumbnail size-thumbnail" alt="" sizes="(max-width: 150px) 100vw, 150px" />' ).replace( /__IMAGE_STATUS__/g, 'rl-status-active' ) ); |
| 277 | } |
| 278 | } ); |
| 279 | } |
| 280 | |
| 281 | // assign copy of attachment ids |
| 282 | var copy = attachment_ids; |
| 283 | |
| 284 | for ( var i = 0; i < attachment_ids.length; i++ ) { |
| 285 | // unselected image? |
| 286 | if ( $.inArray( attachment_ids[i], selected_ids ) === -1 ) { |
| 287 | gallery_container.find( 'li.rl-gallery-image[data-attachment_id="' + attachment_ids[i] + '"]' ).remove(); |
| 288 | |
| 289 | copy = _.without( copy, attachment_ids[i] ); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | gallery_ids.val( $.unique( copy ).join( ',' ) ); |
| 294 | } ); |
| 295 | |
| 296 | // open media frame |
| 297 | gallery_frame.open(); |
| 298 | } ); |
| 299 | |
| 300 | // preview |
| 301 | $( document ).on( 'click', '.rl-gallery-update-preview', function ( e ) { |
| 302 | e.preventDefault(); |
| 303 | |
| 304 | var container = $( '.rl-gallery-tab-inside-images-featured' ), |
| 305 | parent = $( this ).parent(), |
| 306 | spinner = parent.find( '.spinner' ), |
| 307 | rules = $( '.rl-rules-groups > .rl-rules-group' ), |
| 308 | query_args = {}; |
| 309 | |
| 310 | container.find( 'tr[data-field_type]' ).each( function() { |
| 311 | var el = $( this ), |
| 312 | field_name = el.data( 'field_name' ), |
| 313 | value = null; |
| 314 | |
| 315 | switch ( el.data( 'field_type' ) ) { |
| 316 | case 'number': |
| 317 | value = parseInt( el.find( 'input' ).val() ); |
| 318 | |
| 319 | if ( ! value ) { |
| 320 | value = 0; |
| 321 | } |
| 322 | console.log |
| 323 | break; |
| 324 | |
| 325 | case 'select': |
| 326 | value = el.find( 'select option:selected' ).val(); |
| 327 | |
| 328 | if ( ! value ) { |
| 329 | value = ''; |
| 330 | } |
| 331 | break; |
| 332 | |
| 333 | case 'radio': |
| 334 | value = el.find( 'input:checked' ).val(); |
| 335 | |
| 336 | if ( ! value ) { |
| 337 | value = ''; |
| 338 | } |
| 339 | break; |
| 340 | |
| 341 | case 'multiselect': |
| 342 | value = el.find( 'select' ).val(); |
| 343 | |
| 344 | if ( ! value ) { |
| 345 | value = []; |
| 346 | } |
| 347 | break; |
| 348 | } |
| 349 | |
| 350 | query_args[field_name] = value; |
| 351 | } ); |
| 352 | |
| 353 | // disable ui |
| 354 | // container.addClass( 'rl-loading-content' ); |
| 355 | |
| 356 | // display spinner |
| 357 | spinner.fadeIn( 'fast' ).css( 'visibility', 'visible' ); |
| 358 | |
| 359 | // post ajax request |
| 360 | $.post( ajaxurl, { |
| 361 | action: 'rl-get-preview-content', |
| 362 | post_id: rlArgs.post_id, |
| 363 | menu_item: $( '.rl-gallery-tab-menu-images input:checked' ).val(), |
| 364 | query: query_args, |
| 365 | excluded: container.find( '.rl-gallery-exclude-ids' ).val(), |
| 366 | nonce: rlArgs.nonce |
| 367 | } ).done( function ( response ) { |
| 368 | try { |
| 369 | if ( response.success ) { |
| 370 | $( '.rl-gallery-images' ).empty().append( response.data ); |
| 371 | } else { |
| 372 | // @todo |
| 373 | } |
| 374 | } catch ( e ) { |
| 375 | // @todo |
| 376 | } |
| 377 | |
| 378 | // hide spinner |
| 379 | spinner.fadeOut( 'fast' ); |
| 380 | } ).fail( function () { |
| 381 | // hide spinner |
| 382 | spinner.fadeOut( 'fast' ); |
| 383 | } ); |
| 384 | } ); |
| 385 | |
| 386 | // load values for specified rule |
| 387 | $( document ).on( 'change', '.rl-rule-type', function () { |
| 388 | var _this = $( this ), |
| 389 | td = _this.closest( 'tr' ).find( 'td.value' ), |
| 390 | select = td.find( 'select' ), |
| 391 | spinner = td.find( '.spinner' ); |
| 392 | |
| 393 | select.hide(); |
| 394 | spinner.fadeIn( 'fast' ).css( 'visibility', 'visible' ); |
| 395 | |
| 396 | $.post( ajaxurl, { |
| 397 | action: 'rl-get-group-rules-values', |
| 398 | type: _this.val(), |
| 399 | nonce: rlArgs.nonce |
| 400 | } ).done( function ( data ) { |
| 401 | spinner.hide(); |
| 402 | |
| 403 | try { |
| 404 | var response = JSON.parse( data ); |
| 405 | |
| 406 | // remove old select options and adds new ones |
| 407 | select.fadeIn( 'fast' ).find( 'option, optgroup' ).remove().end().append( response.select ); |
| 408 | } catch ( e ) { |
| 409 | // |
| 410 | } |
| 411 | } ).fail( function () { |
| 412 | // |
| 413 | } ); |
| 414 | } ); |
| 415 | |
| 416 | } ); |
| 417 | |
| 418 | // listen for insert/remove media library thumbnail |
| 419 | $( document ).on( 'DOMNodeInserted', '#postimagediv .inside', function ( e ) { |
| 420 | var value = $( '#postimagediv .inside' ).attr( 'data-featured-type' ); |
| 421 | |
| 422 | if ( $( '#rl-gallery-featured-' + value ).length > 0 ) { |
| 423 | $( '#rl-gallery-featured-' + value ).prop( 'checked', true ); |
| 424 | |
| 425 | $( '#postimagediv .inside' ).trigger( 'change' ); |
| 426 | } |
| 427 | } ); |
| 428 | |
| 429 | // handle featured image change |
| 430 | $( document ).on( 'change', '#postimagediv .inside', function () { |
| 431 | var el = $( this ).find( 'input[name="rl_gallery_featured_image"]:checked' ), |
| 432 | value = $( el ).val(); |
| 433 | |
| 434 | $( '#postimagediv .inside' ).attr( 'data-featured-type', value ); |
| 435 | $( '.rl-gallery-featured-image-select' ).children( 'div' ).hide(); |
| 436 | $( '.rl-gallery-featured-image-select-' + value ).show(); |
| 437 | |
| 438 | // media library |
| 439 | if ( value === 'id' ) { |
| 440 | var thumbnail_id = parseInt( $( '#_thumbnail_id' ).attr( 'data-featured-id' ) ); |
| 441 | if ( thumbnail_id > 0 ) { |
| 442 | $( '#_thumbnail_id' ).val( thumbnail_id ).attr( 'data-featured-id', -1 ); |
| 443 | } |
| 444 | // custom URL |
| 445 | } else if ( value === 'url' ) { |
| 446 | var thumbnail_id = parseInt( $( '#_thumbnail_id' ).val() ); |
| 447 | if ( thumbnail_id > 0 ) { |
| 448 | $( '#_thumbnail_id' ).attr( 'data-featured-id', thumbnail_id ).val( -1 ); |
| 449 | } |
| 450 | // first gallery image |
| 451 | } else { |
| 452 | var thumbnail_id = parseInt( $( '#_thumbnail_id' ).val() ); |
| 453 | if ( thumbnail_id > 0 ) { |
| 454 | $( '#_thumbnail_id' ).attr( 'data-featured-id', thumbnail_id ).val( -1 ); |
| 455 | } |
| 456 | } |
| 457 | } ); |
| 458 | |
| 459 | $( document ).on( 'ready ajaxComplete', function () { |
| 460 | // init select2 |
| 461 | $( '.rl-gallery-tab-inside select.select2' ).select2( { |
| 462 | closeOnSelect: true, |
| 463 | multiple: true, |
| 464 | width: 300, |
| 465 | minimumInputLength: 0 |
| 466 | } ); |
| 467 | } ); |
| 468 | |
| 469 | // get attachment ids |
| 470 | function get_current_attachments( gallery_ids ) { |
| 471 | var attachments = gallery_ids.val(); |
| 472 | |
| 473 | // return integer image ids or empty array |
| 474 | return attachments !== '' ? attachments.split( ',' ).map( function ( i ) { |
| 475 | return parseInt( i ) |
| 476 | } ) : [ ]; |
| 477 | } |
| 478 | |
| 479 | // |
| 480 | function media_gallery_sortable( gallery, ids, type ) { |
| 481 | if ( type === 'media' ) { |
| 482 | // images order |
| 483 | gallery.sortable( { |
| 484 | items: 'li.rl-gallery-image', |
| 485 | cursor: 'move', |
| 486 | scrollSensitivity: 40, |
| 487 | forcePlaceholderSize: true, |
| 488 | forceHelperSize: false, |
| 489 | helper: 'clone', |
| 490 | opacity: 0.65, |
| 491 | placeholder: 'rl-gallery-sortable-placeholder', |
| 492 | start: function ( event, ui ) { |
| 493 | ui.item.css( 'border-color', '#f6f6f6' ); |
| 494 | }, |
| 495 | stop: function ( event, ui ) { |
| 496 | ui.item.removeAttr( 'style' ); |
| 497 | }, |
| 498 | update: function ( event, ui ) { |
| 499 | var attachment_ids = [ ]; |
| 500 | |
| 501 | gallery.find( 'li.rl-gallery-image' ).each( function () { |
| 502 | attachment_ids.push( parseInt( $( this ).attr( 'data-attachment_id' ) ) ); |
| 503 | } ); |
| 504 | |
| 505 | ids.val( $.unique( attachment_ids ).join( ',' ) ); |
| 506 | } |
| 507 | } ); |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | } )( jQuery ); |