/
toolbar.ts
142 lines (123 loc) · 3.45 KB
/
toolbar.ts
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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { each, zip } from '@phosphor/algorithm';
import { Message } from '@phosphor/messaging';
import { ISignal, Signal } from '@phosphor/signaling';
import { Widget } from '@phosphor/widgets';
import { Styling } from '@jupyterlab/apputils';
/**
* The supported parsing delimiters.
*/
const DELIMITERS = [',', ';', '\t'];
/**
* The labels for each delimiter as they appear in the dropdown menu.
*/
const LABELS = [',', ';', 'tab'];
/**
* The class name added to a csv toolbar widget.
*/
const CSV_DELIMITER_CLASS = 'jp-CSVDelimiter';
const CSV_DELIMITER_LABEL_CLASS = 'jp-CSVDelimiter-label';
/**
* The class name added to a csv toolbar's dropdown element.
*/
const CSV_DELIMITER_DROPDOWN_CLASS = 'jp-CSVDelimiter-dropdown';
/**
* A widget for selecting a delimiter.
*/
export class CSVDelimiter extends Widget {
/**
* Construct a new csv table widget.
*/
constructor(options: CSVToolbar.IOptions) {
super({ node: Private.createNode(options.selected) });
this.addClass(CSV_DELIMITER_CLASS);
}
/**
* A signal emitted when the delimiter selection has changed.
*/
get delimiterChanged(): ISignal<this, string> {
return this._delimiterChanged;
}
/**
* The delimiter dropdown menu.
*/
get selectNode(): HTMLSelectElement {
return this.node.getElementsByTagName('select')![0];
}
/**
* Handle the DOM events for the widget.
*
* @param event - The DOM event sent to the widget.
*
* #### Notes
* This method implements the DOM `EventListener` interface and is
* called in response to events on the dock panel's node. It should
* not be called directly by user code.
*/
handleEvent(event: Event): void {
switch (event.type) {
case 'change':
this._delimiterChanged.emit(this.selectNode.value);
break;
default:
break;
}
}
/**
* Handle `after-attach` messages for the widget.
*/
protected onAfterAttach(msg: Message): void {
this.selectNode.addEventListener('change', this);
}
/**
* Handle `before-detach` messages for the widget.
*/
protected onBeforeDetach(msg: Message): void {
this.selectNode.removeEventListener('change', this);
}
private _delimiterChanged = new Signal<this, string>(this);
}
/**
* A namespace for `CSVToolbar` statics.
*/
export namespace CSVToolbar {
/**
* The instantiation options for a CSV toolbar.
*/
export interface IOptions {
/**
* The initially selected delimiter.
*/
selected: string;
}
}
/**
* A namespace for private toolbar methods.
*/
namespace Private {
/**
* Create the node for the delimiter switcher.
*/
export function createNode(selected: string): HTMLElement {
let div = document.createElement('div');
let label = document.createElement('span');
let select = document.createElement('select');
label.textContent = 'Delimiter: ';
label.className = CSV_DELIMITER_LABEL_CLASS;
each(zip(DELIMITERS, LABELS), ([delimiter, label]) => {
let option = document.createElement('option');
option.value = delimiter;
option.textContent = label;
if (delimiter === selected) {
option.selected = true;
}
select.appendChild(option);
});
div.appendChild(label);
let node = Styling.wrapSelect(select);
node.classList.add(CSV_DELIMITER_DROPDOWN_CLASS);
div.appendChild(node);
return div;
}
}