forked from FirebaseExtended/polymerfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirebase-query.html
More file actions
368 lines (307 loc) · 9.71 KB
/
firebase-query.html
File metadata and controls
368 lines (307 loc) · 9.71 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<!--
@license
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file or at
https://github.com/firebase/polymerfire/blob/master/LICENSE
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="firebase-database-behavior.html">
<link rel="import" href="firebase.html">
<!--
`firebase-query` combines the given properties into query options that generate
a query, a request for a filtered, ordered, immutable set of Firebase data. The
results of this Firebase query are then synchronized into the `data` parameter.
Example usage:
```html
<firebase-query
id="query"
app-name="notes"
path="/notes/[[uid]]"
data="{{data}}">
</firebase-query>
<template is="dom-repeat" items="{{data}}" as="{{note}}">
<sticky-note note-data="{{note}}"></sticky-note>
</template>
<script>
Polymer({
properties: {
uid: String,
data: {
type: Object,
observer: 'dataChanged'
}
},
dataChanged: function (newData, oldData) {
// do something when the query returns values
}
});
</script>
```
-->
<dom-module id="firebase-query">
<script>
(function() {
'use strict';
Polymer({
is: 'firebase-query',
behaviors: [
Polymer.FirebaseDatabaseBehavior
],
properties: {
/**
* [`firebase.database.Query`](https://firebase.google.com/docs/reference/js/firebase.database.Query#property)
* object computed by the following parameters.
*/
query: {
type: Object,
computed: '__computeQuery(ref, orderByChild, limitToFirst, limitToLast, startAt, endAt, equalTo)',
observer: '__queryChanged'
},
/**
* The child key of each query result to order the query by.
*
* Changing this value generates a new `query` ordered by the
* specified child key.
*/
orderByChild: {
type: String,
value: ''
},
/**
* The value to start at in the query.
*
* Changing this value generates a new `query` with the specified
* starting point. The generated `query` includes children which match
* the specified starting point.
*/
startAt: {
type: String,
value: ''
},
/**
* The value to end at in the query.
*
* Changing this value generates a new `query` with the specified
* ending point. The generated `query` includes children which match
* the specified ending point.
*/
endAt: {
type: String,
value: ''
},
/**
* Specifies a child-key value that must be matched for each candidate result.
*
* Changing this value generates a new `query` which includes children
* which match the specified value.
*/
equalTo: {
type: Object,
value: null
},
/**
* The maximum number of nodes to include in the query.
*
* Changing this value generates a new `query` limited to the first
* number of children.
*/
limitToFirst: {
type: Number,
value: 0
},
/**
* The maximum number of nodes to include in the query.
*
* Changing this value generates a new `query` limited to the last
* number of children.
*/
limitToLast: {
type: Number,
value: 0
}
},
created: function() {
this.__map = {};
},
attached: function() {
this.__queryChanged(this.query, this.query);
},
detached: function() {
if (this.query == null) {
return;
}
this.query.off();
},
child: function(key) {
return this.__map[key];
},
get isNew() {
return !this.path;
},
get zeroValue() {
return [];
},
/**
* @override
*/
memoryPathToStoragePath: function(path) {
var storagePath = this.path;
if (path !== 'data') {
var parts = path.split('.');
var index = window.parseInt(parts[1], 10);
if (index != null && !isNaN(index)) {
parts[1] = this.data[index] != null && this.data[index].$key;
}
storagePath += parts.join('/').replace(/^data\.?/, '');
}
return storagePath;
},
/**
* @override
*/
storagePathToMemoryPath: function(storagePath) {
var path = 'data';
if (storagePath !== this.path) {
var parts = storagePath.replace(this.path + '/', '').split('/');
var key = parts[0];
var datum = this.__map[key];
if (datum) {
parts[0] = this.__indexFromKey(key);
}
path += '.' + parts.join('.');
}
return path;
},
/**
* @override
*/
setStoredValue: function(storagePath, value) {
if (storagePath === this.path || /\$key$/.test(storagePath)) {
return Promise.resolve();
} else {
return this._setFirebaseValue(storagePath, value);
}
},
_propertyToKey: function(property) {
var index = window.parseInt(property, 10);
if (index != null && !isNaN(index)) {
return this.data[index].$key;
}
},
__computeQuery: function(ref, orderByChild, limitToFirst, limitToLast, startAt, endAt, equalTo) {
if (ref == null) {
return null;
}
var query;
if (orderByChild) {
query = ref.orderByChild(orderByChild);
} else {
query = ref.orderByKey();
}
if (limitToFirst) {
query = query.limitToFirst(limitToFirst);
} else if (limitToLast) {
query = query.limitToLast(limitToLast);
}
if (startAt) {
query = query.startAt(startAt);
}
if (endAt) {
query = query.endAt(endAt);
}
if (equalTo) {
query = query.equalTo(equalTo);
}
return query;
},
__queryChanged: function(query, oldQuery) {
if (oldQuery) {
oldQuery.off();
this.syncToMemory(function() {
this.set('data', this.zeroValue);
});
}
if (query) {
query.on('child_added', this.__onFirebaseChildAdded, this);
query.on('child_removed', this.__onFirebaseChildRemoved, this);
query.on('child_changed', this.__onFirebaseChildChanged, this);
query.on('child_moved', this.__onFirebaseChildMoved, this);
}
},
__indexFromKey: function(key) {
for (var i = 0; i < this.data.length; i++) {
if (this.data[i].$key === key) {
return i;
}
}
return -1;
},
__onFirebaseChildAdded: function(snapshot) {
var value = snapshot.val();
var key = snapshot.key;
this._log('Firebase child_added:', key, value);
value.$key = key;
this.__map[key] = value;
this.push('data', value);
},
__onFirebaseChildRemoved: function(snapshot) {
var key = snapshot.key;
var value = this.__map[key];
this._log('Firebase child_removed:', key, value);
if (value) {
this.__map[key] = null;
this.async(function() {
this.syncToMemory(function() {
this.splice('data', this.__indexFromKey(key), 1);
});
});
}
},
__onFirebaseChildChanged: function(snapshot) {
var key = snapshot.key;
var value = this.__map[key];
this._log('Firebase child_changed:', key, value);
if (value) {
this.async(function() {
var index = this.__indexFromKey(key);
value = snapshot.val();
value.$key = key;
this.__map[key] = value;
this.syncToMemory(function() {
// TODO(cdata): Update this as appropriate when dom-repeat
// supports custom object key indices.
if (value instanceof Object) {
for (var property in value) {
this.set(['data', index, property], value[property]);
}
} else {
this.set(['data', index], value);
}
});
});
}
},
__onFirebaseChildMoved: function(snapshot, previousChildKey) {
var key = snapshot.key;
var value = this.__map[key];
var targetIndex = previousChildKey ? this.__indexFromKey(previousChildKey) : 0;
this._log('Firebase child_moved:', key, value,
'to index', targetIndex);
if (value) {
var index = this.__indexFromKey(key);
value = snapshot.val();
value.$key = key;
this.__map[key] = value;
this.async(function() {
this.syncToMemory(function() {
this.splice('data', index, 1);
this.splice('data', targetIndex, 0, value);
});
});
}
}
});
})();
</script>
</dom-module>