522 | | }, |
523 | | |
524 | | buildComposite: function() { |
525 | | var original = this.get('_library'), |
526 | | exclude = this.get('exclude'), |
527 | | composite; |
528 | | |
529 | | this.destroyComposite(); |
530 | | if ( ! this.get('exclude') ) |
531 | | return; |
532 | | |
533 | | // Remember the state's original library. |
534 | | if ( ! original ) |
535 | | this.set( '_library', original = this.get('library') ); |
536 | | |
537 | | // Create a composite library in its place. |
538 | | composite = new media.model.Attachments( null, { |
539 | | props: _.pick( original.props.toJSON(), 'order', 'orderby' ) |
540 | | }); |
541 | | |
542 | | // Accepts attachments that exist in the original library and |
543 | | // that do not exist in the excluded library. |
544 | | composite.validator = function( attachment ) { |
545 | | return !! original.getByCid( attachment.cid ) && ! exclude.getByCid( attachment.cid ); |
546 | | }; |
547 | | |
548 | | composite.mirror( original ).observe( exclude ); |
549 | | |
550 | | this.set( 'library', composite ); |
551 | | }, |
552 | | |
553 | | destroyComposite: function() { |
554 | | var composite = this.get('library'), |
555 | | original = this.get('_library'); |
556 | | |
557 | | if ( ! original ) |
558 | | return; |
559 | | |
560 | | composite.unobserve(); |
561 | | this.set( 'library', original ); |
562 | | this.unset('_library'); |
563 | | }, |
564 | | |
565 | | _excludeState: function() { |
566 | | var current = this.get('excludeState'), |
567 | | previous = this.previous('excludeState'); |
568 | | |
569 | | if ( previous ) |
570 | | this.frame.state( previous ).off( 'change:library', this._excludeStateLibrary, this ); |
571 | | |
572 | | if ( current ) |
573 | | this.frame.state( current ).on( 'change:library', this._excludeStateLibrary, this ); |
574 | | }, |
575 | | |
576 | | _excludeStateLibrary: function() { |
577 | | var current = this.get('excludeState'); |
578 | | |
579 | | if ( ! current ) |
580 | | return; |
581 | | |
582 | | this.set( 'exclude', this.frame.state( current ).get('library') ); |
| 593 | // wp.media.controller.GalleryAdd |
| 594 | // --------------------------------- |
| 595 | media.controller.GalleryAdd = media.controller.Library.extend({ |
| 596 | defaults: _.defaults({ |
| 597 | id: 'gallery-library', |
| 598 | filterable: 'uploaded', |
| 599 | multiple: 'add', |
| 600 | menu: 'gallery', |
| 601 | toolbar: 'gallery-add', |
| 602 | title: l10n.addToGalleryTitle, |
| 603 | priority: 100 |
| 604 | }, media.controller.Library.prototype.defaults ), |
| 605 | |
| 606 | initialize: function() { |
| 607 | // If we haven't been provided a `library`, create a `Selection`. |
| 608 | if ( ! this.get('library') ) |
| 609 | this.set( 'library', media.query({ type: 'image' }) ); |
| 610 | |
| 611 | media.controller.Library.prototype.initialize.apply( this, arguments ); |
| 612 | }, |
| 613 | |
| 614 | activate: function() { |
| 615 | var library = this.get('library'), |
| 616 | edit = this.frame.state('gallery-edit').get('library'); |
| 617 | |
| 618 | if ( this.editLibrary && this.editLibrary !== edit ) |
| 619 | library.unobserve( this.editLibrary ); |
| 620 | |
| 621 | // Accepts attachments that exist in the original library and |
| 622 | // that do not exist in gallery's library. |
| 623 | library.validator = function( attachment ) { |
| 624 | return !! this.mirroring.getByCid( attachment.cid ) && ! edit.getByCid( attachment.cid ) && media.model.Selection.prototype.validator.apply( this, arguments ); |
| 625 | }; |
| 626 | |
| 627 | library.observe( edit ); |
| 628 | this.editLibrary = edit; |
| 629 | |
| 630 | media.controller.Library.prototype.activate.apply( this, arguments ); |
| 631 | } |
| 632 | }); |
| 633 | |