Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-implement util functions using URL API #2954

Merged
merged 15 commits into from
Aug 10, 2021
10 changes: 7 additions & 3 deletions app/assets/javascripts/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,15 @@ function initExerciseDescription() {
centerImagesAndTables();
}

function initExerciseShow(exerciseId, programmingLanguage, loggedIn, editorShown, courseId, _deadline) {
function initExerciseShow(exerciseId, programmingLanguage, loggedIn, editorShown, courseId, _deadline, _baseSubmissionUrl) {
let editor;
let lastSubmission;
let lastTimeout;

// remove the submission id to get the base submission url
const to = (_baseSubmissionUrl.substr(0, _baseSubmissionUrl.length - 1)).lastIndexOf("/");
TimonDB marked this conversation as resolved.
Show resolved Hide resolved
const baseSubmissionUrl = _baseSubmissionUrl.substring(0, to + 1);

function init() {
if (editorShown) {
initEditor();
Expand Down Expand Up @@ -270,7 +274,7 @@ function initExerciseShow(exerciseId, programmingLanguage, loggedIn, editorShown
return;
}
event.preventDefault();
loadFeedback($(this).attr("href"), $(this).data("submission_id"));
loadFeedback(baseSubmissionUrl + $(this).data("submission_id"), $(this).data("submission_id"));
TimonDB marked this conversation as resolved.
Show resolved Hide resolved
});
}

Expand All @@ -296,7 +300,7 @@ function initExerciseShow(exerciseId, programmingLanguage, loggedIn, editorShown
$submissionRow.find(".load-submission").get(0).click();
} else if ($("#activity-feedback-link").hasClass("active") &&
$("#activity-feedback-link").data("submission_id") === lastSubmission) {
loadFeedback(`/submissions/${lastSubmission}`, lastSubmission);
loadFeedback(baseSubmissionUrl + lastSubmission, lastSubmission);
TimonDB marked this conversation as resolved.
Show resolved Hide resolved
}
showFABStatus(status);
setTimeout(enableSubmitButton, 100);
Expand Down
112 changes: 15 additions & 97 deletions app/assets/javascripts/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,111 +14,29 @@ const delay = (function () {
};
})();

function updateURLParameter(url, param, paramVal) {
let TheAnchor = null;
let newAdditionalURL = "";
let tempArray = url.split("?");
let baseURL = tempArray[0];
let additionalURL = tempArray[1];
let temp = "";
let i;

if (additionalURL) {
const tmpAnchor = additionalURL.split("#");
const TheParams = tmpAnchor[0];
TheAnchor = tmpAnchor[1];
if (TheAnchor) {
additionalURL = TheParams;
}
tempArray = additionalURL.split("&");
for (i = 0; i < tempArray.length; i++) {
if (tempArray[i].split("=")[0] != param) {
newAdditionalURL += temp + tempArray[i];
temp = "&";
}
}
} else {
const tmpAnchor = baseURL.split("#");
const TheParams = tmpAnchor[0];
TheAnchor = tmpAnchor[1];

if (TheParams) {
baseURL = TheParams;
}
}
let rowsTxt = "";
if (paramVal) {
rowsTxt += `${temp}${param}=${paramVal}`;
}
if (TheAnchor) {
rowsTxt += "#" + TheAnchor;
}
return baseURL + "?" + newAdditionalURL + rowsTxt;
function updateURLParameter(_url, param, paramVal) {
const url = new URL(_url);
url.searchParams.set(param, paramVal);
return url;
}

function updateArrayURLParameter(url, param, _paramVals) {
function updateArrayURLParameter(_url, param, _paramVals) {
const paramVals = [...new Set(_paramVals)]; // remove duplicate items
let TheAnchor = null;
let newAdditionalURL = "";
let tempArray = url.split("?");
let baseURL = tempArray[0];
let additionalURL = tempArray[1];
let temp = "";

if (additionalURL) {
const tmpAnchor = additionalURL.split("#");
const TheParams = tmpAnchor[0];
TheAnchor = tmpAnchor[1];
if (TheAnchor) {
additionalURL = TheParams;
}
tempArray = additionalURL.split("&");
for (let i = 0; i < tempArray.length; i++) {
if (tempArray[i].split("=")[0] !== `${param}%5B%5D`) {
newAdditionalURL += temp + tempArray[i];
temp = "&";
}
}
} else {
const tmpAnchor = baseURL.split("#");
const TheParams = tmpAnchor[0];
TheAnchor = tmpAnchor[1];

if (TheParams) {
baseURL = TheParams;
}
}
let rowsTxt = "";
for (const paramVal of paramVals) {
rowsTxt += `${temp}${param}%5B%5D=${paramVal}`;
temp = "&";
}
if (TheAnchor) {
rowsTxt += "#" + TheAnchor;
}
return baseURL + "?" + newAdditionalURL + rowsTxt;
const url = new URL(_url);
paramVals.forEach( paramVal => {
url.searchParams.set(param, paramVal);
});
return url;
}

function getURLParameter(_name, _url) {
const url = _url || window.location.href;
const name = _name.replace(/[[\]]/g, "\\$&");
const regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)");
const results = regex.exec(url);
if (!results) return null;
if (!results[2]) return "";
return decodeURIComponent(results[2].replace(/\+/g, " "));
function getURLParameter(name, _url) {
const url = new URL(_url ?? window.location.href);
return url.searchParams.get(name);
}

function getArrayURLParameter(name, _url) {
const url = _url || window.location.href;
const result = [];
for (const part of url.split(/[?&]/)) {
const regResults = new RegExp(`${name}%5B%5D=([^#]+)`).exec(part);
if (regResults && regResults[1]) {
result.push(decodeURIComponent(regResults[1]));
}
}
return result;
const url = new URL(_url ?? window.location.href);
return url.searchParams.getAll(name);
}

/*
Expand Down
3 changes: 2 additions & 1 deletion app/views/activities/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ end %>
<%= user_signed_in? %>,
<%= policy(@activity).submit? || !user_signed_in? %>,
<%= @course&.id || "null" %>,
<%= raw "\"#{@series&.deadline&.httpdate}\"" || "null" %>
<%= raw "\"#{@series&.deadline&.httpdate}\"" || "null" %>,
"<%= submission_url %>"
);
});
</script>