2016-01-28 23:48:14 +01:00
import Ember from 'ember' ;
2015-11-12 15:52:14 +01:00
2016-08-11 23:37:14 +02:00
export default Ember . Component . extend ( {
2016-01-28 23:48:14 +01:00
didInsertElement ( ) {
2015-11-12 15:52:14 +01:00
this . _super ( ) ;
2016-01-28 23:48:14 +01:00
Ember . run . scheduleOnce ( 'afterRender' , this , function ( ) {
2015-11-12 15:52:14 +01:00
/ *
* adding floatThead jQuery plugin to poll table
* https : //mkoryak.github.io/floatThead/
*
2016-08-23 11:43:58 +02:00
* top :
* Offset from the top of the ` window ` where the floating header will
2015-11-12 15:52:14 +01:00
* 'stick' when scrolling down
* Since we are adding a browser horizontal scrollbar on top , scrollingTop
* has to be set to height of horizontal scrollbar which depends on
* used browser
* /
Ember . $ ( '.user-selections-table' ) . floatThead ( {
2016-08-23 11:43:58 +02:00
top : this . getScrollbarHeight
2015-11-12 15:52:14 +01:00
} ) ;
/ *
* fix width calculation error caused by bootstrap glyphicon on webkit
* /
Ember . $ ( '.glyphicon' ) . css ( 'width' , '14px' ) ;
/ *
* scrollbar on top of table
* /
2016-01-28 23:48:14 +01:00
const topScrollbarInner = Ember . $ ( '<div></div>' )
. css ( 'width' , Ember . $ ( '.user-selections-table' ) . width ( ) )
2015-11-12 15:52:14 +01:00
. css ( 'height' , '1px' ) ;
2016-01-28 23:48:14 +01:00
const topScrollbarOuter = Ember . $ ( '<div></div>' )
2015-11-12 15:52:14 +01:00
. addClass ( 'top-scrollbar' )
2016-01-28 23:48:14 +01:00
. css ( 'width' , '100%' )
2015-11-12 15:52:14 +01:00
. css ( 'overflow-x' , 'scroll' )
. css ( 'overflow-y' , 'hidden' )
. css ( 'position' , 'relative' )
. css ( 'z-index' , '1002' ) ;
Ember . $ ( '.table-scroll' ) . before (
topScrollbarOuter . append ( topScrollbarInner )
) ;
/ *
* scrollbar on top of table for thead
* /
2016-01-28 23:48:14 +01:00
const topScrollbarInnerThead = Ember . $ ( '<div></div>' )
. css ( 'width' , Ember . $ ( '.user-selections-table' ) . width ( ) )
2015-11-12 15:52:14 +01:00
. css ( 'height' , '1px' ) ;
2016-01-28 23:48:14 +01:00
const topScrollbarOuterThead = Ember . $ ( '<div></div>' )
2015-11-12 15:52:14 +01:00
. addClass ( 'top-scrollbar-floatThead' )
2016-01-28 23:48:14 +01:00
. css ( 'width' , Ember . $ ( '.table-scroll' ) . outerWidth ( ) )
2015-11-12 15:52:14 +01:00
. css ( 'overflow-x' , 'scroll' )
. css ( 'overflow-y' , 'hidden' )
. css ( 'position' , 'fixed' )
. css ( 'top' , '-1px' )
. css ( 'z-index' , '1002' )
2016-01-28 23:48:14 +01:00
. css ( 'margin-left' , ` ${ ( Ember . $ ( '.table-scroll' ) . outerWidth ( ) - Ember . $ ( '.table-scroll' ) . width ( ) ) / 2 * ( - 1 ) } px ` )
. css ( 'margin-right' , ` ${ ( Ember . $ ( '.table-scroll' ) . outerWidth ( ) - Ember . $ ( '.table-scroll' ) . width ( ) ) / 2 * ( - 1 ) } px ` ) ;
2015-11-12 15:52:14 +01:00
Ember . $ ( '.table-scroll' ) . prepend (
topScrollbarOuterThead . append ( topScrollbarInnerThead ) . hide ( )
) ;
// add listener to resize scrollbars if window get resized
2016-01-28 23:48:14 +01:00
Ember . $ ( window ) . resize ( this . resizeScrollbars ) ;
2015-11-12 15:52:14 +01:00
/ *
* bind scroll event on all scrollbars
* /
2016-01-28 23:48:14 +01:00
Ember . $ ( '.table-scroll' ) . scroll ( function ( ) {
Ember . $ ( '.top-scrollbar' ) . scrollLeft ( Ember . $ ( '.table-scroll' ) . scrollLeft ( ) ) ;
Ember . $ ( '.top-scrollbar-floatThead' ) . scrollLeft ( Ember . $ ( '.table-scroll' ) . scrollLeft ( ) ) ;
2015-11-12 15:52:14 +01:00
} ) ;
2016-01-28 23:48:14 +01:00
Ember . $ ( '.top-scrollbar' ) . scroll ( function ( ) {
Ember . $ ( '.table-scroll' ) . scrollLeft ( Ember . $ ( '.top-scrollbar' ) . scrollLeft ( ) ) ;
Ember . $ ( '.top-scrollbar-floatThead' ) . scrollLeft ( Ember . $ ( '.top-scrollbar' ) . scrollLeft ( ) ) ;
2015-11-12 15:52:14 +01:00
} ) ;
2016-01-28 23:48:14 +01:00
Ember . $ ( '.top-scrollbar-floatThead' ) . scroll ( function ( ) {
Ember . $ ( '.table-scroll' ) . scrollLeft ( Ember . $ ( '.top-scrollbar-floatThead' ) . scrollLeft ( ) ) ;
Ember . $ ( '.top-scrollbar' ) . scrollLeft ( Ember . $ ( '.top-scrollbar-floatThead' ) . scrollLeft ( ) ) ;
2015-11-12 15:52:14 +01:00
} ) ;
/ *
* show inner scrollbar only , if header is fixed
* /
2016-01-28 23:48:14 +01:00
Ember . $ ( window ) . scroll ( Ember . $ . proxy ( this . updateScrollbarTopVisibility , this ) ) ;
2015-11-12 15:52:14 +01:00
} ) ;
} ,
/ *
* calculates horizontal scrollbar height depending on current browser
* /
2016-01-28 23:48:14 +01:00
getScrollbarHeight ( ) {
2016-08-13 18:46:46 +02:00
const wideScrollWtml = Ember . $ ( '<div>' ) . attr ( 'id' , 'wide_scroll_div_one' ) . css ( {
'width' : 50 ,
'height' : 50 ,
'overflow-y' : 'scroll' ,
'position' : 'absolute' ,
'top' : - 200 ,
'left' : - 200
} ) . append (
Ember . $ ( '<div>' ) . attr ( 'id' , 'wide_scroll_div_two' ) . css ( {
'height' : '100%' ,
'width' : 100
} )
) ;
2016-01-28 23:48:14 +01:00
Ember . $ ( 'body' ) . append ( wideScrollWtml ) ; // Append our div and add the hmtl to your document for calculations
const scrollW1 = Ember . $ ( '#wide_scroll_div_one' ) . height ( ) ; // Getting the width of the surrounding(parent) div - we already know it is 50px since we styled it but just to make sure.
const scrollW2 = Ember . $ ( '#wide_scroll_div_two' ) . innerHeight ( ) ; // Find the inner width of the inner(child) div.
const scrollBarWidth = scrollW1 - scrollW2 ; // subtract the difference
Ember . $ ( '#wide_scroll_div_one' ) . remove ( ) ; // remove the html from your document
return scrollBarWidth ;
2015-11-12 15:52:14 +01:00
} ,
/ *
* show / hide top scrollbar depending on window position
* used as event callback when window is scrolled
* /
2016-01-28 23:48:14 +01:00
updateScrollbarTopVisibility ( ) {
const windowTop = Ember . $ ( window ) . scrollTop ( ) ;
const tableTop = Ember . $ ( '.table-scroll table' ) . offset ( ) . top ;
if ( windowTop >= tableTop - this . getScrollbarHeight ( ) ) {
2015-11-12 15:52:14 +01:00
Ember . $ ( '.top-scrollbar-floatThead' ) . show ( ) ;
// update scroll position
2016-01-28 23:48:14 +01:00
Ember . $ ( '.top-scrollbar-floatThead' ) . scrollLeft ( Ember . $ ( '.table-scroll' ) . scrollLeft ( ) ) ;
} else {
2015-11-12 15:52:14 +01:00
Ember . $ ( '.top-scrollbar-floatThead' ) . hide ( ) ;
}
} ,
/ *
* resize scrollbars
* used as event callback when window is resized
* /
2016-01-28 23:48:14 +01:00
resizeScrollbars ( ) {
Ember . $ ( '.top-scrollbar div' ) . css ( 'width' , Ember . $ ( '.user-selections-table' ) . width ( ) ) ;
Ember . $ ( '.top-scrollbar-floatThead' ) . css ( 'width' , Ember . $ ( '.table-scroll' ) . outerWidth ( ) ) ;
Ember . $ ( '.top-scrollbar-floatThead div' ) . css ( 'width' , Ember . $ ( '.user-selections-table' ) . width ( ) ) ;
2015-11-12 15:52:14 +01:00
} ,
/ *
* resize scrollbars if document height might be changed
* and therefore scrollbars might be added
* /
2016-08-02 01:55:48 +02:00
triggerResizeScrollbars : Ember . observer ( 'controller.isEvaluable' , 'controller.model.users.[]' , function ( ) {
2016-01-28 23:48:14 +01:00
Ember . run . next ( ( ) => {
this . resizeScrollbars ( ) ;
2015-11-12 15:52:14 +01:00
} ) ;
2016-01-28 23:48:14 +01:00
} ) ,
2015-11-12 15:52:14 +01:00
/ *
* clean up
* especially remove event listeners
* /
2016-01-28 23:48:14 +01:00
willDestroyElement ( ) {
Ember . $ ( window ) . off ( 'resize' , this . resizeScrollbars ) ;
Ember . $ ( window ) . off ( 'scroll' , this . updateScrollbarTopVisibility ) ;
2015-11-12 15:52:14 +01:00
}
} ) ;