toast.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /**
  2. * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * Adapted from LiteMol (c) David Sehnal
  5. *
  6. * @author David Sehnal <david.sehnal@gmail.com>
  7. */
  8. import { PluginUIComponent } from './base';
  9. import { PluginToastManager } from '../mol-plugin/util/toast';
  10. import { IconButton } from './controls/common';
  11. import { CancelSvg } from './controls/icons';
  12. class ToastEntry extends PluginUIComponent<{ entry: PluginToastManager.Entry }> {
  13. private hide = () => {
  14. const entry = this.props.entry;
  15. (entry.hide || function () { }).call(null);
  16. };
  17. render() {
  18. const entry = this.props.entry;
  19. const message = typeof entry.message === 'string'
  20. ? <div dangerouslySetInnerHTML={{ __html: entry.message }} />
  21. : <div><entry.message /></div>;
  22. return <div className='msp-toast-entry'>
  23. <div className='msp-toast-title' onClick={() => this.hide()}>
  24. {entry.title}
  25. </div>
  26. <div className='msp-toast-message'>
  27. {message}
  28. </div>
  29. <div className='msp-toast-clear'></div>
  30. <div className='msp-toast-hide'>
  31. <IconButton svg={CancelSvg} onClick={this.hide} title='Hide' className='msp-no-hover-outline' />
  32. </div>
  33. </div>;
  34. }
  35. }
  36. export class Toasts extends PluginUIComponent {
  37. componentDidMount() {
  38. this.subscribe(this.plugin.managers.toast.events.changed, () => this.forceUpdate());
  39. }
  40. render() {
  41. const state = this.plugin.managers.toast.state;
  42. if (!state.entries.count()) return null;
  43. const entries: PluginToastManager.Entry[] = [];
  44. state.entries.forEach((t, k) => entries.push(t!));
  45. entries.sort(function (x, y) { return x.serialNumber - y.serialNumber; });
  46. return <div className='msp-toast-container'>
  47. {entries.map(e => <ToastEntry key={e.serialNumber} entry={e} />)}
  48. </div>;
  49. }
  50. }