Debian Codegraph @

jupyter-widgets-controls/lib/widget_button.js

raw
1 // Copyright (c) Jupyter Development Team.
2 // Distributed under the terms of the Modified BSD License.
3 import { DOMWidgetView, StyleModel } from '@jupyter-widgets/base';
4 import { CoreDOMWidgetModel } from './widget_core';
5 import { JUPYTER_CONTROLS_VERSION } from './version';
6 export class ButtonStyleModel extends StyleModel {
7 defaults() {
8 return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'ButtonStyleModel', _model_module: '@jupyter-widgets/controls', _model_module_version: JUPYTER_CONTROLS_VERSION });
9 }
10 }
11 ButtonStyleModel.styleProperties = {
12 button_color: {
13 selector: '',
14 attribute: 'background-color',
15 default: null,
16 },
17 font_family: {
18 selector: '',
19 attribute: 'font-family',
20 default: '',
21 },
22 font_size: {
23 selector: '',
24 attribute: 'font-size',
25 default: '',
26 },
27 font_style: {
28 selector: '',
29 attribute: 'font-style',
30 default: '',
31 },
32 font_variant: {
33 selector: '',
34 attribute: 'font-variant',
35 default: '',
36 },
37 font_weight: {
38 selector: '',
39 attribute: 'font-weight',
40 default: '',
41 },
42 text_color: {
43 selector: '',
44 attribute: 'color',
45 default: '',
46 },
47 text_decoration: {
48 selector: '',
49 attribute: 'text-decoration',
50 default: '',
51 },
52 };
53 export class ButtonModel extends CoreDOMWidgetModel {
54 defaults() {
55 return Object.assign(Object.assign({}, super.defaults()), { description: '', tooltip: '', disabled: false, icon: '', button_style: '', _view_name: 'ButtonView', _model_name: 'ButtonModel', style: null });
56 }
57 }
58 export class ButtonView extends DOMWidgetView {
59 /**
60 * Called when view is rendered.
61 */
62 render() {
63 super.render();
64 this.el.classList.add('jupyter-widgets');
65 this.el.classList.add('jupyter-button');
66 this.el.classList.add('widget-button');
67 this.listenTo(this.model, 'change:button_style', this.update_button_style);
68 this.listenTo(this.model, 'change:tabbable', this.updateTabindex);
69 this.set_button_style();
70 this.update(); // Set defaults.
71 }
72 /**
73 * Update the contents of this view
74 *
75 * Called when the model is changed. The model may have been
76 * changed by another view or by a state update from the back-end.
77 */
78 update() {
79 this.el.disabled = this.model.get('disabled');
80 this.updateTabindex();
81 const tooltip = this.model.get('tooltip');
82 const description = this.model.get('description');
83 const icon = this.model.get('icon');
84 this.el.setAttribute('title', tooltip !== null && tooltip !== void 0 ? tooltip : description);
85 if (description.length || icon.length) {
86 this.el.textContent = '';
87 if (icon.length) {
88 const i = document.createElement('i');
89 i.classList.add('fa');
90 i.classList.add(...icon
91 .split(/[\s]+/)
92 .filter(Boolean)
93 .map((v) => `fa-${v}`));
94 if (description.length === 0) {
95 i.classList.add('center');
96 }
97 this.el.appendChild(i);
98 }
99 this.el.appendChild(document.createTextNode(description));
100 }
101 return super.update();
102 }
103 update_button_style() {
104 this.update_mapped_classes(ButtonView.class_map, 'button_style');
105 }
106 set_button_style() {
107 this.set_mapped_classes(ButtonView.class_map, 'button_style');
108 }
109 /**
110 * Dictionary of events and handlers
111 */
112 events() {
113 // TODO: return typing not needed in Typescript later than 1.8.x
114 // See http://stackoverflow.com/questions/22077023/why-cant-i-indirectly-return-an-object-literal-to-satisfy-an-index-signature-re and https://github.com/Microsoft/TypeScript/pull/7029
115 return { click: '_handle_click' };
116 }
117 /**
118 * Handles when the button is clicked.
119 */
120 _handle_click(event) {
121 event.preventDefault();
122 this.send({ event: 'click' });
123 }
124 preinitialize() {
125 // Must set this before the initialize method creates the element
126 this.tagName = 'button';
127 }
128 }
129 ButtonView.class_map = {
130 primary: ['mod-primary'],
131 success: ['mod-success'],
132 info: ['mod-info'],
133 warning: ['mod-warning'],
134 danger: ['mod-danger'],
135 };
136 //# sourceMappingURL=widget_button.js.map