UNPKG

3.53 kBJavaScriptView Raw
1import toNow from 'date-fns/distance_in_words_to_now';
2
3var defaultConverter = (function (date, locale, converterOptions) {
4 var includeSeconds = converterOptions.includeSeconds;
5 var addSuffix = converterOptions.addSuffix; if ( addSuffix === void 0 ) addSuffix = true;
6 return toNow(date, {
7 locale: locale,
8 includeSeconds: includeSeconds,
9 addSuffix: addSuffix
10 });
11});
12
13var createTimeago = function (opts) {
14 if ( opts === void 0 ) opts = {};
15
16 var locales = opts.locales || {};
17 var name = opts.name || 'Timeago';
18 return {
19 name: name,
20 props: {
21 datetime: {
22 required: true
23 },
24 title: {
25 type: [String, Boolean]
26 },
27 locale: {
28 type: String
29 },
30 autoUpdate: {
31 type: [Number, Boolean]
32 },
33 converter: {
34 type: Function
35 },
36 converterOptions: {
37 type: Object
38 }
39 },
40
41 data: function data() {
42 return {
43 timeago: this.getTimeago()
44 };
45 },
46
47 computed: {
48 localeName: function localeName() {
49 return this.locale || this.$timeago.locale;
50 }
51
52 },
53
54 mounted: function mounted() {
55 this.startUpdater();
56 },
57
58 beforeDestroy: function beforeDestroy() {
59 this.stopUpdater();
60 },
61
62 render: function render(h) {
63 return h('time', {
64 attrs: {
65 datetime: new Date(this.datetime).toISOString(),
66 title: typeof this.title === 'string' ? this.title : this.title === false ? null : this.timeago
67 }
68 }, [this.timeago]);
69 },
70
71 methods: {
72 getTimeago: function getTimeago(datetime) {
73 var converter = this.converter || opts.converter || defaultConverter;
74 return converter(datetime || this.datetime, locales[this.locale || this.$timeago.locale], this.converterOptions || {});
75 },
76
77 convert: function convert(datetime) {
78 this.timeago = this.getTimeago(datetime);
79 },
80
81 startUpdater: function startUpdater() {
82 var this$1 = this;
83
84 if (this.autoUpdate) {
85 var autoUpdaye = this.autoUpdate === true ? 60 : this.autoUpdate;
86 this.updater = setInterval(function () {
87 this$1.convert();
88 }, autoUpdaye * 1000);
89 }
90 },
91
92 stopUpdater: function stopUpdater() {
93 if (this.updater) {
94 clearInterval(this.updater);
95 this.updater = null;
96 }
97 }
98
99 },
100 watch: {
101 autoUpdate: function autoUpdate(newValue) {
102 this.stopUpdater();
103
104 if (newValue) {
105 this.startUpdater();
106 }
107 },
108
109 datetime: function datetime() {
110 this.convert();
111 },
112
113 localeName: function localeName() {
114 this.convert();
115 },
116
117 converter: function converter() {
118 this.convert();
119 },
120
121 converterOptions: {
122 handler: function handler() {
123 this.convert();
124 },
125
126 deep: true
127 }
128 }
129 };
130};
131var install = function (Vue, opts) {
132 if (Vue.prototype.$timeago) {
133 return;
134 }
135
136 if (process.env.NODE_ENV === 'development' && !Vue.observable) {
137 console.warn("[vue-timeago] Vue 2.6 or above is recommended.");
138 }
139
140 var $timeago = {
141 locale: opts.locale
142 };
143 Vue.prototype.$timeago = Vue.observable ? Vue.observable($timeago) : new Vue({
144 data: $timeago
145 });
146 var Component = createTimeago(opts);
147 Vue.component(Component.name, Component);
148};
149var converter = defaultConverter;
150
151export default install;
152export { createTimeago, install, converter };