From a8dcd7acedbfe6d5f09b9924246c70dc066625e0 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Thu, 26 Feb 2015 23:01:25 +0000 Subject: [PATCH] Persist search terms across grid/list modes: * In `grid` mode, when the page loads and `s` is in the URL, all attachments are loaded and then the search value is set, which will filter the attachments. If the page loads with the attachments already filtered, the library will have to be requery'd to get the full set, which will require weirder code. * When a user searches, the mode-switcher link for `list` view is updated dynamically to represent the current `location.href` in the proper `mode=` and `s=` context. Fixes #30583. Built from https://develop.svn.wordpress.org/trunk@31562 git-svn-id: http://core.svn.wordpress.org/trunk@31543 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/upload.php | 7 ++-- wp-includes/js/media/grid.js | 39 ++++++++++++++++++---- wp-includes/js/media/grid.min.js | 2 +- wp-includes/js/media/views/frame/manage.js | 36 ++++++++++++++++---- wp-includes/version.php | 2 +- 5 files changed, 70 insertions(+), 16 deletions(-) diff --git a/wp-admin/upload.php b/wp-admin/upload.php index 5346c2aef1..5408395dc7 100644 --- a/wp-admin/upload.php +++ b/wp-admin/upload.php @@ -25,7 +25,10 @@ if ( 'grid' === $mode ) { wp_enqueue_script( 'media-grid' ); wp_enqueue_script( 'media' ); - $vars = wp_edit_attachments_query_vars(); + $q = $_GET; + // let JS handle this + unset( $q['s'] ); + $vars = wp_edit_attachments_query_vars( $q ); $ignore = array( 'mode', 'post_type', 'post_status', 'posts_per_page' ); foreach ( $vars as $key => $value ) { if ( ! $value || in_array( $key, $ignore ) ) { @@ -67,7 +70,7 @@ if ( 'grid' === $mode ) { require_once( ABSPATH . 'wp-admin/admin-header.php' ); ?> -
+

