Skip to content

Commit

Permalink
Make #241 backward compatible (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
PaperStrike authored Apr 30, 2021
1 parent eefc25d commit e301598
Showing 1 changed file with 55 additions and 42 deletions.
97 changes: 55 additions & 42 deletions source/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,53 +337,66 @@ NexT.utils = {
}
},

getScript: (url, {
condition = false,
attributes: {
id = '',
async = false,
defer = false,
crossOrigin = '',
dataset = {},
...otherAttributes
} = {},
parentNode = null
} = {}) => new Promise((resolve, reject) => {
if (condition) {
resolve();
} else {
const script = document.createElement('script');
getScript: function(url, options = {}, legacyCondition) {
if (typeof options === 'function') {
return this.getScript(url, {
condition: legacyCondition
}).then(options);
}
const {
condition = false,
attributes: {
id = '',
async = false,
defer = false,
crossOrigin = '',
dataset = {},
...otherAttributes
} = {},
parentNode = null
} = options;
return new Promise((resolve, reject) => {
if (condition) {
resolve();
} else {
const script = document.createElement('script');

if (id) script.id = id;
if (crossOrigin) script.crossOrigin = crossOrigin;
script.async = async;
script.defer = defer;
Object.assign(script.dataset, dataset);
Object.entries(otherAttributes).forEach(([name, value]) => {
script.setAttribute(name, String(value));
});
if (id) script.id = id;
if (crossOrigin) script.crossOrigin = crossOrigin;
script.async = async;
script.defer = defer;
Object.assign(script.dataset, dataset);
Object.entries(otherAttributes).forEach(([name, value]) => {
script.setAttribute(name, String(value));
});

script.onload = resolve;
script.onerror = reject;
script.onload = resolve;
script.onerror = reject;

script.src = url;
(parentNode || document.head).appendChild(script);
}
}),
script.src = url;
(parentNode || document.head).appendChild(script);
}
});
},

loadComments: (selector) => new Promise((resolve) => {
const element = document.querySelector(selector);
if (!CONFIG.comments.lazyload || !element) {
resolve();
return;
loadComments: function(selector, legacyCallback) {
if (legacyCallback) {
return this.loadComments(selector).then(legacyCallback);
}
const intersectionObserver = new IntersectionObserver((entries, observer) => {
const entry = entries[0];
if (!entry.isIntersecting) return;
return new Promise((resolve) => {
const element = document.querySelector(selector);
if (!CONFIG.comments.lazyload || !element) {
resolve();
return;
}
const intersectionObserver = new IntersectionObserver((entries, observer) => {
const entry = entries[0];
if (!entry.isIntersecting) return;

resolve();
observer.disconnect();
resolve();
observer.disconnect();
});
intersectionObserver.observe(element);
});
intersectionObserver.observe(element);
})
}
};

0 comments on commit e301598

Please sign in to comment.