Skip to content

Config options coerce / schema #1188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/lib/loggers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
/* eslint-disable no-console */

var config = require('../plot_api/plot_config');
var loggingDflt = config.dflt;

var loggers = module.exports = {};

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

loggers.log = function() {
if(config.logging > 1) {
if(loggingDflt > 1) {
var messages = ['LOG:'];

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

loggers.warn = function() {
if(config.logging > 0) {
if(loggingDflt > 0) {
var messages = ['WARN:'];

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

loggers.error = function() {
if(config.logging > 0) {
if(loggingDflt > 0) {
var messages = ['ERROR:'];

for(var i = 0; i < arguments.length; i++) {
Expand Down
3 changes: 1 addition & 2 deletions src/lib/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
'use strict';

var Lib = require('../lib');
var config = require('../plot_api/plot_config');


/**
Expand Down Expand Up @@ -91,7 +90,7 @@ queue.add = function(gd, undoFunc, undoArgs, redoFunc, redoArgs) {
queueObj.redo.args.push(redoArgs);
}

if(gd.undoQueue.queue.length > config.queueLength) {
if(gd.undoQueue.queue.length > gd._context.queueLength) {
gd.undoQueue.queue.shift();
gd.undoQueue.index--;
}
Expand Down
51 changes: 35 additions & 16 deletions src/plot_api/plot_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var ErrorBars = require('../components/errorbars');
var xmlnsNamespaces = require('../constants/xmlns_namespaces');
var svgTextUtils = require('../lib/svg_text_utils');

var defaultConfig = require('./plot_config');
var helpers = require('./helpers');
var subroutines = require('./subroutines');

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

// transfer configuration options to gd until we move over to
// a more OO like model

setPlotContext(gd, config);

if(!layout) layout = {};
Expand Down Expand Up @@ -363,32 +365,49 @@ Plotly.plot = function(gd, data, layout, config) {
});
};


function opaqueSetBackground(gd, bgColor) {
gd._fullLayout._paperdiv.style('background', 'white');
Plotly.defaultConfig.setBackground(gd, bgColor);
defaultConfig.setBackground.dflt(gd, bgColor);
}

function setPlotContext(gd, config) {
if(!gd._context) gd._context = Lib.extendFlat({}, Plotly.defaultConfig);
var context = gd._context;
var keys, i;

if(config) {
Object.keys(config).forEach(function(key) {
if(key in context) {
if(key === 'setBackground' && config[key] === 'opaque') {
context[key] = opaqueSetBackground;
}
else context[key] = config[key];
}
});
if(!gd._context) {
gd._context = {};
keys = Object.keys(defaultConfig);

// map plot3dPixelRatio to plotGlPixelRatio for backward compatibility
if(config.plot3dPixelRatio && !context.plotGlPixelRatio) {
context.plotGlPixelRatio = context.plot3dPixelRatio;
// init context
for(i = 0; i < keys.length; i++) {
var k = keys[i];
gd._context[k] = defaultConfig[k].dflt;
}
}

var context = gd._context;

config = Lib.isPlainObject(config) ? config : {};
keys = Object.keys(config);

function coerce(key, dflt) {
return Lib.coerce(config, context, defaultConfig, key, dflt);
}

// extend context with config
for(i = 0; i < keys.length; i++) {
coerce(keys[i]);
}

// 'opaque' is special value for setBackground
if(config.setBackground === 'opaque') {
context.setBackground = opaqueSetBackground;
}

// map plot3dPixelRatio to plotGlPixelRatio for backward compatibility
if(config.plot3dPixelRatio && !context.plotGlPixelRatio) {
context.plotGlPixelRatio = context.plot3dPixelRatio;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this happen to the input object before coercing, so it can be validated? Anyway if it stays here it would need to be context.plotGlPixelRatio = config.plot3dPixelRatio;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comment. I discovered some breaking behavior.

As this PR isn't important to anyone right now, I'll drop it from the 1.21.1 release.

}

// staticPlot forces a bunch of others:
if(context.staticPlot) {
context.editable = false;
Expand Down
Loading