| 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 widgets from '@jupyter-widgets/base'; |
| 17 |
import * as d3 from 'd3'; |
| 18 |
// var d3 =Object.assign({}, require("d3-selection")); |
| 19 |
import * as _ from 'underscore'; |
| 20 |
import { Figure } from './Figure'; |
| 21 |
import { Mark } from './Mark'; |
| 22 |
|
| 23 |
export class Interaction extends widgets.WidgetView { |
| 24 |
initialize(parameters) { |
| 25 |
this.setElement( |
| 26 |
document.createElementNS(d3.namespaces.svg, 'rect') as HTMLElement |
| 27 |
); |
| 28 |
this.d3el = d3.select(this.el); |
| 29 |
super.initialize.call(this, parameters); |
| 30 |
} |
| 31 |
|
| 32 |
render() { |
| 33 |
this.parent = this.options.parent; |
| 34 |
|
| 35 |
// Opaque interaction layer |
| 36 |
this.d3el |
| 37 |
.attr('x', 0) |
| 38 |
.attr('y', 0) |
| 39 |
.attr( |
| 40 |
'width', |
| 41 |
this.parent.width - this.parent.margin.left - this.parent.margin.right |
| 42 |
) |
| 43 |
.attr( |
| 44 |
'height', |
| 45 |
this.parent.height - this.parent.margin.top - this.parent.margin.bottom |
| 46 |
) |
| 47 |
.attr('pointer-events', 'all') |
| 48 |
.attr('visibility', 'hidden'); |
| 49 |
this.listenTo(this.parent, 'margin_updated', this.relayout); |
| 50 |
} |
| 51 |
|
| 52 |
relayout() { |
| 53 |
// Called when the figure margins are updated. |
| 54 |
this.d3el |
| 55 |
.attr( |
| 56 |
'width', |
| 57 |
this.parent.width - this.parent.margin.left - this.parent.margin.right |
| 58 |
) |
| 59 |
.attr( |
| 60 |
'height', |
| 61 |
this.parent.height - this.parent.margin.top - this.parent.margin.bottom |
| 62 |
); |
| 63 |
} |
| 64 |
|
| 65 |
remove() { |
| 66 |
_.each(this.mark_views, (mark: any) => { |
| 67 |
mark.invert_range(); |
| 68 |
}); |
| 69 |
this.d3el.remove(); |
| 70 |
super.remove.apply(this); |
| 71 |
} |
| 72 |
|
| 73 |
mark_views: Mark[]; |
| 74 |
d3el: d3.Selection<Element, any, any, any>; |
| 75 |
parent: Figure; |
| 76 |
} |