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