| 1 |
/** |
| 2 |
* Choose Attachment(s) from the Media Library, inserting into the DOM |
| 3 |
* hidden field and output preview. |
| 4 |
* |
| 5 |
* @package WPZincDashboardWidget |
| 6 |
* @author WP Zinc |
| 7 |
*/ |
| 8 |
|
| 9 |
( function ( $ ) { |
| 10 |
|
| 11 |
// Make Attachment(s) sortable. |
| 12 |
$( '.wpzinc-media-library-selector' ).each( |
| 13 |
function () { |
| 14 |
|
| 15 |
// Determine if this Media Library selector instance supports multiple Attachments. |
| 16 |
var multiple = $( this ).data( 'multiple' ); |
| 17 |
if ( typeof multiple == 'undefined' ) { |
| 18 |
multiple = false; |
| 19 |
} |
| 20 |
|
| 21 |
// Bail if multiple Attachments are not supported, as we don't need to sort a single Attachment. |
| 22 |
if ( ! multiple ) { |
| 23 |
return; |
| 24 |
} |
| 25 |
|
| 26 |
// Initialize jQuery UI sortable. |
| 27 |
$( 'ul', $( this ) ).sortable(); |
| 28 |
|
| 29 |
} |
| 30 |
); |
| 31 |
|
| 32 |
// Open Media Library Backbone Modal to allow selection of attachments. |
| 33 |
$( '#wpbody' ).on( |
| 34 |
'click', |
| 35 |
'.wpzinc-media-library-insert', |
| 36 |
function ( e ) { |
| 37 |
|
| 38 |
// Prevent default action. |
| 39 |
e.preventDefault(); |
| 40 |
|
| 41 |
// Get container and attributes. |
| 42 |
var container = $( this ).closest( '.wpzinc-media-library-selector' ), |
| 43 |
input_name = $( container ).data( 'input-name' ), |
| 44 |
input_url = $( container ).data( 'input-url' ), |
| 45 |
output_size = $( container ).data( 'output-size' ), // The size of the image to output. |
| 46 |
file_type = $( container ).data( 'file-type' ), // The file types that can be selected. |
| 47 |
multiple = $( container ).data( 'multiple' ), // If multiple attachments can be selected. |
| 48 |
limit = $( container ).data( 'limit' ); // The number of attachments that can be selected. |
| 49 |
|
| 50 |
// Define some attributes if they're not defined in the data- attributes. |
| 51 |
if ( typeof input_url == 'undefined' ) { |
| 52 |
input_url = false; |
| 53 |
} |
| 54 |
if ( typeof output_size == 'undefined' ) { |
| 55 |
output_size = 'thumbnail'; |
| 56 |
} |
| 57 |
if ( typeof file_type == 'undefined' ) { |
| 58 |
file_type = 'image'; |
| 59 |
} |
| 60 |
if ( typeof multiple == 'undefined' ) { |
| 61 |
multiple = false; |
| 62 |
} |
| 63 |
if ( typeof limit == 'undefined' ) { |
| 64 |
limit = 99999; |
| 65 |
} |
| 66 |
|
| 67 |
// Get existing Attachment(s) that were selected. |
| 68 |
var existing_selected_attachment_ids = []; |
| 69 |
$( 'input[type=hidden]', $( container ) ).each( |
| 70 |
function () { |
| 71 |
existing_selected_attachment_ids.push( $( this ).val() ); |
| 72 |
} |
| 73 |
); |
| 74 |
|
| 75 |
// If plugin_media_manager has already been defined, open it now. |
| 76 |
if ( wpzinc_plugin_media_manager ) { |
| 77 |
wpzinc_plugin_media_manager.open(); |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
// Setup new wp.media instance, if it's not already set by one of our plugins. |
| 82 |
var wpzinc_plugin_media_manager = wp.media( |
| 83 |
{ |
| 84 |
title: 'Choose Item', |
| 85 |
button: { |
| 86 |
text: 'Select' |
| 87 |
}, |
| 88 |
library: { |
| 89 |
type: file_type |
| 90 |
}, |
| 91 |
multiple: ( multiple ? 'add' : false ) |
| 92 |
} |
| 93 |
); |
| 94 |
|
| 95 |
// When the modal is opened, select any existing Attachments that were previously selected. |
| 96 |
wpzinc_plugin_media_manager.on( |
| 97 |
'open', |
| 98 |
function () { |
| 99 |
|
| 100 |
// Get selected attachments. |
| 101 |
var selection = wpzinc_plugin_media_manager.state().get( 'selection' ); |
| 102 |
|
| 103 |
// Add existing Attachment(s) that were selected to the selection, so they're |
| 104 |
// marked as selected in the modal. |
| 105 |
existing_selected_attachment_ids.forEach( |
| 106 |
function ( id ) { |
| 107 |
attachment = wp.media.attachment( id ); |
| 108 |
attachment.fetch(); |
| 109 |
selection.add( attachment ? [attachment] : [] ); |
| 110 |
} |
| 111 |
); |
| 112 |
|
| 113 |
} |
| 114 |
); |
| 115 |
|
| 116 |
// Remove all attached 'select' events. |
| 117 |
wpzinc_plugin_media_manager.off( 'select' ); |
| 118 |
wpzinc_plugin_media_manager.off( 'selection:toggle' ); |
| 119 |
|
| 120 |
// When an Attachment is selected or deselected, check that the total |
| 121 |
// number of selected Attachments doesn't exceed the limit. |
| 122 |
// If it does, remove (deselect) the selected attachment. |
| 123 |
wpzinc_plugin_media_manager.on( |
| 124 |
'selection:toggle', |
| 125 |
function () { |
| 126 |
|
| 127 |
var selection = wpzinc_plugin_media_manager.state().get( 'selection' ); |
| 128 |
|
| 129 |
if ( selection.length > limit ) { |
| 130 |
selection.remove( selection.last() ); |
| 131 |
} |
| 132 |
|
| 133 |
} |
| 134 |
); |
| 135 |
|
| 136 |
// When the Select button in the modal is clicked, insert. |
| 137 |
wpzinc_plugin_media_manager.on( |
| 138 |
'select', |
| 139 |
function () { |
| 140 |
|
| 141 |
// Get selected attachments. |
| 142 |
var selection = wpzinc_plugin_media_manager.state().get( 'selection' ).map( |
| 143 |
function ( attachment ) { |
| 144 |
|
| 145 |
attachment.toJSON(); |
| 146 |
return attachment; |
| 147 |
|
| 148 |
} |
| 149 |
); |
| 150 |
|
| 151 |
// Clear Attachments HTML. |
| 152 |
$( 'ul', $( container ) ).html( '' ); |
| 153 |
|
| 154 |
// Iterate through selected attachments, outputting HTML for each. |
| 155 |
var length = selection.length; |
| 156 |
for ( var i = 0; i < length; i++ ) { |
| 157 |
|
| 158 |
// Get this attachment's ID and URL. |
| 159 |
var attachment = selection[ i ], |
| 160 |
attachment_id = attachment.get( 'id' ), |
| 161 |
attachment_url = attachment.get( 'url' ); |
| 162 |
|
| 163 |
// Build HTML. |
| 164 |
var html = '<li class="wpzinc-media-library-attachment">'; |
| 165 |
html += '<a href="#" class="wpzinc-media-library-insert">'; |
| 166 |
html += '<input type="hidden" name="' + input_name + '" value="' + attachment_id + '" />'; |
| 167 |
|
| 168 |
// Build Output Preview, depending on the Attachment's type. |
| 169 |
switch ( attachment.attributes.type ) { |
| 170 |
|
| 171 |
case 'image': |
| 172 |
// If the image size we're requesting exists, use that instead. |
| 173 |
if ( typeof attachment.attributes.sizes[ output_size ] !== 'undefined' ) { |
| 174 |
attachment_url = attachment.attributes.sizes[ output_size ].url; |
| 175 |
} |
| 176 |
|
| 177 |
html += '<img src="' + attachment_url + '" />'; |
| 178 |
|
| 179 |
// Include URL as hidden field if required. |
| 180 |
if ( input_url ) { |
| 181 |
html += '<input type="hidden" name="' + input_url + '" value="' + attachment.attributes.url + '" />'; |
| 182 |
} |
| 183 |
break; |
| 184 |
|
| 185 |
default: |
| 186 |
html += attachment.attributes.filename; |
| 187 |
break; |
| 188 |
|
| 189 |
} |
| 190 |
|
| 191 |
// Finish building HTML. |
| 192 |
html += '<a href="#" class="wpzinc-media-library-remove">Remove</a>'; |
| 193 |
html += '</li>'; |
| 194 |
|
| 195 |
// Inject. |
| 196 |
$( 'ul', $( container ) ).append( html ); |
| 197 |
|
| 198 |
// Trigger event so other JS can hook into when an attachment is added. |
| 199 |
$( 'body' ).trigger( 'wpzinc-media-library-attachment-added' ); |
| 200 |
|
| 201 |
} |
| 202 |
|
| 203 |
} |
| 204 |
); |
| 205 |
|
| 206 |
// Open the Media View. |
| 207 |
wpzinc_plugin_media_manager.open(); |
| 208 |
|
| 209 |
} |
| 210 |
); |
| 211 |
|
| 212 |
// Removes a selected Attachment. |
| 213 |
$( '#wpbody' ).on( |
| 214 |
'click', |
| 215 |
'.wpzinc-media-library-remove', |
| 216 |
function ( e ) { |
| 217 |
|
| 218 |
// Prevent default action. |
| 219 |
e.preventDefault(); |
| 220 |
|
| 221 |
// Remove. |
| 222 |
$( this ).closest( '.wpzinc-media-library-attachment' ).remove(); |
| 223 |
|
| 224 |
} |
| 225 |
); |
| 226 |
|
| 227 |
} ( jQuery ) ); |
| 228 |
|