Debian Codegraph @

jupyter-widgets-controls/lib/widget_string.js

raw
1 // Copyright (c) Jupyter Development Team.
2 // Distributed under the terms of the Modified BSD License.
3 import { CoreDescriptionModel } from './widget_core';
4 import { DescriptionStyleModel, DescriptionView } from './widget_description';
5 import { uuid } from './utils';
6 import { JUPYTER_CONTROLS_VERSION } from './version';
7 /**
8 * Class name for a combobox with an invalid value.
9 */
10 const INVALID_VALUE_CLASS = 'jpwidgets-invalidComboValue';
11 class StringStyleModel extends DescriptionStyleModel {
12 defaults() {
13 return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'StringStyleModel', _model_module: '@jupyter-widgets/controls', _model_module_version: JUPYTER_CONTROLS_VERSION });
14 }
15 }
16 StringStyleModel.styleProperties = Object.assign(Object.assign({}, DescriptionStyleModel.styleProperties), { background: {
17 selector: '',
18 attribute: 'background',
19 default: null,
20 }, font_size: {
21 selector: '',
22 attribute: 'font-size',
23 default: '',
24 }, text_color: {
25 selector: '',
26 attribute: 'color',
27 default: '',
28 } });
29 export class HTMLStyleModel extends StringStyleModel {
30 defaults() {
31 return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'HTMLStyleModel', _model_module: '@jupyter-widgets/controls', _model_module_version: JUPYTER_CONTROLS_VERSION });
32 }
33 }
34 HTMLStyleModel.styleProperties = Object.assign({}, StringStyleModel.styleProperties);
35 export class HTMLMathStyleModel extends StringStyleModel {
36 defaults() {
37 return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'HTMLMathStyleModel', _model_module: '@jupyter-widgets/controls', _model_module_version: JUPYTER_CONTROLS_VERSION });
38 }
39 }
40 HTMLMathStyleModel.styleProperties = Object.assign({}, StringStyleModel.styleProperties);
41 export class LabelStyleModel extends StringStyleModel {
42 defaults() {
43 return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'LabelStyleModel', _model_module: '@jupyter-widgets/controls', _model_module_version: JUPYTER_CONTROLS_VERSION });
44 }
45 }
46 LabelStyleModel.styleProperties = Object.assign(Object.assign({}, StringStyleModel.styleProperties), { font_family: {
47 selector: '',
48 attribute: 'font-family',
49 default: '',
50 }, font_style: {
51 selector: '',
52 attribute: 'font-style',
53 default: '',
54 }, font_variant: {
55 selector: '',
56 attribute: 'font-variant',
57 default: '',
58 }, font_weight: {
59 selector: '',
60 attribute: 'font-weight',
61 default: '',
62 }, text_decoration: {
63 selector: '',
64 attribute: 'text-decoration',
65 default: '',
66 } });
67 export class TextStyleModel extends DescriptionStyleModel {
68 defaults() {
69 return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'TextStyleModel', _model_module: '@jupyter-widgets/controls', _model_module_version: JUPYTER_CONTROLS_VERSION });
70 }
71 }
72 TextStyleModel.styleProperties = Object.assign(Object.assign({}, DescriptionStyleModel.styleProperties), { background: {
73 selector: '.widget-input',
74 attribute: 'background',
75 default: null,
76 }, font_size: {
77 selector: '.widget-input',
78 attribute: 'font-size',
79 default: '',
80 }, text_color: {
81 selector: '.widget-input',
82 attribute: 'color',
83 default: '',
84 } });
85 export class StringModel extends CoreDescriptionModel {
86 defaults() {
87 return Object.assign(Object.assign({}, super.defaults()), { value: '', disabled: false, placeholder: '\u200b', _model_name: 'StringModel' });
88 }
89 }
90 export class StringView extends DescriptionView {
91 /**
92 * Called when view is rendered.
93 */
94 render() {
95 super.render(); // Incl. setting some defaults.
96 this.el.classList.add('jupyter-widgets');
97 this.el.classList.add('widget-inline-hbox');
98 }
99 }
100 export class HTMLModel extends StringModel {
101 defaults() {
102 return Object.assign(Object.assign({}, super.defaults()), { _view_name: 'HTMLView', _model_name: 'HTMLModel' });
103 }
104 }
105 export class HTMLView extends StringView {
106 /**
107 * Called when view is rendered.
108 */
109 render() {
110 super.render();
111 this.el.classList.add('widget-html');
112 this.content = document.createElement('div');
113 this.content.classList.add('widget-html-content');
114 this.el.appendChild(this.content);
115 this.update(); // Set defaults.
116 }
117 /**
118 * Update the contents of this view
119 *
120 * Called when the model is changed. The model may have been
121 * changed by another view or by a state update from the back-end.
122 */
123 update() {
124 this.content.innerHTML = this.model.get('value');
125 return super.update();
126 }
127 /**
128 * Handle message sent to the front end.
129 */
130 handle_message(content) {
131 if (content.do === 'focus') {
132 this.content.focus();
133 }
134 else if (content.do === 'blur') {
135 this.content.blur();
136 }
137 }
138 }
139 export class HTMLMathModel extends StringModel {
140 defaults() {
141 return Object.assign(Object.assign({}, super.defaults()), { _view_name: 'HTMLMathView', _model_name: 'HTMLMathModel' });
142 }
143 }
144 export class HTMLMathView extends StringView {
145 /**
146 * Called when view is rendered.
147 */
148 render() {
149 super.render();
150 this.el.classList.add('widget-htmlmath');
151 this.content = document.createElement('div');
152 this.content.classList.add('widget-htmlmath-content');
153 this.el.appendChild(this.content);
154 this.update(); // Set defaults.
155 }
156 /**
157 * Update the contents of this view
158 */
159 update() {
160 this.content.innerHTML = this.model.get('value');
161 this.typeset(this.content);
162 return super.update();
163 }
164 /**
165 * Handle message sent to the front end.
166 */
167 handle_message(content) {
168 if (content.do === 'focus') {
169 this.content.focus();
170 }
171 else if (content.do === 'blur') {
172 this.content.blur();
173 }
174 }
175 }
176 export class LabelModel extends StringModel {
177 defaults() {
178 return Object.assign(Object.assign({}, super.defaults()), { _view_name: 'LabelView', _model_name: 'LabelModel' });
179 }
180 }
181 export class LabelView extends StringView {
182 /**
183 * Called when view is rendered.
184 */
185 render() {
186 super.render();
187 this.el.classList.add('widget-label');
188 this.update(); // Set defaults.
189 }
190 /**
191 * Update the contents of this view
192 *
193 * Called when the model is changed. The model may have been
194 * changed by another view or by a state update from the back-end.
195 */
196 update() {
197 this.typeset(this.el, this.model.get('value'));
198 return super.update();
199 }
200 }
201 export class TextareaModel extends StringModel {
202 defaults() {
203 return Object.assign(Object.assign({}, super.defaults()), { _view_name: 'TextareaView', _model_name: 'TextareaModel', rows: null, continuous_update: true });
204 }
205 }
206 export class TextareaView extends StringView {
207 /**
208 * Called when view is rendered.
209 */
210 render() {
211 super.render();
212 this.el.classList.add('widget-textarea');
213 this.textbox = document.createElement('textarea');
214 this.textbox.setAttribute('rows', '5');
215 this.textbox.id = this.label.htmlFor = uuid();
216 this.textbox.classList.add('widget-input');
217 this.el.appendChild(this.textbox);
218 this.update(); // Set defaults.
219 this.listenTo(this.model, 'change:placeholder', (model, value, options) => {
220 this.update_placeholder(value);
221 });
222 this.update_placeholder();
223 this.updateTooltip();
224 }
225 update_placeholder(value) {
226 const v = value || this.model.get('placeholder');
227 this.textbox.setAttribute('placeholder', v.toString());
228 }
229 /**
230 * Update the contents of this view
231 *
232 * Called when the model is changed. The model may have been
233 * changed by another view or by a state update from the back-end.
234 */
235 update(options) {
236 if (options === undefined || options.updated_view !== this) {
237 this.textbox.value = this.model.get('value');
238 let rows = this.model.get('rows');
239 if (rows === null) {
240 rows = '';
241 }
242 this.textbox.setAttribute('rows', rows);
243 this.textbox.disabled = this.model.get('disabled');
244 }
245 this.updateTabindex();
246 this.updateTooltip();
247 return super.update();
248 }
249 updateTabindex() {
250 if (!this.textbox) {
251 return; // we might be constructing the parent
252 }
253 const tabbable = this.model.get('tabbable');
254 if (tabbable === true) {
255 this.textbox.setAttribute('tabIndex', '0');
256 }
257 else if (tabbable === false) {
258 this.textbox.setAttribute('tabIndex', '-1');
259 }
260 else if (tabbable === null) {
261 this.textbox.removeAttribute('tabIndex');
262 }
263 }
264 updateTooltip() {
265 if (!this.textbox)
266 return; // we might be constructing the parent
267 const title = this.model.get('tooltip');
268 if (!title) {
269 this.textbox.removeAttribute('title');
270 }
271 else if (this.model.get('description').length === 0) {
272 this.textbox.setAttribute('title', title);
273 }
274 }
275 events() {
276 return {
277 'keydown input': 'handleKeyDown',
278 'keypress input': 'handleKeypress',
279 'input textarea': 'handleChanging',
280 'change textarea': 'handleChanged',
281 };
282 }
283 /**
284 * Handle key down
285 *
286 * Stop propagation so the event isn't sent to the application.
287 */
288 handleKeyDown(e) {
289 e.stopPropagation();
290 }
291 /**
292 * Handles key press
293 *
294 * Stop propagation so the keypress isn't sent to the application.
295 */
296 handleKeypress(e) {
297 e.stopPropagation();
298 }
299 /**
300 * Triggered on input change
301 */
302 handleChanging(e) {
303 if (this.model.get('continuous_update')) {
304 this.handleChanged(e);
305 }
306 }
307 /**
308 * Sync the value with the kernel.
309 *
310 * @param e Event
311 */
312 handleChanged(e) {
313 const target = e.target;
314 this.model.set('value', target.value, { updated_view: this });
315 this.touch();
316 }
317 /**
318 * Handle message sent to the front end.
319 */
320 handle_message(content) {
321 if (content.do === 'focus') {
322 this.textbox.focus();
323 }
324 else if (content.do === 'blur') {
325 this.textbox.blur();
326 }
327 }
328 }
329 export class TextModel extends StringModel {
330 defaults() {
331 return Object.assign(Object.assign({}, super.defaults()), { _view_name: 'TextView', _model_name: 'TextModel', continuous_update: true });
332 }
333 }
334 export class TextView extends StringView {
335 constructor() {
336 super(...arguments);
337 this.inputType = 'text';
338 }
339 /**
340 * Called when view is rendered.
341 */
342 render() {
343 super.render();
344 this.el.classList.add('widget-text');
345 this.textbox = document.createElement('input');
346 this.textbox.setAttribute('type', this.inputType);
347 this.textbox.id = this.label.htmlFor = uuid();
348 this.textbox.classList.add('widget-input');
349 this.el.appendChild(this.textbox);
350 this.update(); // Set defaults.
351 this.listenTo(this.model, 'change:placeholder', (model, value, options) => {
352 this.update_placeholder(value);
353 });
354 this.update_placeholder();
355 this.updateTabindex();
356 this.updateTooltip();
357 }
358 update_placeholder(value) {
359 this.textbox.setAttribute('placeholder', value || this.model.get('placeholder'));
360 }
361 updateTabindex() {
362 if (!this.textbox) {
363 return; // we might be constructing the parent
364 }
365 const tabbable = this.model.get('tabbable');
366 if (tabbable === true) {
367 this.textbox.setAttribute('tabIndex', '0');
368 }
369 else if (tabbable === false) {
370 this.textbox.setAttribute('tabIndex', '-1');
371 }
372 else if (tabbable === null) {
373 this.textbox.removeAttribute('tabIndex');
374 }
375 }
376 updateTooltip() {
377 if (!this.textbox)
378 return; // we might be constructing the parent
379 const title = this.model.get('tooltip');
380 if (!title) {
381 this.textbox.removeAttribute('title');
382 }
383 else if (this.model.get('description').length === 0) {
384 this.textbox.setAttribute('title', title);
385 }
386 }
387 update(options) {
388 /**
389 * Update the contents of this view
390 *
391 * Called when the model is changed. The model may have been
392 * changed by another view or by a state update from the back-end.
393 */
394 if (options === undefined || options.updated_view !== this) {
395 if (this.textbox.value !== this.model.get('value')) {
396 this.textbox.value = this.model.get('value');
397 }
398 this.textbox.disabled = this.model.get('disabled');
399 }
400 return super.update();
401 }
402 events() {
403 return {
404 'keydown input': 'handleKeyDown',
405 'keypress input': 'handleKeypress',
406 'input input': 'handleChanging',
407 'change input': 'handleChanged',
408 };
409 }
410 /**
411 * Handle key down
412 *
413 * Stop propagation so the keypress isn't sent to the application.
414 */
415 handleKeyDown(e) {
416 e.stopPropagation();
417 }
418 /**
419 * Handles text submission
420 */
421 handleKeypress(e) {
422 e.stopPropagation();
423 // The submit message is deprecated in widgets 7
424 if (e.keyCode === 13) {
425 // Return key
426 this.send({ event: 'submit' });
427 }
428 }
429 /**
430 * Handles user input.
431 *
432 * Calling model.set will trigger all of the other views of the
433 * model to update.
434 */
435 handleChanging(e) {
436 if (this.model.get('continuous_update')) {
437 this.handleChanged(e);
438 }
439 }
440 /**
441 * Handles user input.
442 *
443 * Calling model.set will trigger all of the other views of the
444 * model to update.
445 */
446 handleChanged(e) {
447 const target = e.target;
448 this.model.set('value', target.value, { updated_view: this });
449 this.touch();
450 }
451 /**
452 * Handle message sent to the front end.
453 */
454 handle_message(content) {
455 if (content.do === 'focus') {
456 this.textbox.focus();
457 }
458 else if (content.do === 'blur') {
459 this.textbox.blur();
460 }
461 }
462 }
463 export class PasswordModel extends TextModel {
464 defaults() {
465 return Object.assign(Object.assign({}, super.defaults()), { _view_name: 'PasswordView', _model_name: 'PasswordModel' });
466 }
467 }
468 export class PasswordView extends TextView {
469 constructor() {
470 super(...arguments);
471 this.inputType = 'password';
472 }
473 }
474 /**
475 * Combobox widget model class.
476 */
477 export class ComboboxModel extends TextModel {
478 defaults() {
479 return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'ComboboxModel', _view_name: 'ComboboxView', options: [], ensure_options: false });
480 }
481 }
482 /**
483 * Combobox widget view class.
484 */
485 export class ComboboxView extends TextView {
486 constructor() {
487 super(...arguments);
488 this.isInitialRender = true;
489 }
490 render() {
491 this.datalist = document.createElement('datalist');
492 this.datalist.id = uuid();
493 super.render();
494 this.textbox.setAttribute('list', this.datalist.id);
495 this.el.appendChild(this.datalist);
496 this.updateTooltip();
497 }
498 update(options) {
499 super.update(options);
500 if (!this.datalist) {
501 return;
502 }
503 const valid = this.isValid(this.model.get('value'));
504 this.highlightValidState(valid);
505 // Check if we need to update options
506 if ((options !== undefined && options.updated_view) ||
507 (!this.model.hasChanged('options') && !this.isInitialRender)) {
508 // Value update only, keep current options
509 return;
510 }
511 this.isInitialRender = false;
512 const opts = this.model.get('options');
513 const optionFragment = document.createDocumentFragment();
514 for (const v of opts) {
515 const o = document.createElement('option');
516 o.value = v;
517 optionFragment.appendChild(o);
518 }
519 this.datalist.replaceChildren(...optionFragment.children);
520 }
521 isValid(value) {
522 if (true === this.model.get('ensure_option')) {
523 const options = this.model.get('options');
524 if (options.indexOf(value) === -1) {
525 return false;
526 }
527 }
528 return true;
529 }
530 handleChanging(e) {
531 // Override to validate value
532 const target = e.target;
533 const valid = this.isValid(target.value);
534 this.highlightValidState(valid);
535 if (valid) {
536 super.handleChanging(e);
537 }
538 }
539 handleChanged(e) {
540 // Override to validate value
541 const target = e.target;
542 const valid = this.isValid(target.value);
543 this.highlightValidState(valid);
544 if (valid) {
545 super.handleChanged(e);
546 }
547 }
548 /**
549 * Handle message sent to the front end.
550 */
551 handle_message(content) {
552 if (content.do === 'focus') {
553 this.textbox.focus();
554 }
555 else if (content.do === 'blur') {
556 this.textbox.blur();
557 }
558 }
559 highlightValidState(valid) {
560 this.textbox.classList.toggle(INVALID_VALUE_CLASS, !valid);
561 }
562 }
563 //# sourceMappingURL=widget_string.js.map