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
|
var l10n = wp.media.view.l10n,
$ = Backbone.$,
Embed;
/**
* wp.media.controller.Embed
*
* A state for embedding media from a URL.
*
* @memberOf wp.media.controller
*
* @class
* @augments wp.media.controller.State
* @augments Backbone.Model
*
* @param {object} attributes The attributes hash passed to the state.
* @param {string} [attributes.id=embed] Unique identifier.
* @param {string} [attributes.title=Insert From URL] Title for the state. Displays in the media menu and the frame's title region.
* @param {string} [attributes.content=embed] Initial mode for the content region.
* @param {string} [attributes.menu=default] Initial mode for the menu region.
* @param {string} [attributes.toolbar=main-embed] Initial mode for the toolbar region.
* @param {string} [attributes.menu=false] Initial mode for the menu region.
* @param {int} [attributes.priority=120] The priority for the state link in the media menu.
* @param {string} [attributes.type=link] The type of embed. Currently only link is supported.
* @param {string} [attributes.url] The embed URL.
* @param {object} [attributes.metadata={}] Properties of the embed, which will override attributes.url if set.
*/
Embed = wp.media.controller.State.extend(/** @lends wp.media.controller.Embed.prototype */{
defaults: {
id: 'embed',
title: l10n.insertFromUrlTitle,
content: 'embed',
menu: 'default',
toolbar: 'main-embed',
priority: 120,
type: 'link',
url: '',
metadata: {}
},
// The amount of time used when debouncing the scan.
sensitivity: 400,
initialize: function(options) {
this.metadata = options.metadata;
this.debouncedScan = _.debounce( _.bind( this.scan, this ), this.sensitivity );
this.props = new Backbone.Model( this.metadata || { url: '' });
this.props.on( 'change:url', this.debouncedScan, this );
this.props.on( 'change:url', this.refresh, this );
this.on( 'scan', this.scanImage, this );
},
/**
* Trigger a scan of the embedded URL's content for metadata required to embed.
*
* @fires wp.media.controller.Embed#scan
*/
scan: function() {
var scanners,
embed = this,
attributes = {
type: 'link',
scanners: []
};
/*
* Scan is triggered with the list of `attributes` to set on the
* state, useful for the 'type' attribute and 'scanners' attribute,
* an array of promise objects for asynchronous scan operations.
*/
if ( this.props.get('url') ) {
this.trigger( 'scan', attributes );
}
if ( attributes.scanners.length ) {
scanners = attributes.scanners = $.when.apply( $, attributes.scanners );
scanners.always( function() {
if ( embed.get('scanners') === scanners ) {
embed.set( 'loading', false );
}
});
} else {
attributes.scanners = null;
}
attributes.loading = !! attributes.scanners;
this.set( attributes );
},
/**
* Try scanning the embed as an image to discover its dimensions.
*
* @param {Object} attributes
*/
scanImage: function( attributes ) {
var frame = this.frame,
state = this,
url = this.props.get('url'),
image = new Image(),
deferred = $.Deferred();
attributes.scanners.push( deferred.promise() );
// Try to load the image and find its width/height.
image.onload = function() {
deferred.resolve();
if ( state !== frame.state() || url !== state.props.get('url') ) {
return;
}
state.set({
type: 'image'
});
state.props.set({
width: image.width,
height: image.height
});
};
image.onerror = deferred.reject;
image.src = url;
},
refresh: function() {
this.frame.toolbar.get().refresh();
},
reset: function() {
this.props.clear().set({ url: '' });
if ( this.active ) {
this.refresh();
}
}
});
module.exports = Embed;
|