Make WordPress Core

Changeset 27841


Ignore:
Timestamp:
03/29/2014 08:01:13 AM (11 years ago)
Author:
nacin
Message:

Media Manager: Hide 'Create Playlist' menu items until the user has uploaded audio or video.

props gcorne.
fixes #27554.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/js/media-views.js

    r27840 r27841  
    18861886    media.view.MediaFrame.Post = media.view.MediaFrame.Select.extend({
    18871887        initialize: function() {
     1888            this.counts = {
     1889                audio: {
     1890                    count: media.view.settings.attachmentCounts.audio,
     1891                    state: 'playlist'
     1892                },
     1893                video: {
     1894                    count: media.view.settings.attachmentCounts.video,
     1895                    state: 'video-playlist'
     1896                }
     1897            };
     1898
    18881899            _.defaults( this.options, {
    18891900                multiple:  true,
     
    18961907            media.view.MediaFrame.Select.prototype.initialize.apply( this, arguments );
    18971908            this.createIframeStates();
     1909
    18981910        },
    18991911
     
    20332045
    20342046        bindHandlers: function() {
    2035             /**
    2036              * call 'bindHandlers' directly on the parent class
    2037              */
     2047            var handlers, checkCounts;
     2048
    20382049            media.view.MediaFrame.Select.prototype.bindHandlers.apply( this, arguments );
     2050
     2051            this.on( 'activate', this.activate, this );
     2052
     2053            // Only bother checking media type counts if one of the counts is zero
     2054            checkCounts = _.find( this.counts, function( type ) {
     2055                return type.count === 0;
     2056            } );
     2057
     2058            if ( typeof checkCounts !== 'undefined' ) {
     2059                this.listenTo( media.model.Attachments.all, 'change:type', this.mediaTypeCounts );
     2060            }
     2061
    20392062            this.on( 'menu:create:gallery', this.createMenu, this );
    20402063            this.on( 'menu:create:playlist', this.createMenu, this );
     
    20472070            this.on( 'toolbar:create:main-embed', this.mainEmbedToolbar, this );
    20482071
    2049             var handlers = {
     2072            handlers = {
    20502073                menu: {
    20512074                    'default': 'mainMenu',
     
    20822105        },
    20832106
     2107        activate: function() {
     2108            // Hide menu items for states tied to particular media types if there are no items
     2109            _.each( this.counts, function( type ) {
     2110                if ( type.count < 1 ) {
     2111                    this.menuItemVisibility( type.state, 'hide' );
     2112                }
     2113            }, this );
     2114        },
     2115
     2116        mediaTypeCounts: function( model, attr ) {
     2117            if ( typeof this.counts[ attr ] !== 'undefined' && this.counts[ attr ].count < 1 ) {
     2118                this.counts[ attr ].count++;
     2119                this.menuItemVisibility( this.counts[ attr ].state, 'show' );
     2120            }
     2121        },
     2122
    20842123        // Menus
    20852124        /**
     
    20932132                })
    20942133            });
     2134        },
     2135
     2136        menuItemVisibility: function( state, visibility ) {
     2137            var menu = this.menu.get();
     2138            if ( visibility === 'hide' ) {
     2139                menu.hide( state );
     2140            } else if ( visibility === 'show' ) {
     2141                menu.show( state );
     2142            }
    20952143        },
    20962144        /**
     
    40674115        deselect: function() {
    40684116            this.$el.children().removeClass('active');
     4117        },
     4118
     4119        hide: function( id ) {
     4120            var view = this.get( id );
     4121
     4122            if ( ! view ) {
     4123                return;
     4124            }
     4125
     4126            view.$el.addClass('hidden');
     4127        },
     4128
     4129        show: function( id ) {
     4130            var view = this.get( id );
     4131
     4132            if ( ! view ) {
     4133                return;
     4134            }
     4135
     4136            view.$el.removeClass('hidden');
    40694137        }
    40704138    });
Note: See TracChangeset for help on using the changeset viewer.