Skip to content

Commit f2b9250

Browse files
Merge pull request #132 from opencomponents/remove-more-jquery
[v2] Remove more jquery
2 parents 9645d1b + c3a7e69 commit f2b9250

File tree

1 file changed

+55
-34
lines changed

1 file changed

+55
-34
lines changed

src/oc-client.js

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,17 @@ export function createOc(oc) {
106106
return href + (~href.indexOf("?") ? "&" : "?") + $.param(parameters);
107107
};
108108

109+
const reanimateScripts = (component) => {
110+
for (const script of Array.from(component.querySelectorAll("script"))) {
111+
const newScript = document.createElement("script");
112+
newScript.textContent = script.textContent;
113+
for (const attribute of Array.from(script.attributes)) {
114+
newScript.setAttribute(attribute.name, attribute.value);
115+
}
116+
script.parentNode?.replaceChild(newScript, script);
117+
}
118+
};
119+
109120
const getHeaders = () => {
110121
const globalHeaders = ocConf.globalHeaders;
111122
return {
@@ -115,7 +126,9 @@ export function createOc(oc) {
115126
};
116127

117128
oc.addStylesToHead = (styles) => {
118-
$("<style>" + styles + "</style>").appendTo($document.head);
129+
const style = document.createElement("style");
130+
style.textContent = styles;
131+
document.head.appendChild(style);
119132
};
120133

121134
const loadAfterReady = () => {
@@ -186,29 +199,31 @@ export function createOc(oc) {
186199

187200
oc.requireSeries = asyncRequireForEach;
188201

189-
const processHtml = ($component, data, callback) => {
190-
const attr = $component.attr.bind($component);
202+
const processHtml = (component, data, callback) => {
203+
const setAttribute = component.setAttribute.bind(component);
191204
const dataName = data.name;
192205
const dataVersion = data.version;
193-
attr("id", data.id);
194-
attr(dataRenderedAttribute, true);
195-
attr(dataRenderingAttribute, false);
196-
attr("data-version", dataVersion);
197-
attr("data-id", data.ocId);
198-
$component.html(data.html);
206+
setAttribute("id", data.id);
207+
setAttribute(dataRenderedAttribute, true);
208+
setAttribute(dataRenderingAttribute, false);
209+
setAttribute("data-version", dataVersion);
210+
setAttribute("data-id", data.ocId);
211+
component.innerHTML = data.html;
212+
// If the html contains <scripts> tags, innerHTML will not execute them.
213+
// So we need to do it manually.
214+
reanimateScripts(component);
199215

200216
if (data.key) {
201-
attr("data-hash", data.key);
217+
setAttribute("data-hash", data.key);
202218
}
203219

204220
if (dataName) {
205-
attr("data-name", dataName);
221+
setAttribute("data-name", dataName);
206222
renderedComponents[dataName] = { version: dataVersion };
207223
if (data.baseUrl) {
208224
renderedComponents[dataName].baseUrl = data.baseUrl;
209225
}
210-
// Get raw element from jQuery object
211-
data.element = $component[0];
226+
data.element = component;
212227
oc.events.fire("oc:rendered", data);
213228
}
214229

@@ -431,39 +446,44 @@ export function createOc(oc) {
431446

432447
oc.renderNestedComponent = (component, callback) => {
433448
oc.ready(() => {
434-
const $component = $(component);
435-
const attr = $component.attr.bind($component);
436-
const dataRendering = attr(dataRenderingAttribute);
437-
const dataRendered = attr(dataRenderedAttribute);
449+
// If the component is a jQuery object, we need to get the first element
450+
component = component[0] || component;
451+
const getAttribute = component.getAttribute.bind(component);
452+
const setAttribute = component.setAttribute.bind(component);
453+
const dataRendering = getAttribute(dataRenderingAttribute);
454+
const dataRendered = getAttribute(dataRenderedAttribute);
438455
const isRendering = dataRendering == "true";
439456
const isRendered = dataRendered == "true";
440457

441458
if (!isRendering && !isRendered) {
442459
logInfo(MESSAGES_RETRIEVING);
443-
attr(dataRenderingAttribute, true);
460+
setAttribute(dataRenderingAttribute, true);
444461
if (!DISABLE_LOADER) {
445-
$component.html(
446-
'<div class="oc-loading">' + MESSAGES_LOADING_COMPONENT + "</div>",
447-
);
462+
component.innerHTML =
463+
'<div class="oc-loading">' + MESSAGES_LOADING_COMPONENT + "</div>";
448464
}
449465

450466
oc.renderByHref(
451-
{ href: attr("href"), id: attr("id"), element: $component[0] },
467+
{
468+
href: getAttribute("href"),
469+
id: getAttribute("id"),
470+
element: component,
471+
},
452472
(err, data) => {
453473
if (err || !data) {
454-
attr(dataRenderingAttribute, false);
455-
attr(dataRenderedAttribute, false);
456-
attr("data-failed", true);
457-
$component.html("");
474+
setAttribute(dataRenderingAttribute, false);
475+
setAttribute(dataRenderedAttribute, false);
476+
setAttribute("data-failed", true);
477+
component.innerHTML = "";
458478
oc.events.fire("oc:failed", {
459479
originalError: err,
460480
data: data,
461-
component: $component[0],
481+
component,
462482
});
463483
logError(err);
464484
callback();
465485
} else {
466-
processHtml($component, data, callback);
486+
processHtml(component, data, callback);
467487
}
468488
},
469489
);
@@ -549,13 +569,13 @@ export function createOc(oc) {
549569

550570
oc.renderUnloadedComponents = () => {
551571
oc.ready(() => {
552-
const $unloadedComponents = $(
553-
OC_TAG + "[data-rendered!=true][data-failed!=true]",
572+
const unloadedComponents = document.querySelectorAll(
573+
`${OC_TAG}:not([data-rendered="true"]):not([data-failed="true"])`,
554574
);
555575

556-
$unloadedComponents.map((idx, unloadedComponent) => {
576+
unloadedComponents.forEach((unloadedComponent, idx) => {
557577
oc.renderNestedComponent(unloadedComponent, () => {
558-
if (idx == $unloadedComponents.length - 1) {
578+
if (idx == unloadedComponents.length - 1) {
559579
oc.renderUnloadedComponents();
560580
}
561581
});
@@ -568,8 +588,9 @@ export function createOc(oc) {
568588
callback = callback || noop;
569589

570590
if (placeholder) {
571-
$(placeholder).html("<" + OC_TAG + ' href="' + href + '" />');
572-
const newComponent = $(OC_TAG, placeholder);
591+
placeholder = placeholder[0] || placeholder;
592+
placeholder.innerHTML = "<" + OC_TAG + ' href="' + href + '" />';
593+
const newComponent = placeholder.querySelector(OC_TAG);
573594
oc.renderNestedComponent(newComponent, () => {
574595
callback(newComponent);
575596
});

0 commit comments

Comments
 (0)