mpl.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. /* Put everything inside the global mpl namespace */
  2. /* global mpl */
  3. window.mpl = {};
  4. mpl.get_websocket_type = function () {
  5. if (typeof WebSocket !== 'undefined') {
  6. return WebSocket;
  7. } else if (typeof MozWebSocket !== 'undefined') {
  8. return MozWebSocket;
  9. } else {
  10. alert(
  11. 'Your browser does not have WebSocket support. ' +
  12. 'Please try Chrome, Safari or Firefox ≥ 6. ' +
  13. 'Firefox 4 and 5 are also supported but you ' +
  14. 'have to enable WebSockets in about:config.'
  15. );
  16. }
  17. };
  18. mpl.figure = function (figure_id, websocket, ondownload, parent_element) {
  19. this.id = figure_id;
  20. this.ws = websocket;
  21. this.supports_binary = this.ws.binaryType !== undefined;
  22. if (!this.supports_binary) {
  23. var warnings = document.getElementById('mpl-warnings');
  24. if (warnings) {
  25. warnings.style.display = 'block';
  26. warnings.textContent =
  27. 'This browser does not support binary websocket messages. ' +
  28. 'Performance may be slow.';
  29. }
  30. }
  31. this.imageObj = new Image();
  32. this.context = undefined;
  33. this.message = undefined;
  34. this.canvas = undefined;
  35. this.rubberband_canvas = undefined;
  36. this.rubberband_context = undefined;
  37. this.format_dropdown = undefined;
  38. this.image_mode = 'full';
  39. this.root = document.createElement('div');
  40. this.root.setAttribute('style', 'display: inline-block');
  41. this._root_extra_style(this.root);
  42. parent_element.appendChild(this.root);
  43. this._init_header(this);
  44. this._init_canvas(this);
  45. this._init_toolbar(this);
  46. var fig = this;
  47. this.waiting = false;
  48. this.ws.onopen = function () {
  49. fig.send_message('supports_binary', { value: fig.supports_binary });
  50. fig.send_message('send_image_mode', {});
  51. if (fig.ratio !== 1) {
  52. fig.send_message('set_device_pixel_ratio', {
  53. device_pixel_ratio: fig.ratio,
  54. });
  55. }
  56. fig.send_message('refresh', {});
  57. };
  58. this.imageObj.onload = function () {
  59. if (fig.image_mode === 'full') {
  60. // Full images could contain transparency (where diff images
  61. // almost always do), so we need to clear the canvas so that
  62. // there is no ghosting.
  63. fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);
  64. }
  65. fig.context.drawImage(fig.imageObj, 0, 0);
  66. };
  67. this.imageObj.onunload = function () {
  68. fig.ws.close();
  69. };
  70. this.ws.onmessage = this._make_on_message_function(this);
  71. this.ondownload = ondownload;
  72. };
  73. mpl.figure.prototype._init_header = function () {
  74. var titlebar = document.createElement('div');
  75. titlebar.classList =
  76. 'ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix';
  77. var titletext = document.createElement('div');
  78. titletext.classList = 'ui-dialog-title';
  79. titletext.setAttribute(
  80. 'style',
  81. 'width: 100%; text-align: center; padding: 3px;'
  82. );
  83. titlebar.appendChild(titletext);
  84. this.root.appendChild(titlebar);
  85. this.header = titletext;
  86. };
  87. mpl.figure.prototype._canvas_extra_style = function (_canvas_div) {};
  88. mpl.figure.prototype._root_extra_style = function (_canvas_div) {};
  89. mpl.figure.prototype._init_canvas = function () {
  90. var fig = this;
  91. var canvas_div = (this.canvas_div = document.createElement('div'));
  92. canvas_div.setAttribute('tabindex', '0');
  93. canvas_div.setAttribute(
  94. 'style',
  95. 'border: 1px solid #ddd;' +
  96. 'box-sizing: content-box;' +
  97. 'clear: both;' +
  98. 'min-height: 1px;' +
  99. 'min-width: 1px;' +
  100. 'outline: 0;' +
  101. 'overflow: hidden;' +
  102. 'position: relative;' +
  103. 'resize: both;' +
  104. 'z-index: 2;'
  105. );
  106. function on_keyboard_event_closure(name) {
  107. return function (event) {
  108. return fig.key_event(event, name);
  109. };
  110. }
  111. canvas_div.addEventListener(
  112. 'keydown',
  113. on_keyboard_event_closure('key_press')
  114. );
  115. canvas_div.addEventListener(
  116. 'keyup',
  117. on_keyboard_event_closure('key_release')
  118. );
  119. this._canvas_extra_style(canvas_div);
  120. this.root.appendChild(canvas_div);
  121. var canvas = (this.canvas = document.createElement('canvas'));
  122. canvas.classList.add('mpl-canvas');
  123. canvas.setAttribute(
  124. 'style',
  125. 'box-sizing: content-box;' +
  126. 'pointer-events: none;' +
  127. 'position: relative;' +
  128. 'z-index: 0;'
  129. );
  130. this.context = canvas.getContext('2d');
  131. var backingStore =
  132. this.context.backingStorePixelRatio ||
  133. this.context.webkitBackingStorePixelRatio ||
  134. this.context.mozBackingStorePixelRatio ||
  135. this.context.msBackingStorePixelRatio ||
  136. this.context.oBackingStorePixelRatio ||
  137. this.context.backingStorePixelRatio ||
  138. 1;
  139. this.ratio = (window.devicePixelRatio || 1) / backingStore;
  140. var rubberband_canvas = (this.rubberband_canvas = document.createElement(
  141. 'canvas'
  142. ));
  143. rubberband_canvas.setAttribute(
  144. 'style',
  145. 'box-sizing: content-box;' +
  146. 'left: 0;' +
  147. 'pointer-events: none;' +
  148. 'position: absolute;' +
  149. 'top: 0;' +
  150. 'z-index: 1;'
  151. );
  152. // Apply a ponyfill if ResizeObserver is not implemented by browser.
  153. if (this.ResizeObserver === undefined) {
  154. if (window.ResizeObserver !== undefined) {
  155. this.ResizeObserver = window.ResizeObserver;
  156. } else {
  157. var obs = _JSXTOOLS_RESIZE_OBSERVER({});
  158. this.ResizeObserver = obs.ResizeObserver;
  159. }
  160. }
  161. this.resizeObserverInstance = new this.ResizeObserver(function (entries) {
  162. // There's no need to resize if the WebSocket is not connected:
  163. // - If it is still connecting, then we will get an initial resize from
  164. // Python once it connects.
  165. // - If it has disconnected, then resizing will clear the canvas and
  166. // never get anything back to refill it, so better to not resize and
  167. // keep something visible.
  168. if (fig.ws.readyState != 1) {
  169. return;
  170. }
  171. var nentries = entries.length;
  172. for (var i = 0; i < nentries; i++) {
  173. var entry = entries[i];
  174. var width, height;
  175. if (entry.contentBoxSize) {
  176. if (entry.contentBoxSize instanceof Array) {
  177. // Chrome 84 implements new version of spec.
  178. width = entry.contentBoxSize[0].inlineSize;
  179. height = entry.contentBoxSize[0].blockSize;
  180. } else {
  181. // Firefox implements old version of spec.
  182. width = entry.contentBoxSize.inlineSize;
  183. height = entry.contentBoxSize.blockSize;
  184. }
  185. } else {
  186. // Chrome <84 implements even older version of spec.
  187. width = entry.contentRect.width;
  188. height = entry.contentRect.height;
  189. }
  190. // Keep the size of the canvas and rubber band canvas in sync with
  191. // the canvas container.
  192. if (entry.devicePixelContentBoxSize) {
  193. // Chrome 84 implements new version of spec.
  194. canvas.setAttribute(
  195. 'width',
  196. entry.devicePixelContentBoxSize[0].inlineSize
  197. );
  198. canvas.setAttribute(
  199. 'height',
  200. entry.devicePixelContentBoxSize[0].blockSize
  201. );
  202. } else {
  203. canvas.setAttribute('width', width * fig.ratio);
  204. canvas.setAttribute('height', height * fig.ratio);
  205. }
  206. /* This rescales the canvas back to display pixels, so that it
  207. * appears correct on HiDPI screens. */
  208. canvas.style.width = width + 'px';
  209. canvas.style.height = height + 'px';
  210. rubberband_canvas.setAttribute('width', width);
  211. rubberband_canvas.setAttribute('height', height);
  212. // And update the size in Python. We ignore the initial 0/0 size
  213. // that occurs as the element is placed into the DOM, which should
  214. // otherwise not happen due to the minimum size styling.
  215. if (width != 0 && height != 0) {
  216. fig.request_resize(width, height);
  217. }
  218. }
  219. });
  220. this.resizeObserverInstance.observe(canvas_div);
  221. function on_mouse_event_closure(name) {
  222. /* User Agent sniffing is bad, but WebKit is busted:
  223. * https://bugs.webkit.org/show_bug.cgi?id=144526
  224. * https://bugs.webkit.org/show_bug.cgi?id=181818
  225. * The worst that happens here is that they get an extra browser
  226. * selection when dragging, if this check fails to catch them.
  227. */
  228. var UA = navigator.userAgent;
  229. var isWebKit = /AppleWebKit/.test(UA) && !/Chrome/.test(UA);
  230. if(isWebKit) {
  231. return function (event) {
  232. /* This prevents the web browser from automatically changing to
  233. * the text insertion cursor when the button is pressed. We
  234. * want to control all of the cursor setting manually through
  235. * the 'cursor' event from matplotlib */
  236. event.preventDefault()
  237. return fig.mouse_event(event, name);
  238. };
  239. } else {
  240. return function (event) {
  241. return fig.mouse_event(event, name);
  242. };
  243. }
  244. }
  245. canvas_div.addEventListener(
  246. 'mousedown',
  247. on_mouse_event_closure('button_press')
  248. );
  249. canvas_div.addEventListener(
  250. 'mouseup',
  251. on_mouse_event_closure('button_release')
  252. );
  253. canvas_div.addEventListener(
  254. 'dblclick',
  255. on_mouse_event_closure('dblclick')
  256. );
  257. // Throttle sequential mouse events to 1 every 20ms.
  258. canvas_div.addEventListener(
  259. 'mousemove',
  260. on_mouse_event_closure('motion_notify')
  261. );
  262. canvas_div.addEventListener(
  263. 'mouseenter',
  264. on_mouse_event_closure('figure_enter')
  265. );
  266. canvas_div.addEventListener(
  267. 'mouseleave',
  268. on_mouse_event_closure('figure_leave')
  269. );
  270. canvas_div.addEventListener('wheel', function (event) {
  271. if (event.deltaY < 0) {
  272. event.step = 1;
  273. } else {
  274. event.step = -1;
  275. }
  276. on_mouse_event_closure('scroll')(event);
  277. });
  278. canvas_div.appendChild(canvas);
  279. canvas_div.appendChild(rubberband_canvas);
  280. this.rubberband_context = rubberband_canvas.getContext('2d');
  281. this.rubberband_context.strokeStyle = '#000000';
  282. this._resize_canvas = function (width, height, forward) {
  283. if (forward) {
  284. canvas_div.style.width = width + 'px';
  285. canvas_div.style.height = height + 'px';
  286. }
  287. };
  288. // Disable right mouse context menu.
  289. canvas_div.addEventListener('contextmenu', function (_e) {
  290. event.preventDefault();
  291. return false;
  292. });
  293. function set_focus() {
  294. canvas.focus();
  295. canvas_div.focus();
  296. }
  297. window.setTimeout(set_focus, 100);
  298. };
  299. mpl.figure.prototype._init_toolbar = function () {
  300. var fig = this;
  301. var toolbar = document.createElement('div');
  302. toolbar.classList = 'mpl-toolbar';
  303. this.root.appendChild(toolbar);
  304. function on_click_closure(name) {
  305. return function (_event) {
  306. return fig.toolbar_button_onclick(name);
  307. };
  308. }
  309. function on_mouseover_closure(tooltip) {
  310. return function (event) {
  311. if (!event.currentTarget.disabled) {
  312. return fig.toolbar_button_onmouseover(tooltip);
  313. }
  314. };
  315. }
  316. fig.buttons = {};
  317. var buttonGroup = document.createElement('div');
  318. buttonGroup.classList = 'mpl-button-group';
  319. for (var toolbar_ind in mpl.toolbar_items) {
  320. var name = mpl.toolbar_items[toolbar_ind][0];
  321. var tooltip = mpl.toolbar_items[toolbar_ind][1];
  322. var image = mpl.toolbar_items[toolbar_ind][2];
  323. var method_name = mpl.toolbar_items[toolbar_ind][3];
  324. if (!name) {
  325. /* Instead of a spacer, we start a new button group. */
  326. if (buttonGroup.hasChildNodes()) {
  327. toolbar.appendChild(buttonGroup);
  328. }
  329. buttonGroup = document.createElement('div');
  330. buttonGroup.classList = 'mpl-button-group';
  331. continue;
  332. }
  333. var button = (fig.buttons[name] = document.createElement('button'));
  334. button.classList = 'mpl-widget';
  335. button.setAttribute('role', 'button');
  336. button.setAttribute('aria-disabled', 'false');
  337. button.addEventListener('click', on_click_closure(method_name));
  338. button.addEventListener('mouseover', on_mouseover_closure(tooltip));
  339. var icon_img = document.createElement('img');
  340. icon_img.src = '_images/' + image + '.png';
  341. icon_img.srcset = '_images/' + image + '_large.png 2x';
  342. icon_img.alt = tooltip;
  343. button.appendChild(icon_img);
  344. buttonGroup.appendChild(button);
  345. }
  346. if (buttonGroup.hasChildNodes()) {
  347. toolbar.appendChild(buttonGroup);
  348. }
  349. var fmt_picker = document.createElement('select');
  350. fmt_picker.classList = 'mpl-widget';
  351. toolbar.appendChild(fmt_picker);
  352. this.format_dropdown = fmt_picker;
  353. for (var ind in mpl.extensions) {
  354. var fmt = mpl.extensions[ind];
  355. var option = document.createElement('option');
  356. option.selected = fmt === mpl.default_extension;
  357. option.innerHTML = fmt;
  358. fmt_picker.appendChild(option);
  359. }
  360. var status_bar = document.createElement('span');
  361. status_bar.classList = 'mpl-message';
  362. toolbar.appendChild(status_bar);
  363. this.message = status_bar;
  364. };
  365. mpl.figure.prototype.request_resize = function (x_pixels, y_pixels) {
  366. // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,
  367. // which will in turn request a refresh of the image.
  368. this.send_message('resize', { width: x_pixels, height: y_pixels });
  369. };
  370. mpl.figure.prototype.send_message = function (type, properties) {
  371. properties['type'] = type;
  372. properties['figure_id'] = this.id;
  373. this.ws.send(JSON.stringify(properties));
  374. };
  375. mpl.figure.prototype.send_draw_message = function () {
  376. if (!this.waiting) {
  377. this.waiting = true;
  378. this.ws.send(JSON.stringify({ type: 'draw', figure_id: this.id }));
  379. }
  380. };
  381. mpl.figure.prototype.handle_save = function (fig, _msg) {
  382. var format_dropdown = fig.format_dropdown;
  383. var format = format_dropdown.options[format_dropdown.selectedIndex].value;
  384. fig.ondownload(fig, format);
  385. };
  386. mpl.figure.prototype.handle_resize = function (fig, msg) {
  387. var size = msg['size'];
  388. if (size[0] !== fig.canvas.width || size[1] !== fig.canvas.height) {
  389. fig._resize_canvas(size[0], size[1], msg['forward']);
  390. fig.send_message('refresh', {});
  391. }
  392. };
  393. mpl.figure.prototype.handle_rubberband = function (fig, msg) {
  394. var x0 = msg['x0'] / fig.ratio;
  395. var y0 = (fig.canvas.height - msg['y0']) / fig.ratio;
  396. var x1 = msg['x1'] / fig.ratio;
  397. var y1 = (fig.canvas.height - msg['y1']) / fig.ratio;
  398. x0 = Math.floor(x0) + 0.5;
  399. y0 = Math.floor(y0) + 0.5;
  400. x1 = Math.floor(x1) + 0.5;
  401. y1 = Math.floor(y1) + 0.5;
  402. var min_x = Math.min(x0, x1);
  403. var min_y = Math.min(y0, y1);
  404. var width = Math.abs(x1 - x0);
  405. var height = Math.abs(y1 - y0);
  406. fig.rubberband_context.clearRect(
  407. 0,
  408. 0,
  409. fig.canvas.width / fig.ratio,
  410. fig.canvas.height / fig.ratio
  411. );
  412. fig.rubberband_context.strokeRect(min_x, min_y, width, height);
  413. };
  414. mpl.figure.prototype.handle_figure_label = function (fig, msg) {
  415. // Updates the figure title.
  416. fig.header.textContent = msg['label'];
  417. };
  418. mpl.figure.prototype.handle_cursor = function (fig, msg) {
  419. fig.canvas_div.style.cursor = msg['cursor'];
  420. };
  421. mpl.figure.prototype.handle_message = function (fig, msg) {
  422. fig.message.textContent = msg['message'];
  423. };
  424. mpl.figure.prototype.handle_draw = function (fig, _msg) {
  425. // Request the server to send over a new figure.
  426. fig.send_draw_message();
  427. };
  428. mpl.figure.prototype.handle_image_mode = function (fig, msg) {
  429. fig.image_mode = msg['mode'];
  430. };
  431. mpl.figure.prototype.handle_history_buttons = function (fig, msg) {
  432. for (var key in msg) {
  433. if (!(key in fig.buttons)) {
  434. continue;
  435. }
  436. fig.buttons[key].disabled = !msg[key];
  437. fig.buttons[key].setAttribute('aria-disabled', !msg[key]);
  438. }
  439. };
  440. mpl.figure.prototype.handle_navigate_mode = function (fig, msg) {
  441. if (msg['mode'] === 'PAN') {
  442. fig.buttons['Pan'].classList.add('active');
  443. fig.buttons['Zoom'].classList.remove('active');
  444. } else if (msg['mode'] === 'ZOOM') {
  445. fig.buttons['Pan'].classList.remove('active');
  446. fig.buttons['Zoom'].classList.add('active');
  447. } else {
  448. fig.buttons['Pan'].classList.remove('active');
  449. fig.buttons['Zoom'].classList.remove('active');
  450. }
  451. };
  452. mpl.figure.prototype.updated_canvas_event = function () {
  453. // Called whenever the canvas gets updated.
  454. this.send_message('ack', {});
  455. };
  456. // A function to construct a web socket function for onmessage handling.
  457. // Called in the figure constructor.
  458. mpl.figure.prototype._make_on_message_function = function (fig) {
  459. return function socket_on_message(evt) {
  460. if (evt.data instanceof Blob) {
  461. var img = evt.data;
  462. if (img.type !== 'image/png') {
  463. /* FIXME: We get "Resource interpreted as Image but
  464. * transferred with MIME type text/plain:" errors on
  465. * Chrome. But how to set the MIME type? It doesn't seem
  466. * to be part of the websocket stream */
  467. img.type = 'image/png';
  468. }
  469. /* Free the memory for the previous frames */
  470. if (fig.imageObj.src) {
  471. (window.URL || window.webkitURL).revokeObjectURL(
  472. fig.imageObj.src
  473. );
  474. }
  475. fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(
  476. img
  477. );
  478. fig.updated_canvas_event();
  479. fig.waiting = false;
  480. return;
  481. } else if (
  482. typeof evt.data === 'string' &&
  483. evt.data.slice(0, 21) === 'data:image/png;base64'
  484. ) {
  485. fig.imageObj.src = evt.data;
  486. fig.updated_canvas_event();
  487. fig.waiting = false;
  488. return;
  489. }
  490. var msg = JSON.parse(evt.data);
  491. var msg_type = msg['type'];
  492. // Call the "handle_{type}" callback, which takes
  493. // the figure and JSON message as its only arguments.
  494. try {
  495. var callback = fig['handle_' + msg_type];
  496. } catch (e) {
  497. console.log(
  498. "No handler for the '%s' message type: ",
  499. msg_type,
  500. msg
  501. );
  502. return;
  503. }
  504. if (callback) {
  505. try {
  506. // console.log("Handling '%s' message: ", msg_type, msg);
  507. callback(fig, msg);
  508. } catch (e) {
  509. console.log(
  510. "Exception inside the 'handler_%s' callback:",
  511. msg_type,
  512. e,
  513. e.stack,
  514. msg
  515. );
  516. }
  517. }
  518. };
  519. };
  520. function getModifiers(event) {
  521. var mods = [];
  522. if (event.ctrlKey) {
  523. mods.push('ctrl');
  524. }
  525. if (event.altKey) {
  526. mods.push('alt');
  527. }
  528. if (event.shiftKey) {
  529. mods.push('shift');
  530. }
  531. if (event.metaKey) {
  532. mods.push('meta');
  533. }
  534. return mods;
  535. }
  536. /*
  537. * return a copy of an object with only non-object keys
  538. * we need this to avoid circular references
  539. * https://stackoverflow.com/a/24161582/3208463
  540. */
  541. function simpleKeys(original) {
  542. return Object.keys(original).reduce(function (obj, key) {
  543. if (typeof original[key] !== 'object') {
  544. obj[key] = original[key];
  545. }
  546. return obj;
  547. }, {});
  548. }
  549. mpl.figure.prototype.mouse_event = function (event, name) {
  550. if (name === 'button_press') {
  551. this.canvas.focus();
  552. this.canvas_div.focus();
  553. }
  554. // from https://stackoverflow.com/q/1114465
  555. var boundingRect = this.canvas.getBoundingClientRect();
  556. var x = (event.clientX - boundingRect.left) * this.ratio;
  557. var y = (event.clientY - boundingRect.top) * this.ratio;
  558. this.send_message(name, {
  559. x: x,
  560. y: y,
  561. button: event.button,
  562. step: event.step,
  563. buttons: event.buttons,
  564. modifiers: getModifiers(event),
  565. guiEvent: simpleKeys(event),
  566. });
  567. return false;
  568. };
  569. mpl.figure.prototype._key_event_extra = function (_event, _name) {
  570. // Handle any extra behaviour associated with a key event
  571. };
  572. mpl.figure.prototype.key_event = function (event, name) {
  573. // Prevent repeat events
  574. if (name === 'key_press') {
  575. if (event.key === this._key) {
  576. return;
  577. } else {
  578. this._key = event.key;
  579. }
  580. }
  581. if (name === 'key_release') {
  582. this._key = null;
  583. }
  584. var value = '';
  585. if (event.ctrlKey && event.key !== 'Control') {
  586. value += 'ctrl+';
  587. }
  588. else if (event.altKey && event.key !== 'Alt') {
  589. value += 'alt+';
  590. }
  591. else if (event.shiftKey && event.key !== 'Shift') {
  592. value += 'shift+';
  593. }
  594. value += 'k' + event.key;
  595. this._key_event_extra(event, name);
  596. this.send_message(name, { key: value, guiEvent: simpleKeys(event) });
  597. return false;
  598. };
  599. mpl.figure.prototype.toolbar_button_onclick = function (name) {
  600. if (name === 'download') {
  601. this.handle_save(this, null);
  602. } else {
  603. this.send_message('toolbar_button', { name: name });
  604. }
  605. };
  606. mpl.figure.prototype.toolbar_button_onmouseover = function (tooltip) {
  607. this.message.textContent = tooltip;
  608. };
  609. ///////////////// REMAINING CONTENT GENERATED BY embed_js.py /////////////////
  610. // prettier-ignore
  611. var _JSXTOOLS_RESIZE_OBSERVER=function(A){var t,i=new WeakMap,n=new WeakMap,a=new WeakMap,r=new WeakMap,o=new Set;function s(e){if(!(this instanceof s))throw new TypeError("Constructor requires 'new' operator");i.set(this,e)}function h(){throw new TypeError("Function is not a constructor")}function c(e,t,i,n){e=0 in arguments?Number(arguments[0]):0,t=1 in arguments?Number(arguments[1]):0,i=2 in arguments?Number(arguments[2]):0,n=3 in arguments?Number(arguments[3]):0,this.right=(this.x=this.left=e)+(this.width=i),this.bottom=(this.y=this.top=t)+(this.height=n),Object.freeze(this)}function d(){t=requestAnimationFrame(d);var s=new WeakMap,p=new Set;o.forEach((function(t){r.get(t).forEach((function(i){var r=t instanceof window.SVGElement,o=a.get(t),d=r?0:parseFloat(o.paddingTop),f=r?0:parseFloat(o.paddingRight),l=r?0:parseFloat(o.paddingBottom),u=r?0:parseFloat(o.paddingLeft),g=r?0:parseFloat(o.borderTopWidth),m=r?0:parseFloat(o.borderRightWidth),w=r?0:parseFloat(o.borderBottomWidth),b=u+f,F=d+l,v=(r?0:parseFloat(o.borderLeftWidth))+m,W=g+w,y=r?0:t.offsetHeight-W-t.clientHeight,E=r?0:t.offsetWidth-v-t.clientWidth,R=b+v,z=F+W,M=r?t.width:parseFloat(o.width)-R-E,O=r?t.height:parseFloat(o.height)-z-y;if(n.has(t)){var k=n.get(t);if(k[0]===M&&k[1]===O)return}n.set(t,[M,O]);var S=Object.create(h.prototype);S.target=t,S.contentRect=new c(u,d,M,O),s.has(i)||(s.set(i,[]),p.add(i)),s.get(i).push(S)}))})),p.forEach((function(e){i.get(e).call(e,s.get(e),e)}))}return s.prototype.observe=function(i){if(i instanceof window.Element){r.has(i)||(r.set(i,new Set),o.add(i),a.set(i,window.getComputedStyle(i)));var n=r.get(i);n.has(this)||n.add(this),cancelAnimationFrame(t),t=requestAnimationFrame(d)}},s.prototype.unobserve=function(i){if(i instanceof window.Element&&r.has(i)){var n=r.get(i);n.has(this)&&(n.delete(this),n.size||(r.delete(i),o.delete(i))),n.size||r.delete(i),o.size||cancelAnimationFrame(t)}},A.DOMRectReadOnly=c,A.ResizeObserver=s,A.ResizeObserverEntry=h,A}; // eslint-disable-line