Debian Codegraph @

jupyter-widgets-controls/lib/widget_selection.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 noUiSlider from 'nouislider';
7 import * as utils from './utils';
8 export class SelectionModel extends CoreDescriptionModel {
9 defaults() {
10 return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'SelectionModel', index: '', _options_labels: [], disabled: false });
11 }
12 }
13 export class SelectionView extends DescriptionView {
14 /**
15 * Called when view is rendered.
16 */
17 render() {
18 super.render(); // Incl. setting some defaults.
19 this.el.classList.add('jupyter-widgets');
20 this.el.classList.add('widget-inline-hbox');
21 }
22 /**
23 * Update the contents of this view
24 *
25 * Called when the model is changed. The model may have been
26 * changed by another view or by a state update from the back-end.
27 */
28 update() {
29 super.update();
30 // Disable listbox if needed
31 if (this.listbox) {
32 this.listbox.disabled = this.model.get('disabled');
33 }
34 // Set tabindex
35 this.updateTabindex();
36 this.updateTooltip();
37 }
38 updateTabindex() {
39 if (!this.listbox) {
40 return; // we might be constructing the parent
41 }
42 const tabbable = this.model.get('tabbable');
43 if (tabbable === true) {
44 this.listbox.setAttribute('tabIndex', '0');
45 }
46 else if (tabbable === false) {
47 this.listbox.setAttribute('tabIndex', '-1');
48 }
49 else if (tabbable === null) {
50 this.listbox.removeAttribute('tabIndex');
51 }
52 }
53 updateTooltip() {
54 if (!this.listbox)
55 return; // we might be constructing the parent
56 const title = this.model.get('tooltip');
57 if (!title) {
58 this.listbox.removeAttribute('title');
59 }
60 else if (this.model.get('description').length === 0) {
61 this.listbox.setAttribute('title', title);
62 }
63 }
64 }
65 export class DropdownModel extends SelectionModel {
66 defaults() {
67 return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'DropdownModel', _view_name: 'DropdownView', button_style: '' });
68 }
69 }
70 // TODO: Make a Lumino dropdown control, wrapped in DropdownView. Also, fix
71 // bugs in keyboard handling. See
72 // https://github.com/jupyter-widgets/ipywidgets/issues/1055 and
73 // https://github.com/jupyter-widgets/ipywidgets/issues/1049
74 // For now, we subclass SelectView to provide DropdownView
75 // For the old code, see commit f68bfbc566f3a78a8f3350b438db8ed523ce3642
76 export class DropdownView extends SelectionView {
77 /**
78 * Called when view is rendered.
79 */
80 render() {
81 super.render();
82 this.el.classList.add('widget-dropdown');
83 this.listbox = document.createElement('select');
84 this.listbox.id = this.label.htmlFor = uuid();
85 this.el.appendChild(this.listbox);
86 this._updateOptions();
87 this.update();
88 }
89 /**
90 * Update the contents of this view
91 */
92 update(options) {
93 // Debounce set calls from ourselves:
94 if ((options === null || options === void 0 ? void 0 : options.updated_view) !== this) {
95 const optsChanged = this.model.hasChanged('_options_labels');
96 if (optsChanged) {
97 // Need to update options:
98 this._updateOptions();
99 }
100 }
101 // Select the correct element
102 const index = this.model.get('index');
103 this.listbox.selectedIndex = index === null ? -1 : index;
104 return super.update();
105 }
106 _updateOptions() {
107 this.listbox.textContent = '';
108 const items = this.model.get('_options_labels');
109 for (let i = 0; i < items.length; i++) {
110 const item = items[i];
111 const option = document.createElement('option');
112 option.textContent = item.replace(/ /g, '\xa0'); // space -> &nbsp;
113 option.setAttribute('data-value', encodeURIComponent(item));
114 option.value = item;
115 this.listbox.appendChild(option);
116 }
117 }
118 events() {
119 return {
120 'change select': '_handle_change',
121 };
122 }
123 /**
124 * Handle when a new value is selected.
125 */
126 _handle_change() {
127 this.model.set('index', this.listbox.selectedIndex === -1 ? null : this.listbox.selectedIndex, { updated_view: this });
128 this.touch();
129 }
130 /**
131 * Handle message sent to the front end.
132 */
133 handle_message(content) {
134 if (content.do === 'focus') {
135 this.listbox.focus();
136 }
137 else if (content.do === 'blur') {
138 this.listbox.blur();
139 }
140 }
141 }
142 export class SelectModel extends SelectionModel {
143 defaults() {
144 return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'SelectModel', _view_name: 'SelectView', rows: 5 });
145 }
146 }
147 export class SelectView extends SelectionView {
148 /**
149 * Public constructor.
150 */
151 initialize(parameters) {
152 super.initialize(parameters);
153 // Create listbox here so that subclasses can modify it before it is populated in render()
154 this.listbox = document.createElement('select');
155 }
156 /**
157 * Called when view is rendered.
158 */
159 render() {
160 super.render();
161 this.el.classList.add('widget-select');
162 this.listbox.id = this.label.htmlFor = uuid();
163 this.el.appendChild(this.listbox);
164 this._updateOptions();
165 this.update();
166 this.updateSelection();
167 }
168 /**
169 * Update the contents of this view
170 */
171 update(options) {
172 // Don't update options/index on set calls from ourselves:
173 if ((options === null || options === void 0 ? void 0 : options.updated_view) !== this) {
174 const optsChange = this.model.hasChanged('_options_labels');
175 const idxChange = this.model.hasChanged('index');
176 if (optsChange || idxChange) {
177 // Stash the index to guard against change events
178 const idx = this.model.get('index');
179 if (optsChange) {
180 this._updateOptions();
181 }
182 this.updateSelection(idx);
183 }
184 }
185 super.update();
186 let rows = this.model.get('rows');
187 if (rows === null) {
188 rows = '';
189 }
190 this.listbox.setAttribute('size', rows);
191 }
192 updateSelection(index) {
193 index = index || this.model.get('index');
194 this.listbox.selectedIndex = index === null ? -1 : index;
195 }
196 _updateOptions() {
197 this.listbox.textContent = '';
198 const items = this.model.get('_options_labels');
199 for (let i = 0; i < items.length; i++) {
200 const item = items[i];
201 const option = document.createElement('option');
202 option.textContent = item.replace(/ /g, '\xa0'); // space -> &nbsp;
203 option.setAttribute('data-value', encodeURIComponent(item));
204 option.value = item;
205 this.listbox.appendChild(option);
206 }
207 }
208 events() {
209 return {
210 'change select': '_handle_change',
211 };
212 }
213 /**
214 * Handle when a new value is selected.
215 */
216 _handle_change() {
217 this.model.set('index', this.listbox.selectedIndex, { updated_view: this });
218 this.touch();
219 }
220 /**
221 * Handle message sent to the front end.
222 */
223 handle_message(content) {
224 if (content.do == 'focus') {
225 this.listbox.focus();
226 }
227 else if (content.do == 'blur') {
228 this.listbox.blur();
229 }
230 }
231 }
232 export class RadioButtonsModel extends SelectionModel {
233 defaults() {
234 return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'RadioButtonsModel', _view_name: 'RadioButtonsView', tooltips: [], icons: [], button_style: '', orientation: 'vertical' });
235 }
236 }
237 export class RadioButtonsView extends DescriptionView {
238 /**
239 * Called when view is rendered.
240 */
241 render() {
242 super.render();
243 this.el.classList.add('widget-radio');
244 this.container = document.createElement('div');
245 this.el.appendChild(this.container);
246 this.container.classList.add('widget-radio-box');
247 this.update();
248 }
249 /**
250 * Update the contents of this view
251 *
252 * Called when the model is changed. The model may have been
253 * changed by another view or by a state update from the back-end.
254 */
255 update(options) {
256 if (this.model.get('orientation') === 'vertical') {
257 this.container.classList.remove('widget-radio-box-horizontal');
258 this.container.classList.add('widget-radio-box-vertical');
259 }
260 else {
261 this.container.classList.remove('widget-radio-box-vertical');
262 this.container.classList.add('widget-radio-box-horizontal');
263 }
264 const items = this.model.get('_options_labels');
265 const radios = Array.from(this.container.querySelectorAll('input[type="radio"]')).map((x) => x.value);
266 let stale = items.length !== radios.length;
267 if (!stale) {
268 for (let i = 0, len = items.length; i < len; ++i) {
269 if (radios[i] !== items[i]) {
270 stale = true;
271 break;
272 }
273 }
274 }
275 if (stale && (options === undefined || options.updated_view !== this)) {
276 // Add items to the DOM.
277 this.container.textContent = '';
278 items.forEach((item, index) => {
279 const label = document.createElement('label');
280 label.textContent = item;
281 this.container.appendChild(label);
282 const radio = document.createElement('input');
283 radio.setAttribute('type', 'radio');
284 radio.value = index.toString();
285 radio.setAttribute('data-value', encodeURIComponent(item));
286 label.appendChild(radio);
287 });
288 }
289 items.forEach((item, index) => {
290 const item_query = 'input[data-value="' + encodeURIComponent(item) + '"]';
291 const radio = this.container.querySelectorAll(item_query);
292 if (radio.length > 0) {
293 const radio_el = radio[0];
294 radio_el.checked = this.model.get('index') === index;
295 radio_el.disabled = this.model.get('disabled');
296 }
297 });
298 // Schedule adjustPadding asynchronously to
299 // allow dom elements to be created properly
300 setTimeout(this.adjustPadding, 0, this);
301 return super.update(options);
302 }
303 /**
304 * Adjust Padding to Multiple of Line Height
305 *
306 * Adjust margins so that the overall height
307 * is a multiple of a single line height.
308 *
309 * This widget needs it because radio options
310 * are spaced tighter than individual widgets
311 * yet we would like the full widget line up properly
312 * when displayed side-by-side with other widgets.
313 */
314 adjustPadding(e) {
315 // Vertical margins on a widget
316 const elStyles = window.getComputedStyle(e.el);
317 const margins = parseInt(elStyles.marginTop, 10) + parseInt(elStyles.marginBottom, 10);
318 // Total spaces taken by a single-line widget
319 const lineHeight = e.label.offsetHeight + margins;
320 // Current adjustment value on this widget
321 const cStyles = window.getComputedStyle(e.container);
322 const containerMargin = parseInt(cStyles.marginBottom, 10);
323 // How far we are off from a multiple of single windget lines
324 const diff = (e.el.offsetHeight + margins - containerMargin) % lineHeight;
325 // Apply the new adjustment
326 const extraMargin = diff === 0 ? 0 : lineHeight - diff;
327 e.container.style.marginBottom = extraMargin + 'px';
328 }
329 events() {
330 return {
331 'click input[type="radio"]': '_handle_click',
332 };
333 }
334 /**
335 * Handle when a value is clicked.
336 *
337 * Calling model.set will trigger all of the other views of the
338 * model to update.
339 */
340 _handle_click(event) {
341 const target = event.target;
342 this.model.set('index', parseInt(target.value, 10), { updated_view: this });
343 this.touch();
344 }
345 /**
346 * Handle message sent to the front end.
347 */
348 handle_message(content) {
349 if (content.do == 'focus') {
350 const firstItem = this.container.firstElementChild;
351 firstItem.focus();
352 }
353 else if (content.do == 'blur') {
354 for (let i = 0; i < this.container.children.length; i++) {
355 const item = this.container.children[i];
356 item.blur();
357 }
358 }
359 }
360 }
361 export class ToggleButtonsStyleModel extends DescriptionStyleModel {
362 defaults() {
363 return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'ToggleButtonsStyleModel' });
364 }
365 }
366 ToggleButtonsStyleModel.styleProperties = Object.assign(Object.assign({}, DescriptionStyleModel.styleProperties), { button_width: {
367 selector: '.widget-toggle-button',
368 attribute: 'width',
369 default: null,
370 }, font_weight: {
371 selector: '.widget-toggle-button',
372 attribute: 'font-weight',
373 default: '',
374 } });
375 export class ToggleButtonsModel extends SelectionModel {
376 defaults() {
377 return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'ToggleButtonsModel', _view_name: 'ToggleButtonsView' });
378 }
379 }
380 export class ToggleButtonsView extends DescriptionView {
381 initialize(options) {
382 this._css_state = {};
383 super.initialize(options);
384 this.listenTo(this.model, 'change:button_style', this.update_button_style);
385 }
386 /**
387 * Called when view is rendered.
388 */
389 render() {
390 super.render();
391 this.el.classList.add('widget-toggle-buttons');
392 this.buttongroup = document.createElement('div');
393 this.el.appendChild(this.buttongroup);
394 this.update();
395 this.set_button_style();
396 }
397 /**
398 * Update the contents of this view
399 *
400 * Called when the model is changed. The model may have been
401 * changed by another view or by a state update from the back-end.
402 */
403 update(options) {
404 const items = this.model.get('_options_labels');
405 const icons = this.model.get('icons') || [];
406 const previous_icons = this.model.previous('icons') || [];
407 const previous_bstyle = ToggleButtonsView.classMap[this.model.previous('button_style')] || '';
408 const tooltips = this.model.get('tooltips') || [];
409 const disabled = this.model.get('disabled');
410 const buttons = this.buttongroup.querySelectorAll('button');
411 const values = Array.from(buttons).map((x) => x.value);
412 let stale = false;
413 for (let i = 0, len = items.length; i < len; ++i) {
414 if (values[i] !== items[i] || icons[i] !== previous_icons[i]) {
415 stale = true;
416 break;
417 }
418 }
419 if (stale && (options === undefined || options.updated_view !== this)) {
420 // Add items to the DOM.
421 this.buttongroup.textContent = '';
422 items.forEach((item, index) => {
423 let item_html;
424 const empty = item.trim().length === 0 &&
425 (!icons[index] || icons[index].trim().length === 0);
426 if (empty) {
427 item_html = '&nbsp;';
428 }
429 else {
430 item_html = utils.escape_html(item);
431 }
432 const icon = document.createElement('i');
433 const button = document.createElement('button');
434 if (icons[index]) {
435 icon.className = 'fa fa-' + icons[index];
436 }
437 button.setAttribute('type', 'button');
438 button.className = 'widget-toggle-button jupyter-button';
439 if (previous_bstyle) {
440 button.classList.add(previous_bstyle);
441 }
442 button.innerHTML = item_html;
443 button.setAttribute('data-value', encodeURIComponent(item));
444 button.setAttribute('value', index.toString());
445 button.appendChild(icon);
446 button.disabled = disabled;
447 if (tooltips[index]) {
448 button.setAttribute('title', tooltips[index]);
449 }
450 this.update_style_traits(button);
451 this.buttongroup.appendChild(button);
452 });
453 }
454 // Select active button.
455 items.forEach((item, index) => {
456 const item_query = '[data-value="' + encodeURIComponent(item) + '"]';
457 const button = this.buttongroup.querySelector(item_query);
458 if (this.model.get('index') === index) {
459 button.classList.add('mod-active');
460 }
461 else {
462 button.classList.remove('mod-active');
463 }
464 });
465 this.stylePromise.then(function (style) {
466 if (style) {
467 style.style();
468 }
469 });
470 return super.update(options);
471 }
472 update_style_traits(button) {
473 for (const name in this._css_state) {
474 if (Object.prototype.hasOwnProperty.call(this._css_state, 'name')) {
475 if (name === 'margin') {
476 this.buttongroup.style[name] = this._css_state[name];
477 }
478 else if (name !== 'width') {
479 if (button) {
480 button.style[name] = this._css_state[name];
481 }
482 else {
483 const buttons = this.buttongroup.querySelectorAll('button');
484 if (buttons.length) {
485 buttons[0].style[name] = this._css_state[name];
486 }
487 }
488 }
489 }
490 }
491 }
492 update_button_style() {
493 const buttons = this.buttongroup.querySelectorAll('button');
494 for (let i = 0; i < buttons.length; i++) {
495 this.update_mapped_classes(ToggleButtonsView.classMap, 'button_style', buttons[i]);
496 }
497 }
498 set_button_style() {
499 const buttons = this.buttongroup.querySelectorAll('button');
500 for (let i = 0; i < buttons.length; i++) {
501 this.set_mapped_classes(ToggleButtonsView.classMap, 'button_style', buttons[i]);
502 }
503 }
504 events() {
505 return {
506 'click button': '_handle_click',
507 };
508 }
509 /**
510 * Handle when a value is clicked.
511 *
512 * Calling model.set will trigger all of the other views of the
513 * model to update.
514 */
515 _handle_click(event) {
516 const target = event.target;
517 this.model.set('index', parseInt(target.value, 10), { updated_view: this });
518 this.touch();
519 // We also send a clicked event, since the value is only set if it changed.
520 // See https://github.com/jupyter-widgets/ipywidgets/issues/763
521 this.send({ event: 'click' });
522 }
523 }
524 (function (ToggleButtonsView) {
525 ToggleButtonsView.classMap = {
526 primary: ['mod-primary'],
527 success: ['mod-success'],
528 info: ['mod-info'],
529 warning: ['mod-warning'],
530 danger: ['mod-danger'],
531 };
532 })(ToggleButtonsView || (ToggleButtonsView = {}));
533 export class SelectionSliderModel extends SelectionModel {
534 defaults() {
535 return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'SelectionSliderModel', _view_name: 'SelectionSliderView', orientation: 'horizontal', readout: true, continuous_update: true });
536 }
537 }
538 export class SelectionSliderView extends DescriptionView {
539 /**
540 * Called when view is rendered.
541 */
542 render() {
543 super.render();
544 this.el.classList.add('widget-hslider');
545 this.el.classList.add('widget-slider');
546 // Creating noUiSlider instance and scaffolding
547 this.$slider = document.createElement('div');
548 this.$slider.classList.add('slider');
549 // Put the slider in a container
550 this.slider_container = document.createElement('div');
551 this.slider_container.classList.add('slider-container');
552 this.slider_container.appendChild(this.$slider);
553 this.el.appendChild(this.slider_container);
554 this.readout = document.createElement('div');
555 this.el.appendChild(this.readout);
556 this.readout.classList.add('widget-readout');
557 this.readout.style.display = 'none';
558 // noUiSlider constructor and event handlers
559 this.createSlider();
560 // Event handlers
561 this.model.on('change:orientation', this.regenSlider, this);
562 this.model.on('change:index', this.updateSliderValue, this);
563 // Set defaults.
564 this.update();
565 }
566 /**
567 * Update the contents of this view
568 *
569 * Called when the model is changed. The model may have been
570 * changed by another view or by a state update from the back-end.
571 */
572 update(options) {
573 if ((options === null || options === void 0 ? void 0 : options.updated_view) !== this) {
574 this.updateSliderOptions(this.model);
575 const orientation = this.model.get('orientation');
576 const disabled = this.model.get('disabled');
577 if (disabled) {
578 this.readout.contentEditable = 'false';
579 this.$slider.setAttribute('disabled', true);
580 }
581 else {
582 this.readout.contentEditable = 'true';
583 this.$slider.removeAttribute('disabled');
584 }
585 // Use the right CSS classes for vertical & horizontal sliders
586 if (orientation === 'vertical') {
587 this.el.classList.remove('widget-hslider');
588 this.el.classList.remove('widget-inline-hbox');
589 this.el.classList.add('widget-vslider');
590 this.el.classList.add('widget-inline-vbox');
591 }
592 else {
593 this.el.classList.remove('widget-vslider');
594 this.el.classList.remove('widget-inline-vbox');
595 this.el.classList.add('widget-hslider');
596 this.el.classList.add('widget-inline-hbox');
597 }
598 const readout = this.model.get('readout');
599 if (readout) {
600 // this.$readout.show();
601 this.readout.style.display = '';
602 }
603 else {
604 // this.$readout.hide();
605 this.readout.style.display = 'none';
606 }
607 this.updateSelection();
608 }
609 return super.update(options);
610 }
611 regenSlider(e) {
612 this.$slider.noUiSlider.destroy();
613 this.createSlider();
614 }
615 createSlider() {
616 const labels = this.model.get('_options_labels');
617 const min = 0;
618 const max = labels.length - 1;
619 const orientation = this.model.get('orientation');
620 const behavior = this.model.get('behavior');
621 noUiSlider.create(this.$slider, {
622 start: this.model.get('index'),
623 connect: true,
624 behaviour: behavior,
625 range: {
626 min: min,
627 max: max,
628 },
629 step: 1,
630 animate: false,
631 orientation: orientation,
632 direction: orientation === 'horizontal' ? 'ltr' : 'rtl',
633 format: {
634 from: (value) => Number(value),
635 to: (value) => Math.round(value),
636 },
637 });
638 // Using noUiSlider's 'update' and 'change' events.
639 // See reference: https://refreshless.com/nouislider/events-callbacks/
640 this.$slider.noUiSlider.on('update', (values, handle) => {
641 this.handleSliderUpdateEvent(values, handle);
642 });
643 this.$slider.noUiSlider.on('change', (values, handle) => {
644 this.handleSliderChangeEvent(values, handle);
645 });
646 }
647 events() {
648 return {
649 slide: 'handleSliderChange',
650 slidestop: 'handleSliderChanged',
651 };
652 }
653 updateSelection() {
654 const index = this.model.get('index');
655 this.updateReadout(index);
656 }
657 updateReadout(index) {
658 const value = this.model.get('_options_labels')[index];
659 this.readout.textContent = value;
660 }
661 /**
662 * Called whilst the slider is dragged, tapped or moved by the arrow keys.
663 */
664 handleSliderUpdateEvent(values, handle) {
665 const index = values[0];
666 this.updateReadout(index);
667 // Only persist the value while sliding if the continuous_update
668 // trait is set to true.
669 if (this.model.get('continuous_update')) {
670 this.handleSliderChanged(values, handle);
671 }
672 }
673 /**
674 * Called when the slider handle is released after dragging,
675 * or by tapping or moving by the arrow keys.
676 */
677 handleSliderChangeEvent(values, handle) {
678 const index = values[0];
679 this.updateReadout(index);
680 this.handleSliderChanged(values, handle);
681 }
682 /**
683 * Called when the slider value has changed.
684 *
685 * Calling model.set will trigger all of the other views of the
686 * model to update.
687 */
688 handleSliderChanged(values, handle) {
689 const index = values[0];
690 this.updateReadout(index);
691 this.model.set('index', index, { updated_view: this });
692 this.touch();
693 }
694 updateSliderOptions(e) {
695 const labels = this.model.get('_options_labels');
696 const min = 0;
697 const max = labels.length - 1;
698 this.$slider.noUiSlider.updateOptions({
699 start: this.model.get('index'),
700 range: {
701 min: min,
702 max: max,
703 },
704 step: 1,
705 });
706 }
707 updateSliderValue(model, _, options) {
708 if (options.updated_view === this) {
709 return;
710 }
711 const prev_index = this.$slider.noUiSlider.get();
712 const index = this.model.get('index');
713 if (prev_index !== index) {
714 this.$slider.noUiSlider.set(index);
715 }
716 }
717 }
718 export class MultipleSelectionModel extends SelectionModel {
719 defaults() {
720 return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'MultipleSelectionModel' });
721 }
722 }
723 export class SelectMultipleModel extends MultipleSelectionModel {
724 defaults() {
725 return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'SelectMultipleModel', _view_name: 'SelectMultipleView', rows: null });
726 }
727 }
728 export class SelectMultipleView extends SelectView {
729 /**
730 * Public constructor.
731 */
732 initialize(parameters) {
733 super.initialize(parameters);
734 this.listbox.multiple = true;
735 }
736 /**
737 * Called when view is rendered.
738 */
739 render() {
740 super.render();
741 this.el.classList.add('widget-select-multiple');
742 }
743 updateSelection() {
744 const selected = this.model.get('index') || [];
745 const listboxOptions = this.listbox.options;
746 // Clear the selection
747 this.listbox.selectedIndex = -1;
748 // Select the appropriate options
749 selected.forEach((i) => {
750 listboxOptions[i].selected = true;
751 });
752 }
753 /**
754 * Handle when a new value is selected.
755 */
756 _handle_change() {
757 const index = Array.prototype.map.call(this.listbox.selectedOptions || [], function (option) {
758 return option.index;
759 });
760 this.model.set('index', index, { updated_view: this });
761 this.touch();
762 }
763 }
764 export class SelectionRangeSliderModel extends MultipleSelectionModel {
765 defaults() {
766 return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'SelectionSliderModel', _view_name: 'SelectionSliderView', orientation: 'horizontal', readout: true, continuous_update: true });
767 }
768 }
769 export class SelectionRangeSliderView extends SelectionSliderView {
770 /**
771 * Called when view is rendered.
772 */
773 render() {
774 super.render();
775 }
776 updateSelection(index) {
777 index = index || this.model.get('index');
778 this.updateReadout(index);
779 }
780 updateReadout(index) {
781 const labels = this.model.get('_options_labels');
782 const minValue = labels[index[0]];
783 const maxValue = labels[index[1]];
784 this.readout.textContent = `${minValue}-${maxValue}`;
785 }
786 /**
787 * Called when the slider value is changing.
788 */
789 handleSliderUpdateEvent(values, handle) {
790 const intValues = values.map(Math.trunc);
791 this.updateReadout(intValues);
792 // Only persist the value while sliding if the continuous_update
793 // trait is set to true.
794 if (this.model.get('continuous_update')) {
795 this.handleSliderChanged(values, handle);
796 }
797 }
798 /**
799 * Called when the slider value has changed.
800 *
801 * Calling model.set will trigger all of the other views of the
802 * model to update.
803 */
804 handleSliderChanged(values, handle) {
805 const intValues = values.map(Math.round);
806 this.updateReadout(intValues);
807 // set index to a snapshot of the values passed by the slider
808 this.model.set('index', intValues.slice(), { updated_view: this });
809 this.touch();
810 }
811 updateSliderValue(model, _, options) {
812 if (options.updated_view === this) {
813 return;
814 }
815 // Rounding values to avoid floating point precision error for the if statement below
816 const prev_index = this.$slider.noUiSlider.get().map(Math.round);
817 const index = this.model.get('index').map(Math.round);
818 if (prev_index[0] !== index[0] || prev_index[1] !== index[1]) {
819 this.$slider.noUiSlider.set(index);
820 }
821 }
822 }
823 //# sourceMappingURL=widget_selection.js.map