Comparing version 1.0.2 to 1.0.3
@@ -1,124 +0,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; | ||
'use strict';var _createClass=function(){function a(b,c){for(var e,d=0;d<c.length;d++)e=c[d],e.enumerable=e.enumerable||!1,e.configurable=!0,'value'in e&&(e.writable=!0),Object.defineProperty(b,e.key,e)}return function(b,c,d){return c&&a(b.prototype,c),d&&a(b,d),b}}();Object.defineProperty(exports,'__esModule',{value:!0});function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError('Cannot call a class as a function')}var Asyncei=function(){function a(b){var c=1<arguments.length&&void 0!==arguments[1]?arguments[1]:function(e,f){e.innerHTML=f},d=2<arguments.length&&void 0!==arguments[2]?arguments[2]:'data-fetch';_classCallCheck(this,a),this.fetchBlocks(b,c,d).then(function(){return document.dispatchEvent(new CustomEvent('allBlocksLoaded'))}).catch(function(e){return console.error('Asyncei',e)})}return _createClass(a,[{key:'fetchBlocks',value:function fetchBlocks(b,c,d){var f=this,e={};return document.querySelectorAll('['+d+']').forEach(function(g){var h=g.getAttribute(d);e.hasOwnProperty(h)||(e[h]=fetch(b+h).then(function(j){return 200===j.status&&j.text().then(function(k){return f.fetchContent(k)})||console.error('Asyncei','Error fetching')}).catch(function(j){return console.error('Asyncei',j)})),e[h].then(function(j){c(g,j),g.dispatchEvent(new CustomEvent('blockContentLoaded',{bubbles:!0,detail:{content:j}}))}).catch(function(j){return console.error('Asyncei',j)})}),Promise.all(Object.values(e))}},{key:'fetchContent',value:function fetchContent(b){try{return Promise.all(b.match(/<img\ssrc=["|']([^"|^']\S+)["|']/ig).map(function(c){return new Promise(function(d){var e=c.match(/src=["|']([^"|^']\S+)["|']/i)[1],f=new Image;f.onload=function(){return d()},f.onerror=function(){return d()},f.src=e})})).then(function(){return b})}catch(c){return b}}}]),a}();exports.default=Asyncei; |
{ | ||
"name": "asyncei", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Pure JS, lighweight, asynchronous content loader", | ||
@@ -5,0 +5,0 @@ "main": "dist/asyncei.js", |
/** | ||
* @class Asyncei | ||
* @author Alfreds Genkins, Ilja Lapkovskis | ||
* @version 1.0.2 | ||
* @version 1.0.3 | ||
*/ | ||
@@ -6,0 +6,0 @@ class Asyncei { |
7530
73