Skip to content

Commit

Permalink
add support for div id as 1st arg to Plotly.makeTemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
etpinard committed Dec 26, 2018
1 parent a56fa2a commit f634b39
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/plot_api/template_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ var dfltConfig = require('./plot_config');
* Note: separated from the rest of templates because otherwise we get circular
* references due to PlotSchema.
*
* @param {object|DOM element} figure: The figure to base the template on
* @param {object|DOM element|string} figure: The figure to base the template on
* should contain a trace array `figure.data`
* and a layout object `figure.layout`
* @returns {object} template: the extracted template - can then be used as
* `layout.template` in another figure.
*/
exports.makeTemplate = function(figure) {
figure = Lib.isPlainObject(figure) ? figure : Lib.getGraphDiv(figure);

This comment has been minimized.

Copy link
@archmoj

archmoj Dec 27, 2018

Contributor

Is there any character that should be disqualified e.g. space, empty string, quotation, semicolon, etc.?

This comment has been minimized.

Copy link
@etpinard

etpinard Dec 27, 2018

Author Contributor

I think Lib.getGraphDiv already does a good enough job here:

module.exports = function(gd) {
var gdElement;
if(typeof gd === 'string') {
gdElement = document.getElementById(gd);
if(gdElement === null) {
throw new Error('No DOM element with id \'' + gd + '\' exists on the page.');
}
return gdElement;
}
else if(gd === null || gd === undefined) {
throw new Error('DOM element provided is null or undefined');
}
return gd; // otherwise assume that gd is a DOM element
};

What do you think?

figure = Lib.extendDeep({_context: dfltConfig}, {data: figure.data, layout: figure.layout});
Plots.supplyDefaults(figure);
var data = figure.data || [];
Expand Down
34 changes: 34 additions & 0 deletions test/jasmine/tests/template_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,40 @@ describe('makeTemplate', function() {
.then(destroyGraphDiv)
.then(done);
});

it('works with div id', function(done) {
var mock = Lib.extendDeep({}, scatterFillMock);

var gd = document.createElement('div');
gd.id = 'myDiv';
document.body.appendChild(gd);

Plotly.newPlot('myDiv', mock)
.then(function() {
var template = Plotly.makeTemplate('myDiv');
delete(template.layout.xaxis);
delete(template.layout.yaxis);
expect(template).toEqual({
data: {scatter: [
{fill: 'tonext', line: {shape: 'spline'}},
{fill: 'tonext'},
{fill: 'toself'}
] },
layout: {
title: {
text: 'Fill toself and tonext'
},
width: 400,
height: 400
}
});
})
.catch(failTest)
.then(function() {
document.body.removeChild(gd);
})
.then(done);
});
});

// statics of template application are all covered by the template mock
Expand Down

0 comments on commit f634b39

Please sign in to comment.