Skip to content

Commit 2ef6887

Browse files
authored
Merge pull request #54 from cruse1977/0.5.0
0.5.0
2 parents c7eafe6 + fc1f971 commit 2ef6887

File tree

4 files changed

+87
-19
lines changed

4 files changed

+87
-19
lines changed

netbox_floorplan/static/netbox_floorplan/floorplan/edit.js

+47-9
Original file line numberDiff line numberDiff line change
@@ -453,9 +453,16 @@ window.center_pan_on_slected_object = center_pan_on_slected_object;
453453

454454
function update_background() {
455455
var assigned_image = document.getElementById("id_assigned_image").value;
456-
if (assigned_image == "") { assigned_image = null; }
456+
if (assigned_image == "") {
457+
assigned_image = null;
458+
canvas.setBackgroundImage(null, canvas.renderAll.bind(canvas));
459+
}
457460
var floor_json = canvas.toJSON(["id", "text", "_controlsVisibility", "custom_meta", "lockMovementY", "lockMovementX", "evented", "selectable"]);
458461

462+
463+
464+
465+
459466
$.ajax({
460467
type: "PATCH",
461468
url: `/api/plugins/floorplan/floorplans/${obj_pk}/`,
@@ -481,15 +488,45 @@ function update_background() {
481488
}
482489

483490
var img = fabric.Image.fromURL(img_url, function(img) {
484-
let scaleRatio = Math.max(canvas.width / img.width, canvas.height / img.height);
485-
canvas.setBackgroundImage(img, canvas.renderAll.bind(canvas), {
486-
scaleX: scaleRatio,
487-
scaleY: scaleRatio,
488-
left: canvas.width / 2,
489-
top: canvas.height / 2,
490-
originX: 'middle',
491-
originY: 'middle'
491+
492+
493+
var left = 0;
494+
var top = 0;
495+
var width = 0;
496+
var height = 0;
497+
canvas.getObjects().forEach(function (object) {
498+
if (object.custom_meta) {
499+
if (object.custom_meta.object_type == "floorplan_boundry") {
500+
left = object.left;
501+
top = object.top;
502+
width = object.width;
503+
height = object.height;
504+
}
505+
}
492506
});
507+
// if we have a floorplan boundary, position the image in there
508+
if (height != 0 && width != 0) {
509+
let scaleRatioX = Math.max(width / img.width)
510+
let scaleRatioY = Math.max(height / img.height);
511+
canvas.setBackgroundImage(img, canvas.renderAll.bind(canvas), {
512+
scaleX: scaleRatioX,
513+
scaleY: scaleRatioY,
514+
left: left,
515+
top: top
516+
});
517+
}
518+
else
519+
{
520+
let scaleRatio = Math.max(canvas.width / img.width, canvas.height / img.height);
521+
canvas.setBackgroundImage(img, canvas.renderAll.bind(canvas), {
522+
scaleX: scaleRatio,
523+
scaleY: scaleRatio,
524+
left: canvas.width / 2,
525+
top: canvas.height / 2,
526+
originX: 'middle',
527+
originY: 'middle'
528+
});
529+
}
493530
});
494531

495532
} else {
@@ -571,6 +608,7 @@ function update_dimensions() {
571608
centeredRotation: true,
572609
});
573610

611+
574612
var text = new fabric.IText(`${obj_name}`, {
575613
fontFamily: "Courier New",
576614
fontSize: 16,

netbox_floorplan/static/netbox_floorplan/floorplan/utils.js

+37-8
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,45 @@ function init_floor_plan(floorplan_id, canvas, mode) {
161161
img_url = floorplan.assigned_image.file;
162162
}
163163

164+
164165
var img = fabric.Image.fromURL(img_url, function(img) {
165-
let scaleRatio = Math.max(canvas.width / img.width, canvas.height / img.height);
166-
canvas.setBackgroundImage(img, canvas.renderAll.bind(canvas), {
167-
scaleX: scaleRatio,
168-
scaleY: scaleRatio,
169-
left: canvas.width / 2,
170-
top: canvas.height / 2,
171-
originX: 'middle',
172-
originY: 'middle'
166+
var left = 0;
167+
var top = 0;
168+
var width = 0;
169+
var height = 0;
170+
canvas.getObjects().forEach(function (object) {
171+
if (object.custom_meta) {
172+
if (object.custom_meta.object_type == "floorplan_boundry") {
173+
left = object.left;
174+
top = object.top;
175+
width = object.width;
176+
height = object.height;
177+
}
178+
}
173179
});
180+
// if we have a floorplan boundary, position the image in there
181+
if (height != 0 && width != 0) {
182+
let scaleRatioX = Math.max(width / img.width)
183+
let scaleRatioY = Math.max(height / img.height);
184+
canvas.setBackgroundImage(img, canvas.renderAll.bind(canvas), {
185+
scaleX: scaleRatioX,
186+
scaleY: scaleRatioY,
187+
left: left,
188+
top: top
189+
});
190+
}
191+
else
192+
{
193+
let scaleRatio = Math.max(canvas.width / img.width, canvas.height / img.height);
194+
canvas.setBackgroundImage(img, canvas.renderAll.bind(canvas), {
195+
scaleX: scaleRatio,
196+
scaleY: scaleRatio,
197+
left: canvas.width / 2,
198+
top: canvas.height / 2,
199+
originX: 'middle',
200+
originY: 'middle'
201+
});
202+
}
174203
});
175204

176205

netbox_floorplan/static/netbox_floorplan/vendors/htmx.min.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

netbox_floorplan/templates/netbox_floorplan/floorplan_edit.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
{{ block.super }}
99
<script src="{% static 'netbox_floorplan/vendors/fabric-js-6.0.2.js' %}"></script>
1010
<script src="{% static 'netbox_floorplan/vendors/jquery-3.7.1.js' %}"></script>
11-
11+
<script src="{% static 'netbox_floorplan/vendors/htmx.min.js' %}"></script>
1212

1313
{% endblock head %}
1414

@@ -251,5 +251,5 @@ <h5 class="modal-title" id="control_unit_modal_label">Set Floorplan Background I
251251
</div>
252252

253253
<script type="module" src="{% static 'netbox_floorplan/floorplan/edit.js' %}"></script>
254-
<script src="https://unpkg.com/[email protected]" integrity="sha384-ujb1lZYygJmzgSwoxRggbCHcjc0rB2XoQrxeTUQyRjrOnlCoYta87iKBWq3EsdM2" crossorigin="anonymous"></script>
254+
255255
{% endblock content %}

0 commit comments

Comments
 (0)