/* *+------------------------------------------------------------------------+ *| Licensed Materials - Property of IBM *| IBM Cognos Products: Content Explorer *| (C) Copyright IBM Corp. 2015, 2018 *| *| US Government Users Restricted Rights - Use, duplication or disclosure *| restricted by GSA ADP Schedule Contract with IBM Corp. *+------------------------------------------------------------------------+ */ import { GlassContextHelper } from '@businessanalytics/content-nav'; import AjaxService from '../services/AjaxService'; export class UploadFileAction { constructor(options) { const ajaxOptions = { glassContext: options.glassContext }; this.ajaxService = new AjaxService(ajaxOptions); } getFileUploader = (glassContext) => { let fUploaderPromise = glassContext.getSvc('.FileUpload') .then((fileUploader) => { return fileUploader; }) .catch((err) => { glassContext.appController.showErrorMessage(err); return null; }); return fUploaderPromise; } getSearchPath = (parentObj, glassContext, ancestors) => { const targetFolder = ancestors[ancestors.length -1]; const url = targetFolder._meta.links.self.url; const data = { 'fields': 'searchPath' }; return this.ajaxService.getData(url, data) .then((response) => { parentObj = response.data.data[0]; if ((url.indexOf('.my_folders') > -1)) { parentObj.searchPath = null; } }) .catch((err) => { GlassContextHelper.showAjaxServiceError(glassContext, err); return Promise.reject(err); }); } onFileUpload = (parentObj, glassContext, ancestors) => { this.getFileUploader(glassContext) .then(fileUploader => { var fileDestination; if (!parentObj || !parentObj.searchPath) { this.getSearchPath(parentObj, glassContext, ancestors) .then(() => { fileDestination = parentObj.searchPath || '.my_folders'; fileUploader.showFilePicker(null, null, null, { destination: fileDestination }); }); } else { fileDestination = parentObj.searchPath || '.my_folders'; fileUploader.showFilePicker(null, null, null, { destination: fileDestination }); } }); } }