diff --git a/js/package.json b/js/package.json index dab3221d..f38af12d 100644 --- a/js/package.json +++ b/js/package.json @@ -28,8 +28,6 @@ }, "dependencies": { "@jupyter-widgets/base": "^1.1.0", - "jquery": "^2.1.4", - "jquery-ui": "^1.12.1", "lodash": "^4.17.4" } } diff --git a/js/src/mpl.js b/js/src/mpl.js index a24a83d1..5df9f75b 100644 --- a/js/src/mpl.js +++ b/js/src/mpl.js @@ -1,17 +1,12 @@ -/* Put everything inside the global mpl namespace */ +window.mpl = {}; -// Universal Module Definition -(function (root, factory) { - if (typeof define === 'function' && define.amd) { - // AMD - define(['jquery'], factory); - } else { - // Browser globals (root is window) - root.returnExports = factory(root.jQuery); +function offset(el) { + var boundingRect = el.getBoundingClientRect(); + return { + top: boundingRect.top + document.body.scrollTop, + left: boundingRect.left + document.body.scrollLeft } -}(this, function ($) { - -window.mpl = {}; +} mpl.figure = function(figure_id, websocket, ondownload, parent_element) { @@ -40,11 +35,11 @@ mpl.figure = function(figure_id, websocket, ondownload, parent_element) { this.image_mode = 'full'; - this.root = $('
'); + this.root = document.createElement('div'); this._root_extra_style(this.root) - this.root.attr('style', 'display: inline-block'); + this.root.setAttribute('style', 'display: inline-block'); - $(parent_element).append(this.root); + parent_element.appendChild(this.root); this._init_header(this); this._init_canvas(this); @@ -71,19 +66,19 @@ mpl.figure = function(figure_id, websocket, ondownload, parent_element) { } mpl.figure.prototype._init_header = function() { - var titletext = $(''); - this.root.append(titletext); - this.header = titletext[0]; + this.header = document.createElement('div'); + this.header.setAttribute('style', 'width: 100%; text-align: center; padding: 3px;'); + this.root.appendChild(this.header); } mpl.figure.prototype._canvas_extra_style = function(canvas_div) { // this is important to make the div 'focusable' - canvas_div.attr('tabindex', 0); + canvas_div.setAttribute('tabindex', 0); } mpl.figure.prototype._root_extra_style = function(canvas_div) { var fig = this; - canvas_div.on("remove", function(){ + canvas_div.addEventListener('remove', function(){ fig.close_ws(fig, {}); }); } @@ -96,60 +91,59 @@ mpl.figure.prototype.close_ws = function(fig, msg){ mpl.figure.prototype._init_canvas = function() { var fig = this; - var canvas_div = $(''); + var canvas_div = document.createElement('div'); + canvas_div.setAttribute('style', 'position: relative; clear: both; outline:none'); - canvas_div.attr('style', 'position: relative; clear: both; outline:none'); - - function canvas_keyboard_event(event) { - event.stopPropagation(); - event.preventDefault(); - return fig.key_event(event, event['data']); + function on_keyboard_event_closure(name) { + return function(event) { + event.stopPropagation(); + event.preventDefault(); + return fig.key_event(event, name); + }; } - canvas_div.keydown('key_press', canvas_keyboard_event); - canvas_div.keyup('key_release', canvas_keyboard_event); - this.canvas_div = canvas_div - this._canvas_extra_style(canvas_div) - this.root.append(canvas_div); + canvas_div.addEventListener('keydown', on_keyboard_event_closure('key_press')); + canvas_div.addEventListener('keyup', on_keyboard_event_closure('key_release')); + this.canvas_div = canvas_div; + this._canvas_extra_style(canvas_div); + this.root.appendChild(canvas_div); - var canvas = $(''); - canvas.addClass('mpl-canvas'); - canvas.attr('style', "left: 0; top: 0; z-index: 0; ") + var canvas = this.canvas = document.createElement('canvas'); + canvas.classList.add('mpl-canvas'); + canvas.setAttribute('style', "left: 0; top: 0; z-index: 0; "); - this.canvas = canvas[0]; - this.context = canvas[0].getContext("2d"); + this.context = canvas.getContext("2d"); var backingStore = this.context.backingStorePixelRatio || - this.context.webkitBackingStorePixelRatio || - this.context.mozBackingStorePixelRatio || - this.context.msBackingStorePixelRatio || - this.context.oBackingStorePixelRatio || - this.context.backingStorePixelRatio || 1; + this.context.webkitBackingStorePixelRatio || + this.context.mozBackingStorePixelRatio || + this.context.msBackingStorePixelRatio || + this.context.oBackingStorePixelRatio || + this.context.backingStorePixelRatio || 1; mpl.ratio = (window.devicePixelRatio || 1) / backingStore; - var rubberband = $(''); - rubberband.attr('style', "position: absolute; left: 0; top: 0; z-index: 1;") - - var pass_mouse_events = true; + var rubberband_canvas = this.rubberband_canvas = document.createElement('canvas'); + rubberband_canvas.setAttribute('style', "position: absolute; left: 0; top: 0; z-index: 1;") // TODO: on resize event // fig.request_resize(width, height); - function mouse_event_fn(event) { - if (pass_mouse_events) - return fig.mouse_event(event, event['data']); + function on_mouse_event_closure(name) { + return function(event) { + return fig.mouse_event(event, name); + }; } - rubberband.mousedown('button_press', mouse_event_fn); - rubberband.mouseup('button_release', mouse_event_fn); + rubberband_canvas.addEventListener('mousedown', on_mouse_event_closure('button_press')); + rubberband_canvas.addEventListener('mouseup', on_mouse_event_closure('button_release')); // Throttle sequential mouse events to 1 every 20ms. - rubberband.mousemove('motion_notify', mouse_event_fn); + rubberband_canvas.addEventListener('mousemove', on_mouse_event_closure('motion_notify')); - rubberband.mouseenter('figure_enter', mouse_event_fn); - rubberband.mouseleave('figure_leave', mouse_event_fn); + rubberband_canvas.addEventListener('mouseenter', on_mouse_event_closure('figure_enter')); + rubberband_canvas.addEventListener('mouseleave', on_mouse_event_closure('figure_leave')); - canvas_div.on("wheel", function (event) { + canvas_div.addEventListener('wheel', function (event) { event = event.originalEvent; event['data'] = 'scroll' if (event.deltaY < 0) { @@ -160,26 +154,24 @@ mpl.figure.prototype._init_canvas = function() { mouse_event_fn(event); }); - canvas_div.append(canvas); - canvas_div.append(rubberband); + canvas_div.appendChild(canvas); + canvas_div.appendChild(rubberband_canvas); - this.rubberband = rubberband; - this.rubberband_canvas = rubberband[0]; - this.rubberband_context = rubberband[0].getContext("2d"); + this.rubberband_context = rubberband_canvas.getContext("2d"); this.rubberband_context.strokeStyle = "#000000"; this._resize_canvas = function(width, height) { // Keep the size of the canvas, canvas container, and rubber band // canvas in synch. - canvas_div.css('width', width) - canvas_div.css('height', height) + canvas_div.style.width = width; + canvas_div.style.height = height; - canvas.attr('width', width * mpl.ratio); - canvas.attr('height', height * mpl.ratio); - canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;'); + canvas.setAttribute('width', width * mpl.ratio); + canvas.setAttribute('height', height * mpl.ratio); + canvas.setAttribute('style', 'width: ' + width + 'px; height: ' + height + 'px;'); - rubberband.attr('width', width); - rubberband.attr('height', height); + rubberband_canvas.setAttribute('width', width); + rubberband_canvas.setAttribute('height', height); } // Set the figure to an initial 600x600px, this will subsequently be updated @@ -187,7 +179,7 @@ mpl.figure.prototype._init_canvas = function() { this._resize_canvas(600, 600); // Disable right mouse context menu. - $(this.rubberband_canvas).bind("contextmenu",function(e){ + this.rubberband_canvas.addEventListener('contextmenu', function(e) { return false; }); } @@ -195,22 +187,21 @@ mpl.figure.prototype._init_canvas = function() { mpl.figure.prototype._init_image = function() { var fig = this; - this.imageObj_div = $('