Home Information - Your home, your data, organized.
Python
Added entity delete and some Javascript fixes/cleanup.
Anthony Cassandraf12cee68b45e
diff --git a/src/hi/apps/entity/edit/views.py b/src/hi/apps/entity/edit/views.pyindex 622da328..782971fd 100644--- a/src/hi/apps/entity/edit/views.py+++ b/src/hi/apps/entity/edit/views.py@@ -14,7 +14,7 @@ from hi.apps.location.forms import SvgPositionFormfrom hi.constants import DIVIDfrom hi.decorators import edit_required-from hi.views import bad_request_response, page_not_found_response+from hi.views import bad_request_response, not_authorized_response, page_not_found_responsefrom . import forms@@ -111,6 +111,9 @@ class EntityDeleteView( View ):except Entity.DoesNotExist:return page_not_found_response( request )+ if not entity.can_user_delete:+ return not_authorized_response( request, message = 'This entity cannot be deleted.' )+context = {'entity': entity,}@@ -121,10 +124,22 @@ class EntityDeleteView( View ):)def post( self, request, *args, **kwargs ):+ action = request.POST.get( 'action' )+ if action != 'confirm':+ return bad_request_response( request, message = 'Missing confirmation value.' )+entity_id = kwargs.get( 'entity_id' )if not entity_id:- return bad_request_response( request, message = 'Missing entity id in request.' )-+ return bad_request_response( request, message = 'Missing entity id.' )+ try:+ entity = Entity.objects.get( id = entity_id )+ except Entity.DoesNotExist:+ return page_not_found_response( request )+ if not entity.can_user_delete:+ return not_authorized_response( request, message = 'This entity cannot be deleted.' )++ entity.delete()- pass+ redirect_url = reverse('home')+ return redirect( redirect_url )diff --git a/src/hi/apps/entity/templates/entity/edit/modals/entity_delete.html b/src/hi/apps/entity/templates/entity/edit/modals/entity_delete.htmlnew file mode 100644index 00000000..9c517ef4--- /dev/null+++ b/src/hi/apps/entity/templates/entity/edit/modals/entity_delete.html@@ -0,0 +1,30 @@+{% extends "modals/action_with_cancel.html" %}++{% block title_text %}+Delete Entity?+{% endblock %}++{% block body %}++<div>+ Please confirm that you want to delete the entity:+ <ul>+ {{ entity.name }}+ </ul>+</div>+<div>+ This action cannot be undone.+</div>+{% endblock %}++{% block action_right %}+<form action="{% url 'entity_edit_entity_delete' entity_id=entity.id %}"+ method="post" class="form"+ data-async="true" >+ {% csrf_token %}+ <button id="hi-delete-entity-button" type="submit" class="btn btn-lg btn-danger"+ name="action" value="confirm">+ DELETE+ </button>+</form>+{% endblock %}diff --git a/src/hi/apps/location/templates/location/location_view.html b/src/hi/apps/location/templates/location/location_view.htmlindex 33a7b8aa..9ec50d40 100644--- a/src/hi/apps/location/templates/location/location_view.html+++ b/src/hi/apps/location/templates/location/location_view.html@@ -1,6 +1,7 @@{% load common_template_helpers %}<div id="hi-location-view-main" class="hi-location-view"><svg id="{{ location_view_data.location_view.html_id }}"+ class="hi-location-view"xmlns="http://www.w3.org/2000/svg"viewBox="{{ location_view_data.location_view.svg_view_box }}"width="100%" height="100%" preserveAspectRatio="xMidYMid meet"diff --git a/src/hi/static/js/edit-svg-icon.js b/src/hi/static/js/edit-svg-icon.jsindex 91cfaba6..aaf2a1e7 100644--- a/src/hi/static/js/edit-svg-icon.js+++ b/src/hi/static/js/edit-svg-icon.js@@ -50,7 +50,7 @@let gSvgIconActionState = SvgActionStateType.MOVE;let gSelectedIconSvgGroup = null;let gSvgIconDragData = null;- let gSvgIconActionData = null;+ let gSvgIconEditData = null;let gClickStart = null;let gClickTimeout = null;@@ -83,10 +83,7 @@});function handleMouseDown( event ) {- if ( Hi.DEBUG ) {- Hi.displayEventInfo( 'Mouse down event: ', event );- Hi.displayElementInfo( 'Event target: ', $(event.target) );- }+ if ( Hi.DEBUG ) { Hi.displayEventInfo( `Mouse down event [${MODULE_NAME}]`, event ); }// Need to track start time to differentiate drag/scale/rotate actions from regular clicks.gClickStart = {@@ -95,57 +92,59 @@time: Date.now()};- if ( gSvgIconActionState == SvgActionStateType.MOVE ) {-- const enclosingSvgGroup = $(event.target).closest('g');- if ( enclosingSvgGroup.length < 1 ) {- return;- }- const svgDataType = $(enclosingSvgGroup).attr( Hi.DATA_TYPE_ATTR );- if ( svgDataType == Hi.DATA_TYPE_ICON_VALUE ) {- createIconDragData( event, enclosingSvgGroup );- }- event.preventDefault();- return;- }- if ( gSvgIconActionData ) {+ if ( gSvgIconEditData ) {if ( gSvgIconActionState == SvgActionStateType.SCALE ) {- gSvgIconActionData.isScaling = true;- event.preventDefault();+ gSvgIconEditData.isScaling = true;} else if ( gSvgIconActionState == SvgActionStateType.ROTATE ) {- gSvgIconActionData.isRotating = true;- event.preventDefault();+ gSvgIconEditData.isRotating = true;+ }+ event.preventDefault();+ 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 ) {+ createIconDragData( event, enclosingSvgGroup );+ event.preventDefault();+ return;+ }}}+ if ( Hi.DEBUG ) { console.log( `Mouse down skipped [${MODULE_NAME}]` ); }}function handleMouseUp( event ) {+ if ( Hi.DEBUG ) { Hi.displayEventInfo( `Mouse up event [${MODULE_NAME}]`, event ); }+if ( gSvgIconDragData ) {if ( gSvgIconDragData.isDragging ) {endDrag( event );$(Hi.BASE_SVG_SELECTOR).attr( ICON_ACTION_STATE_ATTR_NAME, '');- event.preventDefault();- return;}$(Hi.BASE_SVG_SELECTOR).attr( ICON_ACTION_STATE_ATTR_NAME, '' );gSvgIconDragData = null;+ event.preventDefault();+ return;}- else if ( gSvgIconActionData ) {+ else if ( gSvgIconEditData ) {if ( gSvgIconActionState == SvgActionStateType.SCALE ) {- gSvgIconActionData.isScaling = false;+ gSvgIconEditData.isScaling = false;iconActionScaleApply();} else if ( gSvgIconActionState == SvgActionStateType.ROTATE ) {- gSvgIconActionData.isRotating = false;+ gSvgIconEditData.isRotating = false;iconActionRotateApply();}gSvgIconActionState = SvgActionStateType.MOVE;$(Hi.BASE_SVG_SELECTOR).attr( ICON_ACTION_STATE_ATTR_NAME, '');- gSvgIconActionData = null;+ gSvgIconEditData = null;gIgnoreCLick = true;event.preventDefault();-+ return;}+ if ( Hi.DEBUG ) { console.log( `Mouse up skipped [${MODULE_NAME}]` ); }}function handleMouseMove( event ) {@@ -168,11 +167,11 @@event.preventDefault();}}- else if ( gSvgIconActionData ) {- if ( gSvgIconActionData.isScaling ) {+ else if ( gSvgIconEditData ) {+ if ( gSvgIconEditData.isScaling ) {iconActionScaleUpdate( currentMousePosition );event.preventDefault();- } else if ( gSvgIconActionData.isRotating ) {+ } else if ( gSvgIconEditData.isRotating ) {iconActionRotateUpdate( currentMousePosition );event.preventDefault();}@@ -183,19 +182,21 @@function handleClick( event ) {if ( gIgnoreCLick ) {- if ( Hi.DEBUG ) { console.log( 'Ignoring click', event ); }+ if ( Hi.DEBUG ) { console.log( `Ignoring click [${MODULE_NAME}]`, event ); }gIgnoreCLick = false;event.preventDefault();return;}gIgnoreCLick = false;- Hi.displayEventInfo( 'Click', event );- Hi.displayElementInfo( 'Event Target', $(event.target) );-+ 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 ) {- Hi.displayElementInfo( 'SVG Target Element', enclosingSvgGroup );+ 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 );const svgItemId = enclosingSvgGroup.attr('id');@@ -205,6 +206,7 @@return;}}+ if ( Hi.DEBUG ) { console.log( `Click skipped [${MODULE_NAME}]` ); }}function handleKeyDown( event ) {@@ -213,8 +215,10 @@}if ( gSelectedIconSvgGroup ) {handleSvgIconSelectedKeyDown( event );- event.preventDefault();+ event.preventDefault();+ return;}+ if ( Hi.DEBUG ) { console.log( `Key down skipped [${MODULE_NAME}]` ); }}function handleSvgIconSelectedKeyDown( event ) {@@ -228,7 +232,7 @@gLastMousePosition.y >= targetOffset.top &&gLastMousePosition.y <= targetOffset.top + targetHeight) {- Hi.displayEventInfo( 'Key Down', event );+ if ( Hi.DEBUG ) { Hi.displayEventInfo( 'Key Down', event ); }if ( event.key == ICON_ACTION_SCALE_KEY ) {iconActionRotateAbort();@@ -381,7 +385,7 @@let transform = gSelectedIconSvgGroup.attr('transform');let { scale, translate, rotate } = getSvgTransformValues( transform );- gSvgIconActionData = {+ gSvgIconEditData = {element: gSelectedIconSvgGroup,scaleStart: scale,translateStart: translate,@@ -396,12 +400,12 @@}function revertIconAction( element ) {- if ( gSvgIconActionData ) {- setSvgTransformAttr( gSvgIconActionData.element,- gSvgIconActionData.scaleStart,- gSvgIconActionData.translateStart,- gSvgIconActionData.rotateStart );- gSvgIconActionData = null;+ if ( gSvgIconEditData ) {+ setSvgTransformAttr( gSvgIconEditData.element,+ gSvgIconEditData.scaleStart,+ gSvgIconEditData.translateStart,+ gSvgIconEditData.rotateStart );+ gSvgIconEditData = null;}}@@ -411,12 +415,12 @@function iconActionScaleUpdate( currentMousePosition ) {- let center = getScreenCenterPoint( gSvgIconActionData.element );+ let center = getScreenCenterPoint( gSvgIconEditData.element );let scaleFactor = getScaleFactor( center.x, center.y,gLastMousePosition.x, gLastMousePosition.y,currentMousePosition.x, currentMousePosition.y );- let transform = gSvgIconActionData.element.attr('transform');+ let transform = gSvgIconEditData.element.attr('transform');let { scale, translate, rotate } = getSvgTransformValues( transform );const newScale = {@@ -433,13 +437,13 @@Scale = ${scale.x}, T = ${translate.x}, R = ${rotate.angle}` );}- setSvgTransformAttr( gSvgIconActionData.element, newScale, translate, rotate );+ setSvgTransformAttr( gSvgIconEditData.element, newScale, translate, rotate );}function iconActionScaleApply() {if ( Hi.DEBUG ) { console.log( 'Scale Apply' ); }- saveIconSvgPosition( gSvgIconActionData.element );+ saveIconSvgPosition( gSvgIconEditData.element );}function iconActionScaleAbort() {@@ -456,17 +460,17 @@function iconActionRotateUpdate( currentMousePosition ) {if ( Hi.DEBUG ) { console.log( 'Rotate Update' ); }- let center = getScreenCenterPoint( gSvgIconActionData.element );+ let center = getScreenCenterPoint( gSvgIconEditData.element );let deltaAngle = getIconRotationAngle( center.x, center.y,gLastMousePosition.x, gLastMousePosition.y,currentMousePosition.x, currentMousePosition.y );- let transform = gSvgIconActionData.element.attr('transform');+ let transform = gSvgIconEditData.element.attr('transform');let { scale, translate, rotate } = getSvgTransformValues( transform );rotate.angle += deltaAngle;rotate.angle = normalizeAngle( rotate.angle );- setSvgTransformAttr( gSvgIconActionData.element, scale, translate, rotate );+ setSvgTransformAttr( gSvgIconEditData.element, scale, translate, rotate );}function getIconRotationAngle( centerX, centerY, startX, startY, endX, endY ) {@@ -496,7 +500,7 @@function iconActionRotateApply() {- saveIconSvgPosition( gSvgIconActionData.element );+ saveIconSvgPosition( gSvgIconEditData.element );}function iconActionRotateAbort() {diff --git a/src/hi/static/js/edit-svg-path.js b/src/hi/static/js/edit-svg-path.jsindex d12a3698..e57386c1 100644--- a/src/hi/static/js/edit-svg-path.js+++ b/src/hi/static/js/edit-svg-path.js@@ -78,6 +78,7 @@const PATH_EDIT_NEW_PATH_RADIUS_PERCENT = 5; // Preferrable if this matches server new path sizing.const PATH_EDIT_PROXY_LINE_COLOR = 'red';const PATH_EDIT_PROXY_POINT_COLOR = 'red';+ const CURSOR_MOVEMENT_THRESHOLD_PIXELS = 3;const API_EDIT_SVG_PATH_URL = '/edit/svg/path';@@ -103,17 +104,18 @@});function handleClick( event ) {- if ( Hi.DEBUG ) {- Hi.displayEventInfo( 'Path Edit Click', event );- Hi.displayElementInfo( 'Event Target', $(event.target) );- }if ( gSelectedPathSvgGroup && gIgnoreCLick ) {- if ( Hi.DEBUG ) { console.log( 'Ignoring click', event ); }+ if ( Hi.DEBUG ) { console.log( `Ignoring click [${MODULE_NAME}]`, event ); }gIgnoreCLick = false;event.preventDefault();return;}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 ) {@@ -129,11 +131,14 @@}if ( gSelectedPathSvgGroup ) {- handleProxyPathClick( event );- event.preventDefault();- return;+ const enclosingSvg = $(event.target).closest('svg');+ if ( $(enclosingSvg).hasClass( Hi.LOCATION_VIEW_SVG_CLASS ) ) {+ handleProxyPathClick( event );+ event.preventDefault();+ return;+ }}-+ if ( Hi.DEBUG ) { console.log( `Click skipped [${MODULE_NAME}]` ); }}function handleKeyDown( event ) {@@ -143,10 +148,8 @@if ( ! gSvgPathEditData ) {return;}+ if ( Hi.DEBUG ) { console.log( 'Key Down', event ); }- Hi.displayEventInfo( 'Key Down', event );- console.log( `KEY '${event.key}', CODE = ${event.keyCode}` );-if ( PATH_ACTION_ADD_KEY_CODES.includes( event.keyCode ) ) {addProxyPath();@@ -758,26 +761,46 @@// Drag logic for the proxy point$(proxyPoint).on('mousedown', function( event ) {event.preventDefault();++ let startMousePosition = {+ x: event.clientX,+ y: event.clientY+ };const offsetX = event.clientX - parseFloat($(proxyPoint).attr('cx'));const offsetY = event.clientY - parseFloat($(proxyPoint).attr('cy'));-++ let isDragging = false;+// Function to handle mouse movementfunction onMouseMove( event ) {- event.preventDefault();- gSvgPathEditData.dragProxyPoint = event.target;- const newCx = event.clientX - offsetX;- const newCy = event.clientY - offsetY;- $(proxyPoint).attr('cx', newCx).attr('cy', newCy);-- // Update the line endpoints to follow proxy point movement- if ( $(beforeProxyLine).length > 0 ) {- $(beforeProxyLine).attr('x2', newCx).attr('y2', newCy);- }- if ( $(afterProxyLine).length > 0 ) {- $(afterProxyLine).attr('x1', newCx).attr('y1', newCy);- }- setSelectedProxyElement(proxyPoint);+ let currentMousePosition = {+ x: event.clientX,+ y: event.clientY+ };+ const distanceX = Math.abs( currentMousePosition.x - startMousePosition.x );+ const distanceY = Math.abs( currentMousePosition.y - startMousePosition.y );++ if ( isDragging+ || ( distanceX > CURSOR_MOVEMENT_THRESHOLD_PIXELS )+ || ( distanceY > CURSOR_MOVEMENT_THRESHOLD_PIXELS )) {+ isDragging = true;+ event.preventDefault();+ gSvgPathEditData.dragProxyPoint = event.target;+ const newCx = event.clientX - offsetX;+ const newCy = event.clientY - offsetY;+ $(proxyPoint).attr('cx', newCx).attr('cy', newCy);++ // Update the line endpoints to follow proxy point movement+ if ( $(beforeProxyLine).length > 0 ) {+ $(beforeProxyLine).attr('x2', newCx).attr('y2', newCy);+ }+ if ( $(afterProxyLine).length > 0 ) {+ $(afterProxyLine).attr('x1', newCx).attr('y1', newCy);+ }++ setSelectedProxyElement(proxyPoint);+ }}// Function to handle mouse up (end of drag)@@ -785,7 +808,10 @@event.preventDefault();saveSvgPath();gSvgPathEditData.dragProxyPoint = null;- gIgnoreCLick = true;++ if ( isDragging ) {+ gIgnoreCLick = true;+ }$(document).off('mousemove', onMouseMove);$(document).off('mouseup', onMouseUp);}diff --git a/src/hi/static/js/main.js b/src/hi/static/js/main.jsindex 2b6d7d2e..f001b970 100644--- a/src/hi/static/js/main.js+++ b/src/hi/static/js/main.js@@ -4,6 +4,7 @@DEBUG: true,LOCATION_VIEW_AREA_SELECTOR: '#hi-location-view-main',+ LOCATION_VIEW_SVG_CLASS: 'hi-location-view',BASE_SVG_SELECTOR: '#hi-location-view-main > svg',HIGHLIGHTED_CLASS: 'highlighted',DATA_TYPE_ATTR: 'data-type',