Debian Codegraph @

jupyter-widgets-controls/lib/widget_bool.js

raw
1 // Copyright (c) Jupyter Development Team.
2 // Distributed under the terms of the Modified BSD License.
3 import { DOMWidgetView } from '@jupyter-widgets/base';
4 import { CoreDescriptionModel } from './widget_core';
5 import { DescriptionStyleModel, DescriptionView } from './widget_description';
6 export class CheckboxStyleModel extends DescriptionStyleModel {
7 defaults() {
8 return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'CheckboxStyleModel' });
9 }
10 }
11 CheckboxStyleModel.styleProperties = Object.assign(Object.assign({}, DescriptionStyleModel.styleProperties), { background: {
12 selector: '',
13 attribute: 'background',
14 default: null,
15 } });
16 export class ToggleButtonStyleModel extends DescriptionStyleModel {
17 defaults() {
18 return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'ToggleButtonStyleModel' });
19 }
20 }
21 ToggleButtonStyleModel.styleProperties = Object.assign(Object.assign({}, DescriptionStyleModel.styleProperties), { font_family: {
22 selector: '',
23 attribute: 'font-family',
24 default: '',
25 }, font_size: {
26 selector: '',
27 attribute: 'font-size',
28 default: '',
29 }, font_style: {
30 selector: '',
31 attribute: 'font-style',
32 default: '',
33 }, font_variant: {
34 selector: '',
35 attribute: 'font-variant',
36 default: '',
37 }, font_weight: {
38 selector: '',
39 attribute: 'font-weight',
40 default: '',
41 }, text_color: {
42 selector: '',
43 attribute: 'color',
44 default: '',
45 }, text_decoration: {
46 selector: '',
47 attribute: 'text-decoration',
48 default: '',
49 } });
50 export class BoolModel extends CoreDescriptionModel {
51 defaults() {
52 return Object.assign(Object.assign({}, super.defaults()), { value: false, disabled: false, _model_name: 'BoolModel' });
53 }
54 }
55 export class CheckboxModel extends CoreDescriptionModel {
56 defaults() {
57 return Object.assign(Object.assign({}, super.defaults()), { indent: true, style: null, _view_name: 'CheckboxView', _model_name: 'CheckboxModel' });
58 }
59 }
60 export class CheckboxView extends DescriptionView {
61 /**
62 * Called when view is rendered.
63 */
64 render() {
65 super.render();
66 this.el.classList.add('jupyter-widgets');
67 this.el.classList.add('widget-inline-hbox');
68 this.el.classList.add('widget-checkbox');
69 // adding a zero-width space to the label to help
70 // the browser set the baseline correctly
71 this.label.innerHTML = '​';
72 // label containing the checkbox and description span
73 this.checkboxLabel = document.createElement('label');
74 this.checkboxLabel.classList.add('widget-label-basic');
75 this.el.appendChild(this.checkboxLabel);
76 // checkbox
77 this.checkbox = document.createElement('input');
78 this.checkbox.setAttribute('type', 'checkbox');
79 this.checkboxLabel.appendChild(this.checkbox);
80 // span to the right of the checkbox that will render the description
81 this.descriptionSpan = document.createElement('span');
82 this.checkboxLabel.appendChild(this.descriptionSpan);
83 this.listenTo(this.model, 'change:indent', this.updateIndent);
84 this.listenTo(this.model, 'change:tabbable', this.updateTabindex);
85 this.update(); // Set defaults.
86 this.updateDescription();
87 this.updateIndent();
88 this.updateTabindex();
89 this.updateTooltip();
90 }
91 /**
92 * Overridden from super class
93 *
94 * Update the description span (rather than the label) since
95 * we want the description to the right of the checkbox.
96 */
97 updateDescription() {
98 // can be called before the view is fully initialized
99 if (this.checkboxLabel == null) {
100 return;
101 }
102 const description = this.model.get('description');
103 if (this.model.get('description_allow_html')) {
104 this.descriptionSpan.innerHTML =
105 this.model.widget_manager.inline_sanitize(description);
106 }
107 else {
108 this.descriptionSpan.textContent = description;
109 }
110 this.typeset(this.descriptionSpan);
111 this.descriptionSpan.title = description;
112 this.checkbox.title = description;
113 }
114 /**
115 * Update the visibility of the label in the super class
116 * to provide the optional indent.
117 */
118 updateIndent() {
119 const indent = this.model.get('indent');
120 this.label.style.display = indent ? '' : 'none';
121 }
122 updateTabindex() {
123 if (!this.checkbox) {
124 return; // we might be constructing the parent
125 }
126 const tabbable = this.model.get('tabbable');
127 if (tabbable === true) {
128 this.checkbox.setAttribute('tabIndex', '0');
129 }
130 else if (tabbable === false) {
131 this.checkbox.setAttribute('tabIndex', '-1');
132 }
133 else if (tabbable === null) {
134 this.checkbox.removeAttribute('tabIndex');
135 }
136 }
137 updateTooltip() {
138 if (!this.checkbox)
139 return; // we might be constructing the parent
140 const title = this.model.get('tooltip');
141 if (!title) {
142 this.checkbox.removeAttribute('title');
143 }
144 else if (this.model.get('description').length === 0) {
145 this.checkbox.setAttribute('title', title);
146 }
147 }
148 events() {
149 return {
150 'click input[type="checkbox"]': '_handle_click',
151 };
152 }
153 /**
154 * Handles when the checkbox is clicked.
155 *
156 * Calling model.set will trigger all of the other views of the
157 * model to update.
158 */
159 _handle_click() {
160 const value = this.model.get('value');
161 this.model.set('value', !value, { updated_view: this });
162 this.touch();
163 }
164 /**
165 * Update the contents of this view
166 *
167 * Called when the model is changed. The model may have been
168 * changed by another view or by a state update from the back-end.
169 */
170 update(options) {
171 this.checkbox.checked = this.model.get('value');
172 if (options === undefined || options.updated_view != this) {
173 this.checkbox.disabled = this.model.get('disabled');
174 }
175 return super.update();
176 }
177 /**
178 * Handle message sent to the front end.
179 *
180 * Used to focus or blur the widget.
181 */
182 handle_message(content) {
183 if (content.do == 'focus') {
184 this.checkbox.focus();
185 }
186 else if (content.do == 'blur') {
187 this.checkbox.blur();
188 }
189 }
190 }
191 export class ToggleButtonModel extends BoolModel {
192 defaults() {
193 return Object.assign(Object.assign({}, super.defaults()), { _view_name: 'ToggleButtonView', _model_name: 'ToggleButtonModel', tooltip: '', icon: '', button_style: '', style: null });
194 }
195 }
196 export class ToggleButtonView extends DOMWidgetView {
197 /**
198 * Called when view is rendered.
199 */
200 render() {
201 super.render();
202 this.el.classList.add('jupyter-widgets');
203 this.el.classList.add('jupyter-button');
204 this.el.classList.add('widget-toggle-button');
205 this.listenTo(this.model, 'change:button_style', this.update_button_style);
206 this.listenTo(this.model, 'change:tabbable', this.updateTabindex);
207 this.set_button_style();
208 this.update(); // Set defaults.
209 }
210 update_button_style() {
211 this.update_mapped_classes(ToggleButtonView.class_map, 'button_style');
212 }
213 set_button_style() {
214 this.set_mapped_classes(ToggleButtonView.class_map, 'button_style');
215 }
216 /**
217 * Update the contents of this view
218 *
219 * Called when the model is changed. The model may have been
220 * changed by another view or by a state update from the back-end.
221 */
222 update(options) {
223 if (this.model.get('value')) {
224 this.el.classList.add('mod-active');
225 }
226 else {
227 this.el.classList.remove('mod-active');
228 }
229 if (options === undefined || options.updated_view !== this) {
230 this.el.disabled = this.model.get('disabled');
231 this.el.setAttribute('tabbable', this.model.get('tabbable'));
232 this.el.setAttribute('title', this.model.get('tooltip'));
233 const description = this.model.get('description');
234 const icon = this.model.get('icon');
235 if (description.trim().length === 0 && icon.trim().length === 0) {
236 this.el.innerHTML = ' '; // Preserve button height
237 }
238 else {
239 this.el.textContent = '';
240 if (icon.trim().length) {
241 const i = document.createElement('i');
242 this.el.appendChild(i);
243 i.classList.add('fa');
244 i.classList.add('fa-' + icon);
245 }
246 this.el.appendChild(document.createTextNode(description));
247 }
248 }
249 this.updateTabindex();
250 return super.update();
251 }
252 events() {
253 return {
254 // Dictionary of events and their handlers.
255 click: '_handle_click',
256 };
257 }
258 /**
259 * Handles and validates user input.
260 *
261 * Calling model.set will trigger all of the other views of the
262 * model to update.
263 */
264 _handle_click(event) {
265 event.preventDefault();
266 const value = this.model.get('value');
267 this.model.set('value', !value, { updated_view: this });
268 this.touch();
269 }
270 preinitialize() {
271 // Must set this before the initialize method creates the element
272 this.tagName = 'button';
273 }
274 }
275 ToggleButtonView.class_map = {
276 primary: ['mod-primary'],
277 success: ['mod-success'],
278 info: ['mod-info'],
279 warning: ['mod-warning'],
280 danger: ['mod-danger'],
281 };
282 export class ValidModel extends BoolModel {
283 defaults() {
284 return Object.assign(Object.assign({}, super.defaults()), { readout: 'Invalid', _view_name: 'ValidView', _model_name: 'ValidModel' });
285 }
286 }
287 export class ValidView extends DescriptionView {
288 /**
289 * Called when view is rendered.
290 */
291 render() {
292 super.render();
293 this.el.classList.add('jupyter-widgets');
294 this.el.classList.add('widget-valid');
295 this.el.classList.add('widget-inline-hbox');
296 this.icon = document.createElement('i');
297 this.icon.classList.add('fa', 'fa-fw');
298 this.el.appendChild(this.icon);
299 this.readout = document.createElement('span');
300 this.readout.classList.add('widget-valid-readout');
301 this.readout.classList.add('widget-readout');
302 this.el.appendChild(this.readout);
303 this.update();
304 }
305 /**
306 * Update the contents of this view
307 *
308 * Called when the model is changed. The model may have been
309 * changed by another view or by a state update from the back-end.
310 */
311 update() {
312 this.el.classList.remove('mod-valid');
313 this.el.classList.remove('mod-invalid');
314 this.icon.classList.remove('fa-check');
315 this.icon.classList.remove('fa-times');
316 this.readout.textContent = this.model.get('readout');
317 if (this.model.get('value')) {
318 this.el.classList.add('mod-valid');
319 this.icon.classList.add('fa-check');
320 }
321 else {
322 this.el.classList.add('mod-invalid');
323 this.icon.classList.add('fa-times');
324 }
325 }
326 }
327 //# sourceMappingURL=widget_bool.js.map