redux-gallery.js (3699B)
1 /* global redux_change, wp, redux */ 2 3 (function( $ ) { 4 'use strict'; 5 6 redux.field_objects = redux.field_objects || {}; 7 redux.field_objects.gallery = redux.field_objects.gallery || {}; 8 9 redux.field_objects.gallery.init = function( selector ) { 10 selector = $.redux.getSelector( selector, 'gallery' ); 11 12 $( selector ).each( 13 function() { 14 var el = $( this ); 15 var parent = el; 16 17 if ( ! el.hasClass( 'redux-field-container' ) ) { 18 parent = el.parents( '.redux-field-container:first' ); 19 } 20 21 if ( parent.is( ':hidden' ) ) { 22 return; 23 } 24 25 if ( parent.hasClass( 'redux-field-init' ) ) { 26 parent.removeClass( 'redux-field-init' ); 27 } else { 28 return; 29 } 30 31 // When the user clicks on the Add/Edit gallery button, we need to display the gallery editing. 32 el.on( 33 { 34 click: function( event ) { 35 var current_gallery; 36 var final; 37 var val; 38 var frame; 39 var uploader; 40 var spinner; 41 var inline; 42 43 // Hide gallery settings used for posts/pages. 44 wp.media.view.Settings.Gallery = wp.media.view.Settings.Gallery.extend( 45 { 46 template: function() { 47 return; 48 } 49 } 50 ); 51 52 current_gallery = $( this ).closest( 'fieldset' ); 53 54 if ( 'clear-gallery' === event.currentTarget.id ) { 55 56 // Remove value from input. 57 current_gallery.find( '.gallery_values' ).val( '' ); 58 59 // Remove preview images. 60 current_gallery.find( '.screenshot' ).html( '' ); 61 62 return; 63 } 64 65 // Make sure the media gallery API exists. 66 if ( 'undefined' === typeof wp || ! wp.media || ! wp.media.gallery ) { 67 return; 68 } 69 70 event.preventDefault(); 71 72 // Activate the media editor. 73 val = current_gallery.find( '.gallery_values' ).val(); 74 75 if ( ! val ) { 76 final = '[gallery ids="0"]'; 77 } else { 78 final = '[gallery ids="' + val + '"]'; 79 } 80 81 frame = wp.media.gallery.edit( final ); 82 83 if ( ! val ) { 84 uploader = $( 'body' ).find( '#' + frame.el.id ); 85 inline = uploader.find( '.uploader-inline' ); 86 spinner = uploader.find( '.media-toolbar .spinner' ); 87 88 setTimeout( 89 function() { 90 if ( inline.hasClass( 'hidden' ) ) { 91 inline.removeClass( 'hidden' ); 92 spinner.removeClass( 'is-active' ); 93 } 94 }, 95 400 96 ); 97 } 98 99 // When the gallery-edit state is updated, copy the attachment ids across. 100 frame.state( 'gallery-edit' ).on( 101 'update', 102 function( selection ) { 103 var ids; 104 var element; 105 var preview_img; 106 107 var preview_html = ''; 108 109 // Clear screenshot div so we can append new selected images. 110 current_gallery.find( '.screenshot' ).html( '' ); 111 112 ids = selection.models.map( 113 function( e ) { 114 element = e.toJSON(); 115 116 preview_img = ( 'undefined' !== typeof element.sizes && 'undefined' !== typeof element.sizes.thumbnail ) ? element.sizes.thumbnail.url : element.url; 117 118 preview_html = '<a class="of-uploaded-image" href="' + preview_img + '"><img class="redux-option-image" src="' + preview_img + '" alt="" /></a>'; 119 current_gallery.find( '.screenshot' ).append( preview_html ); 120 121 return e.id; 122 } 123 ); 124 125 current_gallery.find( '.gallery_values' ).val( ids.join( ',' ) ); 126 127 redux_change( current_gallery.find( '.gallery_values' ) ); 128 129 frame.detach(); 130 } 131 ); 132 133 return false; 134 } 135 }, 136 '.gallery-attachments' 137 ); 138 } 139 ); 140 }; 141 })( jQuery );