2018-05-17 07:53:58 -07:00
|
|
|
import React, { PureComponent, Fragment } from 'react';
|
2018-04-20 06:58:36 -07:00
|
|
|
import ReactDOM from 'react-dom';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { IntlProvider, addLocaleData } from 'react-intl';
|
|
|
|
import { getLocale } from 'mastodon/locales';
|
|
|
|
import MediaGallery from 'flavours/glitch/components/media_gallery';
|
2018-05-17 07:53:58 -07:00
|
|
|
import Video from 'flavours/glitch/features/video';
|
|
|
|
import Card from 'flavours/glitch/features/status/components/card';
|
2018-04-20 06:58:36 -07:00
|
|
|
import ModalRoot from 'flavours/glitch/components/modal_root';
|
|
|
|
import MediaModal from 'flavours/glitch/features/ui/components/media_modal';
|
|
|
|
import { fromJS } from 'immutable';
|
|
|
|
|
|
|
|
const { localeData, messages } = getLocale();
|
|
|
|
addLocaleData(localeData);
|
|
|
|
|
2018-05-17 07:53:58 -07:00
|
|
|
const MEDIA_COMPONENTS = { MediaGallery, Video, Card };
|
|
|
|
|
|
|
|
export default class MediaContainer extends PureComponent {
|
2018-04-20 06:58:36 -07:00
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
locale: PropTypes.string.isRequired,
|
2018-05-17 07:53:58 -07:00
|
|
|
components: PropTypes.object.isRequired,
|
2018-04-20 06:58:36 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
state = {
|
|
|
|
media: null,
|
|
|
|
index: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
handleOpenMedia = (media, index) => {
|
2018-05-17 07:53:58 -07:00
|
|
|
document.body.classList.add('media-standalone__body');
|
2018-04-20 06:58:36 -07:00
|
|
|
this.setState({ media, index });
|
|
|
|
}
|
|
|
|
|
|
|
|
handleCloseMedia = () => {
|
2018-05-17 07:53:58 -07:00
|
|
|
document.body.classList.remove('media-standalone__body');
|
2018-04-20 06:58:36 -07:00
|
|
|
this.setState({ media: null, index: null });
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
2018-05-17 07:53:58 -07:00
|
|
|
const { locale, components } = this.props;
|
2018-04-20 06:58:36 -07:00
|
|
|
|
|
|
|
return (
|
|
|
|
<IntlProvider locale={locale} messages={messages}>
|
2018-05-17 07:53:58 -07:00
|
|
|
<Fragment>
|
|
|
|
{[].map.call(components, (component, i) => {
|
|
|
|
const componentName = component.getAttribute('data-component');
|
|
|
|
const Component = MEDIA_COMPONENTS[componentName];
|
|
|
|
const { media, card, ...props } = JSON.parse(component.getAttribute('data-props'));
|
|
|
|
|
|
|
|
Object.assign(props, {
|
|
|
|
...(media ? { media: fromJS(media) } : {}),
|
|
|
|
...(card ? { card: fromJS(card) } : {}),
|
|
|
|
});
|
2018-04-20 06:58:36 -07:00
|
|
|
|
|
|
|
return ReactDOM.createPortal(
|
2018-05-17 07:53:58 -07:00
|
|
|
<Component onOpenMedia={this.handleOpenMedia} {...props} key={`media-${i}`} />,
|
|
|
|
component,
|
2018-04-20 06:58:36 -07:00
|
|
|
);
|
|
|
|
})}
|
|
|
|
<ModalRoot onClose={this.handleCloseMedia}>
|
|
|
|
{this.state.media === null || this.state.index === null ? null : (
|
|
|
|
<MediaModal
|
|
|
|
media={this.state.media}
|
|
|
|
index={this.state.index}
|
|
|
|
onClose={this.handleCloseMedia}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</ModalRoot>
|
2018-05-17 07:53:58 -07:00
|
|
|
</Fragment>
|
2018-04-20 06:58:36 -07:00
|
|
|
</IntlProvider>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|