media-video-widget.js
6.86 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**
* @output wp-admin/js/widgets/media-video-widget.js
*/
/* eslint consistent-this: [ "error", "control" ] */
(function( component ) {
'use strict';
var VideoWidgetModel, VideoWidgetControl, VideoDetailsMediaFrame;
/**
* Custom video details frame that removes the replace-video state.
*
* @class wp.mediaWidgets.controlConstructors~VideoDetailsMediaFrame
* @augments wp.media.view.MediaFrame.VideoDetails
*
* @private
*/
VideoDetailsMediaFrame = wp.media.view.MediaFrame.VideoDetails.extend(/** @lends wp.mediaWidgets.controlConstructors~VideoDetailsMediaFrame.prototype */{
/**
* Create the default states.
*
* @return {void}
*/
createStates: function createStates() {
this.states.add([
new wp.media.controller.VideoDetails({
media: this.media
}),
new wp.media.controller.MediaLibrary({
type: 'video',
id: 'add-video-source',
title: wp.media.view.l10n.videoAddSourceTitle,
toolbar: 'add-video-source',
media: this.media,
menu: false
}),
new wp.media.controller.MediaLibrary({
type: 'text',
id: 'add-track',
title: wp.media.view.l10n.videoAddTrackTitle,
toolbar: 'add-track',
media: this.media,
menu: 'video-details'
})
]);
}
});
/**
* Video widget model.
*
* See WP_Widget_Video::enqueue_admin_scripts() for amending prototype from PHP exports.
*
* @class wp.mediaWidgets.modelConstructors.media_video
* @augments wp.mediaWidgets.MediaWidgetModel
*/
VideoWidgetModel = component.MediaWidgetModel.extend({});
/**
* Video widget control.
*
* See WP_Widget_Video::enqueue_admin_scripts() for amending prototype from PHP exports.
*
* @class wp.mediaWidgets.controlConstructors.media_video
* @augments wp.mediaWidgets.MediaWidgetControl
*/
VideoWidgetControl = component.MediaWidgetControl.extend(/** @lends wp.mediaWidgets.controlConstructors.media_video.prototype */{
/**
* Show display settings.
*
* @type {boolean}
*/
showDisplaySettings: false,
/**
* Cache of oembed responses.
*
* @type {Object}
*/
oembedResponses: {},
/**
* Map model props to media frame props.
*
* @param {Object} modelProps - Model props.
* @return {Object} Media frame props.
*/
mapModelToMediaFrameProps: function mapModelToMediaFrameProps( modelProps ) {
var control = this, mediaFrameProps;
mediaFrameProps = component.MediaWidgetControl.prototype.mapModelToMediaFrameProps.call( control, modelProps );
mediaFrameProps.link = 'embed';
return mediaFrameProps;
},
/**
* Fetches embed data for external videos.
*
* @return {void}
*/
fetchEmbed: function fetchEmbed() {
var control = this, url;
url = control.model.get( 'url' );
// If we already have a local cache of the embed response, return.
if ( control.oembedResponses[ url ] ) {
return;
}
// If there is an in-flight embed request, abort it.
if ( control.fetchEmbedDfd && 'pending' === control.fetchEmbedDfd.state() ) {
control.fetchEmbedDfd.abort();
}
control.fetchEmbedDfd = wp.apiRequest({
url: wp.media.view.settings.oEmbedProxyUrl,
data: {
url: control.model.get( 'url' ),
maxwidth: control.model.get( 'width' ),
maxheight: control.model.get( 'height' ),
discover: false
},
type: 'GET',
dataType: 'json',
context: control
});
control.fetchEmbedDfd.done( function( response ) {
control.oembedResponses[ url ] = response;
control.renderPreview();
});
control.fetchEmbedDfd.fail( function() {
control.oembedResponses[ url ] = null;
});
},
/**
* Whether a url is a supported external host.
*
* @deprecated since 4.9.
*
* @return {boolean} Whether url is a supported video host.
*/
isHostedVideo: function isHostedVideo() {
return true;
},
/**
* Render preview.
*
* @return {void}
*/
renderPreview: function renderPreview() {
var control = this, previewContainer, previewTemplate, attachmentId, attachmentUrl, poster, html = '', isOEmbed = false, mime, error, urlParser, matches;
attachmentId = control.model.get( 'attachment_id' );
attachmentUrl = control.model.get( 'url' );
error = control.model.get( 'error' );
if ( ! attachmentId && ! attachmentUrl ) {
return;
}
// Verify the selected attachment mime is supported.
mime = control.selectedAttachment.get( 'mime' );
if ( mime && attachmentId ) {
if ( ! _.contains( _.values( wp.media.view.settings.embedMimes ), mime ) ) {
error = 'unsupported_file_type';
}
} else if ( ! attachmentId ) {
urlParser = document.createElement( 'a' );
urlParser.href = attachmentUrl;
matches = urlParser.pathname.toLowerCase().match( /\.(\w+)$/ );
if ( matches ) {
if ( ! _.contains( _.keys( wp.media.view.settings.embedMimes ), matches[1] ) ) {
error = 'unsupported_file_type';
}
} else {
isOEmbed = true;
}
}
if ( isOEmbed ) {
control.fetchEmbed();
if ( control.oembedResponses[ attachmentUrl ] ) {
poster = control.oembedResponses[ attachmentUrl ].thumbnail_url;
html = control.oembedResponses[ attachmentUrl ].html.replace( /\swidth="\d+"/, ' width="100%"' ).replace( /\sheight="\d+"/, '' );
}
}
previewContainer = control.$el.find( '.media-widget-preview' );
previewTemplate = wp.template( 'wp-media-widget-video-preview' );
previewContainer.html( previewTemplate({
model: {
attachment_id: attachmentId,
html: html,
src: attachmentUrl,
poster: poster
},
is_oembed: isOEmbed,
error: error
}));
wp.mediaelement.initialize();
},
/**
* Open the media image-edit frame to modify the selected item.
*
* @return {void}
*/
editMedia: function editMedia() {
var control = this, mediaFrame, metadata, updateCallback;
metadata = control.mapModelToMediaFrameProps( control.model.toJSON() );
// Set up the media frame.
mediaFrame = new VideoDetailsMediaFrame({
frame: 'video',
state: 'video-details',
metadata: metadata
});
wp.media.frame = mediaFrame;
mediaFrame.$el.addClass( 'media-widget' );
updateCallback = function( mediaFrameProps ) {
// Update cached attachment object to avoid having to re-fetch. This also triggers re-rendering of preview.
control.selectedAttachment.set( mediaFrameProps );
control.model.set( _.extend(
_.omit( control.model.defaults(), 'title' ),
control.mapMediaToModelProps( mediaFrameProps ),
{ error: false }
) );
};
mediaFrame.state( 'video-details' ).on( 'update', updateCallback );
mediaFrame.state( 'replace-video' ).on( 'replace', updateCallback );
mediaFrame.on( 'close', function() {
mediaFrame.detach();
});
mediaFrame.open();
}
});
// Exports.
component.controlConstructors.media_video = VideoWidgetControl;
component.modelConstructors.media_video = VideoWidgetModel;
})( wp.mediaWidgets );