forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tableeditor.tsx
151 lines (133 loc) · 4.07 KB
/
tableeditor.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*-----------------------------------------------------------------------------
| Copyright (c) Jupyter Development Team.
| Distributed under the terms of the Modified BSD License.
|----------------------------------------------------------------------------*/
import { ISettingRegistry } from '@jupyterlab/coreutils';
import { Message } from '@phosphor/messaging';
import { Widget } from '@phosphor/widgets';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
/**
* A tabular editor for plugin settings.
*/
export class TableEditor extends Widget {
/**
* Create a new table editor for settings.
*/
constructor(options: TableEditor.IOptions) {
super({ node: document.createElement('fieldset') });
this.addClass('jp-SettingsTableEditor');
}
/**
* Tests whether the settings have been modified and need saving.
*/
get isDirty(): boolean {
return false; // TODO: remove placeholder.
}
/**
* The plugin settings.
*/
get settings(): ISettingRegistry.ISettings | null {
return this._settings;
}
set settings(settings: ISettingRegistry.ISettings | null) {
if (this._settings) {
this._settings.changed.disconnect(this._onSettingsChanged, this);
}
this._settings = settings;
if (this._settings) {
this._settings.changed.connect(this._onSettingsChanged, this);
}
this.update();
}
/**
* Handle `'update-request'` messages.
*/
protected onUpdateRequest(msg: Message): void {
const settings = this._settings;
// Populate if possible.
if (settings) {
Private.populateTable(this.node, settings);
}
}
/**
* Handle setting changes.
*/
private _onSettingsChanged(): void {
this.update();
}
private _settings: ISettingRegistry.ISettings | null = null;
}
/**
* A namespace for `TableEditor` statics.
*/
export namespace TableEditor {
/**
* The instantiation options for a table editor.
*/
export interface IOptions {
/**
* A function the table editor calls on save errors.
*/
onSaveError: (reason: any) => void;
}
}
/**
* A namespace for private module data.
*/
namespace Private {
/**
* Populate the fieldset with a specific plugin's metadata.
*/
export function populateTable(
node: HTMLElement,
settings: ISettingRegistry.ISettings
): void {
const { plugin, schema } = settings;
const fields: { [property: string]: React.ReactElement<any> } = {};
const properties = schema.properties || {};
const title = `(${plugin}) ${schema.description}`;
const label = `Fields - ${schema.title || plugin}`;
Object.keys(properties).forEach(property => {
const field = properties[property];
const { type } = field;
const defaultValue = settings.default(property);
const title = field.title || property;
const value = JSON.stringify(defaultValue) || '';
const valueTitle = JSON.stringify(defaultValue, null, 4);
fields[property] = (
<tr key={property}>
<td className="jp-SettingsTableEditor-key" title={title}>
<code title={title}>{property}</code>
</td>
<td className="jp-SettingsTableEditor-value" title={valueTitle}>
<code title={valueTitle}>{value}</code>
</td>
<td className="jp-SettingsTableEditor-type">{type}</td>
</tr>
);
});
const rows = Object.keys(fields)
.sort((a, b) => a.localeCompare(b))
.map(property => fields[property]);
const fragment = (
<React.Fragment>
<legend title={title}>{label}</legend>
<div className="jp-SettingsTableEditor-wrapper">
<table>
<thead>
<tr>
<th className="jp-SettingsTableEditor-key">Key</th>
<th className="jp-SettingsTableEditor-value">Default</th>
<th className="jp-SettingsTableEditor-type">Type</th>
</tr>
</thead>
<tbody>{rows}</tbody>
</table>
</div>
</React.Fragment>
);
ReactDOM.unmountComponentAtNode(node);
ReactDOM.render(fragment, node);
}
}