Changeset 27212
- Timestamp:
- 02/20/2014 06:06:31 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/js/media-editor.js
r27131 r27212 273 273 return html; 274 274 } 275 }; 276 277 /** 278 * wp.media.collection 279 * @namespace 280 */ 281 wp.media.collection = { 282 attachments : function ( prop, type ) { 283 /** 284 * Retrieve attachments based on the properties of the passed shortcode 285 * 286 * @global wp.media.query 287 * 288 * @param {wp.shortcode} shortcode An instance of wp.shortcode(). 289 * @returns {wp.media.model.Attachments} A Backbone.Collection containing 290 * the media items belonging to a collection. 291 * The 'prop' specified by the passed prop is a Backbone.Model 292 * containing the 'props' for the gallery. 293 */ 294 return function( shortcode ) { 295 var shortcodeString = shortcode.string(), 296 result = cache[ shortcodeString ], 297 attrs, args, query, others; 298 299 delete cache[ shortcodeString ]; 300 301 if ( result ) { 302 return result; 303 } 304 305 // Fill the default shortcode attributes. 306 attrs = _.defaults( shortcode.attrs.named, this.defaults ); 307 args = _.pick( attrs, 'orderby', 'order' ); 308 309 args.type = type; 310 args.perPage = -1; 311 312 // Mark the `orderby` override attribute. 313 if ( undefined !== attrs.orderby ) { 314 attrs._orderByField = attrs.orderby; 315 } 316 317 if ( 'rand' === attrs.orderby ) { 318 attrs._orderbyRandom = true; 319 } 320 321 if ( -1 !== jQuery.inArray( prop, ['playlist', 'video-playlist'] ) ) { 322 _.each(['tracknumbers', 'tracklist', 'images'], function (setting) { 323 if ( 'undefined' === typeof attrs[setting] ) { 324 attrs['_' + setting] = wp.media[ prop ].defaults[ setting ]; 325 } else if ( 'true' === attrs[setting] || true === attrs[setting] ) { 326 attrs['_' + setting] = true; 327 } 328 }); 329 } 330 331 // Map the `orderby` attribute to the corresponding model property. 332 if ( ! attrs.orderby || /^menu_order(?: ID)?$/i.test( attrs.orderby ) ) { 333 args.orderby = 'menuOrder'; 334 } 335 336 // Map the `ids` param to the correct query args. 337 if ( attrs.ids ) { 338 args.post__in = attrs.ids.split(','); 339 args.orderby = 'post__in'; 340 } else if ( attrs.include ) { 341 args.post__in = attrs.include.split(','); 342 } 343 344 if ( attrs.exclude ) { 345 args.post__not_in = attrs.exclude.split(','); 346 } 347 348 if ( ! args.post__in ) { 349 args.uploadedTo = attrs.id; 350 } 351 352 // Collect the attributes that were not included in `args`. 353 others = _.omit( attrs, 'id', 'ids', 'include', 'exclude', 'orderby', 'order' ); 354 355 query = wp.media.query( args ); 356 query[ prop ] = new Backbone.Model( others ); 357 return query; 358 }; 359 }, 360 361 shortcodeAttrs : function ( prop, attachments ) { 362 var props = attachments.props.toJSON(), 363 attrs = _.pick( props, 'orderby', 'order', 'style' ); 364 365 if ( attachments[ prop ] ) { 366 _.extend( attrs, attachments[ prop ].toJSON() ); 367 } 368 369 // Convert all collection shortcodes to use the `ids` property. 370 // Ignore `post__in` and `post__not_in`; the attachments in 371 // the collection will already reflect those properties. 372 attrs.ids = attachments.pluck('id'); 373 374 // Copy the `uploadedTo` post ID. 375 if ( props.uploadedTo ) { 376 attrs.id = props.uploadedTo; 377 } 378 379 // Check if the collection is randomly ordered. 380 delete attrs.orderby; 381 382 if ( attrs._orderbyRandom ) { 383 attrs.orderby = 'rand'; 384 } else if ( attrs._orderByField && attrs._orderByField != 'rand' ) { 385 attrs.orderby = attrs._orderByField; 386 } 387 388 delete attrs._orderbyRandom; 389 delete attrs._orderByField; 390 391 // If the `ids` attribute is set and `orderby` attribute 392 // is the default value, clear it for cleaner output. 393 if ( attrs.ids && 'post__in' === attrs.orderby ) { 394 delete attrs.orderby; 395 } 396 397 if ( -1 !== jQuery.inArray( prop, ['playlist', 'video-playlist'] ) ) { 398 _.each(['tracknumbers', 'tracklist', 'images'], function (setting) { 399 if ( attrs['_' + setting] ) { 400 attrs[setting] = true; 401 } else { 402 attrs[setting] = false; 403 } 404 delete attrs['_' + setting]; 405 }); 406 } 407 408 // Remove default attributes from the shortcode. 409 _.each( this.defaults, function( value, key ) { 410 if ( value === attrs[ key ] ) { 411 delete attrs[ key ]; 412 } 413 }); 414 return attrs; 415 }, 416 417 editSelection : function ( prop, shortcode ) { 418 var defaultPostId = wp.media[ prop ].defaults.id, 419 attachments, selection; 420 421 // Ignore the rest of the match object. 422 shortcode = shortcode.shortcode; 423 424 if ( _.isUndefined( shortcode.get('id') ) && ! _.isUndefined( defaultPostId ) ) { 425 shortcode.set( 'id', defaultPostId ); 426 } 427 428 attachments = wp.media[ prop ].attachments( shortcode ); 429 430 selection = new wp.media.model.Selection( attachments.models, { 431 props: attachments.props.toJSON(), 432 multiple: true 433 }); 434 435 selection[ prop ] = attachments[ prop ]; 436 437 // Fetch the query's attachments, and then break ties from the 438 // query to allow for sorting. 439 selection.more().done( function() { 440 // Break ties with the query. 441 selection.props.set({ query: false }); 442 selection.unmirror(); 443 selection.props.unset('orderby'); 444 }); 445 446 return selection; 447 }, 448 449 /** 450 * 451 * @param {string} prop The shortcode slug 452 * @param {wp.media.model.Attachments} attachments 453 * @param {wp.shortcode} shortcode 454 * @returns {wp.shortcode} 455 */ 456 cacheShortcode : function ( prop, attachments, shortcode ) { 457 // Use a cloned version of the playlist. 458 var clone = new wp.media.model.Attachments( attachments.models, { 459 props: attachments.props.toJSON() 460 }); 461 clone[ prop ] = attachments[ prop ]; 462 cache[ shortcode.string() ] = clone; 463 464 return shortcode; 465 }, 466 467 getEditFrame : function ( args ) { 468 // Destroy the previous gallery frame. 469 if ( this.frame ) { 470 this.frame.dispose(); 471 } 472 473 // Store the current gallery frame. 474 this.frame = wp.media( _.extend( { 475 frame: 'post', 476 editing: true, 477 multiple: true, 478 }, args ) ).open(); 479 480 return this.frame; 481 }, 482 483 instance : function ( prop, args ) { 484 return { 485 attachments: this.attachments( prop, args.type ), 486 /** 487 * Triggered when clicking 'Insert {label}' or 'Update {label}' 488 * 489 * @global wp.shortcode 490 * @global wp.media.model.Attachments 491 * 492 * @param {wp.media.model.Attachments} attachments A Backbone.Collection containing 493 * the media items belonging to a collection. 494 * The 'prop' specified by the passed prop is a Backbone.Model 495 * containing the 'props' for the gallery. 496 * @returns {wp.shortcode} 497 */ 498 shortcode: function( attachments ) { 499 var shortcode = new wp.shortcode({ 500 tag: prop, 501 attrs: wp.media.collection.shortcodeAttrs( prop, attachments ), 502 type: 'single' 503 }); 504 505 return wp.media.collection.cacheShortcode( prop, attachments, shortcode ); 506 }, 507 /** 508 * Triggered when double-clicking a collection shortcode placeholder 509 * in the editor 510 * 511 * @global wp.shortcode 512 * @global wp.media.model.Selection 513 * @global wp.media.view.l10n 514 * 515 * @param {string} content Content that is searched for possible 516 * shortcode markup matching the passed tag name, 517 * 518 * @this wp.media.{prop} 519 * 520 * @returns {wp.media.view.MediaFrame.Select} A media workflow. 521 */ 522 edit: function( content ) { 523 var shortcode = wp.shortcode.next( prop, content ); 524 525 // Bail if we didn't match the shortcode or all of the content. 526 if ( ! shortcode || shortcode.content !== content ) { 527 return; 528 } 529 530 return wp.media.collection.getEditFrame( { 531 title: args.title, 532 state: prop + '-edit', 533 selection: wp.media.collection.editSelection( prop, shortcode ) 534 } ); 535 } 536 }; 537 } 275 538 }; 276 539
Note: See TracChangeset
for help on using the changeset viewer.