import * as $ from 'jquery' ; import { fromEvent , Observable } from 'rxjs' ; import { takeUntil , throttleTime } from 'rxjs/operators' ; import { ZC_DL_BUTTON_CLASS , ZC_DL_BUTTON_ICON_CLASS } from 'src/ts/constants' ; import { ContentPageMessenger } from 'src/ts/messaging/page/content-page-messenger' ; import { RequestDownloadMessage } from 'src/ts/messaging/page/request-download.message' ; export const DownloadButtonFactory = { create ( onUnload$ , resourceInfoUrl ) { const dlButton = createDlButton ( ) ; addDlButtonBehavior ( dlButton , onUnload$ , resourceInfoUrl ) ; return dlButton ; } , } ; function createDlButton ( ) { return $ ( '<button/>' ) . addClass ( [ 'sc-button' , 'sc-button-responsive' ] ) . addClass ( [ ZC_DL_BUTTON_CLASS , ZC_DL_BUTTON_ICON_CLASS ] ) . prop ( 'title' , 'Download' ) . html ( 'Download' ) ; } function addDlButtonBehavior ( dlButton , onUnload$ , resourceInfoUrl ) { fromEvent ( dlButton , 'click' ) . pipe ( takeUntil ( onUnload$ ) , throttleTime ( 3000 ) ) . subscribe ( ( ) => ContentPageMessenger . sendToExtension$ ( new RequestDownloadMessage ( resourceInfoUrl ) ) ) ; }