-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
229 lines (202 loc) · 5.83 KB
/
index.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
"use strict";
let workspace = null;
let projectId = null;
let templateObject = null;
const DEFAULT_TEMPLATE = "-Lghd7w_KIKDqZs64b6L";
const loadXml = url => {
return fetch(url)
.then(response => response.text())
.then(data => data)
.catch(error => console.error(error));
};
const makeWorkspace = toolbox => {
const blocklyArea = document.getElementById("blocklyArea");
workspace = Blockly.inject(blocklyArea, makeOption(toolbox));
Blockly.svgResize(workspace);
const saveWorkspace = () => {
var code = Generator.workspaceToCode(workspace);
if (code === "") {
return;
}
firebase
.database()
.ref(`projects/${projectId}`)
.update({
"kanna-code": code,
datetime: firebase.database.ServerValue.TIMESTAMP
});
};
workspace.addChangeListener(saveWorkspace);
};
const makeOption = toolbox => {
return {
toolbox: toolbox,
collapse: true,
maxBlocks: Infinity,
trashcan: true,
tooltips: true,
css: true,
media: "https://blockly-demo.appspot.com/static/media/",
rtl: false,
scrollbars: true,
sounds: true,
oneBasedIndex: true
};
};
// make block functions
const startBlock = connectedBlock => {
return `<xml xmlns="http://www.w3.org/1999/xhtml"><block type="start"><next>${connectedBlock}</next></block></xml>`;
};
const conditionBlock = (conditionText, yesBlock, noBlock) => {
return `<block type="condition"><field name="condition_name">${conditionText}</field><statement name="yes">${yesBlock}</statement><statement name="no">${noBlock}</statement></block>`;
};
const targetBlock = text => {
return `<block type="target"><field name="target_name">${text}</field></block>`;
};
const parse = (json, template) => {
if (json.isCondition) {
const yesBlock = json.hasOwnProperty("yes")
? parse(json.yes, template)
: {};
const noBlock = json.hasOwnProperty("no") ? parse(json.no, template) : {};
return conditionBlock(json.link, yesBlock, noBlock);
}
return targetBlock(json.link);
};
const initBlock = (block, template) => {
const xmlText = startBlock(parse(block, template));
const xml = Blockly.Xml.textToDom(xmlText);
workspace.clear();
Blockly.Xml.domToWorkspace(xml, workspace);
};
const getProject = () => {
if (projectId == null) {
throw new Error("invalid project id");
return;
}
const db = firebase.database();
const projectDatabase = db.ref(`/projects/${projectId}`);
projectDatabase.once("value", async snapshot => {
const yattoko = snapshot.val()["kanna-code"];
let template = snapshot.val()["template"];
if (template == null) {
template = DEFAULT_TEMPLATE;
projectDatabase.update({
template: template
});
}
templateObject = await getTemplate(template);
initSelectFromTemplate(templateObject);
if (yattoko != null) {
initBlock(JSON.parse(yattoko), templateObject, projectId);
}
});
};
const getTemplate = async templateId => {
const db = firebase.database();
const templateDatabase = db.ref(`/store/${templateId}`);
return new Promise((resolve, reject) => {
templateDatabase.once("value", snapshot => {
try {
resolve(snapshot.val());
} catch (error) {
reject(error);
}
});
});
};
const validate = () => {
const code = JSON.parse(Generator.workspaceToCode(workspace));
const isSuccess =
validateUseAllBlocks(templateObject, code) &&
satisfyConditions(templateObject.targets, code, [], []);
validateAnimation(isSuccess);
};
const validateUseAllBlocks = (template, code) => {
const targets = template["targets"];
for (const t of targets) {
if (!searchTarget(code, t["id"])) {
// not using all result blocks
return false;
}
}
return true;
};
const searchTarget = (code, targetId) => {
if (code == null) {
return false;
}
if (code.isCondition) {
return (
searchTarget(code["yes"], targetId) || searchTarget(code["no"], targetId)
);
}
// result block
return code.link === targetId;
};
const satisfyConditions = (targets, code, yes, no) => {
if (code == null) {
return false;
}
if (code.isCondition) {
return (
satisfyConditions(targets, code["yes"], [...yes, code.link], no) &&
satisfyConditions(targets, code["no"], yes, [...no, code.link])
);
}
// validate
const targetCondition = targets.find(t => t.id == code.link)["condition"];
if (targetCondition == null) {
throw new Error("cannot find target");
}
for (const y of yes) {
if (!targetCondition[y]) {
return false;
}
}
for (const n of no) {
if (targetCondition[n]) {
return false;
}
}
return true;
};
// from yattoko
const validateAnimation = result => {
$("#startButton").animate(
{ top: -$("#startButton").height(), opacity: 0 },
{ duration: "normal", easing: "swing" }
);
$("#validateButtonRope").animate(
{ top: -$("#startButton").height(), opacity: 0 },
"normal",
"swing",
function() {
if (result) {
$("#validateResult").attr("src", "../yattoko/asset/succeeded.png");
} else {
$("#validateResult").attr("src", "../yattoko/asset/failed.png");
}
$("#validateResult").css({ top: -$("#validateResult").height() });
$("#validateResult")
.fadeIn("normal")
.animate({ top: 70 }, { duration: "normal", easing: "swing" });
$("#validateButtonRope").animate(
{ top: 0, opacity: 100 },
{ duration: "normal", easing: "swing" }
);
}
);
};
const unValidated = () => {
$("#startButton").css({ top: 70, opacity: 1 });
$("#validateResult").hide();
};
(async () => {
const requestUrl = ["/kanna/toolbox.xml"];
const result = await Promise.all(requestUrl.map(loadXml));
projectId = window.location.search.replace(/\?id=/, "");
const htmlToolbox = result[0];
makeWorkspace(htmlToolbox);
getProject();
})();