@aplus-frontend/ui
Version:
2 lines (1 loc) • 2.5 kB
JavaScript
;Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("vue"),J=require("../../utils/index.js"),d=require("../components/setting/utils.js"),R=require("../context.js"),f=require("lodash-unified");require("../../config-provider/index.js");const W=require("@aplus-frontend/antdv/es/theme/internal"),z=require("../utils.js"),D=require("../../config-provider/hooks/use-namespace.js");function g(r,n){r.forEach(i=>{n(i),i.children?.length&&g(i.children,n)})}function G(r,n){if(!n)return{};const i=r.getItem(n);return i?JSON.parse(i):{}}function y(r,n){if(f.isObject(r)&&f.isObject(n))return f.mergeWith({},r,n,y)}const k=Number.MAX_SAFE_INTEGER;function P(r,n,i,a){return a?a.fixed:n?n.fixed:i&&"fixed"in i?i.fixed:f.isBoolean(r.fixed)?r.fixed?"left":void 0:r.fixed}const U=(r,n,i)=>{const{be:a}=D.useNamespace("ap-table"),[X,q]=W.useToken(),c=o.ref(i),h=o.shallowRef(d.customCloneColumnStates(i)),{columnsBackup:x,updateColumns:N}=R.useInjectApTable(),A=r.persistenceType||"localStorage",C=window[A];function O(){const e=G(C,o.unref(n)),t=f.mergeWith({},r.defaultValue||{},e,y),s=o.unref(x);function l(j,M,_){return j.map(u=>{const T=M?.[u.key]||{},S={key:u.key,label:f.isString(u.title)?z.getTableTitle(u,a("table-header","title"),void 0,q.value.colorTextTertiary):u.title,show:!0,fixed:P(u,e[u.key],r.defaultValue?.[u.key],_),disabled:!1,...f.omit(T,["children","fixed"])};return S.children=u.children?.length?l(u.children,T.children,S):void 0,S}).sort(B)}return l(s||[],t)}function B(e,t){const s=f.isNumber(e.order)?e.order:k;let l=f.isNumber(t.order)?t.order:k;return e.fixed==="right"&&t.fixed!=="right"&&(l=s-1),t.fixed==="left"&&e.fixed!=="left"&&(l=s-1),s-l}const m=o.computed(()=>o.unref(c).every(e=>e.show));function b(){const e=o.unref(c);g(e,t=>{!t.disabled&&(t.show=!0)})}function p(){const e=o.unref(c);g(e,t=>{!t.disabled&&(t.show=!1)})}function E(){o.unref(m)?p():b()}function F(e,t){g(o.unref(c),s=>{s.key===e&&(s.show=J.isDef(t)?t:!s.show)})}function v(e){c.value=e}function I(e){h.value=e}function w(e){if(o.unref(n)){const t=d.getColumnStateString(e);C.setItem(o.unref(n),JSON.stringify(t))}}return o.watchEffect(()=>{const e=O();v(e),r.onChange?.(e),N?.(d.default(o.unref(x),o.unref(e))),h.value=d.customCloneColumnStates(e),w(e)}),{columnState:c,selectAll:b,unSelectAll:p,toggleSelect:F,setColumnState:v,isAllSelected:m,toggleSelectAll:E,columnStateBackup:h,setColumnStateBackup:I,cacheColumnStateIfNeeded:w}};exports.useTableColumnState=U;