-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
206 lines (188 loc) · 6.29 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
// Add block names/categories here to add them to the toolbox
var BLOCKS = {
categories: [{
name: 'Basic',
blocks: ['controls_whileUntil', 'text', 'text_print', 'controls_if', 'logic_boolean', 'logic_compare', 'variables_get', 'variables_set'],
color: '210'
},
{
name: 'Requests',
blocks: [],
color: '80'
},
{
name: 'Chatbot',
blocks: ['response', 'input', 'question_block', 'userresponsevarable', 'contains', 'if_enter', 'get_user_response', 'user_info_save'],
color: '20'
},
{
name: 'Function',
blocks: [],
color: '50'
}
]
};
function generateBlocks() {
var toolbox = '<xml>';
for (var category in BLOCKS.categories) {
if (BLOCKS.categories[category].blocks.length > 0) {
toolbox += `<category name="${BLOCKS.categories[category].name}" colour="${BLOCKS.categories[category].color}">`;
/* Swap the previous line with this code to start with the first category expanded
if(category==0){
toolbox += `<category name="${BLOCKS.categories[category].name}" colour="${BLOCKS.categories[category].color}">`
}else{
toolbox += `<category name="${BLOCKS.categories[category].name}" colour="${BLOCKS.categories[category].color}">`
}
*/
for (var block in BLOCKS.categories[category].blocks) {
toolbox += ` <block type="${BLOCKS.categories[category].blocks[block]}"></block>`;
}
toolbox += '</category>';
}
}
toolbox += '</xml>';
return toolbox;
}
// Download code from https://stackoverflow.com/questions/2897619/using-html5-javascript-to-generate-and-save-a-file
function download(filename, text) {
var pom = document.createElement('a');
pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
pom.setAttribute('download', filename);
if (document.createEvent) {
var event = document.createEvent('MouseEvents');
event.initEvent('click', true, true);
pom.dispatchEvent(event);
} else {
pom.click();
}
}
window.onload = function() {
var blocklyArea = document.getElementById('blocklyArea');
var blocklyDiv = document.getElementById('blocklyDiv');
var workspace = Blockly.inject(blocklyDiv, {
toolbox: generateBlocks()
});
var onresize = function(e) { // jshint ignore:line
// Compute the absolute coordinates and dimensions of blocklyArea.
var element = blocklyArea;
var x = 0;
var y = 0;
do {
x += element.offsetLeft;
y += element.offsetTop;
element = element.offsetParent;
} while (element);
// Position blocklyDiv over blocklyArea.
blocklyDiv.style.left = x + 'px';
blocklyDiv.style.top = y + 'px';
blocklyDiv.style.width = blocklyArea.offsetWidth + 'px';
blocklyDiv.style.height = blocklyArea.offsetHeight + 'px';
};
window.addEventListener('resize', onresize, false);
onresize();
Blockly.svgResize(workspace);
Chatbot.loadLocalWorkspace();
function generateCode(event) { // jshint ignore:line
var code = Blockly.JavaScript.workspaceToCode(workspace);
if (code === '') {
$('#codeDisplay code').text('Add More Blocks to Generate Code');
} else {
$('#codeDisplay code').text(code);
}
Chatbot.saveLocalWorkspace();
}
workspace.addChangeListener(generateCode);
function runCode() {
Blockly.JavaScript.INFINITE_LOOP_TRAP = 'if (--window.LoopTrap == 0)throw "INfinite loop.";\n';
var code = Blockly.JavaScript.INFINITE_LOOP_TRAP = Blockly.JavaScript.workspaceToCode(workspace);
try {
eval(code);
//jshint ignore: line
} catch (e) {
alert(e); //jshint ignore: line
}
}
var runCodeButton = $('#runCodeButton');
runCodeButton.click(runCode);
function exportCode() {
var code = Blockly.JavaScript.workspaceToCode(workspace);
// alert(code)
download('blocklyCode_javascript.js', code);
}
function exportBlocks() {
download('blocklyCode_blocks.txt', Chatbot.getBlocks());
}
function importBlocks(e) {
var file = e.target.files[0];
if (!file) {
return;
}
var reader = new FileReader();
reader.onload = function(e) {
var contents = e.target.result;
// Display file content
Blockly.mainWorkspace.clear();
Blockly.Xml.domToWorkspace(Blockly.Xml.textToDom(contents), Blockly.mainWorkspace);
};
reader.readAsText(file);
}
$('#exportJS').click(exportCode);
$('#exportBlocks').click(exportBlocks);
$('#fileButton').change(importBlocks);
$('#importBlocks').click(function() {
$('#fileButton').click();
});
function clearBlocks() {
var count = workspace.getAllBlocks().length;
if (count < 2 || window.confirm(Blockly.Msg.DELETE_ALL_BLOCKS.replace('%1', count))) {
workspace.clear();
$('#codeDisplay code').text('Add More Blocks to Generate Code');
}
}
$('#clearButton').click(clearBlocks);
var codeButton = $('#codePreviewButton');
var chatbotButton = $('#chatbotPreviewButton');
var chatbotPreview = $('#chatbotDisplay');
var codePreview = $('#codeDisplay');
//Still generates code in the code preview even while hidden
function previewCode() {
if (chatbotButton.hasClass('active')) {
chatbotButton.toggleClass('active', false);
codeButton.toggleClass('active', true);
chatbotPreview.hide();
codePreview.show();
}
}
function previewChatbot() {
if (codeButton.hasClass('active')) {
chatbotButton.toggleClass('active', true);
codeButton.toggleClass('active', false);
codePreview.hide();
chatbotPreview.show();
}
}
codeButton.click(previewCode);
chatbotButton.click(previewChatbot);
$("#copyScripts").click(function() {
var $temp = $("<input>");
$("body").append($temp);
$temp.val($("#scriptsToCopy").text()).select();
document.execCommand("copy");
$temp.remove();
});
$("#copyChatbot").click(function() {
var $temp = $("<input>");
$("body").append($temp);
$temp.val($("#widgetToPaste").text()).select();
document.execCommand("copy");
$temp.remove();
});
$("#showWidgetButton").click(function() {
if (Chatbot.getUserId) {
$("#widgetModal").modal();
$("#widgetToPaste").append(`<div id="BlocklyChatbot" data="${Chatbot.getUserId()}"></div>`);
} else {
alert("You have to sign in to export your chatbot");
}
});
};