Skip to content

Commit fefadfb

Browse files
committed
Merge branch 't/13135' into major
2 parents b0d867d + 620ceb4 commit fefadfb

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

samples/toolbarconfigurator/index.html

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ <h2>What am I doing here?</h2>
147147

148148
<script>
149149
( function() {
150+
'use strict';
151+
150152
var mode = ( window.location.hash.substr( 1 ) === 'advanced' ) ? 'advanced' : 'basic',
151153
configuratorSection = CKEDITOR.document.findOne( 'main > .grid-container.configurator' ),
152154
basicInstruction = CKEDITOR.document.findOne( '#instructions-container > .basic' ),
@@ -293,6 +295,95 @@ <h2>What am I doing here?</h2>
293295

294296
modeSwitchBasic.on( 'click', toggleModeBasic );
295297
modeSwitchAdvanced.on( 'click', toggleModeAdvanced );
298+
299+
//
300+
// Position:sticky for the toolbar.
301+
//
302+
303+
// Will make elements behave like they were styled with position:sticky.
304+
var positionSticky = {
305+
// Store object: {
306+
// element: CKEDITOR.dom.element, // Element which will float.
307+
// placeholder: CKEDITOR.dom.element, // Placeholder which is place to prevent page bounce.
308+
// isFixed: boolean // Whether element float now.
309+
// }
310+
watched: [],
311+
312+
watch: function( element ) {
313+
this.watched.push( {
314+
element: element,
315+
placeholder: new CKEDITOR.dom.element( 'div' ),
316+
isFixed: false
317+
} );
318+
},
319+
320+
checkAll: function() {
321+
for ( var i = 0; i < this.watched.length; i++ ) {
322+
this.check( this.watched[ i ] );
323+
}
324+
},
325+
326+
check: function( element ) {
327+
var isFixed = element.isFixed;
328+
var shouldBeFixed = this.shouldBeFixed( element );
329+
330+
// Nothing to be done.
331+
if ( isFixed === shouldBeFixed ) {
332+
return;
333+
}
334+
335+
var placeholder = element.placeholder;
336+
337+
if ( isFixed ) {
338+
// Unfixing.
339+
placeholder.remove();
340+
341+
element.element.removeStyle( 'width' );
342+
element.element.removeStyle( 'height' );
343+
344+
element.element.removeStyle( 'position' );
345+
element.element.removeStyle( 'top' );
346+
element.element.removeStyle( 'z-index' );
347+
348+
} else {
349+
// Fixing.
350+
placeholder.setStyle( 'width', element.element.getSize( 'width' ) + 'px' );
351+
placeholder.setStyle( 'height', element.element.getSize( 'height' ) + 'px' );
352+
353+
var width = element.element.getSize( 'width' ),
354+
height = element.element.getSize( 'height' );
355+
356+
element.element.setStyles( {
357+
// Workaround: Decrement width by one to keep the same visual width.
358+
width: ( width - 1 ) + 'px',
359+
height: height + 'px',
360+
position: 'fixed',
361+
top: '0',
362+
'z-index': '1000'
363+
} );
364+
365+
placeholder.insertAfter( element.element );
366+
}
367+
368+
element.isFixed = !element.isFixed;
369+
},
370+
371+
shouldBeFixed: function( element ) {
372+
// If element is already fixed we are checking it's placeholder.
373+
var related = ( element.isFixed ? element.placeholder : element.element ),
374+
clientRect = related.$.getBoundingClientRect();
375+
376+
return ( clientRect.top < 0 );
377+
}
378+
};
379+
380+
CKEDITOR.document.getWindow().on( 'scroll',
381+
new CKEDITOR.tools.eventsBuffer( 100, positionSticky.checkAll, positionSticky ).input
382+
);
383+
384+
// Make the toolbar sticky.
385+
positionSticky.watch( CKEDITOR.document.findOne( '.editors-container' ) );
386+
296387
} )();
297388
</script>
298389
</body>

0 commit comments

Comments
 (0)