Comparing version 1.0.1 to 1.0.2
@@ -1,1 +0,124 @@ | ||
!function c(u,i,a){function f(e,n){if(!i[e]){if(!u[e]){var t="function"==typeof require&&require;if(!n&&t)return t(e,!0);if(l)return l(e,!0);var r=new Error("Cannot find module '"+e+"'");throw r.code="MODULE_NOT_FOUND",r}var o=i[e]={exports:{}};u[e][0].call(o.exports,function(n){return f(u[e][1][n]||n)},o,o.exports,c,u,i,a)}return i[e].exports}for(var l="function"==typeof require&&require,n=0;n<a.length;n++)f(a[n]);return f}({1:[function(n,e,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var o=function(){function r(n,e){for(var t=0;t<e.length;t++){var r=e[t];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(n,r.key,r)}}return function(n,e,t){return e&&r(n.prototype,e),t&&r(n,t),n}}();var r=function(){function r(n){var e=1<arguments.length&&void 0!==arguments[1]?arguments[1]:function(n,e){n.innerHTML=e},t=2<arguments.length&&void 0!==arguments[2]?arguments[2]:"data-fetch";!function(n,e){if(!(n instanceof e))throw new TypeError("Cannot call a class as a function")}(this,r),this.fetchBlocks(n,e,t).then(function(){return document.dispatchEvent(new CustomEvent("allBlocksLoaded"))}).catch(function(n){return console.error("Asyncei",n)})}return o(r,[{key:"fetchBlocks",value:function(t,r,o){var c=this,u={};return document.querySelectorAll("["+o+"]").forEach(function(e){var n=e.getAttribute(o);u.hasOwnProperty(n)||(u[n]=fetch(t+n).then(function(n){return 200===n.status&&n.text().then(function(n){return c.fetchContent(n)})||console.error("Asyncei","Error fetching")}).catch(function(n){return console.error("Asyncei",n)})),u[n].then(function(n){r(e,n),e.dispatchEvent(new CustomEvent("blockContentLoaded",{bubbles:!0,detail:{content:n}}))}).catch(function(n){return console.error("Asyncei",n)})}),Promise.all(Object.values(u))}},{key:"fetchContent",value:function(e){try{return Promise.all(e.match(/<img\ssrc=["|']([^"|^']\S+)["|']/gi).map(function(r){return new Promise(function(n){var e=r.match(/src=["|']([^"|^']\S+)["|']/i)[1],t=new Image;t.onload=function(){return n()},t.onerror=function(){return n()},t.src=e})})).then(function(){return e})}catch(n){return e}}}]),r}();t.default=r},{}]},{},[1]); | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
/** | ||
* @class Asyncei | ||
* @author Alfreds Genkins, Ilja Lapkovskis | ||
* @version 0.0.1 | ||
*/ | ||
var Asyncei = function () { | ||
/** | ||
* Asynchronous block loading script | ||
* @param {string} handlerURL URL to fetch blocks | ||
* @param {function} renderFunction Function to render blocks | ||
* @param {string} queryAttribute data attribute to query blocks | ||
* @event allBlocksLoaded fires when all blocks are loaded | ||
* @event blockContentLoaded fires when a block is loaded | ||
*/ | ||
function Asyncei(handlerURL) { | ||
var renderFunction = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function (element, text) { | ||
element.innerHTML = text; | ||
}; | ||
var queryAttribute = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'data-fetch'; | ||
_classCallCheck(this, Asyncei); | ||
this.fetchBlocks(handlerURL, renderFunction, queryAttribute).then(function () { | ||
return document.dispatchEvent(new CustomEvent('allBlocksLoaded')); | ||
}) // dispatch all elements load event | ||
.catch(function (err) { | ||
return console.error('Asyncei', err); | ||
}); | ||
} | ||
/** | ||
* Fetch blocks (query & fetch content) | ||
* @param {string} handlerURL | ||
* @param {function} renderFunction | ||
* @param {string} queryAttribute | ||
*/ | ||
_createClass(Asyncei, [{ | ||
key: 'fetchBlocks', | ||
value: function fetchBlocks(handlerURL, renderFunction, queryAttribute) { | ||
var _this = this; | ||
var renders = {}; // to collect all renders by key (for elements) | ||
document.querySelectorAll('[' + queryAttribute + ']').forEach(function (element) { | ||
var blockName = element.getAttribute(queryAttribute); | ||
if (!renders.hasOwnProperty(blockName)) { | ||
// fetch content of the block if it is not present | ||
renders[blockName] = fetch(handlerURL + blockName).then(function (res) { | ||
return res.status === 200 && // if response status 200 – fetch content | ||
res.text().then(function (text) { | ||
return _this.fetchContent(text); | ||
}) || console.error('Asyncei', 'Error fetching'); | ||
}).catch(function (err) { | ||
return console.error('Asyncei', err); | ||
}); | ||
} | ||
renders[blockName].then(function (text) { | ||
renderFunction(element, text); | ||
element.dispatchEvent(new CustomEvent('blockContentLoaded', { // dispatch element content load event | ||
bubbles: true, | ||
detail: { | ||
content: text | ||
} | ||
})); | ||
}).catch(function (err) { | ||
return console.error('Asyncei', err); | ||
}); | ||
}); | ||
return Promise.all(Object.values(renders)); // return promise waiting for every element to resolve | ||
} | ||
/** | ||
* Fetch content (lazy load images, before show) | ||
* @param {string} text | ||
*/ | ||
}, { | ||
key: 'fetchContent', | ||
value: function fetchContent(text) { | ||
try { | ||
// try matching regex | ||
return Promise.all( // make a promise waiting for other promises to resolve | ||
text.match(/<img\ssrc=["|']([^"|^']\S+)["|']/ig).map( // map regex to a promise | ||
function (el) { | ||
return new Promise(function (resolve) { | ||
var imageSource = el.match(/src=["|']([^"|^']\S+)["|']/i)[1], | ||
fakeImage = new Image(); | ||
fakeImage.onload = function () { | ||
return resolve(); | ||
}; | ||
fakeImage.onerror = function () { | ||
return resolve(); | ||
}; | ||
fakeImage.src = imageSource; | ||
}); | ||
})).then(function () { | ||
return text; | ||
}); | ||
} catch (error) { | ||
return text; | ||
} | ||
} | ||
}]); | ||
return Asyncei; | ||
}(); | ||
exports.default = Asyncei; |
{ | ||
"name": "asyncei", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Pure JS, lighweight, asynchronous content loader", | ||
@@ -5,0 +5,0 @@ "main": "dist/asyncei.js", |
/** | ||
* @class Asyncei | ||
* @author Alfreds Genkins, Ilja Lapkovskis | ||
* @version 0.0.1 | ||
* @version 1.0.2 | ||
*/ | ||
@@ -6,0 +6,0 @@ class Asyncei { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
10825
178
1
0