Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.5.0 #54

Merged
merged 2 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 47 additions & 9 deletions netbox_floorplan/static/netbox_floorplan/floorplan/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,9 +453,16 @@ window.center_pan_on_slected_object = center_pan_on_slected_object;

function update_background() {
var assigned_image = document.getElementById("id_assigned_image").value;
if (assigned_image == "") { assigned_image = null; }
if (assigned_image == "") {
assigned_image = null;
canvas.setBackgroundImage(null, canvas.renderAll.bind(canvas));
}
var floor_json = canvas.toJSON(["id", "text", "_controlsVisibility", "custom_meta", "lockMovementY", "lockMovementX", "evented", "selectable"]);





$.ajax({
type: "PATCH",
url: `/api/plugins/floorplan/floorplans/${obj_pk}/`,
Expand All @@ -481,15 +488,45 @@ function update_background() {
}

var img = fabric.Image.fromURL(img_url, function(img) {
let scaleRatio = Math.max(canvas.width / img.width, canvas.height / img.height);
canvas.setBackgroundImage(img, canvas.renderAll.bind(canvas), {
scaleX: scaleRatio,
scaleY: scaleRatio,
left: canvas.width / 2,
top: canvas.height / 2,
originX: 'middle',
originY: 'middle'


var left = 0;
var top = 0;
var width = 0;
var height = 0;
canvas.getObjects().forEach(function (object) {
if (object.custom_meta) {
if (object.custom_meta.object_type == "floorplan_boundry") {
left = object.left;
top = object.top;
width = object.width;
height = object.height;
}
}
});
// if we have a floorplan boundary, position the image in there
if (height != 0 && width != 0) {
let scaleRatioX = Math.max(width / img.width)
let scaleRatioY = Math.max(height / img.height);
canvas.setBackgroundImage(img, canvas.renderAll.bind(canvas), {
scaleX: scaleRatioX,
scaleY: scaleRatioY,
left: left,
top: top
});
}
else
{
let scaleRatio = Math.max(canvas.width / img.width, canvas.height / img.height);
canvas.setBackgroundImage(img, canvas.renderAll.bind(canvas), {
scaleX: scaleRatio,
scaleY: scaleRatio,
left: canvas.width / 2,
top: canvas.height / 2,
originX: 'middle',
originY: 'middle'
});
}
});

} else {
Expand Down Expand Up @@ -571,6 +608,7 @@ function update_dimensions() {
centeredRotation: true,
});


var text = new fabric.IText(`${obj_name}`, {
fontFamily: "Courier New",
fontSize: 16,
Expand Down
45 changes: 37 additions & 8 deletions netbox_floorplan/static/netbox_floorplan/floorplan/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,45 @@ function init_floor_plan(floorplan_id, canvas, mode) {
img_url = floorplan.assigned_image.file;
}


var img = fabric.Image.fromURL(img_url, function(img) {
let scaleRatio = Math.max(canvas.width / img.width, canvas.height / img.height);
canvas.setBackgroundImage(img, canvas.renderAll.bind(canvas), {
scaleX: scaleRatio,
scaleY: scaleRatio,
left: canvas.width / 2,
top: canvas.height / 2,
originX: 'middle',
originY: 'middle'
var left = 0;
var top = 0;
var width = 0;
var height = 0;
canvas.getObjects().forEach(function (object) {
if (object.custom_meta) {
if (object.custom_meta.object_type == "floorplan_boundry") {
left = object.left;
top = object.top;
width = object.width;
height = object.height;
}
}
});
// if we have a floorplan boundary, position the image in there
if (height != 0 && width != 0) {
let scaleRatioX = Math.max(width / img.width)
let scaleRatioY = Math.max(height / img.height);
canvas.setBackgroundImage(img, canvas.renderAll.bind(canvas), {
scaleX: scaleRatioX,
scaleY: scaleRatioY,
left: left,
top: top
});
}
else
{
let scaleRatio = Math.max(canvas.width / img.width, canvas.height / img.height);
canvas.setBackgroundImage(img, canvas.renderAll.bind(canvas), {
scaleX: scaleRatio,
scaleY: scaleRatio,
left: canvas.width / 2,
top: canvas.height / 2,
originX: 'middle',
originY: 'middle'
});
}
});


Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
{{ block.super }}
<script src="{% static 'netbox_floorplan/vendors/fabric-js-6.0.2.js' %}"></script>
<script src="{% static 'netbox_floorplan/vendors/jquery-3.7.1.js' %}"></script>

<script src="{% static 'netbox_floorplan/vendors/htmx.min.js' %}"></script>

{% endblock head %}

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

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

{% endblock content %}
Loading