mediawiki-extensions-Visual.../modules/es/views/es.ContextView.js

49 lines
1.5 KiB
JavaScript
Raw Normal View History

2011-11-29 23:29:02 +00:00
/**
* Creates an es.ContextView object.
*
* @class
* @constructor
* @param {jQuery} $overlay DOM selection to add nodes to
*/
es.ContextView = function( surfaceView, $overlay ) {
this.surfaceView = surfaceView;
this.$ = $( '<div class="es-contextView"></div>' ).appendTo( $overlay || $( 'body' ) );
this.$menu = $( '<div class="es-contextView-menu"></div>' ).appendTo( this.$ );
this.$icon = $( '<div class="es-contextView-icon"></div>' ).appendTo( this.$ );
};
/* Methods */
es.ContextView.prototype.set = function() {
this.$.removeClass( 'es-contextView-position-below es-contextView-position-above' );
var selection = this.surfaceView.getModel().getSelection(),
position,
offset,
bias;
if ( selection.from < selection.to ) {
var $lastRange = this.surfaceView.$.find( '.es-contentView-range:visible:last' );
if ( $lastRange.length ) {
offset = $lastRange.offset();
position = new es.Position(
offset.left + $lastRange.width(), offset.top + $lastRange.height()
);
this.$.addClass( 'es-contextView-position-below' );
}
} else if ( selection.from > selection.to ) {
var $firstRange = this.surfaceView.$.find( '.es-contentView-range:visible:first' );
if ( $firstRange.length ) {
offset = $firstRange.offset();
position = new es.Position( offset.left, offset.top );
this.$.addClass( 'es-contextView-position-above' );
}
}
if ( position ) {
this.$.css( { 'left': position.left, 'top': position.top } );
this.$icon.fadeIn( 'fast' );
}
};
es.ContextView.prototype.clear = function() {
this.$icon.hide();
};