| 1 |
/* Copyright 2015 Bloomberg Finance L.P. |
| 2 |
* |
| 3 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
* you may not use this file except in compliance with the License. |
| 5 |
* You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software |
| 10 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
* See the License for the specific language governing permissions and |
| 13 |
* limitations under the License. |
| 14 |
*/ |
| 15 |
|
| 16 |
import * as d3 from 'd3'; |
| 17 |
import { d3GetEvent } from './utils'; |
| 18 |
import * as _ from 'underscore'; |
| 19 |
import * as selector from './Selector'; |
| 20 |
import * as utils from './utils'; |
| 21 |
import * as sel_utils from './selector_utils'; |
| 22 |
import { Mark } from './Mark'; |
| 23 |
|
| 24 |
// TODO: examine refactoring the two abstract base classes belowing using an |
| 25 |
// up-to-date mixin pattern. |
| 26 |
|
| 27 |
// Because we use abstract base classes, the mixins cannot work with current TypeScript: |
| 28 |
// https://github.com/microsoft/TypeScript/issues/29653 |
| 29 |
|
| 30 |
// Instead of using mixins we use free functions for now |
| 31 |
|
| 32 |
function point_selector(x, y) { |
| 33 |
return function (xar, yar) { |
| 34 |
if (typeof yar === 'undefined') { |
| 35 |
// the 'old' method for backwards compatibility |
| 36 |
return sel_utils.point_in_rectangle(xar, x, y); |
| 37 |
} |
| 38 |
|
| 39 |
const len = Math.min(xar.length, yar.length); |
| 40 |
const mask = new Uint8Array(len); |
| 41 |
// for performance we keep the if statement out of the loop |
| 42 |
if (x.length && y.length) { |
| 43 |
for (let i = 0; i < len; i++) { |
| 44 |
mask[i] = |
| 45 |
x[0] <= xar[i] && xar[i] <= x[1] && y[0] <= yar[i] && yar[i] <= y[1] |
| 46 |
? 1 |
| 47 |
: 0; |
| 48 |
} |
| 49 |
return mask; |
| 50 |
} else if (x.length) { |
| 51 |
for (let i = 0; i < len; i++) { |
| 52 |
mask[i] = x[0] <= xar[i] && xar[i] <= x[1] ? 1 : 0; |
| 53 |
} |
| 54 |
} else { |
| 55 |
// (y.length) |
| 56 |
for (let i = 0; i < len; i++) { |
| 57 |
mask[i] = y[0] <= yar[i] && yar[i] <= y[1] ? 1 : 0; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
return mask; |
| 62 |
}; |
| 63 |
} |
| 64 |
|
| 65 |
function rect_selector(x, y) { |
| 66 |
return function (xy) { |
| 67 |
return sel_utils.rect_inter_rect(xy[0], xy[1], x, y); |
| 68 |
}; |
| 69 |
} |
| 70 |
|
| 71 |
function sort(a, b) { |
| 72 |
return a - b; |
| 73 |
} |
| 74 |
|
| 75 |
function update_mark_selected(brush, extent_x?, extent_y?) { |
| 76 |
if (extent_x === undefined || extent_x.length === 0) { |
| 77 |
// Reset all the selected in marks |
| 78 |
_.each(brush.mark_views, (mark_view: any) => { |
| 79 |
return mark_view.selector_changed(); |
| 80 |
}); |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
let x, y; |
| 85 |
if (extent_y === undefined) { |
| 86 |
// 1d brush |
| 87 |
const orient = brush.model.get('orientation'); |
| 88 |
(x = orient == 'vertical' ? [] : extent_x), |
| 89 |
(y = orient == 'vertical' ? extent_x : []); |
| 90 |
} else { |
| 91 |
// 2d brush |
| 92 |
(x = extent_x), (y = extent_y); |
| 93 |
} |
| 94 |
|
| 95 |
if (x.length) { |
| 96 |
x.sort(sort); |
| 97 |
} |
| 98 |
if (y.length) { |
| 99 |
y.sort(sort); |
| 100 |
} |
| 101 |
|
| 102 |
_.each(brush.mark_views, (mark_view: any) => { |
| 103 |
mark_view.selector_changed(point_selector(x, y), rect_selector(x, y)); |
| 104 |
}); |
| 105 |
} |
| 106 |
|
| 107 |
function adjust_rectangle(brush) { |
| 108 |
if (brush.model.get('orientation') == 'vertical') { |
| 109 |
brush.d3el.selectAll('rect').attr('x', 0).attr('width', brush.width); |
| 110 |
} else { |
| 111 |
brush.d3el.selectAll('rect').attr('y', 0).attr('height', brush.height); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
abstract class BrushMixinXYSelector extends selector.BaseXYSelector { |
| 116 |
brush_render() { |
| 117 |
this.brushing = false; |
| 118 |
} |
| 119 |
|
| 120 |
color_change() { |
| 121 |
if (this.model.get('color') !== null) { |
| 122 |
this.d3el.selectAll('.selection').style('fill', this.model.get('color')); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
brush_start() { |
| 127 |
if (this.ignoreBrushEvents) { |
| 128 |
return; |
| 129 |
} |
| 130 |
this.brushing = true; |
| 131 |
this.model.set('brushing', true); |
| 132 |
this.touch(); |
| 133 |
} |
| 134 |
|
| 135 |
brush_move() { |
| 136 |
if (this.ignoreBrushEvents) { |
| 137 |
return; |
| 138 |
} |
| 139 |
this.convert_and_save(); |
| 140 |
} |
| 141 |
|
| 142 |
brush_end() { |
| 143 |
if (this.ignoreBrushEvents) { |
| 144 |
return; |
| 145 |
} |
| 146 |
this.model.set('brushing', false); |
| 147 |
this.convert_and_save(); |
| 148 |
this.brushing = false; |
| 149 |
} |
| 150 |
|
| 151 |
adjust_rectangle() { |
| 152 |
adjust_rectangle(this); |
| 153 |
} |
| 154 |
|
| 155 |
update_mark_selected(extent_x?, extent_y?) { |
| 156 |
return update_mark_selected(this, extent_x, extent_y); |
| 157 |
} |
| 158 |
|
| 159 |
abstract convert_and_save(extent?, item?); |
| 160 |
|
| 161 |
brush: d3.BrushBehavior<any>; |
| 162 |
brushing: boolean; |
| 163 |
|
| 164 |
// TODO: should this be mark_views_promises? |
| 165 |
mark_views: Mark[]; |
| 166 |
|
| 167 |
ignoreBrushEvents = false; |
| 168 |
} |
| 169 |
|
| 170 |
abstract class BrushMixinXSelector extends selector.BaseXSelector { |
| 171 |
brush_render() { |
| 172 |
this.brushing = false; |
| 173 |
} |
| 174 |
|
| 175 |
color_change() { |
| 176 |
if (this.model.get('color') !== null) { |
| 177 |
this.d3el.selectAll('.selection').style('fill', this.model.get('color')); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
brush_start() { |
| 182 |
if (this.ignoreBrushEvents) { |
| 183 |
return; |
| 184 |
} |
| 185 |
this.brushing = true; |
| 186 |
this.model.set('brushing', true); |
| 187 |
this.touch(); |
| 188 |
} |
| 189 |
|
| 190 |
brush_move() { |
| 191 |
if (this.ignoreBrushEvents) { |
| 192 |
return; |
| 193 |
} |
| 194 |
this.convert_and_save(); |
| 195 |
} |
| 196 |
|
| 197 |
brush_end() { |
| 198 |
if (this.ignoreBrushEvents) { |
| 199 |
return; |
| 200 |
} |
| 201 |
this.model.set('brushing', false); |
| 202 |
this.convert_and_save(); |
| 203 |
this.brushing = false; |
| 204 |
} |
| 205 |
|
| 206 |
adjust_rectangle() { |
| 207 |
adjust_rectangle(this); |
| 208 |
} |
| 209 |
|
| 210 |
update_mark_selected(extent_x?, extent_y?) { |
| 211 |
return update_mark_selected(this, extent_x, extent_y); |
| 212 |
} |
| 213 |
|
| 214 |
abstract convert_and_save(extent?, item?); |
| 215 |
|
| 216 |
brush: d3.BrushBehavior<any>; |
| 217 |
brushing: boolean; |
| 218 |
|
| 219 |
// TODO: should this be mark_views_promises? |
| 220 |
mark_views: Mark[]; |
| 221 |
|
| 222 |
ignoreBrushEvents = false; |
| 223 |
} |
| 224 |
|
| 225 |
export class BrushSelector extends BrushMixinXYSelector { |
| 226 |
async render() { |
| 227 |
await super.render(); |
| 228 |
this.brush_render(); |
| 229 |
|
| 230 |
await this.create_scales(); |
| 231 |
await this.mark_views_promise; |
| 232 |
this.brush = d3 |
| 233 |
.brush() |
| 234 |
.on('start', _.bind(this.brush_start, this)) |
| 235 |
.on('brush', _.bind(this.brush_move, this)) |
| 236 |
.on('end', _.bind(this.brush_end, this)); |
| 237 |
this.brush.extent([ |
| 238 |
[0, 0], |
| 239 |
[this.width, this.height], |
| 240 |
]); |
| 241 |
|
| 242 |
this.d3el.attr('class', 'selector brushintsel'); |
| 243 |
this.d3el.call(this.brush); |
| 244 |
this.adjust_rectangle(); |
| 245 |
this.color_change(); |
| 246 |
this.create_listeners(); |
| 247 |
this.selected_changed(); |
| 248 |
} |
| 249 |
|
| 250 |
create_listeners() { |
| 251 |
super.create_listeners(); |
| 252 |
this.listenTo(this.model, 'change:color', this.color_change); |
| 253 |
// Move these to BaseXYSelector |
| 254 |
this.listenTo(this.model, 'change:selected_x', this.selected_changed); |
| 255 |
this.listenTo(this.model, 'change:selected_y', this.selected_changed); |
| 256 |
} |
| 257 |
|
| 258 |
empty_selection() { |
| 259 |
this.update_mark_selected(); |
| 260 |
this.model.set('selected_x', null); |
| 261 |
this.model.set('selected_y', null); |
| 262 |
this.touch(); |
| 263 |
} |
| 264 |
|
| 265 |
convert_and_save() { |
| 266 |
const e = d3GetEvent(); |
| 267 |
if (!e.sourceEvent) { |
| 268 |
return; |
| 269 |
} |
| 270 |
if (!e.selection) { |
| 271 |
this.empty_selection(); |
| 272 |
} else { |
| 273 |
const d0 = e.selection; |
| 274 |
const pixel_extent_x = [d0[0][0], d0[1][0]]; |
| 275 |
const pixel_extent_y = [d0[1][1], d0[0][1]]; |
| 276 |
|
| 277 |
const extent_x = pixel_extent_x |
| 278 |
.map(this.x_scale.invert.bind(this.x_scale)) |
| 279 |
.sort(sort); |
| 280 |
const extent_y = pixel_extent_y |
| 281 |
.map(this.y_scale.invert.bind(this.y_scale)) |
| 282 |
.sort(sort); |
| 283 |
|
| 284 |
this.update_mark_selected(pixel_extent_x, pixel_extent_y); |
| 285 |
this.set_selected( |
| 286 |
'selected_x', |
| 287 |
this.x_scale.model.typedRange(extent_x as ArrayLike<number>) |
| 288 |
); |
| 289 |
this.set_selected( |
| 290 |
'selected_y', |
| 291 |
this.y_scale.model.typedRange(extent_y as ArrayLike<number>) |
| 292 |
); |
| 293 |
this.touch(); |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
selected_changed() { |
| 298 |
if (this.brushing) { |
| 299 |
return; |
| 300 |
} |
| 301 |
//reposition the interval selector and set the selected attribute. |
| 302 |
const selected_x = this.model.get('selected_x') || [], |
| 303 |
selected_y = this.model.get('selected_y') || []; |
| 304 |
if (selected_x.length === 0 || selected_y.length === 0) { |
| 305 |
this.update_mark_selected(); |
| 306 |
} else if (selected_x.length != 2 || selected_y.length != 2) { |
| 307 |
// invalid value for selected. Ignoring the value |
| 308 |
return; |
| 309 |
} else { |
| 310 |
const pixel_extent_x = selected_x |
| 311 |
.map((v) => this.x_scale.offset + this.x_scale.scale(v)) |
| 312 |
.sort(sort); |
| 313 |
const pixel_extent_y = selected_y |
| 314 |
.map((v) => this.y_scale.offset + this.y_scale.scale(v)) |
| 315 |
.sort(sort); |
| 316 |
this.update_mark_selected(pixel_extent_x, pixel_extent_y); |
| 317 |
} |
| 318 |
this.syncModelToBrush(); |
| 319 |
} |
| 320 |
|
| 321 |
relayout() { |
| 322 |
super.relayout(); |
| 323 |
this.d3el |
| 324 |
.select('.background') |
| 325 |
.attr('width', this.width) |
| 326 |
.attr('height', this.height); |
| 327 |
|
| 328 |
this.set_x_range([this.x_scale]); |
| 329 |
this.set_y_range([this.y_scale]); |
| 330 |
|
| 331 |
this.brush.extent([ |
| 332 |
[0, 0], |
| 333 |
[this.width, this.height], |
| 334 |
]); |
| 335 |
this.syncModelToBrush(); |
| 336 |
} |
| 337 |
|
| 338 |
private syncModelToBrush() { |
| 339 |
// Move and redraw the brush selector, preventing move events to be triggered |
| 340 |
this.ignoreBrushEvents = true; |
| 341 |
|
| 342 |
try { |
| 343 |
if (this.model.get('selected_x') && this.model.get('selected_y')) { |
| 344 |
const range_x = this.model |
| 345 |
.get('selected_x') |
| 346 |
.map((v) => this.x_scale.offset + this.x_scale.scale(v)) |
| 347 |
.sort(sort); |
| 348 |
const range_y = this.model |
| 349 |
.get('selected_y') |
| 350 |
.map((v) => this.y_scale.offset + this.y_scale.scale(v)) |
| 351 |
.sort(sort); |
| 352 |
this.brush.move(this.d3el, [ |
| 353 |
[range_x[0], range_y[0]], |
| 354 |
[range_x[1], range_y[1]], |
| 355 |
]); |
| 356 |
} else { |
| 357 |
this.brush.move(this.d3el, null); |
| 358 |
} |
| 359 |
|
| 360 |
this.d3el.call(this.brush); |
| 361 |
} finally { |
| 362 |
this.ignoreBrushEvents = false; |
| 363 |
} |
| 364 |
|
| 365 |
this.d3el.call(this.brush); |
| 366 |
} |
| 367 |
|
| 368 |
// TODO: check that we've properly overridden the mixin. |
| 369 |
adjust_rectangle() {} |
| 370 |
reset() {} |
| 371 |
|
| 372 |
d3el: d3.Selection<SVGGElement, any, any, any>; |
| 373 |
} |
| 374 |
|
| 375 |
export class BrushIntervalSelector extends BrushMixinXSelector { |
| 376 |
async render() { |
| 377 |
await super.render(); |
| 378 |
this.brush_render(); |
| 379 |
|
| 380 |
await this.mark_views_promise; |
| 381 |
await this.create_scales(); |
| 382 |
this.brush = ( |
| 383 |
this.model.get('orientation') == 'vertical' ? d3.brushY() : d3.brushX() |
| 384 |
) |
| 385 |
.on('start', _.bind(this.brush_start, this)) |
| 386 |
.on('brush', _.bind(this.brush_move, this)) |
| 387 |
.on('end', _.bind(this.brush_end, this)); |
| 388 |
this.brush.extent([ |
| 389 |
[0, 0], |
| 390 |
[this.width, this.height], |
| 391 |
]); |
| 392 |
|
| 393 |
this.d3el.attr('class', 'selector brushintsel'); |
| 394 |
this.d3el.call(this.brush); |
| 395 |
this.adjust_rectangle(); |
| 396 |
this.color_change(); |
| 397 |
this.create_listeners(); |
| 398 |
this.selected_changed(); |
| 399 |
} |
| 400 |
|
| 401 |
create_listeners() { |
| 402 |
super.create_listeners(); |
| 403 |
this.listenTo(this.model, 'change:color', this.color_change); |
| 404 |
this.listenTo(this.model, 'change:selected', this.selected_changed); |
| 405 |
} |
| 406 |
|
| 407 |
empty_selection() { |
| 408 |
this.update_mark_selected(); |
| 409 |
this.model.set('selected', null); |
| 410 |
this.touch(); |
| 411 |
} |
| 412 |
|
| 413 |
convert_and_save() { |
| 414 |
const e = d3GetEvent(); |
| 415 |
if (!e.sourceEvent) { |
| 416 |
return; |
| 417 |
} |
| 418 |
if (!e.selection) { |
| 419 |
this.empty_selection(); |
| 420 |
} else { |
| 421 |
const pixel_extent = e.selection; |
| 422 |
const extent = pixel_extent |
| 423 |
.map(this.scale.invert.bind(this.scale)) |
| 424 |
.sort(sort); |
| 425 |
|
| 426 |
this.update_mark_selected(pixel_extent); |
| 427 |
this.set_selected('selected', this.scale.model.typedRange(extent)); |
| 428 |
this.touch(); |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
update_scale_domain(ignore_gui_update) { |
| 433 |
// Call the base class function to update the scale. |
| 434 |
super.update_scale_domain(); |
| 435 |
if (ignore_gui_update !== true) { |
| 436 |
this.selected_changed(); |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
selected_changed() { |
| 441 |
if (this.brushing) { |
| 442 |
return; |
| 443 |
} |
| 444 |
//reposition the interval selector and set the selected attribute. |
| 445 |
const selected = this.model.get('selected') || []; |
| 446 |
if (selected.length === 0) { |
| 447 |
this.update_mark_selected(); |
| 448 |
} else if (selected.length != 2) { |
| 449 |
// invalid value for selected. Ignoring the value |
| 450 |
return; |
| 451 |
} else { |
| 452 |
const extent = [selected[0], selected[1]]; |
| 453 |
const pixel_extent = extent.map((v) => this.scale.scale(v)).sort(sort); |
| 454 |
this.update_mark_selected(pixel_extent); |
| 455 |
} |
| 456 |
this.syncModelToBrush(); |
| 457 |
} |
| 458 |
|
| 459 |
relayout() { |
| 460 |
super.relayout(); |
| 461 |
|
| 462 |
this.adjust_rectangle(); |
| 463 |
this.d3el |
| 464 |
.select('.background') |
| 465 |
.attr('width', this.width) |
| 466 |
.attr('height', this.height); |
| 467 |
|
| 468 |
this.set_range([this.scale]); |
| 469 |
this.brush.extent([ |
| 470 |
[0, 0], |
| 471 |
[this.width, this.height], |
| 472 |
]); |
| 473 |
this.syncModelToBrush(); |
| 474 |
} |
| 475 |
|
| 476 |
private syncModelToBrush() { |
| 477 |
// Move and redraw the brush selector, preventing move events to be triggered |
| 478 |
this.ignoreBrushEvents = true; |
| 479 |
try { |
| 480 |
if (this.model.get('selected')) { |
| 481 |
const range = this.model |
| 482 |
.get('selected') |
| 483 |
.map((v) => this.scale.scale(v)) |
| 484 |
.sort(sort); |
| 485 |
|
| 486 |
this.brush.move(this.d3el, range); |
| 487 |
} else { |
| 488 |
this.brush.move(this.d3el, null); |
| 489 |
} |
| 490 |
|
| 491 |
this.d3el.call(this.brush); |
| 492 |
} finally { |
| 493 |
this.ignoreBrushEvents = false; |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
reset() {} |
| 498 |
|
| 499 |
d3el: d3.Selection<SVGGElement, any, any, any>; |
| 500 |
} |
| 501 |
|
| 502 |
function add_remove_classes(selection, add_classes, remove_classes) { |
| 503 |
//adds the classes present in add_classes and removes the classes in |
| 504 |
//the list remove_classes |
| 505 |
//selection attribute should be a d3-selection |
| 506 |
if (remove_classes) { |
| 507 |
remove_classes.forEach((r_class) => { |
| 508 |
selection.classed(r_class, false); |
| 509 |
}); |
| 510 |
} |
| 511 |
if (add_classes) { |
| 512 |
add_classes.forEach((a_class) => { |
| 513 |
selection.classed(a_class, true); |
| 514 |
}); |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
export class MultiSelector extends BrushMixinXSelector { |
| 519 |
render() { |
| 520 |
super.render.apply(this); |
| 521 |
this.brush_render(); |
| 522 |
|
| 523 |
this.names = this.model.get('names'); |
| 524 |
this.curr_index = 0; |
| 525 |
|
| 526 |
const scale_creation_promise = this.create_scales(); |
| 527 |
Promise.all([this.mark_views_promise, scale_creation_promise]).then(() => { |
| 528 |
this.d3el.attr('class', 'multiselector'); |
| 529 |
this.d3el.attr('width', this.width); |
| 530 |
this.d3el.attr('height', this.height); |
| 531 |
this.create_brush(); |
| 532 |
this.selecting_brush = false; |
| 533 |
this.create_listeners(); |
| 534 |
}); |
| 535 |
} |
| 536 |
|
| 537 |
create_listeners() { |
| 538 |
super.create_listeners(); |
| 539 |
this.listenTo(this.model, 'change:names', this.labels_change); |
| 540 |
this.listenTo(this.model, 'change:color', this.color_change); |
| 541 |
} |
| 542 |
|
| 543 |
labels_change(model, value) { |
| 544 |
const prev_names = model.previous('names'); |
| 545 |
this.names = value; |
| 546 |
|
| 547 |
const data = _.range(this.curr_index + 1); |
| 548 |
const selected = utils.deepCopy(this.model.get('selected')); |
| 549 |
|
| 550 |
// TODO: Use do diff? |
| 551 |
data.forEach((elem) => { |
| 552 |
const label = this.get_label(elem); |
| 553 |
const prev_label = this.get_label(elem, prev_names); |
| 554 |
if (prev_label !== label) { |
| 555 |
this.d3el.select('.brush_text_' + elem).text(label); |
| 556 |
selected[label] = selected[prev_label]; |
| 557 |
delete selected[prev_label]; |
| 558 |
} |
| 559 |
}); |
| 560 |
this.set_selected('_selected', selected); |
| 561 |
this.touch(); |
| 562 |
} |
| 563 |
|
| 564 |
create_brush() { |
| 565 |
// Function to add new brushes. |
| 566 |
const index = this.curr_index; |
| 567 |
|
| 568 |
const vertical = this.model.get('orientation') == 'vertical'; |
| 569 |
const brush: d3.BrushBehavior<any> = (vertical ? d3.brushY() : d3.brushX()) |
| 570 |
.on('start', () => { |
| 571 |
this.brush_start(); |
| 572 |
}) |
| 573 |
.on('brush', () => { |
| 574 |
this.multi_brush_move(index); |
| 575 |
}) |
| 576 |
.on('end', () => { |
| 577 |
this.multi_brush_end(index); |
| 578 |
}); |
| 579 |
brush.extent([ |
| 580 |
[0, 0], |
| 581 |
[this.width, this.height], |
| 582 |
]); |
| 583 |
|
| 584 |
const new_brush_g: d3.Selection<SVGGElement, any, any, any> = this.d3el |
| 585 |
.append('g') |
| 586 |
.attr('class', 'selector brushintsel active'); |
| 587 |
|
| 588 |
new_brush_g |
| 589 |
.append('text') |
| 590 |
.text(this.get_label(this.curr_index)) |
| 591 |
.attr('class', 'brush_text_' + this.curr_index) |
| 592 |
.style('display', 'none'); |
| 593 |
|
| 594 |
if (this.model.get('orientation') == 'vertical') { |
| 595 |
new_brush_g.select('text').attr('x', 30); |
| 596 |
} else { |
| 597 |
new_brush_g.select('text').attr('y', 30); |
| 598 |
} |
| 599 |
|
| 600 |
new_brush_g.call(brush); |
| 601 |
|
| 602 |
this.color_change(); |
| 603 |
this.adjust_rectangle(); |
| 604 |
this.reset_handler(new_brush_g); |
| 605 |
|
| 606 |
this.brushes[this.curr_index] = brush; |
| 607 |
this.brush_g[this.curr_index] = new_brush_g; |
| 608 |
this.curr_index = this.curr_index + 1; |
| 609 |
} |
| 610 |
|
| 611 |
reset_handler(brush_g) { |
| 612 |
const that = this; |
| 613 |
const old_handler = brush_g.on('mousedown.brush'); |
| 614 |
|
| 615 |
brush_g.on('mousedown.brush', function () { |
| 616 |
const accelKey = d3GetEvent().ctrlKey || d3GetEvent().metaKey; |
| 617 |
if (d3GetEvent().shiftKey && accelKey) { |
| 618 |
that.reset(); |
| 619 |
} else if (accelKey) { |
| 620 |
add_remove_classes(d3.select(this), ['inactive'], ['active']); |
| 621 |
that.create_brush(); |
| 622 |
} else if (d3GetEvent().shiftKey && that.selecting_brush === false) { |
| 623 |
add_remove_classes( |
| 624 |
that.d3el.selectAll('.selector'), |
| 625 |
['visible'], |
| 626 |
['active', 'inactive'] |
| 627 |
); |
| 628 |
that.selecting_brush = true; |
| 629 |
} else { |
| 630 |
add_remove_classes( |
| 631 |
that.d3el.selectAll('.selector'), |
| 632 |
['inactive'], |
| 633 |
['visible'] |
| 634 |
); |
| 635 |
add_remove_classes(d3.select(this), ['active'], ['inactive']); |
| 636 |
old_handler.call(this); |
| 637 |
that.selecting_brush = false; |
| 638 |
} |
| 639 |
}); |
| 640 |
} |
| 641 |
|
| 642 |
get_label(index, arr?) { |
| 643 |
//arr is optional. If you do not pass anything, this.names is |
| 644 |
//considered arr. |
| 645 |
if (arr === undefined || arr === null) { |
| 646 |
arr = this.names; |
| 647 |
} |
| 648 |
return arr.length > index ? arr[index] : index; |
| 649 |
} |
| 650 |
|
| 651 |
multi_brush_move(item) { |
| 652 |
if (this.ignoreBrushEvents) { |
| 653 |
return; |
| 654 |
} |
| 655 |
|
| 656 |
const extent = d3GetEvent().selection; |
| 657 |
|
| 658 |
this.update_text(item, extent); |
| 659 |
this.convert_and_save(extent, item); |
| 660 |
} |
| 661 |
|
| 662 |
update_text(item, extent) { |
| 663 |
if (extent === null) { |
| 664 |
this.d3el.select('.brush_text_' + item).style('display', 'none'); |
| 665 |
return; |
| 666 |
} |
| 667 |
|
| 668 |
const orient = this.model.get('orientation') == 'vertical' ? 'y' : 'x'; |
| 669 |
const hide_names = !this.model.get('show_names'); |
| 670 |
const mid = (extent[0] + extent[1]) / 2; |
| 671 |
|
| 672 |
this.d3el |
| 673 |
.select('.brush_text_' + item) |
| 674 |
.style('display', hide_names ? 'none' : 'inline') |
| 675 |
.attr(orient, mid); |
| 676 |
} |
| 677 |
|
| 678 |
multi_brush_end(item) { |
| 679 |
if (this.ignoreBrushEvents) { |
| 680 |
return; |
| 681 |
} |
| 682 |
|
| 683 |
const sel = d3GetEvent().selection; |
| 684 |
this.model.set('brushing', false); |
| 685 |
this.convert_and_save(sel, item); |
| 686 |
this.brushing = false; |
| 687 |
} |
| 688 |
|
| 689 |
reset() { |
| 690 |
this.d3el.selectAll('.selector').remove(); |
| 691 |
this.model.set('_selected', {}); |
| 692 |
this.curr_index = 0; |
| 693 |
this.brushes = []; |
| 694 |
this.brush_g = []; |
| 695 |
this.touch(); |
| 696 |
this.create_brush(); |
| 697 |
} |
| 698 |
|
| 699 |
convert_and_save(extent, item) { |
| 700 |
if (!extent) { |
| 701 |
this.update_mark_selected(); |
| 702 |
this.model.set('_selected', {}); |
| 703 |
} else { |
| 704 |
const selected = utils.deepCopy(this.model.get('_selected')); |
| 705 |
selected[this.get_label(item)] = extent.map( |
| 706 |
this.scale.invert.bind(this.scale) |
| 707 |
); |
| 708 |
this.update_mark_selected(extent); |
| 709 |
this.model.set('_selected', selected); |
| 710 |
} |
| 711 |
this.touch(); |
| 712 |
} |
| 713 |
|
| 714 |
// TODO: make a proper implementation |
| 715 |
selected_changed() {} |
| 716 |
|
| 717 |
relayout() { |
| 718 |
super.relayout(); |
| 719 |
|
| 720 |
this.adjust_rectangle(); |
| 721 |
this.d3el |
| 722 |
.select('.background') |
| 723 |
.attr('width', this.width) |
| 724 |
.attr('height', this.height); |
| 725 |
|
| 726 |
this.set_range([this.scale]); |
| 727 |
this.brushes.forEach((brush) => { |
| 728 |
brush.extent([ |
| 729 |
[0, 0], |
| 730 |
[this.width, this.height], |
| 731 |
]); |
| 732 |
}); |
| 733 |
this.syncModelToBrush(); |
| 734 |
} |
| 735 |
|
| 736 |
private syncModelToBrush() { |
| 737 |
// Move and redraw the brush selectors, preventing move events to be triggered |
| 738 |
this.ignoreBrushEvents = true; |
| 739 |
try { |
| 740 |
const selected = this.model.get('_selected'); |
| 741 |
|
| 742 |
this.brushes.forEach((brush, index) => { |
| 743 |
const brushSelected = selected[this.get_label(index)]; |
| 744 |
|
| 745 |
if (brushSelected) { |
| 746 |
const range = brushSelected |
| 747 |
.map((v) => this.scale.scale(v)) |
| 748 |
.sort(sort); |
| 749 |
|
| 750 |
this.update_text(index, range); |
| 751 |
brush.move(this.brush_g[index], range); |
| 752 |
} else { |
| 753 |
this.update_text(index, null); |
| 754 |
brush.move(this.brush_g[index], null); |
| 755 |
} |
| 756 |
|
| 757 |
this.brush_g[index].call(brush); |
| 758 |
this.reset_handler(this.brush_g[index]); |
| 759 |
}); |
| 760 |
} finally { |
| 761 |
this.ignoreBrushEvents = false; |
| 762 |
} |
| 763 |
} |
| 764 |
|
| 765 |
remove() { |
| 766 |
this.model.off('change:names', null, this); |
| 767 |
this.model.off('change:color', null, this); |
| 768 |
super.remove(); |
| 769 |
} |
| 770 |
|
| 771 |
brushes: d3.BrushBehavior<any>[] = []; |
| 772 |
brush_g: d3.Selection<SVGGElement, any, any, any>[] = []; |
| 773 |
|
| 774 |
curr_index: number; |
| 775 |
selecting_brush: boolean; |
| 776 |
names: string[]; |
| 777 |
} |