Skip to content

Commit ccbffd0

Browse files
committedJan 18, 2017
fix and test overrideCursor
1 parent b683903 commit ccbffd0

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed
 

‎src/lib/override_cursor.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
var setCursor = require('./setcursor');
1313

1414
var STASHATTR = 'data-savedcursor';
15+
var NO_CURSOR = '!!';
1516

1617
/*
1718
* works with our CSS cursor classes (see css/_cursor.scss)
@@ -22,10 +23,7 @@ var STASHATTR = 'data-savedcursor';
2223
module.exports = function overrideCursor(el3, csr) {
2324
var savedCursor = el3.attr(STASHATTR);
2425
if(csr) {
25-
if(savedCursor) {
26-
setCursor(el3, csr);
27-
}
28-
else {
26+
if(!savedCursor) {
2927
var classes = (el3.attr('class') || '').split(' ');
3028
for(var i = 0; i < classes.length; i++) {
3129
var cls = classes[i];
@@ -34,10 +32,16 @@ module.exports = function overrideCursor(el3, csr) {
3432
.classed(cls, false);
3533
}
3634
}
35+
if(!el3.attr(STASHATTR)) {
36+
el3.attr(STASHATTR, NO_CURSOR);
37+
}
3738
}
39+
setCursor(el3, csr);
3840
}
3941
else if(savedCursor) {
4042
el3.attr(STASHATTR, null);
41-
setCursor(el3, savedCursor);
43+
44+
if(savedCursor === NO_CURSOR) setCursor(el3);
45+
else setCursor(el3, savedCursor);
4246
}
4347
};

‎test/jasmine/tests/lib_test.js

+51
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var Lib = require('@src/lib');
22
var setCursor = require('@src/lib/setcursor');
3+
var overrideCursor = require('@src/lib/override_cursor');
34

45
var d3 = require('d3');
56
var Plotly = require('@lib');
@@ -1191,6 +1192,56 @@ describe('Test lib.js:', function() {
11911192
});
11921193
});
11931194

1195+
describe('overrideCursor', function() {
1196+
1197+
beforeEach(function() {
1198+
this.el3 = d3.select(createGraphDiv());
1199+
});
1200+
1201+
afterEach(destroyGraphDiv);
1202+
1203+
it('should apply the new cursor(s) and revert to the original when removed', function() {
1204+
this.el3
1205+
.classed('cursor-before', true)
1206+
.classed('not-a-cursor', true)
1207+
.classed('another', true);
1208+
1209+
overrideCursor(this.el3, 'after');
1210+
expect(this.el3.attr('class')).toBe('not-a-cursor another cursor-after');
1211+
1212+
overrideCursor(this.el3, 'later');
1213+
expect(this.el3.attr('class')).toBe('not-a-cursor another cursor-later');
1214+
1215+
overrideCursor(this.el3);
1216+
expect(this.el3.attr('class')).toBe('not-a-cursor another cursor-before');
1217+
});
1218+
1219+
it('should apply the new cursor(s) and revert to the none when removed', function() {
1220+
this.el3
1221+
.classed('not-a-cursor', true)
1222+
.classed('another', true);
1223+
1224+
overrideCursor(this.el3, 'after');
1225+
expect(this.el3.attr('class')).toBe('not-a-cursor another cursor-after');
1226+
1227+
overrideCursor(this.el3, 'later');
1228+
expect(this.el3.attr('class')).toBe('not-a-cursor another cursor-later');
1229+
1230+
overrideCursor(this.el3);
1231+
expect(this.el3.attr('class')).toBe('not-a-cursor another');
1232+
});
1233+
1234+
it('should do nothing if no existing or new override is present', function() {
1235+
this.el3
1236+
.classed('cursor-before', true)
1237+
.classed('not-a-cursor', true);
1238+
1239+
overrideCursor(this.el3);
1240+
1241+
expect(this.el3.attr('class')).toBe('cursor-before not-a-cursor');
1242+
});
1243+
});
1244+
11941245
describe('getTranslate', function() {
11951246

11961247
it('should work with regular DOM elements', function() {

0 commit comments

Comments
 (0)
Please sign in to comment.