This repository has been archived by the owner on Mar 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor such that only base.js is necessary for native HTMLImports.
- Loading branch information
Showing
4 changed files
with
184 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/* | ||
* Copyright 2013 The Polymer Authors. All rights reserved. | ||
* Use of this source code is governed by a BSD-style | ||
* license that can be found in the LICENSE file. | ||
*/ | ||
|
||
(function(scope) { | ||
|
||
var hasNative = ('import' in document.createElement('link')); | ||
var useNative = hasNative; | ||
|
||
// TODO(sorvell): SD polyfill intrusion | ||
var mainDoc = window.ShadowDOMPolyfill ? | ||
ShadowDOMPolyfill.wrapIfNeeded(document) : document; | ||
|
||
// NOTE: We cannot polyfill document.currentScript because it's not possible | ||
// both to override and maintain the ability to capture the native value; | ||
// therefore we choose to expose _currentScript both when native imports | ||
// and the polyfill are in use. | ||
var currentScriptDescriptor = { | ||
get: function() { | ||
return HTMLImports.currentScript || document.currentScript; | ||
}, | ||
configurable: true | ||
}; | ||
|
||
Object.defineProperty(document, '_currentScript', currentScriptDescriptor); | ||
Object.defineProperty(mainDoc, '_currentScript', currentScriptDescriptor); | ||
|
||
// call a callback when all HTMLImports in the document at call (or at least | ||
// document ready) time have loaded. | ||
// 1. ensure the document is in a ready state (has dom), then | ||
// 2. watch for loading of imports and call callback when done | ||
function whenImportsReady(callback, doc) { | ||
doc = doc || mainDoc; | ||
// if document is loading, wait and try again | ||
whenDocumentReady(function() { | ||
watchImportsLoad(callback, doc); | ||
}, doc); | ||
} | ||
|
||
// call the callback when the document is in a ready state (has dom) | ||
var requiredReadyState = HTMLImports.isIE ? 'complete' : 'interactive'; | ||
var READY_EVENT = 'readystatechange'; | ||
function isDocumentReady(doc) { | ||
return (doc.readyState === 'complete' || | ||
doc.readyState === requiredReadyState); | ||
} | ||
|
||
// call <callback> when we ensure the document is in a ready state | ||
function whenDocumentReady(callback, doc) { | ||
if (!isDocumentReady(doc)) { | ||
var checkReady = function() { | ||
if (doc.readyState === 'complete' || | ||
doc.readyState === requiredReadyState) { | ||
doc.removeEventListener(READY_EVENT, checkReady); | ||
whenDocumentReady(callback, doc); | ||
} | ||
} | ||
doc.addEventListener(READY_EVENT, checkReady); | ||
} else if (callback) { | ||
callback(); | ||
} | ||
} | ||
|
||
// call <callback> when we ensure all imports have loaded | ||
function watchImportsLoad(callback, doc) { | ||
var imports = doc.querySelectorAll('link[rel=import]'); | ||
var loaded = 0, l = imports.length; | ||
function checkDone(d) { | ||
if (loaded == l) { | ||
callback && callback(); | ||
} | ||
} | ||
function loadedImport(e) { | ||
loaded++; | ||
checkDone(); | ||
} | ||
if (l) { | ||
for (var i=0, imp; (i<l) && (imp=imports[i]); i++) { | ||
if (isImportLoaded(imp)) { | ||
loadedImport.call(imp); | ||
} else { | ||
imp.addEventListener('load', loadedImport); | ||
imp.addEventListener('error', loadedImport); | ||
} | ||
} | ||
} else { | ||
checkDone(); | ||
} | ||
} | ||
|
||
function isImportLoaded(link) { | ||
return useNative ? (link.import && (link.import.readyState !== 'loading')) || link.__loaded : | ||
link.__importParsed; | ||
} | ||
|
||
// TODO(sorvell): install a mutation observer to see if HTMLImports have loaded | ||
// this is a workaround for https://www.w3.org/Bugs/Public/show_bug.cgi?id=25007 | ||
// and should be removed when this bug is addressed. | ||
if (useNative) { | ||
new MutationObserver(function(mxns) { | ||
for (var i=0, l=mxns.length, m; (i < l) && (m=mxns[i]); i++) { | ||
if (m.addedNodes) { | ||
handleImports(m.addedNodes); | ||
} | ||
} | ||
}).observe(document.head, {childList: true}); | ||
|
||
function handleImports(nodes) { | ||
for (var i=0, l=nodes.length, n; (i<l) && (n=nodes[i]); i++) { | ||
if (isImport(n)) { | ||
handleImport(n); | ||
} | ||
} | ||
} | ||
|
||
function isImport(element) { | ||
return element.localName === 'link' && element.rel === 'import'; | ||
} | ||
|
||
function handleImport(element) { | ||
var loaded = element.import; | ||
if (loaded) { | ||
markTargetLoaded({target: element}); | ||
} else { | ||
element.addEventListener('load', markTargetLoaded); | ||
element.addEventListener('error', markTargetLoaded); | ||
} | ||
} | ||
|
||
function markTargetLoaded(event) { | ||
event.target.__loaded = true; | ||
} | ||
|
||
} | ||
|
||
// Fire the 'HTMLImportsLoaded' event when imports in document at load time | ||
// have loaded. This event is required to simulate the script blocking | ||
// behavior of native imports. A main document script that needs to be sure | ||
// imports have loaded should wait for this event. | ||
whenImportsReady(function() { | ||
HTMLImports.ready = true; | ||
HTMLImports.readyTime = new Date().getTime(); | ||
mainDoc.dispatchEvent( | ||
new CustomEvent('HTMLImportsLoaded', {bubbles: true}) | ||
); | ||
}); | ||
|
||
// exports | ||
scope.useNative = useNative; | ||
scope.isImportLoaded = isImportLoaded; | ||
scope.whenReady = whenImportsReady; | ||
|
||
// deprecated | ||
scope.whenImportsReady = whenImportsReady; | ||
|
||
})(window.HTMLImports); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters