mediawiki-extensions-Visual.../modules/ve/ui/ve.ui.Inspector.js
Trevor Parscal f31dc45da8 (bug 43841) Major ve.ui rewrite, especially ve.ui.LinkInspector
Objectives:

* Make the link inspector easier to use
* Try to resolve a few bugs (bug 43841, bug 43063, bug 42986)
* Stop using jquery.multiSuggest (which didn't really understand annotations)
* Better divide MediaWiki specifics from generic implementations

Changes:

VisualEditor.php, modules/ve/test/index.php, demos/ve/index.php
* Updated links to files

ve.Registry
* Fixed mistake where registry was initialized as an array - this didn't cause any errors because you can add arbitrary properties to an array and use it like any other object

ve.Factory
* Removed duplicate initialization of registry property
* Added entries property, which is an array that's appended to for tracking the order of registrations

ve.CommandRegistry
* Added mwLink command which opens the mwLink inspector

ve.ui.TextInputWidget
* Added basic widget class for text inputs

ve.ui.TextInputMenuWidget
* Added widget that provides a menu of options for a text input widget

ve.ui.MWLinkTargetInputWidget
* Added MediaWiki specific link target widget

ve.ui.MenuWidget
* Converted ve.ui.Menu into a widget
* Moved the body of onSelect to onMouseUp

ve.ui.LinkTargetInputWidget
* Added link target widget which adds link annotation functionality to a normal text input

ve.ui.InputWidget
* Added generic input widget which emits reliable and instant change events and synchronizes a value property with the DOM value

ve.ui.Widget
* Added base widget class
* Widgets can be used in any frame

ve.ui.Tool
* Fixed line length issues

ve.ui.InspectorFactory
* Made use of new entries property for factories to select the most recently added inspector if more than one match a given annotation

ve.ui.Inspector
* Added auto-focus on the first visible input element on open
* Moved afterClose event to after re-focus on document on close
* Added documentation

ve.ui.Frame
* Adjusted documentation
* Added binding of $$ to the frame context so it can be passed around
* Added documentation

ve.ui.Context
* Added ve.ui.Widget.css to iframes
* Updated code as per moving of ve.ui.Menu to ve.ui.MenuWidget
* Removed unused positionBelowOverlay method
* Added CSS settings to set overlay left and width properties according to context size
* Added documentation

ve.ui.DropdownTool
* Updated code as per moving of ve.ui.Menu to ve.ui.MenuWidget

ve.ui.FormatDropdownTool
* Added documentation

ve.ui.MWLinkButtonTool
* Added MediaWiki specific version of ve.ui.LinkButtonTool, which opens the mwLink inspector

ve.ui.Widget.css
* Added styles for all widgets

ve.ui.Tool.css, ve.init.sa.css, ve.init.mw.ViewPageTarget.css, ve.init.mw.ViewPageTarget-apex.css
* Updated code as per moving of ve.ui.Menu to ve.ui.MenuWidget

ve.ui.Menu.css
* Deleted (merged into ve.ui.Widget.css)

ve.ui.Menu.css
* Deleted suggest styles (no longer used)

pending.gif, pending.psd
* Added diagonal stripe animation to indicate a pending request to the API

ve.ui.MWLinkInspector
* Added MediaWiki specific inspector which uses MediaWiki specific annotations and widgets

ve.ui.LinkInspector
* Removed mw global hint (not needed anymore)
* Switched from comparing targets to annotations (since the target text is ambiguous in some situations)
* Switched to using input widget, which is configured using a static property
* Removed use of jquery.multiSuggest
* Moved MediaWiki specifics to their own class (ve.ui.MWLinkInspector)

ve.init.mw.ViewPageTarget
* Added MediaWiki specific toolbar and command options

Change-Id: I859b5871a9d2f17d970c002067c8ff24f3513e9f
2013-01-15 15:05:11 -08:00

235 lines
5.6 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
* @extends ve.EventEmitter
* @constructor
* @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 */
/**
* Symbolic name of icon.
*
* @static
* @property
* @type {string}
*/
ve.ui.Inspector.static.icon = 'inspector';
/**
* Localized message for title.
*
* @static
* @property
* @type {string}
*/
ve.ui.Inspector.static.titleMessage = 'visualeditor-inspector-title';
/**
* Pattern to use when matching against annotation type strings.
*
* @static
* @property
* @type {RegExp}
*/
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 ) {
e.preventDefault();
this.close();
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.$form.find( ':input:visible:first' ).focus();
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 ) {
if ( !this.closing ) {
this.closing = true;
this.$.hide();
this.emit( 'beforeClose', remove );
if ( this.onClose ) {
this.onClose( remove );
}
this.context.getSurface().getView().getDocument().getDocumentNode().$.focus();
this.emit( 'afterClose', remove );
this.closing = false;
}
};