index.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <template>
  2. <RenderFoldDoult v-bind="props"></RenderFoldDoult>
  3. </template>
  4. <script setup lang="tsx" name="WidgetLjFoldLayoutDouble">
  5. import { ref, onMounted, nextTick, computed, useSlots } from "vue";
  6. import LjFoldLayout from "@/components/LjFoldLayout/index.vue";
  7. // import { useI18n } from "vue-i18n";
  8. import { useLayoutLocalStore } from "@/stores/modules/layoutLocal";
  9. import { cloneDeep, pick, defaultsDeep } from "lodash-es";
  10. import { getDifference, setDifference } from "@/utils";
  11. interface LjFoldLayoutDoubleProp {
  12. dwname?: string;
  13. first: any;
  14. second?: any;
  15. /**
  16. * @description 布局读取/保存时,其他自定义属性
  17. */
  18. params?: any;
  19. /**
  20. * @argument string[] 布局读取/保存时,赋值的属性
  21. */
  22. detailLayoutAttr?: string[];
  23. }
  24. const props = withDefaults(defineProps<LjFoldLayoutDoubleProp>(), {
  25. dwname: "",
  26. first: () => ({}),
  27. second: () => ({}),
  28. detailLayoutAttr: () => ["first.left", "first.right", "second.left", "second.right"]
  29. });
  30. // const { t } = useI18n();
  31. const layoutLocalStore = useLayoutLocalStore();
  32. const FoldLayoutFilterAttr = ["width", "hidden", "lastWidth"];
  33. const DOUBLE_FOLD_ATTR = ["first", "second", "params"];
  34. const firstFoldLayoutRef = ref();
  35. const secondFoldLayoutRef = ref();
  36. const layoutSaved = ref<any>({});
  37. const layoutDefault: any = {
  38. first: {},
  39. second: {}
  40. };
  41. const currentLayout: any = ref({
  42. first: {},
  43. second: {}
  44. });
  45. /**
  46. * @description 当前布局
  47. */
  48. const dwnameLayout = computed(() => {
  49. return props.dwname + "__layout-fold";
  50. });
  51. /**
  52. * @description 读取布局
  53. */
  54. const getLayout = async () => {
  55. let res: any = {};
  56. if (props.dwname !== "") {
  57. res = await layoutLocalStore.getLayoutAttr(dwnameLayout.value);
  58. }
  59. console.log("LJgetLayout getLayout res :>> ", res);
  60. layoutSaved.value = res;
  61. // console.log("LJgetLayout props :>> ", props);
  62. let _props = pick(props, DOUBLE_FOLD_ATTR);
  63. // console.log("LJgetLayout _props :>> ", _props);
  64. let _define = defaultsDeep(_props, layoutDefault);
  65. console.log("LJgetLayout _define :>> ", _define);
  66. currentLayout.value = setDifference(_define, res);
  67. console.log("LJgetLayout currentLayout.value :>> ", currentLayout.value);
  68. // // 加载主表-基础信息布局
  69. // await init();
  70. };
  71. /**
  72. * @description 监听框架属性变化
  73. */
  74. const toSaveFoldLayout = (layout: any, saveAttr: string, readAttr: string) => {
  75. let diff = getDifference(currentLayout.value[saveAttr][readAttr], layout[readAttr], FoldLayoutFilterAttr);
  76. console.log("toSetDetailBase diff :>> ", diff);
  77. console.log("layoutSaved.value :>> ", layoutSaved.value, saveAttr, readAttr);
  78. layoutSaved.value = Object.assign(layoutSaved.value, { [saveAttr]: { [readAttr]: diff } });
  79. // layoutSaved.value[saveAttr][readAttr] = diff;
  80. console.log("save _layout :>> ", layoutSaved.value);
  81. // 保存布局
  82. props.dwname !== "" && layoutLocalStore.setDwLayout(dwnameLayout.value, layoutSaved.value, false).then(() => {});
  83. };
  84. /**
  85. * @description 保存布局:其他参数
  86. */
  87. const setDLayoutParams = (params: any) => {
  88. layoutSaved.value.params = params;
  89. // 保存布局
  90. props.dwname !== "" && layoutLocalStore.setDwLayout(dwnameLayout.value, layoutSaved.value, false).then(() => {});
  91. };
  92. onMounted(async () => {
  93. await getLayout();
  94. });
  95. const slots: any = useSlots();
  96. const RenderFoldDoult = (rProps: any) => {
  97. let firstSlot: any = {};
  98. if (slots.firstLeft) {
  99. firstSlot.left = () => slots.firstLeft();
  100. }
  101. if (slots.firstRihght) {
  102. firstSlot.right = () => slots.firstRihght();
  103. }
  104. let secondSlot: any = {};
  105. if (slots.secondLeft) {
  106. secondSlot.left = () => slots.secondLeft();
  107. }
  108. if (slots.secondMain) {
  109. secondSlot.default = () => slots.secondMain();
  110. }
  111. if (slots.secondRight) {
  112. secondSlot.right = () => slots.secondRight();
  113. }
  114. return (
  115. <>
  116. <div class="main-box" {...rProps}>
  117. <LjFoldLayout
  118. ref={firstFoldLayoutRef}
  119. {...currentLayout.value.first}
  120. onFoldLeft={(layout: any) => toSaveFoldLayout(layout, "first", "left")}
  121. onFoldRight={(layout: any) => toSaveFoldLayout(layout, "first", "right")}
  122. v-slots={firstSlot}
  123. >
  124. <LjFoldLayout
  125. ref={secondFoldLayoutRef}
  126. {...currentLayout.value.second}
  127. onFoldLeft={(layout: any) => toSaveFoldLayout(layout, "second", "left")}
  128. onFoldRight={(layout: any) => toSaveFoldLayout(layout, "second", "right")}
  129. v-slots={secondSlot}
  130. ></LjFoldLayout>
  131. </LjFoldLayout>
  132. </div>
  133. </>
  134. );
  135. };
  136. defineExpose({
  137. firstFoldLayoutRef,
  138. secondFoldLayoutRef,
  139. currentLayout,
  140. setDLayoutParams
  141. });
  142. </script>