Skip to content

Commit 3127dee

Browse files
committed
Rewrite accessibility in es5
1 parent 7baa0d3 commit 3127dee

File tree

1 file changed

+55
-65
lines changed

1 file changed

+55
-65
lines changed

src/plot_api/accessibility.js

Lines changed: 55 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,83 @@
1-
"use strict";
1+
'use strict';
22

3-
var underlyingModule = import('chart2music');
3+
var c2m = require('chart2music');
4+
var Fx = require('../components/fx');
45

5-
6-
const supportedAccessibilityLibraries = ['chart2music'];
6+
var supportedAccessibilityLibraries = ['chart2music'];
77

88
function enable(gd) {
9-
const {library, options} = gd._context.accessibility;
10-
if (!supportedAccessibilityLibraries.includes(library)) {
9+
var accessibilityVars = gd._context.accessibility;
10+
var library = accessibilityVars.library;
11+
var options = accessibilityVars.options;
12+
if(!supportedAccessibilityLibraries.includes(library)) {
1113
// 'Accessibility not implemented for library: ' + library
1214
return;
1315
}
14-
console.log("We're enabled!");
15-
if (library === 'chart2music') {
16-
console.log("Library === chart2music");
17-
const c2mData = {};
18-
const labels = [];
19-
const info = options.info;
16+
if(library === 'chart2music') {
17+
var c2mData = {};
18+
var labels = [];
19+
var info = options.info;
2020
delete options.info;
21-
const fullData = gd._fullData;
21+
var fullData = gd._fullData;
2222

2323
for(var i = 0; i < fullData.length; i++) {
24-
var trace = fullData[i] ?? {};
25-
var {type, x = [], y = [], name = i, text = []} = trace;
24+
var trace = fullData[i] ? fullData[i] : {};
25+
var type = trace.type;
26+
var x = trace.x ? trace.x : [];
27+
var y = trace.y ? trace.y : [];
28+
var name = trace.name ? trace.name : i;
29+
var text = trace.text ? trace.text : [];
2630
if(type === 'scatter') {
27-
console.log("Found a scatter");
2831
var traceData = [];
29-
if ('y' in trace) {
32+
if('y' in trace) {
3033
for(var p = 0; p < y.length; p++) {
3134
traceData.push(
3235
{
3336
x: x.length > 0 ? x[p] : p,
3437
y: y[p],
35-
label: text[p] ?? p
36-
})
38+
label: text[p] ? text[p] : p
39+
});
3740
}
38-
console.log("TraceData.length: " + String(traceData.length));
3941
c2mData[name] = traceData;
4042
labels.push(name);
4143
}
42-
}
43-
else {
44+
} else {
4445
// 'Accessibility not implemented for trace type: ' + trace.type
45-
console.log("Accessibility not implemented for trace type.");
4646
return;
47-
};
48-
};
49-
50-
var closed_captions = document.createElement('div');
51-
closed_captions.id = 'cc';
52-
closed_captions.className = 'closed_captions';
53-
gd.appendChild(closed_captions); // this does get generated
47+
}
48+
}
5449

55-
const {
56-
title: {text: title_text = ''} = {},
57-
xaxis: {title: {text: xaxis_text = ''} = {}} = {},
58-
yaxis: {title: {text: yaxis_text = ''} = {}} = {},
59-
} = gd._fullLayout; // I do not understand this notation that well anyway.
50+
var closedCaptions = document.createElement('div');
51+
closedCaptions.id = 'cc';
52+
closedCaptions.className = 'closed_captions';
53+
gd.appendChild(closedCaptions); // this does get generated
6054

61-
return underlyingModule.then( function(mod) {
62-
console.log("Got underlying module!");
63-
console.log(mod);
64-
var ret = mod.c2mChart({
65-
title: title_text,
66-
type: "line",
67-
axes: {
68-
x: {
69-
label: xaxis_text
70-
},
71-
y: {
72-
label: yaxis_text
73-
}
55+
var titleText = gd._fullLayout.title.text ? gd._fullLayout.title.text : 'Chart';
56+
var xaxisText = gd._fullLayout.xaxis.title.text ? gd._fullLayout.xaxis.title.text : 'X Axis';
57+
var yaxisText = gd._fullLayout.yaxis.title.text ? gd._fullLayout.yaxis.title.text : 'Y Axis';
58+
options.onFocusCallback = function(dataInfo) {
59+
Fx.hover(gd, [{
60+
curveNumber: labels.indexOf(dataInfo.slice),
61+
pointNumber: dataInfo.index
62+
}]);
63+
};
64+
c2m.c2mChart({
65+
title: titleText,
66+
type: 'line',
67+
axes: {
68+
x: {
69+
label: xaxisText
7470
},
75-
element: gd,
76-
cc: closed_captions,
77-
data: c2mData,
78-
options: {
79-
onFocusCallback: ({slice, index}) => {
80-
Plotly.Fx.hover(gd, [{
81-
curveNumber: labels.indexOf(slice),
82-
pointNumber: index
83-
}])
84-
},
85-
...options
71+
y: {
72+
label: yaxisText
8673
},
87-
info: info
88-
});
89-
console.log(ret);
74+
},
75+
element: gd,
76+
cc: closedCaptions,
77+
data: c2mData,
78+
options: options,
79+
info: info
9080
});
91-
};
92-
};
93-
module.exports = { enable };
81+
}
82+
}
83+
exports.enable = enable;

0 commit comments

Comments
 (0)