Changeset 487 for TFP-WebServer/MarkersMod
- Timestamp:
- Jun 17, 2024, 5:25:43 PM (6 months ago)
- Location:
- TFP-WebServer/MarkersMod
- Files:
-
- 1 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
TFP-WebServer/MarkersMod/MarkersMod.csproj
r469 r487 63 63 <Private>False</Private> 64 64 </Reference> 65 <Reference Include="Utf8Json , Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">65 <Reference Include="Utf8Json"> 66 66 <HintPath>..\7dtd-binaries\Utf8Json.dll</HintPath> 67 67 <Private>False</Private> … … 69 69 </ItemGroup> 70 70 <ItemGroup> 71 <Compile Include="src\MarkerData.cs" /> 71 72 <Compile Include="src\ModApi.cs" /> 72 73 <Compile Include="src\AssemblyInfo.cs" /> -
TFP-WebServer/MarkersMod/ModInfo.xml
r485 r487 5 5 <Description value="Allows placing custom markers on the web map" /> 6 6 <Author value="Catalysm and Alloc" /> 7 <Version value=" 22.0.1.0" />7 <Version value="1.1.0.0" /> 8 8 <Website value="" /> 9 9 </xml> -
TFP-WebServer/MarkersMod/WebMod/bundle.js
r485 r487 15 15 LayersControl, 16 16 Marker, 17 Tooltip, 17 18 HideBasedOnAuth, 18 19 L … … 45 46 lng: y 46 47 } 47 } );48 }, /*#__PURE__*/React.createElement(Tooltip, null, marker.name)); 48 49 }); 49 50 setMarkers(markerComponents); … … 82 83 formState: { 83 84 errors 84 } 85 }, 86 setValue 85 87 } = useForm(); 86 88 const { … … 91 93 mutate: createMarker 92 94 } = useMutation('createMarker', data => HTTP.post('/api/markers', { 95 name: data.name, 93 96 x: parseInt(data.x, 10), 94 97 y: parseInt(data.y, 10) … … 104 107 mutate: updateMarker 105 108 } = useMutation('updateMarker', data => HTTP.put(`/api/markers/${data.id}`, { 109 name: data.name, 106 110 x: parseInt(data.x, 10), 107 111 y: parseInt(data.y, 10), … … 114 118 method: 'PUT' 115 119 }); 120 const canDeleteRows = checkPermission({ 121 module: 'webapi.Markers', 122 method: 'DELETE' 123 }); 116 124 const columnDef = [{ 117 125 field: 'id', 118 126 filter: 'agTextColumnFilter', 119 checkboxSelection: true, 120 width: 300, 121 flex: 0 127 flex: 1 128 }, { 129 field: 'name', 130 filter: 'agTextColumnFilter', 131 flex: 1 122 132 }, { 123 133 field: 'x', 124 134 filter: 'agNumberColumnFilter', 125 editable: canEditRows, 126 cellEditorPopup: true, 127 width: 50, 128 flex: 0 135 flex: 0.25, 136 sort: 'asc' 129 137 }, { 130 138 field: 'y', 131 139 filter: 'agNumberColumnFilter', 132 editable: canEditRows, 133 cellEditorPopup: true, 134 width: 50, 135 flex: 0 140 flex: 0.25, 141 sort: 'asc' 136 142 }, { 137 143 field: 'icon', 138 144 filter: 'agTextColumnFilter', 139 editable: canEditRows,140 cellEditorPopup: true,141 145 flex: 1 142 146 }]; 143 const CreateMarker = () => { 147 const Form = /*#__PURE__*/React.createElement(TfpForm, { 148 id: "markers-form", 149 error: errors 150 }, /*#__PURE__*/React.createElement(FormElements.StyledFormItem, null, /*#__PURE__*/React.createElement(FormElements.FormLabel, { 151 htmlFor: "input-id" 152 }, "ID"), /*#__PURE__*/React.createElement(FormElements.FormInput, _extends({ 153 key: "id", 154 id: "input-id" 155 }, register('id'), { 156 disabled: true 157 }))), /*#__PURE__*/React.createElement(FormElements.StyledFormItem, null, /*#__PURE__*/React.createElement(FormElements.FormLabel, { 158 htmlFor: "input-name" 159 }, "Name"), /*#__PURE__*/React.createElement(FormElements.FormInput, _extends({ 160 key: "name", 161 id: "input-name" 162 }, register('name')))), /*#__PURE__*/React.createElement(FormElements.StyledFormItem, null, /*#__PURE__*/React.createElement(FormElements.FormLabel, { 163 htmlFor: "input-x" 164 }, "X"), /*#__PURE__*/React.createElement(FormElements.FormInput, _extends({ 165 key: "x", 166 id: "input-x" 167 }, register('x', { 168 required: true 169 })))), /*#__PURE__*/React.createElement(FormElements.StyledFormItem, null, /*#__PURE__*/React.createElement(FormElements.FormLabel, { 170 htmlFor: "input-y" 171 }, "Y"), /*#__PURE__*/React.createElement(FormElements.FormInput, _extends({ 172 key: "y", 173 id: "input-y" 174 }, register('y', { 175 required: true 176 }))))); 177 async function handleCreate(data) { 144 178 if (!checkPermission({ 145 179 module: 'webapi.Markers', 146 180 method: 'POST' 147 181 })) { 148 return null; 149 } 150 return /*#__PURE__*/React.createElement(TfpForm, { 151 id: "markers-form", 152 handleSubmit: handleSubmit(createMarker), 153 error: errors 154 }, /*#__PURE__*/React.createElement(FormElements.StyledFormItem, null, /*#__PURE__*/React.createElement(FormElements.FormLabel, { 155 htmlFor: "input-x" 156 }, "X"), /*#__PURE__*/React.createElement(FormElements.FormInput, _extends({ 157 key: "x", 158 id: "input-x" 159 }, register('x', { 160 required: true 161 })))), /*#__PURE__*/React.createElement(FormElements.StyledFormItem, null, /*#__PURE__*/React.createElement(FormElements.FormLabel, { 162 htmlFor: "input-y" 163 }, "Y"), /*#__PURE__*/React.createElement(FormElements.FormInput, _extends({ 164 key: "y", 165 id: "input-y" 166 }, register('y', { 167 required: true 168 }))))); 169 }; 170 async function cellEdited({ 171 data, 172 newValue, 173 column 174 }) { 182 return; 183 } 184 createMarker(data); 185 } 186 async function handleEdit(data) { 175 187 if (!checkPermission({ 176 188 module: 'webapi.Markers', … … 179 191 return; 180 192 } 181 const changedField = column.colId; 182 updateMarker({ 183 ...data, 184 [changedField]: newValue 185 }); 193 updateMarker(data); 186 194 } 187 195 async function cellDeleted(row) { … … 194 202 deleteMarker(row.id); 195 203 } 204 const setDefaultValues = data => { 205 setValue('id', data.id); 206 setValue('name', data.name); 207 setValue('x', data.x); 208 setValue('y', data.y); 209 }; 196 210 return /*#__PURE__*/React.createElement("div", { 197 211 style: { … … 199 213 height: '80vh' 200 214 } 201 }, /*#__PURE__*/React.createElement( CreateMarker, null), /*#__PURE__*/React.createElement(EditableTable, {215 }, /*#__PURE__*/React.createElement(EditableTable, { 202 216 columnDef: columnDef, 203 217 rowData: data, 204 218 reloadFn: refetch, 205 editRowFn: cellEdited,206 219 deleteRowFn: cellDeleted, 207 height: '90%' 220 canDeleteRows: canDeleteRows, 221 height: '90%', 222 editRowFn: handleSubmit(handleEdit), 223 editForm: Form, 224 setDefaultValues: setDefaultValues, 225 canEditRows: canEditRows, 226 canCreateRows: canEditRows, 227 createRowFn: handleSubmit(handleCreate) 208 228 })); 209 229 } -
TFP-WebServer/MarkersMod/src/Markers.cs
r459 r487 15 15 "https://upload.wikimedia.org/wikipedia/commons/thumb/1/11/Blue_question_mark_icon.svg/1200px-Blue_question_mark_icon.svg.png"; 16 16 17 private readonly Dictionary<string, (int, int, string)> markers = new Dictionary<string, (int, int, string)> ();17 private readonly Dictionary<string, MarkerData> markers = new Dictionary<string, MarkerData> (); 18 18 19 19 public Markers () { … … 24 24 int y = random.RandomRange (-1000, 1001); 25 25 26 markers.Add (WebUtils.GenerateGuid (), (x, y, null)); 26 string guid = WebUtils.GenerateGuid (); 27 markers.Add (guid, new MarkerData(guid, new Vector2i (x, y), "RandomMarker " + i, null)); 27 28 } 28 29 … … 33 34 private static readonly byte[] jsonKeyX = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("x"); 34 35 private static readonly byte[] jsonKeyY = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("y"); 36 private static readonly byte[] jsonKeyName = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("name"); 35 37 private static readonly byte[] jsonKeyIcon = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("icon"); 36 38 … … 44 46 45 47 bool first = true; 46 foreach (( string markerId, (int, int, string) properties) in markers) {48 foreach ((_, MarkerData iMarker) in markers) { 47 49 if (!first) { 48 50 writer.WriteValueSeparator (); … … 51 53 first = false; 52 54 53 writeMarkerJson (ref writer, markerId, properties);55 writeMarkerJson (ref writer, iMarker); 54 56 } 55 57 … … 59 61 } 60 62 61 if (!markers.TryGetValue (id, out (int, int, string) properties2)) {63 if (!markers.TryGetValue (id, out MarkerData singleMarker)) { 62 64 writer.WriteRaw (WebUtils.JsonEmptyData); 63 65 SendEnvelopedResult (_context, ref writer, HttpStatusCode.NotFound); … … 68 70 writer.WriteBeginArray (); 69 71 70 writeMarkerJson (ref writer, id, properties2);72 writeMarkerJson (ref writer, singleMarker); 71 73 72 74 writer.WriteEndArray (); … … 75 77 } 76 78 77 private void writeMarkerJson (ref JsonWriter _writer, string _markerId, (int, int, string) _properties) {79 private void writeMarkerJson (ref JsonWriter _writer, MarkerData _marker) { 78 80 _writer.WriteRaw (jsonKeyId); 79 _writer.WriteString (_marker Id);81 _writer.WriteString (_marker.Id); 80 82 _writer.WriteRaw (jsonKeyX); 81 (int x, int y, string icon) = _properties; 82 _writer.WriteInt32 (x); 83 _writer.WriteInt32 (_marker.Position.x); 83 84 _writer.WriteRaw (jsonKeyY); 84 _writer.WriteInt32 (y); 85 _writer.WriteInt32 (_marker.Position.y); 86 _writer.WriteRaw (jsonKeyName); 87 _writer.WriteString (_marker.Name); 85 88 _writer.WriteRaw (jsonKeyIcon); 86 _writer.WriteString ( icon ?? defaultIcon);89 _writer.WriteString (_marker.Icon ?? defaultIcon); 87 90 _writer.WriteEndObject (); 88 91 } … … 99 102 } 100 103 104 JsonCommons.TryGetJsonField (_jsonInput, "name", out string name); 105 if (string.IsNullOrEmpty (name)) { 106 name = null; 107 } 108 101 109 JsonCommons.TryGetJsonField (_jsonInput, "icon", out string icon); 102 110 if (string.IsNullOrEmpty (icon)) { … … 105 113 106 114 string newId = WebUtils.GenerateGuid (); 107 markers.Add (newId, (x, y, icon));115 markers.Add (newId, new MarkerData(newId, new Vector2i (x, y), name, icon)); 108 116 109 117 PrepareEnvelopedResult (out JsonWriter writer); … … 123 131 } 124 132 125 bool keepIcon = !_jsonInput.TryGetValue ("icon", out _); 126 JsonCommons.TryGetJsonField (_jsonInput, "icon", out string icon); 127 if (string.IsNullOrEmpty (icon)) { 128 icon = null; 129 } 133 bool keepName = !JsonCommons.TryGetJsonField (_jsonInput, "name", out string name); 134 135 bool keepIcon = !JsonCommons.TryGetJsonField (_jsonInput, "icon", out string icon); 130 136 131 137 string id = _context.RequestPath; 132 138 133 if (!markers.TryGetValue (id, out (int, int, string) properties)) {139 if (!markers.TryGetValue (id, out MarkerData oldMarker)) { 134 140 SendEmptyResponse (_context, HttpStatusCode.NotFound, _jsonInputData, "ID_NOT_FOUND"); 135 141 return; 136 142 } 137 143 144 if (keepName) { 145 name = oldMarker.Name; 146 } 147 138 148 if (keepIcon) { 139 icon = properties.Item3;149 icon = oldMarker.Icon; 140 150 } 141 markers [id] = (x, y, icon); 151 152 MarkerData updatedMarker = new MarkerData(id, new Vector2i (x, y), name, icon); 153 markers [id] = updatedMarker; 142 154 143 155 PrepareEnvelopedResult (out JsonWriter writer); 144 writer.WriteRaw (jsonKeyId); 145 writer.WriteString (id); 146 writer.WriteRaw (jsonKeyX); 147 writer.WriteInt32 (x); 148 writer.WriteRaw (jsonKeyY); 149 writer.WriteInt32 (y); 150 writer.WriteRaw (jsonKeyIcon); 151 writer.WriteString (icon); 152 writer.WriteEndObject (); 156 writeMarkerJson (ref writer, updatedMarker); 153 157 SendEnvelopedResult (_context, ref writer); 154 158 } -
TFP-WebServer/MarkersMod/src/Markers.openapi.yaml
r460 r487 24 24 examples: 25 25 - 321 26 name: 27 type: 28 - string 29 - 'null' 30 examples: 31 - My House 32 description: Name shown with the marker 26 33 icon: 27 34 type: … … 36 43 - x 37 44 - y 45 - name 46 - icon 38 47 39 48 MarkersList: … … 58 67 examples: 59 68 - 842 69 name: 70 type: 71 - string 72 - 'null' 73 examples: 74 - My House 60 75 icon: 61 type: string 76 type: 77 - string 78 - 'null' 62 79 format: uuid 63 80 examples:
Note:
See TracChangeset
for help on using the changeset viewer.