mediawiki-extensions-Visual.../modules/ve/ui/ve.ui.Inspector.js
Trevor Parscal a5aeca3ff5 Major UI refactoring and improvements
Objective:

Refactor UI widgets, improve usability and accessibility of menus, general cleanup and style improvements.

Extras:
Fixed documentation in a few other files to make descriptions of jQuery event arguments more consistent, classes inherit correctly, and made use of the @cfg functionality in jsduck.

Changes:

.docs/config.json
* Added window, HTMLDocument, HTMLElement, DocumentFragment and XMLHttpRequest to externals, so jsduck doesn't throw warnings when they are used

demos/ve/index.php, modules/ve/test/index.php, VisualEditor.php
* Moved widgets above tools (since tools use widgets)

demos/ve/index.php
* Refactored widget initialization to use options
* Renamed variables to match widget names

ve.init.mw.ViewPageTarget.css
* Adjusted text sizes to make widgets work normally
* Added margins for buttons in toolbar (since button widgets
don't have any)
* Removed styles for init buttons (button widgets now)

ve.init.mw.ViewPageTarget.js
* Switched to using button widgets (involved moving things around
a bit)

ve.ui.LinkInspector.js, ve.ui.MWLinkInspector.js
* Renamed static property "inputWidget" to
"linkTargetInputWidget" to better reflect the required base class
for the properties value

icons.ai, check.png, check.svg
* Added "check" icon, used in menu right now to show which item
is selected

ve.ui.Icons-raster.css, ve.ui.Icons-vector.css
* Added check icon
* Removed :before pseudo selectors from most of the icon classes (not need by button tool anymore, makes them more reusable now)

ve.ui.Tool.css
* Adjusted drop down tool styles so menu appears below, instead
of on top, of the label
* Adjusted paragraph font size to better match actual content
* Updated class names to still work with menu widget changes
(items are their own widgets now)
* Updated selectors as per changes in the structure of button tools

ve.ui.Widget.css
* Added styles for buttons and menu items
* Adjusted menu styles

ve.ui.*ButtonTool.js
* Added config options argument passthrough

ve.ui.ButtonTool.js
* Moved var statement to the top inside constructor
* Switched to using "a" tag to get cross-browser :active support
* Added icon to inside of button to make icon styles more reusable
* Removed disabled support (now provided by widget parent class)

ve.ui.FormatDropDownTool.js
* Updated options initialization to construct menu item objects
* Modified handling of items to account for changes in menu and
item classes
* Optimized onUpdateState method a bit, adding early exit to
inner loop

ve.ui.ButtonTool.js, ve.ui.DropdownTool.js, ve.ui.Context.js,
ve.ui.Frame, ve.ui.Tool.js, ve.ui.Widget.js
* Added chain ability to non-getter methods

ve.ui.DropdownTool.js
* Removed items argument to constructor
* Updated code as per changes in menu class
* Fixed inconsistent naming of event handler methods
* Removed item event handling (now handled by items directly)
* Made use of this.$$ to ensure tool works in other frames

ve.ui.Tool.js
* Made tools inherit from widget
* Moved trigger registry event handler to a method

ve.ui.Context.js
* Switched from using menu to contain toolbar to a simple wrapper

ve.ui.js
* Added get$$ method, a convenience function for binding jQuery
to a specific document context

ve.ui.*Widget.js
* Switched to using a config options object instead of individual arguments
* Added options
* Factored out flags and labels into their own classes
* Refactored value setting methods for inputs

ve.ui.MenuWidget.js, ve.ui.MenuItemWidget.js
* Broke items out into their own classes
* Redesigned API
* Updated code that uses these classes
* Added support for keyboard interaction
* Made items flash when selected (delaying the hiding of the menu for 200ms)

ve.ui.LinkTargetInputWidget.js, ve.ui.MWLinkTargetInputWidget
* Refactored annotation setting methods

Change-Id: I7769bd5a5b79f1ab36f258ef9f2be583ca503ce6
2013-02-26 12:29:08 -08:00

233 lines
5.5 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.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 Properties */
/**
* 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 */
/**
* Handle close button click events.
*
* @method
* @param {jQuery.Event} e Mouse click event
*/
ve.ui.Inspector.prototype.onCloseButtonClick = function () {
this.close();
};
/**
* Handle remove button click events.
*
* @method
* @param {jQuery.Event} e Mouse click event
* @emits 'remove'
*/
ve.ui.Inspector.prototype.onRemoveButtonClick = function() {
this.close( true );
};
/**
* Handle form submission events.
*
* @method
* @param {jQuery.Event} e Form submit event
*/
ve.ui.Inspector.prototype.onFormSubmit = function () {
this.close();
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();
return false;
}
};
/**
* Handle 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
};
/**
* Handle the inspector being opened.
*
* This is when an inspector would initialize its form with data from the selection.
*
* @method
*/
ve.ui.Inspector.prototype.onOpen = function () {
// This is a stub, override functionality in child classes
};
/**
* Handle 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
};
/**
* Get matching annotations within a fragment.
*
* @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 );
};
/**
* Open 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' );
};
/**
* Close 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;
}
};