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

rails views: start removing jQuery #4851

Merged
merged 13 commits into from
Aug 8, 2023
21 changes: 21 additions & 0 deletions app/assets/javascripts/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,26 @@ function getParentByClassName(element, classNames) {
return null;
}

/**
* When setting innerHTML, code inside the <script> tag doesn't get executed.
* This function solves that problem.
*/
function setInnerHTML(el, html) {
freyavs marked this conversation as resolved.
Show resolved Hide resolved
el.innerHTML = html;
Array.from(el.querySelectorAll("script")).forEach( oldScriptEl => {
const newScriptEl = document.createElement("script");
Array.from(oldScriptEl.attributes).forEach( attr => {
newScriptEl.setAttribute(attr.name, attr.value);
});
const scriptText = document.createTextNode(oldScriptEl.innerHTML);
newScriptEl.appendChild(scriptText);
oldScriptEl.parentNode.replaceChild(newScriptEl, oldScriptEl);
});
}

// insert `cached` function here after move to typescript
// the function is currently in `app/assets/javascripts/mark.ts`
freyavs marked this conversation as resolved.
Show resolved Hide resolved

export {
createDelayer,
delay,
Expand All @@ -227,4 +247,5 @@ export {
initDatePicker,
ready,
getParentByClassName,
setInnerHTML,
};
3 changes: 2 additions & 1 deletion app/javascript/packs/application_pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ window.bootstrap = bootstrap;
import { Drawer } from "drawer";
import { Toast } from "toast";
import { Notification } from "notification";
import { checkTimeZone, checkIframe, initCSRF, initTooltips, ready } from "util.js";
import { checkTimeZone, checkIframe, initCSRF, initTooltips, ready, setInnerHTML } from "util.js";
import { initClipboard } from "copy";
import { FaviconManager } from "favicon";
import { themeState } from "state/Theme";
Expand Down Expand Up @@ -64,6 +64,7 @@ dodona.Toast = Toast;
dodona.Notification = Notification;
dodona.initTooltips = initTooltips;
dodona.checkIframe = checkIframe;
dodona.setInnerHTML = setInnerHTML;
dodona.setTheme = theme => themeState.selectedTheme = theme;
dodona.setUserId = userId => userState.id = userId;
dodona.ready = ready;
Expand Down
2 changes: 1 addition & 1 deletion app/views/activities/available.js.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
$("#activities-table-wrapper").html("<%= escape_javascript(render partial: 'series_activities_add_table', locals: {activities: @activities, series: @series}) %>");
dodona.setInnerHTML(document.querySelector("#activities-table-wrapper"), "<%= escape_javascript(render partial: 'series_activities_add_table', locals: {activities: @activities, series: @series}) %>");
dodona.seriesEditActivitiesLoaded();
2 changes: 1 addition & 1 deletion app/views/activities/index.js.erb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
$("#activities-table-wrapper").html("<%= escape_javascript(render partial: 'activities_table', locals: {activities: @activities}) %>");
dodona.setInnerHTML(document.querySelector("#activities-table-wrapper"),"<%= escape_javascript(render partial: 'activities_table', locals: {activities: @activities}) %>");
2 changes: 1 addition & 1 deletion app/views/activities/read.js.erb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
$("#content_page_read_action").html("<%= escape_javascript(render partial: 'activities/content_page_read_button', locals: {activity: activity, course: course, read_state: read_state, user: user}) %>");
dodona.setInnerHTML(document.querySelector("#content_page_read_action"), "<%= escape_javascript(render partial: 'activities/content_page_read_button', locals: {activity: activity, course: course, read_state: read_state, user: user}) %>");