From d5fbd00378f8476d859a4c14d3be0d5793998d95 Mon Sep 17 00:00:00 2001 From: Philippe Santana Costa Date: Tue, 14 Jun 2016 08:57:51 -0300 Subject: [PATCH] =?UTF-8?q?Adiciona=20cabe=C3=A7alho=20"X-Hinclude-Request?= =?UTF-8?q?ed-With"=20para=20identificar=20requisi=C3=A7=C3=A3o.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hinclude.js | 453 ++++++++++++++++++++++++------------------------ hinclude.min.js | 2 +- 2 files changed, 228 insertions(+), 227 deletions(-) diff --git a/hinclude.js b/hinclude.js index e48a3ba..df68775 100644 --- a/hinclude.js +++ b/hinclude.js @@ -31,253 +31,254 @@ See http://mnot.github.com/hinclude/ for documentation. var hinclude; -(function () { +(function() { - 'use strict'; + 'use strict'; - var is_being_displayed = function (element, threshold) { - var coordinates = element.getBoundingClientRect(); - return ( - coordinates.top >= 0 && coordinates.left >= 0 && - (coordinates.top - threshold) <= (window.innerHeight || document.documentElement.clientHeight) - ); - }; + var is_being_displayed = function(element, threshold) { + var coordinates = element.getBoundingClientRect(); + return ( + coordinates.top >= 0 && coordinates.left >= 0 && + (coordinates.top - threshold) <= (window.innerHeight || document.documentElement.clientHeight) + ); + }; - hinclude = { - classprefix: 'include_', + hinclude = { + classprefix: 'include_', - set_content_async: function (element, req) { - if (req.readyState === 4) { - if (req.status === 200 || req.status === 304) { - element.innerHTML = req.responseText; - } - element.className = hinclude.classprefix + req.status; - } - }, + set_content_async: function(element, req) { + if (req.readyState === 4) { + if (req.status === 200 || req.status === 304) { + element.innerHTML = req.responseText; + } + element.className = hinclude.classprefix + req.status; + } + }, - buffer: [], - set_content_buffered: function (element, req) { - if (req.readyState === 4) { - hinclude.buffer.push([element, req]); - hinclude.outstanding -= 1; - if (hinclude.outstanding === 0) { - hinclude.show_buffered_content(); - } - } - }, + buffer: [], + set_content_buffered: function(element, req) { + if (req.readyState === 4) { + hinclude.buffer.push([element, req]); + hinclude.outstanding -= 1; + if (hinclude.outstanding === 0) { + hinclude.show_buffered_content(); + } + } + }, - show_buffered_content: function () { - var include; - while (hinclude.buffer.length > 0) { - include = hinclude.buffer.pop(); - if (include[1].status === 200 || include[1].status === 304) { - include[0].innerHTML = include[1].responseText; - } - include[0].className = hinclude.classprefix + include[1].status; - } - }, + show_buffered_content: function() { + var include; + while (hinclude.buffer.length > 0) { + include = hinclude.buffer.pop(); + if (include[1].status === 200 || include[1].status === 304) { + include[0].innerHTML = include[1].responseText; + } + include[0].className = hinclude.classprefix + include[1].status; + } + }, - outstanding: 0, - includes: [], - run: function () { - var i = 0; - var mode = this.get_meta('include_mode', 'buffered'); - var callback; - this.includes = document.getElementsByTagName('hx:include'); - if (this.includes.length === 0) { // remove ns for IE - this.includes = document.getElementsByTagName('include'); - } - if (mode === 'async') { - callback = this.set_content_async; - } else if (mode === 'buffered') { - callback = this.set_content_buffered; - var timeout = this.get_meta('include_timeout', 2.5) * 1000; - setTimeout(hinclude.show_buffered_content, timeout); - } + outstanding: 0, + includes: [], + run: function() { + var i = 0; + var mode = this.get_meta('include_mode', 'buffered'); + var callback; + this.includes = document.getElementsByTagName('hx:include'); + if (this.includes.length === 0) { // remove ns for IE + this.includes = document.getElementsByTagName('include'); + } + if (mode === 'async') { + callback = this.set_content_async; + } else if (mode === 'buffered') { + callback = this.set_content_buffered; + var timeout = this.get_meta('include_timeout', 2.5) * 1000; + setTimeout(hinclude.show_buffered_content, timeout); + } - for (i; i < this.includes.length; i += 1) { - this.include(this.includes[i], this.includes[i].getAttribute('src'), this.includes[i].getAttribute('media'), callback); - } - }, + for (i; i < this.includes.length; i += 1) { + this.include(this.includes[i], this.includes[i].getAttribute('src'), this.includes[i].getAttribute('media'), callback); + } + }, - include: function (element, url, media, incl_cb) { - if (media && window.matchMedia && !window.matchMedia(media).matches) { - return; - } + include: function(element, url, media, incl_cb) { + if (media && window.matchMedia && !window.matchMedia(media).matches) { + return; + } - if (element.dataset.lazy === 'true') { - this.lazy(element, url, media, incl_cb); - return; - } + if (element.dataset.lazy === 'true') { + this.lazy(element, url, media, incl_cb); + return; + } - var scheme = url.substring(0, url.indexOf(':')); - if (scheme.toLowerCase() === 'data') { // just text/plain for now - var data = decodeURIComponent(url.substring(url.indexOf(',') + 1, url.length)); - element.innerHTML = data; - return; - } + var scheme = url.substring(0, url.indexOf(':')); + if (scheme.toLowerCase() === 'data') { // just text/plain for now + var data = decodeURIComponent(url.substring(url.indexOf(',') + 1, url.length)); + element.innerHTML = data; + return; + } - var req = false; - if (window.XMLHttpRequest) { - try { - req = new XMLHttpRequest(); - } catch (e1) { - req = false; - } - } else if (window.ActiveXObject) { - try { - req = new ActiveXObject('Microsoft.XMLHTTP'); - } catch (e2) { - req = false; - } - } - if (req) { - this.outstanding += 1; - req.onreadystatechange = function () { - incl_cb(element, req); - if (req.readyState === 4) { - hinclude.dispatch_loaded_event(element); - } - }; - try { - req.open('GET', url, true); - req.send(''); - } catch (e3) { - this.outstanding -= 1; - alert('Include error: ' + url + ' (' + e3 + ')'); - } - } - }, + var req = false; + if (window.XMLHttpRequest) { + try { + req = new XMLHttpRequest(); + } catch (e1) { + req = false; + } + } else if (window.ActiveXObject) { + try { + req = new ActiveXObject('Microsoft.XMLHTTP'); + } catch (e2) { + req = false; + } + } + if (req) { + this.outstanding += 1; + req.onreadystatechange = function() { + incl_cb(element, req); + if (req.readyState === 4) { + hinclude.dispatch_loaded_event(element); + } + }; + try { + req.open('GET', url, true); + req.setRequestHeader('X-Hinclude-Requested-With', 'XMLHttpRequest'); + req.send(''); + } catch (e3) { + this.outstanding -= 1; + alert('Include error: ' + url + ' (' + e3 + ')'); + } + } + }, - lazy: function (element, url, media, incl_cb) { - var threshold = +element.dataset.threshold || 0; + lazy: function(element, url, media, incl_cb) { + var threshold = +element.dataset.threshold || 0; - if (element.dataset.lazy) delete element.dataset.lazy; + if (element.dataset.lazy) delete element.dataset.lazy; if (element.dataset.treshhold) delete element.dataset.threshold; - if (is_being_displayed(element, threshold)) { - this.include(element, url, media, incl_cb); - return; - } + if (is_being_displayed(element, threshold)) { + this.include(element, url, media, incl_cb); + return; + } - var that = this; - var load_listener = function () { - if (is_being_displayed(element, threshold)) { - window.removeEventListener('scroll', load_listener); - window.removeEventListener('resize', load_listener); - that.include(element, url, media, incl_cb); - } - }; - window.addEventListener('scroll', load_listener); - window.addEventListener('resize', load_listener); - }, + var that = this; + var load_listener = function() { + if (is_being_displayed(element, threshold)) { + window.removeEventListener('scroll', load_listener); + window.removeEventListener('resize', load_listener); + that.include(element, url, media, incl_cb); + } + }; + window.addEventListener('scroll', load_listener); + window.addEventListener('resize', load_listener); + }, - dispatch_loaded_event: function (element) { - var e; - try { - e = new CustomEvent('loaded', { - bubbles: false, - cancelable: false - }); - } catch (err) { - e = document.createEvent('CustomEvent'); - e.initEvent('loaded', false, false); - } - element.dispatchEvent(e); - }, + dispatch_loaded_event: function(element) { + var e; + try { + e = new CustomEvent('loaded', { + bubbles: false, + cancelable: false + }); + } catch (err) { + e = document.createEvent('CustomEvent'); + e.initEvent('loaded', false, false); + } + element.dispatchEvent(e); + }, - refresh: function (element_id) { - var i = 0; - var callback; - callback = this.set_content_buffered; - for (i; i < this.includes.length; i += 1) { - if (this.includes[i].getAttribute('id') === element_id) { - this.include(this.includes[i], this.includes[i].getAttribute('src'), this.includes[i].getAttribute('media'), callback); - } - } - }, + refresh: function(element_id) { + var i = 0; + var callback; + callback = this.set_content_buffered; + for (i; i < this.includes.length; i += 1) { + if (this.includes[i].getAttribute('id') === element_id) { + this.include(this.includes[i], this.includes[i].getAttribute('src'), this.includes[i].getAttribute('media'), callback); + } + } + }, - get_meta: function (name, value_default) { - var m = 0; - var metas = document.getElementsByTagName('meta'); - var meta_name; - for (m; m < metas.length; m += 1) { - meta_name = metas[m].getAttribute('name'); - if (meta_name === name) { - return metas[m].getAttribute('content'); - } - } - return value_default; - }, + get_meta: function(name, value_default) { + var m = 0; + var metas = document.getElementsByTagName('meta'); + var meta_name; + for (m; m < metas.length; m += 1) { + meta_name = metas[m].getAttribute('name'); + if (meta_name === name) { + return metas[m].getAttribute('content'); + } + } + return value_default; + }, - /* - * (c)2006 Dean Edwards/Matthias Miller/John Resig - * Special thanks to Dan Webb's domready.js Prototype extension - * and Simon Willison's addLoadEvent - * - * For more info, see: - * http://dean.edwards.name/weblog/2006/06/again/ - * - * Thrown together by Jesse Skinner (http://www.thefutureoftheweb.com/) - */ - addDOMLoadEvent: function (func) { - if (!window.__load_events) { - var init = function () { - var i = 0; - // quit if this function has already been called - if (hinclude.addDOMLoadEvent.done) { - return; - } - hinclude.addDOMLoadEvent.done = true; - if (window.__load_timer) { - clearInterval(window.__load_timer); - window.__load_timer = null; - } - for (i; i < window.__load_events.length; i += 1) { - window.__load_events[i](); - } - window.__load_events = null; - // clean up the __ie_onload event - /*@cc_on - document.getElementById("__ie_onload").onreadystatechange = ""; - @*/ - }; - // for Mozilla/Opera9 - if (document.addEventListener) { - document.addEventListener('DOMContentLoaded', init, false); - } - // for Internet Explorer - /*@cc_on - document.write( - "<\/scr" - + "ipt>" - ); - var script = document.getElementById("__ie_onload"); - script.onreadystatechange = function () { - if (this.readyState === "complete") { - init(); // call the onload handler - } - }; - @*/ - // for Safari - if (/WebKit/i.test(navigator.userAgent)) { // sniff - window.__load_timer = setInterval(function () { - if (/loaded|complete/.test(document.readyState)) { - init(); - } - }, 10); - } - // for other browsers - window.onload = init; - window.__load_events = []; - } - window.__load_events.push(func); - } - }; + /* + * (c)2006 Dean Edwards/Matthias Miller/John Resig + * Special thanks to Dan Webb's domready.js Prototype extension + * and Simon Willison's addLoadEvent + * + * For more info, see: + * http://dean.edwards.name/weblog/2006/06/again/ + * + * Thrown together by Jesse Skinner (http://www.thefutureoftheweb.com/) + */ + addDOMLoadEvent: function(func) { + if (!window.__load_events) { + var init = function() { + var i = 0; + // quit if this function has already been called + if (hinclude.addDOMLoadEvent.done) { + return; + } + hinclude.addDOMLoadEvent.done = true; + if (window.__load_timer) { + clearInterval(window.__load_timer); + window.__load_timer = null; + } + for (i; i < window.__load_events.length; i += 1) { + window.__load_events[i](); + } + window.__load_events = null; + // clean up the __ie_onload event + /*@cc_on + document.getElementById("__ie_onload").onreadystatechange = ""; + @*/ + }; + // for Mozilla/Opera9 + if (document.addEventListener) { + document.addEventListener('DOMContentLoaded', init, false); + } + // for Internet Explorer + /*@cc_on + document.write( + "<\/scr" + + "ipt>" + ); + var script = document.getElementById("__ie_onload"); + script.onreadystatechange = function () { + if (this.readyState === "complete") { + init(); // call the onload handler + } + }; + @*/ + // for Safari + if (/WebKit/i.test(navigator.userAgent)) { // sniff + window.__load_timer = setInterval(function() { + if (/loaded|complete/.test(document.readyState)) { + init(); + } + }, 10); + } + // for other browsers + window.onload = init; + window.__load_events = []; + } + window.__load_events.push(func); + } + }; - hinclude.addDOMLoadEvent(function () { - hinclude.run(); - }); + hinclude.addDOMLoadEvent(function() { + hinclude.run(); + }); }()); diff --git a/hinclude.min.js b/hinclude.min.js index 3424615..4cba092 100644 --- a/hinclude.min.js +++ b/hinclude.min.js @@ -1 +1 @@ -var hinclude;!function(){"use strict";var e=function(e,t){var n=e.getBoundingClientRect();return n.top>=0&&n.left>=0&&n.top-t<=(window.innerHeight||document.documentElement.clientHeight)};hinclude={classprefix:"include_",set_content_async:function(e,t){4===t.readyState&&((200===t.status||304===t.status)&&(e.innerHTML=t.responseText),e.className=hinclude.classprefix+t.status)},buffer:[],set_content_buffered:function(e,t){4===t.readyState&&(hinclude.buffer.push([e,t]),hinclude.outstanding-=1,0===hinclude.outstanding&&hinclude.show_buffered_content())},show_buffered_content:function(){for(var e;hinclude.buffer.length>0;)e=hinclude.buffer.pop(),(200===e[1].status||304===e[1].status)&&(e[0].innerHTML=e[1].responseText),e[0].className=hinclude.classprefix+e[1].status},outstanding:0,includes:[],run:function(){var e,t=0,n=this.get_meta("include_mode","buffered");if(this.includes=document.getElementsByTagName("hx:include"),0===this.includes.length&&(this.includes=document.getElementsByTagName("include")),"async"===n)e=this.set_content_async;else if("buffered"===n){e=this.set_content_buffered;var i=1e3*this.get_meta("include_timeout",2.5);setTimeout(hinclude.show_buffered_content,i)}for(t;t=0&&n.left>=0&&n.top-t<=(window.innerHeight||document.documentElement.clientHeight)};hinclude={classprefix:"include_",set_content_async:function(e,t){4===t.readyState&&(200!==t.status&&304!==t.status||(e.innerHTML=t.responseText),e.className=hinclude.classprefix+t.status)},buffer:[],set_content_buffered:function(e,t){4===t.readyState&&(hinclude.buffer.push([e,t]),hinclude.outstanding-=1,0===hinclude.outstanding&&hinclude.show_buffered_content())},show_buffered_content:function(){for(var e;hinclude.buffer.length>0;)e=hinclude.buffer.pop(),200!==e[1].status&&304!==e[1].status||(e[0].innerHTML=e[1].responseText),e[0].className=hinclude.classprefix+e[1].status},outstanding:0,includes:[],run:function(){var e,t=0,n=this.get_meta("include_mode","buffered");if(this.includes=document.getElementsByTagName("hx:include"),0===this.includes.length&&(this.includes=document.getElementsByTagName("include")),"async"===n)e=this.set_content_async;else if("buffered"===n){e=this.set_content_buffered;var i=1e3*this.get_meta("include_timeout",2.5);setTimeout(hinclude.show_buffered_content,i)}for(t;t