mediawiki-extensions-Visual.../modules/ve/ui/ve.ui.Inspector.js
Trevor Parscal 7ac32bc0f6 No more confusing boolean argument for closing windows
window.close( true ) thing sucked, and was being named and used
inconsistently throughout the code.

The new approach uses an action string, so it looks more like
window.close( 'accept' ) or window.close( 'back' ). This makes it easy
to steer the behavior at any point in the window close code path.

Most importantly for the link inspector, this allows us to now restore
the previous selection when the user presses escape or clicks the back
button, while still moving the cursor to the end and collapsing the
selection upon pressing enter and allowing removal by clicking the
trash can.

This commit also cleans some things up, like the various ways we have
to close an inspector which all seem useless because we wouldn't want
to just randomly close an inspector on someone. An inspector should
be closed only when the user has dealt with it.

ve.InspectorAction.js
* Removed close method

ve.ui.LinkInspector.js
* Updated documentation
* Passing action to parent method
* Updated logic to deal with change from "remove" to "action" argument
* Added selection restauration on "back" action

ve.ui.Context.js
* Added action to call to close
* Removed closeInspector method

ve.ui.Dialog.js
* Moved event handlers to the top
* Added actions to calls to close
* Added click block event handler to prevent focus changes

ve.ui.Inspector.js
* Added actions to calls to close
* Added storing of previous selection - this is different from
  initialSelection because it's captured before the selection is
  modified by setup

ve.ui.Window.js
* Updated documentation
* Updated argument name from "remove" to "action"

ve.ui.WindowSet.js
* Updated documentation
* Removed auto-close, replaced it with error if trying to open a window
  when another is already open
* Removed close method

Change-Id: Ie8f72504177dd6ba169fdddbb776fd5397b831c4
2013-04-10 12:31:49 -07:00

154 lines
3.4 KiB
JavaScript

/*!
* VisualEditor UserInterface Inspector class.
*
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* UserInterface inspector.
*
* @class
* @extends ve.ui.Window
*
* @constructor
* @param {ve.Surface} surface
*/
ve.ui.Inspector = function VeUiInspector( surface ) {
// Inheritance
ve.ui.Window.call( this, surface );
// Properties
this.initialSelection = null;
// Initialization
this.$.addClass( 've-ui-inspector' );
};
/* Inheritance */
ve.inheritClass( ve.ui.Inspector, ve.ui.Window );
/* Static Properties */
/**
* Pattern to use when matching against annotation type strings.
*
* @static
* @property {RegExp}
*/
ve.ui.Inspector.static.typePattern = new RegExp();
ve.ui.Inspector.static.titleMessage = 've-ui-inspector-title';
/* Methods */
/**
* Handle frame ready events.
*
* @method
*/
ve.ui.Inspector.prototype.initialize = function () {
// Call parent method
ve.ui.Window.prototype.initialize.call( this );
// Initialization
this.$form = this.$$( '<form>' );
this.closeButton = new ve.ui.IconButtonWidget( {
'$$': this.$$, 'icon': 'previous', 'title': ve.msg( 'visualeditor-inspector-close-tooltip' )
} );
this.removeButton = new ve.ui.IconButtonWidget( {
'$$': this.$$, 'icon': 'remove', 'title': ve.msg( 'visualeditor-inspector-remove-tooltip' )
} );
// Events
this.$form.on( {
'submit': ve.bind( this.onFormSubmit, this ),
'keydown': ve.bind( this.onFormKeyDown, this )
} );
this.closeButton.on( 'click', ve.bind( this.onCloseButtonClick, this ) );
this.removeButton.on( 'click', ve.bind( this.onRemoveButtonClick, this ) );
// Initialization
this.closeButton.$.addClass( 've-ui-inspector-closeButton' );
this.removeButton.$.addClass( 've-ui-inspector-removeButton' );
this.$head.prepend( this.closeButton.$ ).append( this.removeButton.$ );
this.$body.append( this.$form );
};
/**
* Handle close button click events.
*
* @method
*/
ve.ui.Inspector.prototype.onCloseButtonClick = function () {
this.close( 'back' );
};
/**
* Handle remove button click events.
*
* @method
*/
ve.ui.Inspector.prototype.onRemoveButtonClick = function() {
this.close( 'remove' );
};
/**
* Handle form submission events.
*
* @method
* @param {jQuery.Event} e Form submit event
*/
ve.ui.Inspector.prototype.onFormSubmit = function () {
this.close( 'apply' );
return false;
};
/**
* Handle form keydown events.
*
* @method
* @param {jQuery.Event} e Key down event
*/
ve.ui.Inspector.prototype.onFormKeyDown = function ( e ) {
// Escape
if ( e.which === 27 ) {
this.close( 'back' );
return false;
}
};
/**
* Handle inspector setup events.
*
* @method
*/
ve.ui.Inspector.prototype.onSetup = function () {
this.previousSelection = this.surface.getModel().getSelection();
};
/**
* Handle inspector open events.
*
* @method
*/
ve.ui.Inspector.prototype.onOpen = function () {
this.initialSelection = this.surface.getModel().getSelection();
};
/**
* Get matching annotations within a fragment.
*
* @method
* @param {ve.dm.SurfaceFragment} fragment Fragment to get matching annotations within
* @returns {ve.dm.AnnotationSet} Matching annotations
*/
ve.ui.Inspector.prototype.getMatchingAnnotations = function ( fragment ) {
return fragment.getAnnotations().getAnnotationsByName( this.constructor.static.typePattern );
};
/* Initialization */
ve.ui.Inspector.static.addLocalStylesheets( [ 've.ui.Inspector.css' ] );