8000 Introducing transform plugins by etpinard · Pull Request #499 · plotly/plotly.js · GitHub
[go: up one dir, main page]

Skip to content

Introducing transform plugins #499

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

Merged
merged 27 commits into from
Jul 22, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
53a5971
expose Lib on test dashboard window
etpinard May 3, 2016
9909382
lint
etpinard May 3, 2016
28147e2
add check so that we don't try to destroy gd if it DNE
etpinard May 3, 2016
9ac6d0f
make Plotly.register handle transform modules
etpinard May 3, 2016
06f2e6d
add support for transform plugins:
etpinard May 3, 2016
8ab9d0c
make Plotly.restyle support transforms:
etpinard May 3, 2016
2697975
add two example transform modules
etpinard May 3, 2016
56665f2
add transform test suite
etpinard May 3, 2016
aa274ef
update tests (plots.supplyDataDefaults -> supplyTraceDefaults)
etpinard May 3, 2016
a06441a
[tmp commit] add example transforms to ./lib/ Plotly for testing
etpinard May 3, 2016
0b62e45
Merge branch 'master' into transform-plugins
etpinard Jul 4, 2016
1c651e8
toimage_test: make sure that all graph divs are deleted
etpinard Jul 4, 2016
f93b9c1
plots: rm useless skip-over-step
etpinard Jul 4, 2016
ccbcb58
Merge branch 'master' into transform-plugins
etpinard Jul 18, 2016
37e291b
plots: make relinkPrivateKeys less strict
etpinard Jul 19, 2016
383c6d4
move common code in pushModule func
etpinard Jul 19, 2016
be36402
rm useless comment about fits ;)
etpinard Jul 19, 20 8000 16
0e1eb5a
do not add transforms container if fullTrace is no transform present
etpinard Jul 19, 2016
6a8e158
try more useless refs to input container in full trace
etpinard Jul 19, 2016
9a341c1
mutate uid of expanded traces w.r.t to expanded index:
etpinard Jul 19, 2016
1752d30
make uid of expanded trace valid alpha-numerical hashes
etpinard Jul 19, 2016
b5ec01e
add support for transform in plot schema and Plotly.validate
etpinard Jul 20, 2016
a6fda02
improve validate test cases
etpinard Jul 20, 2016
da2c24c
validate transform module on registration:
etpinard Jul 20, 2016
8d7dc46
improve transform tests
etpinard Jul 20, 2016
320d62e
Revert "[tmp commit] add example transforms to ./lib/ Plotly for test…
etpinard Jul 20, 2016
3e6e9c1
rm @assets path shortcut,
etpinard Jul 20, 2016
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
Prev Previous commit
Next Next commit
validate transform module on registration:
- module.transform must be a function
- module.attributes and module.supplyDefaults are
  recommended but optional
- log warning if transform module associated with transform
  type isn't found
- extend traceIn if no supplyDefaults method is found.
  • Loading branch information
etpinard committed Jul 20, 2016
commit da2c24c4f4e16f2cc290ba2bd3570749fcc5d6c1
21 changes: 19 additions & 2 deletions src/plotly.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
require('es6-promise').polyfill();

// lib functions
exports.Lib = require('./lib');
var Lib = exports.Lib = require('./lib');
exports.util = require('./lib/svg_text_utils');
exports.Queue = require('./lib/queue');

Expand Down Expand Up @@ -55,7 +55,8 @@ exports.ModeBar = require('./components/modebar');
exports.register = function register(_modules) {
if(!_modules) {
throw new Error('No argument passed to Plotly.register.');
} else if(_modules && !Array.isArray(_modules)) {
}
else if(_modules && !Array.isArray(_modules)) {
_modules = [_modules];
}

Expand All @@ -77,6 +78,22 @@ exports.register = function register(_modules) {
break;

case 'transform':
if(typeof newModule.name !== 'string') {
throw new Error('Transform module *name* must be a string.');
}

var prefix = 'Transform module ' + newModule.name;

if(typeof newModule.transform !== 'function') {
throw new Error(prefix + ' is missing a *transform* function.');
}
if(!Lib.isPlainObject(newModule.attributes)) {
Lib.log(prefix + ' registered without an *attributes* object.');
}
if( 8000 typeof newModule.supplyDefaults !== 'function') {
Lib.log(prefix + ' registered without a *supplyDefaults* function.');
}

Plots.transformsRegistry[newModule.name] = newModule;

break;
Expand Down
28 changes: 19 additions & 9 deletions src/plots/plots.js
Original file line number Diff line number Diff line change
Expand Up @@ -758,10 +758,18 @@ function supplyTransformDefaults(traceIn, traceOut, layout) {
for(var i = 0; i < containerIn.length; i++) {
var transformIn = containerIn[i],
type = transformIn.type,
_module = transformsRegistry[type];
_module = transformsRegistry[type],
transformOut;

if(!_module) Lib.warn('Unrecognized transform type ' + type + '.');

var transformOut = _module.supplyDefaults(transformIn, traceOut, layout);
transformOut.type = type;
if(_module && _module.supplyDefaults) {
transformOut = _module.supplyDefaults(transformIn, traceOut, layout);
transformOut.type = type;
}
else {
transformOut = Lib.extendFlat({}, transformIn);
}

containerOut.push(transformOut);
}
Expand All @@ -776,12 +784,14 @@ function applyTransforms(fullTrace, fullData, layout) {
type = transform.type,
_module = transformsRegistry[type];

dataOut = _module.transform(dataOut, {
transform: transform,
fullTrace: fullTrace,
fullData: fullData,
layout: layout
});
if(_module) {
dataOut = _module.transform(dataOut, {
transform: transform,
fullTrace: fullTrace,
fullData: fullData,
layout: layout
});
}
}

return dataOut;
Expand Down
33 changes: 33 additions & 0 deletions test/jasmine/tests/register_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe('the register function', function() {
this.modulesKeys = Object.keys(Plots.modules);
this.allTypesKeys = Object.keys(Plots.allTypes);
this.allCategoriesKeys = Object.keys(Plots.allCategories);
this.allTransformsKeys = Object.keys(Plots.transformsRegistry);
});

afterEach(function() {
Expand All @@ -21,6 +22,7 @@ describe('the register function', function() {
revertObj(Plots.modules, this.modulesKeys);
revertObj(Plots.allTypes, this.allTypesKeys);
revertObj(Plots.allCategories, this.allCategoriesKeys);
revertObj(Plots.transformsRegistry, this.allTransformsKeys);
});

it('should throw an error when no argument is given', function() {
Expand Down Expand Up @@ -68,4 +70,35 @@ describe('the register function', function() {
Plotly.register([invalidTrace]);
}).toThrowError(Error, 'Invalid module was attempted to be registered!');
});

it('should throw when if transform module is invalid', function() {
var missingTransformName = {
moduleType: 'transform'
};

expect(function() {
Plotly.register(missingTransformName);
}).toThrowError(Error, 'Transform module *name* must be a string.');

var missingTransformFunc = {
moduleType: 'transform',
name: 'mah-transform'
};

expect(function() {
Plotly.register(missingTransformFunc);
}).toThrowError(Error, 'Transform module mah-transform is missing a *transform* function.');

var transformModule = {
moduleType: 'transform',
name: 'mah-transform',
transform: function() {}
};

expect(function() {
Plotly.register(transformModule);
}).not.toThrow();

expect(Plots.transformsRegistry['mah-transform']).toBeDefined();
});
});
0