mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-11-25 06:46:26 +00:00
353297e5b5
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
352 lines
9 KiB
JavaScript
352 lines
9 KiB
JavaScript
/**
|
|
* VisualEditor user interface Context class.
|
|
*
|
|
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
*/
|
|
|
|
/**
|
|
* Creates an ve.ui.Context object.
|
|
*
|
|
* @class
|
|
* @constructor
|
|
* @param surface
|
|
* @param {jQuery} $overlay DOM selection to add nodes to
|
|
*/
|
|
ve.ui.Context = function VeUiContext( surface, $overlay ) {
|
|
// Properties
|
|
this.surface = surface;
|
|
this.inspectors = {};
|
|
this.inspector = null;
|
|
this.position = null;
|
|
this.visible = false;
|
|
this.selecting = false;
|
|
this.selection = null;
|
|
this.frame = null;
|
|
this.menu = null;
|
|
this.toolbar = null;
|
|
this.$ = $( '<div class="ve-ui-context"></div>' );
|
|
this.$callout = $( '<div class="ve-ui-context-callout"></div>' );
|
|
this.$inner = $( '<div class="ve-ui-context-inner"></div>' );
|
|
this.$overlay = $( '<div class="ve-ui-context-frame-overlay"></div>' );
|
|
this.$menu = $( '<div class="ve-ui-context-menu"></div>' );
|
|
this.$inspectors = $( '<div class="ve-ui-context-inspectors"></div>' );
|
|
|
|
// Initialization
|
|
this.$.append( this.$callout, this.$inner, this.$overlay );
|
|
this.$inner.append( this.$menu, this.$inspectors );
|
|
( $overlay || $( 'body' ) ).append( this.$ );
|
|
this.frame = new ve.ui.Frame( this.constructor.static.frameOptions, this.$inspectors );
|
|
|
|
// Events
|
|
this.surface.getModel().addListenerMethods( this, { 'change': 'onChange' } );
|
|
this.surface.getView()
|
|
.addListenerMethods( this, {
|
|
'selectionStart': 'onSelectionStart',
|
|
'selectionEnd': 'onSelectionEnd'
|
|
} );
|
|
|
|
$( window ).on( {
|
|
'resize': ve.bind( this.update, this ),
|
|
'focus': ve.bind( this.onWindowFocus, this )
|
|
} );
|
|
};
|
|
|
|
/* Static Members */
|
|
|
|
ve.ui.Context.static = {};
|
|
|
|
ve.ui.Context.static.frameOptions = {
|
|
'stylesheets': [
|
|
ve.init.platform.getModulesUrl() + '/ve/ui/styles/ve.ui.Inspector.css',
|
|
ve.init.platform.getModulesUrl() + (
|
|
window.devicePixelRatio > 1 ?
|
|
'/ve/ui/styles/ve.ui.Inspector.Icons-vector.css' :
|
|
'/ve/ui/styles/ve.ui.Inspector.Icons-raster.css'
|
|
)
|
|
]
|
|
};
|
|
|
|
/* Methods */
|
|
|
|
/**
|
|
* Responds to change events on the model.
|
|
*
|
|
* Changes are ignored while the user is selecting text.
|
|
*
|
|
* @method
|
|
* @param {ve.dm.Transaction} tx Change transaction
|
|
* @param {ve.Range} selection Change selection
|
|
*/
|
|
ve.ui.Context.prototype.onChange = function ( tx, selection ) {
|
|
if ( selection && selection.start === 0 ) {
|
|
return;
|
|
}
|
|
if ( selection && !this.selecting ) {
|
|
this.update();
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Responds to selection start events on the view.
|
|
*
|
|
* @method
|
|
*/
|
|
ve.ui.Context.prototype.onSelectionStart = function () {
|
|
this.selecting = true;
|
|
this.hide();
|
|
};
|
|
|
|
/**
|
|
* Responds to selection end events on the view.
|
|
*
|
|
* @method
|
|
*/
|
|
ve.ui.Context.prototype.onSelectionEnd = function () {
|
|
this.selecting = false;
|
|
this.update();
|
|
};
|
|
|
|
ve.ui.Context.prototype.onWindowFocus = function () {
|
|
this.hide();
|
|
};
|
|
|
|
/**
|
|
* Responds to an inspector being opened.
|
|
*
|
|
* @method
|
|
* @param {String} name Name of inspector being opened (this is not part of the normal event, it's
|
|
* mixed in when we bound to the event in {initInspector})
|
|
*/
|
|
ve.ui.Context.prototype.onBeforeInspectorOpen = function ( name ) {
|
|
var inspector = this.inspectors[name];
|
|
// Close menu
|
|
if ( this.menu ) {
|
|
this.obscure( this.$menu );
|
|
}
|
|
// Remember which inspector is open
|
|
this.inspector = name;
|
|
// Resize frame to the size of the inspector.
|
|
this.frame.setSize( inspector.$.outerWidth(), inspector.$.outerHeight() );
|
|
// Cache selection, in the case of manually opened inspector.
|
|
this.selection = this.surface.getModel().getSelection();
|
|
// Show context
|
|
this.show();
|
|
};
|
|
|
|
/**
|
|
* Responds to an inspector being closed.
|
|
*
|
|
* @method
|
|
* @param {String} name Name of inspector being closed
|
|
* @param {Boolean} remove Annotation should be removed
|
|
*/
|
|
ve.ui.Context.prototype.onAfterInspectorClose = function () {
|
|
this.obscure( this.$inspectors );
|
|
this.inspector = null;
|
|
this.hide();
|
|
this.update();
|
|
};
|
|
|
|
/**
|
|
* Gets the surface this context is being used in.
|
|
*
|
|
* @method
|
|
* @returns {ve.Surface} Surface of context
|
|
*/
|
|
ve.ui.Context.prototype.getSurface = function () {
|
|
return this.surface;
|
|
};
|
|
|
|
/**
|
|
* Gets the frame that inspectors are being rendered in.
|
|
*
|
|
* @method
|
|
* @returns {ve.ui.Frame} Inspector frame
|
|
*/
|
|
ve.ui.Context.prototype.getFrame = function () {
|
|
return this.frame;
|
|
};
|
|
|
|
/**
|
|
* Destroy the context, removing all DOM elements.
|
|
*
|
|
* @method
|
|
* @returns {ve.ui.Context} Context user interface
|
|
*/
|
|
ve.ui.Context.prototype.destroy = function () {
|
|
this.$.remove();
|
|
};
|
|
|
|
/**
|
|
* Updates the context menu.
|
|
*
|
|
* @method
|
|
*/
|
|
ve.ui.Context.prototype.update = function () {
|
|
var inspectors,
|
|
fragment = this.surface.getModel().getFragment(),
|
|
selection = fragment.getRange(),
|
|
annotations = fragment.getAnnotations();
|
|
|
|
// Update the inspector if the change didn't affect the selection
|
|
if ( this.inspector && selection.equals( this.selection ) ) {
|
|
this.show();
|
|
} else {
|
|
this.hide();
|
|
}
|
|
|
|
if ( !this.inspector ) {
|
|
inspectors = ve.ui.inspectorFactory.getInspectorsForAnnotations( annotations );
|
|
if ( inspectors.length ) {
|
|
// The selection is inspectable but not being inspected
|
|
this.$menu.empty();
|
|
// Create inspector toolbar
|
|
this.toolbar = new ve.ui.Toolbar(
|
|
$( '<div class="ve-ui-context-toolbar"></div>' ),
|
|
this.surface,
|
|
[{ 'name': 'inspectors', 'items' : inspectors }]
|
|
);
|
|
// Note: Menu attaches the provided $tool element to the container.
|
|
this.menu = new ve.ui.Menu(
|
|
[ { 'name': 'tools', '$': this.toolbar.$ } ], // Tools
|
|
null, // Callback
|
|
this.$menu, // Container
|
|
this.$inner // Parent
|
|
);
|
|
if ( !this.visible ) {
|
|
this.show();
|
|
}
|
|
}
|
|
}
|
|
|
|
// Remember selection for next time
|
|
this.selection = selection.clone();
|
|
};
|
|
|
|
ve.ui.Context.prototype.show = function () {
|
|
var selectionRect = this.surface.getView().getSelectionRect();
|
|
|
|
this.position = new ve.Position( selectionRect.end.x, selectionRect.end.y );
|
|
this.$.css( this.position );
|
|
|
|
// Show context
|
|
this.$.css( 'visibility', 'visible' );
|
|
|
|
if ( this.inspector ) {
|
|
// Reveal inspector
|
|
this.reveal( this.$inspectors );
|
|
this.$overlay.show();
|
|
} else {
|
|
if ( !this.visible ) {
|
|
// Fade in the context.
|
|
this.$.fadeIn( 'fast' );
|
|
this.visible = true;
|
|
}
|
|
// Reveal menu
|
|
this.reveal( this.$menu );
|
|
}
|
|
// Position inner context.
|
|
this.positionInner();
|
|
};
|
|
|
|
ve.ui.Context.prototype.hide = function () {
|
|
if ( this.inspector ) {
|
|
this.closeInspector();
|
|
this.$overlay.hide();
|
|
}
|
|
if ( this.menu ) {
|
|
this.obscure( this.$menu );
|
|
this.menu = null;
|
|
}
|
|
this.$.css( 'visibility', 'hidden' );
|
|
this.visible = false;
|
|
};
|
|
|
|
/**
|
|
* Positions the context
|
|
*
|
|
* @param {jQuery} $overlay
|
|
* @param {jQuery} $element
|
|
*/
|
|
ve.ui.Context.prototype.positionInner = function () {
|
|
var $container = this.inspector ? this.$inspectors : this.$menu,
|
|
width = $container.outerWidth( true ),
|
|
height = $container.outerHeight( true ),
|
|
left = -( width / 2 );
|
|
|
|
// Clamp on left boundary
|
|
if ( this.position.left < width / 2 ) {
|
|
left = -( this.$.children().outerWidth( true ) / 2 ) - ( this.position.left / 2 );
|
|
// Clamp on right boundary
|
|
} else if ( $( 'body' ).width() - this.position.left < width ) {
|
|
left = -( width - ( ( $( 'body' ).width() - this.position.left ) / 2) );
|
|
}
|
|
// Apply dimensions to inner
|
|
this.$inner.css( { 'left': left, 'height': height, 'width': width } );
|
|
};
|
|
|
|
/**
|
|
* Positions an overlay element below another element.
|
|
*
|
|
* TODO: Does this really need to be here? Why are we halving the width of $inner?
|
|
*
|
|
* @param {jQuery} $overlay
|
|
* @param {jQuery} $element
|
|
*/
|
|
ve.ui.Context.prototype.positionOverlayBelow = function ( $overlay, $element ) {
|
|
// Set iframe overlay below element.
|
|
$overlay.css( {
|
|
'left': $element.offset().left - ( this.$inner.width() / 2 ),
|
|
'top': $element.offset().top + $element.outerHeight( true ),
|
|
// RTL position fix.
|
|
'width': $overlay.children().outerWidth( true )
|
|
} );
|
|
};
|
|
|
|
ve.ui.Context.prototype.initInspector = function ( name ) {
|
|
var inspector;
|
|
// Add inspector on demand.
|
|
if ( ve.ui.inspectorFactory.lookup( name ) ) {
|
|
if ( !( name in this.inspectors ) ) {
|
|
inspector = this.inspectors[name] = ve.ui.inspectorFactory.create( name, this );
|
|
inspector.on( 'beforeOpen', ve.bind( this.onBeforeInspectorOpen, this, name ) );
|
|
inspector.on( 'afterClose', ve.bind( this.onAfterInspectorClose, this ) );
|
|
inspector.$.hide();
|
|
this.frame.$.append( inspector.$ );
|
|
this.obscure( this.$inspectors );
|
|
}
|
|
return true;
|
|
}
|
|
return false;
|
|
};
|
|
|
|
ve.ui.Context.prototype.openInspector = function ( name ) {
|
|
// Auto-initialize the inspector
|
|
if ( !this.initInspector( name ) ) {
|
|
throw new Error( 'Missing inspector. Cannot open nonexistent inspector: ' + name );
|
|
}
|
|
// Only allow one inspector open at a time
|
|
if ( this.inspector ) {
|
|
this.closeInspector();
|
|
}
|
|
// Open the inspector
|
|
this.inspectors[name].open();
|
|
};
|
|
|
|
ve.ui.Context.prototype.closeInspector = function ( remove ) {
|
|
// Quietly ignore if nothing is open
|
|
if ( this.inspector ) {
|
|
// Close the current inspector
|
|
this.inspectors[this.inspector].close( remove );
|
|
}
|
|
};
|
|
|
|
ve.ui.Context.prototype.reveal = function ( $element ) {
|
|
$element.css( 'top', 0 );
|
|
};
|
|
|
|
ve.ui.Context.prototype.obscure = function ( $element ) {
|
|
$element.css( 'top', -5000 );
|
|
};
|