Home Information - Your home, your data, organized.
Python
Added saving SVG geoemtry after Javascript editing.
Anthony Cassandra25859864f366
diff --git a/src/hi/apps/location/edit/urls.py b/src/hi/apps/location/edit/urls.pyindex 425b655c..1a9bdefd 100644--- a/src/hi/apps/location/edit/urls.py+++ b/src/hi/apps/location/edit/urls.py@@ -28,4 +28,9 @@ urlpatterns = [re_path( r'^location-view/collection/toggle/(?P<location_view_id>\d+)/(?P<collection_id>\d+)$',views.LocationViewEntityToggleCollectionView.as_view(),name='location_edit_location_view_collection_toggle' ),++ re_path( r'^location-view/geometry/(?P<location_view_id>\d+)$',+ views.LocationViewGeometryView.as_view(),+ name='location_edit_location_view_geometry' ),+]diff --git a/src/hi/apps/location/edit/views.py b/src/hi/apps/location/edit/views.pyindex ad961175..88c11643 100644--- a/src/hi/apps/location/edit/views.py+++ b/src/hi/apps/location/edit/views.py@@ -1,3 +1,4 @@+from decimal import Decimalimport jsonimport logging@@ -9,6 +10,7 @@ from django.views.generic import Viewfrom hi.apps.collection.models import Collectionimport hi.apps.common.antinode as antinode+from hi.apps.common.svg_models import SvgViewBoxfrom hi.apps.entity.models import Entityfrom hi.apps.location.location_view_manager import LocationViewManagerfrom hi.apps.location.models import LocationView@@ -287,6 +289,33 @@ class LocationViewReorder( View ):LocationEditHelpers.set_location_view_order(location_view_id_list = location_view_id_list,)- return antinode.response( main_content = 'OK' )-+ return antinode.response( main_content = 'OK' )++class LocationViewGeometryView( View ):++ def post(self, request, *args, **kwargs):++ location_view_id = kwargs.get('location_view_id')+ try:+ location_view = LocationView.objects.select_related(+ 'location' ).get( id = location_view_id )+ except LocationView.DoesNotExist:+ return page_not_found_response( request )++ try:+ svg_view_box_str = request.POST.get('view_box')+ svg_view_box = SvgViewBox.from_attribute_value( svg_view_box_str )+ except (TypeError, ValueError ) as e:+ return bad_request_response( request, message = f'Bad viewbox: {svg_view_box_str}' )++ try:+ svg_rotate_angle = float( request.POST.get('rotate_angle'))+ except (TypeError, ValueError ) as e:+ return bad_request_response( request, message = f'Bad rotate angle: {svg_rotate_angle}' )++ location_view.svg_view_box_str = str(svg_view_box)+ location_view.svg_rotate = Decimal( svg_rotate_angle )+ location_view.save()++ return antinode.response( main_content = 'OK' )diff --git a/src/hi/apps/location/templates/location/location_view.html b/src/hi/apps/location/templates/location/location_view.htmlindex 5ed4b6b4..41fb306b 100644--- a/src/hi/apps/location/templates/location/location_view.html+++ b/src/hi/apps/location/templates/location/location_view.html@@ -2,6 +2,7 @@<div id="hi-location-view-main" class="hi-location-view"><svg id="{{ location_view_data.location_view.html_id }}"class="hi-location-view"+ location-view-id="{{ location_view_data.location_view.id }}"xmlns="http://www.w3.org/2000/svg"viewBox="{{ location_view_data.location_view.svg_view_box }}"extents="{{ location_view_data.location_view.location.svg_view_box }}"diff --git a/src/hi/apps/location/views.py b/src/hi/apps/location/views.pyindex 3c386f0b..d96efe8c 100644--- a/src/hi/apps/location/views.py+++ b/src/hi/apps/location/views.py@@ -91,69 +91,3 @@ class LocationViewView( HiGridView ):push_url_name = 'location_view',push_url_kwargs = kwargs,)---class LocationViewPanZoomRotateView( HiGridView ):-- def get(self, request, *args, **kwargs):-- location_view_id = kwargs.get('id')- try:- location_view = LocationView.objects.select_related(- 'location' ).get( id = location_view_id )- except LocationView.DoesNotExist:- return page_not_found_response( request )-- action = zzz- if not action:- return bad_request_response( request, message = 'Missing action param.' )-- svg_view_box = zzz- svg_rotate = zzz-- if not svg_view_box:- svg_view_box = location_view.svg_view_box- svg_rotate = location_view.svg_rotate-- pan_factor = 0.1- zoom_factor = 0.1- rotate_angle = 9-- if action == 'pan-left':- new_x = svg_view_box.x - ( pan_factor * svg_view_box.width )- elif action == 'pan-right':- new_x = svg_view_box.x + ( pan_factor * svg_view_box.width )- elif action == 'pan-up':- new_y = svg_view_box.y - ( pan_factor * svg_view_box.height )- elif action == 'pan-down':- new_y = svg_view_box.y + ( pan_factor * svg_view_box.height )- elif action == 'zoom-in':- new_width = zoom_factor * svg_view_box.width- new_height = zoom_factor * svg_view_box.height- elif action == 'zoom-out':- new_width = ( 1.0 + zoom_factor ) * svg_view_box.width- new_height = ( 1.0 * zoom_factor ) * svg_view_box.height- elif action == 'rotate-clockwise':- new_rotate = float(svg_rotate) - rotate_angle- elif action == 'rotate-counterclockwise':- new_rotate = float(svg_rotate) + rotate_angle---- # zzz norm angle to 0-360- # zzz limit on zoom- # zzz limit on pan-- context = {- 'svg_view_box_override': svg_view_box,- 'svg_rotate_override': svg_rotate,- }-- return self.hi_grid_response(- request = request,- context = context,- main_template_name = 'location/location_view.html',- push_url_name = 'location_view',- push_url_kwargs = kwargs,- )-diff --git a/src/hi/static/js/edit-svg-icon.js b/src/hi/static/js/edit-svg-icon.jsindex 30304e9b..04dd9c7d 100644--- a/src/hi/static/js/edit-svg-icon.js+++ b/src/hi/static/js/edit-svg-icon.js@@ -11,7 +11,8 @@this.clearSelection.bind(this) );},clearSelection: function( data ) {- if ( data != MODULE_NAME ) {+ gCurrentSelectionModule = data.moduleName;+ if ( data.moduleName != MODULE_NAME ) {clearSelectedIconSvgGroup();}}@@ -47,6 +48,7 @@ROTATE: 'rotate'};+ let gCurrentSelectionModule = null;let gSvgIconActionState = SvgActionStateType.MOVE;let gSelectedIconSvgGroup = null;let gSvgIconDragData = null;@@ -81,7 +83,6 @@});function handleMouseDown( event ) {- if ( Hi.DEBUG ) { console.log( `Mouse down event [${MODULE_NAME}]`, event ); }// Need to track start time to differentiate drag/scale/rotate actions from regular clicks.gClickStart = {@@ -91,6 +92,7 @@};if ( gSvgIconEditData ) {+ if ( Hi.DEBUG ) { console.log( `Mouse down event [${MODULE_NAME}]`, event ); }if ( gSvgIconActionState == SvgActionStateType.SCALE ) {gSvgIconEditData.isScaling = true;} else if ( gSvgIconActionState == SvgActionStateType.ROTATE ) {@@ -100,11 +102,11 @@event.stopImmediatePropagation();return;} else {- if ( Hi.DEBUG ) { Hi.displayElementInfo( 'Event target: ', $(event.target) ); }const enclosingSvgGroup = $(event.target).closest('g');if ( enclosingSvgGroup.length > 0 ) {const svgDataType = $(enclosingSvgGroup).attr( Hi.DATA_TYPE_ATTR );if ( svgDataType == Hi.DATA_TYPE_ICON_VALUE ) {+ if ( Hi.DEBUG ) { console.log( `Mouse down event [${MODULE_NAME}]`, event ); }createIconDragData( event, enclosingSvgGroup );event.preventDefault();event.stopImmediatePropagation();@@ -120,7 +122,7 @@if ( gSvgIconDragData ) {if ( gSvgIconDragData.isDragging ) {- endDrag( event );+ applyDrag( event );$(Hi.BASE_SVG_SELECTOR).attr( Hi.SVG_ACTION_STATE_ATTR_NAME, '');}$(Hi.BASE_SVG_SELECTOR).attr( Hi.SVG_ACTION_STATE_ATTR_NAME, '' );@@ -193,13 +195,9 @@}gIgnoreCLick = false;- if ( Hi.DEBUG ) {- console.log( `Click [${MODULE_NAME}]`, event );- Hi.displayElementInfo( 'Event Target', $(event.target) );- }-const enclosingSvgGroup = $(event.target).closest('g');if ( enclosingSvgGroup.length > 0 ) {+ console.log( `Click [${MODULE_NAME}]`, event );if ( Hi.DEBUG ) { console.log( 'SVG Target Element', enclosingSvgGroup ); }let svgDataType = $(enclosingSvgGroup).attr( Hi.DATA_TYPE_ATTR );const isSvgIcon = ( svgDataType == Hi.DATA_TYPE_ICON_VALUE );@@ -279,7 +277,10 @@clearSelectedIconSvgGroup();gSelectedIconSvgGroup = enclosingSvgGroup;$(enclosingSvgGroup).addClass( Hi.HIGHLIGHTED_CLASS );- Hi.edit.eventBus.emit( Hi.edit.SELECTION_MADE_EVENT_NAME, MODULE_NAME );+ let data = {+ moduleName: MODULE_NAME,+ };+ Hi.edit.eventBus.emit( Hi.edit.SELECTION_MADE_EVENT_NAME, data );AN.get( `${Hi.API_SHOW_DETAILS_URL}/${svgItemId}` );}@@ -359,7 +360,7 @@}}- function endDrag( event ) {+ function applyDrag( event ) {if ( gSvgIconDragData == null ) {return;}diff --git a/src/hi/static/js/edit-svg-path.js b/src/hi/static/js/edit-svg-path.jsindex 2760f6d9..708cddb8 100644--- a/src/hi/static/js/edit-svg-path.js+++ b/src/hi/static/js/edit-svg-path.js@@ -11,7 +11,8 @@this.clearSelection.bind( this ) );},clearSelection: function( data ) {- if ( data != MODULE_NAME ) {+ gCurrentSelectionModule = data.moduleName;+ if ( data.moduleName != MODULE_NAME ) {clearSelectedPathSvgGroup();}}@@ -87,6 +88,7 @@CLOSED: 'closed'};+ let gCurrentSelectionModule = null;let gSelectedPathSvgGroup = null;let gSvgPathEditData = null;let gIgnoreCLick = false; // Set by mouseup handling when no click handling should be done@@ -113,13 +115,9 @@}gIgnoreCLick = false;- if ( Hi.DEBUG ) {- console.log( `Click [${MODULE_NAME}]`, event );- Hi.displayElementInfo( 'Event Target', $(event.target) );- }-const enclosingSvgGroup = $(event.target).closest('g');if ( enclosingSvgGroup.length > 0 ) {+ console.log( `Click [${MODULE_NAME}]`, event );Hi.displayElementInfo( 'SVG Target Element', enclosingSvgGroup );let svgDataType = $(enclosingSvgGroup).attr( Hi.DATA_TYPE_ATTR );const isSvgIcon = ( svgDataType == Hi.DATA_TYPE_PATH_VALUE );@@ -159,7 +157,9 @@} else if ( PATH_ACTION_END_KEY_CODES.includes( event.keyCode ) ) {clearSelectedPathSvgGroup();-+ let data = { moduleName: null };+ Hi.edit.eventBus.emit( Hi.edit.SELECTION_MADE_EVENT_NAME, data );+} else {if ( ! gSvgPathEditData.selectedProxyElement ) {if ( Hi.DEBUG ) { console.log( `Key down skipped [${MODULE_NAME}]` ); }@@ -213,7 +213,10 @@clearSelectedPathSvgGroup();gSelectedPathSvgGroup = enclosingSvgGroup;expandSvgPath( enclosingSvgGroup );- Hi.edit.eventBus.emit( Hi.edit.SELECTION_MADE_EVENT_NAME, MODULE_NAME );+ let data = {+ moduleName: MODULE_NAME,+ };+ Hi.edit.eventBus.emit( Hi.edit.SELECTION_MADE_EVENT_NAME, data );AN.get( `${Hi.API_SHOW_DETAILS_URL}/${svgItemId}` );}diff --git a/src/hi/static/js/svg-location.js b/src/hi/static/js/svg-location.jsindex e7c3ae28..44365e3b 100644--- a/src/hi/static/js/svg-location.js+++ b/src/hi/static/js/svg-location.js@@ -11,7 +11,8 @@this.clearSelection.bind(this) );},clearSelection: function( data ) {- if ( data != MODULE_NAME ) {+ gCurrentSelectionModule = data.moduleName;+ if ( data.moduleName != MODULE_NAME ) {clearSelectedLocationViewSvg();}}@@ -34,6 +35,8 @@const CURSOR_MOVEMENT_THRESHOLD_PIXELS = 3; // Differentiate between move events and sloppy clicksconst PIXEL_MOVE_DISTANCE_SCALE_FACTOR = 500.0;const ZOOM_SCALE_FACTOR_PERCENT = 10.0;++ const API_EDIT_LOCATION_VIEW_GEOMETRY_URL = '/location/edit/location-view/geometry';const SvgTransformType = {MOVE: 'move',@@ -42,6 +45,7 @@};let gSvgTransformType = SvgTransformType.MOVE;+ let gCurrentSelectionModule = null;let gSelectedLocationViewSvg = null;let gSvgTransformData = null;let gLastMousePosition = { x: 0, y: 0 };@@ -67,28 +71,37 @@});function handleMouseDown( event ) {- if ( Hi.DEBUG ) { console.log( `Mouse down event [${MODULE_NAME}]`, event ); }-if ( gSelectedLocationViewSvg ) {+ if ( Hi.DEBUG ) { console.log( `Mouse down event [${MODULE_NAME}]`, event ); }createTransformData( event, gSelectedLocationViewSvg );event.preventDefault();event.stopImmediatePropagation();return;+ } else if ( $(event.target).hasClass( Hi.LOCATION_VIEW_SVG_CLASS ) ) {+ if ( gCurrentSelectionModule != 'svg-path' ) {+ if ( Hi.DEBUG ) { console.log( `Mouse down event [${MODULE_NAME}]`, event ); }+ handleLocationViewSvgClick( event, event.target );+ createTransformData( event, gSelectedLocationViewSvg );+ event.preventDefault();+ event.stopImmediatePropagation();+ return;+ }}+if ( Hi.DEBUG ) { console.log( `Mouse down skipped [${MODULE_NAME}]` ); }}function handleMouseUp( event ) {- if ( Hi.DEBUG ) { console.log( `Mouse up event [${MODULE_NAME}]`, event ); }if ( gSvgTransformData ) {+ if ( Hi.DEBUG ) { console.log( `Mouse up event [${MODULE_NAME}]`, event ); }if ( gSvgTransformType == SvgTransformType.SCALE ) {applyScale( event );} else if( gSvgTransformType == SvgTransformType.ROTATE ) {applyRotation( event );} else {if ( gSvgTransformData.isDragging ) {- endDrag( event );+ applyDrag( event );}}gSvgTransformData = null;@@ -142,10 +155,8 @@}gIgnoreCLick = false;- if ( Hi.DEBUG ) { console.log( `Click [${MODULE_NAME}]`, event ); }-if ( $(event.target).hasClass( Hi.LOCATION_VIEW_SVG_CLASS ) ) {- if ( Hi.DEBUG ) { console.log( 'SVG Target', event.target ); }+ if ( Hi.DEBUG ) { console.log( `Click [${MODULE_NAME}]`, event ); }handleLocationViewSvgClick( event, event.target );event.preventDefault();event.stopImmediatePropagation();@@ -236,7 +247,10 @@gSelectedLocationViewSvg = locationViewSvg;if ( Hi.isEditMode ) {- Hi.edit.eventBus.emit( Hi.edit.SELECTION_MADE_EVENT_NAME, MODULE_NAME );+ let data = {+ moduleName: MODULE_NAME,+ };+ Hi.edit.eventBus.emit( Hi.edit.SELECTION_MADE_EVENT_NAME, data );AN.get( `${Hi.API_SHOW_DETAILS_URL}` );}}@@ -272,17 +286,11 @@return;}- function endDrag( event ) {- if ( Hi.DEBUG ) { console.log( `endDrag [${MODULE_NAME}]` ); }+ function applyDrag( event ) {+ if ( Hi.DEBUG ) { console.log( `applyDrag [${MODULE_NAME}]` ); }if ( gSvgTransformData && gSvgTransformData.isDragging ) {gSvgTransformData.isDragging = false;- if ( Hi.isEditMode ) {- let data = {- view_box: $(gSelectedLocationViewSvg).attr('viewBox'),- };-- // ZZZ TODO: AN.post( `${API_EDIT_LOCATION_VIEW_VIEWBOX_URL}/${locationViewId}`, data );- }+ saveSvgGeometryIfNeeded();}}@@ -291,12 +299,14 @@let scaleFactor = 1.0 / ( 1.0 + ( ZOOM_SCALE_FACTOR_PERCENT / 100.0 ));let initialSvgViewBox = Hi.getSvgViewBox( gSelectedLocationViewSvg );scaleSvgViewBox( initialSvgViewBox, scaleFactor );+ saveSvgGeometryIfNeeded();}function zoomOut( event ) {let scaleFactor = 1.0 + ( ZOOM_SCALE_FACTOR_PERCENT / 100.0 );let initialSvgViewBox = Hi.getSvgViewBox( gSelectedLocationViewSvg );scaleSvgViewBox( initialSvgViewBox, scaleFactor );+ saveSvgGeometryIfNeeded();}function startScale( event ) {@@ -335,13 +345,9 @@function applyScale( event ) {if ( Hi.DEBUG ) { console.log( `applyScale [${MODULE_NAME}]` ); }-- // ZZZ---gSvgTransformType = SvgTransformType.MOVE;$(gSelectedLocationViewSvg).attr( Hi.SVG_ACTION_STATE_ATTR_NAME, '' );+ saveSvgGeometryIfNeeded();}function abortScale( event ) {@@ -386,15 +392,9 @@function applyRotation( event ) {if ( Hi.DEBUG ) { console.log( `applyRotation [${MODULE_NAME}]` ); }---- // ZZZ--gSvgTransformType = SvgTransformType.MOVE;$(gSelectedLocationViewSvg).attr( Hi.SVG_ACTION_STATE_ATTR_NAME, '' );-+ saveSvgGeometryIfNeeded();}function abortRotation( event ) {@@ -497,5 +497,24 @@let newY = point.x * Math.sin(radians) + point.y * Math.cos(radians);return { x: newX, y: newY };}++ function saveSvgGeometryIfNeeded( ) {+ if ( ! Hi.isEditMode || ! gSelectedLocationViewSvg ) {+ return;+ }+ if ( Hi.DEBUG ) { console.log( `Saving SVG geometry [${MODULE_NAME}]` ); }++ let locationViewId = $(gSelectedLocationViewSvg).attr('location-view-id');++ let svgViewBoxStr = $(gSelectedLocationViewSvg).attr('viewBox');+ let transform = $(gSelectedLocationViewSvg).attr('transform');+ let { scale, translate, rotate } = Hi.getSvgTransformValues( transform );+ let data = {+ view_box: svgViewBoxStr,+ rotate_angle: rotate.angle+ };++ AN.post( `${API_EDIT_LOCATION_VIEW_GEOMETRY_URL}/${locationViewId}`, data );+ }})();