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.$ );
|
2011-11-29 23:48:11 +00:00
|
|
|
|
|
|
|
// Example menu items
|
|
|
|
this.$menu.append(
|
2011-11-30 00:06:46 +00:00
|
|
|
'<div class="es-contextView-menuSection">' +
|
2011-11-30 00:19:27 +00:00
|
|
|
'<div class="es-contextView-menuButton es-contextView-menuButton-bold"></div>' +
|
|
|
|
'<div class="es-contextView-menuButton es-contextView-menuButton-italic"></div>' +
|
2011-11-30 00:35:58 +00:00
|
|
|
'<div class="es-contextView-menuButton es-contextView-menuButton-clear"></div>' +
|
2011-11-30 00:06:46 +00:00
|
|
|
'</div>' +
|
|
|
|
'<div class="es-contextView-menuItem-break"></div>' +
|
2011-11-29 23:48:11 +00:00
|
|
|
'<div class="es-contextView-menuItem">Cut</div>' +
|
|
|
|
'<div class="es-contextView-menuItem">Copy</div>' +
|
|
|
|
'<div class="es-contextView-menuItem">Paste</div>' +
|
2011-11-30 00:51:11 +00:00
|
|
|
'<div class="es-contextView-menuItem">Delete</div>'
|
2011-11-29 23:48:11 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Events
|
|
|
|
var _this = this;
|
|
|
|
this.$icon.click( function() {
|
|
|
|
_this.$menu.toggle();
|
|
|
|
} );
|
2011-11-29 23:29:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
es.ContextView.prototype.set = function() {
|
2011-11-30 00:48:46 +00:00
|
|
|
this.$.removeClass(
|
|
|
|
'es-contextView-position-below es-contextView-position-above ' +
|
2011-11-30 01:23:37 +00:00
|
|
|
'es-contextView-position-left es-contextView-position-right' +
|
|
|
|
'es-contextView-position-start es-contextView-position-end'
|
2011-11-30 00:48:46 +00:00
|
|
|
);
|
2011-11-29 23:29:02 +00:00
|
|
|
var selection = this.surfaceView.getModel().getSelection(),
|
|
|
|
position,
|
2011-11-30 01:23:37 +00:00
|
|
|
offset;
|
2011-11-29 23:29:02 +00:00
|
|
|
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()
|
|
|
|
);
|
2011-11-30 01:23:37 +00:00
|
|
|
this.$.addClass( 'es-contextView-position-end' );
|
2011-11-29 23:29:02 +00:00
|
|
|
}
|
|
|
|
} 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 );
|
2011-11-30 01:23:37 +00:00
|
|
|
this.$.addClass( 'es-contextView-position-start' );
|
2011-11-29 23:29:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( position ) {
|
2011-11-30 00:48:46 +00:00
|
|
|
if ( position.left + this.$menu.width() < $( 'body' ).width() ) {
|
|
|
|
this.$.addClass( 'es-contextView-position-left' );
|
|
|
|
} else {
|
|
|
|
this.$.addClass( 'es-contextView-position-right' );
|
|
|
|
}
|
2011-11-30 01:23:37 +00:00
|
|
|
if ( position.top + this.$menu.height() < $( window ).height() ) {
|
|
|
|
this.$.addClass( 'es-contextView-position-below' );
|
|
|
|
} else {
|
|
|
|
this.$.addClass( 'es-contextView-position-above' );
|
|
|
|
}
|
2011-11-29 23:29:02 +00:00
|
|
|
this.$.css( { 'left': position.left, 'top': position.top } );
|
|
|
|
this.$icon.fadeIn( 'fast' );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
es.ContextView.prototype.clear = function() {
|
|
|
|
this.$icon.hide();
|
2011-11-29 23:48:11 +00:00
|
|
|
this.$menu.hide();
|
2011-11-29 23:29:02 +00:00
|
|
|
};
|