|
| 1 | +import * as React from 'react'; |
| 2 | +import styles from './StockImages.module.scss'; |
| 3 | +import { StockImagesEvent, SubmitValue, IStockImagesProps } from '.'; |
| 4 | +import { GeneralHelper } from '../../../Utilities'; |
| 5 | +import { IFilePickerResult } from '..'; |
| 6 | + |
| 7 | +export class StockImages extends React.Component<IStockImagesProps> { |
| 8 | + public componentDidMount() { |
| 9 | + window.addEventListener("message", this._handleImageIframeEvent); |
| 10 | + } |
| 11 | + |
| 12 | + public componentWillUnmount() { |
| 13 | + window.removeEventListener("message", this._handleImageIframeEvent); |
| 14 | + } |
| 15 | + |
| 16 | + public render(): React.ReactElement<IStockImagesProps> { |
| 17 | + const { language } = this.props; |
| 18 | + |
| 19 | + const themesColor = `&themecolors=${encodeURIComponent(this.getCurrentThemeConfiguration())}`; |
| 20 | + const contentPickerUrl = `https://hubblecontent.osi.office.net/contentsvc/external/m365contentpicker/index.html?p=3&app=1001&aud=prod&channel=devmain&setlang=${language}&msel=0&env=prod&premium=1${themesColor}`; |
| 21 | + |
| 22 | + return ( |
| 23 | + <div className={styles.tabContainer}> |
| 24 | + <div className={styles.tab}> |
| 25 | + <div className={styles.stockImagesPickerContainer}> |
| 26 | + <iframe className={styles.stockImagesPicker} role={"application"} id={"stockImagesIFrame"} |
| 27 | + src={contentPickerUrl} /> |
| 28 | + </div> |
| 29 | + </div> |
| 30 | + </div> |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + private _handleImageIframeEvent = (event) => { |
| 35 | + if (!event || !event.origin || event.origin.indexOf("https://hubblecontent.osi.office.net") !== 0) { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + const eventData: StockImagesEvent = JSON.parse(event.data); |
| 40 | + |
| 41 | + if (eventData.MessageId === "AddItem") { |
| 42 | + this._handleSave(eventData); |
| 43 | + } else if (eventData.MessageId === "CancelDialog") { |
| 44 | + this._handleClose(); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Called when user saves |
| 50 | + */ |
| 51 | + private _handleSave = (event: StockImagesEvent) => { |
| 52 | + let filePickerResult: IFilePickerResult = null; |
| 53 | + const cdnFileInfo: SubmitValue = event.Values && (event.Values as SubmitValue[]).length > 0 ? (event.Values as SubmitValue[])[0] : null; |
| 54 | + if (cdnFileInfo) { |
| 55 | + filePickerResult = { |
| 56 | + downloadFileContent: () => { return this.props.fileSearchService.downloadBingContent(cdnFileInfo.sourceUrl, GeneralHelper.getFileNameFromUrl(GeneralHelper.getFileNameFromUrl(cdnFileInfo.sourceUrl))); }, |
| 57 | + fileAbsoluteUrl: cdnFileInfo.sourceUrl, |
| 58 | + fileName: GeneralHelper.getFileNameFromUrl(cdnFileInfo.sourceUrl), |
| 59 | + fileNameWithoutExtension: GeneralHelper.getFileNameWithoutExtension(cdnFileInfo.sourceUrl) |
| 60 | + }; |
| 61 | + } |
| 62 | + |
| 63 | + this.props.onSave(filePickerResult); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Called when user closes tab |
| 68 | + */ |
| 69 | + private _handleClose = () => { |
| 70 | + this.props.onClose(); |
| 71 | + } |
| 72 | + |
| 73 | + private getCurrentThemeConfiguration() { |
| 74 | + if (!window["__themeState__"] || !window["__themeState__"].theme) { |
| 75 | + return ""; |
| 76 | + } |
| 77 | + |
| 78 | + const primaryColor = window["__themeState__"].theme["themePrimary"]; |
| 79 | + const textColor = window["__themeState__"].theme["primaryText"]; |
| 80 | + const primaryBackground = window["__themeState__"].theme["bodyBackground"]; |
| 81 | + const neutralLighter = window["__themeState__"].theme["neutralLighter"]; |
| 82 | + |
| 83 | + const theme = `{"primaryColor":"${primaryColor}","textColor":"${textColor}","backgroundColor":"${primaryBackground}","neutralLighterColor":"${neutralLighter}"}`; |
| 84 | + return theme; |
| 85 | + } |
| 86 | +} |
0 commit comments