-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscripts.js
More file actions
126 lines (109 loc) · 3.43 KB
/
scripts.js
File metadata and controls
126 lines (109 loc) · 3.43 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* Constructs the widget.
* Args:
* ctsTarget - the CTS Node the widget is being constructed in.
* ctsSource - the CTS Node that represents the widget being copied
* ctsRelation - the CTS Relation that caused this invocation
*
*/
function processingWidget_Init(ctsTarget, ctsSource, ctsRelation) {
console.log("processingWidget :: Init");
var widgetContainerNode = ctsTarget.value;
var tryIt = function() {
if (processingWidget_DependenciesLoaded()) {
processingWidget_ConstructUI(widgetContainerNode);
} else {
setTimeout(tryIt, 100);
}
}
tryIt();
}
function Datapoint(d) {
this.d = d;
}
function processingWidget_DependenciesLoaded() {
return (
true
)
}
function processingWidget_ConstructUI(node) {
var data = processingWidget_GrabData();
function parseVal(ctx, val) {
alert(val + '' + parseFloat(val) + '' + isNaN(parseFloat(val)))
var floatAttempt = parseFloat(val);
// 42
if (! isNaN(floatAttempt)) return floatAttempt;
// col(Property)
if (val.indexOf("col") > -1) {
return ctx[eval("'" + val.replace("col(", "").replace(")", "") + "'" )];
}
return val;
}
// Now that we have the transformations, we can set the
// run finction on the datapoint.
Datapoint.prototype.run = function() {
for (var j = 0; j < data.transformations.length; j++) {
var transformation = data.transformations[j];
if (transformation.length > 0) {
// There's a command to run.
var all = [];
if (transformation.length > 1) {
var k = 1;
while ((k < transformation.length) && (typeof transformation[k] != 'undefined')) {
all.push(parseVal(this.d, transformation[k]));
k++;
}
}
// console.log(all);
// alert(all);
// eval(transformation[0]).apply(this, all);
var cmd = transformation[0] + "(" + all.join(",") + ")";
eval(cmd);
}
}
};
// And now we do it.
data.smartDatapoints = [];
for (var i = 0; i < data.data.length; i++) {
data.smartDatapoints.push(new Datapoint(data.data[i]));
}
window.draw = function() {
background([40, 40, 40]);
for (var i = 0; i < data.smartDatapoints.length; i++) {
data.smartDatapoints[i].run();
}
}
window.setup = function() {
createCanvas(parseInt(data.settings.Width), parseInt(data.settings.Height));
frameRate(parseInt(data.settings.frameRate));
}
// Now add processing to the page
var p5 = document.createElement('script');
p5.setAttribute('src','p5.min.js');
document.head.appendChild(p5);
CTS.Util.$(function($) {
w = CTS.Util.$(node).width();
h = CTS.Util.$(node).height();
setInterval(function() {
w = CTS.Util.$(node).width();
h = CTS.Util.$(node).height();
}, 20);
});
}
/*
* Assumptions:
* Spreadsheet label is: processingDatasource
* Spreadsheet has worksheets: data, transformations, settings
*/
function processingWidget_GrabData() {
// data: an array of objects
var data = CTS('processingDatasource|data!rows').nodes[0].toJson();
// settings: a single object
var settings = CTS('processingDatasource|settings!rows').nodes[0].toJson()[0];
// transformations: an array of arrays
var transformations = CTS('processingDatasource|transformations!rows').nodes[0].parentNode.cellFeedNode.getCsv();
return {
data: data,
settings: settings,
transformations: transformations
};
}