| OLD | NEW |
| (Empty) | |
| 1 <!-- |
| 2 @license |
| 3 Copyright (c) 2017 The Polymer Project Authors. All rights reserved. |
| 4 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt |
| 5 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt |
| 6 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt |
| 7 Code distributed by Google as part of the polymer project is also |
| 8 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt |
| 9 --> |
| 10 |
| 11 <link rel="import" href="boot.html"> |
| 12 |
| 13 <script> |
| 14 |
| 15 (function() { |
| 16 |
| 17 'use strict'; |
| 18 |
| 19 // run a callback when HTMLImports are ready or immediately if |
| 20 // this api is not available. |
| 21 function whenImportsReady(cb) { |
| 22 if (window.HTMLImports) { |
| 23 HTMLImports.whenReady(cb); |
| 24 } else { |
| 25 cb(); |
| 26 } |
| 27 } |
| 28 |
| 29 /** |
| 30 * Convenience method for importing an HTML document imperatively. |
| 31 * |
| 32 * This method creates a new `<link rel="import">` element with |
| 33 * the provided URL and appends it to the document to start loading. |
| 34 * In the `onload` callback, the `import` property of the `link` |
| 35 * element will contain the imported document contents. |
| 36 * |
| 37 * @memberof Polymer |
| 38 * @param {string} href URL to document to load. |
| 39 * @param {Function=} onload Callback to notify when an import successfully |
| 40 * loaded. |
| 41 * @param {Function=} onerror Callback to notify when an import |
| 42 * unsuccessfully loaded. |
| 43 * @param {boolean=} optAsync True if the import should be loaded `async`. |
| 44 * Defaults to `false`. |
| 45 * @return {HTMLLinkElement} The link element for the URL to be loaded. |
| 46 */ |
| 47 Polymer.importHref = function(href, onload, onerror, optAsync) { |
| 48 let link = |
| 49 document.head.querySelector('link[href="' + href + '"][import-href]'); |
| 50 if (!link) { |
| 51 link = document.createElement('link'); |
| 52 link.rel = 'import'; |
| 53 link.href = href; |
| 54 link.setAttribute('import-href', ''); |
| 55 } |
| 56 // always ensure link has `async` attribute if user specified one, |
| 57 // even if it was previously not async. This is considered less confusing. |
| 58 if (optAsync) { |
| 59 link.setAttribute('async', ''); |
| 60 } |
| 61 // NOTE: the link may now be in 3 states: (1) pending insertion, |
| 62 // (2) inflight, (3) already laoded. In each case, we need to add |
| 63 // event listeners to process callbacks. |
| 64 let cleanup = function() { |
| 65 link.removeEventListener('load', loadListener); |
| 66 link.removeEventListener('error', errorListener); |
| 67 } |
| 68 let loadListener = function(event) { |
| 69 cleanup(); |
| 70 // In case of a successful load, cache the load event on the link so |
| 71 // that it can be used to short-circuit this method in the future when |
| 72 // it is called with the same href param. |
| 73 link.__dynamicImportLoaded = true; |
| 74 if (onload) { |
| 75 whenImportsReady(() => { |
| 76 onload(event); |
| 77 }); |
| 78 } |
| 79 }; |
| 80 let errorListener = function(event) { |
| 81 cleanup(); |
| 82 // In case of an error, remove the link from the document so that it |
| 83 // will be automatically created again the next time `importHref` is |
| 84 // called. |
| 85 if (link.parentNode) { |
| 86 link.parentNode.removeChild(link); |
| 87 } |
| 88 if (onerror) { |
| 89 whenImportsReady(() => { |
| 90 onerror(event); |
| 91 }); |
| 92 } |
| 93 }; |
| 94 link.addEventListener('load', loadListener); |
| 95 link.addEventListener('error', errorListener); |
| 96 if (link.parentNode == null) { |
| 97 document.head.appendChild(link); |
| 98 // if the link already loaded, dispatch a fake load event |
| 99 // so that listeners are called and get a proper event argument. |
| 100 } else if (link.__dynamicImportLoaded) { |
| 101 link.dispatchEvent(new Event('load')); |
| 102 } |
| 103 return link; |
| 104 }; |
| 105 |
| 106 })(); |
| 107 </script> |
| OLD | NEW |