| 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 { DOMWidgetView } from '@jupyter-widgets/base'; |
| 6 |
import { uuid } from './utils'; |
| 7 |
import { format } from 'd3-format'; |
| 8 |
import noUiSlider from 'nouislider'; |
| 9 |
export class IntModel extends CoreDescriptionModel { |
| 10 |
defaults() { |
| 11 |
return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'IntModel', value: 0 }); |
| 12 |
} |
| 13 |
} |
| 14 |
export class BoundedIntModel extends IntModel { |
| 15 |
defaults() { |
| 16 |
return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'BoundedIntModel', max: 100, min: 0 }); |
| 17 |
} |
| 18 |
} |
| 19 |
export class SliderStyleModel extends DescriptionStyleModel { |
| 20 |
defaults() { |
| 21 |
return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'SliderStyleModel' }); |
| 22 |
} |
| 23 |
} |
| 24 |
SliderStyleModel.styleProperties = Object.assign(Object.assign({}, DescriptionStyleModel.styleProperties), { handle_color: { |
| 25 |
selector: '.noUi-handle', |
| 26 |
attribute: 'background-color', |
| 27 |
default: null, |
| 28 |
} }); |
| 29 |
export class IntSliderModel extends BoundedIntModel { |
| 30 |
defaults() { |
| 31 |
return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'IntSliderModel', _view_name: 'IntSliderView', step: 1, orientation: 'horizontal', readout: true, readout_format: 'd', continuous_update: true, style: null, disabled: false }); |
| 32 |
} |
| 33 |
initialize(attributes, options) { |
| 34 |
super.initialize(attributes, options); |
| 35 |
this.on('change:readout_format', this.update_readout_format, this); |
| 36 |
this.update_readout_format(); |
| 37 |
} |
| 38 |
update_readout_format() { |
| 39 |
this.readout_formatter = format(this.get('readout_format')); |
| 40 |
} |
| 41 |
} |
| 42 |
export class IntRangeSliderModel extends IntSliderModel { |
| 43 |
} |
| 44 |
export class BaseIntSliderView extends DescriptionView { |
| 45 |
constructor() { |
| 46 |
super(...arguments); |
| 47 |
this._parse_value = parseInt; |
| 48 |
} |
| 49 |
render() { |
| 50 |
super.render(); |
| 51 |
this.el.classList.add('jupyter-widgets'); |
| 52 |
this.el.classList.add('widget-inline-hbox'); |
| 53 |
this.el.classList.add('widget-slider'); |
| 54 |
this.el.classList.add('widget-hslider'); |
| 55 |
// Creating noUiSlider instance and scaffolding |
| 56 |
this.$slider = document.createElement('div'); |
| 57 |
this.$slider.classList.add('slider'); |
| 58 |
// Put the slider in a container |
| 59 |
this.slider_container = document.createElement('div'); |
| 60 |
this.slider_container.classList.add('slider-container'); |
| 61 |
this.slider_container.appendChild(this.$slider); |
| 62 |
this.el.appendChild(this.slider_container); |
| 63 |
this.readout = document.createElement('div'); |
| 64 |
this.el.appendChild(this.readout); |
| 65 |
this.readout.classList.add('widget-readout'); |
| 66 |
this.readout.contentEditable = 'true'; |
| 67 |
this.readout.style.display = 'none'; |
| 68 |
// noUiSlider constructor and event handlers |
| 69 |
this.createSlider(); |
| 70 |
// Event handlers |
| 71 |
this.model.on('change:orientation', this.regenSlider, this); |
| 72 |
this.model.on('change:max', this.updateSliderOptions, this); |
| 73 |
this.model.on('change:min', this.updateSliderOptions, this); |
| 74 |
this.model.on('change:step', this.updateSliderOptions, this); |
| 75 |
this.model.on('change:value', this.updateSliderValue, this); |
| 76 |
// Set defaults. |
| 77 |
this.update(); |
| 78 |
} |
| 79 |
/** |
| 80 |
* Update the contents of this view |
| 81 |
* |
| 82 |
* Called when the model is changed. The model may have been |
| 83 |
* changed by another view or by a state update from the back-end. |
| 84 |
*/ |
| 85 |
update(options) { |
| 86 |
if (options === undefined || options.updated_view !== this) { |
| 87 |
if (this.model.get('disabled')) { |
| 88 |
this.readout.contentEditable = 'false'; |
| 89 |
this.$slider.setAttribute('disabled', true); |
| 90 |
} |
| 91 |
else { |
| 92 |
this.readout.contentEditable = 'true'; |
| 93 |
this.$slider.removeAttribute('disabled'); |
| 94 |
} |
| 95 |
// Use the right CSS classes for vertical & horizontal sliders |
| 96 |
const orientation = this.model.get('orientation'); |
| 97 |
if (orientation === 'vertical') { |
| 98 |
this.el.classList.remove('widget-hslider'); |
| 99 |
this.el.classList.add('widget-vslider'); |
| 100 |
this.el.classList.remove('widget-inline-hbox'); |
| 101 |
this.el.classList.add('widget-inline-vbox'); |
| 102 |
} |
| 103 |
else { |
| 104 |
this.el.classList.remove('widget-vslider'); |
| 105 |
this.el.classList.add('widget-hslider'); |
| 106 |
this.el.classList.remove('widget-inline-vbox'); |
| 107 |
this.el.classList.add('widget-inline-hbox'); |
| 108 |
} |
| 109 |
const readout = this.model.get('readout'); |
| 110 |
if (readout) { |
| 111 |
this.readout.style.display = ''; |
| 112 |
this.displayed.then(() => { |
| 113 |
if (this.readout_overflow()) { |
| 114 |
this.readout.classList.add('overflow'); |
| 115 |
} |
| 116 |
else { |
| 117 |
this.readout.classList.remove('overflow'); |
| 118 |
} |
| 119 |
}); |
| 120 |
} |
| 121 |
else { |
| 122 |
this.readout.style.display = 'none'; |
| 123 |
} |
| 124 |
} |
| 125 |
return super.update(); |
| 126 |
} |
| 127 |
/** |
| 128 |
* Returns true if the readout box content overflows. |
| 129 |
*/ |
| 130 |
readout_overflow() { |
| 131 |
return this.readout.scrollWidth > this.readout.clientWidth; |
| 132 |
} |
| 133 |
events() { |
| 134 |
return { |
| 135 |
// Dictionary of events and their handlers. |
| 136 |
'blur [contentEditable=true]': 'handleTextChange', |
| 137 |
'keydown [contentEditable=true]': 'handleKeyDown', |
| 138 |
}; |
| 139 |
} |
| 140 |
handleKeyDown(e) { |
| 141 |
if (e.keyCode === 13) { |
| 142 |
/* keyboard keycodes `enter` */ |
| 143 |
e.preventDefault(); |
| 144 |
this.handleTextChange(); |
| 145 |
} |
| 146 |
} |
| 147 |
/** |
| 148 |
* Create a new noUiSlider object |
| 149 |
*/ |
| 150 |
createSlider() { |
| 151 |
const orientation = this.model.get('orientation'); |
| 152 |
const behavior = this.model.get('behavior'); |
| 153 |
noUiSlider.create(this.$slider, { |
| 154 |
start: this.model.get('value'), |
| 155 |
connect: true, |
| 156 |
behaviour: behavior, |
| 157 |
range: { |
| 158 |
min: this.model.get('min'), |
| 159 |
max: this.model.get('max'), |
| 160 |
}, |
| 161 |
step: this.model.get('step'), |
| 162 |
animate: false, |
| 163 |
orientation: orientation, |
| 164 |
direction: orientation === 'horizontal' ? 'ltr' : 'rtl', |
| 165 |
format: { |
| 166 |
from: (value) => Number(value), |
| 167 |
to: (value) => this._validate_slide_value(value), |
| 168 |
}, |
| 169 |
}); |
| 170 |
// Using noUiSlider's 'update' and 'change' events. |
| 171 |
// See reference: https://refreshless.com/nouislider/events-callbacks/ |
| 172 |
this.$slider.noUiSlider.on('update', (values, handle) => { |
| 173 |
this.handleSliderUpdateEvent(values, handle); |
| 174 |
}); |
| 175 |
this.$slider.noUiSlider.on('change', (values, handle) => { |
| 176 |
this.handleSliderChangeEvent(values, handle); |
| 177 |
}); |
| 178 |
} |
| 179 |
/** |
| 180 |
* Recreate/Regenerate a slider object |
| 181 |
* noUiSlider does not support in-place mutation of the orientation |
| 182 |
* state. We therefore need to destroy the current instance |
| 183 |
* and create a new one with the new properties. This is |
| 184 |
* handled in a separate function and has a dedicated event |
| 185 |
* handler. |
| 186 |
*/ |
| 187 |
regenSlider(e) { |
| 188 |
this.$slider.noUiSlider.destroy(); |
| 189 |
this.createSlider(); |
| 190 |
} |
| 191 |
/** |
| 192 |
* Validate the value of the slider before sending it to the back-end |
| 193 |
* and applying it to the other views on the page. |
| 194 |
*/ |
| 195 |
_validate_slide_value(x) { |
| 196 |
return Math.round(x); |
| 197 |
} |
| 198 |
} |
| 199 |
export class IntRangeSliderView extends BaseIntSliderView { |
| 200 |
constructor() { |
| 201 |
super(...arguments); |
| 202 |
// range numbers can be separated by a hyphen, colon, or an en-dash |
| 203 |
this._range_regex = /^\s*([+-]?\d+)\s*[-:–]\s*([+-]?\d+)/; |
| 204 |
} |
| 205 |
update(options) { |
| 206 |
super.update(options); |
| 207 |
const value = this.model.get('value'); |
| 208 |
this.readout.textContent = this.valueToString(value); |
| 209 |
if (this.model.get('value') !== value) { |
| 210 |
this.model.set('value', value, { updated_view: this }); |
| 211 |
this.touch(); |
| 212 |
} |
| 213 |
} |
| 214 |
/** |
| 215 |
* Write value to a string |
| 216 |
*/ |
| 217 |
valueToString(value) { |
| 218 |
const format = this.model.readout_formatter; |
| 219 |
return value |
| 220 |
.map(function (v) { |
| 221 |
return format(v); |
| 222 |
}) |
| 223 |
.join(' – '); |
| 224 |
} |
| 225 |
/** |
| 226 |
* Parse value from a string |
| 227 |
*/ |
| 228 |
stringToValue(text) { |
| 229 |
if (text === null) { |
| 230 |
return null; |
| 231 |
} |
| 232 |
// ranges can be expressed either 'val-val' or 'val:val' (+spaces) |
| 233 |
const match = this._range_regex.exec(text); |
| 234 |
if (match) { |
| 235 |
return [this._parse_value(match[1]), this._parse_value(match[2])]; |
| 236 |
} |
| 237 |
else { |
| 238 |
return null; |
| 239 |
} |
| 240 |
} |
| 241 |
handleTextChange() { |
| 242 |
let value = this.stringToValue(this.readout.textContent); |
| 243 |
const vmin = this.model.get('min'); |
| 244 |
const vmax = this.model.get('max'); |
| 245 |
// reject input where NaN or lower > upper |
| 246 |
if (value === null || |
| 247 |
isNaN(value[0]) || |
| 248 |
isNaN(value[1]) || |
| 249 |
value[0] > value[1]) { |
| 250 |
this.readout.textContent = this.valueToString(this.model.get('value')); |
| 251 |
} |
| 252 |
else { |
| 253 |
// clamp to range |
| 254 |
value = [ |
| 255 |
Math.max(Math.min(value[0], vmax), vmin), |
| 256 |
Math.max(Math.min(value[1], vmax), vmin), |
| 257 |
]; |
| 258 |
if (value[0] !== this.model.get('value')[0] || |
| 259 |
value[1] !== this.model.get('value')[1]) { |
| 260 |
this.readout.textContent = this.valueToString(value); |
| 261 |
this.model.set('value', value); |
| 262 |
this.touch(); |
| 263 |
} |
| 264 |
else { |
| 265 |
this.readout.textContent = this.valueToString(this.model.get('value')); |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
/** |
| 270 |
* Called when the slider handle is released after dragging, |
| 271 |
* or by tapping or moving by the arrow keys. |
| 272 |
*/ |
| 273 |
handleSliderChangeEvent(values, handle) { |
| 274 |
const actual_value = values.map(this._validate_slide_value); |
| 275 |
this.readout.textContent = this.valueToString(actual_value); |
| 276 |
this.handleSliderChanged(values, handle); |
| 277 |
} |
| 278 |
/** |
| 279 |
* Called whilst the slider is dragged, tapped or moved by the arrow keys. |
| 280 |
*/ |
| 281 |
handleSliderUpdateEvent(values, handle) { |
| 282 |
const actual_value = values.map(this._validate_slide_value); |
| 283 |
this.readout.textContent = this.valueToString(actual_value); |
| 284 |
// Only persist the value while sliding if the continuous_update |
| 285 |
// trait is set to true. |
| 286 |
if (this.model.get('continuous_update')) { |
| 287 |
this.handleSliderChanged(values, handle); |
| 288 |
} |
| 289 |
} |
| 290 |
handleSliderChanged(values, handle) { |
| 291 |
const actual_value = values.map(this._validate_slide_value); |
| 292 |
this.model.set('value', actual_value, { updated_view: this }); |
| 293 |
this.touch(); |
| 294 |
} |
| 295 |
updateSliderOptions(e) { |
| 296 |
this.$slider.noUiSlider.updateOptions({ |
| 297 |
start: this.model.get('value'), |
| 298 |
range: { |
| 299 |
min: this.model.get('min'), |
| 300 |
max: this.model.get('max'), |
| 301 |
}, |
| 302 |
step: this.model.get('step'), |
| 303 |
}); |
| 304 |
} |
| 305 |
updateSliderValue(model, _, options) { |
| 306 |
if (options.updated_view === this) { |
| 307 |
return; |
| 308 |
} |
| 309 |
const prev_value = this.$slider.noUiSlider.get(); |
| 310 |
const value = this.model.get('value'); |
| 311 |
if (prev_value[0] !== value[0] || prev_value[1] !== value[1]) { |
| 312 |
this.$slider.noUiSlider.set(value); |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
export class IntSliderView extends BaseIntSliderView { |
| 317 |
update(options) { |
| 318 |
super.update(options); |
| 319 |
const min = this.model.get('min'); |
| 320 |
const max = this.model.get('max'); |
| 321 |
let value = this.model.get('value'); |
| 322 |
if (value > max) { |
| 323 |
value = max; |
| 324 |
} |
| 325 |
else if (value < min) { |
| 326 |
value = min; |
| 327 |
} |
| 328 |
this.readout.textContent = this.valueToString(value); |
| 329 |
if (this.model.get('value') !== value) { |
| 330 |
this.model.set('value', value, { updated_view: this }); |
| 331 |
this.touch(); |
| 332 |
} |
| 333 |
} |
| 334 |
valueToString(value) { |
| 335 |
const format = this.model.readout_formatter; |
| 336 |
return format(value); |
| 337 |
} |
| 338 |
stringToValue(text) { |
| 339 |
return this._parse_value(text); |
| 340 |
} |
| 341 |
handleTextChange() { |
| 342 |
var _a; |
| 343 |
let value = this.stringToValue((_a = this.readout.textContent) !== null && _a !== void 0 ? _a : ''); |
| 344 |
const vmin = this.model.get('min'); |
| 345 |
const vmax = this.model.get('max'); |
| 346 |
if (isNaN(value)) { |
| 347 |
this.readout.textContent = this.valueToString(this.model.get('value')); |
| 348 |
} |
| 349 |
else { |
| 350 |
value = Math.max(Math.min(value, vmax), vmin); |
| 351 |
if (value !== this.model.get('value')) { |
| 352 |
this.readout.textContent = this.valueToString(value); |
| 353 |
this.model.set('value', value); |
| 354 |
this.touch(); |
| 355 |
} |
| 356 |
else { |
| 357 |
this.readout.textContent = this.valueToString(this.model.get('value')); |
| 358 |
} |
| 359 |
} |
| 360 |
} |
| 361 |
handleSliderChangeEvent(values, handle) { |
| 362 |
const actual_value = values.map(this._validate_slide_value); |
| 363 |
this.readout.textContent = this.valueToString(actual_value); |
| 364 |
this.handleSliderChanged(values, handle); |
| 365 |
} |
| 366 |
handleSliderUpdateEvent(values, handle) { |
| 367 |
const actual_value = values.map(this._validate_slide_value); |
| 368 |
this.readout.textContent = this.valueToString(actual_value); |
| 369 |
// Only persist the value while sliding if the continuous_update |
| 370 |
// trait is set to true. |
| 371 |
if (this.model.get('continuous_update')) { |
| 372 |
this.handleSliderChanged(values, handle); |
| 373 |
} |
| 374 |
} |
| 375 |
handleSliderChanged(values, handle) { |
| 376 |
const actual_value = this._validate_slide_value(values[handle]); |
| 377 |
const model_value = this.model.get('value'); |
| 378 |
if (parseFloat(model_value) !== actual_value) { |
| 379 |
this.model.set('value', actual_value, { updated_view: this }); |
| 380 |
this.touch(); |
| 381 |
} |
| 382 |
} |
| 383 |
updateSliderOptions(e) { |
| 384 |
this.$slider.noUiSlider.updateOptions({ |
| 385 |
start: this.model.get('value'), |
| 386 |
range: { |
| 387 |
min: this.model.get('min'), |
| 388 |
max: this.model.get('max'), |
| 389 |
}, |
| 390 |
step: this.model.get('step'), |
| 391 |
}); |
| 392 |
} |
| 393 |
updateSliderValue(model, _, options) { |
| 394 |
if (options.updated_view === this) { |
| 395 |
return; |
| 396 |
} |
| 397 |
const prev_value = this.$slider.noUiSlider.get(); |
| 398 |
const value = this.model.get('value'); |
| 399 |
if (prev_value !== value) { |
| 400 |
this.$slider.noUiSlider.set(value); |
| 401 |
} |
| 402 |
} |
| 403 |
} |
| 404 |
export class IntTextModel extends IntModel { |
| 405 |
defaults() { |
| 406 |
return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'IntTextModel', _view_name: 'IntTextView', disabled: false, continuous_update: false }); |
| 407 |
} |
| 408 |
} |
| 409 |
export class BoundedIntTextModel extends BoundedIntModel { |
| 410 |
defaults() { |
| 411 |
return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'BoundedIntTextModel', _view_name: 'IntTextView', disabled: false, continuous_update: false, step: 1 }); |
| 412 |
} |
| 413 |
} |
| 414 |
export class IntTextView extends DescriptionView { |
| 415 |
constructor() { |
| 416 |
super(...arguments); |
| 417 |
this._parse_value = parseInt; |
| 418 |
this._default_step = '1'; |
| 419 |
} |
| 420 |
render() { |
| 421 |
super.render(); |
| 422 |
this.el.classList.add('jupyter-widgets'); |
| 423 |
this.el.classList.add('widget-inline-hbox'); |
| 424 |
this.el.classList.add('widget-text'); |
| 425 |
this.textbox = document.createElement('input'); |
| 426 |
this.textbox.type = 'number'; |
| 427 |
this.textbox.required = true; |
| 428 |
this.textbox.id = this.label.htmlFor = uuid(); |
| 429 |
this.el.appendChild(this.textbox); |
| 430 |
this.update(); // Set defaults. |
| 431 |
} |
| 432 |
/** |
| 433 |
* Update the contents of this view |
| 434 |
* |
| 435 |
* Called when the model is changed. The model may have been |
| 436 |
* changed by another view or by a state update from the back-end. |
| 437 |
*/ |
| 438 |
update(options) { |
| 439 |
if (options === undefined || options.updated_view !== this) { |
| 440 |
const value = this.model.get('value'); |
| 441 |
if (this._parse_value(this.textbox.value) !== value) { |
| 442 |
this.textbox.value = value.toString(); |
| 443 |
} |
| 444 |
if (this.model.get('min') !== undefined) { |
| 445 |
this.textbox.min = this.model.get('min'); |
| 446 |
} |
| 447 |
if (this.model.get('max') !== undefined) { |
| 448 |
this.textbox.max = this.model.get('max'); |
| 449 |
} |
| 450 |
if (this.model.get('step') !== undefined && |
| 451 |
this.model.get('step') !== null) { |
| 452 |
this.textbox.step = this.model.get('step'); |
| 453 |
} |
| 454 |
else { |
| 455 |
this.textbox.step = this._default_step; |
| 456 |
} |
| 457 |
this.textbox.disabled = this.model.get('disabled'); |
| 458 |
} |
| 459 |
return super.update(); |
| 460 |
} |
| 461 |
events() { |
| 462 |
return { |
| 463 |
'keydown input': 'handleKeyDown', |
| 464 |
'keypress input': 'handleKeypress', |
| 465 |
'keyup input': 'handleKeyUp', |
| 466 |
'input input': 'handleChanging', |
| 467 |
'change input': 'handleChanged', |
| 468 |
}; |
| 469 |
} |
| 470 |
/** |
| 471 |
* Handle key down |
| 472 |
* |
| 473 |
* Stop propagation so the event isn't sent to the application. |
| 474 |
*/ |
| 475 |
handleKeyDown(e) { |
| 476 |
e.stopPropagation(); |
| 477 |
} |
| 478 |
/** |
| 479 |
* Handles key press |
| 480 |
*/ |
| 481 |
handleKeypress(e) { |
| 482 |
if (/[e,. ]/.test(String.fromCharCode(e.keyCode))) { |
| 483 |
e.preventDefault(); |
| 484 |
} |
| 485 |
} |
| 486 |
/** |
| 487 |
* Handle key up |
| 488 |
*/ |
| 489 |
handleKeyUp(e) { |
| 490 |
if (e.altKey || e.ctrlKey) { |
| 491 |
return; |
| 492 |
} |
| 493 |
const target = e.target; |
| 494 |
/* remove invalid characters */ |
| 495 |
let value = target.value; |
| 496 |
value = value.replace(/[e,.\s]/g, ''); |
| 497 |
if (value.length >= 1) { |
| 498 |
const subvalue = value.substr(1); |
| 499 |
value = value[0] + subvalue.replace(/[+-]/g, ''); |
| 500 |
} |
| 501 |
if (target.value !== value) { |
| 502 |
e.preventDefault(); |
| 503 |
target.value = value; |
| 504 |
} |
| 505 |
} |
| 506 |
/** |
| 507 |
* Call the submit handler if continuous update is true and we are not |
| 508 |
* obviously incomplete. |
| 509 |
*/ |
| 510 |
handleChanging(e) { |
| 511 |
const target = e.target; |
| 512 |
const trimmed = target.value.trim(); |
| 513 |
if (trimmed === '' || ['-', '-.', '.', '+.', '+'].indexOf(trimmed) >= 0) { |
| 514 |
// incomplete number |
| 515 |
return; |
| 516 |
} |
| 517 |
if (this.model.get('continuous_update')) { |
| 518 |
this.handleChanged(e); |
| 519 |
} |
| 520 |
} |
| 521 |
/** |
| 522 |
* Applies validated input. |
| 523 |
*/ |
| 524 |
handleChanged(e) { |
| 525 |
const target = e.target; |
| 526 |
let numericalValue = this._parse_value(target.value); |
| 527 |
// If parse failed, reset value to value stored in model. |
| 528 |
if (isNaN(numericalValue)) { |
| 529 |
target.value = this.model.get('value'); |
| 530 |
} |
| 531 |
else { |
| 532 |
// Handle both the unbounded and bounded case by |
| 533 |
// checking to see if the max/min properties are defined |
| 534 |
let boundedValue = numericalValue; |
| 535 |
if (this.model.get('max') !== undefined) { |
| 536 |
boundedValue = Math.min(this.model.get('max'), boundedValue); |
| 537 |
} |
| 538 |
if (this.model.get('min') !== undefined) { |
| 539 |
boundedValue = Math.max(this.model.get('min'), boundedValue); |
| 540 |
} |
| 541 |
if (boundedValue !== numericalValue) { |
| 542 |
target.value = boundedValue; |
| 543 |
numericalValue = boundedValue; |
| 544 |
} |
| 545 |
// Apply the value if it has changed. |
| 546 |
if (numericalValue !== this.model.get('value')) { |
| 547 |
this.model.set('value', numericalValue, { updated_view: this }); |
| 548 |
this.touch(); |
| 549 |
} |
| 550 |
} |
| 551 |
} |
| 552 |
} |
| 553 |
export class ProgressStyleModel extends DescriptionStyleModel { |
| 554 |
defaults() { |
| 555 |
return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'ProgressStyleModel' }); |
| 556 |
} |
| 557 |
} |
| 558 |
ProgressStyleModel.styleProperties = Object.assign(Object.assign({}, DescriptionStyleModel.styleProperties), { bar_color: { |
| 559 |
selector: '.progress-bar', |
| 560 |
attribute: 'background-color', |
| 561 |
default: null, |
| 562 |
} }); |
| 563 |
export class IntProgressModel extends BoundedIntModel { |
| 564 |
defaults() { |
| 565 |
return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'IntProgressModel', _view_name: 'ProgressView', orientation: 'horizontal', bar_style: '', style: null }); |
| 566 |
} |
| 567 |
} |
| 568 |
export class ProgressView extends DescriptionView { |
| 569 |
initialize(parameters) { |
| 570 |
super.initialize(parameters); |
| 571 |
this.listenTo(this.model, 'change:bar_style', this.update_bar_style); |
| 572 |
this.luminoWidget.addClass('jupyter-widgets'); |
| 573 |
} |
| 574 |
render() { |
| 575 |
super.render(); |
| 576 |
const orientation = this.model.get('orientation'); |
| 577 |
const className = orientation === 'horizontal' ? 'widget-hprogress' : 'widget-vprogress'; |
| 578 |
this.el.classList.add(className); |
| 579 |
this.progress = document.createElement('div'); |
| 580 |
this.progress.classList.add('progress'); |
| 581 |
this.progress.style.position = 'relative'; |
| 582 |
this.el.appendChild(this.progress); |
| 583 |
this.bar = document.createElement('div'); |
| 584 |
this.bar.classList.add('progress-bar'); |
| 585 |
this.bar.style.position = 'absolute'; |
| 586 |
this.bar.style.bottom = '0px'; |
| 587 |
this.bar.style.left = '0px'; |
| 588 |
this.progress.appendChild(this.bar); |
| 589 |
// Set defaults. |
| 590 |
this.update(); |
| 591 |
this.set_bar_style(); |
| 592 |
} |
| 593 |
/** |
| 594 |
* Update the contents of this view |
| 595 |
* |
| 596 |
* Called when the model is changed. The model may have been |
| 597 |
* changed by another view or by a state update from the back-end. |
| 598 |
*/ |
| 599 |
update() { |
| 600 |
const value = this.model.get('value'); |
| 601 |
const max = this.model.get('max'); |
| 602 |
const min = this.model.get('min'); |
| 603 |
const orientation = this.model.get('orientation'); |
| 604 |
const percent = (100.0 * (value - min)) / (max - min); |
| 605 |
if (orientation === 'horizontal') { |
| 606 |
this.el.classList.remove('widget-inline-vbox'); |
| 607 |
this.el.classList.remove('widget-vprogress'); |
| 608 |
this.el.classList.add('widget-inline-hbox'); |
| 609 |
this.el.classList.add('widget-hprogress'); |
| 610 |
this.bar.style.width = percent + '%'; |
| 611 |
this.bar.style.height = '100%'; |
| 612 |
} |
| 613 |
else { |
| 614 |
this.el.classList.remove('widget-inline-hbox'); |
| 615 |
this.el.classList.remove('widget-hprogress'); |
| 616 |
this.el.classList.add('widget-inline-vbox'); |
| 617 |
this.el.classList.add('widget-vprogress'); |
| 618 |
this.bar.style.width = '100%'; |
| 619 |
this.bar.style.height = percent + '%'; |
| 620 |
} |
| 621 |
return super.update(); |
| 622 |
} |
| 623 |
update_bar_style() { |
| 624 |
this.update_mapped_classes(ProgressView.class_map, 'bar_style', this.bar); |
| 625 |
} |
| 626 |
set_bar_style() { |
| 627 |
this.set_mapped_classes(ProgressView.class_map, 'bar_style', this.bar); |
| 628 |
} |
| 629 |
} |
| 630 |
ProgressView.class_map = { |
| 631 |
success: ['progress-bar-success'], |
| 632 |
info: ['progress-bar-info'], |
| 633 |
warning: ['progress-bar-warning'], |
| 634 |
danger: ['progress-bar-danger'], |
| 635 |
}; |
| 636 |
export class PlayModel extends BoundedIntModel { |
| 637 |
defaults() { |
| 638 |
return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'PlayModel', _view_name: 'PlayView', repeat: false, playing: false, show_repeat: true, interval: 100, step: 1, disabled: false }); |
| 639 |
} |
| 640 |
initialize(attributes, options) { |
| 641 |
super.initialize(attributes, options); |
| 642 |
} |
| 643 |
loop() { |
| 644 |
if (!this.get('playing')) { |
| 645 |
return; |
| 646 |
} |
| 647 |
const next_value = this.get('value') + this.get('step'); |
| 648 |
if (next_value <= this.get('max')) { |
| 649 |
this.set('value', next_value); |
| 650 |
this.schedule_next(); |
| 651 |
} |
| 652 |
else { |
| 653 |
if (this.get('repeat')) { |
| 654 |
this.set('value', this.get('min')); |
| 655 |
this.schedule_next(); |
| 656 |
} |
| 657 |
else { |
| 658 |
this.pause(); |
| 659 |
} |
| 660 |
} |
| 661 |
this.save_changes(); |
| 662 |
} |
| 663 |
schedule_next() { |
| 664 |
this._timerId = window.setTimeout(this.loop.bind(this), this.get('interval')); |
| 665 |
} |
| 666 |
stop() { |
| 667 |
this.pause(); |
| 668 |
this.set('value', this.get('min')); |
| 669 |
this.save_changes(); |
| 670 |
} |
| 671 |
pause() { |
| 672 |
window.clearTimeout(this._timerId); |
| 673 |
this._timerId = undefined; |
| 674 |
this.set('playing', false); |
| 675 |
this.save_changes(); |
| 676 |
} |
| 677 |
animate() { |
| 678 |
if (this._timerId !== undefined) { |
| 679 |
return; |
| 680 |
} |
| 681 |
if (this.get('value') === this.get('max')) { |
| 682 |
// if the value is at the end, reset it first, and then schedule the next |
| 683 |
this.set('value', this.get('min')); |
| 684 |
this.schedule_next(); |
| 685 |
this.save_changes(); |
| 686 |
} |
| 687 |
else { |
| 688 |
// otherwise directly start with the next value |
| 689 |
// loop will call save_changes in this case |
| 690 |
this.loop(); |
| 691 |
} |
| 692 |
this.save_changes(); |
| 693 |
} |
| 694 |
play() { |
| 695 |
this.set('playing', !this.get('playing')); |
| 696 |
this.save_changes(); |
| 697 |
} |
| 698 |
repeat() { |
| 699 |
this.set('repeat', !this.get('repeat')); |
| 700 |
this.save_changes(); |
| 701 |
} |
| 702 |
} |
| 703 |
export class PlayView extends DOMWidgetView { |
| 704 |
render() { |
| 705 |
super.render(); |
| 706 |
this.el.classList.add('jupyter-widgets'); |
| 707 |
this.el.classList.add('widget-inline-hbox'); |
| 708 |
this.el.classList.add('widget-play'); |
| 709 |
this.playPauseButton = document.createElement('button'); |
| 710 |
this.stopButton = document.createElement('button'); |
| 711 |
this.repeatButton = document.createElement('button'); |
| 712 |
this.playPauseButton.className = 'jupyter-button'; |
| 713 |
this.stopButton.className = 'jupyter-button'; |
| 714 |
this.repeatButton.className = 'jupyter-button'; |
| 715 |
this.el.appendChild(this.playPauseButton); // Toggle button with playing |
| 716 |
this.el.appendChild(this.stopButton); // Disable if not playing |
| 717 |
this.el.appendChild(this.repeatButton); // Always enabled, but may be hidden |
| 718 |
const playIcon = document.createElement('i'); |
| 719 |
playIcon.className = 'fa fa-play'; |
| 720 |
this.playPauseButton.appendChild(playIcon); |
| 721 |
const stopIcon = document.createElement('i'); |
| 722 |
stopIcon.className = 'fa fa-stop'; |
| 723 |
this.stopButton.appendChild(stopIcon); |
| 724 |
const repeatIcon = document.createElement('i'); |
| 725 |
repeatIcon.className = 'fa fa-retweet'; |
| 726 |
this.repeatButton.appendChild(repeatIcon); |
| 727 |
this.playPauseButton.onclick = this.model.play.bind(this.model); |
| 728 |
this.stopButton.onclick = this.model.stop.bind(this.model); |
| 729 |
this.repeatButton.onclick = this.model.repeat.bind(this.model); |
| 730 |
this.listenTo(this.model, 'change:playing', this.onPlayingChanged); |
| 731 |
this.listenTo(this.model, 'change:repeat', this.updateRepeat); |
| 732 |
this.listenTo(this.model, 'change:show_repeat', this.updateRepeat); |
| 733 |
this.updatePlaying(); |
| 734 |
this.updateRepeat(); |
| 735 |
this.update(); |
| 736 |
} |
| 737 |
update() { |
| 738 |
const disabled = this.model.get('disabled'); |
| 739 |
this.playPauseButton.disabled = disabled; |
| 740 |
this.stopButton.disabled = disabled; |
| 741 |
this.repeatButton.disabled = disabled; |
| 742 |
this.updatePlaying(); |
| 743 |
} |
| 744 |
onPlayingChanged() { |
| 745 |
this.updatePlaying(); |
| 746 |
const previous = this.model.previous('playing'); |
| 747 |
const current = this.model.get('playing'); |
| 748 |
if (!previous && current) { |
| 749 |
this.model.animate(); |
| 750 |
} |
| 751 |
else { |
| 752 |
this.model.pause(); |
| 753 |
} |
| 754 |
} |
| 755 |
updatePlaying() { |
| 756 |
const playing = this.model.get('playing'); |
| 757 |
const icon = this.playPauseButton.getElementsByTagName('i')[0]; |
| 758 |
if (playing) { |
| 759 |
icon.className = 'fa fa-pause'; |
| 760 |
} |
| 761 |
else { |
| 762 |
icon.className = 'fa fa-play'; |
| 763 |
} |
| 764 |
} |
| 765 |
updateRepeat() { |
| 766 |
const repeat = this.model.get('repeat'); |
| 767 |
this.repeatButton.style.display = this.model.get('show_repeat') |
| 768 |
? this.playPauseButton.style.display |
| 769 |
: 'none'; |
| 770 |
if (repeat) { |
| 771 |
this.repeatButton.classList.add('mod-active'); |
| 772 |
} |
| 773 |
else { |
| 774 |
this.repeatButton.classList.remove('mod-active'); |
| 775 |
} |
| 776 |
} |
| 777 |
} |
| 778 |
//# sourceMappingURL=widget_int.js.map |