forked from troxler/vue-headful
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
70 lines (59 loc) · 1.68 KB
/
index.ts
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
import Vue, { PluginObject } from 'vue';
import headful from 'headful';
const plugin: PluginObject<{ key?: string, component?: boolean }> = {
install (Vue, options) {
const key = (options && options.key) || 'headful';
Object.defineProperty(Vue.prototype, `$${key}`, { get: () => headful });
if (window && !window[key])
window[key] = headful;
if (options && options.component) {
const name = `Vue${key[0].toUpperCase() + key.substr(1)}`;
Vue.component(name, {
name,
props: [...Object.keys(headful.props), key],
created() {
if (this[key]) {
this.$watch(key, headful, { deep: true, immediate: true });
} else {
Object.keys(this.$props).forEach(p => (p !== key) && this.$watch(p, headful.props[p], { immediate: true }));
}
},
});
}
Vue.mixin({
data() {
if (!this.$options[key]) return {};
const vm = this;
const _headful = this.$options[key];
return {
get [key]() { return typeof _headful === 'function' ? _headful.bind(vm, vm)() : _headful; }
};
},
created(this: Vue) {
if (this[key]) {
this.$watch(key, headful, { deep: true, immediate: true });
}
}
})
}
};
if (window && window['Vue']) {
plugin.install(window['Vue']);
}
export default plugin;
export interface Headful {
[key: string]: any
}
declare module "vue/types/options" {
interface ComponentOptions<V extends Vue> {
headful?: Headful | {
(vm?: V): Headful
}
}
}
declare module "vue/types/vue" {
interface Vue {
$headful<T extends object>(props: T): void
headful?: Headful
}
}