Debian Codegraph @

bqplot/js/src/LassoSelector.ts

raw
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 { BaseXYSelector } from './Selector';
20 import * as sel_utils from './selector_utils';
21
22 export class LassoSelector extends BaseXYSelector {
23 render() {
24 super.render();
25 const scale_creation_promise = this.create_scales();
26 this.line = d3.line();
27 this.all_vertices = {};
28 this.lasso_counter = 0;
29
30 const that = this;
31 Promise.all([this.mark_views_promise, scale_creation_promise]).then(() => {
32 // Warning: arrow functions actually breaks the drag
33 const drag = d3
34 .drag()
35 .on('start', () => {
36 that.drag_start();
37 })
38 .on('drag', () => {
39 that.drag_move();
40 })
41 .on('end', () => {
42 that.drag_end();
43 });
44
45 d3.select(window).on('keydown', () => {
46 this.keydown();
47 });
48
49 that.d3el.attr('class', 'lassoselector');
50
51 //container for mouse events
52 that.background = that.d3el
53 .append('rect')
54 .attr('x', 0)
55 .attr('y', 0)
56 .attr('width', that.width)
57 .attr('height', that.height)
58 .attr('visibility', 'hidden')
59 .attr('pointer-events', 'all')
60 .style('cursor', 'crosshair')
61 .call(drag);
62
63 that.create_listeners();
64 });
65 }
66
67 create_listeners() {
68 super.create_listeners();
69 this.listenTo(this.model, 'change:color', this.change_color);
70 }
71
72 change_color(model, color) {
73 if (color) {
74 this.d3el.selectAll('path').style('stroke', color);
75 }
76 }
77
78 create_new_lasso() {
79 const lasso = this.d3el
80 .append('path')
81 .attr('id', 'l' + ++this.lasso_counter)
82 .on('click', function () {
83 //toggle the opacity of lassos
84 const lasso = d3.select(this);
85 lasso.classed('selected', !lasso.classed('selected'));
86 });
87 const color = this.model.get('color');
88 if (color) {
89 lasso.style('stroke', color);
90 }
91 }
92
93 drag_start() {
94 this.current_vertices = [];
95 this.create_new_lasso();
96 }
97
98 drag_move() {
99 this.current_vertices.push(d3.mouse(this.background.node()));
100 this.d3el
101 .select('#l' + this.lasso_counter)
102 .attr('d', this.line(this.current_vertices));
103 }
104
105 drag_end() {
106 const lasso_name = 'l' + this.lasso_counter;
107 // Close the lasso
108 this.d3el
109 .select('#' + lasso_name)
110 .attr('d', this.line(this.current_vertices) + 'Z');
111 // Add the current vertices to the global lasso vertices
112 this.all_vertices[lasso_name] = this.current_vertices;
113 // Update selected for each mark
114 this.update_mark_selected(this.all_vertices);
115 }
116
117 update_mark_selected(vertices?) {
118 if (vertices === undefined || vertices.length === 0) {
119 // Reset all the selected in marks
120 _.each(this.mark_views, (mark_view: any) => {
121 return mark_view.selector_changed();
122 });
123 }
124 const point_selector = function (p) {
125 for (const l in vertices) {
126 if (sel_utils.point_in_lasso(p, vertices[l])) {
127 return true;
128 }
129 }
130 return false;
131 };
132 const rect_selector = function (xy) {
133 for (const l in vertices) {
134 if (sel_utils.lasso_inter_rect(xy[0], xy[1], vertices[l])) {
135 return true;
136 }
137 }
138 return false;
139 };
140
141 _.each(
142 this.mark_views,
143 (mark_view: any) => {
144 mark_view.selector_changed(point_selector, rect_selector);
145 },
146 this
147 );
148 }
149
150 relayout() {
151 super.relayout();
152 this.background.attr('width', this.width).attr('height', this.height);
153 }
154
155 keydown() {
156 // delete key pressed
157 if (d3GetEvent().keyCode === 46) {
158 // Delete selected lassos
159 const lassos_to_delete = this.d3el.selectAll('.selected');
160 // Update the lasso vertices
161 const vertices = this.all_vertices;
162 lassos_to_delete.each(function () {
163 const lasso_name = d3.select(this).attr('id');
164 delete vertices[lasso_name];
165 });
166 lassos_to_delete.remove();
167 this.update_mark_selected(this.all_vertices);
168 }
169 }
170
171 reset() {
172 this.lasso_counter = 0;
173 this.all_vertices = {};
174 this.d3el.selectAll('path').remove();
175 this.update_mark_selected();
176 }
177
178 // TODO: this is here to provide an implementation of an abstract method
179 // from the base class.
180 selected_changed() {}
181
182 line: d3.Line<[number, number]>;
183 all_vertices: { [key: string]: [number, number][] };
184 lasso_counter: number;
185 background: d3.Selection<SVGRectElement, any, any, any>;
186 current_vertices: [number, number][];
187 }