<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[27268] trunk/src/wp-admin: Improve pushState, replaceState and routing for Themes page</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/27268">27268</a></dd>
<dt>Author</dt> <dd>markjaquith</dd>
<dt>Date</dt> <dd>2014-02-25 19:21:58 +0000 (Tue, 25 Feb 2014)</dd>
</dl>
<h3>Log Message</h3>
<pre>Improve pushState, replaceState and routing for Themes page
* Uses `pushState()` generally
* Uses `replaceState()` when updating a search query (so no history cruft from typing)
fixes <a href="http://core.trac.wordpress.org/ticket/27198">#27198</a>. props gcorne, adamsilverstein.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminjsthemejs">trunk/src/wp-admin/js/theme.js</a></li>
<li><a href="#trunksrcwpadminthemesphp">trunk/src/wp-admin/themes.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminjsthemejs"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-admin/js/theme.js (27267 => 27268)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-admin/js/theme.js 2014-02-25 17:57:04 UTC (rev 27267)
+++ trunk/src/wp-admin/js/theme.js 2014-02-25 19:21:58 UTC (rev 27268)
</span><span class="lines">@@ -66,7 +66,7 @@
</span><span class="cx"> return;
</span><span class="cx"> }
</span><span class="cx">
</span><del>- view = new themes.view.Search({ collection: self.collection });
</del><ins>+ view = new themes.view.Search({ collection: self.collection, parent: this });
</ins><span class="cx">
</span><span class="cx"> // Render and append after screen title
</span><span class="cx"> view.render();
</span><span class="lines">@@ -350,7 +350,7 @@
</span><span class="cx"> scroll = document.body.scrollTop;
</span><span class="cx">
</span><span class="cx"> // Clean the url structure
</span><del>- themes.router.navigate( themes.router.baseUrl( '' ), { replace: true } );
</del><ins>+ themes.router.navigate( themes.router.baseUrl( '' ) );
</ins><span class="cx">
</span><span class="cx"> // Restore scroll position
</span><span class="cx"> document.body.scrollTop = scroll;
</span><span class="lines">@@ -451,6 +451,12 @@
</span><span class="cx"> self.renderThemes( self.parent.page );
</span><span class="cx"> });
</span><span class="cx">
</span><ins>+ this.listenTo( this.parent, 'theme:close', function() {
+ if ( self.overlay ) {
+ self.overlay.closeOverlay();
+ }
+ } );
+
</ins><span class="cx"> // Bind keyboard events.
</span><span class="cx"> $('body').on( 'keyup', function( event ) {
</span><span class="cx"> if ( ! self.overlay ) {
</span><span class="lines">@@ -573,7 +579,7 @@
</span><span class="cx"> this.model = self.collection.get( id );
</span><span class="cx">
</span><span class="cx"> // Trigger a route update for the current model
</span><del>- themes.router.navigate( themes.router.baseUrl( '?theme=' + this.model.id ), { replace: true } );
</del><ins>+ themes.router.navigate( themes.router.baseUrl( '?theme=' + this.model.id ) );
</ins><span class="cx">
</span><span class="cx"> // Sets this.view to 'detail'
</span><span class="cx"> this.setView( 'detail' );
</span><span class="lines">@@ -659,6 +665,7 @@
</span><span class="cx"> tagName: 'input',
</span><span class="cx"> className: 'theme-search',
</span><span class="cx"> id: 'theme-search-input',
</span><ins>+ searching: false,
</ins><span class="cx">
</span><span class="cx"> attributes: {
</span><span class="cx"> placeholder: l10n.searchPlaceholder,
</span><span class="lines">@@ -669,11 +676,24 @@
</span><span class="cx"> 'input': 'search',
</span><span class="cx"> 'keyup': 'search',
</span><span class="cx"> 'change': 'search',
</span><del>- 'search': 'search'
</del><ins>+ 'search': 'search',
+ 'blur': 'pushState'
</ins><span class="cx"> },
</span><span class="cx">
</span><ins>+ initialize: function( options ) {
+
+ this.parent = options.parent;
+
+ this.listenTo( this.parent, 'theme:close', function() {
+ this.searching = false;
+ } );
+
+ },
+
</ins><span class="cx"> // Runs a search on the theme collection.
</span><span class="cx"> search: function( event ) {
</span><ins>+ var options = {};
+
</ins><span class="cx"> // Clear on escape.
</span><span class="cx"> if ( event.type === 'keyup' && event.which === 27 ) {
</span><span class="cx"> event.target.value = '';
</span><span class="lines">@@ -681,12 +701,31 @@
</span><span class="cx">
</span><span class="cx"> this.collection.doSearch( event.target.value );
</span><span class="cx">
</span><ins>+ // if search is initiated and key is not return
+ if ( this.searching && event.which !== 13 ) {
+ options.replace = true;
+ } else {
+ this.searching = true;
+ }
+
</ins><span class="cx"> // Update the URL hash
</span><span class="cx"> if ( event.target.value ) {
</span><del>- themes.router.navigate( themes.router.baseUrl( '?search=' + event.target.value ), { replace: true } );
</del><ins>+ themes.router.navigate( themes.router.baseUrl( '?search=' + event.target.value ), options );
</ins><span class="cx"> } else {
</span><del>- themes.router.navigate( themes.router.baseUrl( '' ), { replace: true } );
</del><ins>+ themes.router.navigate( themes.router.baseUrl( '' ) );
</ins><span class="cx"> }
</span><ins>+ },
+
+ pushState: function( event ) {
+ var url = themes.router.baseUrl( '' );
+
+ if ( event.target.value ) {
+ url = themes.router.baseUrl( '?search=' + event.target.value );
+ }
+
+ this.searching = false;
+ themes.router.navigate( url );
+
</ins><span class="cx"> }
</span><span class="cx"> });
</span><span class="cx">
</span><span class="lines">@@ -694,14 +733,26 @@
</span><span class="cx"> // Listens to [theme] and [search] params
</span><span class="cx"> themes.routes = Backbone.Router.extend({
</span><span class="cx">
</span><del>- initialize: function() {
- this.routes = _.object([
- ]);
</del><ins>+ routes: {
+ 'themes.php?theme=:slug': 'theme',
+ 'themes.php?search=:query': 'search',
+ 'themes.php?s=:query': 'search',
+ 'themes.php': 'themes',
+ '': 'themes'
</ins><span class="cx"> },
</span><span class="cx">
</span><span class="cx"> baseUrl: function( url ) {
</span><del>- return themes.data.settings.root + url;
</del><ins>+ return 'themes.php' + url;
+ },
+
+ search: function( query ) {
+ $( '.theme-search' ).val( query );
+ },
+
+ themes: function() {
+ $( '.theme-search' ).val( '' );
</ins><span class="cx"> }
</span><ins>+
</ins><span class="cx"> });
</span><span class="cx">
</span><span class="cx"> // Execute and setup the application
</span><span class="lines">@@ -720,32 +771,39 @@
</span><span class="cx"> },
</span><span class="cx">
</span><span class="cx"> render: function() {
</span><ins>+
</ins><span class="cx"> // Render results
</span><span class="cx"> this.view.render();
</span><span class="cx"> this.routes();
</span><span class="cx">
</span><del>- // Set the initial theme
- if ( 'undefined' !== typeof themes.data.settings.theme && '' !== themes.data.settings.theme ){
- this.view.view.theme.trigger( 'theme:expand', this.view.collection.findWhere( { id: themes.data.settings.theme } ) );
- }
-
- // Set the initial search
- if ( 'undefined' !== typeof themes.data.settings.search && '' !== themes.data.settings.search ){
- $( '.theme-search' ).val( themes.data.settings.search );
- this.themes.doSearch( themes.data.settings.search );
- }
-
- // Start the router if browser supports History API
- if ( window.history && window.history.pushState ) {
- // Calls the routes functionality
- Backbone.history.start({ pushState: true, silent: true });
- }
</del><ins>+ Backbone.history.start({
+ root: themes.data.settings.adminUrl,
+ pushState: true,
+ hashChange: false
+ });
</ins><span class="cx"> },
</span><span class="cx">
</span><span class="cx"> routes: function() {
</span><ins>+ var self = this;
</ins><span class="cx"> // Bind to our global thx object
</span><span class="cx"> // so that the object is available to sub-views
</span><span class="cx"> themes.router = new themes.routes();
</span><ins>+
+ // Handles theme details route event
+ themes.router.on( 'route:theme', function( slug ) {
+ self.view.view.expand( slug );
+ });
+
+ themes.router.on( 'route:themes', function() {
+ self.themes.doSearch( '' );
+ self.view.trigger( 'theme:close' );
+ });
+
+ // Handles search route event
+ themes.router.on( 'route:search', function( query ) {
+ self.view.trigger( 'theme:close' );
+ self.themes.doSearch( query );
+ });
</ins><span class="cx"> }
</span><span class="cx"> };
</span><span class="cx">
</span></span></pre></div>
<a id="trunksrcwpadminthemesphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-admin/themes.php (27267 => 27268)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-admin/themes.php 2014-02-25 17:57:04 UTC (rev 27267)
+++ trunk/src/wp-admin/themes.php 2014-02-25 19:21:58 UTC (rev 27268)
</span><span class="lines">@@ -99,10 +99,7 @@
</span><span class="cx"> 'canInstall' => ( ! is_multisite() && current_user_can( 'install_themes' ) ),
</span><span class="cx"> 'installURI' => ( ! is_multisite() && current_user_can( 'install_themes' ) ) ? admin_url( 'theme-install.php' ) : null,
</span><span class="cx"> 'confirmDelete' => __( "Are you sure you want to delete this theme?\n\nClick 'Cancel' to go back, 'OK' to confirm the delete." ),
</span><del>- 'root' => parse_url( admin_url( 'themes.php' ), PHP_URL_PATH ),
- 'theme' => esc_html( $theme ),
- 'search' => esc_html( $search ),
-
</del><ins>+ 'adminUrl' => parse_url( admin_url(), PHP_URL_PATH ),
</ins><span class="cx"> ),
</span><span class="cx"> 'l10n' => array(
</span><span class="cx"> 'addNew' => __( 'Add New Theme' ),
</span></span></pre>
</div>
</div>
</body>
</html>