Permalink
Browse files

[t665] onclose event for editors

1. Right before closing, editors will issue a close message through comm.
2. Rest of "close" code happen (almost) next JS event loop.
  • Loading branch information...
1 parent 728de40 commit 83fad2b6849086b5d2d46521175750ef6795cad5 @secretrobotron committed Apr 26, 2012
Showing with 57 additions and 37 deletions.
  1. +4 −0 editors/default-editor.html
  2. +26 −18 src/dialog/iframe-dialog.js
  3. +27 −19 src/dialog/window-dialog.js
View
4 editors/default-editor.html
@@ -54,6 +54,10 @@
}
}, false);
+ _comm.listen( "close", function( e ){
+ // use this to process something right before the editor closes
+ });
+
_comm.listen( "trackeventupdated", function( e ){
for( var item in _manifest ){
var element = document.getElementById( item );
View
44 src/dialog/iframe-dialog.js
@@ -39,24 +39,32 @@ define( [
} //onCancel
this.close = function(){
- _parent.removeChild( _iframe );
- if( _modalLayer ){
- _modalLayer.destroy();
- _modalLayer = undefined;
- } //if
- _comm.unlisten( "submit", onSubmit );
- _comm.unlisten( "cancel", onCancel );
- _comm.unlisten( "close", _this.close );
- _comm.destroy();
- _open = false;
- window.removeEventListener( "beforeunload", _this.close, false);
- for( var e in _listeners ){
- if( e !== "close" ){
- _em.unlisten( e, _listeners[ e ] );
- }
- } //for
- _em.dispatch( "close" );
- _em.unlisten( "close", _listeners.close );
+ // Send a close message to the dialog first, then actually close the dialog.
+ // A setTimeout is used here to ensure that its associated function will be run
+ // almost right after the postMessage happens. This ensures that messages get to
+ // their destination before we remove the dom element (which will basically ruin
+ // everything) by placing callbacks in the browser's event loop in the correct order.
+ _this.send( "close" );
+ setTimeout(function(){
@mjschranz
mjschranz Apr 26, 2012

setTimeout( function(){

+ _parent.removeChild( _iframe );
+ if( _modalLayer ){
+ _modalLayer.destroy();
+ _modalLayer = undefined;
+ } //if
+ _comm.unlisten( "submit", onSubmit );
+ _comm.unlisten( "cancel", onCancel );
+ _comm.unlisten( "close", _this.close );
+ _comm.destroy();
+ _open = false;
+ window.removeEventListener( "beforeunload", _this.close, false);
@mjschranz
mjschranz Apr 26, 2012

_this.close, false );

+ for( var e in _listeners ){
+ if( e !== "close" ){
+ _em.unlisten( e, _listeners[ e ] );
+ }
+ } //for
+ _em.dispatch( "close" );
+ _em.unlisten( "close", _listeners.close );
+ }, 0);
}; //close
this.open = function( listeners ){
View
46 src/dialog/window-dialog.js
@@ -59,25 +59,33 @@ define( [
} //onError
this.close = function(){
- if( _modalLayer ){
- _modalLayer.destroy();
- _modalLayer = undefined;
- } //if
- _comm.unlisten( "submit", onSubmit );
- _comm.unlisten( "cancel", onCancel );
- _comm.unlisten( "close", _this.close );
- _comm.destroy();
- if( _window.close ){
- _window.close();
- } //if
- clearInterval( _statusInterval );
- window.removeEventListener( "beforeunload", _this.close, false);
- _comm = _window = undefined;
- _open = false;
- for( var e in _listeners ){
- _em.unlisten( e, _listeners[ e ] );
- } //for
- _em.dispatch( "close" );
+ // Send a close message to the dialog first, then actually close the dialog.
+ // A setTimeout is used here to ensure that its associated function will be run
+ // almost right after the postMessage happens. This ensures that messages get to
+ // their destination before we remove the dom element (which will basically ruin
+ // everything) by placing callbacks in the browser's event loop in the correct order.
+ _this.send( "close" );
+ setTimeout(function(){
@mjschranz
mjschranz Apr 26, 2012

setTimeout( function(){

+ if( _modalLayer ){
+ _modalLayer.destroy();
+ _modalLayer = undefined;
+ } //if
+ _comm.unlisten( "submit", onSubmit );
+ _comm.unlisten( "cancel", onCancel );
+ _comm.unlisten( "close", _this.close );
+ _comm.destroy();
+ if( _window.close ){
+ _window.close();
+ } //if
+ clearInterval( _statusInterval );
+ window.removeEventListener( "beforeunload", _this.close, false);
@mjschranz
mjschranz Apr 26, 2012

_this.close, false );

+ _comm = _window = undefined;
+ _open = false;
+ for( var e in _listeners ){
+ _em.unlisten( e, _listeners[ e ] );
+ } //for
+ _em.dispatch( "close" );
+ }, 0 );
}; //close
function checkWindowStatus(){

0 comments on commit 83fad2b

Please sign in to comment.