Home Information - Your home, your data, organized.
Python
Finished pan, zoom, rotate for background SVG.
Anthony Cassandra4dc3cabeedad
diff --git a/src/hi/apps/collection/edit/views.py b/src/hi/apps/collection/edit/views.pyindex 36ce3c2b..95fa084c 100644--- a/src/hi/apps/collection/edit/views.py+++ b/src/hi/apps/collection/edit/views.py@@ -25,6 +25,42 @@ from .helpers import CollectionEditHelperslogger = logging.getLogger(__name__)+@method_decorator( edit_required, name='dispatch' )+class CollectionDetailsView( View ):++ def get( self, request, *args, **kwargs ):+ collection_id = kwargs.get( 'collection_id' )+ if not collection_id:+ return bad_request_response( request, message = 'Missing collection id in request.' )+ try:+ collection = Collection.objects.get( id = collection_id )+ except Collection.DoesNotExist:+ return page_not_found_response( request )++ location_view = request.view_parameters.location_view++ svg_position_form = None+ if request.view_parameters.view_type.is_location_view:+ collection_position = CollectionPosition.objects.filter(+ collection = collection,+ location = location_view.location,+ ).first()+ if collection_position:+ svg_position_form = SvgPositionForm.from_svg_position_model( collection_position )++ context = {+ 'collection': collection,+ 'svg_position_form': svg_position_form,+ }+ template = get_template( 'collection/edit/panes/collection_details.html' )+ content = template.render( context, request = request )+ return antinode.response(+ insert_map = {+ DIVID['EDIT_ITEM']: content,+ },+ )++@method_decorator( edit_required, name='dispatch' )class CollectionAddView( View ):@@ -119,42 +155,6 @@ class CollectionDeleteView( View ):return redirect( redirect_url )-@method_decorator( edit_required, name='dispatch' )-class CollectionDetailsView( View ):-- def get( self, request, *args, **kwargs ):- collection_id = kwargs.get( 'collection_id' )- if not collection_id:- return bad_request_response( request, message = 'Missing collection id in request.' )- try:- collection = Collection.objects.get( id = collection_id )- except Collection.DoesNotExist:- return page_not_found_response( request )-- location_view = request.view_parameters.location_view-- svg_position_form = None- if request.view_parameters.view_type.is_location_view:- collection_position = CollectionPosition.objects.filter(- collection = collection,- location = location_view.location,- ).first()- if collection_position:- svg_position_form = SvgPositionForm.from_svg_position_model( collection_position )-- context = {- 'collection': collection,- 'svg_position_form': svg_position_form,- }- template = get_template( 'collection/edit/panes/collection_details.html' )- content = template.render( context, request = request )- return antinode.response(- insert_map = {- DIVID['EDIT_ITEM']: content,- },- )--@method_decorator( edit_required, name='dispatch' )class CollectionAddRemoveItemView( View ):diff --git a/src/hi/apps/edit/templates/edit/panes/side.html b/src/hi/apps/edit/templates/edit/panes/side.htmlindex f3486722..900e7a1e 100644--- a/src/hi/apps/edit/templates/edit/panes/side.html+++ b/src/hi/apps/edit/templates/edit/panes/side.html@@ -18,9 +18,11 @@<div id="hi-edit-item">{% if entity %}- {% include "edit/panes/entity_details.html" %}+ {% include "entity/edit/panes/entity_details.html" %}{% elif collection %}- {% include "edit/panes/details_collection.html" %}+ {% include "collection/edit/panes/collection_details.html" %}+ {% elif request.view_parameters.view_type.is_location_view %}+ {% include "location/edit/panes/location_view_details.html" %}{% else %}{% include "edit/panes/default.html" %}{% endif %}diff --git a/src/hi/apps/edit/views.py b/src/hi/apps/edit/views.pyindex 2ec2a344..752b2bb4 100644--- a/src/hi/apps/edit/views.py+++ b/src/hi/apps/edit/views.py@@ -133,6 +133,12 @@ class EditDetailsView( View, EditViewMixin ):return bad_request_response( request, message = 'Unknown item type "{item_type}".' )def get_default_details( self, request ):+ if request.view_parameters.view_type.is_location_view:+ return location_edit_views.LocationViewDetailsView().get(+ request = request,+ location_view_id = request.view_parameters.location_view_id,+ )+return self.get_edit_side_panel_response(request = request,template_name = 'edit/panes/default.html',diff --git a/src/hi/apps/location/edit/views.py b/src/hi/apps/location/edit/views.pyindex 33679fdd..ad961175 100644--- a/src/hi/apps/location/edit/views.py+++ b/src/hi/apps/location/edit/views.py@@ -14,7 +14,7 @@ from hi.apps.location.location_view_manager import LocationViewManagerfrom hi.apps.location.models import LocationViewfrom hi.decorators import edit_requiredfrom hi.enums import ViewType-from hi.views import bad_request_response+from hi.views import bad_request_response, page_not_found_responsefrom hi.constants import DIVID@@ -25,6 +25,33 @@ from .helpers import LocationEditHelperslogger = logging.getLogger(__name__)+@method_decorator( edit_required, name='dispatch' )+class LocationViewDetailsView( View ):++ def get( self, request, *args, **kwargs ):+ location_view_id = kwargs.get( 'location_view_id' )+ if not location_view_id:+ return bad_request_response( request, message = 'Missing location_view id in request.' )+ try:+ location_view = LocationView.objects.get( id = location_view_id )+ except LocationView.DoesNotExist:+ return page_not_found_response( request )++ location_view_data = LocationViewManager().get_location_view_data(+ location_view = location_view,+ )+ context = {+ 'location_view_data': location_view_data,+ }+ template = get_template( 'location/edit/panes/location_view_details.html' )+ content = template.render( context, request = request )+ return antinode.response(+ insert_map = {+ DIVID['EDIT_ITEM']: content,+ },+ )++@method_decorator( edit_required, name='dispatch' )class LocationViewAddView( View ):diff --git a/src/hi/apps/location/templates/location/edit/panes/location_view_details.html b/src/hi/apps/location/templates/location/edit/panes/location_view_details.htmlnew file mode 100644index 00000000..26b9223b--- /dev/null+++ b/src/hi/apps/location/templates/location/edit/panes/location_view_details.html@@ -0,0 +1,16 @@+<h5>Location</h5>+<div>{{ location_view_data.location_view.location.name }}</div>+<h5>Location View</h5>+<div>{{ location_view_data.location_view.name }}</div>++<h5>View Type</h5>+<div>{{ location_view_data.location_view.location_view_type }}</div>++<h5>View Box</h5>+<div>{{ location_view_data.location_view.svg_view_box }}</div>++<h5>Rotate</h5>+<div>{{ location_view_data.location_view.svg_rotate }}</div>++<h5>Order Index</h5>+<div>{{ location_view_data.location_view.order_id }}</div>diff --git a/src/hi/apps/location/templates/location/location_view.html b/src/hi/apps/location/templates/location/location_view.htmlindex 9ec50d40..5ed4b6b4 100644--- a/src/hi/apps/location/templates/location/location_view.html+++ b/src/hi/apps/location/templates/location/location_view.html@@ -4,6 +4,7 @@class="hi-location-view"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 }}"width="100%" height="100%" preserveAspectRatio="xMidYMid meet"transform="rotate({{ location_view_data.location_view.svg_rotate }} 0 0)">{% include_media_template location_view_data.location_view.location.svg_filename %}diff --git a/src/hi/static/js/edit-svg-icon.js b/src/hi/static/js/edit-svg-icon.jsindex 23790377..30304e9b 100644--- a/src/hi/static/js/edit-svg-icon.js+++ b/src/hi/static/js/edit-svg-icon.js@@ -459,6 +459,18 @@adjustIconScale( gSvgIconEditData.element, scaleFactor );}+ function iconActionScaleApply() {+ if ( Hi.DEBUG ) { console.log( 'Scale Apply' ); }+ saveIconSvgPosition( gSvgIconEditData.element );+ }++ function iconActionScaleAbort() {+ if ( gSvgIconActionState != SvgActionStateType.SCALE ) {+ return;+ }+ revertIconAction();+ }+function adjustIconScale( svgIconElement, scaleFactor ) {let transform = svgIconElement.attr('transform');let { scale, translate, rotate } = Hi.getSvgTransformValues( transform );@@ -480,18 +492,6 @@setSvgTransformAttr( svgIconElement, newScale, translate, rotate );}- function iconActionScaleApply() {- if ( Hi.DEBUG ) { console.log( 'Scale Apply' ); }- saveIconSvgPosition( gSvgIconEditData.element );- }-- function iconActionScaleAbort() {- if ( gSvgIconActionState != SvgActionStateType.SCALE ) {- return;- }- revertIconAction();- }-function iconActionRotateStart() {createIconEditActionData( SvgActionStateType.ROTATE );}diff --git a/src/hi/static/js/main.js b/src/hi/static/js/main.jsindex 676ae6c4..1f7c0941 100644--- a/src/hi/static/js/main.js+++ b/src/hi/static/js/main.js@@ -30,6 +30,9 @@getSvgViewBox: function( svgElement ) {return _getSvgViewBox( svgElement );},+ getExtentsSvgViewBox: function( svgElement ) {+ return _getExtentsSvgViewBox( svgElement );+ },getSvgCenterPoint: function( element, svgElement ) {return _getSvgCenterPoint( element, svgElement );},@@ -103,7 +106,7 @@return (angle % 360 + 360) % 360;}- function _getSvgViewBox( svgElement ) {+ function _getSvgViewBox( svgElement, attrName = 'viewBox' ) {let x = null;let y = null;let width = null;@@ -112,7 +115,7 @@if (svgElement.length < 1 ) {return { x, y, width, height };}- let viewBoxValue = $(svgElement).attr('viewBox');+ let viewBoxValue = $(svgElement).attr( attrName );if ( ! viewBoxValue) {return { x, y, width, height };}@@ -125,6 +128,11 @@return { x, y, width, height };}++ function _getExtentsSvgViewBox( svgElement ) {+ // Hi-specific attribute used for pan, zoom and edit operations.+ return _getSvgViewBox( svgElement, 'extents' );+ }function _getSvgCenterPoint( element, svgElement ) {diff --git a/src/hi/static/js/svg-location.js b/src/hi/static/js/svg-location.jsindex c194ae25..e7c3ae28 100644--- a/src/hi/static/js/svg-location.js+++ b/src/hi/static/js/svg-location.js@@ -81,13 +81,15 @@function handleMouseUp( event ) {if ( Hi.DEBUG ) { console.log( `Mouse up event [${MODULE_NAME}]`, event ); }- if ( gSvgTransformData && gSvgTransformData.isDragging ) {+ if ( gSvgTransformData ) {if ( gSvgTransformType == SvgTransformType.SCALE ) {applyScale( event );} else if( gSvgTransformType == SvgTransformType.ROTATE ) {applyRotation( event );} else {- endDrag( event );+ if ( gSvgTransformData.isDragging ) {+ endDrag( event );+ }}gSvgTransformData = null;gSvgTransformType = SvgTransformType.MOVE;@@ -217,8 +219,8 @@x: event.clientX,y: event.clientY,},- originalSvgRotate: rotate,- originalSvgViewBox: svgViewBox+ initialSvgRotate: rotate,+ initialSvgViewBox: svgViewBox};}@@ -253,16 +255,21 @@x: ( event.clientX - gSvgTransformData.clickStart.x ) / pixelsPerSvgUnit.scaleX,y: ( event.clientY - gSvgTransformData.clickStart.y ) / pixelsPerSvgUnit.scaleX};++ let transform = $(gSelectedLocationViewSvg).attr('transform');+ let { scale, translate, rotate } = Hi.getSvgTransformValues( transform );+ deltaSvgUnits = rotateVector( deltaSvgUnits, -1.0 * rotate.angle );- let svgViewBox = {- x: gSvgTransformData.originalSvgViewBox.x - deltaSvgUnits.x,- y: gSvgTransformData.originalSvgViewBox.y - deltaSvgUnits.y,- width: gSvgTransformData.originalSvgViewBox.width,- height: gSvgTransformData.originalSvgViewBox.height- };-- let svgViewBoxStr = `${svgViewBox.x} ${svgViewBox.y} ${svgViewBox.width} ${svgViewBox.height}`;- $(gSelectedLocationViewSvg).attr('viewBox', svgViewBoxStr );+ let newX = gSvgTransformData.initialSvgViewBox.x - deltaSvgUnits.x;+ let newY = gSvgTransformData.initialSvgViewBox.y - deltaSvgUnits.y;++ adjustSvgViewBox( gSvgTransformData.initialSvgViewBox,+ gSvgTransformData.initialSvgViewBox.width,+ gSvgTransformData.initialSvgViewBox.height,+ newX,+ newY );++ return;}function endDrag( event ) {@@ -282,14 +289,14 @@function zoomIn( event ) {let scaleFactor = 1.0 / ( 1.0 + ( ZOOM_SCALE_FACTOR_PERCENT / 100.0 ));- let originalSvgViewBox = Hi.getSvgViewBox( gSelectedLocationViewSvg );- adjustSvgViewBox( originalSvgViewBox, scaleFactor );+ let initialSvgViewBox = Hi.getSvgViewBox( gSelectedLocationViewSvg );+ scaleSvgViewBox( initialSvgViewBox, scaleFactor );}function zoomOut( event ) {let scaleFactor = 1.0 + ( ZOOM_SCALE_FACTOR_PERCENT / 100.0 );- let originalSvgViewBox = Hi.getSvgViewBox( gSelectedLocationViewSvg );- adjustSvgViewBox( originalSvgViewBox, scaleFactor );+ let initialSvgViewBox = Hi.getSvgViewBox( gSelectedLocationViewSvg );+ scaleSvgViewBox( initialSvgViewBox, scaleFactor );}function startScale( event ) {@@ -321,31 +328,11 @@+ ( endVector.y * endVector.y ));const vectorLengthDelta = endVectorLength - startVectorLength;- let scale = ( 1.0 - ( vectorLengthDelta / PIXEL_MOVE_DISTANCE_SCALE_FACTOR ));+ let scaleFactor = ( 1.0 - ( vectorLengthDelta / PIXEL_MOVE_DISTANCE_SCALE_FACTOR ));- adjustSvgViewBox( scale, gSvgTransformData.originalSvgViewBox );+ scaleSvgViewBox( gSvgTransformData.initialSvgViewBox, scaleFactor );}- function adjustSvgViewBox( originalSvgViewBox, scaleFactor ) {-- let svgViewBox = {- x: originalSvgViewBox.x,- y: originalSvgViewBox.y,- width: scaleFactor * originalSvgViewBox.width,- height: scaleFactor * originalSvgViewBox.height- };-- let svgOffset = {- x: ( originalSvgViewBox.width - svgViewBox.width ) / 2.0,- y: ( originalSvgViewBox.height - svgViewBox.height ) / 2.0- };- svgViewBox.x += svgOffset.x;- svgViewBox.y += svgOffset.y;-- let svgViewBoxStr = `${svgViewBox.x} ${svgViewBox.y} ${svgViewBox.width} ${svgViewBox.height}`;- $(gSelectedLocationViewSvg).attr('viewBox', svgViewBoxStr );- }-function applyScale( event ) {if ( Hi.DEBUG ) { console.log( `applyScale [${MODULE_NAME}]` ); }@@ -364,6 +351,14 @@$(gSelectedLocationViewSvg).attr( Hi.SVG_ACTION_STATE_ATTR_NAME, '' );}+ function scaleSvgViewBox( initialSvgViewBox, scaleFactor ) {++ let newWidth = scaleFactor * initialSvgViewBox.width;+ let newHeight = scaleFactor * initialSvgViewBox.height;+ adjustSvgViewBox( initialSvgViewBox, newWidth, newHeight );+ return;+ }+function startRotation( event ) {if ( Hi.DEBUG ) { console.log( `startRotation [${MODULE_NAME}]` ); }gSvgTransformType = SvgTransformType.ROTATE;@@ -409,6 +404,98 @@$(gSelectedLocationViewSvg).attr( Hi.SVG_ACTION_STATE_ATTR_NAME, '' );}+ function adjustSvgViewBox( initialSvgViewBox, newWidth, newHeight, newX = null, newY = null ) {++ // Need to account for possible SVG rotation+ let transform = $(gSelectedLocationViewSvg).attr('transform');+ let { scale, translate, rotate } = Hi.getSvgTransformValues( transform );++ // Adjust the dimensions to fill as much as the screen as possible+ let containerRect = $(Hi.LOCATION_VIEW_AREA_SELECTOR)[0].getBoundingClientRect();+ let containerAspectRatio = containerRect.width / containerRect.height;+ let newAspectRatio = newWidth / newHeight;++ if ( newAspectRatio > containerAspectRatio ) {+ newHeight = newWidth / containerAspectRatio;+ } else if ( newAspectRatio < containerAspectRatio ) {+ newWidth = newHeight * containerAspectRatio;+ }++ // Ensure the new viewBox is clamped within the extents of the SVG+ let extentsSvgViewBox = Hi.getExtentsSvgViewBox( gSelectedLocationViewSvg );++ extentsSvgViewBox = calculateRotatedRectangle( extentsSvgViewBox, rotate.angle );++ newWidth = Math.min( newWidth, extentsSvgViewBox.width );+ newHeight = Math.min( newHeight, extentsSvgViewBox.height );++ // Center the new viewBox within the initial viewBox (if no explicit newX/newY)+ if ( ! newX ) {+ newX = initialSvgViewBox.x + ( initialSvgViewBox.width - newWidth ) / 2.0;+ }+ if ( ! newY ) {+ newY = initialSvgViewBox.y + ( initialSvgViewBox.height - newHeight ) / 2.0;+ }++ if (newX < extentsSvgViewBox.x) {+ newX = extentsSvgViewBox.x;+ }+ if (newY < extentsSvgViewBox.y) {+ newY = extentsSvgViewBox.y;+ }+ if (( newX + newWidth ) > ( extentsSvgViewBox.x + extentsSvgViewBox.width )) {+ newX = extentsSvgViewBox.x + extentsSvgViewBox.width - newWidth;+ }+ if (( newY + newHeight ) > ( extentsSvgViewBox.y + extentsSvgViewBox.height )) {+ newY = extentsSvgViewBox.y + extentsSvgViewBox.height - newHeight;+ }++ let svgViewBoxStr = `${newX} ${newY} ${newWidth} ${newHeight}`;+ $(gSelectedLocationViewSvg).attr('viewBox', svgViewBoxStr );+ }+ function calculateRotatedRectangle( initialRect, rotationAngle ) {++ let corners = [+ { x: initialRect.x, y: initialRect.y },+ { x: initialRect.x + initialRect.width, y: initialRect.y },+ { x: initialRect.x, y: initialRect.y + initialRect.height },+ { x: initialRect.x + initialRect.width, y: initialRect.y + initialRect.height }+ ];++ // Rotate the corners around the center of the viewBox+ let centerX = initialRect.x + ( initialRect.width / 2.0 );+ let centerY = initialRect.y + ( initialRect.height / 2.0 );+ let radians = (Math.PI / 180) * rotationAngle;++ let rotatedCorners = corners.map((corner) => {+ let dx = corner.x - centerX;+ let dy = corner.y - centerY;+ return {+ x: centerX + (dx * Math.cos(radians) - dy * Math.sin(radians)),+ y: centerY + (dx * Math.sin(radians) + dy * Math.cos(radians)),+ };+ });++ // Calculate the new bounding box from the rotated corners+ let minX = Math.min(...rotatedCorners.map(c => c.x));+ let minY = Math.min(...rotatedCorners.map(c => c.y));+ let maxX = Math.max(...rotatedCorners.map(c => c.x));+ let maxY = Math.max(...rotatedCorners.map(c => c.y));++ return {+ x: minX,+ y: minY,+ width: maxX - minX,+ height: maxY - minY,+ };+ }++ function rotateVector( point, rotationAngle ) {+ let radians = (Math.PI / 180) * rotationAngle;+ let newX = point.x * Math.cos(radians) - point.y * Math.sin(radians);+ let newY = point.x * Math.sin(radians) + point.y * Math.cos(radians);+ return { x: newX, y: newY };+ }})();