Debian Codegraph @

bqplot/js/src/Toolbar.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 widgets from '@jupyter-widgets/base';
17 import { semver_range } from './version';
18 import * as _ from 'underscore';
19 import { Interaction } from './Interaction';
20
21 export class ToolbarModel extends widgets.DOMWidgetModel {
22 defaults() {
23 return {
24 ...widgets.DOMWidgetModel.prototype.defaults(),
25 _model_name: 'ToolbarModel',
26 _view_name: 'Toolbar',
27 _model_module: 'bqplot',
28 _view_module: 'bqplot',
29 _model_module_version: semver_range,
30 _view_module_version: semver_range,
31
32 figure: undefined,
33 _panning: false,
34 _panzoom: null,
35 };
36 }
37
38 // Backbone attributes:
39 // - _panning: Bool
40 // Whether one is currently panning - zooming the specified figure.
41 // - _panzoom: Instance of Panzoom or undefined:
42 // The created panzoom interaction. It is undefined at first.
43 // Attributes:
44 // - cached_interaction: Instance of Interaction or null or undefined.
45 // The cached interaction of the Figure. It is undefined at first
46 // and can take the value of the figure interaction, which can be
47 // null.
48 panzoom() {
49 const figure = this.get('figure');
50 if (this.get('_panning')) {
51 if (figure) {
52 figure.set('interaction', this.cached_interaction);
53 figure.save_changes();
54 }
55 this.set('_panning', false);
56 this.save_changes();
57 } else {
58 if (figure) {
59 this.cached_interaction = figure.get('interaction');
60 const panzoom = this.get('_panzoom');
61 if (panzoom) {
62 figure.set('interaction', panzoom);
63 figure.save_changes();
64 } else {
65 this._create_panzoom_model(figure).then((model) => {
66 this.set('_panzoom', model);
67 this.save_changes();
68 figure.set('interaction', model);
69 figure.save_changes();
70 });
71 }
72 }
73 this.set('_panning', true);
74 this.save_changes();
75 }
76 }
77
78 reset() {
79 /**
80 * Reset the scales, delete the PanZoom widget, set the figure
81 * interaction back to its previous value.
82 */
83 const figure = this.get('figure');
84 if (figure) {
85 figure.set('interaction', this.cached_interaction);
86 figure.save_changes();
87 const panzoom = this.get('_panzoom');
88 // Should reset_scales be part of PanZoomModel.close()?
89 panzoom.reset_scales();
90 panzoom.close();
91 this.set('_panzoom', null);
92 this.set('_panning', false);
93 this.save_changes();
94 }
95 }
96
97 save_png() {
98 /**
99 * Triggers the saving for all the views of that figure.
100 */
101 // TODO: the toolbar view needs to be associated with a Figure
102 // view to avoid calling a model method here.
103 const figure = this.get('figure');
104 if (figure) {
105 figure.save_png();
106 }
107 }
108
109 _create_panzoom_model(figure) {
110 /**
111 * Creates a panzoom interaction widget for the specified figure.
112 *
113 * It will discover the relevant scales for the specified figure.
114 */
115 return this.widget_manager
116 .new_widget({
117 model_name: 'PanZoomModel',
118 model_module: 'bqplot',
119 model_module_version: figure.get('_model_module_version'),
120 view_name: 'PanZoom',
121 view_module: 'bqplot',
122 view_module_version: figure.get('_view_module_version'),
123 })
124 .then((model) => {
125 return Promise.all(figure.get('marks')).then((marks: any[]) => {
126 const x_scales = [],
127 y_scales = [];
128 for (let i = 0; i < marks.length; ++i) {
129 const preserve_domain = marks[i].get('preserve_domain');
130 const scales = marks[i].get('scales');
131 _.each(scales, (v, k) => {
132 const dimension = marks[i].get('scales_metadata')[k]['dimension'];
133 if (dimension === 'x' && !preserve_domain[k]) {
134 x_scales.push(scales[k]);
135 }
136 if (dimension === 'y' && !preserve_domain[k]) {
137 y_scales.push(scales[k]);
138 }
139 });
140 }
141 model.set('scales', {
142 x: x_scales,
143 y: y_scales,
144 });
145 model.save_changes();
146 return model;
147 });
148 });
149 }
150
151 cached_interaction: Interaction;
152
153 static serializers = {
154 ...widgets.DOMWidgetModel.serializers,
155 figure: { deserialize: widgets.unpack_models },
156 _panzoom: { deserialize: widgets.unpack_models },
157 };
158 }
159
160 export class Toolbar extends widgets.DOMWidgetView {
161 render() {
162 this.el.classList.add('jupyter-widget'); // @jupyter-widgets/controls css
163 this.el.classList.add('widget-hbox'); // @jupyter-widgets/controls css
164
165 // We use @jupyter-widgets/controls css classes (ipywidget and widget-*-*) to
166 // benefit from default width, shadows.
167 // We do not use btn-group to not break alignment with jupyter
168 // buttons.
169
170 // Create the buttons
171 const _panzoom = document.createElement('button');
172 _panzoom.classList.add('jupyter-widgets'); // @jupyter-widgets/controls css
173 _panzoom.classList.add('jupyter-button'); // @jupyter-widgets/controls css
174 _panzoom.classList.add('widget-toggle-button'); // @jupyter-widgets/controls css
175 _panzoom.setAttribute('data-toggle', 'tooltip');
176 _panzoom.setAttribute('title', 'PanZoom');
177 _panzoom.onclick = (e) => {
178 e.preventDefault();
179 this.model.panzoom();
180 };
181 const panzoomicon = document.createElement('i');
182 panzoomicon.className = 'fa fa-arrows';
183 _panzoom.appendChild(panzoomicon);
184
185 const _reset = document.createElement('button');
186 _reset.classList.add('jupyter-widgets'); // @jupyter-widgets/controls css
187 _reset.classList.add('jupyter-button'); // @jupyter-widgets/controls css
188 _reset.classList.add('widget-button'); // @jupyter-widgets/controls css
189 _reset.setAttribute('data-toggle', 'tooltip');
190 _reset.setAttribute('title', 'Reset');
191 _reset.onclick = (e) => {
192 e.preventDefault();
193 this.model.reset();
194 };
195 const refreshicon = document.createElement('i');
196 refreshicon.className = 'fa fa-refresh';
197 _reset.appendChild(refreshicon);
198
199 const _save = document.createElement('button');
200 _save.classList.add('jupyter-widgets'); // @jupyter-widgets/controls css
201 _save.classList.add('jupyter-button'); // @jupyter-widgets/controls css
202 _save.classList.add('widget-button'); // @jupyter-widgets/controls css
203 _save.setAttribute('data-toggle', 'tooltip');
204 _save.setAttribute('title', 'Save');
205 _save.onclick = (e) => {
206 e.preventDefault();
207 this.model.save_png();
208 };
209 const saveicon = document.createElement('i');
210 saveicon.className = 'fa fa-save';
211 _save.appendChild(saveicon);
212
213 this.el.appendChild(_panzoom);
214 this.el.appendChild(_reset);
215 this.el.appendChild(_save);
216
217 // Handle initial state
218 this._panzoom = _panzoom;
219 this.update();
220 }
221
222 update() {
223 if (this.model.get('_panning')) {
224 this._panzoom.classList.add('mod-active');
225 } else {
226 this._panzoom.classList.remove('mod-active');
227 }
228 }
229
230 model: ToolbarModel;
231 _panzoom: HTMLButtonElement;
232 }