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