Skip to content

Commit

Permalink
Merge pull request #7224 from plotly/drop-jquery-support
Browse files Browse the repository at this point in the history
Drop jQuery support
  • Loading branch information
archmoj authored Oct 10, 2024
2 parents 7916edf + 3931ac3 commit 71bc4ee
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 243 deletions.
1 change: 1 addition & 0 deletions draftlogs/7224_remove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Drop jQuery events support [[#7224](https://github.com/plotly/plotly.js/pull/7224)]
37 changes: 4 additions & 33 deletions src/lib/events.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

/* global jQuery:false */

var EventEmitter = require('events').EventEmitter;

var Events = {
Expand Down Expand Up @@ -56,17 +54,7 @@ var Events = {
plotObj._removeInternalListener = internalEv.removeListener.bind(internalEv);
plotObj._removeAllInternalListeners = internalEv.removeAllListeners.bind(internalEv);

/*
* We must wrap emit to continue to support JQuery events. The idea
* is to check to see if the user is using JQuery events, if they are
* we emit JQuery events to trigger user handlers as well as the EventEmitter
* events.
*/
plotObj.emit = function(event, data) {
if(typeof jQuery !== 'undefined') {
jQuery(plotObj).trigger(event, data);
}

ev.emit(event, data);
internalEv.emit(event, data);
};
Expand All @@ -77,29 +65,19 @@ var Events = {
/*
* This function behaves like jQuery's triggerHandler. It calls
* all handlers for a particular event and returns the return value
* of the LAST handler. This function also triggers jQuery's
* triggerHandler for backwards compatibility.
* of the LAST handler.
*/
triggerHandler: function(plotObj, event, data) {
var jQueryHandlerValue;
var nodeEventHandlerValue;

/*
* If jQuery exists run all its handlers for this event and
* collect the return value of the LAST handler function
*/
if(typeof jQuery !== 'undefined') {
jQueryHandlerValue = jQuery(plotObj).triggerHandler(event, data);
}

/*
* Now run all the node style event handlers
*/
var ev = plotObj._ev;
if(!ev) return jQueryHandlerValue;
if(!ev) return;

var handlers = ev._events[event];
if(!handlers) return jQueryHandlerValue;
if(!handlers) return;

// making sure 'this' is the EventEmitter instance
function apply(handler) {
Expand Down Expand Up @@ -129,14 +107,7 @@ var Events = {
// now call the final handler and collect its value
nodeEventHandlerValue = apply(handlers[i]);

/*
* Return either the jQuery handler value if it exists or the
* nodeEventHandler value. jQuery event value supersedes nodejs
* events for backwards compatibility reasons.
*/
return jQueryHandlerValue !== undefined ?
jQueryHandlerValue :
nodeEventHandlerValue;
return nodeEventHandlerValue;
},

purge: function(plotObj) {
Expand Down
2 changes: 0 additions & 2 deletions test/jasmine/assets/jquery-1.8.3.min.js

This file was deleted.

5 changes: 0 additions & 5 deletions test/jasmine/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ if(isFullSuite) {
testFileGlob = path.join(__dirname, 'tests', glob(merge(argv._).map(basename)));
}

var pathToJQuery = path.join(__dirname, 'assets', 'jquery-1.8.3.min.js');
var pathToCustomMatchers = path.join(__dirname, 'assets', 'custom_matchers.js');
var pathToUnpolyfill = path.join(__dirname, 'assets', 'unpolyfill.js');
var pathToSaneTopojsonDist = path.join(__dirname, '..', '..', 'node_modules', 'sane-topojson', 'dist');
Expand Down Expand Up @@ -318,10 +317,6 @@ func.defaultConfig = {
func.defaultConfig.preprocessors[pathToCustomMatchers] = ['esbuild'];
func.defaultConfig.preprocessors[testFileGlob] = ['esbuild'];

if(!isBundleTest) {
func.defaultConfig.files.push(pathToJQuery);
}

if(argv.virtualWebgl) {
// add virtual-webgl to the top
func.defaultConfig.files = [
Expand Down
203 changes: 0 additions & 203 deletions test/jasmine/tests/events_test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
/* global $:false, jQuery:false */

/*
* Note this test requires JQuery in the global scope.
* we should keep it that way to keep testing our backward
* compatibility with JQuery events.
*/

var Events = require('../../../src/lib/events');

describe('Events', function() {
Expand Down Expand Up @@ -57,19 +49,6 @@ describe('Events', function() {
});
});

it('triggers jquery events', function(done) {
Events.init(plotDiv);

$(plotDiv).bind('ping', function(event, data) {
expect(data).toBe('pong');
done();
});

setTimeout(function() {
$(plotDiv).trigger('ping', 'pong');
});
});

it('mirrors events on an internal handler', function(done) {
Events.init(plotDiv);

Expand Down Expand Up @@ -139,86 +118,6 @@ describe('Events', function() {
expect(result).toBe('pong');
});

it('triggers jQuery handlers when no matching node events bound', function() {
var eventBaton = 0;

Events.init(plotDiv);

$(plotDiv).bind('ping', function() {
eventBaton++;
return 'ping';
});

/*
* This will not be called
*/
plotDiv.on('pong', function() {
eventBaton++;
return 'ping';
});

$(plotDiv).bind('ping', function() {
eventBaton++;
return 'pong';
});

var result = Events.triggerHandler(plotDiv, 'ping');

expect(eventBaton).toBe(2);
expect(result).toBe('pong');
});

it('triggers jQuery handlers when no node events initialized', function() {
var eventBaton = 0;

$(plotDiv).bind('ping', function() {
eventBaton++;
return 'ping';
});

$(plotDiv).bind('ping', function() {
eventBaton++;
return 'ping';
});

$(plotDiv).bind('ping', function() {
eventBaton++;
return 'pong';
});

var result = Events.triggerHandler(plotDiv, 'ping');

expect(eventBaton).toBe(3);
expect(result).toBe('pong');
});


it('triggers jQuery + nodejs handlers and returns last jQuery value', function() {
var eventBaton = 0;

Events.init(plotDiv);

$(plotDiv).bind('ping', function() {
eventBaton++;
return 'ping';
});

plotDiv.on('ping', function() {
eventBaton++;
return 'ping';
});

$(plotDiv).bind('ping', function() {
eventBaton++;
return 'pong';
});

var result = Events.triggerHandler(plotDiv, 'ping');

expect(eventBaton).toBe(3);
expect(result).toBe('pong');
});

it('works with *once* event handlers', function() {
var eventBaton = 0;

Expand Down Expand Up @@ -247,106 +146,4 @@ describe('Events', function() {
expect(plotObj).toEqual({});
});
});

describe('when jQuery.noConflict is set, ', function() {
beforeEach(function() {
$.noConflict();
});

afterEach(function() {
window.$ = jQuery;
});

it('triggers jquery events', function(done) {
Events.init(plotDiv);

jQuery(plotDiv).bind('ping', function(event, data) {
expect(data).toBe('pong');
done();
});

setTimeout(function() {
jQuery(plotDiv).trigger('ping', 'pong');
});
});

it('triggers jQuery handlers when no matching node events bound', function() {
var eventBaton = 0;

Events.init(plotDiv);

jQuery(plotDiv).bind('ping', function() {
eventBaton++;
return 'ping';
});

/*
* This will not be called
*/
plotDiv.on('pong', function() {
eventBaton++;
return 'ping';
});

jQuery(plotDiv).bind('ping', function() {
eventBaton++;
return 'pong';
});

var result = Events.triggerHandler(plotDiv, 'ping');

expect(eventBaton).toBe(2);
expect(result).toBe('pong');
});

it('triggers jQuery handlers when no node events initialized', function() {
var eventBaton = 0;

jQuery(plotDiv).bind('ping', function() {
eventBaton++;
return 'ping';
});

jQuery(plotDiv).bind('ping', function() {
eventBaton++;
return 'ping';
});

jQuery(plotDiv).bind('ping', function() {
eventBaton++;
return 'pong';
});

var result = Events.triggerHandler(plotDiv, 'ping');

expect(eventBaton).toBe(3);
expect(result).toBe('pong');
});

it('triggers jQuery + nodejs handlers and returns last jQuery value', function() {
var eventBaton = 0;

Events.init(plotDiv);

jQuery(plotDiv).bind('ping', function() {
eventBaton++;
return 'ping';
});

plotDiv.on('ping', function() {
eventBaton++;
return 'ping';
});

jQuery(plotDiv).bind('ping', function() {
eventBaton++;
return 'pong';
});

var result = Events.triggerHandler(plotDiv, 'ping');

expect(eventBaton).toBe(3);
expect(result).toBe('pong');
});
});
});

0 comments on commit 71bc4ee

Please sign in to comment.