admin-folders.js
5 years ago
admin-galleries.js
5 years ago
admin-gallery.js
5 years ago
admin-media.js
5 years ago
admin-plugins.js
5 years ago
admin-widgets.js
5 years ago
admin.js
5 years ago
front-basicmasonry.js
5 years ago
front-basicslider.js
5 years ago
front.js
5 years ago
gutenberg.min.js
5 years ago
admin-galleries.js
700 lines
| 1 | ( function( $ ) { |
| 2 | |
| 3 | // ready event |
| 4 | $( function() { |
| 5 | var gallery_frame = null, |
| 6 | attachment_frame = null, |
| 7 | gallery_container = $( '.rl-gallery-images' ), |
| 8 | gallery_ids = $( '.rl-gallery-ids' ), |
| 9 | wikimedia = { '1': '' }; |
| 10 | |
| 11 | media_gallery_sortable( gallery_container, gallery_ids, $( 'input[name="rl_gallery[images][menu_item]"]:checked' ).val() ); |
| 12 | |
| 13 | // color picker |
| 14 | $( '.rl-gallery-tab-content .color-picker' ).wpColorPicker(); |
| 15 | |
| 16 | // make sure HTML5 validation is turned off |
| 17 | $( 'form#post' ).attr( 'novalidate', 'novalidate' ); |
| 18 | |
| 19 | // make sure to dispay images metabox at start |
| 20 | $( '#responsive-gallery-images' ).show(); |
| 21 | |
| 22 | // move navigation tabs and metaboxes to second postbox container to fix mobile devices problem |
| 23 | $( '.rl-display-metabox, .rl-hide-metabox, h2.nav-tab-wrapper' ).prependTo( '#postbox-container-2' ); |
| 24 | |
| 25 | init_select2(); |
| 26 | |
| 27 | // change navigation menu |
| 28 | $( document ).on( 'change', '.rl-gallery-tab-menu-item', function() { |
| 29 | var tab = $( this ).closest( '.postbox' ).attr( 'id' ).replace( 'responsive-gallery-', '' ), |
| 30 | source = $( this ).closest( '.rl-gallery-tab-menu' ), |
| 31 | container = $( this ).closest( '.inside' ).find( '.rl-gallery-tab-content' ), |
| 32 | spinner = source.find( '.spinner' ), |
| 33 | menu_item = $( this ).val(); |
| 34 | |
| 35 | // disable nav on ajax |
| 36 | container.addClass( 'rl-loading-content' ); |
| 37 | source.addClass( 'rl-loading-content' ); |
| 38 | |
| 39 | // display spinner |
| 40 | spinner.fadeIn( 'fast' ).css( 'visibility', 'visible' ); |
| 41 | |
| 42 | // post ajax request |
| 43 | $.post( ajaxurl, { |
| 44 | action: 'rl-get-menu-content', |
| 45 | post_id: rlArgsGalleries.post_id, |
| 46 | tab: tab, |
| 47 | menu_item: menu_item, |
| 48 | nonce: rlArgsGalleries.nonce |
| 49 | } ).done( function( response ) { |
| 50 | try { |
| 51 | if ( response.success ) { |
| 52 | // replace HTML |
| 53 | container.html( response.data ); |
| 54 | |
| 55 | // enable nav after ajax |
| 56 | container.removeClass( 'rl-loading-content' ); |
| 57 | source.removeClass( 'rl-loading-content' ); |
| 58 | |
| 59 | // update gallery data |
| 60 | gallery_frame = null; |
| 61 | gallery_container = $( '.rl-gallery-images' ); |
| 62 | gallery_ids = $( '.rl-gallery-ids' ); |
| 63 | |
| 64 | // refresh sortable only for media library |
| 65 | media_gallery_sortable( gallery_container, gallery_ids, menu_item ); |
| 66 | |
| 67 | // color picker |
| 68 | container.find( '.color-picker' ).wpColorPicker(); |
| 69 | |
| 70 | /* |
| 71 | var click = $( this ), |
| 72 | type = click.hasClass( 'rl-gallery-update-preview' ) ? 'update' : 'page', |
| 73 | menu_item = $( '.rl-gallery-tab-menu-images input:checked' ).val(), |
| 74 | container = $( '.rl-gallery-tab-inside-images-' + menu_item ), |
| 75 | spinner = click.closest( 'td' ).find( '.rl-gallery-preview-inside .spinner' ), |
| 76 | query_args = {}, |
| 77 | inside = $( this ).closest( '.inside' ).find( '.rl-gallery-tab-content' ); |
| 78 | |
| 79 | // disable nav on ajax |
| 80 | inside.addClass( 'rl-loading-content' ); |
| 81 | */ |
| 82 | |
| 83 | container.find( 'tr[data-field_type]' ).each( function() { |
| 84 | var el = $( this ), |
| 85 | field_name = el.data( 'field_name' ), |
| 86 | change = false; |
| 87 | |
| 88 | switch ( el.data( 'field_type' ) ) { |
| 89 | case 'text': |
| 90 | el.find( 'input' ).on( 'change', function() { |
| 91 | console.log( 'text' ); |
| 92 | } ); |
| 93 | break; |
| 94 | |
| 95 | case 'number': |
| 96 | el.find( 'input' ).on( 'change', function() { |
| 97 | console.log( 'number' ); |
| 98 | } ); |
| 99 | break; |
| 100 | |
| 101 | case 'taxonomy': |
| 102 | // value = { |
| 103 | // 'id': parseInt( el.find( 'select option:selected' ).val() ), |
| 104 | // 'children': el.find( 'input[type="checkbox"]' ).prop( 'checked' ) |
| 105 | // }; |
| 106 | break; |
| 107 | |
| 108 | case 'select': |
| 109 | el.find( 'select' ).on( 'change', function() { |
| 110 | console.log( 'select' ); |
| 111 | } ); |
| 112 | value = ''; |
| 113 | break; |
| 114 | |
| 115 | case 'radio': |
| 116 | // value = el.find( 'input:checked' ).val(); |
| 117 | break; |
| 118 | |
| 119 | case 'multiselect': |
| 120 | // value = el.find( 'select' ).val(); |
| 121 | break; |
| 122 | } |
| 123 | } ); |
| 124 | } else { |
| 125 | // @todo |
| 126 | } |
| 127 | } catch ( e ) { |
| 128 | // @todo |
| 129 | } |
| 130 | |
| 131 | // hide spinner |
| 132 | spinner.fadeOut( 'fast' ); |
| 133 | } ).fail( function() { |
| 134 | // hide spinner |
| 135 | spinner.fadeOut( 'fast' ); |
| 136 | } ); |
| 137 | } ); |
| 138 | |
| 139 | // change navigation menu |
| 140 | $( document ).on( 'click', '.nav-tab', function( e ) { |
| 141 | e.preventDefault(); |
| 142 | |
| 143 | var anchor = $( this ).attr( 'href' ).substr( 1 ); |
| 144 | |
| 145 | // remove active class |
| 146 | $( '.nav-tab' ).removeClass( 'nav-tab-active' ); |
| 147 | |
| 148 | // add active class |
| 149 | $( this ).addClass( 'nav-tab-active' ); |
| 150 | |
| 151 | // hide all normal metaboxes |
| 152 | $( '#postbox-container-2 div[id^="responsive-gallery-"]' ).removeClass( 'rl-display-metabox' ).addClass( 'rl-hide-metabox' ); |
| 153 | |
| 154 | // display needed metabox |
| 155 | if ( anchor === '' ) |
| 156 | $( '#responsive-gallery-images' ).addClass( 'rl-display-metabox' ).removeClass( 'rl-hide-metabox' ); |
| 157 | else |
| 158 | $( '#responsive-gallery-' + anchor ).addClass( 'rl-display-metabox' ).removeClass( 'rl-hide-metabox' ); |
| 159 | |
| 160 | $( 'input[name="rl_active_tab"]' ).val( anchor ); |
| 161 | } ); |
| 162 | |
| 163 | $( '.rl-shortcode' ).on( 'click', function() { |
| 164 | $( this ).trigger( 'select' ); |
| 165 | } ); |
| 166 | |
| 167 | // remove image |
| 168 | $( document ).on( 'click', '.rl-gallery-image-remove', function( e ) { |
| 169 | e.preventDefault(); |
| 170 | |
| 171 | // prevent featured images being removed |
| 172 | if ( $( this ).closest( '.rl-gallery-images-featured' ).length === 1 ) |
| 173 | return false; |
| 174 | |
| 175 | var li = $( this ).closest( 'li.rl-gallery-image' ), |
| 176 | attachment_ids = get_current_attachments( gallery_ids ); |
| 177 | |
| 178 | // remove id |
| 179 | attachment_ids = _.without( attachment_ids, parseInt( li.data( 'attachment_id' ) ) ); |
| 180 | |
| 181 | // remove attachment |
| 182 | li.remove(); |
| 183 | |
| 184 | // update attachment ids |
| 185 | gallery_ids.val( $.uniqueSort( attachment_ids ).join( ',' ) ); |
| 186 | |
| 187 | return false; |
| 188 | } ); |
| 189 | |
| 190 | // edit image |
| 191 | $( document ).on( 'click', '.rl-gallery-image-edit', function( e ) { |
| 192 | e.preventDefault(); |
| 193 | |
| 194 | var li = $( this ).closest( 'li.rl-gallery-image' ), |
| 195 | attachment_id = parseInt( li.data( 'attachment_id' ) ), |
| 196 | attachment_changed = false; |
| 197 | |
| 198 | // frame already exists? |
| 199 | if ( attachment_frame !== null ) { |
| 200 | attachment_frame.detach(); |
| 201 | attachment_frame.dispose(); |
| 202 | attachment_frame = null; |
| 203 | } |
| 204 | |
| 205 | // create new frame |
| 206 | attachment_frame = wp.media( { |
| 207 | id: 'rl-edit-attachment-modal', |
| 208 | frame: 'select', |
| 209 | uploader: false, |
| 210 | multiple: false, |
| 211 | title: rlArgsGalleries.editTitle, |
| 212 | library: { |
| 213 | post__in: attachment_id |
| 214 | }, |
| 215 | button: { |
| 216 | text: rlArgsGalleries.buttonEditFile |
| 217 | } |
| 218 | } ).on( 'open', function() { |
| 219 | var attachment = wp.media.attachment( attachment_id ), |
| 220 | selection = attachment_frame.state().get( 'selection' ); |
| 221 | |
| 222 | attachment_frame.$el.closest( '.media-modal' ).addClass( 'rl-edit-modal' ); |
| 223 | |
| 224 | // get attachment |
| 225 | attachment.fetch(); |
| 226 | |
| 227 | // reset selection |
| 228 | //selection.reset(); |
| 229 | |
| 230 | // add attachment |
| 231 | selection.add( attachment ); |
| 232 | } ); |
| 233 | |
| 234 | attachment_frame.open(); |
| 235 | |
| 236 | return false; |
| 237 | } ); |
| 238 | |
| 239 | // change image status |
| 240 | $( document ).on( 'click', '.rl-gallery-image-status', function( e ) { |
| 241 | e.preventDefault(); |
| 242 | |
| 243 | var li = $( this ).closest( 'li.rl-gallery-image' ), |
| 244 | input = li.find( '.rl-gallery-exclude' ), |
| 245 | status = li.hasClass( 'rl-status-active' ), |
| 246 | id = parseInt( li.data( 'attachment_id' ) ), |
| 247 | item = ''; |
| 248 | |
| 249 | if ( id > 0 ) |
| 250 | item = id; |
| 251 | else |
| 252 | item = li.find( '.rl-gallery-inner img' ).attr( 'src' ); |
| 253 | |
| 254 | // exclude? |
| 255 | if ( status ) { |
| 256 | li.addClass( 'rl-status-inactive' ).removeClass( 'rl-status-active' ); |
| 257 | |
| 258 | // add item |
| 259 | input.val( item ); |
| 260 | } else { |
| 261 | li.addClass( 'rl-status-active' ).removeClass( 'rl-status-inactive' ); |
| 262 | |
| 263 | // remove item |
| 264 | input.val( '' ); |
| 265 | } |
| 266 | |
| 267 | return false; |
| 268 | } ); |
| 269 | |
| 270 | // open the modal on click |
| 271 | $( document ).on( 'click', '.rl-gallery-select', function( e ) { |
| 272 | e.preventDefault(); |
| 273 | |
| 274 | // open media frame if already exists |
| 275 | if ( gallery_frame !== null ) { |
| 276 | gallery_frame.open(); |
| 277 | |
| 278 | return; |
| 279 | } |
| 280 | |
| 281 | // create the media frame |
| 282 | gallery_frame = wp.media( { |
| 283 | title: rlArgsGalleries.textSelectImages, |
| 284 | multiple: 'add', |
| 285 | autoSelect: true, |
| 286 | library: { |
| 287 | type: 'image' |
| 288 | }, |
| 289 | button: { |
| 290 | text: rlArgsGalleries.textUseImages |
| 291 | } |
| 292 | } ).on( 'open', function() { |
| 293 | var selection = gallery_frame.state().get( 'selection' ), |
| 294 | attachment_ids = get_current_attachments( gallery_ids ); |
| 295 | |
| 296 | // deselect all attachments |
| 297 | selection.reset(); |
| 298 | |
| 299 | $.each( attachment_ids, function() { |
| 300 | // prepare attachment |
| 301 | attachment = wp.media.attachment( this ); |
| 302 | |
| 303 | // select attachment |
| 304 | selection.add( attachment ? [ attachment ] : [ ] ); |
| 305 | } ); |
| 306 | } ).on( 'select', function() { |
| 307 | var selection = gallery_frame.state().get( 'selection' ), |
| 308 | attachment_ids = get_current_attachments( gallery_ids ), |
| 309 | selected_ids = [ ]; |
| 310 | |
| 311 | if ( selection ) { |
| 312 | selection.map( function( attachment ) { |
| 313 | if ( attachment.id ) { |
| 314 | // add attachment |
| 315 | selected_ids.push( attachment.id ); |
| 316 | |
| 317 | // is image already in gallery? |
| 318 | if ( $.inArray( attachment.id, attachment_ids ) !== -1 ) |
| 319 | return; |
| 320 | |
| 321 | // add attachment |
| 322 | attachment_ids.push( attachment.id ); |
| 323 | attachment = attachment.toJSON(); |
| 324 | |
| 325 | // is preview size available? |
| 326 | if ( attachment.sizes && attachment.sizes['thumbnail'] ) |
| 327 | attachment.url = attachment.sizes['thumbnail'].url; |
| 328 | |
| 329 | // append new image |
| 330 | gallery_container.append( rlArgsGalleries.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' ) ); |
| 331 | } |
| 332 | } ); |
| 333 | } |
| 334 | |
| 335 | // assign copy of attachment ids |
| 336 | var copy = attachment_ids; |
| 337 | |
| 338 | for ( var i = 0; i < attachment_ids.length; i++ ) { |
| 339 | // unselected image? |
| 340 | if ( $.inArray( attachment_ids[i], selected_ids ) === -1 ) { |
| 341 | gallery_container.find( 'li.rl-gallery-image[data-attachment_id="' + attachment_ids[i] + '"]' ).remove(); |
| 342 | |
| 343 | copy = _.without( copy, attachment_ids[i] ); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | gallery_ids.val( $.uniqueSort( copy ).join( ',' ) ); |
| 348 | } ); |
| 349 | |
| 350 | // open media frame |
| 351 | gallery_frame.open(); |
| 352 | } ); |
| 353 | |
| 354 | // preview pagination |
| 355 | $( document ).on( 'click', '.rl-gallery-update-preview, .rl-gallery-preview-pagination a', function( e ) { |
| 356 | e.preventDefault(); |
| 357 | |
| 358 | var click = $( this ), |
| 359 | type = click.hasClass( 'rl-gallery-update-preview' ) ? 'update' : 'page', |
| 360 | menu_item = $( '.rl-gallery-tab-menu-images input:checked' ).val(), |
| 361 | container = $( '.rl-gallery-tab-inside-images-' + menu_item ), |
| 362 | spinner = click.closest( 'td' ).find( '.rl-gallery-preview-inside .spinner' ), |
| 363 | query_args = {}, |
| 364 | inside = $( this ).closest( '.inside' ).find( '.rl-gallery-tab-content' ); |
| 365 | |
| 366 | // disable nav on ajax |
| 367 | inside.addClass( 'rl-loading-content' ); |
| 368 | |
| 369 | // pagination? |
| 370 | if ( type === 'page' ) { |
| 371 | var content = click.attr( 'href' ).match( 'preview_page/\\d+' ), |
| 372 | page = 1; |
| 373 | |
| 374 | // get valid page number |
| 375 | if ( content !== null ) |
| 376 | page = content[0].split( '/' )[1]; |
| 377 | |
| 378 | query_args['preview_page'] = page; |
| 379 | } |
| 380 | |
| 381 | container.find( 'tr[data-field_type]' ).each( function() { |
| 382 | var el = $( this ), |
| 383 | field_name = el.data( 'field_name' ), |
| 384 | value = null; |
| 385 | |
| 386 | switch ( el.data( 'field_type' ) ) { |
| 387 | case 'text': |
| 388 | value = el.find( 'input' ).val(); |
| 389 | |
| 390 | if ( ! value ) |
| 391 | value = ''; |
| 392 | break; |
| 393 | |
| 394 | case 'number': |
| 395 | value = parseInt( el.find( 'input' ).val() ); |
| 396 | |
| 397 | if ( ! value ) |
| 398 | value = 0; |
| 399 | break; |
| 400 | |
| 401 | case 'taxonomy': |
| 402 | value = { |
| 403 | 'id': parseInt( el.find( 'select option:selected' ).val() ), |
| 404 | 'children': el.find( 'input[type="checkbox"]' ).prop( 'checked' ) |
| 405 | }; |
| 406 | |
| 407 | if ( ! value ) |
| 408 | value = { |
| 409 | 'id': 0, |
| 410 | 'children': false |
| 411 | }; |
| 412 | break; |
| 413 | |
| 414 | case 'select': |
| 415 | value = el.find( 'select option:selected' ).val(); |
| 416 | |
| 417 | if ( ! value ) |
| 418 | value = ''; |
| 419 | break; |
| 420 | |
| 421 | case 'radio': |
| 422 | value = el.find( 'input:checked' ).val(); |
| 423 | |
| 424 | if ( ! value ) |
| 425 | value = ''; |
| 426 | break; |
| 427 | |
| 428 | case 'multiselect': |
| 429 | value = el.find( 'select' ).val(); |
| 430 | |
| 431 | if ( ! value ) |
| 432 | value = []; |
| 433 | break; |
| 434 | |
| 435 | case 'hidden': |
| 436 | var subel = el.find( 'input[type="hidden"]' ), |
| 437 | nofa = parseInt( subel.data( 'subarg' ) ), |
| 438 | val = el.find( 'input[type="hidden"]' ).val(); |
| 439 | |
| 440 | if ( nofa > 0 ) { |
| 441 | var atts = subel.attr( 'name' ).slice( 0, -1 ).split( '][' ), |
| 442 | new_value = {}, |
| 443 | last; |
| 444 | |
| 445 | for ( var i = atts.length; i > atts.length - nofa; i-- ) { |
| 446 | var number = i - 1; |
| 447 | |
| 448 | // first element? |
| 449 | if ( i === atts.length ) |
| 450 | new_value[atts[number]] = val; |
| 451 | else |
| 452 | new_value[atts[number]] = last; |
| 453 | |
| 454 | // remember last array |
| 455 | last = new_value; |
| 456 | |
| 457 | // do not reset for last element |
| 458 | if ( number > atts.length - nofa ) |
| 459 | new_value = {}; |
| 460 | } |
| 461 | |
| 462 | // get new array |
| 463 | value = new_value; |
| 464 | |
| 465 | // save wikimedia aicontinue for pages |
| 466 | if ( ! ( query_args['preview_page'] in wikimedia ) ) |
| 467 | wikimedia[query_args['preview_page']] = value.wikimedia.continue; |
| 468 | |
| 469 | value.wikimedia.continue = wikimedia[query_args['preview_page']]; |
| 470 | } else |
| 471 | value = val; |
| 472 | break; |
| 473 | } |
| 474 | |
| 475 | query_args[field_name] = value; |
| 476 | } ); |
| 477 | |
| 478 | // display spinner |
| 479 | spinner.fadeIn( 'fast' ).css( 'visibility', 'visible' ); |
| 480 | |
| 481 | // post ajax request |
| 482 | $.post( ajaxurl, { |
| 483 | action: 'rl-get-preview-content', |
| 484 | post_id: rlArgsGalleries.post_id, |
| 485 | menu_item: menu_item, |
| 486 | query: query_args, |
| 487 | preview_type: type, |
| 488 | excluded: $( '.rl-gallery-exclude' ).map( function( i, elem ) { return $( elem ).val(); } ).get(), |
| 489 | nonce: rlArgsGalleries.nonce |
| 490 | } ).done( function( response ) { |
| 491 | try { |
| 492 | if ( response.success ) { |
| 493 | container.find( 'tr[data-field_type]' ).each( function() { |
| 494 | var el = $( this ), |
| 495 | field_name = el.data( 'field_name' ), |
| 496 | value = null; |
| 497 | |
| 498 | switch ( el.data( 'field_type' ) ) { |
| 499 | case 'hidden': |
| 500 | $( '#rl_images_remote_library_response_data_wikimedia_continue' ).val( response.data.data.wikimedia.continue ); |
| 501 | |
| 502 | var next_page = parseInt( query_args['preview_page'] ) + 1; |
| 503 | |
| 504 | // save wikimedia aicontinue for pages |
| 505 | if ( ! ( next_page in wikimedia ) ) |
| 506 | wikimedia[next_page] = response.data.data.wikimedia.continue; |
| 507 | /* |
| 508 | var subel = el.find( 'input[type="hidden"]' ), |
| 509 | nofa = parseInt( subel.data( 'subarg' ) ), |
| 510 | val = el.find( 'input[type="hidden"]' ).val(); |
| 511 | |
| 512 | if ( nofa > 0 ) { |
| 513 | var atts = subel.attr( 'name' ).slice( 0, -1 ).split( '][' ), |
| 514 | new_value = {}, |
| 515 | last; |
| 516 | |
| 517 | for ( var i = atts.length - nofa; i <= atts.length; i++ ) { |
| 518 | var number = i - 1; |
| 519 | |
| 520 | // first element? |
| 521 | if ( i === atts.length ) { |
| 522 | // new_value2 .= '[' + atts[number] + ']'; |
| 523 | new_value = response.data.data[atts[number]]; |
| 524 | } else { |
| 525 | last = new_value[atts[number]]; |
| 526 | } |
| 527 | |
| 528 | // remember last array |
| 529 | new_value = last; |
| 530 | |
| 531 | // do not reset for last element |
| 532 | // if ( number > atts.length - nofa ) |
| 533 | // new_value = {}; |
| 534 | } |
| 535 | |
| 536 | // get new array |
| 537 | value = new_value; |
| 538 | } else |
| 539 | subel.val( response.data.data ); |
| 540 | break; |
| 541 | */ |
| 542 | } |
| 543 | } ); |
| 544 | |
| 545 | $( '.rl-gallery-images' ).empty().append( response.data.images ); |
| 546 | |
| 547 | if ( type === 'page' ) |
| 548 | $( '.rl-gallery-preview-pagination' ).replaceWith( response.data.pagination ); |
| 549 | } else { |
| 550 | // @todo |
| 551 | } |
| 552 | } catch ( e ) { |
| 553 | // @todo |
| 554 | } |
| 555 | } ).always( function() { |
| 556 | // hide spinner |
| 557 | spinner.fadeOut( 'fast' ); |
| 558 | |
| 559 | // enable content |
| 560 | inside.removeClass( 'rl-loading-content' ); |
| 561 | } ); |
| 562 | |
| 563 | return false; |
| 564 | } ); |
| 565 | |
| 566 | // load values for specified rule |
| 567 | $( document ).on( 'change', '.rl-rule-type', function() { |
| 568 | var _this = $( this ), |
| 569 | td = _this.closest( 'tr' ).find( 'td.value' ), |
| 570 | select = td.find( 'select' ), |
| 571 | spinner = td.find( '.spinner' ); |
| 572 | |
| 573 | select.hide(); |
| 574 | spinner.fadeIn( 'fast' ).css( 'visibility', 'visible' ); |
| 575 | |
| 576 | $.post( ajaxurl, { |
| 577 | action: 'rl-get-group-rules-values', |
| 578 | type: _this.val(), |
| 579 | nonce: rlArgsGalleries.nonce |
| 580 | } ).done( function( data ) { |
| 581 | spinner.hide(); |
| 582 | |
| 583 | try { |
| 584 | var response = JSON.parse( data ); |
| 585 | |
| 586 | // remove old select options and adds new ones |
| 587 | select.fadeIn( 'fast' ).find( 'option, optgroup' ).remove().end().append( response.select ); |
| 588 | } catch ( e ) { |
| 589 | // |
| 590 | } |
| 591 | } ).fail( function() { |
| 592 | // |
| 593 | } ); |
| 594 | } ); |
| 595 | } ); |
| 596 | |
| 597 | // listen for insert/remove media library thumbnail |
| 598 | $( document ).on( 'DOMNodeInserted', '#postimagediv .inside', function( e ) { |
| 599 | var value = $( '#postimagediv .inside' ).attr( 'data-featured-type' ); |
| 600 | |
| 601 | if ( $( '#rl-gallery-featured-' + value ).length > 0 ) { |
| 602 | $( '#rl-gallery-featured-' + value ).prop( 'checked', true ); |
| 603 | |
| 604 | $( '#postimagediv .inside' ).trigger( 'change' ); |
| 605 | } |
| 606 | } ); |
| 607 | |
| 608 | // handle featured image change |
| 609 | $( document ).on( 'change', '#postimagediv .inside', function() { |
| 610 | var el = $( this ).find( 'input[name="rl_gallery_featured_image"]:checked' ), |
| 611 | value = $( el ).val(); |
| 612 | |
| 613 | $( '#postimagediv .inside' ).attr( 'data-featured-type', value ); |
| 614 | $( '.rl-gallery-featured-image-select' ).children( 'div' ).hide(); |
| 615 | $( '.rl-gallery-featured-image-select-' + value ).show(); |
| 616 | |
| 617 | // media library |
| 618 | if ( value === 'id' ) { |
| 619 | var thumbnail_id = parseInt( $( '#_thumbnail_id' ).attr( 'data-featured-id' ) ); |
| 620 | |
| 621 | if ( thumbnail_id > 0 ) |
| 622 | $( '#_thumbnail_id' ).val( thumbnail_id ).attr( 'data-featured-id', -1 ); |
| 623 | // custom URL |
| 624 | } else if ( value === 'url' ) { |
| 625 | var thumbnail_id = parseInt( $( '#_thumbnail_id' ).val() ); |
| 626 | |
| 627 | if ( thumbnail_id > 0 ) |
| 628 | $( '#_thumbnail_id' ).attr( 'data-featured-id', thumbnail_id ).val( -1 ); |
| 629 | // first gallery image |
| 630 | } else { |
| 631 | var thumbnail_id = parseInt( $( '#_thumbnail_id' ).val() ); |
| 632 | |
| 633 | if ( thumbnail_id > 0 ) |
| 634 | $( '#_thumbnail_id' ).attr( 'data-featured-id', thumbnail_id ).val( -1 ); |
| 635 | } |
| 636 | } ); |
| 637 | |
| 638 | $( document ).on( 'ajaxComplete', function() { |
| 639 | init_select2(); |
| 640 | } ); |
| 641 | |
| 642 | function init_select2() { |
| 643 | $( '.rl-gallery-tab-inside select.select2' ).select2( { |
| 644 | closeOnSelect: true, |
| 645 | multiple: true, |
| 646 | width: 300, |
| 647 | minimumInputLength: 0 |
| 648 | } ); |
| 649 | } |
| 650 | |
| 651 | function createSubValue( value, new_value, arg ) { |
| 652 | value = new_value; |
| 653 | // value = value[arg]; |
| 654 | |
| 655 | return value |
| 656 | } |
| 657 | |
| 658 | // get attachment ids |
| 659 | function get_current_attachments( gallery_ids ) { |
| 660 | var attachments = gallery_ids.val(); |
| 661 | |
| 662 | // return integer image ids or empty array |
| 663 | return attachments !== '' ? attachments.split( ',' ).map( function( i ) { |
| 664 | return parseInt( i ) |
| 665 | } ) : []; |
| 666 | } |
| 667 | |
| 668 | // |
| 669 | function media_gallery_sortable( gallery, ids, type ) { |
| 670 | if ( type === 'media' ) { |
| 671 | // images order |
| 672 | gallery.sortable( { |
| 673 | items: 'li.rl-gallery-image', |
| 674 | cursor: 'move', |
| 675 | scrollSensitivity: 40, |
| 676 | forcePlaceholderSize: true, |
| 677 | forceHelperSize: false, |
| 678 | helper: 'clone', |
| 679 | opacity: 0.65, |
| 680 | placeholder: 'rl-gallery-sortable-placeholder', |
| 681 | start: function( event, ui ) { |
| 682 | ui.item.css( 'border-color', '#f6f6f6' ); |
| 683 | }, |
| 684 | stop: function( event, ui ) { |
| 685 | ui.item.removeAttr( 'style' ); |
| 686 | }, |
| 687 | update: function( event, ui ) { |
| 688 | var attachment_ids = [ ]; |
| 689 | |
| 690 | gallery.find( 'li.rl-gallery-image' ).each( function() { |
| 691 | attachment_ids.push( parseInt( $( this ).attr( 'data-attachment_id' ) ) ); |
| 692 | } ); |
| 693 | |
| 694 | ids.val( $.uniqueSort( attachment_ids ).join( ',' ) ); |
| 695 | } |
| 696 | } ); |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | } )( jQuery ); |