Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get rid of jQuery use #390

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 26 additions & 27 deletions js/src/_base/Renderable.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var _ = require('underscore');
var widgets = require('@jupyter-widgets/base');
var $ = require('jquery');
var Promise = require('bluebird');

var pkgName = require('../../package.json').name;
Expand Down Expand Up @@ -52,6 +51,7 @@ class RenderableModel extends widgets.DOMWidgetModel {

initialize(attributes, options) {
widgets.DOMWidgetModel.prototype.initialize.apply(this, arguments);
this.boundTick = this.tick.bind(this);

this.createPropertiesArrays();
ThreeModel.prototype.setupListeners.call(this);
Expand Down Expand Up @@ -132,10 +132,10 @@ class RenderableModel extends widgets.DOMWidgetModel {
var canvas;
if (view.isFrozen) {
canvas = document.createElement('canvas');
canvas.width = view.$frozenRenderer.width();
canvas.height = view.$frozenRenderer.height();
canvas.width = view.frozenRenderer.width;
canvas.height = view.frozenRenderer.height;
var ctx = canvas.getContext('2d');
ctx.drawImage(view.$frozenRenderer[0], 0, 0);
ctx.drawImage(view.frozenRenderer, 0, 0);
} else {
canvas = view.renderer.domElement;
}
Expand Down Expand Up @@ -191,7 +191,7 @@ class RenderableView extends widgets.DOMWidgetView {
remove() {
super.remove();

this.$el.empty();
this.el.replaceChildren();
if (!this.isFrozen) {
this.teardownViewer();
}
Expand Down Expand Up @@ -226,8 +226,8 @@ class RenderableView extends widgets.DOMWidgetView {
if (this.renderer) {
candidates.push(this.renderer.domElement);
}
if (this.$frozenRenderer) {
candidates.push(this.$frozenRenderer[0]);
if (this.frozenRenderer) {
candidates.push(this.frozenRenderer);
}
if (candidates.indexOf(event.target) !== -1) {
event.preventDefault();
Expand Down Expand Up @@ -274,7 +274,8 @@ class RenderableView extends widgets.DOMWidgetView {
var height = this.model.get('_height');
if (this.isFrozen) {
// Set size of frozen element
this.$frozenRenderer.width(width).height(height);
this.frozenRenderer.width = width;
this.frozenRenderer.height = height;
} else {
this.renderer.setSize(width, height);
}
Expand Down Expand Up @@ -395,9 +396,9 @@ class RenderableView extends widgets.DOMWidgetView {

this.isFrozen = false;

if(this.$frozenRenderer) {
this.$frozenRenderer.off('mouseenter');
this.$frozenRenderer = null;
if(this.frozenRenderer) {
this.frozenRenderer.removeEventListener('mouseenter', this.boundTick);
this.frozenRenderer = null;
}

this.acquireRenderer();
Expand All @@ -420,10 +421,9 @@ class RenderableView extends widgets.DOMWidgetView {
config,
this.onRendererReclaimed.bind(this));

this.$renderer = $(this.renderer.domElement);
this.$el.empty().append(this.$renderer);
this.el.replaceChildren(this.renderer);

this.$el.css('display', 'block');
this.el.style.display = 'block';

this.updateSize();

Expand All @@ -442,49 +442,48 @@ class RenderableView extends widgets.DOMWidgetView {

this.debug('ThreeView.freeze(id=' + this.renderer.poolId + ')');

this.$el.empty().append('<img src="' + this.renderer.domElement.toDataURL() + '" />');
const frozen = document.createElement('img');
frozen.src = this.renderer.domElement.toDataURL();
this.el.replaceChildren(frozen);

this.teardownViewer();
this.$frozenRenderer = this.$el.find('img');
this.frozenRenderer = frozen;

// Ensure the image gets set the right size:
this.updateSize();

if (this.controls) {
this.$frozenRenderer.on('mouseenter', _.bind(function() {
this.debug('frozenRenderer.mouseenter');
this.tick(); // renderer will be acquired by renderScene
}, this));
this.frozenRenderer.addEventListener('mouseenter', this.boundTick);
}

}

teardownViewer() {
onFrozenMouseEnter() {
this.debug('frozenRenderer.mouseenter');
this.tick(); // renderer will be acquired by renderScene
}

this.$renderer.off('mouseenter');
this.$renderer.off('mouseleave');
teardownViewer() {

this.isFrozen = true;
RendererPool.release(this.renderer);

this.$renderer = null;
this.renderer = null;

if (this.controls) {
this.disableControls();
}

this.$el.css('margin-bottom', 'auto');
this.el.style.marginBottom = 'auto';

}

enableControls() {
this.debug('Enable controls');
this.boundTick = this.tick.bind(this);
var that = this;
this.controls.forEach(function(control) {
control.enabled = true;
control.connectEvents(that.$renderer[0]);
control.connectEvents(that.renderer);
control.addEventListener('change', that.boundTick);
});
}
Expand Down