|
| 1 | +import "./main"; |
| 2 | +import $ from "jquery"; |
| 3 | +import "./compat/json"; |
| 4 | +import "bootstrap/js/dist/tab"; |
| 5 | +import CTFd from "./compat/CTFd";; |
| 6 | +import { ezQuery, ezAlert, ezToast } from "./compat/ezq"; |
| 7 | +import { default as helpers } from "./compat/helpers"; |
| 8 | +import { bindMarkdownEditors } from "./styles"; |
| 9 | +import Vue from "vue"; |
| 10 | +import CommentBox from "./components/comments/CommentBox.vue"; |
| 11 | + |
| 12 | + |
| 13 | +function loadChalTemplate(challenge) { |
| 14 | + CTFd._internal.challenge = {}; |
| 15 | + $.getScript(CTFd.config.urlRoot + challenge.scripts.view, function () { |
| 16 | + let template_data = challenge.create; |
| 17 | + $("#create-chal-entry-div").html(template_data); |
| 18 | + bindMarkdownEditors(); |
| 19 | + |
| 20 | + $.getScript(CTFd.config.urlRoot + challenge.scripts.create, function () { |
| 21 | + $("#create-chal-entry-div form").submit(function (event) { |
| 22 | + event.preventDefault(); |
| 23 | + const params = $("#create-chal-entry-div form").serializeJSON(); |
| 24 | + CTFd.fetch("/userchallenge/api/challenges/", { |
| 25 | + method: "POST", |
| 26 | + credentials: "same-origin", |
| 27 | + headers: { |
| 28 | + Accept: "application/json", |
| 29 | + "Content-Type": "application/json", |
| 30 | + }, |
| 31 | + body: JSON.stringify(params), |
| 32 | + }) |
| 33 | + .then(function (response) { |
| 34 | + return response.json(); |
| 35 | + }) |
| 36 | + .then(function (response) { |
| 37 | + if (response.success) { |
| 38 | + $("#challenge-create-options #challenge_id").val( |
| 39 | + response.data.id, |
| 40 | + ); |
| 41 | + $("#challenge-create-options").modal(); |
| 42 | + } else { |
| 43 | + let body = ""; |
| 44 | + for (const k in response.errors) { |
| 45 | + body += response.errors[k].join("\n"); |
| 46 | + body += "\n"; |
| 47 | + } |
| 48 | + |
| 49 | + ezAlert({ |
| 50 | + title: "Error", |
| 51 | + body: body, |
| 52 | + button: "OK", |
| 53 | + }); |
| 54 | + } |
| 55 | + }); |
| 56 | + }); |
| 57 | + }); |
| 58 | + }); |
| 59 | +} |
| 60 | + |
| 61 | +$(() => { |
| 62 | + $(".preview-challenge").click(function (_e) { |
| 63 | + let url = `/userchallenge/challenges/preview/${window.CHALLENGE_ID}`; |
| 64 | + $("#challenge-window").html( |
| 65 | + `<iframe src="${url}" height="100%" width="100%" frameBorder=0></iframe>`, |
| 66 | + ); |
| 67 | + $("#challenge-modal").modal(); |
| 68 | + }); |
| 69 | + |
| 70 | + $(".comments-challenge").click(function (_event) { |
| 71 | + $("#challenge-comments-window").modal(); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + // Because this JS is shared by a few pages, |
| 76 | + // we should only insert the CommentBox if it's actually in use |
| 77 | + if (document.querySelector("#comment-box")) { |
| 78 | + // Insert CommentBox element |
| 79 | + const commentBox = Vue.extend(CommentBox); |
| 80 | + let vueContainer = document.createElement("div"); |
| 81 | + document.querySelector("#comment-box").appendChild(vueContainer); |
| 82 | + new commentBox({ |
| 83 | + propsData: { type: "challenge", id: window.CHALLENGE_ID }, |
| 84 | + }).$mount(vueContainer); |
| 85 | + } |
| 86 | + |
| 87 | + $.get("/userchallenge/api/challenges/types", function (res) { |
| 88 | + const data = res.data; |
| 89 | + loadChalTemplate(data["standard"]); |
| 90 | + |
| 91 | + $("#create-chals-select input[name=type]").change(function () { |
| 92 | + let challenge = data[this.value]; |
| 93 | + loadChalTemplate(challenge); |
| 94 | + }); |
| 95 | + }); |
0 commit comments