-
Notifications
You must be signed in to change notification settings - Fork 42
/
external-media-without-import.js
90 lines (83 loc) · 2.87 KB
/
external-media-without-import.js
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
jQuery(function ( $ ) {
var isAdding = false;
function clear() {
$( '#emwi-urls' ).val( '' );
$( '#emwi-hidden' ).hide();
$( '#emwi-error' ).text( '' );
$( '#emwi-width' ).val( '' );
$( '#emwi-height' ).val( '' );
$( '#emwi-mime-type' ).val( '' );
}
$( 'body' ).on( 'click', '#emwi-clear', function ( e ) {
clear();
});
$( 'body' ).on( 'click', '#emwi-show', function ( e ) {
$( '#emwi-media-new-panel' ).show();
e.preventDefault();
});
$( 'body' ).on( 'click', '#emwi-in-upload-ui #emwi-add', function ( e ) {
if ( isAdding ) {
return;
}
isAdding = true;
$('#emwi-in-upload-ui #emwi-add').prop('disabled', true);
var postData = {
'urls': $( '#emwi-urls' ).val(),
'width': $( '#emwi-width' ).val(),
'height': $( '#emwi-height' ).val(),
'mime-type': $( '#emwi-mime-type' ).val()
};
wp.media.post( 'add_external_media_without_import', postData )
.done(function ( response ) {
// Update the attachment list in browser.
var frame = wp.media.frame || wp.media.library;
if ( frame ) {
frame.content.mode( 'browse' );
// The frame variable may be MediaFrame.Manage or MediaFrame.EditAttachments.
// In the later case, library = frame.library.
var library = frame.state().get( 'library' ) || frame.library;
response.attachments.forEach( function ( elem ) {
var attachment = wp.media.model.Attachment.create( elem );
attachment.fetch();
library.add( attachment ? [ attachment ] : [] );
if ( wp.media.frame._state != 'library' ) {
var selection = frame.state().get( 'selection' );
if ( selection ) {
selection.add( attachment );
}
}
} );
}
if ( response['error'] ) {
$( '#emwi-error' ).text( response['error'] );
$( '#emwi-width' ).val( response['width'] );
$( '#emwi-height' ).val( response['height'] );
$( '#emwi-mime-type' ).val( response['mime-type'] );
$( '#emwi-hidden' ).show();
} else {
// Reset the input.
clear();
$( '#emwi-hidden' ).hide();
}
$( '#emwi-urls' ).val( response['urls'] );
$( '#emwi-buttons-row .spinner' ).css( 'visibility', 'hidden' );
$( '#emwi-in-upload-ui #emwi-add').prop('disabled', false);
isAdding = false;
}).fail(function (response ) {
$( '#emwi-error' ).text( 'An unknown network error occured' );
$( '#emwi-buttons-row .spinner' ).css( 'visibility', 'hidden' );
$( '#emwi-in-upload-ui #emwi-add' ).prop('disabled', false);
isAdding = false;
});
e.preventDefault();
$( '#emwi-buttons-row .spinner' ).css( 'visibility', 'visible' );
});
$( 'body' ).on( 'click', '#emwi-in-upload-ui #emwi-cancel', function (e ) {
clear();
$( '#emwi-media-new-panel' ).hide();
$( '#emwi-buttons-row .spinner' ).css( 'visibility', 'hidden' );
$( '#emwi-in-upload-ui #emwi-add' ).prop('disabled', false);
isAdding = false;
e.preventDefault();
});
});