Esanders has uploaded a new change for review.

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

Change subject: Refactor WindowAction to include close and toggle methods
......................................................................

Refactor WindowAction to include close and toggle methods

Change-Id: Icce535a0e1b99341417dd3808f2db669c57f9bc2
---
M src/ui/actions/ve.ui.WindowAction.js
1 file changed, 87 insertions(+), 20 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/VisualEditor/VisualEditor 
refs/changes/48/175748/1

diff --git a/src/ui/actions/ve.ui.WindowAction.js 
b/src/ui/actions/ve.ui.WindowAction.js
index c41e3fe..294768b 100644
--- a/src/ui/actions/ve.ui.WindowAction.js
+++ b/src/ui/actions/ve.ui.WindowAction.js
@@ -31,46 +31,113 @@
  * @static
  * @property
  */
-ve.ui.WindowAction.static.methods = [ 'open' ];
+ve.ui.WindowAction.static.methods = [ 'open', 'close', 'toggle' ];
 
 /* Methods */
 
 /**
- * Open a window.
+ * Open or toggle a window.
  *
  * @method
  * @param {string} name Symbolic name of window to open
  * @param {Object} [data] Window opening data
+ * @return {boolean} Action was executed
  */
 ve.ui.WindowAction.prototype.open = function ( name, data ) {
-       var windowManager,
+       var onOpen, openingPromise,
                windowClass = ve.ui.windowFactory.lookup( name ),
+               windowManager = windowClass && this.getWindowManager( 
windowClass ),
                surface = this.surface,
                fragment = surface.getModel().getFragment( undefined, true ),
                dir = 
surface.getView().getDocument().getDirectionFromSelection( 
fragment.getSelection() ) ||
                        surface.getModel().getDocument().getDir();
 
+       if ( !windowManager ) {
+               return false;
+       }
+
        data = ve.extendObject( { dir: dir }, data, { fragment: fragment } );
 
-       if ( windowClass ) {
-               if ( windowClass.prototype instanceof ve.ui.FragmentInspector ) 
{
-                       windowManager = surface.getContext().getInspectors();
-                       windowManager.openWindow( name, data );
-               } else if ( windowClass.prototype instanceof OO.ui.Dialog ) {
-                       // For non-isolated dialogs, remove the selection and 
re-apply on close
-                       surface.getView().nativeSelection.removeAllRanges();
-                       windowManager = surface.getDialogs();
-                       windowManager.openWindow( name, data ).then( function ( 
opened ) {
-                               opened.then( function ( closing ) {
-                                       closing.then( function () {
-                                               // Check the dialog didn't 
modify the selection before restoring from fragment
-                                               if ( 
surface.getModel().getSelection().isNull() ) {
-                                                       fragment.select();
-                                               }
-                                       } );
+       if ( windowClass.prototype instanceof OO.ui.Dialog ) {
+               // For non-isolated dialogs, remove the selection and re-apply 
on close
+               surface.getView().nativeSelection.removeAllRanges();
+               onOpen = function ( opened ) {
+                       opened.then( function ( closing ) {
+                               closing.then( function () {
+                                       // Check the dialog didn't modify the 
selection before restoring from fragment
+                                       if ( 
surface.getModel().getSelection().isNull() ) {
+                                               fragment.select();
+                                       }
                                } );
                        } );
-               }
+               };
+       }
+
+       openingPromise = windowManager.openWindow( name, data );
+
+       if ( onOpen ) {
+               openingPromise.then( onOpen );
+       }
+       return true;
+};
+
+/**
+ * Close a window
+ *
+ * @method
+ * @param {string} name Symbolic name of window to open
+ * @param {Object} [data] Window closing data
+ * @return {boolean} Action was executed
+ */
+ve.ui.WindowAction.prototype.close = function ( name, data ) {
+       var windowClass = ve.ui.windowFactory.lookup( name ),
+               windowManager = windowClass && this.getWindowManager( 
windowClass );
+
+       if ( !windowManager ) {
+               return false;
+       }
+
+       windowManager.closeWindow( name, data );
+       return true;
+};
+
+/**
+ * Toggle a window between open and close
+ *
+ * @method
+ * @param {string} name Symbolic name of window to open or close
+ * @param {Object} [data] Window opening or closing data
+ * @return {boolean} Action was executed
+ */
+ve.ui.WindowAction.prototype.toggle = function ( name, data ) {
+       var win,
+               windowClass = ve.ui.windowFactory.lookup( name ),
+               windowManager = windowClass && this.getWindowManager( 
windowClass );
+
+       if ( !windowManager ) {
+               return false;
+       }
+
+       win = windowManager.getCurrentWindow();
+       if ( !win || win.constructor.static.name !== name ) {
+               this.open( name, data );
+       } else {
+               this.close( name, data );
+       }
+       return true;
+};
+
+/**
+ * Get the window manager for a specified window class
+ *
+ * @param {Function} windowClass Window class
+ * @return {ve.ui.WindowManager|undefined} Window manager
+ */
+ve.ui.WindowAction.prototype.getWindowManager = function ( windowClass ) {
+       if ( windowClass.prototype instanceof ve.ui.FragmentInspector ) {
+               return this.surface.getContext().getInspectors();
+       } else if ( windowClass.prototype instanceof OO.ui.Dialog ) {
+               return this.surface.getDialogs();
        }
 };
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Icce535a0e1b99341417dd3808f2db669c57f9bc2
Gerrit-PatchSet: 1
Gerrit-Project: VisualEditor/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Esanders <[email protected]>

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

Reply via email to