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
|
/**
* @output wp-includes/js/media-models.js
*/
var Attachment, Attachments, l10n, media;
/** @namespace wp */
window.wp = window.wp || {};
/**
* Create and return a media frame.
*
* Handles the default media experience.
*
* @alias wp.media
* @memberOf wp
* @namespace
*
* @param {Object} attributes The properties passed to the main media controller.
* @return {wp.media.view.MediaFrame} A media workflow.
*/
media = wp.media = function( attributes ) {
var MediaFrame = media.view.MediaFrame,
frame;
if ( ! MediaFrame ) {
return;
}
attributes = _.defaults( attributes || {}, {
frame: 'select'
});
if ( 'select' === attributes.frame && MediaFrame.Select ) {
frame = new MediaFrame.Select( attributes );
} else if ( 'post' === attributes.frame && MediaFrame.Post ) {
frame = new MediaFrame.Post( attributes );
} else if ( 'manage' === attributes.frame && MediaFrame.Manage ) {
frame = new MediaFrame.Manage( attributes );
} else if ( 'image' === attributes.frame && MediaFrame.ImageDetails ) {
frame = new MediaFrame.ImageDetails( attributes );
} else if ( 'audio' === attributes.frame && MediaFrame.AudioDetails ) {
frame = new MediaFrame.AudioDetails( attributes );
} else if ( 'video' === attributes.frame && MediaFrame.VideoDetails ) {
frame = new MediaFrame.VideoDetails( attributes );
} else if ( 'edit-attachments' === attributes.frame && MediaFrame.EditAttachments ) {
frame = new MediaFrame.EditAttachments( attributes );
}
delete attributes.frame;
media.frame = frame;
return frame;
};
/** @namespace wp.media.model */
/** @namespace wp.media.view */
/** @namespace wp.media.controller */
/** @namespace wp.media.frames */
_.extend( media, { model: {}, view: {}, controller: {}, frames: {} });
// Link any localized strings.
l10n = media.model.l10n = window._wpMediaModelsL10n || {};
// Link any settings.
media.model.settings = l10n.settings || {};
delete l10n.settings;
Attachment = media.model.Attachment = require( '../../../media/models/attachment.js' );
Attachments = media.model.Attachments = require( '../../../media/models/attachments.js' );
media.model.Query = require( '../../../media/models/query.js' );
media.model.PostImage = require( '../../../media/models/post-image.js' );
media.model.Selection = require( '../../../media/models/selection.js' );
/**
* ========================================================================
* UTILITIES
* ========================================================================
*/
/**
* A basic equality comparator for Backbone models.
*
* Used to order models within a collection - @see wp.media.model.Attachments.comparator().
*
* @param {mixed} a The primary parameter to compare.
* @param {mixed} b The primary parameter to compare.
* @param {string} ac The fallback parameter to compare, a's cid.
* @param {string} bc The fallback parameter to compare, b's cid.
* @return {number} -1: a should come before b.
* 0: a and b are of the same rank.
* 1: b should come before a.
*/
media.compare = function( a, b, ac, bc ) {
if ( _.isEqual( a, b ) ) {
return ac === bc ? 0 : (ac > bc ? -1 : 1);
} else {
return a > b ? -1 : 1;
}
};
_.extend( media, /** @lends wp.media */{
/**
* media.template( id )
*
* Fetch a JavaScript template for an id, and return a templating function for it.
*
* See wp.template() in `wp-includes/js/wp-util.js`.
*
* @borrows wp.template as template
*/
template: wp.template,
/**
* media.post( [action], [data] )
*
* Sends a POST request to WordPress.
* See wp.ajax.post() in `wp-includes/js/wp-util.js`.
*
* @borrows wp.ajax.post as post
*/
post: wp.ajax.post,
/**
* media.ajax( [action], [options] )
*
* Sends an XHR request to WordPress.
* See wp.ajax.send() in `wp-includes/js/wp-util.js`.
*
* @borrows wp.ajax.send as ajax
*/
ajax: wp.ajax.send,
/**
* Scales a set of dimensions to fit within bounding dimensions.
*
* @param {Object} dimensions
* @return {Object}
*/
fit: function( dimensions ) {
var width = dimensions.width,
height = dimensions.height,
maxWidth = dimensions.maxWidth,
maxHeight = dimensions.maxHeight,
constraint;
/*
* Compare ratios between the two values to determine
* which max to constrain by. If a max value doesn't exist,
* then the opposite side is the constraint.
*/
if ( ! _.isUndefined( maxWidth ) && ! _.isUndefined( maxHeight ) ) {
constraint = ( width / height > maxWidth / maxHeight ) ? 'width' : 'height';
} else if ( _.isUndefined( maxHeight ) ) {
constraint = 'width';
} else if ( _.isUndefined( maxWidth ) && height > maxHeight ) {
constraint = 'height';
}
// If the value of the constrained side is larger than the max,
// then scale the values. Otherwise return the originals; they fit.
if ( 'width' === constraint && width > maxWidth ) {
return {
width : maxWidth,
height: Math.round( maxWidth * height / width )
};
} else if ( 'height' === constraint && height > maxHeight ) {
return {
width : Math.round( maxHeight * width / height ),
height: maxHeight
};
} else {
return {
width : width,
height: height
};
}
},
/**
* Truncates a string by injecting an ellipsis into the middle.
* Useful for filenames.
*
* @param {string} string
* @param {number} [length=30]
* @param {string} [replacement=…]
* @return {string} The string, unless length is greater than string.length.
*/
truncate: function( string, length, replacement ) {
length = length || 30;
replacement = replacement || '…';
if ( string.length <= length ) {
return string;
}
return string.substr( 0, length / 2 ) + replacement + string.substr( -1 * length / 2 );
}
});
/**
* ========================================================================
* MODELS
* ========================================================================
*/
/**
* wp.media.attachment
*
* @static
* @param {string} id A string used to identify a model.
* @return {wp.media.model.Attachment}
*/
media.attachment = function( id ) {
return Attachment.get( id );
};
/**
* A collection of all attachments that have been fetched from the server.
*
* @static
* @member {wp.media.model.Attachments}
*/
Attachments.all = new Attachments();
/**
* wp.media.query
*
* Shorthand for creating a new Attachments Query.
*
* @param {Object} [props]
* @return {wp.media.model.Attachments}
*/
media.query = function( props ) {
return new Attachments( null, {
props: _.extend( _.defaults( props || {}, { orderby: 'date' } ), { query: true } )
});
};
|