123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- /**
- * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
- *
- * @author David Sehnal <david.sehnal@gmail.com>
- */
- import { PluginCommands } from 'mol-plugin/command';
- import * as React from 'react';
- import { PluginUIComponent, PurePluginUIComponent } from './base';
- import { shallowEqual } from 'mol-util';
- import { OrderedMap } from 'immutable';
- import { ParameterControls } from './controls/parameters';
- import { ParamDefinition as PD} from 'mol-util/param-definition';
- import { PluginState } from 'mol-plugin/state';
- import { urlCombine } from 'mol-util/url';
- import { IconButton } from './controls/common';
- export class StateSnapshots extends PluginUIComponent<{ }> {
- render() {
- return <div>
- <div className='msp-section-header'>State</div>
- <LocalStateSnapshots />
- <LocalStateSnapshotList />
- <RemoteStateSnapshots />
- </div>;
- }
- }
- class LocalStateSnapshots extends PluginUIComponent<
- { },
- { params: PD.Values<typeof LocalStateSnapshots.Params> }> {
- state = { params: PD.getDefaultValues(LocalStateSnapshots.Params) };
- static Params = {
- name: PD.Text(),
- options: PD.Group({
- description: PD.Text(),
- ...PluginState.GetSnapshotParams
- })
- };
- add = () => {
- PluginCommands.State.Snapshots.Add.dispatch(this.plugin, {
- name: this.state.params.name,
- description: this.state.params.options.description,
- params: this.state.params.options
- });
- this.setState({
- params: {
- name: '',
- options: {
- ...this.state.params.options,
- description: ''
- }
- }
- });
- }
- clear = () => {
- PluginCommands.State.Snapshots.Clear.dispatch(this.plugin, {});
- }
- shouldComponentUpdate(nextProps: any, nextState: any) {
- return !shallowEqual(this.props, nextProps) || !shallowEqual(this.state, nextState);
- }
- downloadToFile = () => {
- PluginCommands.State.Snapshots.DownloadToFile.dispatch(this.plugin, { name: this.state.params.name });
- }
- open = (e: React.ChangeEvent<HTMLInputElement>) => {
- if (!e.target.files || !e.target.files![0]) return;
- PluginCommands.State.Snapshots.OpenFile.dispatch(this.plugin, { file: e.target.files![0] });
- }
- render() {
- // TODO: proper styling
- return <div>
- <div className='msp-btn-row-group' style={{ marginBottom: '10px' }}>
- <button className='msp-btn msp-btn-block msp-form-control' onClick={this.downloadToFile}>Download JSON</button>
- <div className='msp-btn msp-btn-block msp-btn-action msp-loader-msp-btn-file'>
- {'Open JSON'} <input onChange={this.open} type='file' multiple={false} accept='.json' />
- </div>
- </div>
- <ParameterControls params={LocalStateSnapshots.Params} values={this.state.params} onEnter={this.add} onChange={p => {
- const params = { ...this.state.params, [p.name]: p.value };
- this.setState({ params } as any);
- this.plugin.state.snapshots.currentGetSnapshotParams = params.options;
- }}/>
- <div className='msp-btn-row-group'>
- <button className='msp-btn msp-btn-block msp-form-control' onClick={this.add}>Save</button>
- {/* <button className='msp-btn msp-btn-block msp-form-control' onClick={this.upload} disabled={this.state.isUploading}>Upload</button> */}
- <button className='msp-btn msp-btn-block msp-form-control' onClick={this.clear}>Clear</button>
- </div>
- </div>;
- }
- }
- class LocalStateSnapshotList extends PluginUIComponent<{ }, { }> {
- componentDidMount() {
- this.subscribe(this.plugin.events.state.snapshots.changed, () => this.forceUpdate());
- }
- apply = (e: React.MouseEvent<HTMLElement>) => {
- const id = e.currentTarget.getAttribute('data-id');
- if (!id) return;
- PluginCommands.State.Snapshots.Apply.dispatch(this.plugin, { id });
- }
- remove = (e: React.MouseEvent<HTMLElement>) => {
- const id = e.currentTarget.getAttribute('data-id');
- if (!id) return;
- PluginCommands.State.Snapshots.Remove.dispatch(this.plugin, { id });
- }
- moveUp = (e: React.MouseEvent<HTMLElement>) => {
- const id = e.currentTarget.getAttribute('data-id');
- if (!id) return;
- PluginCommands.State.Snapshots.Move.dispatch(this.plugin, { id, dir: -1 });
- }
- moveDown = (e: React.MouseEvent<HTMLElement>) => {
- const id = e.currentTarget.getAttribute('data-id');
- if (!id) return;
- PluginCommands.State.Snapshots.Move.dispatch(this.plugin, { id, dir: 1 });
- }
- replace = (e: React.MouseEvent<HTMLElement>) => {
- const id = e.currentTarget.getAttribute('data-id');
- if (!id) return;
- PluginCommands.State.Snapshots.Replace.dispatch(this.plugin, { id, params: this.plugin.state.snapshots.currentGetSnapshotParams });
- }
- render() {
- const current = this.plugin.state.snapshots.state.current;
- return <ul style={{ listStyle: 'none' }} className='msp-state-list'>
- {this.plugin.state.snapshots.state.entries.map(e =><li key={e!.snapshot.id}>
- <button data-id={e!.snapshot.id} className='msp-btn msp-btn-block msp-form-control' onClick={this.apply}>
- <span style={{ fontWeight: e!.snapshot.id === current ? 'bold' : void 0}}>{e!.name || new Date(e!.timestamp).toLocaleString()}</span> <small>{e!.description}</small>
- </button>
- <div>
- <IconButton data-id={e!.snapshot.id} icon='up-thin' title='Move Up' onClick={this.moveUp} isSmall={true} />
- <IconButton data-id={e!.snapshot.id} icon='down-thin' title='Move Down' onClick={this.moveDown} isSmall={true} />
- <IconButton data-id={e!.snapshot.id} icon='switch' title='Replace' onClick={this.replace} isSmall={true} />
- <IconButton data-id={e!.snapshot.id} icon='remove' title='Remove' onClick={this.remove} isSmall={true} />
- </div>
- </li>)}
- </ul>;
- }
- }
- type RemoteEntry = { url: string, removeUrl: string, timestamp: number, id: string, name: string, description: string }
- class RemoteStateSnapshots extends PluginUIComponent<
- { },
- { params: PD.Values<typeof RemoteStateSnapshots.Params>, entries: OrderedMap<string, RemoteEntry>, isBusy: boolean }> {
- state = { params: PD.getDefaultValues(RemoteStateSnapshots.Params), entries: OrderedMap<string, RemoteEntry>(), isBusy: false };
- static Params = {
- name: PD.Text(),
- options: PD.Group({
- description: PD.Text(),
- serverUrl: PD.Text('https://webchem.ncbr.muni.cz/molstar-state')
- })
- };
- componentDidMount() {
- this.refresh();
- // this.subscribe(UploadedEvent, this.refresh);
- }
- serverUrl(q?: string) {
- if (!q) return this.state.params.options.serverUrl;
- return urlCombine(this.state.params.options.serverUrl, q);
- }
- refresh = async () => {
- try {
- this.setState({ isBusy: true });
- const json = await this.plugin.runTask<RemoteEntry[]>(this.plugin.fetch({ url: this.serverUrl('list'), type: 'json' }));
- const entries = OrderedMap<string, RemoteEntry>().asMutable();
- for (const e of json) {
- entries.set(e.id, {
- ...e,
- url: this.serverUrl(`get/${e.id}`),
- removeUrl: this.serverUrl(`remove/${e.id}`)
- });
- }
- this.setState({ entries: entries.asImmutable(), isBusy: false })
- } catch (e) {
- this.plugin.log.error('Fetching Remote Snapshots: ' + e);
- this.setState({ entries: OrderedMap(), isBusy: false })
- }
- }
- upload = async () => {
- this.setState({ isBusy: true });
- if (this.plugin.state.snapshots.state.entries.size === 0) {
- await PluginCommands.State.Snapshots.Add.dispatch(this.plugin, { name: this.state.params.name, description: this.state.params.options.description });
- }
- await PluginCommands.State.Snapshots.Upload.dispatch(this.plugin, {
- name: this.state.params.name,
- description: this.state.params.options.description,
- serverUrl: this.state.params.options.serverUrl
- });
- this.setState({ isBusy: false });
- this.plugin.log.message('Snapshot uploaded.');
- this.refresh();
- }
- fetch = async (e: React.MouseEvent<HTMLElement>) => {
- const id = e.currentTarget.getAttribute('data-id');
- if (!id) return;
- const entry = this.state.entries.get(id);
- if (!entry) return;
- this.setState({ isBusy: true });
- try {
- await PluginCommands.State.Snapshots.Fetch.dispatch(this.plugin, { url: entry.url });
- } finally {
- this.setState({ isBusy: false });
- }
- }
- remove = async (e: React.MouseEvent<HTMLElement>) => {
- const id = e.currentTarget.getAttribute('data-id');
- if (!id) return;
- const entry = this.state.entries.get(id);
- if (!entry) return;
- this.setState({ entries: this.state.entries.remove(id) });
- try {
- await fetch(entry.removeUrl);
- } catch { }
- }
- render() {
- return <div>
- <div className='msp-section-header'>Remote State</div>
- <ParameterControls params={RemoteStateSnapshots.Params} values={this.state.params} onEnter={this.upload} onChange={p => {
- this.setState({ params: { ...this.state.params, [p.name]: p.value } } as any);
- }} isDisabled={this.state.isBusy}/>
- <div className='msp-btn-row-group'>
- <button className='msp-btn msp-btn-block msp-form-control' onClick={this.upload} disabled={this.state.isBusy}>Upload</button>
- <button className='msp-btn msp-btn-block msp-form-control' onClick={this.refresh} disabled={this.state.isBusy}>Refresh</button>
- </div>
- <RemoteStateSnapshotList entries={this.state.entries} isBusy={this.state.isBusy} serverUrl={this.state.params.options.serverUrl}
- fetch={this.fetch} remove={this.remove} />
- </div>;
- }
- }
- class RemoteStateSnapshotList extends PurePluginUIComponent<
- { entries: OrderedMap<string, RemoteEntry>, serverUrl: string, isBusy: boolean, fetch: (e: React.MouseEvent<HTMLElement>) => void, remove: (e: React.MouseEvent<HTMLElement>) => void },
- { }> {
- open = async (e: React.MouseEvent<HTMLElement>) => {
- const id = e.currentTarget.getAttribute('data-id');
- if (!id) return;
- const entry = this.props.entries.get(id);
- if (!entry) return;
- e.preventDefault();
- let url = `${window.location}`, qi = url.indexOf('?');
- if (qi > 0) url = url.substr(0, qi);
- window.open(`${url}?snapshot-url=${encodeURIComponent(entry.url)}`, '_blank');
- }
- render() {
- return <ul style={{ listStyle: 'none' }} className='msp-state-list'>
- {this.props.entries.valueSeq().map(e =><li key={e!.id}>
- <button data-id={e!.id} className='msp-btn msp-btn-block msp-form-control' onClick={this.props.fetch}
- disabled={this.props.isBusy} onContextMenu={this.open} title='Click to download, right-click to open in a new tab.'>
- {e!.name || new Date(e!.timestamp).toLocaleString()} <small>{e!.description}</small>
- </button>
- <div>
- <IconButton data-id={e!.id} icon='remove' title='Remove' onClick={this.props.remove} disabled={this.props.isBusy} />
- </div>
- </li>)}
- </ul>;
- }
- }
|