diff --git a/source/js/utils.js b/source/js/utils.js index 2ca1a23c2..c4b33f876 100644 --- a/source/js/utils.js +++ b/source/js/utils.js @@ -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); - }) + } };