From 85c94a8fc7fed41f550a49b9d197926fc4d7e01e Mon Sep 17 00:00:00 2001 From: EGOIST <0x142857@gmail.com> Date: Wed, 20 Mar 2019 01:17:13 +0800 Subject: [PATCH] feat: allow to change locale globally, closes #95 --- .prettierrc | 4 +++ example/app.vue | 16 +++++------ example/index.js | 11 ++++---- src/index.js | 72 ++++++++++++++++++++++++------------------------ 4 files changed, 54 insertions(+), 49 deletions(-) create mode 100644 .prettierrc diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..b2095be --- /dev/null +++ b/.prettierrc @@ -0,0 +1,4 @@ +{ + "semi": false, + "singleQuote": true +} diff --git a/example/app.vue b/example/app.vue index fb943c6..3c20b5b 100644 --- a/example/app.vue +++ b/example/app.vue @@ -2,20 +2,20 @@
- +
- Auto Update in every second + + Auto Update in every second
diff --git a/example/index.js b/example/index.js index 7b9798c..b65e9a6 100644 --- a/example/index.js +++ b/example/index.js @@ -18,9 +18,10 @@ Vue.use(Timeago, { new Vue({ el: '#app', - render: h => h(App, { - props: { - localeNames: Object.keys(locales) - } - }) + render: h => + h(App, { + props: { + localeNames: Object.keys(locales) + } + }) }) diff --git a/src/index.js b/src/index.js index 4aab78a..ebc8496 100644 --- a/src/index.js +++ b/src/index.js @@ -1,8 +1,8 @@ -import defaultConverter from "./converter"; +import defaultConverter from './converter' export const createTimeago = (opts = {}) => { - const locales = opts.locales || {}; - const name = opts.name || "Timeago"; + const locales = opts.locales || {} + const name = opts.name || 'Timeago' return { name, @@ -31,31 +31,31 @@ export const createTimeago = (opts = {}) => { data() { return { timeago: this.getTimeago() - }; + } }, computed: { localeName() { - return this.locale || this.$timeago.locale; + return this.locale || this.$timeago.locale } }, mounted() { - this.startUpdater(); + this.startUpdater() }, beforeDestroy() { - this.stopUpdater(); + this.stopUpdater() }, render(h) { return h( - "time", + 'time', { attrs: { datetime: new Date(this.datetime), title: - typeof this.title === "string" + typeof this.title === 'string' ? this.title : this.title === false ? null @@ -63,87 +63,87 @@ export const createTimeago = (opts = {}) => { } }, [this.timeago] - ); + ) }, methods: { getTimeago(datetime) { - const converter = this.converter || opts.converter || defaultConverter; + const converter = this.converter || opts.converter || defaultConverter return converter( datetime || this.datetime, locales[this.localeName], this.converterOptions || {} - ); + ) }, convert(datetime) { - this.timeago = this.getTimeago(datetime); + this.timeago = this.getTimeago(datetime) }, startUpdater() { if (this.autoUpdate) { - const autoUpdaye = this.autoUpdate === true ? 60 : this.autoUpdate; + const autoUpdaye = this.autoUpdate === true ? 60 : this.autoUpdate this.updater = setInterval(() => { - this.convert(); - }, autoUpdaye * 1000); + this.convert() + }, autoUpdaye * 1000) } }, stopUpdater() { if (this.updater) { - clearInterval(this.updater); - this.updater = null; + clearInterval(this.updater) + this.updater = null } } }, watch: { autoUpdate(newValue) { - this.stopUpdater(); + this.stopUpdater() if (newValue) { - this.startUpdater(); + this.startUpdater() } }, datetime() { - this.convert(); + this.convert() }, localeName() { - this.convert(); + this.convert() }, converter() { - this.convert(); + this.convert() }, converterOptions: { handler() { - this.convert(); + this.convert() }, deep: true } } - }; -}; + } +} export const install = (Vue, opts) => { if (Vue.prototype.$timeago) { - return; + return } - if (process.env.NODE_ENV === "development" && !Vue.observable) { - console.warn(`[vue-timeago] Vue 2.6 or above is recommended.`); + if (process.env.NODE_ENV === 'development' && !Vue.observable) { + console.warn(`[vue-timeago] Vue 2.6 or above is recommended.`) } const $timeago = { locale: opts.locale - }; + } Vue.prototype.$timeago = Vue.observable ? Vue.observable($timeago) - : new Vue({ data: $timeago }); + : new Vue({ data: $timeago }) - const Component = createTimeago(opts); - Vue.component(Component.name, Component); -}; + const Component = createTimeago(opts) + Vue.component(Component.name, Component) +} -export const converter = defaultConverter; +export const converter = defaultConverter -export default install; +export default install