<!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>[29025] trunk/src: Prompt the user before leaving the Customizer if they have unsaved changes.</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/29025">29025</a></dd>
<dt>Author</dt> <dd>helen</dd>
<dt>Date</dt> <dd>2014-07-08 17:03:48 +0000 (Tue, 08 Jul 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Prompt the user before leaving the Customizer if they have unsaved changes. props westonruter. fixes <a href="http://core.trac.wordpress.org/ticket/25439">#25439</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminjscustomizecontrolsjs">trunk/src/wp-admin/js/customize-controls.js</a></li>
<li><a href="#trunksrcwpincludesjscustomizeloaderjs">trunk/src/wp-includes/js/customize-loader.js</a></li>
<li><a href="#trunksrcwpincludesscriptloaderphp">trunk/src/wp-includes/script-loader.php</a></li>
<li><a href="#trunksrcwpincludesthemephp">trunk/src/wp-includes/theme.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminjscustomizecontrolsjs"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-admin/js/customize-controls.js (29024 => 29025)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-admin/js/customize-controls.js      2014-07-08 15:47:20 UTC (rev 29024)
+++ trunk/src/wp-admin/js/customize-controls.js 2014-07-08 17:03:48 UTC (rev 29025)
</span><span class="lines">@@ -915,7 +915,9 @@
</span><span class="cx"> 
</span><span class="cx">          var previewer, parent, topFocus,
</span><span class="cx">                  body = $( document.body ),
</span><del>-                       overlay = body.children('.wp-full-overlay');
</del><ins>+                        overlay = body.children( '.wp-full-overlay' ),
+                       backBtn = $( '.back' ),
+                       saveBtn = $( '#save' );
</ins><span class="cx"> 
</span><span class="cx">          // Prevent the form from saving when enter is pressed on an input or select element.
</span><span class="cx">          $('#customize-controls').on( 'keydown', function( e ) {
</span><span class="lines">@@ -1040,20 +1042,17 @@
</span><span class="cx">                          processing = state.create( 'processing' );
</span><span class="cx"> 
</span><span class="cx">                  state.bind( 'change', function() {
</span><del>-                               var save = $('#save'),
-                                       back = $('.back');
-
</del><span class="cx">                           if ( ! activated() ) {
</span><del>-                                       save.val( api.l10n.activate ).prop( 'disabled', false );
-                                       back.text( api.l10n.cancel );
</del><ins>+                                        saveBtn.val( api.l10n.activate ).prop( 'disabled', false );
+                                       backBtn.text( api.l10n.cancel );
</ins><span class="cx"> 
</span><span class="cx">                          } else if ( saved() ) {
</span><del>-                                       save.val( api.l10n.saved ).prop( 'disabled', true );
-                                       back.text( api.l10n.close );
</del><ins>+                                        saveBtn.val( api.l10n.saved ).prop( 'disabled', true );
+                                       backBtn.text( api.l10n.close );
</ins><span class="cx"> 
</span><span class="cx">                          } else {
</span><del>-                                       save.val( api.l10n.save ).prop( 'disabled', false );
-                                       back.text( api.l10n.cancel );
</del><ins>+                                        saveBtn.val( api.l10n.save ).prop( 'disabled', false );
+                                       backBtn.text( api.l10n.cancel );
</ins><span class="cx">                           }
</span><span class="cx">                  });
</span><span class="cx"> 
</span><span class="lines">@@ -1081,7 +1080,7 @@
</span><span class="cx">          }());
</span><span class="cx"> 
</span><span class="cx">          // Button bindings.
</span><del>-               $('#save').click( function( event ) {
</del><ins>+                saveBtn.click( function( event ) {
</ins><span class="cx">                   previewer.save();
</span><span class="cx">                  event.preventDefault();
</span><span class="cx">          }).keydown( function( event ) {
</span><span class="lines">@@ -1092,7 +1091,7 @@
</span><span class="cx">                  event.preventDefault();
</span><span class="cx">          });
</span><span class="cx"> 
</span><del>-               $('.back').keydown( function( event ) {
</del><ins>+                backBtn.keydown( function( event ) {
</ins><span class="cx">                   if ( 9 === event.which ) // tab
</span><span class="cx">                          return;
</span><span class="cx">                  if ( 13 === event.which ) // enter
</span><span class="lines">@@ -1122,16 +1121,25 @@
</span><span class="cx">          // If we receive a 'back' event, we're inside an iframe.
</span><span class="cx">          // Send any clicks to the 'Return' link to the parent page.
</span><span class="cx">          parent.bind( 'back', function() {
</span><del>-                       $('.back').on( 'click.back', function( event ) {
</del><ins>+                        backBtn.on( 'click.back', function( event ) {
</ins><span class="cx">                           event.preventDefault();
</span><span class="cx">                          parent.send( 'close' );
</span><span class="cx">                  });
</span><span class="cx">          });
</span><span class="cx"> 
</span><ins>+               // Prompt user with AYS dialog if leaving the Customizer with unsaved changes
+               $( window ).on( 'beforeunload', function () {
+                       if ( ! api.state( 'saved' )() ) {
+                               return api.l10n.saveAlert;
+                       }
+               } );
+
</ins><span class="cx">           // Pass events through to the parent.
</span><del>-               api.bind( 'saved', function() {
-                       parent.send( 'saved' );
-               });
</del><ins>+                $.each( [ 'saved', 'change' ], function ( i, event ) {
+                       api.bind( event, function() {
+                               parent.send( event );
+                       });
+               } );
</ins><span class="cx"> 
</span><span class="cx">          // When activated, let the loader handle redirecting the page.
</span><span class="cx">          // If no loader exists, redirect the page ourselves (if a url exists).
</span><span class="lines">@@ -1198,7 +1206,7 @@
</span><span class="cx">          api.trigger( 'ready' );
</span><span class="cx"> 
</span><span class="cx">          // Make sure left column gets focus
</span><del>-               topFocus = $('.back');
</del><ins>+                topFocus = backBtn;
</ins><span class="cx">           topFocus.focus();
</span><span class="cx">          setTimeout(function () {
</span><span class="cx">                  topFocus.focus();
</span></span></pre></div>
<a id="trunksrcwpincludesjscustomizeloaderjs"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/js/customize-loader.js (29024 => 29025)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/js/customize-loader.js     2014-07-08 15:47:20 UTC (rev 29024)
+++ trunk/src/wp-includes/js/customize-loader.js        2014-07-08 17:03:48 UTC (rev 29025)
</span><span class="lines">@@ -1,4 +1,4 @@
</span><del>-/* global _wpCustomizeLoaderSettings */
</del><ins>+/* global _wpCustomizeLoaderSettings, confirm */
</ins><span class="cx"> window.wp = window.wp || {};
</span><span class="cx"> 
</span><span class="cx"> (function( exports, $ ){
</span><span class="lines">@@ -36,8 +36,9 @@
</span><span class="cx">                  });
</span><span class="cx"> 
</span><span class="cx">                  // Add navigation listeners.
</span><del>-                       if ( $.support.history )
</del><ins>+                        if ( $.support.history ) {
</ins><span class="cx">                           this.window.on( 'popstate', Loader.popstate );
</span><ins>+                       }
</ins><span class="cx"> 
</span><span class="cx">                  if ( $.support.hashchange ) {
</span><span class="cx">                          this.window.on( 'hashchange', Loader.hashchange );
</span><span class="lines">@@ -47,35 +48,48 @@
</span><span class="cx"> 
</span><span class="cx">          popstate: function( e ) {
</span><span class="cx">                  var state = e.originalEvent.state;
</span><del>-                       if ( state && state.customize )
</del><ins>+                        if ( state && state.customize ) {
</ins><span class="cx">                           Loader.open( state.customize );
</span><del>-                       else if ( Loader.active )
</del><ins>+                        } else if ( Loader.active ) {
</ins><span class="cx">                           Loader.close();
</span><ins>+                       }
</ins><span class="cx">           },
</span><span class="cx"> 
</span><span class="cx">          hashchange: function() {
</span><span class="cx">                  var hash = window.location.toString().split('#')[1];
</span><span class="cx"> 
</span><del>-                       if ( hash && 0 === hash.indexOf( 'wp_customize=on' ) )
</del><ins>+                        if ( hash && 0 === hash.indexOf( 'wp_customize=on' ) ) {
</ins><span class="cx">                           Loader.open( Loader.settings.url + '?' + hash );
</span><ins>+                       }
</ins><span class="cx"> 
</span><del>-                       if ( ! hash && ! $.support.history )
</del><ins>+                        if ( ! hash && ! $.support.history ){
</ins><span class="cx">                           Loader.close();
</span><ins>+                       }
</ins><span class="cx">           },
</span><span class="cx"> 
</span><ins>+               beforeunload: function () {
+                       if ( ! Loader.saved() ) {
+                               return Loader.settings.l10n.saveAlert;
+                       }
+               },
+
</ins><span class="cx">           open: function( src ) {
</span><del>-                       var hash;
</del><span class="cx"> 
</span><del>-                       if ( this.active )
</del><ins>+                        if ( this.active ) {
</ins><span class="cx">                           return;
</span><ins>+                       }
</ins><span class="cx"> 
</span><span class="cx">                  // Load the full page on mobile devices.
</span><del>-                       if ( Loader.settings.browser.mobile )
</del><ins>+                        if ( Loader.settings.browser.mobile ) {
</ins><span class="cx">                           return window.location = src;
</span><ins>+                       }
</ins><span class="cx"> 
</span><span class="cx">                  this.active = true;
</span><span class="cx">                  this.body.addClass('customize-loading');
</span><span class="cx"> 
</span><ins>+                       // Dirty state of customizer in iframe
+                       this.saved = new api.Value( true );
+
</ins><span class="cx">                   this.iframe = $( '<iframe />', { src: src }).appendTo( this.element );
</span><span class="cx">                  this.iframe.one( 'load', this.loaded );
</span><span class="cx"> 
</span><span class="lines">@@ -92,28 +106,46 @@
</span><span class="cx">                  });
</span><span class="cx"> 
</span><span class="cx">                  this.messenger.bind( 'close', function() {
</span><del>-                               if ( $.support.history )
</del><ins>+                                if ( $.support.history ) {
</ins><span class="cx">                                   history.back();
</span><del>-                               else if ( $.support.hashchange )
</del><ins>+                                } else if ( $.support.hashchange ) {
</ins><span class="cx">                                   window.location.hash = '';
</span><del>-                               else
</del><ins>+                                } else {
</ins><span class="cx">                                   Loader.close();
</span><del>-                       });
</del><ins>+                                }
+                       } );
</ins><span class="cx"> 
</span><ins>+                       // Prompt AYS dialog when navigating away
+                       $( window ).on( 'beforeunload', this.beforeunload );
+
</ins><span class="cx">                   this.messenger.bind( 'activated', function( location ) {
</span><del>-                               if ( location )
</del><ins>+                                if ( location ) {
</ins><span class="cx">                                   window.location = location;
</span><ins>+                               }
</ins><span class="cx">                   });
</span><span class="cx"> 
</span><del>-                       hash = src.split('?')[1];
</del><ins>+                        this.messenger.bind( 'saved', function () {
+                               Loader.saved( true );
+                       } );
+                       this.messenger.bind( 'change', function () {
+                               Loader.saved( false );
+                       } );
</ins><span class="cx"> 
</span><ins>+                       this.pushState( src );
+
+                       this.trigger( 'open' );
+               },
+
+               pushState: function ( src ) {
+                       var hash;
+
</ins><span class="cx">                   // Ensure we don't call pushState if the user hit the forward button.
</span><del>-                       if ( $.support.history && window.location.href !== src )
</del><ins>+                        if ( $.support.history && window.location.href !== src ) {
</ins><span class="cx">                           history.pushState( { customize: src }, '', src );
</span><del>-                       else if ( ! $.support.history && $.support.hashchange && hash )
</del><ins>+                        } else if ( ! $.support.history && $.support.hashchange && hash ) {
+                               hash = src.split( '?' )[1];
</ins><span class="cx">                           window.location.hash = 'wp_customize=on&' + hash;
</span><del>-
-                       this.trigger( 'open' );
</del><ins>+                        }
</ins><span class="cx">           },
</span><span class="cx"> 
</span><span class="cx">          opened: function() {
</span><span class="lines">@@ -121,15 +153,25 @@
</span><span class="cx">          },
</span><span class="cx"> 
</span><span class="cx">          close: function() {
</span><del>-                       if ( ! this.active )
</del><ins>+                        if ( ! this.active ) {
</ins><span class="cx">                           return;
</span><ins>+                       }
+
+                       // Display AYS dialog if customizer is dirty
+                       if ( ! this.saved() && ! confirm( Loader.settings.l10n.saveAlert ) ) {
+                               // Go forward since Customizer is exited by history.back()
+                               history.forward();
+                               return;
+                       }
+
</ins><span class="cx">                   this.active = false;
</span><span class="cx"> 
</span><span class="cx">                  this.trigger( 'close' );
</span><span class="cx"> 
</span><span class="cx">                  // Return focus to link that was originally clicked.
</span><del>-                       if ( this.link )
</del><ins>+                        if ( this.link ) {
</ins><span class="cx">                           this.link.focus();
</span><ins>+                       }
</ins><span class="cx">           },
</span><span class="cx"> 
</span><span class="cx">          closed: function() {
</span><span class="lines">@@ -137,7 +179,9 @@
</span><span class="cx">                  Loader.messenger.destroy();
</span><span class="cx">                  Loader.iframe    = null;
</span><span class="cx">                  Loader.messenger = null;
</span><ins>+                       Loader.saved     = null;
</ins><span class="cx">                   Loader.body.removeClass( 'customize-active full-overlay-active' ).removeClass( 'customize-loading' );
</span><ins>+                       $( window ).off( 'beforeunload', Loader.beforeunload );
</ins><span class="cx">           },
</span><span class="cx"> 
</span><span class="cx">          loaded: function() {
</span></span></pre></div>
<a id="trunksrcwpincludesscriptloaderphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/script-loader.php (29024 => 29025)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/script-loader.php  2014-07-08 15:47:20 UTC (rev 29024)
+++ trunk/src/wp-includes/script-loader.php     2014-07-08 17:03:48 UTC (rev 29025)
</span><span class="lines">@@ -382,6 +382,7 @@
</span><span class="cx">  did_action( 'init' ) && $scripts->localize( 'customize-controls', '_wpCustomizeControlsL10n', array(
</span><span class="cx">          'activate'  => __( 'Save &amp; Activate' ),
</span><span class="cx">          'save'      => __( 'Save &amp; Publish' ),
</span><ins>+               'saveAlert' => __( 'The changes you made will be lost if you navigate away from this page.' ),
</ins><span class="cx">           'saved'     => __( 'Saved' ),
</span><span class="cx">          'cancel'    => __( 'Cancel' ),
</span><span class="cx">          'close'     => __( 'Close' ),
</span></span></pre></div>
<a id="trunksrcwpincludesthemephp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/theme.php (29024 => 29025)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/theme.php  2014-07-08 15:47:20 UTC (rev 29024)
+++ trunk/src/wp-includes/theme.php     2014-07-08 17:03:48 UTC (rev 29025)
</span><span class="lines">@@ -1874,6 +1874,9 @@
</span><span class="cx">          'url'           => esc_url( admin_url( 'customize.php' ) ),
</span><span class="cx">          'isCrossDomain' => $cross_domain,
</span><span class="cx">          'browser'       => $browser,
</span><ins>+               'l10n'          => array(
+                       'saveAlert' => __( 'The changes you made will be lost if you navigate away from this page.' ),
+               ),
</ins><span class="cx">   );
</span><span class="cx"> 
</span><span class="cx">  $script = 'var _wpCustomizeLoaderSettings = ' . json_encode( $settings ) . ';';
</span></span></pre>
</div>
</div>

</body>
</html>