Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 40.41 KB | None | 0 0
  1. window.wp = window.wp || {};
  2. window.eml = window.eml || { l10n: {} };
  3.  
  4.  
  5. ( function( $, _ ) {
  6.  
  7.     var media = wp.media,
  8.         l10n = media.view.l10n,
  9.         l10n_defaults = { media_orderby: 'date', media_order: 'DESC' },
  10.         mediaTrash = media.view.settings.mediaTrash,
  11.         original = {};
  12.  
  13.  
  14.     _.extend( eml.l10n, wpuxss_eml_media_views_l10n );
  15.     _.defaults( eml.l10n, l10n_defaults );
  16.  
  17.  
  18.  
  19.     /**
  20.      * wp.media.controller.Library
  21.      *
  22.      */
  23.     original.controllerLibrary = {
  24.  
  25.         activate: media.controller.Library.prototype.activate
  26.     };
  27.  
  28.     _.extend( media.controller.Library.prototype.defaults, {
  29.         idealColumnWidth   : $( window ).width() < 640 ? 120 : 135
  30.     });
  31.  
  32.     _.extend( media.controller.Library.prototype, {
  33.  
  34.         activate: function() {
  35.  
  36.             original.controllerLibrary.activate.apply( this, arguments );
  37.  
  38.             wp.Uploader.queue.on( 'add', this.beforeUpload, this );
  39.             wp.Uploader.queue.on( 'reset', this.afterUpload, this );
  40.         },
  41.  
  42.         beforeUpload: function() {
  43.  
  44.             if ( wp.Uploader.queue.length == 1 ) {
  45.                 $('.attachment-filters:has(option[value!="all"]:selected)').val( 'all' ).change();
  46.             }
  47.         },
  48.  
  49.         afterUpload: function() {
  50.  
  51.             var library = this.get( 'library' ),
  52.                 selection = this.get( 'selection' ),
  53.                 orderby = library.props.get( 'orderby' );
  54.  
  55.  
  56.             if ( 'menuOrder' === orderby ) {
  57.                 library.saveMenuOrder();
  58.             }
  59.  
  60.             library.reset( library.models );
  61.  
  62.             selection.trigger( 'selection:unsingle', selection.model, selection );
  63.             selection.trigger( 'selection:single', selection.model, selection );
  64.         },
  65.  
  66.         uploading: function( attachment ) {
  67.  
  68.             var content = this.frame.content,
  69.                 selection = this.get( 'selection' );
  70.  
  71.  
  72.             if ( 'upload' === content.mode() ) {
  73.                 this.frame.content.mode('browse');
  74.             }
  75.  
  76.             if ( this.get( 'autoSelect' ) ) {
  77.  
  78.                 if ( wp.Uploader.queue.length == 1 && selection.length ) {
  79.                     selection.reset();
  80.                 }
  81.                 selection.add( attachment );
  82.                 selection.trigger( 'selection:unsingle', selection.model, selection );
  83.                 selection.trigger( 'selection:single', selection.model, selection );
  84.             }
  85.         }
  86.     });
  87.  
  88.  
  89.  
  90.     /**
  91.      * wp.media.view.AttachmentCompat
  92.      *
  93.      */
  94.  
  95.     _.extend( media.view.AttachmentCompat.prototype, {
  96.  
  97.         save: function( event ) {
  98.  
  99.             var data = {},
  100.                 spinner;
  101.  
  102.  
  103.             if ( event ) {
  104.                 event.preventDefault();
  105.             }
  106.  
  107.             if ( this.controller.isModeActive( 'eml-grid' ) ) {
  108.                 spinner = this.controller.browserView.toolbar.get( 'spinner' );
  109.             }
  110.  
  111.             _.each( this.$el.serializeArray(), function( pair ) {
  112.                 data[ pair.name ] = pair.value;
  113.             });
  114.  
  115.             $( 'input', this.$el ).prop('disabled', true);
  116.             if ( spinner ) {
  117.                 spinner.show();
  118.             }
  119.  
  120.             this.noRender = true;
  121.             media.model.Query.cleanQueries();
  122.  
  123.             this.controller.trigger( 'attachment:compat:waiting', ['waiting'] );
  124.             this.model.saveCompat( data ).done( _.bind( this.postSave, this, 1 ) ).fail( _.bind( this.postSave, this, 0 ) );
  125.         },
  126.  
  127.         postSave: function( success ) {
  128.  
  129.             var toolbar,
  130.                 spinner,
  131.                 emlMessage;
  132.  
  133.  
  134.             if ( 'edit-attachment' !== this.controller._state ) {
  135.                 toolbar = this.controller.toolbar.get();
  136.             }
  137.  
  138.             if ( this.controller.isModeActive( 'eml-grid' ) ) {
  139.                 spinner = this.controller.browserView.toolbar.get( 'spinner' );
  140.             }
  141.  
  142.  
  143.             $( 'input', this.$el ).prop('disabled', false);
  144.  
  145.             if ( spinner ) {
  146.                 spinner.hide();
  147.             }
  148.  
  149.             if ( toolbar ) {
  150.                 emlMessage = success ? toolbar.get( 'emlAttachmentSuccess' ) : toolbar.get( 'emlAttachmentError' );
  151.  
  152.                 emlMessage.$el.fadeIn( 200 );
  153.                 setTimeout( function() {
  154.                     emlMessage.$el.fadeOut( 100 );
  155.                 }, 800 );
  156.             }
  157.  
  158.             this.controller.trigger( 'attachment:compat:ready', ['ready'] );
  159.         },
  160.  
  161.         render: function() {
  162.  
  163.             var compat = this.model.get('compat'),
  164.                 $compat_el = this.$el,
  165.                 tcount = this.model.get('tcount');
  166.  
  167.  
  168.             _.each( tcount, function( count, term_id ) {
  169.  
  170.                 var $option = $( '.eml-taxonomy-filters option[value="'+term_id+'"]' ),
  171.                     text = $option.text();
  172.  
  173.                 text = text.replace( /\(.*?\)/, '('+count+')' );
  174.                 $option.text( text );
  175.             });
  176.  
  177.             if ( ! compat || ! compat.item ) {
  178.                 return;
  179.             }
  180.  
  181.             if ( this.noRender ) {
  182.                 return this;
  183.             }
  184.  
  185.             this.views.detach();
  186.             this.$el.html( compat.item );
  187.             this.views.render();
  188.  
  189.  
  190.             // TODO: find a better solution
  191.             if ( this.controller.isModeActive( 'select' ) &&
  192.                 'edit-attachment' !== this.controller._state ) {
  193.  
  194.                 $.each( eml.l10n.compat_taxonomies_to_hide, function( id, taxonomy ) {
  195.                     $compat_el.find( '.compat-field-'+taxonomy ).remove();
  196.                 });
  197.  
  198.                 if ( ! this.$el.find( '.compat-attachment-fields tbody' ).children().length ) {
  199.                     this.$el.find( '.media-types-required-info' ).hide();
  200.                 }
  201.             }
  202.  
  203.  
  204.             // TODO: find a better solution
  205.             $.each( eml.l10n.compat_taxonomies, function( id, taxonomy ) {
  206.  
  207.                 $compat_el.find( '.compat-field-'+taxonomy+' .label' ).addClass( 'eml-tax-label' );
  208.                 $compat_el.find( '.compat-field-'+taxonomy+' .field' ).addClass( 'eml-tax-field' );
  209.             });
  210.  
  211.             return this;
  212.         }
  213.     });
  214.  
  215.  
  216.  
  217.     /**
  218.      * wp.media.view.AttachmentFilters
  219.      *
  220.      */
  221.     _.extend( media.view.AttachmentFilters.prototype, {
  222.  
  223.         change: function() {
  224.  
  225.             var filter = this.filters[ this.el.value ],
  226.                 selection = this.controller.state().get( 'selection' ),
  227.                 resetFilterButton = this.controller.content.get().toolbar.get( 'resetFilterButton' ),
  228.  
  229.  
  230.                 all = $('.attachment-filters').length,
  231.                 unchanged = $('.attachment-filters').map(function(){
  232.                     return this.value
  233.                 }).get().filter( function( val ){
  234.                     return 'all' === val
  235.                 }).length;
  236.  
  237.  
  238.             if ( filter ) {
  239.                 this.model.set( filter.props );
  240.             }
  241.  
  242.  
  243.             if ( filter && selection && selection.length && wp.Uploader.queue.length !== 1 ) {
  244.                 selection.reset();
  245.             }
  246.  
  247.  
  248.             if ( filter && mediaTrash && ! _.isUndefined( this.controller.toolbar ) ) {
  249.                 this.controller.toolbar.get().$('.media-selection').toggleClass( 'trash', 'trash' === filter.props.status );
  250.             }
  251.  
  252.  
  253.             if ( _.isUndefined( resetFilterButton ) ) {
  254.                 return;
  255.             }
  256.  
  257.             resetFilterButton.model.set( 'disabled', all === unchanged );
  258.         },
  259.  
  260.         select: function() {
  261.  
  262.             var model = this.model,
  263.                 value = 'all',
  264.                 props = model.toJSON();
  265.  
  266.  
  267.             props = _.omit( props, 'orderby', 'order' );
  268.  
  269.             _.find( this.filters, function( filter, id ) {
  270.  
  271.                 var filterProps = _.omit( filter.props, 'orderby', 'order' );
  272.  
  273.                 var equal = _.all( filterProps, function( prop, key ) {
  274.                     return prop === ( _.isUndefined( props[ key ] ) ? null : props[ key ] );
  275.                 });
  276.  
  277.                 if ( equal ) {
  278.                     return value = id;
  279.                 }
  280.             });
  281.  
  282.             this.$el.val( value );
  283.         }
  284.     });
  285.  
  286.  
  287.  
  288.  
  289.     /**
  290.      * wp.media.view.AttachmentFilters
  291.      *
  292.      */
  293.     original.AttachmentFilters = {
  294.  
  295.         All: {
  296.             createFilters: media.view.AttachmentFilters.All.prototype.createFilters
  297.         },
  298.  
  299.         Uploaded: {
  300.             createFilters: media.view.AttachmentFilters.Uploaded.prototype.createFilters
  301.         }
  302.     };
  303.  
  304.  
  305.  
  306.     /**
  307.      * wp.media.view.AttachmentFilters.All
  308.      *
  309.      */
  310.     _.extend( media.view.AttachmentFilters.All.prototype, {
  311.  
  312.         createFilters: function() {
  313.  
  314.             var uncategorizedProps,
  315.                 taxonomies = _.intersection( _.keys( eml.l10n.taxonomies ), eml.l10n.filter_taxonomies );
  316.  
  317.  
  318.             original.AttachmentFilters.All.createFilters.apply( this, arguments );
  319.  
  320.             _.each( this.filters, function( filter, key ) {
  321.                 filter.props['uncategorized'] = null;
  322.                 filter.props['orderby'] = eml.l10n.media_orderby;
  323.                 filter.props['order'] = eml.l10n.media_order;
  324.             });
  325.  
  326.             this.filters.uncategorized = {
  327.                 text:  eml.l10n.uncategorized,
  328.                 props: {
  329.                     uploadedTo    : null,
  330.                     uncategorized : true,
  331.                     status        : null,
  332.                     type          : null,
  333.                     orderby       : eml.l10n.media_orderby,
  334.                     order         : eml.l10n.media_order
  335.                 },
  336.                 priority: 60
  337.             };
  338.  
  339.  
  340.             uncategorizedProps = this.filters.uncategorized.props;
  341.  
  342.             _.each( taxonomies, function( taxonomy ) {
  343.                 uncategorizedProps[taxonomy] = null;
  344.             });
  345.  
  346.  
  347.             if ( mediaTrash &&
  348.                 ( this.controller.isModeActive( 'grid' ) ||
  349.                 this.controller.isModeActive( 'eml-grid' ) ) ) {
  350.  
  351.                 this.filters.trash = {
  352.                     text:  l10n.trash,
  353.                     props: {
  354.                         uploadedTo : null,
  355.                         status     : 'trash',
  356.                         type       : null,
  357.                         orderby    : 'date',
  358.                         order      : 'DESC'
  359.                     },
  360.                     priority: 70
  361.                 };
  362.             }
  363.         }
  364.     });
  365.  
  366.  
  367.  
  368.     /**
  369.      * wp.media.view.AttachmentFilters.Uploaded
  370.      *
  371.      */
  372.     _.extend( media.view.AttachmentFilters.Uploaded.prototype, {
  373.  
  374.         createFilters: function() {
  375.  
  376.             var uncategorizedProps,
  377.                 taxonomies = _.intersection( _.keys( eml.l10n.taxonomies ), eml.l10n.filter_taxonomies );
  378.  
  379.  
  380.             original.AttachmentFilters.Uploaded.createFilters.apply( this, arguments );
  381.  
  382.             _.each( this.filters, function( filter, key ) {
  383.                 filter.props['orderby'] = eml.l10n.media_orderby;
  384.                 filter.props['order'] = eml.l10n.media_order;
  385.             });
  386.         }
  387.     });
  388.  
  389.  
  390.  
  391.     /**
  392.      * wp.media.view.AttachmentFilters.Taxonomy
  393.      *
  394.      */
  395.     media.view.AttachmentFilters.Taxonomy = media.view.AttachmentFilters.extend({
  396.  
  397.         id: function() {
  398.  
  399.             return 'media-attachment-'+this.options.taxonomy+'-filters';
  400.         },
  401.  
  402.         className: function() {
  403.  
  404.             // TODO: get rid of excess class name that duplicates id
  405.             return 'attachment-filters eml-taxonomy-filters attachment-'+this.options.taxonomy+'-filter';
  406.         },
  407.  
  408.         createFilters: function() {
  409.  
  410.             var filters = {},
  411.                 self = this;
  412.  
  413.  
  414.             _.each( self.options.termList || {}, function( term, key ) {
  415.  
  416.                 var term_id = term.term_id,
  417.                     term_row = $("<div/>").html(term.term_row).text();
  418.  
  419.                 filters[ term_id ] = {
  420.                     text: term_row,
  421.                     props: {
  422.                         uncategorized : null,
  423.                         orderby       : eml.l10n.media_orderby,
  424.                         order         : eml.l10n.media_order
  425.                     },
  426.                     priority: key+4
  427.                 };
  428.  
  429.                 filters[term_id]['props'][self.options.taxonomy] = term_id;
  430.             });
  431.  
  432.             filters.all = {
  433.                 text: eml.l10n.filter_by + ' ' + self.options.singularName,
  434.                 props: {
  435.                     uncategorized : null,
  436.                     orderby       : eml.l10n.media_orderby,
  437.                     order         : eml.l10n.media_order
  438.                 },
  439.                 priority: 1
  440.             };
  441.  
  442.             filters['all']['props'][self.options.taxonomy] = null;
  443.  
  444.             filters.in = {
  445.                 text: '&#8212; ' + eml.l10n.in + ' ' + self.options.pluralName + ' &#8212;',
  446.                 props: {
  447.                     uncategorized : null,
  448.                     orderby       : eml.l10n.media_orderby,
  449.                     order         : eml.l10n.media_order
  450.                 },
  451.                 priority: 2
  452.             };
  453.  
  454.             filters['in']['props'][self.options.taxonomy] = 'in';
  455.  
  456.             filters.not_in = {
  457.                 text: '&#8212; ' + eml.l10n.not_in + ' ' + self.options.singularName + ' &#8212;',
  458.                 props: {
  459.                     uncategorized : null,
  460.                     orderby       : eml.l10n.media_orderby,
  461.                     order         : eml.l10n.media_order
  462.                 },
  463.                 priority: 3
  464.             };
  465.  
  466.             filters['not_in']['props'][self.options.taxonomy] = 'not_in';
  467.  
  468.             this.filters = filters;
  469.         }
  470.     });
  471.  
  472.  
  473.  
  474.     /**
  475.      * wp.media.view.AttachmentFilters.Authors
  476.      *
  477.      */
  478.     media.view.AttachmentFilters.Authors = media.view.AttachmentFilters.extend({
  479.  
  480.         createFilters: function() {
  481.  
  482.             var filters = {},
  483.                 self = this;
  484.  
  485.  
  486.             _.each( self.options.users || {}, function( user, key ) {
  487.  
  488.                 var user_id = user.user_id,
  489.                     user_name = user.user_name;
  490.  
  491.                 filters[ user_id ] = {
  492.                     text: user_name,
  493.                     props: {
  494.                         author        : user_id,
  495.                         orderby       : eml.l10n.media_orderby,
  496.                         order         : eml.l10n.media_order
  497.                     },
  498.                     priority: key+2
  499.                 };
  500.             });
  501.  
  502.             filters.all = {
  503.                 text: eml.l10n.in + ' ' + eml.l10n.authors,
  504.                 props: {
  505.                     author        : null,
  506.                     orderby       : eml.l10n.media_orderby,
  507.                     order         : eml.l10n.media_order
  508.                 },
  509.                 priority: 1
  510.             };
  511.  
  512.             this.filters = filters;
  513.         }
  514.     });
  515.  
  516.  
  517.  
  518.     /**
  519.      * wp.media.view.Button.resetFilters
  520.      *
  521.      */
  522.     media.view.Button.resetFilters = media.view.Button.extend({
  523.  
  524.         id: 'reset-all-filters',
  525.  
  526.         initialize: function() {
  527.  
  528.             media.view.Button.prototype.initialize.apply( this, arguments );
  529.             this.controller.on( 'select:activate select:deactivate', this.toogleResetFilters, this );
  530.         },
  531.  
  532.         click: function( event ) {
  533.  
  534.             if ( '#' === this.attributes.href ) {
  535.                 event.preventDefault();
  536.             }
  537.  
  538.             $('.attachment-filters:has(option[value!="all"]:selected)').each( function( index ) {
  539.                 $(this).val( 'all' ).change();
  540.             });
  541.         },
  542.  
  543.         toogleResetFilters: function() {
  544.             this.$el.toggleClass( 'hidden' );
  545.         }
  546.     });
  547.  
  548.  
  549.  
  550.     /**
  551.      * wp.media.view.emlAttachmentDetailsEditMessage
  552.      *
  553.      */
  554.     media.view.emlAttachmentDetailsEditMessage = media.View.extend({
  555.  
  556.         tagName:    'div',
  557.         id:         'eml-save-changes-message',
  558.  
  559.         initialize: function() {
  560.             this.text = this.options.text;
  561.             this.class = this.options.class;
  562.         },
  563.  
  564.         render: function() {
  565.             this.$el.addClass( this.class );
  566.             this.$el.html( '<p><strong>'+this.text+'</strong></p>' );
  567.  
  568.             return this;
  569.         }
  570.     });
  571.  
  572.  
  573.  
  574.     /**
  575.      * wp.media.view.Attachment.Details
  576.      *
  577.      */
  578.  
  579.     _.extend( media.view.Attachment.Details.prototype, {
  580.  
  581.         deleteAttachment: function( event ) {
  582.             event.preventDefault();
  583.  
  584.             if ( window.confirm( l10n.warnDelete ) ) {
  585.                 this.model.destroy();
  586.                 // Keep focus inside media modal
  587.                 // after image is deleted
  588.                 if ( this.controller.modal ) {
  589.                     this.controller.modal.focusManager.focus();
  590.                 }
  591.             }
  592.         },
  593.     });
  594.  
  595.  
  596.  
  597.     /**
  598.      * wp.media.view.AttachmentsBrowser
  599.      *
  600.      */
  601.     original.AttachmentsBrowser = {
  602.  
  603.         initialize: media.view.AttachmentsBrowser.prototype.initialize,
  604.         createToolbar: media.view.AttachmentsBrowser.prototype.createToolbar,
  605.         createSidebar: media.view.AttachmentsBrowser.prototype.createSidebar,
  606.         createSingle: media.view.AttachmentsBrowser.prototype.createSingle,
  607.         disposeSingle: media.view.AttachmentsBrowser.prototype.disposeSingle
  608.     };
  609.  
  610.     _.extend( media.view.AttachmentsBrowser.prototype, {
  611.  
  612.         initialize: function() {
  613.  
  614.             original.AttachmentsBrowser.initialize.apply( this, arguments );
  615.  
  616.             this.on( 'ready', this.fixLayout, this );
  617.             this.$window = $( window );
  618.             this.$window.on( 'resize', _.debounce( _.bind( this.fixLayout, this ), 15 ) );
  619.  
  620.             if ( $('.notice-dismiss').length ) {
  621.                 $( document ).on( 'click', '.notice-dismiss', _.debounce( _.bind( this.fixLayout, this), 250 ) );
  622.             }
  623.         },
  624.  
  625.         fixLayout: function() {
  626.  
  627.             var $browser = this.$el,
  628.                 $attachments = $browser.find('.attachments'),
  629.                 $uploader = $browser.find('.uploader-inline'),
  630.                 $toolbar = $browser.find('.media-toolbar'),
  631.                 $messages = $('.eml-media-css .updated:visible, .eml-media-css .error:visible, .eml-media-css .notice:visible, .eml-media-css .notice-error:visible, .eml-media-css .notice-warning:visible, .eml-media-css .notice-success:visible, .eml-media-css .notice-info:visible'),
  632.                 $update_nag = $('.eml-media-css .update-nag');
  633.  
  634.  
  635.             if ( $update_nag.length ) {
  636.                 $update_nag.css( 'margin-left', 15 + 'px' );
  637.                 $browser.closest('.wrap').css( 'top', $update_nag.outerHeight() + 25 + 'px' );
  638.             }
  639.  
  640.  
  641.             if ( ! this.controller.isModeActive( 'select' ) &&
  642.                  ! this.controller.isModeActive( 'eml-grid' ) ) {
  643.                 return;
  644.             }
  645.  
  646.             if ( this.controller.isModeActive( 'select' ) ) {
  647.  
  648.                 $attachments.css( 'top', $toolbar.height() + 10 + 'px' );
  649.                 $uploader.css( 'top', $toolbar.height() + 10 + 'px' );
  650.                 $browser.find('.eml-loader').css( 'top', $toolbar.height() + 10 + 'px' );
  651.  
  652.                 // TODO: find a better place for it, something like fixLayoutOnce
  653.                 $toolbar.find('.media-toolbar-secondary').prepend( $toolbar.find('.instructions') );
  654.             }
  655.  
  656.             if ( this.controller.isModeActive( 'eml-grid' ) )
  657.             {
  658.                 var messagesOuterHeight = 0;
  659.  
  660.  
  661.                 if ( ! _.isUndefined( $messages ) )
  662.                 {
  663.                     $messages.each( function() {
  664.                         messagesOuterHeight += $(this).outerHeight( true );
  665.                     });
  666.  
  667.                     messagesOuterHeight = messagesOuterHeight ? messagesOuterHeight - 15 : 0;
  668.                 }
  669.  
  670.                 $browser.css( 'top', $toolbar.outerHeight() + messagesOuterHeight + 15 + 'px' );
  671.                 $toolbar.css( 'top', - $toolbar.outerHeight() - 25 + 'px' );
  672.             }
  673.         },
  674.  
  675.         createToolbar: function() {
  676.  
  677.             var LibraryViewSwitcher, Filters, toolbarOptions,
  678.                 self = this,
  679.                 i = 1,
  680.                 isResetButton = false;
  681.  
  682.  
  683.             toolbarOptions = {
  684.                 controller: this.controller
  685.             };
  686.  
  687.             if ( this.controller.isModeActive( 'grid' ) ||
  688.                 this.controller.isModeActive( 'eml-grid' ) ) {
  689.  
  690.                 toolbarOptions.className = 'media-toolbar wp-filter';
  691.             }
  692.  
  693.             /**
  694.             * @member {wp.media.view.Toolbar}
  695.             */
  696.             this.toolbar = new media.view.Toolbar( toolbarOptions );
  697.  
  698.             this.views.add( this.toolbar );
  699.  
  700.             this.toolbar.set( 'spinner', new media.view.Spinner({
  701.                 priority: -40
  702.             }) );
  703.  
  704.  
  705.             if ( this.controller.isModeActive( 'grid' ) ||
  706.                 this.controller.isModeActive( 'eml-grid' ) ) {
  707.  
  708.                 LibraryViewSwitcher = media.View.extend({
  709.                     className: 'view-switch media-grid-view-switch',
  710.                     template: media.template( 'media-library-view-switcher')
  711.                 });
  712.  
  713.                 this.toolbar.set( 'libraryViewSwitcher', new LibraryViewSwitcher({
  714.                     controller: this.controller,
  715.                     priority: -90
  716.                 }).render() );
  717.             }
  718.  
  719.  
  720.             if ( -1 !== $.inArray( this.options.filters, [ 'uploaded', 'all' ] ) ||
  721.                 ( parseInt( eml.l10n.force_filters ) &&
  722.                 ! this.controller.isModeActive( 'eml-bulk-edit' ) &&
  723.                 'gallery-edit' !== this.controller._state &&
  724.                 'playlist-edit' !== this.controller._state &&
  725.                 'video-playlist-edit' !== this.controller._state ) ||
  726.                 'customize' === eml.l10n.current_screen ||
  727.                 'widgets' === eml.l10n.current_screen ) {
  728.  
  729.  
  730.                 if ( -1 !== $.inArray( 'types', eml.l10n.filters_to_show ) ) {
  731.  
  732.                     this.toolbar.set( 'filtersLabel', new media.view.Label({
  733.                         value: l10n.filterByType,
  734.                         attributes: {
  735.                             'for':  'media-attachment-filters'
  736.                         },
  737.                         priority:   -80
  738.                     }).render() );
  739.  
  740.                     if ( 'uploaded' === this.options.filters ) {
  741.                         this.toolbar.set( 'filters', new media.view.AttachmentFilters.Uploaded({
  742.                             controller: this.controller,
  743.                             model:      this.collection.props,
  744.                             priority:   -80
  745.                         }).render() );
  746.                     } else {
  747.                         Filters = new media.view.AttachmentFilters.All({
  748.                             controller: this.controller,
  749.                             model:      this.collection.props,
  750.                             priority:   -80
  751.                         });
  752.  
  753.                         this.toolbar.set( 'filters', Filters.render() );
  754.                     }
  755.                 }
  756.  
  757.                 if ( eml.l10n.wp_version >= '4.0' && -1 !== $.inArray( 'dates', eml.l10n.filters_to_show ) && media.view.settings.months.length ) {
  758.  
  759.                     this.toolbar.set( 'dateFilterLabel', new media.view.Label({
  760.                         value: l10n.filterByDate,
  761.                         attributes: {
  762.                             'for': 'media-attachment-date-filters'
  763.                         },
  764.                         priority: -75
  765.                     }).render() );
  766.                     this.toolbar.set( 'dateFilter', new media.view.DateFilter({
  767.                         controller: this.controller,
  768.                         model:      this.collection.props,
  769.                         priority: -75
  770.                     }).render() );
  771.                 }
  772.  
  773.                 if ( eml.l10n.users.length > 1 && -1 !== $.inArray( 'authors', eml.l10n.filters_to_show ) ) {
  774.  
  775.                     this.toolbar.set( 'authorFilterLabel', new media.view.Label({
  776.                         value: eml.l10n.filter_by + ' ' + eml.l10n.author,
  777.                         attributes: {
  778.                             'for':  'author-filter',
  779.                         },
  780.                         priority: -70 + i++
  781.                     }).render() );
  782.                     this.toolbar.set( 'author-filter', new media.view.AttachmentFilters.Authors({
  783.                         controller: this.controller,
  784.                         model: this.collection.props,
  785.                         priority: -70 + i++,
  786.                         users: eml.l10n.users,
  787.                     }).render() );
  788.                 }
  789.  
  790.                 if ( -1 !== $.inArray( 'taxonomies', eml.l10n.filters_to_show ) ) {
  791.                     $.each( eml.l10n.taxonomies, function( taxonomy, values ) {
  792.  
  793.                         if ( -1 !== _.indexOf( eml.l10n.filter_taxonomies, taxonomy ) && values.term_list.length ) {
  794.  
  795.                             self.toolbar.set( taxonomy+'FilterLabel', new media.view.Label({
  796.                                 value: eml.l10n.filter_by + values.singular_name,
  797.                                 attributes: {
  798.                                     'for':  'media-attachment-' + taxonomy + '-filters',
  799.                                 },
  800.                                 priority: -70 + i++
  801.                             }).render() );
  802.                             self.toolbar.set( taxonomy+'-filter', new media.view.AttachmentFilters.Taxonomy({
  803.                                 controller: self.controller,
  804.                                 model: self.collection.props,
  805.                                 priority: -70 + i++,
  806.                                 taxonomy: taxonomy,
  807.                                 termList: values.term_list,
  808.                                 singularName: values.singular_name,
  809.                                 pluralName: values.plural_name
  810.                             }).render() );
  811.                         }
  812.                     });
  813.                 }
  814.  
  815.                 if ( this.toolbar.$el.find('.attachment-filters').length > 1 ) {
  816.                     this.toolbar.set( 'resetFilterButton', new media.view.Button.resetFilters({
  817.                         controller: this.controller,
  818.                         text: eml.l10n.reset_filters,
  819.                         disabled: true,
  820.                         priority: -70 + i++
  821.                     }).render() );
  822.                 }
  823.  
  824.             } // endif
  825.  
  826.  
  827.             if ( this.controller.isModeActive( 'eml-grid' ) ) {
  828.  
  829.                 var toolbar = this.controller.toolbar.get();
  830.  
  831.                 if ( $('body').hasClass('eml-pro-media-css') ) {
  832.                     toolbar.set( 'emlSelectAllButton', new media.view.emlSelectAllButton({
  833.                         filters: Filters,
  834.                         disabled: true,
  835.                         text: eml.l10n.select_all,
  836.                         controller: this.controller,
  837.                         priority: -80
  838.                     }).render() );
  839.                 }
  840.  
  841.                 toolbar.set( 'emlDeselectButton', new media.view.emlDeselectButton({
  842.                     filters: Filters,
  843.                     disabled: true,
  844.                     text: l10n.cancelSelection,
  845.                     controller: this.controller,
  846.                     priority: -70
  847.                 }).render() );
  848.  
  849.                 toolbar.set( 'emlDeleteSelectedButton', new media.view.emlDeleteSelectedButton({
  850.                     filters: Filters,
  851.                     style: 'primary',
  852.                     disabled: true,
  853.                     text: mediaTrash ? l10n.trashSelected : l10n.deleteSelected,
  854.                     controller: this.controller,
  855.                     priority: -60
  856.                 }).render() );
  857.  
  858.                 if ( mediaTrash ) {
  859.                     toolbar.set( 'emlDeleteSelectedPermanentlyButton', new media.view.emlDeleteSelectedPermanentlyButton({
  860.                         filters: Filters,
  861.                         style: 'primary',
  862.                         disabled: true,
  863.                         text: l10n.deleteSelected,
  864.                         controller: this.controller,
  865.                         priority: -50
  866.                     }).render() );
  867.                 }
  868.             }
  869.  
  870.  
  871.             // in case it is not eml-grid but the original grid somewhere
  872.             if ( this.controller.isModeActive( 'grid' ) ) {
  873.  
  874.                 // BulkSelection is a <div> with subviews, including screen reader text
  875.                 this.toolbar.set( 'selectModeToggleButton', new media.view.SelectModeToggleButton({
  876.                     text: l10n.bulkSelect,
  877.                     controller: this.controller,
  878.                     priority: -70
  879.                 }).render() );
  880.  
  881.                 this.toolbar.set( 'deleteSelectedButton', new media.view.DeleteSelectedButton({
  882.                     filters: Filters,
  883.                     style: 'primary',
  884.                     disabled: true,
  885.                     text: mediaTrash ? l10n.trashSelected : l10n.deleteSelected,
  886.                     controller: this.controller,
  887.                     priority: -60,
  888.                     click: function() {
  889.                         var changed = [], removed = [],
  890.                             selection = this.controller.state().get( 'selection' ),
  891.                             library = this.controller.state().get( 'library' );
  892.  
  893.                         if ( ! selection.length ) {
  894.                             return;
  895.                         }
  896.  
  897.                         if ( ! mediaTrash && ! window.confirm( l10n.warnBulkDelete ) ) {
  898.                             return;
  899.                         }
  900.  
  901.                         if ( mediaTrash &&
  902.                             'trash' !== selection.at( 0 ).get( 'status' ) &&
  903.                             ! window.confirm( l10n.warnBulkTrash ) ) {
  904.  
  905.                             return;
  906.                         }
  907.  
  908.                         selection.each( function( model ) {
  909.                             if ( ! model.get( 'nonces' )['delete'] ) {
  910.                                 removed.push( model );
  911.                                 return;
  912.                             }
  913.  
  914.                             if ( mediaTrash && 'trash' === model.get( 'status' ) ) {
  915.                                 model.set( 'status', 'inherit' );
  916.                                 changed.push( model.save() );
  917.                                 removed.push( model );
  918.                             } else if ( mediaTrash ) {
  919.                                 model.set( 'status', 'trash' );
  920.                                 changed.push( model.save() );
  921.                                 removed.push( model );
  922.                             } else {
  923.                                 model.destroy({wait: true});
  924.                             }
  925.                         } );
  926.  
  927.                         if ( changed.length ) {
  928.                             selection.remove( removed );
  929.  
  930.                             $.when.apply( null, changed ).then( _.bind( function() {
  931.                                 library._requery( true );
  932.                                 this.controller.trigger( 'selection:action:done' );
  933.                             }, this ) );
  934.                         } else {
  935.                             this.controller.trigger( 'selection:action:done' );
  936.                         }
  937.                     }
  938.                 }).render() );
  939.  
  940.                 if ( mediaTrash ) {
  941.                     this.toolbar.set( 'deleteSelectedPermanentlyButton', new wp.media.view.DeleteSelectedPermanentlyButton({
  942.                         filters: Filters,
  943.                         style: 'primary',
  944.                         disabled: true,
  945.                         text: l10n.deleteSelected,
  946.                         controller: this.controller,
  947.                         priority: -55,
  948.                         click: function() {
  949.                             var removed = [], selection = this.controller.state().get( 'selection' );
  950.  
  951.                             if ( ! selection.length || ! window.confirm( l10n.warnBulkDelete ) ) {
  952.                                 return;
  953.                             }
  954.  
  955.                             selection.each( function( model ) {
  956.                                 if ( ! model.get( 'nonces' )['delete'] ) {
  957.                                     removed.push( model );
  958.                                     return;
  959.                                 }
  960.  
  961.                                 model.destroy({wait: true});
  962.                             } );
  963.  
  964.                             this.controller.trigger( 'selection:action:done' );
  965.                         }
  966.                     }).render() );
  967.                 }
  968.             }
  969.  
  970.             if ( this.options.search ) {
  971.  
  972.                 this.toolbar.set( 'searchLabel', new media.view.Label({
  973.                     value: l10n.searchMediaLabel,
  974.                     attributes: {
  975.                         'for': 'media-search-input'
  976.                     },
  977.                     priority:   -30
  978.                 }).render() );
  979.                 this.toolbar.set( 'search', new media.view.Search({
  980.                     controller: this.controller,
  981.                     model:      this.collection.props,
  982.                     priority:   -30
  983.                 }).render() );
  984.             }
  985.  
  986.             if ( this.options.dragInfo ) {
  987.                 this.toolbar.set( 'dragInfo', new media.View({
  988.                     el: $( '<div class="instructions">' + l10n.dragInfo + '</div>' )[0],
  989.                     priority: -40
  990.                 }) );
  991.             }
  992.  
  993.             if ( 'edit-attachment' !== this.controller._state ) {
  994.  
  995.                 var toolbar = this.controller.toolbar.get();
  996.  
  997.                 toolbar.set( 'emlAttachmentSuccess', new media.view.emlAttachmentDetailsEditMessage({
  998.                     text: eml.l10n.saveButton_success,
  999.                     class: 'updated',
  1000.                     controller: this.controller,
  1001.                     priority:   200
  1002.                 }) );
  1003.  
  1004.                 toolbar.set( 'emlAttachmentError', new media.view.emlAttachmentDetailsEditMessage({
  1005.                     text: eml.l10n.saveButton_failure,
  1006.                     class: 'error',
  1007.                     controller: this.controller,
  1008.                     priority:   220
  1009.                 }) );
  1010.             }
  1011.         },
  1012.  
  1013.         createSidebar: function() {
  1014.             original.AttachmentsBrowser.createSidebar.apply( this, arguments );
  1015.  
  1016.             if ( this.controller.isModeActive( 'eml-grid' ) ) {
  1017.                 this.toggleSidebar();
  1018.             }
  1019.         },
  1020.  
  1021.         toggleSidebar: function() {
  1022.  
  1023.             var selection = this.controller.state().get( 'selection' );
  1024.  
  1025.             if ( selection.length ) {
  1026.                 this.sidebar.$el.removeClass( 'hidden' );
  1027.                 this.$el.children('.attachments').css( 'right', '300px' );
  1028.                 this.$el.children('.uploader-inline').css( 'right', '310px' );
  1029.             }
  1030.             else {
  1031.                 this.sidebar.$el.addClass( 'hidden' );
  1032.                 this.$el.children('.attachments').css( 'right', 0 );
  1033.                 this.$el.children('.uploader-inline').css( 'right', '10px' );
  1034.             }
  1035.         },
  1036.  
  1037.         createSingle: function() {
  1038.  
  1039.             original.AttachmentsBrowser.createSingle.apply( this, arguments );
  1040.  
  1041.             if ( this.controller.isModeActive( 'eml-grid' ) ) {
  1042.  
  1043.                 var sidebar = this.sidebar,
  1044.                     single = this.options.selection.single();
  1045.  
  1046.                 if ( 'trash' !== this.options.selection.at( 0 ).get( 'status' ) ) {
  1047.                     sidebar.set( 'details', new wp.media.view.emlGridAttachmentDetails({
  1048.                         controller: this.controller,
  1049.                         model:      single,
  1050.                         priority:   80
  1051.                     }) );
  1052.                 }
  1053.  
  1054.                 this.toggleSidebar();
  1055.             }
  1056.         },
  1057.  
  1058.         disposeSingle: function() {
  1059.  
  1060.             original.AttachmentsBrowser.disposeSingle.apply( this, arguments );
  1061.  
  1062.             if ( this.controller.isModeActive( 'eml-grid' ) ) {
  1063.                 this.toggleSidebar();
  1064.             }
  1065.         },
  1066.  
  1067.         updateContent: function() {
  1068.  
  1069.             var view = this,
  1070.                 noItemsView;
  1071.  
  1072.             if ( this.controller.isModeActive( 'grid' ) ||
  1073.                  this.controller.isModeActive( 'eml-grid' ) ) {
  1074.                 noItemsView = view.attachmentsNoResults;
  1075.             } else {
  1076.                 noItemsView = view.uploader;
  1077.             }
  1078.  
  1079.             if ( ! this.collection.length ) {
  1080.  
  1081.                 this.toolbar.get( 'spinner' ).show();
  1082.  
  1083.                 this.dfd = this.collection.more().done( function() {
  1084.  
  1085.                     if ( ! view.collection.length ) {
  1086.                         noItemsView.$el.removeClass( 'hidden' );
  1087.                     } else {
  1088.                         noItemsView.$el.addClass( 'hidden' );
  1089.                     }
  1090.                     view.toolbar.get( 'spinner' ).hide();
  1091.                 } );
  1092.  
  1093.             } else {
  1094.  
  1095.                 noItemsView.$el.addClass( 'hidden' );
  1096.                 view.toolbar.get( 'spinner' ).hide();
  1097.             }
  1098.         },
  1099.  
  1100.         createUploader: function() {
  1101.  
  1102.             this.uploader = new media.view.UploaderInline({
  1103.                 controller: this.controller,
  1104.                 status:     false,
  1105.                 message:    this.controller.isModeActive( 'grid' ) || this.controller.isModeActive( 'eml-grid' ) ? '' : l10n.noItemsFound,
  1106.                 canClose:   this.controller.isModeActive( 'grid' ) || this.controller.isModeActive( 'eml-grid' )
  1107.             });
  1108.  
  1109.             this.uploader.$el.addClass( 'hidden' );
  1110.             this.views.add( this.uploader );
  1111.         },
  1112.  
  1113.         createAttachments: function() {
  1114.             this.attachments = new media.view.Attachments({
  1115.                 controller:           this.controller,
  1116.                 collection:           this.collection,
  1117.                 selection:            this.options.selection,
  1118.                 model:                this.model,
  1119.                 sortable:             this.options.sortable,
  1120.                 scrollElement:        this.options.scrollElement,
  1121.                 idealColumnWidth:     this.options.idealColumnWidth,
  1122.  
  1123.                 // The single `Attachment` view to be used in the `Attachments` view.
  1124.                 AttachmentView: this.options.AttachmentView
  1125.             });
  1126.  
  1127.             // Add keydown listener to the instance of the Attachments view
  1128.             this.attachments.listenTo( this.controller, 'attachment:keydown:arrow',     this.attachments.arrowEvent );
  1129.             this.attachments.listenTo( this.controller, 'attachment:details:shift-tab', this.attachments.restoreFocus );
  1130.  
  1131.             this.views.add( this.attachments );
  1132.  
  1133.  
  1134.             if ( this.controller.isModeActive( 'grid' ) ||
  1135.                 this.controller.isModeActive( 'eml-grid' ) ) {
  1136.  
  1137.                 this.attachmentsNoResults = new media.View({
  1138.                     controller: this.controller,
  1139.                     tagName: 'p'
  1140.                 });
  1141.  
  1142.                 this.attachmentsNoResults.$el.addClass( 'hidden no-media' );
  1143.                 this.attachmentsNoResults.$el.html( l10n.noItemsFound );
  1144.  
  1145.                 this.views.add( this.attachmentsNoResults );
  1146.             }
  1147.         }
  1148.     });
  1149.  
  1150.  
  1151.  
  1152.     /**
  1153.      * wp.media.view.MediaFrame.Post
  1154.      *
  1155.      */
  1156.     original.MediaFrame = {
  1157.  
  1158.         Post: {
  1159.             activate: media.view.MediaFrame.Post.prototype.activate
  1160.         }
  1161.     };
  1162.  
  1163.     _.extend( media.view.MediaFrame.Post.prototype, {
  1164.  
  1165.         activate: function() {
  1166.  
  1167.             var content = this.content.get();
  1168.  
  1169.             original.MediaFrame.Post.activate.apply( this, arguments );
  1170.  
  1171.             this.on( 'open', content.fixLayout, content );
  1172.             if ( typeof acf !== 'undefined' && $('.acf-expand-details').length ) {
  1173.                 $( document ).on( 'click', '.acf-expand-details', _.debounce( _.bind( content.fixLayout, content ), 250 ) );
  1174.             }
  1175.         }
  1176.     });
  1177.  
  1178.  
  1179.  
  1180.     $( document ).ready( function() {
  1181.  
  1182.         // TODO: find a better place for this
  1183.         $( document ).on( 'mousedown', '.media-frame .attachments-browser .attachments li', function ( event ) {
  1184.  
  1185.             if ( event.ctrlKey || event.shiftKey ) {
  1186.                 event.preventDefault();
  1187.             }
  1188.         });
  1189.     });
  1190.  
  1191.  
  1192.  
  1193.     // TODO: move to the PHP side
  1194.     $('body').addClass('eml-media-css');
  1195.  
  1196. })( jQuery, _ );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement