forked from gongfudev/Exquis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexquis.js
188 lines (158 loc) · 7.22 KB
/
exquis.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
"use strict";
define([ "csshelper", "evileval", "net", "menubar", "controlPanel", "assemblageController", "factory"], function(
csshelper, evileval, net, menubar, makeControlPanel, makeAssemblageController, factory){
const addCellUItoCell = function(cell){
const {row, col} = cell;
const {height, width} = cell.context.canvas;
cell.ui = makeCellUi(row, col, height, width);
}
const addHintToCell = function(cell){
const {row, col} = cell;
const {height, width} = cell.context.canvas;
cell.hint = factory.createCellDiv("hint", row, col, height, width);
cell.hint.style.border = '1px solid rgba(200, 200, 200, 0.5)';
cell.hint.style.width = (width - 2) + "px";
cell.hint.style.height = (height - 2) + "px";
}
const addCodeHandling = canvasAnim => {
return Object.assign(canvasAnim,{
//TODO add the following functions in a further step
// maybe as a subclass :O
// then export the makeCanvasAnim function for use in observablehq
addCodeStringToEvaluate: function(codeString){
this.evaluateCode = function(){
return new Promise((resolve, reject) => {
var codeAsUri = evileval.toDataUri(codeString);
evileval.loadJsAnim(codeAsUri)
.then((evaluatedAnimationClone) => {
const animationName =
net.extractAnimationNameFromUri(this.originalUrl);
this.setAnimation(evaluatedAnimationClone,
this.originalUrl, animationName);
this.codeCacheUri = codeAsUri;
resolve();
})
.catch( function(err){
console.log(err);
reject(err);
});
});
};
},
loadAnim: function(url){
return evileval.loadJsAnim(url).then(
function(evaluatedAnimationClone){
this.animationState = {};
// console.log('created animationState', this.animationState);
const animationName =
net.extractAnimationNameFromUri(url);
this.setAnimation(evaluatedAnimationClone, url, animationName);
this.codeCacheUri = null;
return this;
}.bind(this));
},
getSourceCode: function(){
if(this.currentCode.source){
// if the source code is not javascript
// it is stored in the attribute source
// of the current animation.
return Promise.resolve(this.currentCode.source);
}
return this.getSourceCodeString().then(function(scs){
return {code: scs, lang: 'javascript'};
});
},
getSourceCodeString: function(){
if (this.codeCacheUri){
// the code is in the cache
return new Promise(function(resolve, reject){
var animCodeString = evileval.dataUri2text(this.codeCacheUri);
resolve(animCodeString);
}.bind(this));
}else{
// get the code from the internets
return net.HTTPget(this.originalUrl)
.then(function(animCodeString){
var url = this.originalUrl;
this.animationName =
net.extractAnimationNameFromUri(url);
return animCodeString;
}.bind(this));
}
}
})
};
var makeCellUi = function(row, col, height, width){
var cellUi = factory.createCellDiv("cellUi", row, col, height, width);
return cellUi;
};
var addControlPanelIconHandler = function(cell, controlPanel){
cell.ui.addEventListener('click', function(){
csshelper.addClassForSelector('invisible','.hint');
cell.hint.classList.remove('invisible');
controlPanel.show(cell);
});
};
var addEditor = function(exquis, controlPanel, editorController){
exquis.editorController = editorController;
controlPanel.addEditor(editorController);
};
var hideHints = function(){
csshelper.addClassForSelector('invisible','.cellUi, .hint');
};
var showHints = function(){
csshelper.removeClassForSelector('invisible','.cellUi, .hint');
};
var init = function (assName, animUris, makeEditorController, store,
{cellWidth=150, cellHeight=150}) {
const exquis = {};
const controlPanel = makeControlPanel(store);
exquis.assName = assName;
const dashboardWidth = animUris[0].length * cellWidth;
const assemblageController = makeAssemblageController(exquis, store);
menubar.init(dashboardWidth);
menubar.addCloseListener(hideHints);
menubar.addCloseListener(function(){
controlPanel.hide();
});
menubar.addOpenListener(showHints);
menubar.addAssemblageLoadListener(assemblageController.load);
menubar.addAssemblageSaveAsListener(assemblageController.saveAs);
var possiblyHideControlPanel = function(event){
if (event.target.tagName === "HTML"){
csshelper.removeClassForSelector('invisible','.hint');
controlPanel.hide();
}
};
document.addEventListener('click', possiblyHideControlPanel, true);
const colCount = animUris.length;
const rowCount = animUris[0].length;
let parent = document.getElementById("dashboard");
exquis.cells = factory.makeCells( colCount, rowCount, cellHeight, cellWidth, parent);
factory.forEach2d(animUris, (animUri, rowIndex, colIndex) => {
const cell = exquis.cells[rowIndex][colIndex];
addHintToCell(cell);
addCellUItoCell(cell);
addControlPanelIconHandler(cell, controlPanel);
addCodeHandling(cell.canvasAnim);
cell.canvasAnim.loadAnim(animUri);
});
exquis.assemblage = function(){
var animationNames = factory.map2d(
this.cells,
function(cell, row, col){
return cell.canvasAnim.animationName;
});
return animationNames;
};
var editorController = makeEditorController(exquis, store);
addEditor(exquis, controlPanel, editorController);
var render = async function(){
await factory.draw(exquis.cells, exquis.editorController.displayInvalidity);
requestAnimationFrame(render);
};
render();
return exquis;
};
return init;
});