Skip to content

Commit 1417622

Browse files
committed
rewrite plot_config as valObjects
- and rewrite setPlotConfig + setPlotContext
1 parent 846f2c7 commit 1417622

File tree

5 files changed

+268
-84
lines changed

5 files changed

+268
-84
lines changed

src/lib/loggers.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
/* eslint-disable no-console */
1212

1313
var config = require('../plot_api/plot_config');
14+
var loggingDflt = config.dflt;
1415

1516
var loggers = module.exports = {};
1617

@@ -21,7 +22,7 @@ var loggers = module.exports = {};
2122
*/
2223

2324
loggers.log = function() {
24-
if(config.logging > 1) {
25+
if(loggingDflt > 1) {
2526
var messages = ['LOG:'];
2627

2728
for(var i = 0; i < arguments.length; i++) {
@@ -37,7 +38,7 @@ loggers.log = function() {
3738
};
3839

3940
loggers.warn = function() {
40-
if(config.logging > 0) {
41+
if(loggingDflt > 0) {
4142
var messages = ['WARN:'];
4243

4344
for(var i = 0; i < arguments.length; i++) {
@@ -53,7 +54,7 @@ loggers.warn = function() {
5354
};
5455

5556
loggers.error = function() {
56-
if(config.logging > 0) {
57+
if(loggingDflt > 0) {
5758
var messages = ['ERROR:'];
5859

5960
for(var i = 0; i < arguments.length; i++) {

src/plot_api/plot_api.js

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var ErrorBars = require('../components/errorbars');
2828
var xmlnsNamespaces = require('../constants/xmlns_namespaces');
2929
var svgTextUtils = require('../lib/svg_text_utils');
3030

31+
var defaultConfig = require('./plot_config');
3132
var helpers = require('./helpers');
3233
var subroutines = require('./subroutines');
3334

@@ -80,6 +81,7 @@ Plotly.plot = function(gd, data, layout, config) {
8081

8182
// transfer configuration options to gd until we move over to
8283
// a more OO like model
84+
8385
setPlotContext(gd, config);
8486

8587
if(!layout) layout = {};
@@ -363,32 +365,49 @@ Plotly.plot = function(gd, data, layout, config) {
363365
});
364366
};
365367

366-
367368
function opaqueSetBackground(gd, bgColor) {
368369
gd._fullLayout._paperdiv.style('background', 'white');
369-
Plotly.defaultConfig.setBackground(gd, bgColor);
370+
defaultConfig.setBackground.dflt(gd, bgColor);
370371
}
371372

372373
function setPlotContext(gd, config) {
373-
if(!gd._context) gd._context = Lib.extendFlat({}, Plotly.defaultConfig);
374-
var context = gd._context;
374+
var keys, i;
375375

376-
if(config) {
377-
Object.keys(config).forEach(function(key) {
378-
if(key in context) {
379-
if(key === 'setBackground' && config[key] === 'opaque') {
380-
context[key] = opaqueSetBackground;
381-
}
382-
else context[key] = config[key];
383-
}
384-
});
376+
if(!gd._context) {
377+
gd._context = {};
378+
keys = Object.keys(defaultConfig);
385379

386-
// map plot3dPixelRatio to plotGlPixelRatio for backward compatibility
387-
if(config.plot3dPixelRatio && !context.plotGlPixelRatio) {
388-
context.plotGlPixelRatio = context.plot3dPixelRatio;
380+
// init context
381+
for(i = 0; i < keys.length; i++) {
382+
var k = keys[i];
383+
gd._context[k] = defaultConfig[k].dflt;
389384
}
390385
}
391386

387+
var context = gd._context;
388+
389+
config = Lib.isPlainObject(config) ? config : {};
390+
keys = Object.keys(config);
391+
392+
function coerce(key, dflt) {
393+
return Lib.coerce(config, context, defaultConfig, key, dflt);
394+
}
395+
396+
// extend context with config
397+
for(i = 0; i < keys.length; i++) {
398+
coerce(keys[i]);
399+
}
400+
401+
// 'opaque' is special value for setBackground
402+
if(config.setBackground === 'opaque') {
403+
context.setBackground = opaqueSetBackground;
404+
}
405+
406+
// map plot3dPixelRatio to plotGlPixelRatio for backward compatibility
407+
if(config.plot3dPixelRatio && !context.plotGlPixelRatio) {
408+
context.plotGlPixelRatio = context.plot3dPixelRatio;
409+
}
410+
392411
// staticPlot forces a bunch of others:
393412
if(context.staticPlot) {
394413
context.editable = false;

0 commit comments

Comments
 (0)