/
widget.ts
151 lines (138 loc) · 3.57 KB
/
widget.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
import { Widget } from '@lumino/widgets';
import { checkIcon } from '@jupyterlab/ui-components';
import { TagTool } from './tool';
/**
* A widget which hosts a cell tags area.
*/
export class TagWidget extends Widget {
/**
* Construct a new tag widget.
*/
constructor(name: string) {
super();
this.applied = true;
this.name = name;
this.addClass('tag');
this.buildTag();
}
/**
* Create tag div with icon and attach to this.node.
*/
buildTag() {
let text = document.createElement('span');
text.textContent = this.name;
text.style.textOverflow = 'ellipsis';
let tag = document.createElement('div');
tag.className = 'tag-holder';
tag.appendChild(text);
let iconContainer = checkIcon.element({
tag: 'span',
center: true,
height: '18px',
width: '18px',
marginLeft: '5px',
marginRight: '-3px'
});
if (this.applied) {
this.addClass('applied-tag');
} else {
this.addClass('unapplied-tag');
iconContainer.style.display = 'none';
}
tag.appendChild(iconContainer);
this.node.appendChild(tag);
}
/**
* Handle `after-attach` messages for the widget.
*/
onAfterAttach() {
this.node.addEventListener('mousedown', this);
this.node.addEventListener('mouseover', this);
this.node.addEventListener('mouseout', this);
}
/**
* Handle `before-detach` messages for the widget.
*/
onBeforeDetach() {
this.node.removeEventListener('mousedown', this);
this.node.removeEventListener('mouseover', this);
this.node.removeEventListener('mouseout', 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._evtClick();
break;
case 'mouseover':
this._evtMouseOver();
break;
case 'mouseout':
this._evtMouseOut();
break;
default:
break;
}
}
/**
* Handle `update-request` messages. Check if applied to current active cell.
*/
onUpdateRequest() {
let applied = this.parent.checkApplied(this.name);
if (applied !== this.applied) {
this.toggleApplied();
}
}
/**
* Update styling to reflect whether tag is applied to current active cell.
*/
toggleApplied() {
if (this.applied) {
this.removeClass('applied-tag');
(this.node.firstChild.lastChild as HTMLSpanElement).style.display =
'none';
this.addClass('unapplied-tag');
} else {
this.removeClass('unapplied-tag');
(this.node.firstChild.lastChild as HTMLSpanElement).style.display =
'inline-block';
this.addClass('applied-tag');
}
this.applied = !this.applied;
}
/**
* Handle the `'click'` event for the widget.
*/
private _evtClick() {
if (this.applied) {
this.parent.removeTag(this.name);
} else {
this.parent.addTag(this.name);
}
this.toggleApplied();
}
/**
* Handle the `'mouseover'` event for the widget.
*/
private _evtMouseOver() {
(this.node as HTMLElement).classList.add('tag-hover');
}
/**
* Handle the `'mouseout'` event for the widget.
*/
private _evtMouseOut() {
(this.node as HTMLElement).classList.remove('tag-hover');
}
public name: string;
private applied: boolean;
public parent: TagTool;
}