/
addwidget.ts
169 lines (159 loc) · 4.38 KB
/
addwidget.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import {
ITranslator,
nullTranslator,
TranslationBundle
} from '@jupyterlab/translation';
import { addIcon } from '@jupyterlab/ui-components';
import { Widget } from '@lumino/widgets';
import { TagTool } from './tool';
/**
* A widget which hosts a cell tags area.
*/
export class AddWidget extends Widget {
/**
* Construct a new tag widget.
*/
constructor(translator?: ITranslator) {
super();
this.translator = translator || nullTranslator;
this._trans = this.translator.load('jupyterlab');
this.addClass('tag');
this.editing = false;
this.buildTag();
}
/**
* Create input box with icon and attach to this.node.
*/
buildTag() {
const text = this.input || document.createElement('input');
text.value = this._trans.__('Add Tag');
text.contentEditable = 'true';
text.className = 'add-tag';
text.style.width = '49px';
this.input = text;
const tag = document.createElement('div');
tag.className = 'tag-holder';
tag.appendChild(text);
const iconContainer = addIcon.element({
tag: 'span',
elementPosition: 'center',
height: '18px',
width: '18px',
marginLeft: '3px',
marginRight: '-5px'
});
this.addClass('unapplied-tag');
tag.appendChild(iconContainer);
this.node.appendChild(tag);
}
/**
* Handle `after-attach` messages for the widget.
*/
onAfterAttach() {
this.node.addEventListener('mousedown', this);
this.input.addEventListener('keydown', this);
this.input.addEventListener('focus', this);
this.input.addEventListener('blur', this);
}
/**
* Handle `before-detach` messages for the widget.
*/
onBeforeDetach() {
this.node.removeEventListener('mousedown', this);
this.input.removeEventListener('keydown', this);
this.input.removeEventListener('focus', this);
this.input.removeEventListener('blur', this);
}
/**
* 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 'mousedown':
this._evtMouseDown(event as MouseEvent);
break;
case 'keydown':
this._evtKeyDown(event as KeyboardEvent);
break;
case 'blur':
this._evtBlur();
break;
case 'focus':
this._evtFocus();
break;
default:
break;
}
}
/**
* Handle the `'mousedown'` event for the input box.
*
* @param event - The DOM event sent to the widget
*/
private _evtMouseDown(event: MouseEvent) {
if (!this.editing) {
this.editing = true;
this.input.value = '';
this.input.focus();
} else if (event.target !== this.input) {
if (this.input.value !== '') {
const value = this.input.value;
(this.parent as TagTool).addTag(value);
this.input.blur();
this._evtBlur();
}
}
event.preventDefault();
}
/**
* Handle the `'focus'` event for the input box.
*/
private _evtFocus() {
if (!this.editing) {
this.input.blur();
}
}
/**
* Handle the `'keydown'` event for the input box.
*
* @param event - The DOM event sent to the widget
*/
private _evtKeyDown(event: KeyboardEvent) {
const tmp = document.createElement('span');
tmp.className = 'add-tag';
tmp.innerHTML = this.input.value;
// set width to the pixel length of the text
document.body.appendChild(tmp);
this.input.style.width = tmp.getBoundingClientRect().width + 8 + 'px';
document.body.removeChild(tmp);
// if they hit Enter, add the tag and reset state
if (event.keyCode === 13) {
const value = this.input.value;
(this.parent as TagTool).addTag(value);
this.input.blur();
this._evtBlur();
}
}
/**
* Handle the `'focusout'` event for the input box.
*/
private _evtBlur() {
if (this.editing) {
this.editing = false;
this.input.value = this._trans.__('Add Tag');
this.input.style.width = '49px';
}
}
public parent: TagTool | null = null;
private editing: boolean;
private input: HTMLInputElement = document.createElement('input');
protected translator: ITranslator;
private _trans: TranslationBundle;
}