' ).css( 'clear', 'both' )
).append(
$( '
' ).addClass( 'es-toolbar-shadow' )
)
);
surface.$base.find( '.es-panes' ).before( surface.toolbarWrapper[name] );
if ( 'float' in config && config.float === true ) {
// Float top toolbar
surface.floatTopToolbar();
}
}
// Instantiate the toolbar
surface['toolbar-' + name] = new ve.ui.Toolbar(
surface.$base.find( '.es-toolbar' ),
surface.view,
config.tools
);
}
} );
};
/*
* This code is responsible for switching toolbar into floating mode when scrolling ( with
* keyboard or mouse ).
* TODO: Determine if this would be better in ui.toolbar vs here.
* TODO: This needs to be refactored so that it only works on the main editor top tool bar.
*/
ve.Surface.prototype.floatTopToolbar = function () {
if ( !this.toolbarWrapper.top ) {
return;
}
var $toolbarWrapper = this.toolbarWrapper.top,
$toolbar = $toolbarWrapper.find( '.es-toolbar' ),
$window = $( window );
$window.on( {
'resize': function () {
if ( $toolbarWrapper.hasClass( 'es-toolbar-wrapper-floating' ) ) {
var toolbarWrapperOffset = $toolbarWrapper.offset(),
left = toolbarWrapperOffset.left,
right = $window.width() - $toolbarWrapper.outerWidth() - left;
$toolbar.css( {
'left': left,
'right': right
} );
}
},
'scroll': function () {
var left, right,
toolbarWrapperOffset = $toolbarWrapper.offset(),
$editorDocument = $toolbarWrapper.parent().find('.ve-surface .ve-ce-documentNode'),
$lastBranch = $editorDocument.children( '.ve-ce-branchNode:last' );
if ( $window.scrollTop() > toolbarWrapperOffset.top ) {
left = toolbarWrapperOffset.left;
right = $window.width() - $toolbarWrapper.outerWidth() - left;
// If not floating, set float
if ( !$toolbarWrapper.hasClass( 'es-toolbar-wrapper-floating' ) ) {
$toolbarWrapper
.css( 'height', $toolbarWrapper.height() )
.addClass( 'es-toolbar-wrapper-floating' );
$toolbar.css( {
'left': left,
'right': right
} );
} else {
// Toolbar is floated
if (
// There's at least one branch
$lastBranch.length &&
// Toolbar is at or below the top of last node in the document
$window.scrollTop() + $toolbar.height() >= $lastBranch.offset().top
) {
if ( !$toolbarWrapper.hasClass( 'es-toolbar-wrapper-bottom' ) ) {
$toolbarWrapper
.removeClass( 'es-toolbar-wrapper-floating' )
.addClass( 'es-toolbar-wrapper-bottom' );
$toolbar.css({
'top': $window.scrollTop() + 'px',
'left': left,
'right': right
});
}
} else { // Unattach toolbar
if ( $toolbarWrapper.hasClass( 'es-toolbar-wrapper-bottom' ) ) {
$toolbarWrapper
.removeClass( 'es-toolbar-wrapper-bottom' )
.addClass( 'es-toolbar-wrapper-floating' );
$toolbar.css( {
'top': 0,
'left': left,
'right': right
} );
}
}
}
} else { // Return toolbar to top position
if (
$toolbarWrapper.hasClass( 'es-toolbar-wrapper-floating' ) ||
$toolbarWrapper.hasClass( 'es-toolbar-wrapper-bottom' )
) {
$toolbarWrapper.css( 'height', 'auto' )
.removeClass( 'es-toolbar-wrapper-floating' )
.removeClass( 'es-toolbar-wrapper-bottom' );
$toolbar.css( {
'top': 0,
'left': 0,
'right': 0
} );
}
}
}
} );
};