-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcw-moment.html
executable file
·132 lines (107 loc) · 3.11 KB
/
cw-moment.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
122
123
124
125
126
127
128
129
130
131
132
<link rel="import" href="../polymer/polymer.html">
<script src="../momentjs/min/moment-with-langs.min.js"></script>
<!--
A Polymer wrapper for moment.js.
##### Example
<cw-moment datetime="2014-06-05T20:40:26" fromNow></cw-moment>
@element cw-moment
@homepage http://cletusw.github.io/cw-moment
-->
<polymer-element name="cw-moment">
<template>
<link rel="stylesheet" href="cw-moment.css" />
<content></content>
</template>
<script>
Polymer('cw-moment', {
publish: {
/**
* When greater than 0, specifies the time (in seconds) between
* automatic calls to the `refresh` method.
*
* When used as a boolean attribute, the defaultAutoRefreshRate is
* used.
*
* @attribute autoRefresh
* @type int or bool
*/
autoRefresh: 0,
datetime: '',
format: '',
formats: {
reflect: true,
value: []
},
fromNow: false,
lang: '',
noSuffix: false,
strict: false,
unixOffset: 0,
unixTimestamp: 0,
utc: false
},
observe: {
datetime: 'createMoment',
format: 'createMoment',
formats: 'createMoment',
fromNow: 'createMoment',
lang: 'createMoment',
noSuffix: 'createMoment',
strict: 'createMoment',
unixOffset: 'createMoment',
unixTimestamp: 'createMoment',
utc: 'createMoment'
},
/**
* Value that the autoRefresh property should be set to when its
* corresponding attribute is used as a boolean (no period specified).
*
* @property defaultAutoRefreshRate
* @type int
*/
defaultAutoRefreshRate: 60,
intervalId: 0,
moment: {},
autoRefreshChanged: function() {
if (this.intervalId) {
clearInterval(this.intervalId);
}
if (this.autoRefresh === '') {
this.autoRefresh = this.defaultAutoRefreshRate;
}
else if (this.autoRefresh > 0) {
this.intervalId = setInterval(this.refresh.bind(this), this.autoRefresh * 1000);
}
},
created: function() {
this.formats = [];
},
createMoment: function() {
if (this.unixOffset) {
this.moment = moment(this.unixOffset);
}
else if (this.unixTimestamp) {
this.moment = moment.unix(this.unixTimestamp);
}
else {
var fn = this.utc ? moment.utc : moment;
this.moment = fn(this.datetime || undefined, this.format || ((this.formats && this.formats.length) ? this.formats : undefined), this.lang || undefined, this.strict || undefined);
}
},
momentChanged: function() {
this.refresh();
},
ready: function() {
this.createMoment();
},
/**
* Updates the text content of this cw-moment.
*
* @method refresh
*/
refresh: function() {
this.innerText = (this.fromNow) ? this.moment.fromNow(this.noSuffix) : this.moment.format();
}
});
</script>
</polymer-element>