Debian Codegraph @

examples/js/extensions.js

raw
1 var RenderControls = function(args) {
2
3 var $ = jQuery;
4
5 this.initialize = function() {
6
7 this.element = args.element;
8 this.graph = args.graph;
9 this.settings = this.serialize();
10
11 this.inputs = {
12 renderer: this.element.elements.renderer,
13 interpolation: this.element.elements.interpolation,
14 offset: this.element.elements.offset
15 };
16
17 this.element.addEventListener('change', function(e) {
18
19 this.settings = this.serialize();
20
21 if (e.target.name == 'renderer') {
22 this.setDefaultOffset(e.target.value);
23 }
24
25 this.syncOptions();
26 this.settings = this.serialize();
27
28 var config = {
29 renderer: this.settings.renderer,
30 interpolation: this.settings.interpolation
31 };
32
33 if (this.settings.offset == 'value') {
34 config.unstack = true;
35 config.offset = 'zero';
36 } else if (this.settings.offset == 'expand') {
37 config.unstack = false;
38 config.offset = this.settings.offset;
39 } else {
40 config.unstack = false;
41 config.offset = this.settings.offset;
42 }
43
44 this.graph.configure(config);
45 this.graph.render();
46
47 }.bind(this), false);
48 }
49
50 this.serialize = function() {
51
52 var values = {};
53 var pairs = $(this.element).serializeArray();
54
55 pairs.forEach( function(pair) {
56 values[pair.name] = pair.value;
57 } );
58
59 return values;
60 };
61
62 this.syncOptions = function() {
63
64 var options = this.rendererOptions[this.settings.renderer];
65
66 Array.prototype.forEach.call(this.inputs.interpolation, function(input) {
67
68 if (options.interpolation) {
69 input.disabled = false;
70 input.parentNode.classList.remove('disabled');
71 } else {
72 input.disabled = true;
73 input.parentNode.classList.add('disabled');
74 }
75 });
76
77 Array.prototype.forEach.call(this.inputs.offset, function(input) {
78
79 if (options.offset.filter( function(o) { return o == input.value } ).length) {
80 input.disabled = false;
81 input.parentNode.classList.remove('disabled');
82
83 } else {
84 input.disabled = true;
85 input.parentNode.classList.add('disabled');
86 }
87
88 }.bind(this));
89
90 };
91
92 this.setDefaultOffset = function(renderer) {
93
94 var options = this.rendererOptions[renderer];
95
96 if (options.defaults && options.defaults.offset) {
97
98 Array.prototype.forEach.call(this.inputs.offset, function(input) {
99 if (input.value == options.defaults.offset) {
100 input.checked = true;
101 } else {
102 input.checked = false;
103 }
104
105 }.bind(this));
106 }
107 };
108
109 this.rendererOptions = {
110
111 area: {
112 interpolation: true,
113 offset: ['zero', 'wiggle', 'expand', 'value'],
114 defaults: { offset: 'zero' }
115 },
116 line: {
117 interpolation: true,
118 offset: ['expand', 'value'],
119 defaults: { offset: 'value' }
120 },
121 bar: {
122 interpolation: false,
123 offset: ['zero', 'wiggle', 'expand', 'value'],
124 defaults: { offset: 'zero' }
125 },
126 scatterplot: {
127 interpolation: false,
128 offset: ['value'],
129 defaults: { offset: 'value' }
130 }
131 };
132
133 this.initialize();
134 };
135