Skip to content

Commit 7cec79e

Browse files
separate concept originUrl and codeCacheUri on CanvasAnim
1 parent e221470 commit 7cec79e

File tree

5 files changed

+33
-43
lines changed

5 files changed

+33
-43
lines changed

doc.org

+3-4
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ net.loadAssemblage(x, assName, init)
1616
exquis.init is called with all the loaded animations as argument
1717
exquis.init creates an exquis object and calls makeEditor(exquis)
1818

19-
* ev.loadJsAnimOnCanvasAnim
19+
* ev.loadJsAnimOnCanvasAnim DELETED
2020
loads animationCode from uri with requirejs
2121
sets canvasAnim.animationName
2222
TODO compute the animation name from the url instead of taking an argument
23-
** evileval.evalAnimation
23+
** evileval.evalAnimation DELETED
2424
converts codeString to data uri
2525
TODO converting the codeString to data uri is now the job of canvasAnim.setCodeString
2626
delete this method
@@ -39,7 +39,6 @@ or else loads it from the store with originalUrl and calls setCodeString
3939

4040
***** editorController.textAreaController.updateWithCanvasAnim (if ! match data:)
4141
- loads the code as text (codeString) from the url TODO put this in canvasAnim.getSourceCodeString
42-
- stores it in a data uri TODO put this on canvasAnim.setCodeString
4342
- puts the codeString in the editor
4443

4544
******* TODO find a better name for this method: editorController.loadCodeString(canvasAnim)?
@@ -61,7 +60,7 @@ The store needs to know where to save an animation based on its url.
6160
Currently the original url is replaced by a data uri when it is first edited,
6261
because it's only used to load the code. We need to keep it in order to save it.
6362

64-
******* TODO canvasAnim.originalUrl .codeUri
63+
******* DONE canvasAnim.originalUrl .codeUri
6564
originalUrl is the address from where the animation is first loaded
6665
codeUri is a data uri that contains the code being edited
6766

public/evileval.js

