1 | (()=>{"use strict";class e{constructor(e){this.http=e}async getMarkers(){return await this.http.get("/api/markers")}async getMarker(e){return await this.http.get(`/api/markers/${e}`)}async deleteMarker(e){return await this.http.delete(`/api/markers/${e}`)}async updateMarker(e,{x:t,y:a,icon:r}){return await this.http.put(`/api/markers/${e}`,{x:parseInt(t,10),y:parseInt(a,10),icon:r})}async createMarker({x:e,y:t,icon:a}){return await this.http.post("/api/markers",{x:parseInt(e,10),y:parseInt(t,10),icon:a})}}const t="TFP_MarkersExample",a={about:"This mod manages markers on a Leaflet map.",routes:{},settings:{"Map markers":function({React:t,styled:a,HTTP:r,EditableTable:n,checkPermission:i}){const[l,o]=t.useState([]),[s,c]=t.useState(!0),[u,m]=t.useState({x:1,y:2}),d=new e(r),p=i({module:"webapi.Markers",method:"PUT"}),k=[{field:"id",filter:"agTextColumnFilter",checkboxSelection:!0,width:300,flex:0},{field:"x",filter:"agNumberColumnFilter",editable:p,cellEditorPopup:!0,width:50,flex:0},{field:"y",filter:"agNumberColumnFilter",editable:p,cellEditorPopup:!0,width:50,flex:0},{field:"icon",filter:"agTextColumnFilter",editable:p,cellEditorPopup:!0,flex:1}],y=()=>i({module:"webapi.Markers",method:"POST"})?t.createElement("form",{id:"markers-form",onSubmit:w},t.createElement("strong",null,"Create marker"),t.createElement("div",null,t.createElement("label",{htmlFor:"input-x"},"X"),t.createElement("input",{key:"markers-x",id:"input-x",value:u.x,onChange:e=>f(e,"x")})),t.createElement("div",null,t.createElement("label",{htmlFor:"input-y"},"Y"),t.createElement("input",{key:"markers-y",id:"input-y",value:u.y,onChange:e=>f(e,"y")})),t.createElement("button",{type:"submit"},"Create")):null;async function h(){const e=await d.getMarkers();o(e)}function f(e,t){switch(t){case"x":m({...u,x:e.target.value});break;case"y":m({...u,y:e.target.value})}}async function w(e){const{x:t,y:a}=u;e.preventDefault(),await d.createMarker({x:t,y:a}),c(!s)}return t.useEffect((()=>{h()}),[s]),t.createElement(t.Fragment,null,t.createElement(y,null),t.createElement(n,{columnDef:k,rowData:l,reloadFn:h,editRowFn:async function({data:e,newValue:t,column:a}){if(!i({module:"webapi.Markers",method:"PUT"}))return;const r=a.colId;d.updateMarker(e.id,{...e,[r]:t})},deleteRowFn:async function(e){i({module:"webapi.Markers",method:"DELETE"})&&(d.deleteMarker(e.id),c(!s))}}))}},mapComponents:[function({map:t,React:a,HTTP:r,checkPermission:n,LayerGroup:i,LayersControl:l,Marker:o,HideBasedOnAuth:s,L:c}){const u=new e(r),[m,d]=a.useState([]);return a.useEffect((()=>{!async function(){if(n({module:"webapi.Markers",method:"GET"})){const e=(await u.getMarkers()).map((e=>{const{x:t,y:r,icon:n="https://upload.wikimedia.org/wikipedia/commons/thumb/1/11/Blue_question_mark_icon.svg/1200px-Blue_question_mark_icon.svg.png"}=e,i=c.icon({iconSize:[25,25],iconUrl:n});return a.createElement(o,{key:e.id,icon:i,position:{lat:t,lng:r}})}));d(e)}}()}),[]),a.createElement(s,{requiredPermission:{module:"webapi.Markers",method:"GET"}},a.createElement(l.Overlay,{name:"Markers"},a.createElement(i,null,m)))}]};window[t]=a,window.dispatchEvent(new Event(`mod:${t}:ready`))})();
|
---|