| 1 | window.onload = function() { |
| 2 | |
| 3 | new Vue({ |
| 4 | el: '#courseList', |
| 5 | data: { |
| 6 | courseArray: courseArray, |
| 7 | newCourse: { |
| 8 | code: "", |
| 9 | description: "", |
| 10 | }, |
| 11 | }, |
| 12 | mounted: function() { |
| 13 | $('.modal').modal(); |
| 14 | }, |
| 15 | methods: { |
| 16 | redirect: function(code) { |
| 17 | document.location.href = "/" + initials + "/" + code; |
| 18 | }, |
| 19 | addCourse: function() { |
| 20 | if (!admin) |
| 21 | return; |
| 22 | // modal, fill code and description |
| 23 | let error = Validator.checkCode(this.newCourse.code); |
| 24 | if (!!error) |
| 25 | return alert(error); |
| 26 | else |
| 27 | $('#newCourse').modal('close'); |
| 28 | $.ajax("/add/course", |
| 29 | { |
| 30 | method: "GET", |
| 31 | data: this.newCourse, |
| 32 | dataType: "json", |
| 33 | success: res => { |
| 34 | if (!res.errmsg) |
| 35 | { |
| 36 | this.newCourse["code"] = ""; |
| 37 | this.newCourse["description"] = ""; |
| 38 | this.courseArray.push(res); |
| 39 | } |
| 40 | else |
| 41 | alert(res.errmsg); |
| 42 | }, |
| 43 | } |
| 44 | ); |
| 45 | }, |
| 46 | deleteCourse: function(course) { |
| 47 | if (!admin) |
| 48 | return; |
| 49 | if (confirm("Delete course '" + course.code + "' ?")) |
| 50 | $.ajax("/remove/course", |
| 51 | { |
| 52 | method: "GET", |
| 53 | data: { cid: course._id }, |
| 54 | dataType: "json", |
| 55 | success: res => { |
| 56 | if (!res.errmsg) |
| 57 | this.courseArray.splice( this.courseArray.findIndex( item => { |
| 58 | return item._id == course._id; |
| 59 | }), 1 ); |
| 60 | else |
| 61 | alert(res.errmsg); |
| 62 | }, |
| 63 | } |
| 64 | ); |
| 65 | }, |
| 66 | } |
| 67 | }); |
| 68 | |
| 69 | }; |