gapiService.ts

export const SignedInState = { Init: 0, True: 1, False: 2 }; export class GapiService { @Output() static onAuthChange = new EventEmitter<boolean>(); static signInState = SignedInState.Init; static get auth(): Promise<any> { return new Promise<any>((resolve) => { if (gapi.auth2 && gapi.auth2.getAuthInstance()) { resolve(gapi.auth2) } else { gapi.load('auth2', () => { gapi.auth2.init({ client_id: GapiConfig.gapiClientId, scope: GapiConfig.gapiScope }).then(auth2 => { auth2.isSignedIn.listen(signedIn => { this.signInState = signedIn ? SignedInState.True : SignedInState.False; this.onAuthChange.emit(signedIn); }); resolve(gapi.auth2) }); }); } }); } static get client(): Promise<any> { return new Promise<any>((resolve) => { if (gapi.client) { resolve(gapi.client) } else { gapi.load('client', () => { resolve(gapi.client) }); } }); } static get drive(): Promise<any> { return new Promise<any>((resolve) => { GapiService.client.then(gclient => { if (gclient.drive) { resolve(gclient.drive); } else { gclient.load('drive', 'v3', () => { resolve(gclient.drive); }) } }) }); } static get calendar(): Promise<any> { return new Promise<any>((resolve) => { GapiService.client.then(gclient => { if (gclient.calendar) { resolve(gclient.calendar); } else { gclient.load('calendar', 'v3', () => { resolve(gclient.calendar); }) } }) }); } static signOut() { gapi.auth2.getAuthInstance().signOut(); } static buildQuery(params: Parameter[]): string { let q = ''; params.forEach((p, i) => { let qq = p.key; if (p.type === ParameterType.pString) { qq += '=' + Utils.addSingleQuotes(p.value) } else if (p.type === ParameterType.pBooleanTrue) { qq += '=true' } else if (p.type === ParameterType.pBooleanFalse) { qq += '=false' } else if (p.type === ParameterType.pCollection) { qq += '=' + p.value } else if (p.type === ParameterType.pCustom) { qq = p.key + p.value // just override } q += qq; if (i < params.length - 1) { q += ' and '; } }); return q; } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.