-
Notifications
You must be signed in to change notification settings - Fork 11
/
view-factory.js
88 lines (80 loc) · 2.32 KB
/
view-factory.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
var Collection = require('ampersand-collection');
var AmpersandModel = require('ampersand-model');
var Chart = require('chart.js');
// extend plot with errorbars
var extendWithErrorBar = require('./chartjs-errorbars');
extendWithErrorBar(Chart, 'line', 'lineError');
extendWithErrorBar(Chart, 'bubble', 'bubbleError');
extendWithErrorBar(Chart, 'bar', 'barError');
extendWithErrorBar(Chart, 'horizontalBar', 'horizontalBarError');
extendWithErrorBar(Chart, 'bubble', 'bubbleError');
// extend plots with a duration scale type
var extendWithDurationScale = require('./chartjs-duration-scale');
extendWithDurationScale(Chart);
/**
* A factory producing the Ampersand views corresponding to the different chart types.
* @example
* var factory = require('./view-factory')
*
* var view = factory.newView(options);
* @module client/view-factory
*/
var widgetEntry = AmpersandModel.extend({
props: {
modelType: {type: 'string', required: true},
newView: {type: 'any', required: false}
}
});
var WidgetCollection = Collection.extend({
model: widgetEntry,
mainIndex: 'modelType'
});
/**
* An Ampersand collection containing all available widgets
*/
module.exports.widgets = new WidgetCollection([
{
modelType: 'piechart',
newView: require('./views/chartjs')
},
{
modelType: 'barchart',
newView: require('./views/chartjs')
},
{
modelType: 'horizontalbarchart',
newView: require('./views/chartjs')
},
{
modelType: 'linechart',
newView: require('./views/chartjs1d')
},
{
modelType: 'radarchart',
newView: require('./views/chartjs')
},
{
modelType: 'bubbleplot',
newView: require('./views/chartjs2d')
},
{
modelType: 'vis3dchart',
newView: require('./views/vis3d')
},
{
modelType: 'networkchart',
newView: require('./views/sigma')
}
// Register new widgets here
]);
/**
* Create a new Ampersand view for a widget
* @param {Object} options - passed on to the view constructor, see https://github.com/AmpersandJS/ampersand-view#constructor-new-ampersandviewoptions
* @param {Object} options.model - The widget
* @returns {View} view - An Ampersand view
*/
module.exports.newView = function newView (options) {
var entry = module.exports.widgets.get(options.model.modelType);
var constructor = entry.newView;
return new constructor(options);
};