mediawiki-extensions-Visual.../modules/ve/ui/ve.ui.Inspector.js
Trevor Parscal 353297e5b5 (bug 42925) Inspector doesn't open properly
ve.Range
* Rewrote truncate so that it works as expected, truncation should always reduce the length using the start/end values, not the from/to values

ve.ui.Inspector
* Added a comment about where the name argument to onBeforeInspectorOpen comes from, since it's a little bit confusing on first read (and I wrote it!)
* Calling onInitialize, onOpen and onClose methods directly, since we need to control the before or after-ness of listeners getting in there and doing their stuff - plus it's more direct
* Removed onRemove stub, which is never actually called
* Added before/after versions of initialize, open and close events
* Got rid of recursion guard since we don't need it anymore thanks to changes made in ve.dm.Surface (see below)

ve.ui.Context
* Updated event names to deal with new before/after naming of initialize, open and close events
* Removed fade-in logic since fading in doesn't even work anymore - since now we now annotate first, then open the inspector, the menu will actually exist and be open when we open the inspector even though you don't see it because it's quickly obscured

ve.ui.LinkInspector
* Made fragments non-auto selecting, in the case of onInitialize we actually call select(), which is silly since we were using an auto-selecting fragment - it's clear that this was a mistake

ve.dm.Surface
* Moved locking (polling stop and start) to the far outside edges of the change method
* I need a lot of eyes and testing on this change, it seems OK to me, but I'm suspicious that it may have side effects
* What was happening is that selection changes were being applied and then the poll was picking them up, and then the selection was coming in again as a change, but it wasn't a change at all, it was just feedback - this change event was then closing the inspector the instant it was opened - the odd part was that this only occurred when you selected backwards, which seems to be caused by the range being normalized, so it looked like a new selection even though it wasn't

ve.dm.Document
* trimOuterSpace from Range didn't consider annotated spaces to be spaces, by using the [0] trick (first character of a plain text character string or first element in an annotated character's array both are the character's value - but elements don't have a property named '0' so it skips those safely as well) we can always get the right value for comparison

Change-Id: I0873d906c058203b83b8d4bbe5a4b274f05a26fd
2012-12-10 16:48:13 -08:00

209 lines
5.2 KiB
JavaScript

