UNPKG

@dtu-olp-2024/carousel-nocobase

Version:

A NocoBase plugin for carousel view display. Built for the DTU GreenHope project.

17 lines (13 loc) 10.7 kB
/** * This file is part of the NocoBase (R) project. * Copyright (c) 2020-2024 NocoBase Co., Ltd. * Authors: NocoBase Team. * * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License. * For more information, please refer to: https://www.nocobase.com/agreement. */ (function(o,t){typeof exports=="object"&&typeof module!="undefined"?t(exports,require("@nocobase/client"),require("react"),require("@formily/react"),require("react/jsx-runtime"),require("antd"),require("@ant-design/icons"),require("@formily/shared")):typeof define=="function"&&define.amd?define(["exports","@nocobase/client","react","@formily/react","react/jsx-runtime","antd","@ant-design/icons","@formily/shared"],t):(o=typeof globalThis!="undefined"?globalThis:o||self,t(o["@dtu-olp-2024/carousel-nocobase"]={},o["@nocobase/client"],o.react,o["@formily/react"],o.jsxRuntime,o.antd,o["@ant-design/icons"],o["@formily/shared"]))})(this,function(o,t,c,d,u,x,A,S){"use strict";var Z=Object.defineProperty,ee=Object.defineProperties;var te=Object.getOwnPropertyDescriptors;var P=Object.getOwnPropertySymbols;var q=Object.prototype.hasOwnProperty,M=Object.prototype.propertyIsEnumerable;var T=(o,t,c)=>t in o?Z(o,t,{enumerable:!0,configurable:!0,writable:!0,value:c}):o[t]=c,h=(o,t)=>{for(var c in t||(t={}))q.call(t,c)&&T(o,c,t[c]);if(P)for(var c of P(t))M.call(t,c)&&T(o,c,t[c]);return o},f=(o,t)=>ee(o,te(t));var F=(o,t)=>{var c={};for(var d in o)q.call(o,d)&&t.indexOf(d)<0&&(c[d]=o[d]);if(o!=null&&P)for(var d of P(o))t.indexOf(d)<0&&M.call(o,d)&&(c[d]=o[d]);return c};var B=(o,t,c)=>new Promise((d,u)=>{var x=g=>{try{S(c.next(g))}catch(l){u(l)}},A=g=>{try{S(c.throw(g))}catch(l){u(l)}},S=g=>g.done?d(g.value):Promise.resolve(g.value).then(x,A);S((c=c.apply(o,t)).next())});const g="Carousel",l=g.toLowerCase(),z={name:"@dtu-olp-2024/carousel-nocobase",version:"1.0.6",license:"GPL-3.0",private:!1,main:"dist/server/index.js",peerDependencies:{"@nocobase/client":"1.x","@nocobase/server":"1.x","@nocobase/test":"1.x"},files:["dist","dist/node_modules","README.md","LICENSE","package.json","server.js","server.d.ts","client.js","client.d.ts","CHANGELOG.md"],publishConfig:{access:"public",registry:"https://registry.npmjs.org/"},repository:{type:"git",url:"git+https://github.com/olp-dtu-2024/DTU-GreenHope"},displayName:"Carousel Plugin - DTU GreenHope 2024",description:"A NocoBase plugin for carousel view display. Built for the DTU GreenHope project.",keywords:["plugin","nocobase","dtu","greenhope","carousel"]};function k(){const e=t.useApp();return a=>e.i18n.t(a,{ns:[z.name,"client"]})}const $={name:"height",type:"actionModal",useComponentProps(){var n,p;const e=d.useFieldSchema(),{deepMerge:a}=t.useDesignable(),r=k();return{title:r("Edit Height"),schema:{type:"object",title:r("Edit Height"),properties:{height:{title:r("Height"),type:"number",default:(p=(n=e["x-decorator-props"])==null?void 0:n[l])==null?void 0:p.height,"x-decorator":"FormItem","x-component":"InputNumber"}}},onSubmit({height:s}){var i;a({"x-uid":e["x-uid"],"x-decorator-props":f(h({},e["x-decorator-props"]),{[l]:f(h({},(i=e["x-decorator-props"])==null?void 0:i[l]),{height:s})})})}}}},O={name:"objectFit",type:"select",useComponentProps(){var n,p;const e=d.useFieldSchema(),{deepMerge:a}=t.useDesignable();return{title:k()("Object Fit"),options:[{label:"Cover",value:"cover"},{label:"Contain",value:"contain"},{label:"Fill",value:"fill"},{label:"None",value:"none"},{label:"Scale Down",value:"scale-down"}],value:((p=(n=e["x-decorator-props"])==null?void 0:n[l])==null?void 0:p.objectFit)||"cover",onChange(s){var i;a({"x-uid":e["x-uid"],"x-decorator-props":f(h({},e["x-decorator-props"]),{[l]:f(h({},(i=e["x-decorator-props"])==null?void 0:i[l]),{objectFit:s})})})}}}},E={name:"autoplay",type:"switch",useComponentProps(){var n,p;const e=d.useFieldSchema(),{deepMerge:a}=t.useDesignable();return{title:k()("Autoplay"),checked:!!((p=(n=e["x-decorator-props"])==null?void 0:n[l])!=null&&p.autoplay),onChange(s){var i;a({"x-uid":e["x-uid"],"x-decorator-props":f(h({},e["x-decorator-props"]),{[l]:f(h({},(i=e["x-decorator-props"])==null?void 0:i[l]),{autoplay:s})})})}}}},j=new t.SchemaSettings({name:`blockSettings:${l}`,items:[{name:t.SchemaSettingsDataScope.name,Component:t.SchemaSettingsDataScope,useComponentProps:()=>{var s,i;const e=t.useCollection(),a=t.useAPIClient(),r=d.useFieldSchema(),n=((i=(s=r==null?void 0:r["x-decorator-props"])==null?void 0:s.params)==null?void 0:i.filter)||{},{deepMerge:p}=t.useDesignable();return{collectionName:e.name,defaultFilter:n,onSubmit:m=>B(this,null,function*(){const C={"x-uid":r["x-uid"],"x-decorator-props":f(h({},r["x-decorator-props"]),{params:{filter:h(h({},n),m.filter),appends:["images"]}})};yield a.request({url:"/uiSchemas:patch",method:"POST",data:C}).then(b=>{b.status===200&&p({"x-decorator-props":f(h({},r["x-decorator-props"]),{params:{filter:h(h({},n),m.filter),appends:["images"]}})})})})}}},{name:"editBlockTitle",Component:t.SchemaSettingsBlockTitleItem},{type:"remove",name:"remove",componentProps:{removeParentsIfNoChildren:!0,breakRemoveOn:{"x-component":"Grid"}}},$,O,E]});function G(e){var s;const{filter:a,parseVariableLoading:r}=t.useParsedFilter({filterOption:(s=e.params)==null?void 0:s.filter}),n=c.useMemo(()=>{var b,v;const i=[],m=(b=e.fieldNames)==null?void 0:b.start,C=(v=e.fieldNames)==null?void 0:v.end;return Array.isArray(m)&&m.length>=2&&i.push(m[0]),Array.isArray(C)&&C.length>=2&&i.push(C[0]),i},[e.fieldNames]);return{params:c.useMemo(()=>{var i;return f(h({},e.params),{appends:[...n,...((i=e.params)==null?void 0:i.appends)||[],"images"],filter:a})},[n,JSON.stringify(a),e.params]),parseVariableLoading:r}}function L(e){const{params:a,parseVariableLoading:r}=G(e);let n;return e.association&&(n=t.useParentRecordCommon(e.association)),{params:a,parentRecord:n,parseVariableLoading:r}}function H(){var a,r;return(r=(a=d.useFieldSchema().parent)==null?void 0:a["x-decorator-props"])==null?void 0:r[l]}const _=({dataSource:e="main",collection:a})=>({type:"void","x-decorator":"DataBlockProvider","x-component":"FormItem","x-settings":j.name,"x-toolbar":"BlockSchemaToolbar","x-decorator-props":{[l]:{},rowKey:"id",runWhenParamsChanged:!0,readPretty:!0,dataSource:e,collection:a,action:"list"},"x-use-decorator-props":"useBlockScopeDecoratorProps",properties:{[l]:{type:"void","x-component":g,"x-use-component-props":"useCarouselBlockProps"}}}),U=()=>{const{data:e}=t.useDataBlockRequest();return{images:e?(e==null?void 0:e.data)[0].images:[]}},V=t.withDynamicSchemaProps(e=>{const{images:a}=U(),i=e,{data:r,height:n=300,objectFit:p="cover"}=i,s=F(i,["data","height","objectFit"]);return a&&a.length?u.jsx(x.Carousel,f(h({},s),{children:a.map(m=>u.jsx("div",{children:u.jsx("img",{src:m.url,alt:m.title,style:{height:n,width:"100%",objectFit:p}},m.title)},m.url))})):u.jsx(x.Result,{status:"404"})},{displayName:g}),N={type:"item",name:l,Component:"DataBlockInitializer",icon:"PlayCircleOutlined",useComponentProps(){const{insert:e}=t.useSchemaInitializer();return{title:k()(g),icon:u.jsx(A.CodeOutlined,{}),componentType:g,useTranslationHooks:k,onCreateBlockSchema({item:r}){e(_({dataSource:r.dataSource,collection:r.name}))}}}},K=e=>Object.entries(e).map(([a,r])=>{const n=r.database?`, Database: ${r.database}`:"";return` - ${a} không khớp: Blockchain: ${r.blockchain}${n}`}).join(` `),J=e=>"error"in e?` - ${e.error}`:K(e.differences),R=()=>{const{message:e}=x.App.useApp(),a=t.useAPIClient(),r=()=>B(this,null,function*(){var p;const n="comparing";try{e.loading({content:"Đang kiểm tra giao dịch...",key:n});const s=yield a.request({url:"transactions:checkTransaction",method:"post"});if(!((p=s==null?void 0:s.data)!=null&&p.data))throw new Error("Invalid response format");const{status:i,mismatches:m=[],totalChecked:C=0,totalMismatches:b=0}=s.data.data;if(i===!0){e.success({content:"Giao dịch khớp",key:n,duration:3}),alert("Tất cả giao dịch đều khớp!");return}if(!Array.isArray(m)||m.length===0)throw new Error("No mismatch data available");const v=m.map((y,I)=>y!=null&&y.transaction_code?`${I+1}. Giao dịch: ${y.transaction_code} `+J(y):null).filter(Boolean).join(` `);v&&(alert(`Phát hiện ${b}/${C} giao dịch không khớp! `+v),e.error({content:u.jsxs("div",{children:[u.jsxs("div",{children:["Có ",b,"/",C," giao dịch không khớp:"]}),m.map((y,I)=>u.jsxs("div",{children:[u.jsx("strong",{children:`${I+1}. Giao dịch: ${y.transaction_code}`}),"error"in y?u.jsxs("div",{style:{marginLeft:20},children:["• ",y.error]}):Object.entries(y.differences).map(([Q,X],Y)=>{const D=X;return u.jsxs("div",{style:{marginLeft:20},children:["• ",Q,": Blockchain = ",D.blockchain,D.database&&`, Database = ${D.database}`]},Y)})]},I))]}),key:n,duration:10}))}catch(s){e.destroy(),e.error({content:`Có lỗi xảy ra khi kiểm tra giao dịch: ${s.message}`,key:n,duration:3}),console.error("Error checking transactions:",s)}});return u.jsx(x.Button,{onClick:r,children:"So sánh giao dịch"})},W=()=>{const e=t.useSchemaInitializerItem(),{insert:a}=t.useSchemaInitializer(),{name:r}=t.useCollection_deprecated(),n={type:"void",title:'{{ t("Compare Transaction") }}',"x-action":"compare","x-toolbar":"ActionSchemaToolbar","x-settings":"actionSettings:compare","x-decorator":"ACLActionProvider","x-component":"CompareButton","x-action-settings":{collection:r},"x-component-props":{icon:"CompareOutlined",useAction:"{{ useCompareAction }}"}};return u.jsx(t.SchemaInitializerItem,f(h({},e),{onClick:()=>{var s;const p=S.merge(n||{},e.schema||{});(s=e==null?void 0:e.schemaInitialize)==null||s.call(e,p),a(p)}}))};class w extends t.Plugin{load(){return B(this,null,function*(){this.app.addScopes({useBlockScopeDecoratorProps:L,useCarouselBlockProps:H}),this.app.addComponents({Carousel:V,CompareButton:R,CompareButtonInitializer:W}),this.app.schemaSettingsManager.add(j),this.app.schemaInitializerManager.addItem("page:addBlock",`dataBlocks.${N.name}`,N),this.app.schemaInitializerManager.addItem("table:configureActions","enableActions.customRequest",{type:"item",name:"customRequest",title:'{{t("Custom request")}}',Component:"CustomRequestInitializer"}),this.app.schemaInitializerManager.addItem("list:configureActions","enableActions.customRequest",{type:"item",name:"customRequest",title:'{{t("Custom request")}}',Component:"CustomRequestInitializer"}),this.app.schemaInitializerManager.addItem("list:configureActions","enableActions.compareButton",{type:"item",name:"compareButton",title:'{{t("Compare Transaction")}}',Component:"CompareButtonInitializer"})})}}o.CarouselNocobaseClient=w,o.default=w,Object.defineProperties(o,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});