-
Notifications
You must be signed in to change notification settings - Fork 0
/
moment-between.html
121 lines (109 loc) · 3.08 KB
/
moment-between.html
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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="moment-imports.html">
<!--
An element allowing to easily display durations.
Examples:
<moment-between
from="2016/01/18 17:14:00"
from-format="YYYY/MM/DD HH:mm:ss"
to="2016/01/21 11:14:00"
to-format="YYYY/MM/DD HH:mm:ss">
</moment-between>
@demo demo/index.html
-->
<dom-module id="moment-between">
<template>
<style>
:host {
display: block;
box-sizing: border-box;
}
</style>
<span id="result"></span>
</template>
<script>
(function() {
Polymer({
is: 'moment-between',
properties: {
/**
* Starting point of the computation.
*/
from: {
type: Object
},
/**
* A format describing the `from` property.
*/
fromFormat: {
type: String,
value: 'YYYY/MM/DD HH:mm:ss'
},
startPoint: {
type: Object,
computed: '_computePoint(from, fromFormat)'
},
/**
* Starting point of the computation.
*/
to: {
type: Object
},
/**
* A format describing the `to` property.
*/
toFormat: {
type: String,
value: 'YYYY/MM/DD HH:mm:ss'
},
endPoint: {
type: Object,
computed: '_computePoint(to, toFormat)'
},
/**
* The locale to be used.
*/
locale: {
type: String,
value: 'en-ie'
},
/**
* The final output string based on all input properties.
*/
formatted: {
type: String,
computed: '_computeFormatted(locale, startPoint, endPoint)'
}
},
observers: [ '_render(formatted)' ],
_computePoint: function(value, format) {
if ((value instanceof Date) || (typeof value === 'number'))
return moment(value);
else
return moment(value, format);
},
/**
* In charge of computing the final `formatted` property, whenever the
* `from`, `fromFormat`, `to` or `toFormat` change.
*/
_computeFormatted: function(locale, startPoint, endPoint) {
return moment
.duration(startPoint.diff(endPoint))
.locale(locale)
.humanize();
},
/**
* Whenever the `formatted` property changes, `_render` is in charge of
* refreshing displayed DOM text.
*/
_render: function(formatted) {
this.$.result.innerText = formatted;
},
ready: function() {
this.from = this.from || Date.now();
this.to = this.to || Date.now();
}
});
})();
</script>
</dom-module>