-
Notifications
You must be signed in to change notification settings - Fork 18
/
h-include-extensions.js
181 lines (141 loc) · 5.13 KB
/
h-include-extensions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*jslint indent: 2, browser: true, vars: true, nomen: true, loopfunc: true */
// Only loads on refresh(), to be used with initLazyLoad (below)
window.HInclude.HIncludeLazyElement = (function() {
var proto = Object.create(HInclude.HIncludeElement.prototype);
proto.connectedCallback = function(){};
var HIncludeLazyElement = function() {
return Reflect.construct(HTMLElement, arguments, HIncludeLazyElement);
};
HIncludeLazyElement.prototype = proto;
customElements.define('h-include-lazy', HIncludeLazyElement);
return HIncludeLazyElement;
})();
// Imports resource fragments (containing links to scripts and styles)
window.HInclude.HImportElement = (function() {
var proto = Object.create(HInclude.HIncludeElement.prototype);
proto.createContainer = function(req){
var container = document.implementation.createHTMLDocument(' ').documentElement;
container.innerHTML = req.responseText;
var importableElements =
Array.prototype.slice.call(container.querySelectorAll('script, link'));
var urls = importableElements.map(function(element) {
if (element.tagName === 'SCRIPT') {
return element.src;
} else if (
element.tagName === 'LINK' &&
element.rel &&
element.rel.toLowerCase() === 'stylesheet') {
return element.href;
}
}).filter(function(url){ return !!url; });
HInclude.loadResources(urls);
return container;
};
proto.replaceContent = function(node) {};
var HImportElement = function() {
return Reflect.construct(HTMLElement, arguments, HImportElement);
};
HImportElement.prototype = proto;
customElements.define('h-import', HImportElement);
return HImportElement;
})();
// Import resource fragments only on refresh(), to be used with initLazyLoad (below)
window.HInclude.HImportLazyElement = (function() {
var proto = Object.create(HInclude.HImportElement.prototype);
proto.connectedCallback = function(){};
var HImportLazyElement = function() {
return Reflect.construct(HTMLElement, arguments, HImportLazyElement);
};
HImportLazyElement.prototype = proto;
customElements.define('h-import-lazy', HImportLazyElement);
return HImportLazyElement;
})();
// Navigate within included content by handling link clicks
window.HInclude.HIncludeNavigateElement = (function() {
var getElement = function(elementArg, nodeName) {
var element;
element = elementArg;
while (element.parentNode && element.nodeName !== nodeName) {
element = element.parentNode;
}
if (element.nodeName === nodeName) {
return element;
}
};
var getLink = function(element) {
var link = getElement(element, 'A');
if (link && link.href.length !== 0) {
return link;
}
};
var getHinclude = function(element) {
return getElement(element, 'H-INCLUDE-NAVIGATE');
};
var handle = function(e) {
var link = getLink(e.target);
if (!link) return;
// Detect target attribute
var targetAttribute = link.getAttribute('target');
if (targetAttribute === '_top') {
// Treat as regular link
return;
}
// Navigate
var href = link.href;
if (!href) return;
var hElement = getHinclude(e.target);
hElement.setAttribute('src', href);
hElement.refresh();
e.preventDefault();
};
var registerNavigation = function(element) {
element.addEventListener('click', handle, true);
};
var proto = Object.create(HInclude.HIncludeElement.prototype);
proto.connectedCallback = function(){
HIncludeElement.prototype.connectedCallback.apply(this, arguments); // call super
registerNavigation(this);
};
var HIncludeNavigateElement = function() {
return Reflect.construct(HTMLElement, arguments, HIncludeNavigateElement);
};
HIncludeNavigateElement.prototype = proto;
customElements.define('h-include-navigate', HIncludeNavigateElement);
return HIncludeNavigateElement;
})();
// Lazy load elements using IntersectionObserver
window.HInclude.initLazyLoad = function(selectorArg, configArg){
var selector = selectorArg || 'h-include-lazy, h-import-lazy';
var elements = Array.prototype.slice.call(document.querySelectorAll(selector));
// When IntersectionObserver is absent, load eagerly instead
if (!('IntersectionObserver' in window)) {
[].forEach.call(elements, function(element) {
element.refresh();
});
return;
}
var config = {
root: configArg && configArg.root || null,
rootMargin: configArg && configArg.rootMargin || '400px 0px',
threshold: configArg && configArg.threshold || 0.01 // 1% of the target is visible
};
var observer = new IntersectionObserver(onIntersection, config);
[].forEach.call(elements, function(element) {
observer.observe(element);
});
function onIntersection(entries) {
entries.forEach(function(entry) {
if (entry.intersectionRatio > 0) {
observer.unobserve(entry.target);
entry.target.refresh();
}
});
}
};
// Load an array of script and stylesheet resources (to be overridden)
// By default, loadjs (https://github.com/muicss/loadjs) is used
window.HInclude.loadResources = function(urls){
loadjs(urls, {
async: false,
});
}