+1-18
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
define([], function(){
22

3-
var evalAnimation = function(codeString, canvasAnim){
4-
// TODO this is not a path, rename
5-
var jsAnimPath = toDataUri(codeString);
6-
return loadJsAnimOnCanvasAnim(jsAnimPath, canvasAnim, canvasAnim.animationName);
7-
};
8-
93
var loadJsAnim = function(jsAnimPath){
104
return new Promise(function(resolve, reject){
115
require([jsAnimPath],
@@ -18,16 +12,6 @@ define([], function(){
1812
});
1913
};
2014

21-
// TODO this method should only be called by a canvasAnim who uses the store to
22-
// convert path to name
23-
var loadJsAnimOnCanvasAnim = function(jsAnimPath, canvasAnim, animationName){
24-
return loadJsAnim(jsAnimPath)
25-
.then(function(evaluatedAnimationClone){
26-
canvasAnim.setAnimation(evaluatedAnimationClone, jsAnimPath);
27-
return canvasAnim;
28-
});
29-
};
30-
3115
var toDataUri = function(jsCode){
3216
return "data:text/javascript;base64," + btoa(jsCode);
3317
},
@@ -45,8 +29,7 @@ define([], function(){
4529
};
4630

4731
return {
48-
loadJsAnimOnCanvasAnim: loadJsAnimOnCanvasAnim,
49-
evalAnimation: evalAnimation,
32+
loadJsAnim: loadJsAnim ,
5033
toDataUri: toDataUri,
5134
dataUri2text: dataUri2text,
5235
stringifyJSON: stringifyJSON

public/exquis.js

+27-16
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,38 @@ define(["iter2d", "csshelper", "evileval", "net", "ui"], function(iter2d, csshel
5252
this.currentCode.draw(context, borders, this.lib);
5353
},
5454

55+
toDataUri : function(jsCode){
56+
return "data:text/javascript;base64," + btoa(jsCode);
57+
},
58+
5559
addCodeStringToEvaluate: function(codeString){
56-
var canvasAnim = this;
5760
return new Promise(function(resolve, reject){
58-
canvasAnim.evaluateCode = function(){
59-
evileval.evalAnimation(codeString, canvasAnim)
60-
.then(function(){
61+
this.evaluateCode = function(){
62+
var codeAsUri = this.toDataUri(codeString);
63+
evileval.loadJsAnim(codeAsUri)
64+
.then(function(evaluatedAnimationClone){
65+
this.setAnimation(evaluatedAnimationClone, this.originalUrl);
66+
this.codeCacheUri = codeAsUri;
6167
resolve();
62-
}, function(err){
68+
}.bind(this))
69+
.catch( function(err){
6370
console.log(err);
6471
reject(err);
6572
});
6673
};
67-
});
74+
}.bind(this));
75+
},
76+
loadAnim: function(url){
77+
return evileval.loadJsAnim(url).then(function(evaluatedAnimationClone){
78+
this.setAnimation(evaluatedAnimationClone, url);
79+
this.codeCacheUri = null;
80+
return this;
81+
}.bind(this));
6882
},
69-
7083
setAnimation: function(codeToSetup, uri){
7184
this.codeToSetup = codeToSetup;
7285
this.animationName = net.extractAnimationNameFromUri(uri),
73-
this.uri = uri;
86+
this.originalUrl = uri;
7487
this.setup = function(){
7588
// force reset matrix
7689
context.setTransform(1, 0, 0, 1, 0, 0);
@@ -83,16 +96,16 @@ define(["iter2d", "csshelper", "evileval", "net", "ui"], function(iter2d, csshel
8396

8497
getSourceCodeString: function(){
8598
//TODO continue refactoring here, see doc.org
86-
if (this.uri.match(/^data:/)){
99+
if (this.codeCacheUri){
87100
// the code is in the cache
88101
return new Promise(function(resolve, reject){
89-
var animCodeString = evileval.dataUri2text(this.uri);
102+
var animCodeString = evileval.dataUri2text(this.codeCacheUri);
90103
resolve(animCodeString);
91104
}.bind(this));
92105
}else{
93106
// get the code from the internets
94-
return net.HTTPget(this.uri).then(function(animCodeString){
95-
this.animationName = net.extractAnimationNameFromUri(this.uri);
107+
return net.HTTPget(this.originalUrl).then(function(animCodeString){
108+
this.animationName = net.extractAnimationNameFromUri(this.originalUrl);
96109
this.addCodeStringToEvaluate(animCodeString);
97110
return animCodeString;
98111
}.bind(this));
@@ -161,7 +174,7 @@ define(["iter2d", "csshelper", "evileval", "net", "ui"], function(iter2d, csshel
161174
}).then(function(animationName){
162175
if (animationName){
163176
var fileUri = store.animationNameToUri(animationName);
164-
return evileval.loadJsAnimOnCanvasAnim(fileUri, canvasAnim, animationName);
177+
return canvasAnim.loadAnim(fileUri);
165178
}else{
166179
throw "no animation name";
167180
}
@@ -236,9 +249,7 @@ define(["iter2d", "csshelper", "evileval", "net", "ui"], function(iter2d, csshel
236249
cell = makeCell(row, col, height, width);
237250
cell.canvasAnim = makeCanvasAnimation(cell.context);
238251
addCellUiListeners(cell.ui, cell.canvasAnim, store);
239-
// TODO the next two lines should be a function on canvasAnim
240-
var animationName = store.uriToAnimationName(animUri);
241-
evileval.loadJsAnimOnCanvasAnim(animUri, cell.canvasAnim, animationName);
252+
cell.canvasAnim.loadAnim(animUri);
242253
return cell;
243254
});
244255

public/net.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
define(["iter2d", "evileval"], function(iter2d, evileval){
44

55
var saveAnimation = function(canvasAnim, callback, fileName){
6-
if (!canvasAnim.uri.match(/^data:/)){
6+
if (!canvasAnim.codeCacheUri){
77
return;
88
}
9-
var JSString = evileval.dataUri2text(canvasAnim.uri),
9+
var JSString = evileval.dataUri2text(canvasAnim.codeCacheUri),
1010
dirName = "animations",
1111
name = (fileName || canvasAnim.animationName) + ".js";
1212

todo.org

-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ par exemple en mettant les boutons de l'animation à coté de son nom, ou les me
1313
** remplacer setInterval par requestAnimation
1414
* new animations
1515
* bugs
16-
** TODO animation name is displayed incorectly the second time you click on edit cell button
17-
net.extractAnimationNameFromUri cannot deduce the name from a data uri
18-
this problem will be solved when we introduce the distinction between canvasAnim.originalUrl and .codeUri
1916

2017
* implement blockly interface https://code.google.com/p/blockly/
2118
* allow usage of files from external urls (github.com, pastebin, etc)

0 commit comments

Comments
 (0)