Skip to content

Commit bb4b7a8

Browse files
committed
fix #29 #26 #10 wip #35
1 parent 0eac20a commit bb4b7a8

File tree

12 files changed

+289
-182
lines changed

12 files changed

+289
-182
lines changed

package-lock.json

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
},
1616
"dependencies": {
1717
"@fortawesome/fontawesome-free": "^5.15.4",
18+
"vuelidate": "^0.7.7",
1819
"axios": "^0.25.0",
1920
"base-64": "^1.0.0",
2021
"blockly": "^7.20211209.2",

src/components/Activity.vue

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,24 @@
234234
</v-card-actions>
235235
</v-card>
236236
</v-dialog>
237+
<!-- Confirm exit dialog -->
238+
<v-dialog v-model="confirm_exit_dialog" max-width="290">
239+
<v-card>
240+
<v-card-title class="headline">{{ $t("message.confirm") }}</v-card-title>
241+
<v-card-text>
242+
{{ $t("message.activity_confirm_exit_text") }}
243+
</v-card-text>
244+
<v-card-actions>
245+
<v-spacer></v-spacer>
246+
<v-btn color="green darken-1" text="text" @click="confirm_exit_dialog=false">
247+
{{ $t("message.cancel") }}
248+
</v-btn>
249+
<v-btn color="green darken-1" text="text" @click="confirm_exit_dialog=false; router_next(true)">
250+
{{ $t("message.ok") }}
251+
</v-btn>
252+
</v-card-actions>
253+
</v-card>
254+
</v-dialog>
237255
<!-- Notification Snackbar -->
238256
<v-snackbar v-model="snackbar">
239257
{{ snackText }}
@@ -293,9 +311,10 @@ Object.keys(music_package.packages).forEach((key) => {
293311
Blockly.Blocks.CoderBotSettings.animalist[Blockly.Blocks.CoderBotSettings.animalist.length] = names;
294312
}
295313
});
314+
// let workspace = null;
296315
297316
export default {
298-
name: 'Blockly',
317+
name: 'Activity',
299318
components: {
300319
sidebar,
301320
Prism: () => import('vue-prism-component'),
@@ -342,6 +361,9 @@ export default {
342361
defaultProgramName: '',
343362
overwrite: 0,
344363
overwriteDialog: false,
364+
dirty: false,
365+
confirm_exit_dialog: null,
366+
router_next: null,
345367
}),
346368
computed: {
347369
statusText() {
@@ -448,6 +470,14 @@ export default {
448470
this.initBlockly(settings);
449471
});
450472
},
473+
beforeRouteLeave(to, from, next) {
474+
if (this.workspace.dirty) {
475+
this.router_next = next;
476+
this.confirm_exit_dialog = true;
477+
} else {
478+
next();
479+
}
480+
},
451481
methods: {
452482
updateCssProps() {
453483
// (Re)Compute the CSS variables from the activity definition, then update them
@@ -519,6 +549,13 @@ export default {
519549
},
520550
},
521551
);
552+
const {
553+
workspace
554+
} = this;
555+
workspace.dirty = false;
556+
workspace.addChangeListener(() => {
557+
workspace.dirty = true;
558+
});
522559
523560
// Pass the reference to the method to call, don't execute it (`()`)
524561
// window.addEventListener('resize', this.resizeWorkspace, false);
@@ -527,7 +564,6 @@ export default {
527564
// this.resizeWorkspace()
528565
// Blockly.svgResize(this.workspace);
529566
},
530-
531567
blocksExtensions() {
532568
const {
533569
settings
@@ -688,6 +724,7 @@ export default {
688724
this.$data.isDefault = '';
689725
this.$data.overwrite = 0;
690726
console.log('salvato');
727+
this.workspace.dirty = false;
691728
}
692729
});
693730
} else {
@@ -774,7 +811,7 @@ export default {
774811
this.info = response.data;
775812
})
776813
.catch((error) => {
777-
console.log(error);
814+
console.log(`pollStatus error: ${error}`);
778815
// If the disconnection happened while in this component, send a notification
779816
if (this.status) {
780817
this.snackText = this.$i18n.t('coderbot_offline_2');
@@ -862,8 +899,7 @@ export default {
862899
dom_code,
863900
code,
864901
options,
865-
}).then((response) => {
866-
console.log(response);
902+
}).then(() => {
867903
this.runtimeDialog = true;
868904
setTimeout(() => {
869905
this.updateExecStatus();
@@ -875,42 +911,11 @@ export default {
875911
this.generalDialogText = this.$i18n.t('coderbot_offline_3');
876912
}
877913
},
878-
/*
879-
runProgramLegacy() {
880-
if (this.status) {
881-
const axios = this.$axios;
882-
const qs = this.$qs;
883-
this.log = '';
884-
// POST /program/save
885-
const xml_code = Blockly.Xml.workspaceToDom(this.workspace);
886-
const dom_code = Blockly.Xml.domToText(xml_code);
887-
window.LoopTrap = 1000;
888-
Blockly.Python.INFINITE_LOOP_TRAP = 'get_prog_eng().check_end()\n';
889-
const code = Blockly.Python.workspaceToCode(this.workspace);
890-
Blockly.Python.INFINITE_LOOP_TRAP = null;
891-
892-
const valuesAsString = qs.stringify({
893-
name: 'Hello, World!',
894-
dom_code,
895-
code,
896-
});
897-
898-
axios.post(`${this.CBv1}/program/exec`, valuesAsString)
899-
.then(() => {
900-
this.runtimeDialog = true;
901-
setTimeout(() => {
902-
this.updateExecStatus();
903-
}, 1000);
904-
});
905-
}
906-
},
907-
*/
908914
stopProgram() {
909915
console.log('Stopping');
910916
const axios = this.$axios;
911917
axios.post(`${this.CBv1}/program/end`);
912918
},
913-
914919
updateExecStatus() {
915920
const axios = this.$axios;
916921
console.log('Updating Execution status');

src/components/ActivityEditor.vue

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<v-app-bar color="indigo" dark fixed app>
66
<v-app-bar-nav-icon @click.stop="toggleSidebar()"></v-app-bar-nav-icon>
77
<v-app-bar-title v-if="!saved">{{ $t("message.activity_new") }} {{prefix}} {{activity.name}}</v-app-bar-title>
8-
<v-app-bar-title v-else>{{ $t("message.activity_modify") }} {{prefix}} {{activity.name}}</v-app-bar-title>
8+
<v-app-bar-title v-else>{{ $t("message.activity_edit") }} {{prefix}} {{activity.name}}</v-app-bar-title>
99
<v-spacer></v-spacer>
1010
<v-btn text @click="save()">
1111
<v-icon>save</v-icon>
@@ -274,10 +274,28 @@
274274
</v-tab-item>
275275
</v-tabs-items>
276276
</v-main>
277+
<!-- Confirm exit dialog -->
278+
<v-dialog v-model="confirm_exit_dialog" max-width="290">
279+
<v-card>
280+
<v-card-title class="headline">{{ $t("message.confirm") }}</v-card-title>
281+
<v-card-text>
282+
{{ $t("message.activity_confirm_exit_text") }}
283+
</v-card-text>
284+
<v-card-actions>
285+
<v-spacer></v-spacer>
286+
<v-btn color="green darken-1" text="text" @click="confirm_exit_dialog=false">
287+
{{ $t("message.cancel") }}
288+
</v-btn>
289+
<v-btn color="green darken-1" text="text" @click="confirm_exit_dialog=false; router_next(true)">
290+
{{ $t("message.ok") }}
291+
</v-btn>
292+
</v-card-actions>
293+
</v-card>
294+
</v-dialog>
295+
<v-snackbar v-model="snackbar">
296+
{{ snackbarText }}
297+
</v-snackbar>
277298
</v-app>
278-
<v-snackbar v-model="snackbar">
279-
{{ snackbarText }}
280-
</v-snackbar>
281299
</div>
282300
</template>
283301
<script>
@@ -491,6 +509,10 @@ export default {
491509
disabled: true
492510
},
493511
],
512+
confirm_exit_dialog: null,
513+
route_next: null,
514+
dirty: false,
515+
unwatch: null
494516
};
495517
},
496518
mounted() {
@@ -511,6 +533,12 @@ export default {
511533
} else {
512534
this.restoreDefaults();
513535
}
536+
this.unwatch = this.$watch('activity', () => {
537+
this.dirty = true;
538+
}, { deep: true });
539+
},
540+
unmounted() {
541+
this.unwatch();
514542
},
515543
methods: {
516544
save() {

src/components/ActivityList.vue

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,13 @@ export default {
9191
components: {
9292
sidebar
9393
},
94-
name: 'CoderBot',
94+
name: 'ActivityList',
9595
mounted() {
9696
this.getActivities();
9797
},
9898
methods: {
99-
goToActivity: (name) => {
100-
window.location = `#/activity/open/${name}`;
99+
goToActivity(name) {
100+
this.$router.push({ name: 'activity_open', params: { name } });
101101
},
102102
getActivities() {
103103
const axios = this.$axios;
@@ -108,7 +108,6 @@ export default {
108108
axios.get(`${CB}/listActivities`)
109109
.then((response) => {
110110
this.$data.activityList = response.data;
111-
console.log(this.$data.activityList);
112111
});
113112
},
114113
deleteActivity(name) {

src/components/Control.vue

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,12 @@ export default {
249249
this.snackbar = true;
250250
}
251251
252-
// console.log(response)
253252
this.statusData = response.data;
254253
this.status = response.status;
255254
})
256255
.catch((error) => {
257256
// handle error
258-
console.log(error);
257+
console.log(`pollStatus error: ${error}`);
259258
260259
if (this.status) {
261260
this.snackText = this.$i18n.t('message.coderbot_offline_2');
@@ -279,40 +278,32 @@ export default {
279278
axios.post(`${CB}/move`, {
280279
speed: 100,
281280
elapse: -1,
282-
}).then((response) => {
283-
console.log(response);
284281
}).catch((error) => {
285-
console.log(error);
282+
console.log(`move error: ${error}`);
286283
});
287284
} else if (direction == 1) {
288285
// RIGHT, turn right
289286
axios.post(`${CB}/turn`, {
290287
speed: -80,
291288
elapse: -1,
292-
}).then((response) => {
293-
console.log(response);
294289
}).catch((error) => {
295-
console.log(error);
290+
console.log(`turn error: ${error}`);
296291
});
297292
} else if (direction == 2) {
298293
// LEFT, turn left
299294
axios.post(`${CB}/turn`, {
300295
speed: 80,
301296
elapse: -1,
302-
}).then((response) => {
303-
console.log(response);
304297
}).catch((error) => {
305-
console.log(error);
298+
console.log(`turn error: ${error}`);
306299
});
307300
} else if (direction == 3) {
308301
// DOWN, move backwards
309302
axios.post(`${CB}/move`, {
310303
speed: -100,
311304
elapse: -1,
312-
}).then((response) => {
313-
console.log(response);
314305
}).catch((error) => {
315-
console.log(error);
306+
console.log(`move error: ${error}`);
316307
});
317308
}
318309
},
@@ -332,17 +323,13 @@ export default {
332323
if (pressDuration < 500) {
333324
console.log('Too fast, postponing it by', delay, 'ms..');
334325
setTimeout(() => {
335-
axios.post(`${CB}/stop`).then((response) => {
336-
console.log(response);
337-
}).catch((error) => {
338-
console.log(error);
326+
axios.post(`${CB}/stop`).catch((error) => {
327+
console.log(`stopping error: ${error}`);
339328
});
340329
}, delay);
341330
} else {
342-
axios.post(`${CB}/stop`).then((response) => {
343-
console.log(response);
344-
}).catch((error) => {
345-
console.log(error);
331+
axios.post(`${CB}/stop`).catch((error) => {
332+
console.log(`stop error: ${error}`);
346333
});
347334
}
348335
},

0 commit comments

Comments
 (0)