|
| 1 | +this.Polymer = this.Polymer || {}; |
| 2 | +this.Polymer.decorators = (function (exports) { |
| 3 | +'use strict'; |
| 4 | + |
| 5 | +/** |
| 6 | + * @license |
| 7 | + * Copyright (c) 2016 The Polymer Project Authors. All rights reserved. |
| 8 | + * This code may only be used under the BSD style license found at |
| 9 | + * http://polymer.github.io/LICENSE.txt The complete set of authors may be found |
| 10 | + * at http://polymer.github.io/AUTHORS.txt The complete set of contributors may |
| 11 | + * be found at http://polymer.github.io/CONTRIBUTORS.txt Code distributed by |
| 12 | + * Google as part of the polymer project is also subject to an additional IP |
| 13 | + * rights grant found at http://polymer.github.io/PATENTS.txt |
| 14 | + */ |
| 15 | +/// <reference types="reflect-metadata" /> |
| 16 | +/** |
| 17 | + * A TypeScript class decorator factory that defines a custom element with name |
| 18 | + * `tagname` and the decorated class. If `tagname` is not provided, the static |
| 19 | + * `is` property of the class is used. |
| 20 | + */ |
| 21 | +function customElement(tagname) { |
| 22 | + // TODO Investigate narrowing down the type of clazz. |
| 23 | + return (clazz) => { |
| 24 | + // TODO(justinfagnani): we could also use the kebab-cased class name |
| 25 | + if (clazz.is) { |
| 26 | + tagname = clazz.is; |
| 27 | + } |
| 28 | + else { |
| 29 | + clazz.is = tagname; |
| 30 | + } |
| 31 | + window.customElements.define(tagname, clazz); |
| 32 | + }; |
| 33 | +} |
| 34 | +function createProperty(proto, name, options) { |
| 35 | + const notify = options && options.notify || false; |
| 36 | + const reflectToAttribute = options && options.reflectToAttribute || false; |
| 37 | + const readOnly = options && options.readOnly || false; |
| 38 | + const computed = options && options.computed || ''; |
| 39 | + const observer = options && options.observer || ''; |
| 40 | + let type; |
| 41 | + if (options && options.hasOwnProperty('type')) { |
| 42 | + type = options.type; |
| 43 | + } |
| 44 | + else if (window.Reflect && Reflect.hasMetadata && Reflect.getMetadata && |
| 45 | + Reflect.hasMetadata('design:type', proto, name)) { |
| 46 | + type = Reflect.getMetadata('design:type', proto, name); |
| 47 | + } |
| 48 | + else { |
| 49 | + console.error('A type could not be found for ${propName}. ' + |
| 50 | + 'Set a type or configure Metadata Reflection API support.'); |
| 51 | + } |
| 52 | + if (!proto.constructor.hasOwnProperty('properties')) { |
| 53 | + Object.defineProperty(proto.constructor, 'properties', { value: {} }); |
| 54 | + } |
| 55 | + const finalOpts = { type, notify, reflectToAttribute, readOnly, computed, observer }; |
| 56 | + proto.constructor.properties[name] = finalOpts; |
| 57 | +} |
| 58 | +/** |
| 59 | + * A TypeScript property decorator factory that defines this as a Polymer |
| 60 | + * property. |
| 61 | + * |
| 62 | + * This function must be invoked to return a decorator. |
| 63 | + */ |
| 64 | +function property(options) { |
| 65 | + return (proto, propName) => { |
| 66 | + createProperty(proto, propName, options); |
| 67 | + }; |
| 68 | +} |
| 69 | +/** |
| 70 | + * A TypeScript property decorator factory that causes the decorated method to |
| 71 | + * be called when a property changes. |
| 72 | + * |
| 73 | + * This function must be invoked to return a decorator. |
| 74 | + */ |
| 75 | +function observe(...targets) { |
| 76 | + return (proto, propName) => { |
| 77 | + if (!proto.constructor.hasOwnProperty('observers')) { |
| 78 | + proto.constructor.observers = []; |
| 79 | + } |
| 80 | + proto.constructor.observers.push(`${propName}(${targets.join(',')})`); |
| 81 | + }; |
| 82 | +} |
| 83 | +/** |
| 84 | + * A TypeScript accessor decorator factory that causes the decorated getter to |
| 85 | + * be called when a set of dependencies change. The arguments of this decorator |
| 86 | + * should be paths of the data dependencies as described |
| 87 | + * [here](https://www.polymer-project.org/2.0/docs/devguide/observers#define-a-computed-property) |
| 88 | + * The decorated getter should not have an associated setter. |
| 89 | + * |
| 90 | + * This function must be invoked to return a decorator. |
| 91 | + */ |
| 92 | +function computed(...targets) { |
| 93 | + return (proto, propName, descriptor) => { |
| 94 | + const fnName = `__compute${propName}`; |
| 95 | + Object.defineProperty(proto, fnName, { value: descriptor.get }); |
| 96 | + descriptor.get = undefined; |
| 97 | + createProperty(proto, propName, { computed: `${fnName}(${targets.join(',')})` }); |
| 98 | + }; |
| 99 | +} |
| 100 | +/** |
| 101 | + * A TypeScript property decorator factory that converts a class property into |
| 102 | + * a getter that executes a querySelector on the element's shadow root. |
| 103 | + * |
| 104 | + * By annotating the property with the correct type, element's can have |
| 105 | + * type-checked access to internal elements. |
| 106 | + * |
| 107 | + * This function must be invoked to return a decorator. |
| 108 | + */ |
| 109 | +const query = _query((target, selector) => target.querySelector(selector)); |
| 110 | +/** |
| 111 | + * A TypeScript property decorator factory that converts a class property into |
| 112 | + * a getter that executes a querySelectorAll on the element's shadow root. |
| 113 | + * |
| 114 | + * By annotating the property with the correct type, element's can have |
| 115 | + * type-checked access to internal elements. The type should be NodeList |
| 116 | + * with the correct type argument. |
| 117 | + * |
| 118 | + * This function must be invoked to return a decorator. |
| 119 | + */ |
| 120 | +const queryAll = _query((target, selector) => target.querySelectorAll(selector)); |
| 121 | +/** |
| 122 | + * Creates a decorator function that accepts a selector, and replaces a |
| 123 | + * property with a getter than executes the selector with the given queryFn |
| 124 | + * |
| 125 | + * @param queryFn A function that executes a query with a selector |
| 126 | + */ |
| 127 | +function _query(queryFn) { |
| 128 | + return (selector) => (proto, propName) => { |
| 129 | + Object.defineProperty(proto, propName, { |
| 130 | + get() { |
| 131 | + return queryFn(this.shadowRoot, selector); |
| 132 | + }, |
| 133 | + enumerable: true, |
| 134 | + configurable: true, |
| 135 | + }); |
| 136 | + }; |
| 137 | +} |
| 138 | +/** |
| 139 | + * A TypeScript property decorator factory that causes the decorated method to |
| 140 | + * be called when a imperative event is fired on the targeted element. `target` |
| 141 | + * can be either a single element by id or element. |
| 142 | + * |
| 143 | + * You must apply the supplied DeclarativeEventListeners mixin to your element |
| 144 | + * class for this decorator to function. |
| 145 | + * |
| 146 | + * https://www.polymer-project.org/2.0/docs/devguide/events#imperative-listeners |
| 147 | + * |
| 148 | + * @param eventName A string representing the event type to listen for |
| 149 | + * @param target A single element by id or EventTarget to target |
| 150 | + */ |
| 151 | +const listen = (eventName, target) => (proto, methodName) => { |
| 152 | + proto.constructor._addDeclarativeEventListener(target, eventName, proto[methodName]); |
| 153 | +}; |
| 154 | + |
| 155 | +exports.customElement = customElement; |
| 156 | +exports.property = property; |
| 157 | +exports.observe = observe; |
| 158 | +exports.computed = computed; |
| 159 | +exports.query = query; |
| 160 | +exports.queryAll = queryAll; |
| 161 | +exports.listen = listen; |
| 162 | + |
| 163 | +return exports; |
| 164 | + |
| 165 | +}({})); |
0 commit comments