| 1 |
// Copyright (c) Jupyter Development Team. |
| 2 |
// Distributed under the terms of the Modified BSD License. |
| 3 |
import { CoreDescriptionModel } from './widget_core'; |
| 4 |
import { BaseIntSliderView, IntRangeSliderView, IntSliderView, IntTextView, } from './widget_int'; |
| 5 |
import { format } from 'd3-format'; |
| 6 |
import noUiSlider from 'nouislider'; |
| 7 |
export class FloatModel extends CoreDescriptionModel { |
| 8 |
defaults() { |
| 9 |
return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'FloatModel', value: 0 }); |
| 10 |
} |
| 11 |
} |
| 12 |
export class BoundedFloatModel extends FloatModel { |
| 13 |
defaults() { |
| 14 |
return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'BoundedFloatModel', max: 100.0, min: 0.0 }); |
| 15 |
} |
| 16 |
} |
| 17 |
export class FloatSliderModel extends BoundedFloatModel { |
| 18 |
defaults() { |
| 19 |
return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'FloatSliderModel', _view_name: 'FloatSliderView', step: 1.0, orientation: 'horizontal', _range: false, readout: true, readout_format: '.2f', slider_color: null, continuous_update: true, disabled: false }); |
| 20 |
} |
| 21 |
initialize(attributes, options) { |
| 22 |
super.initialize(attributes, options); |
| 23 |
this.on('change:readout_format', this.update_readout_format, this); |
| 24 |
this.update_readout_format(); |
| 25 |
} |
| 26 |
update_readout_format() { |
| 27 |
this.readout_formatter = format(this.get('readout_format')); |
| 28 |
} |
| 29 |
} |
| 30 |
export class FloatLogSliderModel extends BoundedFloatModel { |
| 31 |
defaults() { |
| 32 |
return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'FloatLogSliderModel', _view_name: 'FloatLogSliderView', step: 0.1, orientation: 'horizontal', _range: false, readout: true, readout_format: '.3g', slider_color: null, continuous_update: true, disabled: false, base: 10, value: 1.0, min: 0, max: 4 }); |
| 33 |
} |
| 34 |
initialize(attributes, options) { |
| 35 |
super.initialize(attributes, options); |
| 36 |
this.on('change:readout_format', this.update_readout_format, this); |
| 37 |
this.update_readout_format(); |
| 38 |
} |
| 39 |
update_readout_format() { |
| 40 |
this.readout_formatter = format(this.get('readout_format')); |
| 41 |
} |
| 42 |
} |
| 43 |
export class FloatRangeSliderModel extends FloatSliderModel { |
| 44 |
} |
| 45 |
export class FloatSliderView extends IntSliderView { |
| 46 |
constructor() { |
| 47 |
super(...arguments); |
| 48 |
this._parse_value = parseFloat; |
| 49 |
} |
| 50 |
/** |
| 51 |
* Validate the value of the slider before sending it to the back-end |
| 52 |
* and applying it to the other views on the page. |
| 53 |
*/ |
| 54 |
_validate_slide_value(x) { |
| 55 |
return x; |
| 56 |
} |
| 57 |
} |
| 58 |
export class FloatLogSliderView extends BaseIntSliderView { |
| 59 |
constructor() { |
| 60 |
super(...arguments); |
| 61 |
this._parse_value = parseFloat; |
| 62 |
} |
| 63 |
update(options) { |
| 64 |
super.update(options); |
| 65 |
const value = this.model.get('value'); |
| 66 |
this.readout.textContent = this.valueToString(value); |
| 67 |
} |
| 68 |
/** |
| 69 |
* Convert from value to exponent |
| 70 |
* |
| 71 |
* @param value the widget value |
| 72 |
* @returns the log-value between the min/max exponents |
| 73 |
*/ |
| 74 |
logCalc(value) { |
| 75 |
const min = this.model.get('min'); |
| 76 |
const max = this.model.get('max'); |
| 77 |
const base = this.model.get('base'); |
| 78 |
let log_value = Math.log(value) / Math.log(base); |
| 79 |
if (log_value > max) { |
| 80 |
log_value = max; |
| 81 |
} |
| 82 |
else if (log_value < min) { |
| 83 |
log_value = min; |
| 84 |
} |
| 85 |
return log_value; |
| 86 |
} |
| 87 |
createSlider() { |
| 88 |
var _a; |
| 89 |
const orientation = this.model.get('orientation'); |
| 90 |
const behavior = this.model.get('behavior'); |
| 91 |
noUiSlider.create(this.$slider, { |
| 92 |
start: this.logCalc(this.model.get('value')), |
| 93 |
behaviour: behavior, |
| 94 |
range: { |
| 95 |
min: this.model.get('min'), |
| 96 |
max: this.model.get('max'), |
| 97 |
}, |
| 98 |
step: (_a = this.model.get('step')) !== null && _a !== void 0 ? _a : undefined, |
| 99 |
animate: false, |
| 100 |
orientation: orientation, |
| 101 |
direction: orientation === 'horizontal' ? 'ltr' : 'rtl', |
| 102 |
format: { |
| 103 |
from: (value) => Number(value), |
| 104 |
to: (value) => value, |
| 105 |
}, |
| 106 |
}); |
| 107 |
// Using noUiSlider's 'update' and 'change' events. |
| 108 |
// See reference: https://refreshless.com/nouislider/events-callbacks/ |
| 109 |
this.$slider.noUiSlider.on('update', (values, handle) => { |
| 110 |
this.handleSliderUpdateEvent(values, handle); |
| 111 |
}); |
| 112 |
this.$slider.noUiSlider.on('change', (values, handle) => { |
| 113 |
this.handleSliderChangeEvent(values, handle); |
| 114 |
}); |
| 115 |
} |
| 116 |
/** |
| 117 |
* Write value to a string |
| 118 |
*/ |
| 119 |
valueToString(value) { |
| 120 |
const format = this.model.readout_formatter; |
| 121 |
return format(value); |
| 122 |
} |
| 123 |
/** |
| 124 |
* Parse value from a string |
| 125 |
*/ |
| 126 |
stringToValue(text) { |
| 127 |
return text === null ? NaN : this._parse_value(text); |
| 128 |
} |
| 129 |
/** |
| 130 |
* this handles the entry of text into the contentEditable label first, the |
| 131 |
* value is checked if it contains a parseable value then it is clamped |
| 132 |
* within the min-max range of the slider finally, the model is updated if |
| 133 |
* the value is to be changed |
| 134 |
* |
| 135 |
* if any of these conditions are not met, the text is reset |
| 136 |
*/ |
| 137 |
handleTextChange() { |
| 138 |
let value = this.stringToValue(this.readout.textContent); |
| 139 |
const vmin = this.model.get('min'); |
| 140 |
const vmax = this.model.get('max'); |
| 141 |
const base = this.model.get('base'); |
| 142 |
if (isNaN(value)) { |
| 143 |
this.readout.textContent = this.valueToString(this.model.get('value')); |
| 144 |
} |
| 145 |
else { |
| 146 |
value = Math.max(Math.min(value, Math.pow(base, vmax)), Math.pow(base, vmin)); |
| 147 |
if (value !== this.model.get('value')) { |
| 148 |
this.readout.textContent = this.valueToString(value); |
| 149 |
this.model.set('value', value); |
| 150 |
this.touch(); |
| 151 |
} |
| 152 |
else { |
| 153 |
this.readout.textContent = this.valueToString(this.model.get('value')); |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
/** |
| 158 |
* Called whilst the slider is dragged, tapped or moved by the arrow keys. |
| 159 |
*/ |
| 160 |
handleSliderUpdateEvent(values, handle) { |
| 161 |
const base = this.model.get('base'); |
| 162 |
const actual_value = Math.pow(base, this._validate_slide_value(values[0])); |
| 163 |
this.readout.textContent = this.valueToString(actual_value); |
| 164 |
// Only persist the value while sliding if the continuous_update |
| 165 |
// trait is set to true. |
| 166 |
if (this.model.get('continuous_update')) { |
| 167 |
this.handleSliderChanged(values, handle); |
| 168 |
} |
| 169 |
} |
| 170 |
/** |
| 171 |
* Called when the slider handle is released after dragging, |
| 172 |
* or by tapping or moving by the arrow keys. |
| 173 |
*/ |
| 174 |
handleSliderChangeEvent(values, handle) { |
| 175 |
const base = this.model.get('base'); |
| 176 |
const actual_value = Math.pow(base, this._validate_slide_value(values[0])); |
| 177 |
this.readout.textContent = this.valueToString(actual_value); |
| 178 |
this.handleSliderChanged(values, handle); |
| 179 |
} |
| 180 |
/** |
| 181 |
* Called when the slider value has changed. |
| 182 |
* |
| 183 |
* Calling model.set will trigger all of the other views of the |
| 184 |
* model to update. |
| 185 |
*/ |
| 186 |
handleSliderChanged(values, handle) { |
| 187 |
if (this._updating_slider) { |
| 188 |
return; |
| 189 |
} |
| 190 |
const base = this.model.get('base'); |
| 191 |
const actual_value = Math.pow(base, this._validate_slide_value(values[0])); |
| 192 |
this.model.set('value', actual_value, { updated_view: this }); |
| 193 |
this.touch(); |
| 194 |
} |
| 195 |
updateSliderValue(model, value, options) { |
| 196 |
if (options.updated_view === this) { |
| 197 |
return; |
| 198 |
} |
| 199 |
const log_value = this.logCalc(this.model.get('value')); |
| 200 |
this.$slider.noUiSlider.set(log_value); |
| 201 |
} |
| 202 |
updateSliderOptions(e) { |
| 203 |
this.$slider.noUiSlider.updateOptions({ |
| 204 |
start: this.logCalc(this.model.get('value')), |
| 205 |
range: { |
| 206 |
min: this.model.get('min'), |
| 207 |
max: this.model.get('max'), |
| 208 |
}, |
| 209 |
step: this.model.get('step'), |
| 210 |
}); |
| 211 |
} |
| 212 |
_validate_slide_value(x) { |
| 213 |
return x; |
| 214 |
} |
| 215 |
} |
| 216 |
export class FloatRangeSliderView extends IntRangeSliderView { |
| 217 |
constructor() { |
| 218 |
super(...arguments); |
| 219 |
this._parse_value = parseFloat; |
| 220 |
// matches: whitespace?, float, whitespace?, (hyphen, colon, or en-dash), whitespace?, float |
| 221 |
this._range_regex = /^\s*([+-]?(?:\d*\.?\d+|\d+\.)(?:[eE][-:]?\d+)?)\s*[-:–]\s*([+-]?(?:\d*\.?\d+|\d+\.)(?:[eE][+-]?\d+)?)/; |
| 222 |
} |
| 223 |
/** |
| 224 |
* Validate the value of the slider before sending it to the back-end |
| 225 |
* and applying it to the other views on the page. |
| 226 |
*/ |
| 227 |
_validate_slide_value(x) { |
| 228 |
return x; |
| 229 |
} |
| 230 |
} |
| 231 |
export class FloatTextModel extends FloatModel { |
| 232 |
defaults() { |
| 233 |
return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'FloatTextModel', _view_name: 'FloatTextView', disabled: false, continuous_update: false }); |
| 234 |
} |
| 235 |
} |
| 236 |
export class BoundedFloatTextModel extends BoundedFloatModel { |
| 237 |
defaults() { |
| 238 |
return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'BoundedFloatTextModel', _view_name: 'FloatTextView', disabled: false, continuous_update: false, step: 0.1 }); |
| 239 |
} |
| 240 |
} |
| 241 |
export class FloatTextView extends IntTextView { |
| 242 |
constructor() { |
| 243 |
super(...arguments); |
| 244 |
this._parse_value = parseFloat; |
| 245 |
this._default_step = 'any'; |
| 246 |
} |
| 247 |
/** |
| 248 |
* Handle key press |
| 249 |
*/ |
| 250 |
handleKeypress(e) { |
| 251 |
// Overwrite IntTextView's handleKeypress |
| 252 |
// which prevents decimal points. |
| 253 |
e.stopPropagation(); |
| 254 |
} |
| 255 |
/** |
| 256 |
* Handle key up |
| 257 |
*/ |
| 258 |
handleKeyUp(e) { |
| 259 |
// Overwrite IntTextView's handleKeyUp |
| 260 |
// which prevents decimal points. |
| 261 |
} |
| 262 |
} |
| 263 |
export class FloatProgressModel extends BoundedFloatModel { |
| 264 |
defaults() { |
| 265 |
return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'FloatProgressModel', _view_name: 'ProgressView', orientation: 'horizontal', bar_style: '', style: null }); |
| 266 |
} |
| 267 |
} |
| 268 |
//# sourceMappingURL=widget_float.js.map |