diff --git a/src/c3nav/editor/static/editor/js/editor.js b/src/c3nav/editor/static/editor/js/editor.js index 312ab026..b5a2d93d 100644 --- a/src/c3nav/editor/static/editor/js/editor.js +++ b/src/c3nav/editor/static/editor/js/editor.js @@ -501,6 +501,10 @@ editor = { // listener for form submits in the sidebar. e.preventDefault(); if (editor._loading_geometry || $(this).is('.creation-lock') || $(this).is('.scan-lock')) return; + if (editor._staircase_layer) { + editor._staircase_submit($(this)) + return + } var data = $(this).serialize(); var btn = $(this).data('btn'); if (btn !== undefined && btn !== null) { @@ -1906,12 +1910,12 @@ editor = { }, _destroy_staircase_editing: function() { - if (editor._staircase_layer !== null) { + if (editor._staircase_layer) { editor.map.removeLayer(editor._staircase_layer) editor._staircase_layer = null } editor.map.off('editable:editing', editor._update_staircase_preview) - if (editor._current_editing_shape !== null) { + if (editor._current_editing_shape && editor._current_editing_shape.editor) { editor._current_editing_shape.editor.cancelDrawing() editor._current_editing_shape.remove() editor._current_editing_shape = null @@ -1989,29 +1993,59 @@ editor = { return lines }, - _update_staircase_preview: function(e = null) { - if (editor._current_editing_shape === null) { - return + _get_staircase_lines: function() { + if (!editor._current_editing_shape || !editor._current_editing_shape._parts) { + return [] } points = editor._current_editing_shape._parts[0] || [] - editor._staircase_layer.clearLayers() - //console.log(points) if (points.length < 3) { - return + return [] } xs = points.map(p => p.x) ys = points.map(p => p.y) lines = editor._transform_for_staircase(xs, ys, editor._staircase_steps_count) + lines = lines.map(l => [ + editor.map.layerPointToLatLng([l.p1.x, l.p1.y]), + editor.map.layerPointToLatLng([l.p2.x, l.p2.y]), + ]) + return lines + }, + _update_staircase_preview: function(e = null) { + if (editor._staircase_layer) { + editor._staircase_layer.clearLayers() + } + lines = editor._get_staircase_lines() lines.forEach(l => { - L.polyline( - [ - editor.map.layerPointToLatLng([l.p1.x, l.p1.y]), - editor.map.layerPointToLatLng([l.p2.x, l.p2.y]), - ], - {color: "red"} - ).addTo(editor._staircase_layer); + L.polyline(l, {color: "red"}).addTo(editor._staircase_layer); + }) + }, + + _staircase_submit: function(form) { + csrfmiddlewaretoken = form.find('input[name=csrfmiddlewaretoken]').attr('value') + import_tag = form.find('input[name=import_tag]').val() + space = form.attr('space') + lines = editor._get_staircase_lines() + + console.log("hereeeeeee", csrfmiddlewaretoken, import_tag, space, lines) + Promise.all(lines.map(l => + fetch("/editor/spaces/" + space + "/stairs/create", { + method: "POST", + headers: { + "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8", + "X-Requested-With": "XMLHttpRequest", + }, + body: "csrfmiddlewaretoken=" + encodeURIComponent(csrfmiddlewaretoken) + + "&geometry=" + encodeURIComponent(JSON.stringify({ + type: "LineString", + coordinates: [[l[0]["lng"], l[0]["lat"]], [l[1]["lng"], l[1]["lat"]]] + })) + + "&import_tag=" + encodeURIComponent(import_tag) + }) + )).then(() => { + form.remove() + window.location.href = "/editor/spaces/" + space + "/stairs" }) } }; diff --git a/src/c3nav/editor/templates/editor/create_staircase.html b/src/c3nav/editor/templates/editor/create_staircase.html index b17e0754..0c3937ea 100644 --- a/src/c3nav/editor/templates/editor/create_staircase.html +++ b/src/c3nav/editor/templates/editor/create_staircase.html @@ -8,7 +8,7 @@ {% bootstrap_messages %} -
+ {% csrf_token %} {% bootstrap_form form %}
diff --git a/src/c3nav/editor/views/edit.py b/src/c3nav/editor/views/edit.py index 28442e52..3b7aa8c7 100644 --- a/src/c3nav/editor/views/edit.py +++ b/src/c3nav/editor/views/edit.py @@ -412,6 +412,7 @@ def edit(request, pk=None, model=None, level=None, space=None, on_top_of=None, e }) if request.path.endswith("staircase"): + ctx["space"] = space_id return render(request, 'editor/create_staircase.html', ctx) else: return render(request, 'editor/edit.html', ctx)