Trevor Parscal has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/155664

Change subject: Make window managers destroyable
......................................................................

Make window managers destroyable

Destroying will remove the element, unbind global events and restore screen 
reader visibility to the rest of the document.

Change-Id: I4ac89a85a762b19a107786df449baa98b25e1054
---
M src/WindowManager.js
1 file changed, 83 insertions(+), 30 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/oojs/ui refs/changes/64/155664/1

diff --git a/src/WindowManager.js b/src/WindowManager.js
index 37dbb25..e387998 100644
--- a/src/WindowManager.js
+++ b/src/WindowManager.js
@@ -401,21 +401,8 @@
                // Ensure handlers get called after preparingToOpen is set
                this.preparingToOpen.done( function () {
                        if ( manager.modal ) {
-                               manager.$( manager.getElementDocument() ).on( {
-                                       // Prevent scrolling by keys in 
top-level window
-                                       keydown: 
manager.onDocumentKeyDownHandler
-                               } );
-                               manager.$( manager.getElementWindow() ).on( {
-                                       // Prevent scrolling by wheel in 
top-level window
-                                       mousewheel: 
manager.onWindowMouseWheelHandler,
-                                       // Start listening for top-level window 
dimension changes
-                                       'orientationchange resize': 
manager.onWindowResizeHandler
-                               } );
-                               // Hide other content from screen readers
-                               manager.$ariaHidden = $( 'body' )
-                                       .children()
-                                       .not( manager.$element.parentsUntil( 
'body' ).last() )
-                                       .attr( 'aria-hidden', '' );
+                               manager.toggleGlobalEvents( true );
+                               manager.toggleAriaIsolation( true );
                        }
                        manager.currentWindow = win;
                        manager.opening = opening;
@@ -502,21 +489,8 @@
                                                win.teardown( data ).then( 
function () {
                                                        closing.notify( { 
state: 'teardown' } );
                                                        if ( manager.modal ) {
-                                                               manager.$( 
manager.getElementDocument() ).off( {
-                                                                       // 
Allow scrolling by keys in top-level window
-                                                                       
keydown: manager.onDocumentKeyDownHandler
-                                                               } );
-                                                               manager.$( 
manager.getElementWindow() ).off( {
-                                                                       // 
Allow scrolling by wheel in top-level window
-                                                                       
mousewheel: manager.onWindowMouseWheelHandler,
-                                                                       // Stop 
listening for top-level window dimension changes
-                                                                       
'orientationchange resize': manager.onWindowResizeHandler
-                                                               } );
-                                                       }
-                                                       // Restore screen 
reader visiblity
-                                                       if ( 
manager.$ariaHidden ) {
-                                                               
manager.$ariaHidden.removeAttr( 'aria-hidden' );
-                                                               
manager.$ariaHidden = null;
+                                                               
manager.toggleGlobalEvents( false );
+                                                               
manager.toggleAriaIsolation( false );
                                                        }
                                                        manager.closing = null;
                                                        manager.currentWindow = 
null;
@@ -634,3 +608,82 @@
 
        return this;
 };
+
+/**
+ * Toggle screen reader visibility of content other than the window manager.
+ *
+ * @param {boolean} [isolate] Make only the window manager visible to screen 
readers
+ * @chainable
+ */
+OO.ui.WindowManager.prototype.toggleGlobalEvents = function ( on ) {
+       on = on === undefined ? !!this.globalEvents : !!on;
+
+       if ( on ) {
+               if ( !this.globalEvents ) {
+                       // Hide everything other than the window manager from 
screen readers
+                       this.$( this.getElementDocument() ).on( {
+                               // Prevent scrolling by keys in top-level window
+                               keydown: this.onDocumentKeyDownHandler
+                       } );
+                       this.$( this.getElementWindow() ).on( {
+                               // Prevent scrolling by wheel in top-level 
window
+                               mousewheel: this.onWindowMouseWheelHandler,
+                               // Start listening for top-level window 
dimension changes
+                               'orientationchange resize': 
this.onWindowResizeHandler
+                       } );
+                       this.globalEvents = true;
+               }
+       } else if ( this.globalEvents ) {
+               // Unbind global events
+               this.$( this.getElementDocument() ).off( {
+                       // Allow scrolling by keys in top-level window
+                       keydown: this.onDocumentKeyDownHandler
+               } );
+               this.$( this.getElementWindow() ).off( {
+                       // Allow scrolling by wheel in top-level window
+                       mousewheel: this.onWindowMouseWheelHandler,
+                       // Stop listening for top-level window dimension changes
+                       'orientationchange resize': this.onWindowResizeHandler
+               } );
+               this.globalEvents = false;
+       }
+
+       return this;
+};
+
+/**
+ * Toggle screen reader visibility of content other than the window manager.
+ *
+ * @param {boolean} [isolate] Make only the window manager visible to screen 
readers
+ * @chainable
+ */
+OO.ui.WindowManager.prototype.toggleAriaIsolation = function ( isolate ) {
+       isolate = isolate === undefined ? !this.$ariaHidden : !!isolate;
+
+       if ( isolate ) {
+               if ( !this.$ariaHidden ) {
+                       // Hide everything other than the window manager from 
screen readers
+                       this.$ariaHidden = $( 'body' )
+                               .children()
+                               .not( this.$element.parentsUntil( 'body' 
).last() )
+                               .attr( 'aria-hidden', '' );
+               }
+       } else if ( this.$ariaHidden ) {
+               // Restore screen reader visiblity
+               this.$ariaHidden.removeAttr( 'aria-hidden' );
+               this.$ariaHidden = null;
+       }
+
+       return this;
+};
+
+/**
+ * Destroy window manager.
+ *
+ * Windows will not be closed, only removed from the DOM.
+ */
+OO.ui.WindowManager.prototype.destroy = function () {
+       this.toggleGlobalEvents( false );
+       this.toggleAriaIsolation( false );
+       this.$element.remove();
+};

-- 
To view, visit https://gerrit.wikimedia.org/r/155664
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I4ac89a85a762b19a107786df449baa98b25e1054
Gerrit-PatchSet: 1
Gerrit-Project: oojs/ui
Gerrit-Branch: master
Gerrit-Owner: Trevor Parscal <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to