import { Preconditions } from 'polar-shared/src/Preconditions' ; import { Rects } from '../Rects' ; import { Rect } from '../Rect' ; const $ = require ( 'jquery' ) ; export class Elements { param element param parentElement { HTMLElement } return { Rect } public static getRelativeOffsetRect ( element , parentElement ? ) { Preconditions . assertNotNull ( element , "element" ) ; if ( ! parentElement ) { parentElement = element . ownerDocument ! . documentElement ! ; } const offsetRect = { left : 0 , top : 0 , width : 0 , height : 0 } ; function toInt ( value ) { if ( isNaN ( value ) ) { return 0 ; } return value ; } offsetRect . width = toInt ( element . offsetWidth ) ; offsetRect . height = toInt ( element . offsetHeight ) ; while ( element !== null ) { if ( element === parentElement ) { break ; } offsetRect . left += toInt ( element . offsetLeft ) ; offsetRect . top += toInt ( element . offsetTop ) ; element = < HTMLElement > element . offsetParent ; } return Rects . createFromBasicRect ( offsetRect ) ; } param innerHTML return { HTMLDivElement } public static createWrapperElementHTML ( innerHTML ) { const div = document . createElement ( "div" ) ; div . innerHTML = innerHTML ; return div ; } param html public static createElementHTML ( html , tagName = 'div' ) { const div = document . createElement ( tagName ) ; div . innerHTML = html ; return < HTMLElement > div . firstChild ! ; } public static offset ( element ) { const result = { left : element . offsetLeft , top : element . offsetTop , width : element . offsetWidth , height : element . offsetHeight , right : 0 , bottom : 0 } ; result . right = result . left + result . width ; result . bottom = result . top + result . height ; return Rects . validate ( Rects . createFromBasicRect ( result ) ) ; } /**
     * Require that the element have the given classname.
     */ public static requireClass ( element , clazz ) { const classValue = element . getAttribute ( "class" ) ; if ( ! classValue || classValue . indexOf ( clazz ) === - 1 ) { throw new Error ( "Element does not have the proper class: " + clazz ) ; } } /**
     * Keep searching parent notes until we find an element matching the
     * selector, or return null when one was not found.
     *
     */ public static untilRoot ( element , selector ) { if ( ! element ) { throw new Error ( "element required" ) ; } if ( ! selector ) { throw new Error ( "selector required" ) ; } if ( element . matches ( selector ) ) { return element ; } if ( element . parentElement == null ) { return null ; } return Elements . untilRoot ( element . parentElement , selector ) ; } public static calculateVisibilityForDiv ( div ) { if ( div == null ) { throw Error ( "Not given a div" ) ; } const windowHeight = $ ( window ) . height ( ) ; const docScroll = $ ( document ) . scrollTop ( ) ; const divPosition = $ ( div ) . offset ( ) . top ; const divHeight = $ ( div ) . height ( ) ; const hiddenBefore = docScroll - divPosition ; const hiddenAfter = ( divPosition + divHeight ) - ( docScroll + windowHeight ) ; if ( ( docScroll > divPosition + divHeight ) || ( divPosition > docScroll + windowHeight ) ) { return 0 ; } else { let result = 100 ; if ( hiddenBefore > 0 ) { result -= ( hiddenBefore * 100 ) / divHeight ; } if ( hiddenAfter > 0 ) { result -= ( hiddenAfter * 100 ) / divHeight ; } return result ; } } public static getScrollParent ( element ) { if ( ! element ) { return undefined ; } const scrollHeight = element . scrollHeight ; const clientHeight = element . clientHeight ; if ( scrollHeight > clientHeight ) { return element ; } else { return this . getScrollParent ( element . parentElement ) ; } } }