| 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 <link rel="import" href="../utils/boot.html"> |
| 11 <link rel="import" href="../utils/resolve-url.html"> |
| 12 <script> |
| 13 (function() { |
| 14 'use strict'; |
| 15 |
| 16 let modules = {}; |
| 17 let lcModules = {}; |
| 18 function findModule(id) { |
| 19 return modules[id] || lcModules[id.toLowerCase()]; |
| 20 } |
| 21 |
| 22 function styleOutsideTemplateCheck(inst) { |
| 23 if (inst.querySelector('style')) { |
| 24 console.warn('dom-module %s has style outside template', inst.id); |
| 25 } |
| 26 } |
| 27 |
| 28 /** |
| 29 * The `dom-module` element registers the dom it contains to the name given |
| 30 * by the module's id attribute. It provides a unified database of dom |
| 31 * accessible via its static `import` API. |
| 32 * |
| 33 * A key use case of `dom-module` is for providing custom element `<template>`
s |
| 34 * via HTML imports that are parsed by the native HTML parser, that can be |
| 35 * relocated during a bundling pass and still looked up by `id`. |
| 36 * |
| 37 * Example: |
| 38 * |
| 39 * <dom-module id="foo"> |
| 40 * <img src="stuff.png"> |
| 41 * </dom-module> |
| 42 * |
| 43 * Then in code in some other location that cannot access the dom-module above |
| 44 * |
| 45 * let img = document.createElement('dom-module').import('foo', 'img'); |
| 46 * |
| 47 * @extends HTMLElement |
| 48 * @memberof Polymer |
| 49 * @summary Custom element that provides a registry of relocatable DOM content |
| 50 * by `id` that is agnostic to bundling. |
| 51 */ |
| 52 class DomModule extends HTMLElement { |
| 53 |
| 54 static get observedAttributes() { return ['id'] } |
| 55 |
| 56 /** |
| 57 * Retrieves the element specified by the css `selector` in the module |
| 58 * registered by `id`. For example, this.import('foo', 'img'); |
| 59 * @param {string} id The id of the dom-module in which to search. |
| 60 * @param {string=} selector The css selector by which to find the element. |
| 61 * @return {Element} Returns the element which matches `selector` in the |
| 62 * module registered at the specified `id`. |
| 63 */ |
| 64 static import(id, selector) { |
| 65 if (id) { |
| 66 let m = findModule(id); |
| 67 if (m && selector) { |
| 68 return m.querySelector(selector); |
| 69 } |
| 70 return m; |
| 71 } |
| 72 return null; |
| 73 } |
| 74 |
| 75 attributeChangedCallback(name, old, value) { |
| 76 if (old !== value) { |
| 77 this.register(); |
| 78 } |
| 79 } |
| 80 |
| 81 /** |
| 82 * The absolute URL of the original location of this `dom-module`. |
| 83 * |
| 84 * This value will differ from this element's `ownerDocument` in the |
| 85 * following ways: |
| 86 * - Takes into account any `assetpath` attribute added during bundling |
| 87 * to indicate the original location relative to the bundled location |
| 88 * - Uses the HTMLImports polyfill's `importForElement` API to ensure |
| 89 * the path is relative to the import document's location since |
| 90 * `ownerDocument` is not currently polyfilled |
| 91 */ |
| 92 get assetpath() { |
| 93 // Don't override existing assetpath. |
| 94 if (!this.__assetpath) { |
| 95 // note: assetpath set via an attribute must be relative to this |
| 96 // element's location; accomodate polyfilled HTMLImports |
| 97 const owner = window.HTMLImports && HTMLImports.importForElement ? |
| 98 HTMLImports.importForElement(this) || document : this.ownerDocument; |
| 99 const url = Polymer.ResolveUrl.resolveUrl( |
| 100 this.getAttribute('assetpath') || '', owner.baseURI); |
| 101 this.__assetpath = Polymer.ResolveUrl.pathFromUrl(url); |
| 102 } |
| 103 return this.__assetpath; |
| 104 } |
| 105 |
| 106 /** |
| 107 * Registers the dom-module at a given id. This method should only be called |
| 108 * when a dom-module is imperatively created. For |
| 109 * example, `document.createElement('dom-module').register('foo')`. |
| 110 * @param {string=} id The id at which to register the dom-module. |
| 111 */ |
| 112 register(id) { |
| 113 id = id || this.id; |
| 114 if (id) { |
| 115 this.id = id; |
| 116 // store id separate from lowercased id so that |
| 117 // in all cases mixedCase id will stored distinctly |
| 118 // and lowercase version is a fallback |
| 119 modules[id] = this; |
| 120 lcModules[id.toLowerCase()] = this; |
| 121 styleOutsideTemplateCheck(this); |
| 122 } |
| 123 } |
| 124 } |
| 125 |
| 126 DomModule.prototype['modules'] = modules; |
| 127 |
| 128 customElements.define('dom-module', DomModule); |
| 129 |
| 130 // export |
| 131 Polymer.DomModule = DomModule; |
| 132 |
| 133 })(); |
| 134 </script> |
| OLD | NEW |