-
Notifications
You must be signed in to change notification settings - Fork 20
/
iron-meta.js
208 lines (168 loc) · 4.84 KB
/
iron-meta.js
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
/**
@license
Copyright (c) 2015 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 '@polymer/polymer/polymer-legacy.js';
import {Polymer} from '@polymer/polymer/lib/legacy/polymer-fn.js';
export class IronMeta {
/**
* @param {{
* type: (string|null|undefined),
* key: (string|null|undefined),
* value: *,
* }=} options
*/
constructor(options) {
IronMeta[' '](options);
/** @type {string} */
this.type = (options && options.type) || 'default';
/** @type {string|null|undefined} */
this.key = options && options.key;
if (options && 'value' in options) {
/** @type {*} */
this.value = options.value;
}
}
/** @return {*} */
get value() {
var type = this.type;
var key = this.key;
if (type && key) {
return IronMeta.types[type] && IronMeta.types[type][key];
}
}
/** @param {*} value */
set value(value) {
var type = this.type;
var key = this.key;
if (type && key) {
type = IronMeta.types[type] = IronMeta.types[type] || {};
if (value == null) {
delete type[key];
} else {
type[key] = value;
}
}
}
/** @return {!Array<*>} */
get list() {
if (!this.type) {
return [];
}
var items = IronMeta.types[this.type];
if (!items) {
return [];
}
return Object.keys(items).map(function(key) {
return metaDatas[this.type][key];
}, this);
}
/**
* @param {string} key
* @return {*}
*/
byKey(key) {
this.key = key;
return this.value;
}
};
// This function is used to convince Closure not to remove constructor calls
// for instances that are not held anywhere. For example, when
// `new IronMeta({...})` is used only for the side effect of adding a value.
IronMeta[' '] = function() {};
IronMeta.types = {};
var metaDatas = IronMeta.types;
/**
`iron-meta` is a generic element you can use for sharing information across the
DOM tree. It uses [monostate pattern](http://c2.com/cgi/wiki?MonostatePattern)
such that any instance of iron-meta has access to the shared information. You
can use `iron-meta` to share whatever you want (or create an extension [like
x-meta] for enhancements).
The `iron-meta` instances containing your actual data can be loaded in an
import, or constructed in any way you see fit. The only requirement is that you
create them before you try to access them.
Examples:
If I create an instance like this:
<iron-meta key="info" value="foo/bar"></iron-meta>
Note that value="foo/bar" is the metadata I've defined. I could define more
attributes or use child nodes to define additional metadata.
Now I can access that element (and it's metadata) from any iron-meta instance
via the byKey method, e.g.
meta.byKey('info');
Pure imperative form would be like:
document.createElement('iron-meta').byKey('info');
Or, in a Polymer element, you can include a meta in your template:
<iron-meta id="meta"></iron-meta>
...
this.$.meta.byKey('info');
*/
Polymer({
is: 'iron-meta',
/** @override */
_template: null,
properties: {
/**
* The type of meta-data. All meta-data of the same type is stored
* together.
* @type {string}
*/
type: {
type: String,
value: 'default',
},
/**
* The key used to store `value` under the `type` namespace.
* @type {?string}
*/
key: {
type: String,
},
/**
* The meta-data to store or retrieve.
* @type {*}
*/
value: {
type: String,
notify: true,
},
/**
* If true, `value` is set to the iron-meta instance itself.
*/
self: {type: Boolean, observer: '_selfChanged'},
__meta: {type: Boolean, computed: '__computeMeta(type, key, value)'}
},
hostAttributes: {hidden: true},
__computeMeta: function(type, key, value) {
var meta = new IronMeta({type: type, key: key});
if (value !== undefined && value !== meta.value) {
meta.value = value;
} else if (this.value !== meta.value) {
this.value = meta.value;
}
return meta;
},
get list() {
return this.__meta && this.__meta.list;
},
_selfChanged: function(self) {
if (self) {
this.value = this;
}
},
/**
* Retrieves meta data value by key.
*
* @method byKey
* @param {string} key The key of the meta-data to be returned.
* @return {*}
*/
byKey: function(key) {
return new IronMeta({type: this.type, key: key}).value;
}
});