/**
* VisualEditor user interface Inspector class.
*
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* Creates an ve.ui.Inspector object.
*
* @class
* @constructor
* @extends {ve.EventEmitter}
* @param {ve.ui.Context} context
*/
ve.ui.Inspector = function VeUiInspector( context ) {
// Inheritance
ve.EventEmitter.call( this );
// Properties
this.context = context;
this.initialSelection = null;
this.closing = false;
this.frame = context.getFrame();
this.$ = $( '<div class="ve-ui-inspector"></div>' );
this.$form = this.frame.$$( '<form></form>' );
this.$title = this.frame.$$( '<div class="ve-ui-inspector-title"></div>' )
.text( ve.msg( this.constructor.static.titleMessage ) );
this.$titleIcon = this.frame.$$( '<div class="ve-ui-inspector-titleIcon"></div>' )
.addClass( 've-ui-icon-' + this.constructor.static.icon );
this.$closeButton = this.frame.$$(
'<div class="ve-ui-inspector-button ve-ui-inspector-closeButton ve-ui-icon-close"></div>'
).attr( 'title', ve.msg( 'visualeditor-inspector-close-tooltip' ) );
this.$removeButton = this.frame.$$(
'<div class="ve-ui-inspector-button ve-ui-inspector-removeButton ve-ui-icon-remove"></div>'
).attr( 'title', ve.msg( 'visualeditor-inspector-remove-tooltip' ) );
// Events
this.$closeButton.on( {
'click': ve.bind( this.onCloseButtonClick, this ),
} );
this.$removeButton.on( {
'click': ve.bind( this.onRemoveButtonClick, this ),
} );
this.$form.on( {
'submit': ve.bind( this.onFormSubmit, this ),
'keydown': ve.bind( this.onFormKeyDown, this )
} );
// Initialization
this.$.append(
this.$closeButton,
this.$titleIcon,
this.$title,
this.$removeButton,
this.$form
);
};
/* Inheritance */
ve.inheritClass( ve.ui.Inspector, ve.EventEmitter );
/* Static Members */
ve.ui.Inspector.static.icon = 'inspector';
ve.ui.Inspector.static.titleMessage = 'visualeditor-inspector-title';
ve.ui.Inspector.static.typePattern = new RegExp();
/* Methods */
/**
* Responds to close button click events.
*
* @method
* @param {jQuery.Event} e Click event
*/
ve.ui.Inspector.prototype.onCloseButtonClick = function () {
this.close();
};
/**
* Responds to remove button click events.
*
* @method
* @param {jQuery.Event} e Click event
* @emits 'remove'
*/
ve.ui.Inspector.prototype.onRemoveButtonClick = function() {
this.close( true );
};
/**
* Responds to form submission events.
*
* @method
* @param {jQuery.Event} e Submit event
*/
ve.ui.Inspector.prototype.onFormSubmit = function ( e ) {
this.close();
e.preventDefault();
return false;
};
/**
* Responds to form keydown events.
*
* @method
* @param {jQuery.Event} e Keydown event
*/
ve.ui.Inspector.prototype.onFormKeyDown = function ( e ) {
// Escape
if ( e.which === 27 ) {
this.close();
e.preventDefault();
return false;
}
};
/**
* Responds to the inspector being initialized.
*
* This gives an inspector an opportunity to make selection and annotation changes prior to the
* inspector being opened.
*
* @method
*/
ve.ui.Inspector.prototype.onInitialize = function () {
// This is a stub, override functionality in child classes
};
/**
* Responds to the inspector being opened.
*
* This is when an inspector would initialize it's form with data from the selection.
*
* @method
*/
ve.ui.Inspector.prototype.onOpen = function () {
// This is a stub, override functionality in child classes
};
/**
* Responds to the inspector being closed.
*
* This is when an inspector would apply any changes made in the form to the selection.
*
* @method
* @param {Boolean} accept Changes to the form should be applied
*/
ve.ui.Inspector.prototype.onClose = function () {
// This is a stub, override functionality in child classes
};
/**
* Gets a list of matching annotations in selection.
*
* @method
* @param {ve.dm.SurfaceFragment} fragment Fragment to get matching annotations within
* @returns {ve.AnnotationSet} Matching annotations
*/
ve.ui.Inspector.prototype.getMatchingAnnotations = function ( fragment ) {
return fragment.getAnnotations().getAnnotationsByName( this.constructor.static.typePattern );
};
/**
* Opens inspector.
*
* @method
* @emits 'initialize'
* @emits 'open'
*/
ve.ui.Inspector.prototype.open = function () {
this.$.show();
this.emit( 'beforeInitialize' );
if ( this.onInitialize ) {
this.onInitialize();
}
this.emit( 'afterInitialize' );
this.initialSelection = this.context.getSurface().getModel().getSelection();
this.emit( 'beforeOpen' );
if ( this.onOpen ) {
this.onOpen();
}
this.emit( 'afterOpen' );
};
/**
* Closes inspector.
*
* This method guards against recursive calling internally. Recursion on this method is caused by
* changes to the document occuring in a close handler which in turn produce document model change
* events, which in turn cause the context to close the inspector again, and so on.
*
* @method
* @emits 'close' (remove)
*/
ve.ui.Inspector.prototype.close = function ( remove ) {
this.$.hide();
this.emit( 'beforeClose', remove );
if ( this.onClose ) {
this.onClose( remove );
}
this.emit( 'afterClose', remove );
this.context.getSurface().getView().getDocument().getDocumentNode().$.focus();
};