1 | // Copyright (c) Jupyter Development Team.
|
2 | // Distributed under the terms of the Modified BSD License.
|
3 | /*-----------------------------------------------------------------------------
|
4 | | Copyright (c) 2014-2017, PhosphorJS Contributors
|
5 | |
|
6 | | Distributed under the terms of the BSD 3-Clause License.
|
7 | |
|
8 | | The full license is in the file LICENSE, distributed with this software.
|
9 | |----------------------------------------------------------------------------*/
|
10 | import { IDisposable } from '@lumino/disposable';
|
11 |
|
12 | import { ISignal, Signal } from '@lumino/signaling';
|
13 |
|
14 | import { VirtualElement } from '@lumino/virtualdom';
|
15 |
|
16 | /**
|
17 | * An object which holds data related to an object's title.
|
18 | *
|
19 | * #### Notes
|
20 | * A title object is intended to hold the data necessary to display a
|
21 | * header for a particular object. A common example is the `TabPanel`,
|
22 | * which uses the widget title to populate the tab for a child widget.
|
23 | *
|
24 | * It is the responsibility of the owner to call the title disposal.
|
25 | */
|
26 | export class Title<T> implements IDisposable {
|
27 | /**
|
28 | * Construct a new title.
|
29 | *
|
30 | * @param options - The options for initializing the title.
|
31 | */
|
32 | constructor(options: Title.IOptions<T>) {
|
33 | this.owner = options.owner;
|
34 | if (options.label !== undefined) {
|
35 | this._label = options.label;
|
36 | }
|
37 | if (options.mnemonic !== undefined) {
|
38 | this._mnemonic = options.mnemonic;
|
39 | }
|
40 | if (options.icon !== undefined) {
|
41 | /* <DEPRECATED> */
|
42 | if (typeof options.icon === 'string') {
|
43 | // when ._icon is null, the .icon getter will alias .iconClass
|
44 | this._icon = null;
|
45 | this._iconClass = options.icon;
|
46 | } else {
|
47 | /* </DEPRECATED> */
|
48 |
|
49 | this._icon = options.icon;
|
50 |
|
51 | /* <DEPRECATED> */
|
52 | }
|
53 | /* </DEPRECATED> */
|
54 | } else {
|
55 | /* <DEPRECATED> */
|
56 | // if unset, default to aliasing .iconClass
|
57 | this._icon = null;
|
58 | }
|
59 | /* </DEPRECATED> */
|
60 |
|
61 | if (options.iconClass !== undefined) {
|
62 | this._iconClass = options.iconClass;
|
63 | }
|
64 | if (options.iconLabel !== undefined) {
|
65 | this._iconLabel = options.iconLabel;
|
66 | }
|
67 | if (options.iconRenderer !== undefined) {
|
68 | this._icon = options.iconRenderer;
|
69 | }
|
70 | if (options.caption !== undefined) {
|
71 | this._caption = options.caption;
|
72 | }
|
73 | if (options.className !== undefined) {
|
74 | this._className = options.className;
|
75 | }
|
76 | if (options.closable !== undefined) {
|
77 | this._closable = options.closable;
|
78 | }
|
79 | this._dataset = options.dataset || {};
|
80 | }
|
81 |
|
82 | /**
|
83 | * A signal emitted when the state of the title changes.
|
84 | */
|
85 | get changed(): ISignal<this, void> {
|
86 | return this._changed;
|
87 | }
|
88 |
|
89 | /**
|
90 | * The object which owns the title.
|
91 | */
|
92 | readonly owner: T;
|
93 |
|
94 | /**
|
95 | * Get the label for the title.
|
96 | *
|
97 | * #### Notes
|
98 | * The default value is an empty string.
|
99 | */
|
100 | get label(): string {
|
101 | return this._label;
|
102 | }
|
103 |
|
104 | /**
|
105 | * Set the label for the title.
|
106 | */
|
107 | set label(value: string) {
|
108 | if (this._label === value) {
|
109 | return;
|
110 | }
|
111 | this._label = value;
|
112 | this._changed.emit(undefined);
|
113 | }
|
114 |
|
115 | /**
|
116 | * Get the mnemonic index for the title.
|
117 | *
|
118 | * #### Notes
|
119 | * The default value is `-1`.
|
120 | */
|
121 | get mnemonic(): number {
|
122 | return this._mnemonic;
|
123 | }
|
124 |
|
125 | /**
|
126 | * Set the mnemonic index for the title.
|
127 | */
|
128 | set mnemonic(value: number) {
|
129 | if (this._mnemonic === value) {
|
130 | return;
|
131 | }
|
132 | this._mnemonic = value;
|
133 | this._changed.emit(undefined);
|
134 | }
|
135 |
|
136 | /**
|
137 | * Get the icon renderer for the title.
|
138 | *
|
139 | * #### Notes
|
140 | * The default value is undefined.
|
141 | *
|
142 | * DEPRECATED: if set to a string value, the .icon field will function as
|
143 | * an alias for the .iconClass field, for backwards compatibility
|
144 | */
|
145 | get icon():
|
146 | | VirtualElement.IRenderer
|
147 | | undefined
|
148 | /* <DEPRECATED> */
|
149 | | string /* </DEPRECATED> */ {
|
150 | /* <DEPRECATED> */
|
151 | if (this._icon === null) {
|
152 | // only alias .iconClass if ._icon has been explicitly nulled
|
153 | return this.iconClass;
|
154 | }
|
155 | /* </DEPRECATED> */
|
156 |
|
157 | return this._icon;
|
158 | }
|
159 |
|
160 | /**
|
161 | * Set the icon renderer for the title.
|
162 | *
|
163 | * #### Notes
|
164 | * A renderer is an object that supplies a render and unrender function.
|
165 | *
|
166 | * DEPRECATED: if set to a string value, the .icon field will function as
|
167 | * an alias for the .iconClass field, for backwards compatibility
|
168 | */
|
169 | set icon(
|
170 | value:
|
171 | | VirtualElement.IRenderer
|
172 | | undefined
|
173 | /* <DEPRECATED> */
|
174 | | string /* </DEPRECATED> */
|
175 | ) {
|
176 | /* <DEPRECATED> */
|
177 | if (typeof value === 'string') {
|
178 | // when ._icon is null, the .icon getter will alias .iconClass
|
179 | this._icon = null;
|
180 | this.iconClass = value;
|
181 | } else {
|
182 | /* </DEPRECATED> */
|
183 |
|
184 | if (this._icon === value) {
|
185 | return;
|
186 | }
|
187 | this._icon = value;
|
188 | this._changed.emit(undefined);
|
189 |
|
190 | /* <DEPRECATED> */
|
191 | }
|
192 | /* </DEPRECATED> */
|
193 | }
|
194 |
|
195 | /**
|
196 | * Get the icon class name for the title.
|
197 | *
|
198 | * #### Notes
|
199 | * The default value is an empty string.
|
200 | */
|
201 | get iconClass(): string {
|
202 | return this._iconClass;
|
203 | }
|
204 |
|
205 | /**
|
206 | * Set the icon class name for the title.
|
207 | *
|
208 | * #### Notes
|
209 | * Multiple class names can be separated with whitespace.
|
210 | */
|
211 | set iconClass(value: string) {
|
212 | if (this._iconClass === value) {
|
213 | return;
|
214 | }
|
215 | this._iconClass = value;
|
216 | this._changed.emit(undefined);
|
217 | }
|
218 |
|
219 | /**
|
220 | * Get the icon label for the title.
|
221 | *
|
222 | * #### Notes
|
223 | * The default value is an empty string.
|
224 | */
|
225 | get iconLabel(): string {
|
226 | return this._iconLabel;
|
227 | }
|
228 |
|
229 | /**
|
230 | * Set the icon label for the title.
|
231 | *
|
232 | * #### Notes
|
233 | * Multiple class names can be separated with whitespace.
|
234 | */
|
235 | set iconLabel(value: string) {
|
236 | if (this._iconLabel === value) {
|
237 | return;
|
238 | }
|
239 | this._iconLabel = value;
|
240 | this._changed.emit(undefined);
|
241 | }
|
242 |
|
243 | /**
|
244 | * @deprecated Use `icon` instead.
|
245 | */
|
246 | get iconRenderer(): VirtualElement.IRenderer | undefined {
|
247 | return this._icon || undefined;
|
248 | }
|
249 |
|
250 | /**
|
251 | * @deprecated Use `icon` instead.
|
252 | */
|
253 | set iconRenderer(value: VirtualElement.IRenderer | undefined) {
|
254 | this.icon = value;
|
255 | }
|
256 |
|
257 | /**
|
258 | * Get the caption for the title.
|
259 | *
|
260 | * #### Notes
|
261 | * The default value is an empty string.
|
262 | */
|
263 | get caption(): string {
|
264 | return this._caption;
|
265 | }
|
266 |
|
267 | /**
|
268 | * Set the caption for the title.
|
269 | */
|
270 | set caption(value: string) {
|
271 | if (this._caption === value) {
|
272 | return;
|
273 | }
|
274 | this._caption = value;
|
275 | this._changed.emit(undefined);
|
276 | }
|
277 |
|
278 | /**
|
279 | * Get the extra class name for the title.
|
280 | *
|
281 | * #### Notes
|
282 | * The default value is an empty string.
|
283 | */
|
284 | get className(): string {
|
285 | return this._className;
|
286 | }
|
287 |
|
288 | /**
|
289 | * Set the extra class name for the title.
|
290 | *
|
291 | * #### Notes
|
292 | * Multiple class names can be separated with whitespace.
|
293 | */
|
294 | set className(value: string) {
|
295 | if (this._className === value) {
|
296 | return;
|
297 | }
|
298 | this._className = value;
|
299 | this._changed.emit(undefined);
|
300 | }
|
301 |
|
302 | /**
|
303 | * Get the closable state for the title.
|
304 | *
|
305 | * #### Notes
|
306 | * The default value is `false`.
|
307 | */
|
308 | get closable(): boolean {
|
309 | return this._closable;
|
310 | }
|
311 |
|
312 | /**
|
313 | * Set the closable state for the title.
|
314 | *
|
315 | * #### Notes
|
316 | * This controls the presence of a close icon when applicable.
|
317 | */
|
318 | set closable(value: boolean) {
|
319 | if (this._closable === value) {
|
320 | return;
|
321 | }
|
322 | this._closable = value;
|
323 | this._changed.emit(undefined);
|
324 | }
|
325 |
|
326 | /**
|
327 | * Get the dataset for the title.
|
328 | *
|
329 | * #### Notes
|
330 | * The default value is an empty dataset.
|
331 | */
|
332 | get dataset(): Title.Dataset {
|
333 | return this._dataset;
|
334 | }
|
335 |
|
336 | /**
|
337 | * Set the dataset for the title.
|
338 | *
|
339 | * #### Notes
|
340 | * This controls the data attributes when applicable.
|
341 | */
|
342 | set dataset(value: Title.Dataset) {
|
343 | if (this._dataset === value) {
|
344 | return;
|
345 | }
|
346 | this._dataset = value;
|
347 | this._changed.emit(undefined);
|
348 | }
|
349 |
|
350 | /**
|
351 | * Test whether the title has been disposed.
|
352 | */
|
353 | get isDisposed(): boolean {
|
354 | return this._isDisposed;
|
355 | }
|
356 |
|
357 | /**
|
358 | * Dispose of the resources held by the title.
|
359 | *
|
360 | * #### Notes
|
361 | * It is the responsibility of the owner to call the title disposal.
|
362 | */
|
363 | dispose(): void {
|
364 | if (this.isDisposed) {
|
365 | return;
|
366 | }
|
367 | this._isDisposed = true;
|
368 |
|
369 | Signal.clearData(this);
|
370 | }
|
371 |
|
372 | private _label = '';
|
373 | private _caption = '';
|
374 | private _mnemonic = -1;
|
375 |
|
376 | private _icon:
|
377 | | VirtualElement.IRenderer
|
378 | | undefined
|
379 | /* <DEPRECATED> */
|
380 | | null /* </DEPRECATED> */;
|
381 |
|
382 | private _iconClass = '';
|
383 | private _iconLabel = '';
|
384 | private _className = '';
|
385 | private _closable = false;
|
386 | private _dataset: Title.Dataset;
|
387 | private _changed = new Signal<this, void>(this);
|
388 | private _isDisposed = false;
|
389 | }
|
390 |
|
391 | /**
|
392 | * The namespace for the `Title` class statics.
|
393 | */
|
394 | export namespace Title {
|
395 | /**
|
396 | * A type alias for a simple immutable string dataset.
|
397 | */
|
398 | export type Dataset = { readonly [key: string]: string };
|
399 |
|
400 | /**
|
401 | * An options object for initializing a title.
|
402 | */
|
403 | export interface IOptions<T> {
|
404 | /**
|
405 | * The object which owns the title.
|
406 | */
|
407 | owner: T;
|
408 |
|
409 | /**
|
410 | * The label for the title.
|
411 | */
|
412 | label?: string;
|
413 |
|
414 | /**
|
415 | * The mnemonic index for the title.
|
416 | */
|
417 | mnemonic?: number;
|
418 |
|
419 | /**
|
420 | * The icon renderer for the title.
|
421 | *
|
422 | * DEPRECATED: if set to a string value, the .icon field will function as
|
423 | * an alias for the .iconClass field, for backwards compatibility
|
424 | */
|
425 | icon?: VirtualElement.IRenderer | string;
|
426 |
|
427 | /**
|
428 | * The icon class name for the title.
|
429 | */
|
430 | iconClass?: string;
|
431 |
|
432 | /**
|
433 | * The icon label for the title.
|
434 | */
|
435 | iconLabel?: string;
|
436 |
|
437 | /**
|
438 | * @deprecated Use `icon` instead.
|
439 | */
|
440 | iconRenderer?: VirtualElement.IRenderer;
|
441 |
|
442 | /**
|
443 | * The caption for the title.
|
444 | */
|
445 | caption?: string;
|
446 |
|
447 | /**
|
448 | * The extra class name for the title.
|
449 | */
|
450 | className?: string;
|
451 |
|
452 | /**
|
453 | * The closable state for the title.
|
454 | */
|
455 | closable?: boolean;
|
456 |
|
457 | /**
|
458 | * The dataset for the title.
|
459 | */
|
460 | dataset?: Dataset;
|
461 | }
|
462 | }
|