/
decorators.ts
218 lines (201 loc) · 7.6 KB
/
decorators.ts
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* @license
* Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
* This code may only be used under the BSD style license found at
* http://polymer.github.io/LICENSE.txt The complete set of authors may be found
* at http://polymer.github.io/AUTHORS.txt The complete set of contributors may
* be found at http://polymer.github.io/CONTRIBUTORS.txt Code distributed by
* Google as part of the polymer project is also subject to an additional IP
* rights grant found at http://polymer.github.io/PATENTS.txt
*/
import {PolymerElement} from '@polymer/polymer/polymer-element.js';
export interface ElementConstructor extends Function {
is?: string;
properties?: {[prop: string]: PropertyOptions};
observers?: string[];
_addDeclarativeEventListener?:
(target: string|EventTarget, eventName: string,
handler: (ev: Event) => void) => void;
}
export interface ElementPrototype extends PolymerElement {
constructor: ElementConstructor;
}
/**
* A TypeScript class decorator factory that registers the class as a custom
* element.
*
* If `tagname` is provided, it will be used as the custom element name, and
* will be assigned to the class static `is` property. If `tagname` is omitted,
* the static `is` property of the class will be used instead. If neither exist,
* or if both exist but have different values (except in the case that the `is`
* property is not an own-property of the class), an exception is thrown.
*/
export function customElement(tagname?: string) {
return (class_: {new (): PolymerElement}&ElementConstructor) => {
if (tagname) {
// Only check that tag names match when `is` is our own property. It might
// be inherited from a superclass, in which case it's ok if they're
// different, and we'll override it with our own property below.
if (class_.hasOwnProperty('is')) {
if (tagname !== class_.is) {
throw new Error(
`custom element tag names do not match: ` +
`(${tagname} !== ${class_.is})`);
}
} else {
Object.defineProperty(class_, 'is', {value: tagname});
}
}
// Throws if tag name is missing or invalid.
window.customElements.define(class_.is!, class_);
};
}
/**
* Options for the @property decorator.
* See https://www.polymer-project.org/2.0/docs/devguide/properties.
*/
export interface PropertyOptions {
type?: BooleanConstructor|DateConstructor|NumberConstructor|StringConstructor|
ArrayConstructor|ObjectConstructor;
notify?: boolean;
reflectToAttribute?: boolean;
readOnly?: boolean;
computed?: string;
observer?: string|((val: {}, old: {}) => void);
}
function createProperty(
proto: ElementPrototype, name: string, options?: PropertyOptions): void {
if (!proto.constructor.hasOwnProperty('properties')) {
Object.defineProperty(proto.constructor, 'properties', {value: {}});
}
proto.constructor.properties![name] = {
...proto.constructor.properties![name],
...options,
};
}
/**
* A TypeScript property decorator factory that defines this as a Polymer
* property.
*
* This function must be invoked to return a decorator.
*
* @ExportDecoratedItems
*/
export function property(options?: PropertyOptions) {
return (proto: ElementPrototype, propName: string) => {
createProperty(proto, propName, options);
};
}
/**
* A TypeScript property decorator factory that causes the decorated method to
* be called when a property changes.
*
* This function must be invoked to return a decorator.
*
* @ExportDecoratedItems
*/
export function observe(...targets: string[]) {
return (proto: ElementPrototype, propName: string) => {
if (!proto.constructor.hasOwnProperty('observers')) {
Object.defineProperty(proto.constructor, 'observers', {value: []});
}
proto.constructor.observers!.push(`${propName}(${targets.join(',')})`);
};
}
/**
* A TypeScript accessor decorator factory that causes the decorated getter to
* be called when a set of dependencies change. The arguments of this decorator
* should be paths of the data dependencies as described
* [here](https://www.polymer-project.org/2.0/docs/devguide/observers#define-a-computed-property)
* The decorated getter should not have an associated setter.
*
* This function must be invoked to return a decorator.
*
* @ExportDecoratedItems
*/
export function computed<P extends string, El extends ElementPrototype&
{[K in P]: {} | null | undefined}>(
firstTarget: P, ...moreTargets: P[]) {
return (proto: El,
propName: string,
descriptor: PropertyDescriptor): void => {
const fnName = `__compute${propName}`;
Object.defineProperty(proto, fnName, {value: descriptor.get});
descriptor.get = undefined;
const targets = [firstTarget, ...moreTargets].join(',');
createProperty(proto, propName, {computed: `${fnName}(${targets})`});
};
}
/**
* A TypeScript property decorator factory that converts a class property into
* a getter that executes a querySelector on the element's shadow root.
*
* By annotating the property with the correct type, elements can have
* type-checked access to internal elements.
*
* This function must be invoked to return a decorator.
*/
export const query = _query(
(target: NodeSelector, selector: string) => target.querySelector(selector));
/**
* A TypeScript property decorator factory that converts a class property into
* a getter that executes a querySelectorAll on the element's shadow root.
*
* By annotating the property with the correct type, elements can have
* type-checked access to internal elements. The type should be NodeList
* with the correct type argument.
*
* This function must be invoked to return a decorator.
*/
export const queryAll = _query(
(target: NodeSelector, selector: string) =>
target.querySelectorAll(selector));
/**
* Creates a decorator function that accepts a selector, and replaces a
* property with a getter than executes the selector with the given queryFn
*
* @param queryFn A function that executes a query with a selector
*/
function _query(
queryFn: (target: NodeSelector, selector: string) =>
Element | NodeList | null) {
return (selector: string) => (proto: ElementPrototype, propName: string) => {
Object.defineProperty(proto, propName, {
get(this: HTMLElement) {
return queryFn(this.shadowRoot!, selector);
},
enumerable: true,
configurable: true,
});
};
}
export type HasEventListener<P extends string> = {
[K in P]: (e: Event) => void
};
/**
* A TypeScript property decorator factory that causes the decorated method to
* be called when a imperative event is fired on the targeted element. `target`
* can be either a single element by id or element.
*
* You must apply the supplied DeclarativeEventListeners mixin to your element
* class for this decorator to function.
*
* https://www.polymer-project.org/2.0/docs/devguide/events#imperative-listeners
*
* @param eventName A string representing the event type to listen for
* @param target A single element by id or EventTarget to target
*
* @ExportDecoratedItems
*/
export function listen(eventName: string, target: string|EventTarget) {
return <P extends string, El extends ElementPrototype&HasEventListener<P>>(
proto: El, methodName: P) => {
if (!proto.constructor._addDeclarativeEventListener) {
throw new Error(
`Cannot add listener for ${eventName} because ` +
`DeclarativeEventListeners mixin was not applied to element.`);
}
proto.constructor._addDeclarativeEventListener(
target, eventName, (proto as HasEventListener<P>)[methodName]);
};
}