@zhsz/cool-design-crud
Version:
141 lines (140 loc) • 3.27 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const vue = require("vue");
const test = require("../utils/test.js");
function useParent(name, r) {
var _a, _b, _c;
const d = vue.getCurrentInstance();
if (d) {
let parent = (_a = d.proxy) == null ? void 0 : _a.$.parent;
if (parent) {
while (parent && ((_b = parent.type) == null ? void 0 : _b.name) !== name && ((_c = parent.type) == null ? void 0 : _c.name) !== "cl-crud") {
parent = parent == null ? void 0 : parent.parent;
}
if (parent) {
if (parent.type.name === name) {
r.value = parent.exposed;
}
}
}
}
}
function useEvent(names, { r, options, clear }) {
const d = {};
if (!r.__ev)
r.__ev = {};
names.forEach((k) => {
if (!r.__ev[k])
r.__ev[k] = [];
if (options[k]) {
r.__ev[k].push(options[k]);
}
d[k] = (...args) => {
r.__ev[k].filter(Boolean).forEach((e) => {
e(...args);
});
if (clear === k) {
for (const i in r.__ev) {
r.__ev[i].splice(1, 999);
}
}
};
});
return d;
}
function useCrud(options, cb) {
const Crud = vue.ref();
useParent("cl-crud", Crud);
if (options) {
vue.provide("useCrud__options", {
...options,
service: options.service === "test" ? test.TestService : options.service
});
}
vue.watch(Crud, (val) => {
if (val) {
if (cb) {
cb(val);
}
}
});
return Crud;
}
function useUpsert(options) {
const Upsert = vue.ref();
useParent("cl-upsert", Upsert);
if (options) {
vue.provide("useUpsert__options", options);
}
vue.watch(
Upsert,
(val) => {
if (val) {
if (options) {
const event = useEvent(["onOpen", "onOpened", "onClosed"], {
r: val,
options,
clear: "onClosed"
});
Object.assign(val.config, event);
}
}
},
{
immediate: true
}
);
return Upsert;
}
function useTable(options) {
const Table = vue.ref();
useParent("cl-table", Table);
if (options) {
vue.provide("useTable__options", options);
}
return Table;
}
function useForm(cb) {
const Form = vue.ref();
useParent("cl-form", Form);
vue.nextTick(() => {
if (cb && Form.value) {
cb(Form.value);
}
});
return Form;
}
function useSearch(options) {
const Search = vue.ref();
useParent("cl-search", Search);
if (options) {
vue.provide("useSearch__options", options);
}
return Search;
}
function useAdvSearch(options) {
const AdvSearch = vue.ref();
useParent("cl-adv-search", AdvSearch);
if (options) {
vue.provide("useAdvSearch__options", options);
}
return AdvSearch;
}
function useDialog(options) {
const Dialog = vue.inject("dialog");
vue.watch(
() => Dialog == null ? void 0 : Dialog.fullscreen.value,
(val) => {
options == null ? void 0 : options.onFullscreen(val);
}
);
return Dialog;
}
exports.useAdvSearch = useAdvSearch;
exports.useCrud = useCrud;
exports.useDialog = useDialog;
exports.useForm = useForm;
exports.useParent = useParent;
exports.useSearch = useSearch;
exports.useTable = useTable;
exports.useUpsert = useUpsert;