import Utils from '../core/Utils' import Plugin from './Plugin' export default class Google extends Plugin { constructor (core, opts) { super(core, opts) this.type = 'selecter' this.files = [] // set default options const defaultOptions = {} // merge default options with the ones set by user this.opts = Object.assign({}, defaultOptions, opts) this.currentFolder = 'root' this.isAuthenticated = false this.checkAuthentication() } focus () { if (!this.isAuthenticated) { this.target.innerHTML = this.renderAuth() } else { this.renderFolder() } } checkAuthentication () { fetch('http://localhost:3020/google/authorize', { method: 'get', credentials: 'include', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' } }) .then(res => { if (res.status >= 200 && res.status <= 300) { return res.json().then(data => { this.isAuthenticated = data.isAuthenticated this.authUrl = data.authUrl }) } else { let error = new Error(res.statusText) error.response = res throw error } }) } getFolder (folderId = this.currentFolder) { /** * Leave this here */ // fetch('http://localhost:3020/google/logout', { // method: 'get', // credentials: 'include', // headers: { // 'Accept': 'application/json', // 'Content-Type': 'application/json' // } // }).then(res => console.log(res)) return fetch('http://localhost:3020/google/list', { method: 'get', credentials: 'include', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: { dir: folderId || undefined } }) .then(res => { if (res.status >= 200 && res.status <= 300) { return res.json().then(data => { let folders = [] let files = [] data.items.forEach(item => { if (item.mimeType === 'application/vnd.google-apps.folder') { folders.push(item) } else { files.push(item) } }) return { folders, files } }) } }) } getFile (fileId) { console.log(typeof fileId) if (fileId !== 'string') { return console.log('Error: File Id not a string.') } return fetch('http://localhost:3020/google/get', { method: 'get', credentials: 'include', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: { fileId } }) } install () { const caller = this this.target = this.getTarget(this.opts.target, caller) return } renderAuth () { return `