wangzhibo
2026-07-16 b57020623cf0c946706573bf102e548c3a544423
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { assign } from "lodash-es";
import { TestService } from "../test/service";
import { watch, ref, nextTick, getCurrentInstance, type Ref, inject, provide } from "vue";
 
// 获取上级
function useParent(name: string, r: Ref) {
    const d = getCurrentInstance();
 
    if (d) {
        let parent = d.proxy?.$.parent;
 
        if (parent) {
            while (parent && parent.type?.name != name && parent.type?.name != "cl-crud") {
                parent = parent?.parent;
            }
 
            if (parent) {
                if (parent.type.name == name) {
                    r.value = parent.exposed;
                }
            }
        }
    }
}
 
// 多事件
function useEvent(
    names: string[],
    { r, options, clear, isChild }: { r: any; options: any; clear?: string; isChild?: boolean }
) {
    if (!r.__ev) r.__ev = {};
 
    const d: { [key: string]: (args: any[]) => void } = {};
    const ev = r.__ev as { [key: string]: { fn: any; isChild?: boolean }[] };
 
    names.forEach((k) => {
        if (!ev[k]) ev[k] = [];
 
        if (options[k]) {
            ev[k].push({
                fn: options[k],
                isChild
            });
        }
 
        d[k] = (...args: any[]) => {
            ev[k].forEach((e) => {
                if (e.fn) {
                    e.fn(...args);
                }
            });
 
            if (clear == k) {
                for (const i in ev) {
                    ev[i] = ev[i].filter((e) => !e.isChild);
                }
            }
        };
    });
 
    return d;
}
 
// crud
export function useCrud(options?: ClCrud.Options, cb?: (app: ClCrud.Ref) => void) {
    const Crud = ref<ClCrud.Ref>();
    useParent("cl-crud", Crud);
 
    if (options) {
        // 测试模式
        if (options.service == "test") {
            options.service = new TestService();
        }
 
        provide("useCrud__options", options);
    }
 
    watch(Crud, (val) => {
        if (val) {
            if (cb) {
                cb(val);
            }
        }
    });
 
    return Crud;
}
 
// 新增、编辑
export function useUpsert<T = any>(options?: ClUpsert.Options<T>) {
    const Upsert = ref<ClUpsert.Ref>();
    useParent("cl-upsert", Upsert);
    const isChild = !!Upsert.value;
 
    if (options) {
        provide("useUpsert__options", options);
    }
 
    watch(
        Upsert,
        (val) => {
            if (val) {
                if (options) {
                    const event = useEvent(["onOpen", "onOpened", "onClosed"], {
                        r: val,
                        options,
                        clear: "onClosed",
                        isChild
                    });
 
                    assign(val.config, event);
                }
            }
        },
        {
            immediate: true
        }
    );
 
    return Upsert;
}
 
// 表格
export function useTable<T = any>(options?: ClTable.Options<T>, cb?: (table: ClTable.Ref) => void) {
    const Table = ref<ClTable.Ref<T>>();
    useParent("cl-table", Table);
 
    if (options) {
        provide("useTable__options", options);
    }
 
    watch(Table, (val) => {
        if (val) {
            if (cb) {
                cb(val);
            }
        }
    });
 
    return Table;
}
 
// 表单
export function useForm<T = any>(cb?: (app: ClForm.Ref<T>) => void) {
    const Form = ref<ClForm.Ref<T>>();
    useParent("cl-form", Form);
 
    nextTick(() => {
        if (cb && Form.value) {
            cb(Form.value);
        }
    });
 
    return Form;
}
 
// 高级搜索
export function useAdvSearch<T = any>(options?: ClAdvSearch.Options<T>) {
    const AdvSearch = ref<ClAdvSearch.Ref<T>>();
    useParent("cl-adv-search", AdvSearch);
 
    if (options) {
        provide("useAdvSearch__options", options);
    }
 
    return AdvSearch;
}
 
// 搜索
export function useSearch<T = any>(options?: ClSearch.Options<T>) {
    const Search = ref<ClSearch.Ref<T>>();
    useParent("cl-search", Search);
 
    provide("useSearch__options", options);
 
    return Search;
}
 
// 对话框
export function useDialog(options?: { onFullscreen(visible: boolean): void }) {
    const Dialog = inject("dialog") as ClDialog.Provide;
 
    watch(
        () => Dialog?.fullscreen.value,
        (val) => {
            options?.onFullscreen(val);
        }
    );
 
    return Dialog;
}