'use strict' ; import type DataEventEmitter from '../DataEventEmitter' ; import type JSON from '../../Core/JSON' ; import DataStore from './DataStore.js' ; import DataTable from '../DataTable.js' ; import GoogleSheetsParser from '../Parsers/GoogleSheetsParser.js' ; import HU from '../../Core/HttpUtilities.js' ; const { ajax } = HU ; import U from '../../Core/Utilities.js' ; const { merge } = U ; private class GoogleSheetsStore extends DataStore < GoogleSheetsStore . Event > { protected static readonly defaultOptions : GoogleSheetsStore . Options = { googleSpreadsheetKey : '' , worksheet : 1 , enablePolling : false , dataRefreshRate : 2 , firstRowAsNames : true } ; param { DataTable } table param { CSVStore . OptionsType } options param { DataParser } parser public constructor ( table , options , parser ? ) { super ( table ) ; this . options = merge ( GoogleSheetsStore . defaultOptions , options ) ; this . parser = parser || new GoogleSheetsParser ( { firstRowAsNames : this . options . firstRowAsNames } ) ; } public readonly options : GoogleSheetsStore . Options ; /**
     * The attached parser, which can be replaced in the constructor
     */ public readonly parser : GoogleSheetsParser ; param { DataEventEmitter . EventDetail } eventDetail private fetchSheet ( eventDetail ? ) { const store = this , { enablePolling , dataRefreshRate , googleSpreadsheetKey , worksheet } = store . options , url = [ 'https://spreadsheets.google.com/feeds/cells' , googleSpreadsheetKey , worksheet , 'public/values?alt=json' ] . join ( '/' ) ; store . table . deleteColumns ( ) ; store . emit ( { type : 'load' , detail : eventDetail , table : store . table , url } ) ; ajax ( { url : url , dataType : 'json' , success : function ( json ) : void { store . parser . parse ( json ) ; store . table . setColumns ( store . parser . getTable ( ) . getColumns ( ) ) ; if ( enablePolling ) { setTimeout ( function ( ) : void { store . fetchSheet ( ) ; } , dataRefreshRate * 1000 ) ; } store . emit ( { type : 'afterLoad' , detail : eventDetail , table : store . table , url } ) ; } , error : function ( xhr , error ) : void { store . emit ( { type : 'loadError' , detail : eventDetail , error , table : store . table , xhr } ) ; } } ) ; } param { DataEventEmitter . EventDetail } eventDetail public load ( eventDetail ? ) { if ( this . options . googleSpreadsheetKey ) { this . fetchSheet ( eventDetail ) ; } } } namespace GoogleSheetsStore { export type Event = ( ErrorEvent | LoadEvent ) ; export interface ErrorEvent extends DataStore . Event { readonly type : 'loadError' ; readonly error : ( string | Error ) ; readonly xhr : XMLHttpRequest ; } export interface LoadEvent extends DataStore . Event { readonly type : ( 'load' | 'afterLoad' ) ; readonly url : string ; } export interface Options extends JSON . Object { googleSpreadsheetKey : string ; worksheet ? : number ; enablePolling : boolean ; dataRefreshRate : number ; firstRowAsNames : boolean ; } } DataStore . addStore ( GoogleSheetsStore ) ; declare module './StoreType' { interface StoreTypeRegistry { Google : typeof GoogleSheetsStore ; } } export default GoogleSheetsStore ;