-1 ) { + href = href.replace( /mode=[^&]+/g, 'mode=list' ); + } else { + href += href.indexOf( '?' ) > -1 ? '&mode=list' : '?mode=list'; } - self.gridRouter.navigate( self.gridRouter.baseUrl( url ) ); - }, 1000 ) ); + href = href.replace( 'search=', 's=' ); + listMode.prop( 'href', href ); + } ); }, /** diff --git a/wp-includes/js/media/grid.min.js b/wp-includes/js/media/grid.min.js index 7f1d247236..21eccdbfec 100644 --- a/wp-includes/js/media/grid.min.js +++ b/wp-includes/js/media/grid.min.js @@ -1 +1 @@ -!function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);var j=new Error("Cannot find module '"+g+"'");throw j.code="MODULE_NOT_FOUND",j}var k=c[g]={exports:{}};b[g][0].call(k.exports,function(a){var c=b[g][1][a];return e(c?c:a)},k,k.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g *, .media-toolbar-primary > *"),this.controller.isModeActive("select")?(this.model.set("text",e.cancelSelection),a.not(".media-button").hide(),this.$el.show(),b.$(".delete-selected-button").removeClass("hidden")):(this.model.set("text",e.bulkSelect),this.controller.content.get().$el.removeClass("fixed"),b.$el.css("width",""),b.$(".delete-selected-button").addClass("hidden"),a.not(".spinner, .media-button").show(),this.controller.state().get("selection").reset())}}),b.exports=c},{}],8:[function(a,b){var c,d=wp.media.View,e=wp.media.view.EditImage;c=e.extend({initialize:function(a){this.editor=window.imageEdit,this.frame=a.frame,this.controller=a.controller,d.prototype.initialize.apply(this,arguments)},back:function(){this.frame.content.mode("edit-metadata")},save:function(){this.model.fetch().done(_.bind(function(){this.frame.content.mode("edit-metadata")},this))}}),b.exports=c},{}],9:[function(a,b){var c,d=wp.media.view.Frame,e=wp.media.view.MediaFrame,f=wp.media.view.Modal,g=wp.media.view.AttachmentCompat,h=wp.media.controller.EditImage,i=a("../../controllers/edit-attachment-metadata.js"),j=a("../attachment/details-two-column.js"),k=a("../edit-image-details.js"),l=jQuery;c=e.extend({className:"edit-attachment-frame",template:wp.template("edit-attachment-frame"),regions:["title","content"],events:{"click .left":"previousMediaItem","click .right":"nextMediaItem"},initialize:function(){d.prototype.initialize.apply(this,arguments),_.defaults(this.options,{modal:!0,state:"edit-attachment"}),this.controller=this.options.controller,this.gridRouter=this.controller.gridRouter,this.library=this.options.library,this.options.model&&(this.model=this.options.model),this.bindHandlers(),this.createStates(),this.createModal(),this.title.mode("default"),this.toggleNav()},bindHandlers:function(){this.on("title:create:default",this.createTitle,this),this.listenTo(this.model,"change:status destroy",this.close,this),this.on("content:create:edit-metadata",this.editMetadataMode,this),this.on("content:create:edit-image",this.editImageMode,this),this.on("content:render:edit-image",this.editImageModeRender,this),this.on("close",this.detach)},createModal:function(){this.options.modal&&(this.modal=new f({controller:this,title:this.options.title}),this.modal.on("open",_.bind(function(){l("body").on("keydown.media-modal",_.bind(this.keyEvent,this))},this)),this.modal.on("close",_.bind(function(){this.modal.remove(),l("body").off("keydown.media-modal"),l('li.attachment[data-id="'+this.model.get("id")+'"]').focus(),this.resetRoute()},this)),this.modal.content(this),this.modal.open())},createStates:function(){this.states.add([new i({model:this.model})])},editMetadataMode:function(a){a.view=new j({controller:this,model:this.model}),a.view.views.set(".attachment-compat",new g({controller:this,model:this.model})),this.model&&this.gridRouter.navigate(this.gridRouter.baseUrl("?item="+this.model.id))},editImageMode:function(a){var b=new h({model:this.model,frame:this});b._toolbar=function(){},b._router=function(){},b._menu=function(){},a.view=new k({model:this.model,frame:this,controller:b})},editImageModeRender:function(a){a.on("ready",a.loadEditor)},toggleNav:function(){this.$(".left").toggleClass("disabled",!this.hasPrevious()),this.$(".right").toggleClass("disabled",!this.hasNext())},rerender:function(){"edit-metadata"!==this.content.mode()?this.content.mode("edit-metadata"):this.content.render(),this.toggleNav()},previousMediaItem:function(){return this.hasPrevious()?(this.model=this.library.at(this.getCurrentIndex()-1),this.rerender(),void this.$(".left").focus()):void this.$(".left").blur()},nextMediaItem:function(){return this.hasNext()?(this.model=this.library.at(this.getCurrentIndex()+1),this.rerender(),void this.$(".right").focus()):void this.$(".right").blur()},getCurrentIndex:function(){return this.library.indexOf(this.model)},hasNext:function(){return this.getCurrentIndex()+1-1},keyEvent:function(a){("INPUT"!==a.target.nodeName&&"TEXTAREA"!==a.target.nodeName||a.target.readOnly||a.target.disabled)&&(39===a.keyCode&&this.nextMediaItem(),37===a.keyCode&&this.previousMediaItem())},resetRoute:function(){this.gridRouter.navigate(this.gridRouter.baseUrl(""))}}),b.exports=c},{"../../controllers/edit-attachment-metadata.js":1,"../attachment/details-two-column.js":4,"../edit-image-details.js":8}],10:[function(a,b){var c,d=wp.media.view.MediaFrame,e=wp.media.view.UploaderWindow,f=wp.media.view.AttachmentsBrowser,g=wp.media.controller.Library,h=a("../../routers/manage.js"),i=jQuery;c=d.extend({initialize:function(){var a=this;_.defaults(this.options,{title:"",modal:!1,selection:[],library:{},multiple:"add",state:"library",uploader:!0,mode:["grid","edit"]}),this.$body=i(document.body),this.$window=i(window),this.$adminBar=i("#wpadminbar"),this.$window.on("scroll resize",_.debounce(_.bind(this.fixPosition,this),15)),i(document).on("click",".add-new-h2",_.bind(this.addNewClickHandler,this)),this.$el.addClass("wp-core-ui"),(wp.Uploader.limitExceeded||!wp.Uploader.browser.supported)&&(this.options.uploader=!1),this.options.uploader&&(this.uploader=new e({controller:this,uploader:{dropzone:document.body,container:document.body}}).render(),this.uploader.ready(),i("body").append(this.uploader.el),this.options.uploader=!1),this.gridRouter=new h,d.prototype.initialize.apply(this,arguments),this.$el.appendTo(this.options.container),this.createStates(),this.bindRegionModeHandlers(),this.render(),i("#media-search-input").on("input",_.debounce(function(b){var c=i(b.currentTarget).val(),d="";c&&(d+="?search="+c),a.gridRouter.navigate(a.gridRouter.baseUrl(d))},1e3))},createStates:function(){var a=this.options;this.options.states||this.states.add([new g({library:wp.media.query(a.library),multiple:a.multiple,title:a.title,content:"browse",toolbar:"select",contentUserSetting:!1,filterable:"all",autoSelect:!1})])},bindRegionModeHandlers:function(){this.on("content:create:browse",this.browseContent,this),this.on("edit:attachment",this.openEditAttachmentModal,this),this.on("select:activate",this.bindKeydown,this),this.on("select:deactivate",this.unbindKeydown,this)},handleKeydown:function(a){27===a.which&&(a.preventDefault(),this.deactivateMode("select").activateMode("edit"))},bindKeydown:function(){this.$body.on("keydown.select",_.bind(this.handleKeydown,this))},unbindKeydown:function(){this.$body.off("keydown.select")},fixPosition:function(){var a,b;this.isModeActive("select")&&(a=this.$(".attachments-browser"),b=a.find(".media-toolbar"),a.offset().top+16 *, .media-toolbar-primary > *"),this.controller.isModeActive("select")?(this.model.set("text",e.cancelSelection),a.not(".media-button").hide(),this.$el.show(),b.$(".delete-selected-button").removeClass("hidden")):(this.model.set("text",e.bulkSelect),this.controller.content.get().$el.removeClass("fixed"),b.$el.css("width",""),b.$(".delete-selected-button").addClass("hidden"),a.not(".spinner, .media-button").show(),this.controller.state().get("selection").reset())}}),b.exports=c},{}],8:[function(a,b){var c,d=wp.media.View,e=wp.media.view.EditImage;c=e.extend({initialize:function(a){this.editor=window.imageEdit,this.frame=a.frame,this.controller=a.controller,d.prototype.initialize.apply(this,arguments)},back:function(){this.frame.content.mode("edit-metadata")},save:function(){this.model.fetch().done(_.bind(function(){this.frame.content.mode("edit-metadata")},this))}}),b.exports=c},{}],9:[function(a,b){var c,d=wp.media.view.Frame,e=wp.media.view.MediaFrame,f=wp.media.view.Modal,g=wp.media.view.AttachmentCompat,h=wp.media.controller.EditImage,i=a("../../controllers/edit-attachment-metadata.js"),j=a("../attachment/details-two-column.js"),k=a("../edit-image-details.js"),l=jQuery;c=e.extend({className:"edit-attachment-frame",template:wp.template("edit-attachment-frame"),regions:["title","content"],events:{"click .left":"previousMediaItem","click .right":"nextMediaItem"},initialize:function(){d.prototype.initialize.apply(this,arguments),_.defaults(this.options,{modal:!0,state:"edit-attachment"}),this.controller=this.options.controller,this.gridRouter=this.controller.gridRouter,this.library=this.options.library,this.options.model&&(this.model=this.options.model),this.bindHandlers(),this.createStates(),this.createModal(),this.title.mode("default"),this.toggleNav()},bindHandlers:function(){this.on("title:create:default",this.createTitle,this),this.listenTo(this.model,"change:status destroy",this.close,this),this.on("content:create:edit-metadata",this.editMetadataMode,this),this.on("content:create:edit-image",this.editImageMode,this),this.on("content:render:edit-image",this.editImageModeRender,this),this.on("close",this.detach)},createModal:function(){this.options.modal&&(this.modal=new f({controller:this,title:this.options.title}),this.modal.on("open",_.bind(function(){l("body").on("keydown.media-modal",_.bind(this.keyEvent,this))},this)),this.modal.on("close",_.bind(function(){this.modal.remove(),l("body").off("keydown.media-modal"),l('li.attachment[data-id="'+this.model.get("id")+'"]').focus(),this.resetRoute()},this)),this.modal.content(this),this.modal.open())},createStates:function(){this.states.add([new i({model:this.model})])},editMetadataMode:function(a){a.view=new j({controller:this,model:this.model}),a.view.views.set(".attachment-compat",new g({controller:this,model:this.model})),this.model&&this.gridRouter.navigate(this.gridRouter.baseUrl("?item="+this.model.id))},editImageMode:function(a){var b=new h({model:this.model,frame:this});b._toolbar=function(){},b._router=function(){},b._menu=function(){},a.view=new k({model:this.model,frame:this,controller:b})},editImageModeRender:function(a){a.on("ready",a.loadEditor)},toggleNav:function(){this.$(".left").toggleClass("disabled",!this.hasPrevious()),this.$(".right").toggleClass("disabled",!this.hasNext())},rerender:function(){"edit-metadata"!==this.content.mode()?this.content.mode("edit-metadata"):this.content.render(),this.toggleNav()},previousMediaItem:function(){return this.hasPrevious()?(this.model=this.library.at(this.getCurrentIndex()-1),this.rerender(),void this.$(".left").focus()):void this.$(".left").blur()},nextMediaItem:function(){return this.hasNext()?(this.model=this.library.at(this.getCurrentIndex()+1),this.rerender(),void this.$(".right").focus()):void this.$(".right").blur()},getCurrentIndex:function(){return this.library.indexOf(this.model)},hasNext:function(){return this.getCurrentIndex()+1-1},keyEvent:function(a){("INPUT"!==a.target.nodeName&&"TEXTAREA"!==a.target.nodeName||a.target.readOnly||a.target.disabled)&&(39===a.keyCode&&this.nextMediaItem(),37===a.keyCode&&this.previousMediaItem())},resetRoute:function(){this.gridRouter.navigate(this.gridRouter.baseUrl(""))}}),b.exports=c},{"../../controllers/edit-attachment-metadata.js":1,"../attachment/details-two-column.js":4,"../edit-image-details.js":8}],10:[function(a,b){var c,d=wp.media.view.MediaFrame,e=wp.media.view.UploaderWindow,f=wp.media.view.AttachmentsBrowser,g=wp.media.controller.Library,h=a("../../routers/manage.js"),i=jQuery;c=d.extend({initialize:function(){_.defaults(this.options,{title:"",modal:!1,selection:[],library:{},multiple:"add",state:"library",uploader:!0,mode:["grid","edit"]}),this.$body=i(document.body),this.$window=i(window),this.$adminBar=i("#wpadminbar"),this.$window.on("scroll resize",_.debounce(_.bind(this.fixPosition,this),15)),i(document).on("click",".add-new-h2",_.bind(this.addNewClickHandler,this)),this.$el.addClass("wp-core-ui"),(wp.Uploader.limitExceeded||!wp.Uploader.browser.supported)&&(this.options.uploader=!1),this.options.uploader&&(this.uploader=new e({controller:this,uploader:{dropzone:document.body,container:document.body}}).render(),this.uploader.ready(),i("body").append(this.uploader.el),this.options.uploader=!1),this.gridRouter=new h,d.prototype.initialize.apply(this,arguments),this.$el.appendTo(this.options.container),this.createStates(),this.bindRegionModeHandlers(),this.render(),this.bindSearchHandler()},bindSearchHandler:function(){var a=this.$("#media-search-input"),b=this.options.container.data("search"),c=this.browserView.toolbar.get("search").$el,d=this.$(".view-list"),e=_.debounce(function(a){var b=i(a.currentTarget).val(),c="";b&&(c+="?search="+b),this.gridRouter.navigate(this.gridRouter.baseUrl(c))},1e3);a.on("input",_.bind(e,this)),c.val(b).trigger("input"),this.gridRouter.on("route:search",function(){var a=window.location.href;a.indexOf("mode=")>-1?a=a.replace(/mode=[^&]+/g,"mode=list"):a+=a.indexOf("?")>-1?"&mode=list":"?mode=list",a=a.replace("search=","s="),d.prop("href",a)})},createStates:function(){var a=this.options;this.options.states||this.states.add([new g({library:wp.media.query(a.library),multiple:a.multiple,title:a.title,content:"browse",toolbar:"select",contentUserSetting:!1,filterable:"all",autoSelect:!1})])},bindRegionModeHandlers:function(){this.on("content:create:browse",this.browseContent,this),this.on("edit:attachment",this.openEditAttachmentModal,this),this.on("select:activate",this.bindKeydown,this),this.on("select:deactivate",this.unbindKeydown,this)},handleKeydown:function(a){27===a.which&&(a.preventDefault(),this.deactivateMode("select").activateMode("edit"))},bindKeydown:function(){this.$body.on("keydown.select",_.bind(this.handleKeydown,this))},unbindKeydown:function(){this.$body.off("keydown.select")},fixPosition:function(){var a,b;this.isModeActive("select")&&(a=this.$(".attachments-browser"),b=a.find(".media-toolbar"),a.offset().top+16 -1 ? '&mode=list' : '?mode=list'; } - self.gridRouter.navigate( self.gridRouter.baseUrl( url ) ); - }, 1000 ) ); + href = href.replace( 'search=', 's=' ); + listMode.prop( 'href', href ); + } ); }, /** diff --git a/wp-includes/version.php b/wp-includes/version.php index 32f4175bbb..05a4fdddba 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.2-alpha-31561'; +$wp_version = '4.2-alpha-31562'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.