Skip to content

Commit

Permalink
Rewrite raster layer export form to antd
Browse files Browse the repository at this point in the history
  • Loading branch information
drnextgis committed Jul 31, 2022
1 parent 7c47aff commit 6f48f6b
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 174 deletions.
119 changes: 0 additions & 119 deletions nextgisweb/raster_layer/amd/ngw-raster-layer/ExportForm.js

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion nextgisweb/raster_layer/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def export(request):
srs = int(request.GET.get("srs", request.context.srs.id))
srs = SRS.filter_by(id=srs).one()
format = request.GET.get("format", "GTiff")
bands = request.GET.getall("bands")
bands = request.GET.get("bands").split(",")

if format is None:
raise ValidationError(_("Output format is not provided."))
Expand Down
114 changes: 114 additions & 0 deletions nextgisweb/raster_layer/nodepkg/export-form/ExportForm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import settings from "@nextgisweb/pyramid/settings!raster_layer";
import { route, routeURL } from "@nextgisweb/pyramid/api";
import { Form } from "@nextgisweb/gui/antd";
import { FieldsForm, Select, useForm } from "@nextgisweb/gui/fields-form";
import { LoadingWrapper, SaveButton } from "@nextgisweb/gui/component";
import { errorModal } from "@nextgisweb/gui/error";
import { useMemo, useState, useEffect } from "react";
import i18n from "@nextgisweb/pyramid/i18n!";

const srsListToOptions = (srsList) => {
return srsList.map((srs) => {
return {
label: srs.display_name,
value: srs.id,
};
});
};

const bandListToOptions = (bandList) => {
return bandList.map((band, idx) => {
return {
label:
i18n.gettext("Band") +
" " +
(idx + 1) +
(band !== "Undefined" ? " (" + band + ")" : ""),
value: idx + 1,
};
});
};

export function ExportForm({ id }) {
const [status, setStatus] = useState("loading");
const [srsOptions, setSrsOptions] = useState([]);
const [bandOptions, setBandOptions] = useState([]);
const [defaultSrs, setDefaultSrs] = useState();
const form = useForm()[0];

async function load() {
try {
const srsInfo = await route("spatial_ref_sys.collection").get();
const itemInfo = await route("resource.item", id).get();
setSrsOptions(srsListToOptions(srsInfo));
setBandOptions(bandListToOptions(itemInfo.raster_layer.color_interpretation));
setDefaultSrs(itemInfo.raster_layer.srs.id);
} catch (err) {
errorModal(err);
} finally {
setStatus("loaded");
}
}

useEffect(() => load(), []);

const fields = useMemo(
() => [
{
name: "format",
label: i18n.gettext("Format"),
widget: Select,
choices: settings.export_formats.map((format) => ({
value: format.name,
label: format.display_name,
})),
},
{
name: "srs",
label: i18n.gettext("SRS"),
widget: Select,
choices: srsOptions,
},
{
name: "bands",
label: i18n.gettext("Bands"),
widget: Select,
mode: "multiple",
choices: bandOptions,
},
],
[srsOptions, bandOptions]
);

const exportRaster = () => {
const fields = form.getFieldsValue();
window.open(
routeURL("resource.export", id) +
"?" +
new URLSearchParams(fields).toString()
);
};

if (status === "loading") {
return <LoadingWrapper />;
}

return (
<FieldsForm
fields={fields}
form={form}
initialValues={{
srs: defaultSrs,
bands: bandOptions.map((band) => band.value),
format: settings.export_formats[0].name,
}}
labelCol={{ span: 6 }}
>
<Form.Item>
<SaveButton onClick={exportRaster} icon={null}>
{i18n.gettext("Save")}
</SaveButton>
</Form.Item>
</FieldsForm>
);
}
3 changes: 3 additions & 0 deletions nextgisweb/raster_layer/nodepkg/export-form/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/** @entrypoint */
import { ExportForm } from "./ExportForm";
export default ExportForm;
5 changes: 5 additions & 0 deletions nextgisweb/raster_layer/nodepkg/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "@nextgisweb/raster_layer",
"version": "0.0.0",
"type": "module"
}
17 changes: 0 additions & 17 deletions nextgisweb/raster_layer/template/export.mako

This file was deleted.

18 changes: 14 additions & 4 deletions nextgisweb/raster_layer/view.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from pyramid.httpexceptions import HTTPNotFound

from .. import dynmenu as dm
from ..pyramid import viewargs
from ..gui import REACT_RENDERER
from ..resource import Widget, Resource

from .model import RasterLayer
Expand All @@ -14,15 +14,25 @@ class RasterLayerWidget(Widget):
amdmod = 'ngw-raster-layer/Widget'


@viewargs(renderer='nextgisweb:raster_layer/template/export.mako')
def export(request):
if not request.context.has_export_permission(request.user):
raise HTTPNotFound()
return dict(obj=request.context, title=_("Save as"), maxheight=True)
return dict(
obj=request.context,
title=_("Save as"),
props=dict(id=request.context.id),
entrypoint="@nextgisweb/raster_layer/export-form",
maxheight=True,
)


def setup_pyramid(comp, config):
config.add_view(export, route_name='resource.export.page', context=RasterLayer)
config.add_view(
export,
route_name='resource.export.page',
context=RasterLayer,
renderer=REACT_RENDERER,
)

# Layer menu extension
class LayerMenuExt(dm.DynItem):
Expand Down

0 comments on commit 6f48f6b

Please sign in to comment.