2017-07-21 22:16:45 +08:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import { defineMessages, injectIntl } from 'react-intl';
|
|
|
|
import { withModalMounter } from '/imports/ui/components/modal/service';
|
|
|
|
import Clipboard from 'clipboard';
|
2017-07-22 02:17:25 +08:00
|
|
|
import _ from 'lodash';
|
2017-07-21 22:16:45 +08:00
|
|
|
import Dropdown from '/imports/ui/components/dropdown/component';
|
|
|
|
import DropdownTrigger from '/imports/ui/components/dropdown/trigger/component';
|
|
|
|
import DropdownContent from '/imports/ui/components/dropdown/content/component';
|
|
|
|
import DropdownList from '/imports/ui/components/dropdown/list/component';
|
|
|
|
import DropdownListItem from '/imports/ui/components/dropdown/list/item/component';
|
2017-07-22 02:17:25 +08:00
|
|
|
import Auth from '/imports/ui/services/auth';
|
|
|
|
import Acl from '/imports/startup/acl';
|
2017-10-17 03:28:27 +08:00
|
|
|
import Button from '/imports/ui/components/button/component';
|
|
|
|
|
2017-07-21 22:16:45 +08:00
|
|
|
import ChatService from './../service';
|
|
|
|
import styles from './styles';
|
|
|
|
|
|
|
|
const intlMessages = defineMessages({
|
|
|
|
clear: {
|
|
|
|
id: 'app.chat.dropdown.clear',
|
|
|
|
description: 'Clear button label',
|
|
|
|
},
|
|
|
|
save: {
|
|
|
|
id: 'app.chat.dropdown.save',
|
|
|
|
description: 'Clear button label',
|
|
|
|
},
|
|
|
|
copy: {
|
|
|
|
id: 'app.chat.dropdown.copy',
|
|
|
|
description: 'Copy button label',
|
|
|
|
},
|
|
|
|
options: {
|
|
|
|
id: 'app.chat.dropdown.options',
|
|
|
|
description: 'Chat Options',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
class ChatDropdown extends Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
isSettingOpen: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.onActionsShow = this.onActionsShow.bind(this);
|
|
|
|
this.onActionsHide = this.onActionsHide.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.clipboard = new Clipboard('#clipboardButton', {
|
2017-08-03 22:10:05 +08:00
|
|
|
text: () => ChatService.exportChat(ChatService.getPublicMessages()),
|
2017-07-21 22:16:45 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.clipboard.destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
onActionsShow() {
|
|
|
|
this.setState({
|
|
|
|
isSettingOpen: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onActionsHide() {
|
|
|
|
this.setState({
|
|
|
|
isSettingOpen: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-22 02:17:25 +08:00
|
|
|
getAvailableActions() {
|
|
|
|
const { intl } = this.props;
|
|
|
|
|
2017-07-28 04:45:40 +08:00
|
|
|
const clearIcon = 'delete';
|
|
|
|
const saveIcon = 'save_notes';
|
|
|
|
const copyIcon = 'copy';
|
|
|
|
|
2017-07-22 02:17:25 +08:00
|
|
|
return _.compact([
|
|
|
|
(<DropdownListItem
|
2017-07-28 04:45:40 +08:00
|
|
|
icon={saveIcon}
|
2017-07-22 02:17:25 +08:00
|
|
|
label={intl.formatMessage(intlMessages.save)}
|
|
|
|
key={_.uniqueId('action-item-')}
|
|
|
|
onClick={() => {
|
|
|
|
const link = document.createElement('a');
|
|
|
|
const mimeType = 'text/plain';
|
|
|
|
|
2017-08-08 22:52:26 +08:00
|
|
|
link.setAttribute('download', `public-chat-${Date.now()}.txt`);
|
|
|
|
link.setAttribute('href', `data: ${mimeType} ;charset=utf-8,
|
2017-08-03 22:10:05 +08:00
|
|
|
${encodeURIComponent(ChatService.exportChat(ChatService.getPublicMessages()))}`);
|
2017-07-22 02:17:25 +08:00
|
|
|
link.click();
|
|
|
|
}}
|
|
|
|
/>),
|
|
|
|
(<DropdownListItem
|
2017-07-28 04:45:40 +08:00
|
|
|
icon={copyIcon}
|
2017-07-22 02:17:25 +08:00
|
|
|
id="clipboardButton"
|
|
|
|
label={intl.formatMessage(intlMessages.copy)}
|
|
|
|
key={_.uniqueId('action-item-')}
|
|
|
|
/>),
|
2017-07-28 04:45:40 +08:00
|
|
|
(Acl.can('methods.clearPublicChatHistory', Auth.credentials) ?
|
|
|
|
<DropdownListItem
|
|
|
|
icon={clearIcon}
|
|
|
|
label={intl.formatMessage(intlMessages.clear)}
|
|
|
|
key={_.uniqueId('action-item-')}
|
2017-08-03 01:05:20 +08:00
|
|
|
onClick={ChatService.clearPublicChatHistory}
|
2017-07-28 04:45:40 +08:00
|
|
|
/>
|
|
|
|
: null),
|
2017-07-22 02:17:25 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-07-21 22:16:45 +08:00
|
|
|
render() {
|
|
|
|
const { intl } = this.props;
|
|
|
|
|
2017-07-22 02:17:25 +08:00
|
|
|
const availableActions = this.getAvailableActions();
|
|
|
|
|
2017-07-21 22:16:45 +08:00
|
|
|
return (
|
2017-10-17 03:28:27 +08:00
|
|
|
|
2017-07-21 22:16:45 +08:00
|
|
|
<Dropdown
|
|
|
|
isOpen={this.state.isSettingOpen}
|
|
|
|
onShow={this.onActionsShow}
|
|
|
|
onHide={this.onActionsHide}
|
|
|
|
>
|
2017-10-17 03:28:27 +08:00
|
|
|
<DropdownTrigger tabIndex={0}>
|
|
|
|
<Button
|
|
|
|
className={styles.btn}
|
|
|
|
icon="more"
|
|
|
|
ghost
|
|
|
|
circle
|
|
|
|
hideLabel
|
|
|
|
color="primary"
|
2017-10-24 22:52:49 +08:00
|
|
|
aria-label={intl.formatMessage(intlMessages.options)}
|
2017-10-17 03:28:27 +08:00
|
|
|
/>
|
2017-07-21 22:16:45 +08:00
|
|
|
</DropdownTrigger>
|
|
|
|
<DropdownContent placement="bottom right">
|
|
|
|
<DropdownList>
|
2017-07-22 02:17:25 +08:00
|
|
|
{availableActions}
|
2017-07-21 22:16:45 +08:00
|
|
|
</DropdownList>
|
|
|
|
</DropdownContent>
|
|
|
|
</Dropdown>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default withModalMounter(injectIntl(ChatDropdown));
|