This repository has been archived on 2025-01-01. You can view files and clone it, but cannot push or open issues or pull requests.
moodle-local_treestudyplan/amd/src/modedit-modal.js

126 lines
4.4 KiB
JavaScript
Raw Normal View History

/*eslint no-var: "error"*/
/*eslint no-console: "off"*/
/*eslint-disable no-trailing-spaces */
/*eslint-env es6*/
// Put this file in path/to/plugin/amd/src
import {loadFragment} from 'core/fragment';
2023-08-19 17:54:40 +02:00
import {load_strings} from './util/string-helper';
import {call} from 'core/ajax';
import notification from 'core/notification';
import {replaceNodeContents} from 'core/templates';
//import {markFormSubmitted} from 'core_form/changechecker'; // Moodle 4.00+ only
//import {notifyFormSubmittedByJavascript} from 'core_form/events'; // Moodle 4.00+ only
/* Moodle 3.11 safe import for when needed
let markFormSubmitted = () => {};
let notifyFormSubmittedByJavascript = () => {};
import('core_form/changechecker').then((ns) => {
debug.info(ns);
if(ns.markFormSubmitted) {
markFormSubmitted = ns.markFormSubmitted;
}
if(ns.notifyFormSubmittedByJavascript) {
notifyFormSubmittedByJavascript = ns.notifyFormSubmittedByJavascript;
}
}).catch(()=>{});
*/
export default {
install(Vue/*,options*/){
let strings = load_strings({
editmod: {
2023-06-16 23:11:43 +02:00
save$core: "save$core",
cancel$core: "cancel$core",
}
});
Vue.component('s-edit-mod', {
props: {
cmid: {
type: Number,
},
coursectxid:{
type: Number,
},
title: {
type: String,
default: "",
},
genericonly: {
type: Boolean,
default: false,
}
},
data() {
return {
content: "",
text: strings.editmod,
};
},
computed: {
},
methods: {
openForm(){
const self = this;
self.$refs["editormodal"].show();
},
onShown(){
const self = this;
let params = {cmid: this.cmid};
console.info("Loading form");
loadFragment('local_treestudyplan', 'mod_edit_form', this.coursectxid, params).then((html,js) =>{
replaceNodeContents(self.$refs["content"], html, js);
}).catch(notification.exception);
2023-09-08 12:47:29 +02:00
},
onSave(){
const self = this;
let form = this.$refs["content"].getElementsByTagName("form")[0];
2023-09-08 12:47:29 +02:00
// markFormSubmitted(form); // Moodle 4.00+ only
// We call this, so other modules can update the form with the latest state.
form.dispatchEvent(new Event("save-form-state"));
// Tell all form fields we are about to submit the form.
// notifyFormSubmittedByJavascript(form); // Moodle 4.00+ only
const formdata = new FormData(form);
2023-12-13 23:49:06 +01:00
const data = new URLSearchParams(formdata).toString();
//const formdata = new FormData(form);
//const data = {};
//formdata.forEach((value, key) => (data[key] = value));
call([{
methodname: 'local_treestudyplan_submit_cm_editform',
args: {cmid: this.cmid, formdata: data}
2023-12-13 23:49:06 +01:00
}])[0].then(()=>{
self.$emit("saved",formdata);
2023-12-13 23:49:06 +01:00
}).catch(notification.exception);
}
},
template: `
<span class='s-edit-mod'><a href='#' @click.prevent="openForm"><slot><i class="fa fa-cog"></i></slot></a>
2023-09-08 12:47:29 +02:00
<b-modal
ref="editormodal"
scrollable
centered
size="xl"
id="'modal-cm-'+cmid"
@shown="onShown"
@ok="onSave"
:title="title"
:ok-title="text.save$core"
><div :class="'s-edit-mod-form '+ (genericonly?'genericonly':'')" ref="content"
><div class="d-flex justify-content-center mb-3"><b-spinner variant="primary"></b-spinner></div
></div
></b-modal>
</span>
`,
});
}
};