|
1 | | -<!-- |
2 | | -@license |
3 | | -Copyright (c) 2016 The Polymer Project Authors. All rights reserved. |
4 | | -This code may only be used under the BSD style license found at http://polymer.github.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/CONTRIBUTORS.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/PATENTS.txt |
9 | | ---> |
10 | | - |
11 | 1 | <link rel="import" href="../bower_components/polymer/polymer-element.html"> |
| 2 | +<link rel="import" href="../bower_components/polymerfire/polymerfire.html"> |
| 3 | +<link rel="import" href="../bower_components/polymerfire/firebase-query.html"> |
| 4 | +<link rel="import" href="../bower_components/app-storage/app-indexeddb-mirror/app-indexeddb-mirror.html"> |
12 | 5 | <link rel="import" href="shared-styles.html"> |
13 | 6 |
|
14 | 7 | <dom-module id="stats-page"> |
|
21 | 14 | } |
22 | 15 | </style> |
23 | 16 |
|
| 17 | + <!--<firebase-document |
| 18 | + id="doc2" |
| 19 | + app-name="puppyscheduler" |
| 20 | + path="/events" |
| 21 | + data="{{events}}"> |
| 22 | + </firebase-document> --> |
| 23 | + <firebase-query |
| 24 | + id="query" |
| 25 | + app-name="puppyscheduler" |
| 26 | + path="/events" |
| 27 | + data="{{events}}" |
| 28 | + limit-to-last=[[queryLimit]] |
| 29 | + </firebase-query> |
| 30 | + |
| 31 | + <!--<app-indexeddb-mirror |
| 32 | + session="1" |
| 33 | + key="events" |
| 34 | + data="{{events}}" |
| 35 | + persisted-data="{{persistedEvents}}"> |
| 36 | + </app-indexeddb-mirror>--> |
| 37 | + |
24 | 38 | <div class="card"> |
25 | | - <div class="circle">2</div> |
26 | | - <h1>View Two</h1> |
27 | | - <p>Ea duis bonorum nec, falli paulo aliquid ei eum.</p> |
28 | | - <p>Id nam odio natum malorum, tibique copiosae expetenda mel ea.Detracto suavitate repudiandae no eum. Id adhuc minim soluta nam.Id nam odio natum malorum, tibique copiosae expetenda mel ea.</p> |
| 39 | + Last time Peed: [[lastTimePeed]] |
29 | 40 | </div> |
30 | 41 | </template> |
31 | 42 |
|
32 | 43 | <script> |
33 | 44 | class StatsPage extends Polymer.Element { |
34 | 45 | static get is() { return 'stats-page'; } |
35 | | - } |
| 46 | + |
| 47 | + static get observers() { |
| 48 | + return [ |
| 49 | + 'dataChanged(events.splices)' |
| 50 | + ] |
| 51 | + } |
| 52 | + |
| 53 | + static get properties() { |
| 54 | + return { |
| 55 | + events: Object, |
| 56 | + lastTimePeed: String, |
| 57 | + queryLimit: { |
| 58 | + type: Number, |
| 59 | + value: 10 |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + dataChanged(changeSet) { |
| 65 | + var self = this; |
| 66 | + if(changeSet) { |
| 67 | + changeSet.indexSplices.forEach(function(s) { |
| 68 | + if(s.object.length < self.queryLimit) { |
| 69 | + return; |
| 70 | + } else { |
| 71 | + self.lastTimePeed = self._findLastEvent("peed", s.object); |
| 72 | + } |
| 73 | + }); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + _findLastEvent(eventName, eventList) { |
| 78 | + var self = this; |
| 79 | + for(var i = self.queryLimit - 1; i > 0; i--) { |
| 80 | + if(eventList[i].action === eventName) { |
| 81 | + return self._formatDateReadable(eventList[i].$key); |
| 82 | + } |
| 83 | + } |
| 84 | + console.log('error'); |
| 85 | + return ''; |
| 86 | + } |
| 87 | + |
| 88 | + _formatDateReadable(ts) { |
| 89 | + // how long ago in ms |
| 90 | + var diff = Date.now() - ts; |
| 91 | + // convert to a readable time |
| 92 | + var secs = Math.floor(diff / 1000); |
| 93 | + var mins = Math.floor(secs / 60 ); |
| 94 | + var hours = Math.floor(mins / 60); |
| 95 | + |
| 96 | + if(hours >= 1) { |
| 97 | + return hours + ' hours ago'; |
| 98 | + } else if (mins >= 1){ |
| 99 | + return mins + ' minutes ago'; |
| 100 | + } else { |
| 101 | + return secs + ' seconds ago'; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
36 | 106 |
|
37 | 107 | window.customElements.define(StatsPage.is, StatsPage); |
38 | 108 | </script> |
|
0 commit comments