| 1 |
// Copyright (c) Jupyter Development Team. |
| 2 |
// Distributed under the terms of the Modified BSD License. |
| 3 |
import * as d3Color from 'd3-color'; |
| 4 |
import * as d3Format from 'd3-format'; |
| 5 |
import { CoreDOMWidgetModel } from './widget_core'; |
| 6 |
import { DOMWidgetView, uuid } from '@jupyter-widgets/base'; |
| 7 |
/** |
| 8 |
* Returns a new string after removing any leading and trailing whitespaces. |
| 9 |
* The original string is left unchanged. |
| 10 |
*/ |
| 11 |
function trim(value) { |
| 12 |
return value.replace(/^\s+|\s+$/g, ''); |
| 13 |
} |
| 14 |
/** |
| 15 |
* Clamp a number between min and max and return the result. |
| 16 |
*/ |
| 17 |
function clamp(value, min, max) { |
| 18 |
return Math.min(Math.max(value, min), max); |
| 19 |
} |
| 20 |
/** |
| 21 |
* Remove children from an HTMLElement |
| 22 |
*/ |
| 23 |
function removeChildren(el) { |
| 24 |
while (el.firstChild) { |
| 25 |
el.removeChild(el.firstChild); |
| 26 |
} |
| 27 |
} |
| 28 |
/** |
| 29 |
* Selection class which keeps track on selected indices. |
| 30 |
*/ |
| 31 |
class Selection { |
| 32 |
constructor(start, dx, max) { |
| 33 |
this.start = start; |
| 34 |
this.dx = dx; |
| 35 |
this.max = max; |
| 36 |
} |
| 37 |
/** |
| 38 |
* Check if a given index is currently selected. |
| 39 |
*/ |
| 40 |
isSelected(index) { |
| 41 |
let min; |
| 42 |
let max; |
| 43 |
if (this.dx >= 0) { |
| 44 |
min = this.start; |
| 45 |
max = this.start + this.dx; |
| 46 |
} |
| 47 |
else { |
| 48 |
min = this.start + this.dx; |
| 49 |
max = this.start; |
| 50 |
} |
| 51 |
return min <= index && index < max; |
| 52 |
} |
| 53 |
/** |
| 54 |
* Update selection |
| 55 |
*/ |
| 56 |
updateSelection(dx) { |
| 57 |
this.dx += dx; |
| 58 |
if (this.start + this.dx > this.max) { |
| 59 |
this.dx = this.max - this.start; |
| 60 |
} |
| 61 |
if (this.start + this.dx < 0) { |
| 62 |
this.dx = -this.start; |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
class TagsInputBaseModel extends CoreDOMWidgetModel { |
| 67 |
defaults() { |
| 68 |
return Object.assign(Object.assign({}, super.defaults()), { value: [], placeholder: '\u200b', allowed_tags: null, allow_duplicates: true }); |
| 69 |
} |
| 70 |
} |
| 71 |
class TagsInputBaseView extends DOMWidgetView { |
| 72 |
constructor() { |
| 73 |
super(...arguments); |
| 74 |
this.hoveredTag = null; |
| 75 |
this.hoveredTagIndex = null; |
| 76 |
} |
| 77 |
/** |
| 78 |
* Called when view is rendered. |
| 79 |
*/ |
| 80 |
render() { |
| 81 |
super.render(); |
| 82 |
this.el.classList.add('jupyter-widgets'); |
| 83 |
this.el.classList.add('jupyter-widget-tagsinput'); |
| 84 |
this.taginputWrapper = document.createElement('div'); |
| 85 |
// The taginput is hidden until the user focuses on the widget |
| 86 |
// Unless there is no value |
| 87 |
if (this.model.get('value').length) { |
| 88 |
this.taginputWrapper.style.display = 'none'; |
| 89 |
} |
| 90 |
else { |
| 91 |
this.taginputWrapper.style.display = 'inline-block'; |
| 92 |
} |
| 93 |
this.datalistID = uuid(); |
| 94 |
this.taginput = document.createElement('input'); |
| 95 |
this.taginput.classList.add('jupyter-widget-tag'); |
| 96 |
this.taginput.classList.add('jupyter-widget-taginput'); |
| 97 |
this.taginput.setAttribute('list', this.datalistID); |
| 98 |
this.taginput.setAttribute('type', 'text'); |
| 99 |
this.autocompleteList = document.createElement('datalist'); |
| 100 |
this.autocompleteList.id = this.datalistID; |
| 101 |
this.updateAutocomplete(); |
| 102 |
this.model.on('change:allowed_tags', this.updateAutocomplete.bind(this)); |
| 103 |
this.updatePlaceholder(); |
| 104 |
this.model.on('change:placeholder', this.updatePlaceholder.bind(this)); |
| 105 |
this.taginputWrapper.classList.add('widget-text'); |
| 106 |
this.taginputWrapper.appendChild(this.taginput); |
| 107 |
this.taginputWrapper.appendChild(this.autocompleteList); |
| 108 |
this.el.onclick = this.focus.bind(this); |
| 109 |
this.el.ondrop = (event) => { |
| 110 |
// Put the tag at the end of the list if there is no currently hovered tag |
| 111 |
const index = this.hoveredTagIndex == null ? this.tags.length : this.hoveredTagIndex; |
| 112 |
return this.ondrop(event, index); |
| 113 |
}; |
| 114 |
this.el.ondragover = this.ondragover.bind(this); |
| 115 |
this.taginput.onchange = this.handleValueAdded.bind(this); |
| 116 |
this.taginput.oninput = this.resizeInput.bind(this); |
| 117 |
this.taginput.onkeydown = this.handleKeyEvent.bind(this); |
| 118 |
this.taginput.onblur = this.loseFocus.bind(this); |
| 119 |
this.resizeInput(); |
| 120 |
this.inputIndex = this.model.get('value').length; |
| 121 |
this.selection = null; |
| 122 |
this.preventLoosingFocus = false; |
| 123 |
this.update(); |
| 124 |
} |
| 125 |
/** |
| 126 |
* Update the contents of this view |
| 127 |
* |
| 128 |
* Called when the model is changed. The model may have been |
| 129 |
* changed by another view or by a state update from the back-end. |
| 130 |
*/ |
| 131 |
update() { |
| 132 |
// Prevent hiding the input element and clearing the selection when updating everything |
| 133 |
this.preventLoosingFocus = true; |
| 134 |
removeChildren(this.el); |
| 135 |
this.tags = []; |
| 136 |
const value = this.model.get('value'); |
| 137 |
this.inputIndex = value.length; |
| 138 |
for (const idx in value) { |
| 139 |
const index = parseInt(idx); |
| 140 |
const tag = this.createTag(value[index], index, this.selection != null && this.selection.isSelected(index)); |
| 141 |
// Drag and drop |
| 142 |
tag.draggable = true; |
| 143 |
tag.ondragstart = ((index, value) => { |
| 144 |
return (event) => { |
| 145 |
this.ondragstart(event, index, value, this.model.model_id); |
| 146 |
}; |
| 147 |
})(index, value[index]); |
| 148 |
tag.ondrop = ((index) => { |
| 149 |
return (event) => { |
| 150 |
this.ondrop(event, index); |
| 151 |
}; |
| 152 |
})(index); |
| 153 |
tag.ondragover = this.ondragover.bind(this); |
| 154 |
tag.ondragenter = ((index) => { |
| 155 |
return (event) => { |
| 156 |
this.ondragenter(event, index); |
| 157 |
}; |
| 158 |
})(index); |
| 159 |
tag.ondragend = this.ondragend.bind(this); |
| 160 |
this.tags.push(tag); |
| 161 |
this.el.appendChild(tag); |
| 162 |
} |
| 163 |
this.el.insertBefore(this.taginputWrapper, this.el.children[this.inputIndex]); |
| 164 |
// The taginput is hidden until the user focuses on the widget |
| 165 |
// Unless there is no value |
| 166 |
if (this.model.get('value').length) { |
| 167 |
this.taginputWrapper.style.display = 'none'; |
| 168 |
} |
| 169 |
else { |
| 170 |
this.taginputWrapper.style.display = 'inline-block'; |
| 171 |
} |
| 172 |
this.preventLoosingFocus = false; |
| 173 |
return super.update(); |
| 174 |
} |
| 175 |
/** |
| 176 |
* Update the auto-completion list |
| 177 |
*/ |
| 178 |
updateAutocomplete() { |
| 179 |
removeChildren(this.autocompleteList); |
| 180 |
const allowedTags = this.model.get('allowed_tags'); |
| 181 |
for (const tag of allowedTags) { |
| 182 |
const option = document.createElement('option'); |
| 183 |
option.value = tag; |
| 184 |
this.autocompleteList.appendChild(option); |
| 185 |
} |
| 186 |
} |
| 187 |
/** |
| 188 |
* Update the auto-completion list |
| 189 |
*/ |
| 190 |
updatePlaceholder() { |
| 191 |
this.taginput.placeholder = this.model.get('placeholder'); |
| 192 |
this.resizeInput(); |
| 193 |
} |
| 194 |
/** |
| 195 |
* Update the tags, called when the selection has changed and we need to update the tags CSS |
| 196 |
*/ |
| 197 |
updateTags() { |
| 198 |
const value = this.model.get('value'); |
| 199 |
for (const idx in this.tags) { |
| 200 |
const index = parseInt(idx); |
| 201 |
this.updateTag(this.tags[index], value[index], index, this.selection != null && this.selection.isSelected(index)); |
| 202 |
} |
| 203 |
} |
| 204 |
/** |
| 205 |
* Handle a new value is added from the input element |
| 206 |
*/ |
| 207 |
handleValueAdded(event) { |
| 208 |
const newTagValue = trim(this.taginput.value); |
| 209 |
const tagIndex = this.inputIndex; |
| 210 |
if (newTagValue == '') { |
| 211 |
return; |
| 212 |
} |
| 213 |
this.inputIndex++; |
| 214 |
const tagAdded = this.addTag(tagIndex, newTagValue); |
| 215 |
if (tagAdded) { |
| 216 |
// Clear the input and keep focus on it allowing the user to add more tags |
| 217 |
this.taginput.value = ''; |
| 218 |
this.resizeInput(); |
| 219 |
this.focus(); |
| 220 |
} |
| 221 |
} |
| 222 |
/** |
| 223 |
* Add a new tag with a value of `tagValue` at the `index` position |
| 224 |
* Return true if the tag was correctly added, false otherwise |
| 225 |
*/ |
| 226 |
addTag(index, tagValue) { |
| 227 |
const value = this.model.get('value'); |
| 228 |
let newTagValue; |
| 229 |
try { |
| 230 |
newTagValue = this.validateValue(tagValue); |
| 231 |
} |
| 232 |
catch (error) { |
| 233 |
return false; |
| 234 |
} |
| 235 |
const allowedTagValues = this.model.get('allowed_tags'); |
| 236 |
if (allowedTagValues.length && !allowedTagValues.includes(newTagValue)) { |
| 237 |
// Do nothing for now, maybe show a proper error message? |
| 238 |
return false; |
| 239 |
} |
| 240 |
if (!this.model.get('allow_duplicates') && value.includes(newTagValue)) { |
| 241 |
// Do nothing for now, maybe add an animation to highlight the tag? |
| 242 |
return false; |
| 243 |
} |
| 244 |
// Clearing the current selection before setting the new value |
| 245 |
this.selection = null; |
| 246 |
// Making a copy so that backbone sees the change, and insert the new tag |
| 247 |
const newValue = [...value]; |
| 248 |
newValue.splice(index, 0, newTagValue); |
| 249 |
this.model.set('value', newValue); |
| 250 |
this.model.save_changes(); |
| 251 |
return true; |
| 252 |
} |
| 253 |
/** |
| 254 |
* Resize the input element |
| 255 |
*/ |
| 256 |
resizeInput() { |
| 257 |
let content; |
| 258 |
if (this.taginput.value.length != 0) { |
| 259 |
content = this.taginput.value; |
| 260 |
} |
| 261 |
else { |
| 262 |
content = this.model.get('placeholder'); |
| 263 |
} |
| 264 |
const size = content.length + 1; |
| 265 |
this.taginput.setAttribute('size', String(size)); |
| 266 |
} |
| 267 |
/** |
| 268 |
* Handle key events on the input element |
| 269 |
*/ |
| 270 |
handleKeyEvent(event) { |
| 271 |
const valueLength = this.model.get('value').length; |
| 272 |
// Do nothing if the user is typing something |
| 273 |
if (this.taginput.value.length) { |
| 274 |
return; |
| 275 |
} |
| 276 |
const currentElement = this.inputIndex; |
| 277 |
switch (event.key) { |
| 278 |
case 'ArrowLeft': |
| 279 |
if (event.ctrlKey && event.shiftKey) { |
| 280 |
this.select(currentElement, -currentElement); |
| 281 |
} |
| 282 |
if (!event.ctrlKey && event.shiftKey) { |
| 283 |
this.select(currentElement, -1); |
| 284 |
} |
| 285 |
if (event.ctrlKey) { |
| 286 |
this.inputIndex = 0; |
| 287 |
} |
| 288 |
else { |
| 289 |
this.inputIndex--; |
| 290 |
} |
| 291 |
break; |
| 292 |
case 'ArrowRight': |
| 293 |
if (event.ctrlKey && event.shiftKey) { |
| 294 |
this.select(currentElement, valueLength - currentElement); |
| 295 |
} |
| 296 |
if (!event.ctrlKey && event.shiftKey) { |
| 297 |
this.select(currentElement, 1); |
| 298 |
} |
| 299 |
if (event.ctrlKey) { |
| 300 |
this.inputIndex = valueLength; |
| 301 |
} |
| 302 |
else { |
| 303 |
this.inputIndex++; |
| 304 |
} |
| 305 |
break; |
| 306 |
case 'Backspace': |
| 307 |
if (this.selection) { |
| 308 |
this.removeSelectedTags(); |
| 309 |
} |
| 310 |
else { |
| 311 |
this.removeTag(this.inputIndex - 1); |
| 312 |
} |
| 313 |
break; |
| 314 |
case 'Delete': |
| 315 |
if (this.selection) { |
| 316 |
this.removeSelectedTags(); |
| 317 |
} |
| 318 |
else { |
| 319 |
this.removeTag(this.inputIndex); |
| 320 |
} |
| 321 |
break; |
| 322 |
default: |
| 323 |
// Do nothing by default |
| 324 |
return; |
| 325 |
break; |
| 326 |
} |
| 327 |
// Reset selection is shift key is not pressed |
| 328 |
if (!event.shiftKey) { |
| 329 |
this.selection = null; |
| 330 |
} |
| 331 |
this.inputIndex = clamp(this.inputIndex, 0, valueLength); |
| 332 |
this.update(); |
| 333 |
this.focus(); |
| 334 |
} |
| 335 |
/** |
| 336 |
* Function that gets called when a tag with a given `value` is being dragged. |
| 337 |
*/ |
| 338 |
ondragstart(event, index, tagValue, origin) { |
| 339 |
if (event.dataTransfer == null) { |
| 340 |
return; |
| 341 |
} |
| 342 |
event.dataTransfer.setData('index', String(index)); |
| 343 |
event.dataTransfer.setData('tagValue', String(tagValue)); |
| 344 |
event.dataTransfer.setData('origin', origin); |
| 345 |
} |
| 346 |
/** |
| 347 |
* Function that gets called when a tag has been dragged on the tag at the `index` position. |
| 348 |
*/ |
| 349 |
ondrop(event, index) { |
| 350 |
if (event.dataTransfer == null) { |
| 351 |
return; |
| 352 |
} |
| 353 |
event.preventDefault(); |
| 354 |
event.stopPropagation(); |
| 355 |
const draggedTagValue = event.dataTransfer.getData('tagValue'); |
| 356 |
const draggedTagindex = parseInt(event.dataTransfer.getData('index')); |
| 357 |
const sameOrigin = event.dataTransfer.getData('origin') == this.model.model_id; |
| 358 |
// If something else than a tag was dropped, draggedTagindex should be NaN |
| 359 |
if (isNaN(draggedTagindex)) { |
| 360 |
return; |
| 361 |
} |
| 362 |
// If it's the same origin, the drag and drop results in a reordering |
| 363 |
if (sameOrigin) { |
| 364 |
const value = this.model.get('value'); |
| 365 |
const newValue = [...value]; |
| 366 |
// If the old position is on the left of the new position, we need to re-index the new position |
| 367 |
// after removing the tag at the old position |
| 368 |
if (draggedTagindex < index) { |
| 369 |
index--; |
| 370 |
} |
| 371 |
newValue.splice(draggedTagindex, 1); // Removing at the old position |
| 372 |
newValue.splice(index, 0, draggedTagValue); // Adding at the new one |
| 373 |
this.model.set('value', newValue); |
| 374 |
this.model.save_changes(); |
| 375 |
return; |
| 376 |
} |
| 377 |
// Else we add a new tag with the given draggedTagValue |
| 378 |
this.addTag(index, draggedTagValue); |
| 379 |
} |
| 380 |
ondragover(event) { |
| 381 |
// This is needed for the drag and drop to work |
| 382 |
event.preventDefault(); |
| 383 |
} |
| 384 |
ondragenter(event, index) { |
| 385 |
if (this.hoveredTag != null && this.hoveredTag != this.tags[index]) { |
| 386 |
this.hoveredTag.style.marginLeft = '1px'; |
| 387 |
} |
| 388 |
this.hoveredTag = this.tags[index]; |
| 389 |
this.hoveredTagIndex = index; |
| 390 |
this.hoveredTag.style.marginLeft = '30px'; |
| 391 |
} |
| 392 |
ondragend() { |
| 393 |
if (this.hoveredTag != null) { |
| 394 |
this.hoveredTag.style.marginLeft = '1px'; |
| 395 |
} |
| 396 |
this.hoveredTag = null; |
| 397 |
this.hoveredTagIndex = null; |
| 398 |
} |
| 399 |
/** |
| 400 |
* Select tags from `start` to `start + dx` not included. |
| 401 |
*/ |
| 402 |
select(start, dx) { |
| 403 |
const valueLength = this.model.get('value').length; |
| 404 |
if (!this.selection) { |
| 405 |
this.selection = new Selection(start, dx, valueLength); |
| 406 |
} |
| 407 |
else { |
| 408 |
this.selection.updateSelection(dx); |
| 409 |
} |
| 410 |
} |
| 411 |
/** |
| 412 |
* Remove all the selected tags. |
| 413 |
*/ |
| 414 |
removeSelectedTags() { |
| 415 |
const value = [...this.model.get('value')]; |
| 416 |
const valueLength = value.length; |
| 417 |
// It is simpler to remove from right to left |
| 418 |
for (let idx = valueLength - 1; idx >= 0; idx--) { |
| 419 |
if (this.selection != null && this.selection.isSelected(idx)) { |
| 420 |
value.splice(idx, 1); |
| 421 |
// Move the input to the left if we remove a tag that is before the input |
| 422 |
if (idx < this.inputIndex) { |
| 423 |
this.inputIndex--; |
| 424 |
} |
| 425 |
} |
| 426 |
} |
| 427 |
this.model.set('value', value); |
| 428 |
this.model.save_changes(); |
| 429 |
} |
| 430 |
/** |
| 431 |
* Remove a tag given its index in the list |
| 432 |
*/ |
| 433 |
removeTag(tagIndex) { |
| 434 |
const value = [...this.model.get('value')]; |
| 435 |
value.splice(tagIndex, 1); |
| 436 |
// Move the input to the left if we remove a tag that is before the input |
| 437 |
if (tagIndex < this.inputIndex) { |
| 438 |
this.inputIndex--; |
| 439 |
} |
| 440 |
this.model.set('value', value); |
| 441 |
this.model.save_changes(); |
| 442 |
} |
| 443 |
/** |
| 444 |
* Focus on the input element |
| 445 |
*/ |
| 446 |
focus() { |
| 447 |
this.taginputWrapper.style.display = 'inline-block'; |
| 448 |
this.taginput.focus(); |
| 449 |
} |
| 450 |
/** |
| 451 |
* Lose focus on the input element |
| 452 |
*/ |
| 453 |
loseFocus() { |
| 454 |
if (this.preventLoosingFocus) { |
| 455 |
return; |
| 456 |
} |
| 457 |
// Only hide the input if we have tags displayed |
| 458 |
if (this.model.get('value').length) { |
| 459 |
this.taginputWrapper.style.display = 'none'; |
| 460 |
} |
| 461 |
this.selection = null; |
| 462 |
this.updateTags(); |
| 463 |
} |
| 464 |
preinitialize() { |
| 465 |
// Must set this before the initialize method creates the element |
| 466 |
this.tagName = 'div'; |
| 467 |
} |
| 468 |
/** |
| 469 |
* Validate an input tag typed by the user, returning the correct tag type. This should be overridden in subclasses. |
| 470 |
*/ |
| 471 |
validateValue(value) { |
| 472 |
return value; |
| 473 |
} |
| 474 |
} |
| 475 |
export class TagsInputModel extends TagsInputBaseModel { |
| 476 |
defaults() { |
| 477 |
return Object.assign(Object.assign({}, super.defaults()), { value: [], tag_style: '', _view_name: 'TagsInputView', _model_name: 'TagsInputModel' }); |
| 478 |
} |
| 479 |
} |
| 480 |
export class TagsInputView extends TagsInputBaseView { |
| 481 |
/** |
| 482 |
* Create the string tag |
| 483 |
*/ |
| 484 |
createTag(value, index, selected) { |
| 485 |
const tag = document.createElement('div'); |
| 486 |
const style = this.model.get('tag_style'); |
| 487 |
tag.classList.add('jupyter-widget-tag'); |
| 488 |
tag.classList.add(TagsInputView.class_map[style]); |
| 489 |
if (selected) { |
| 490 |
tag.classList.add('mod-active'); |
| 491 |
} |
| 492 |
tag.appendChild(document.createTextNode(this.getTagText(value))); |
| 493 |
const i = document.createElement('i'); |
| 494 |
i.classList.add('fa'); |
| 495 |
i.classList.add('fa-times'); |
| 496 |
i.classList.add('jupyter-widget-tag-close'); |
| 497 |
tag.appendChild(i); |
| 498 |
i.onmousedown = ((index) => { |
| 499 |
return () => { |
| 500 |
this.removeTag(index); |
| 501 |
this.loseFocus(); |
| 502 |
}; |
| 503 |
})(index); |
| 504 |
return tag; |
| 505 |
} |
| 506 |
/** |
| 507 |
* Returns the text that should be displayed in the tag element |
| 508 |
*/ |
| 509 |
getTagText(value) { |
| 510 |
return value; |
| 511 |
} |
| 512 |
/** |
| 513 |
* Update a given tag |
| 514 |
*/ |
| 515 |
updateTag(tag, value, index, selected) { |
| 516 |
if (selected) { |
| 517 |
tag.classList.add('mod-active'); |
| 518 |
} |
| 519 |
else { |
| 520 |
tag.classList.remove('mod-active'); |
| 521 |
} |
| 522 |
} |
| 523 |
} |
| 524 |
TagsInputView.class_map = { |
| 525 |
primary: 'mod-primary', |
| 526 |
success: 'mod-success', |
| 527 |
info: 'mod-info', |
| 528 |
warning: 'mod-warning', |
| 529 |
danger: 'mod-danger', |
| 530 |
}; |
| 531 |
export class ColorsInputModel extends TagsInputBaseModel { |
| 532 |
defaults() { |
| 533 |
return Object.assign(Object.assign({}, super.defaults()), { value: [], _view_name: 'ColorsInputView', _model_name: 'ColorsInputModel' }); |
| 534 |
} |
| 535 |
} |
| 536 |
export class ColorsInputView extends TagsInputBaseView { |
| 537 |
/** |
| 538 |
* Create the Color tag |
| 539 |
*/ |
| 540 |
createTag(value, index, selected) { |
| 541 |
const tag = document.createElement('div'); |
| 542 |
const color = value; |
| 543 |
const darkerColor = d3Color.color(value).darker().toString(); |
| 544 |
tag.classList.add('jupyter-widget-tag'); |
| 545 |
tag.classList.add('jupyter-widget-colortag'); |
| 546 |
if (!selected) { |
| 547 |
tag.style.backgroundColor = color; |
| 548 |
} |
| 549 |
else { |
| 550 |
tag.classList.add('mod-active'); |
| 551 |
tag.style.backgroundColor = darkerColor; |
| 552 |
} |
| 553 |
const i = document.createElement('i'); |
| 554 |
i.classList.add('fa'); |
| 555 |
i.classList.add('fa-times'); |
| 556 |
i.classList.add('jupyter-widget-tag-close'); |
| 557 |
tag.appendChild(i); |
| 558 |
i.onmousedown = ((index) => { |
| 559 |
return () => { |
| 560 |
this.removeTag(index); |
| 561 |
this.loseFocus(); |
| 562 |
}; |
| 563 |
})(index); |
| 564 |
return tag; |
| 565 |
} |
| 566 |
/** |
| 567 |
* Update a given tag |
| 568 |
*/ |
| 569 |
updateTag(tag, value, index, selected) { |
| 570 |
const color = value; |
| 571 |
const darkerColor = d3Color.color(value).darker().toString(); |
| 572 |
if (!selected) { |
| 573 |
tag.classList.remove('mod-active'); |
| 574 |
tag.style.backgroundColor = color; |
| 575 |
} |
| 576 |
else { |
| 577 |
tag.classList.add('mod-active'); |
| 578 |
tag.style.backgroundColor = darkerColor; |
| 579 |
} |
| 580 |
} |
| 581 |
/** |
| 582 |
* Validate an input tag typed by the user, returning the correct tag type. This should be overridden in subclasses. |
| 583 |
*/ |
| 584 |
validateValue(value) { |
| 585 |
if (d3Color.color(value) == null) { |
| 586 |
throw value + ' is not a valid Color'; |
| 587 |
} |
| 588 |
return value; |
| 589 |
} |
| 590 |
} |
| 591 |
class NumbersInputModel extends TagsInputModel { |
| 592 |
defaults() { |
| 593 |
return Object.assign(Object.assign({}, super.defaults()), { min: null, max: null }); |
| 594 |
} |
| 595 |
} |
| 596 |
class NumbersInputView extends TagsInputView { |
| 597 |
render() { |
| 598 |
// Initialize text formatter |
| 599 |
this.model.on('change:format', () => { |
| 600 |
this.formatter = d3Format.format(this.model.get('format')); |
| 601 |
this.update(); |
| 602 |
}); |
| 603 |
this.formatter = d3Format.format(this.model.get('format')); |
| 604 |
super.render(); |
| 605 |
} |
| 606 |
/** |
| 607 |
* Returns the text that should be displayed in the tag element |
| 608 |
*/ |
| 609 |
getTagText(value) { |
| 610 |
return this.formatter(this.parseNumber(value)); |
| 611 |
} |
| 612 |
/** |
| 613 |
* Validate an input tag typed by the user, returning the correct tag type. This should be overridden in subclasses. |
| 614 |
*/ |
| 615 |
validateValue(value) { |
| 616 |
const parsed = this.parseNumber(value); |
| 617 |
const min = this.model.get('min'); |
| 618 |
const max = this.model.get('max'); |
| 619 |
if (isNaN(parsed) || |
| 620 |
(min != null && parsed < min) || |
| 621 |
(max != null && parsed > max)) { |
| 622 |
throw (value + |
| 623 |
' is not a valid number, it should be in the range [' + |
| 624 |
min + |
| 625 |
', ' + |
| 626 |
max + |
| 627 |
']'); |
| 628 |
} |
| 629 |
return parsed; |
| 630 |
} |
| 631 |
} |
| 632 |
export class FloatsInputModel extends NumbersInputModel { |
| 633 |
defaults() { |
| 634 |
return Object.assign(Object.assign({}, super.defaults()), { _view_name: 'FloatsInputView', _model_name: 'FloatsInputModel', format: '.1f' }); |
| 635 |
} |
| 636 |
} |
| 637 |
export class FloatsInputView extends NumbersInputView { |
| 638 |
parseNumber(value) { |
| 639 |
return parseFloat(value); |
| 640 |
} |
| 641 |
} |
| 642 |
export class IntsInputModel extends NumbersInputModel { |
| 643 |
defaults() { |
| 644 |
return Object.assign(Object.assign({}, super.defaults()), { _view_name: 'IntsInputView', _model_name: 'IntsInputModel', format: 'd' }); |
| 645 |
} |
| 646 |
} |
| 647 |
export class IntsInputView extends NumbersInputView { |
| 648 |
parseNumber(value) { |
| 649 |
const int = parseInt(value); |
| 650 |
if (int != parseFloat(value)) { |
| 651 |
throw value + ' should be an integer'; |
| 652 |
} |
| 653 |
return int; |
| 654 |
} |
| 655 |
} |
| 656 |
//# sourceMappingURL=widget_tagsinput.js.map |