Skip to content

Commit c6a00c1

Browse files
authored
Merge pull request #58 from uedvt359/feat/deviceimages
load device front images if available
2 parents 9fbc184 + 9dbc0ae commit c6a00c1

File tree

2 files changed

+79
-30
lines changed

2 files changed

+79
-30
lines changed

netbox_floorplan/static/netbox_floorplan/floorplan/edit.js

+78-29
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ function add_text() {
275275
}
276276
window.add_text = add_text;
277277

278-
function add_floorplan_object(top, left, width, height, unit, fill, rotation, object_id, object_name, object_type, status) {
278+
function add_floorplan_object(top, left, width, height, unit, fill, rotation, object_id, object_name, object_type, status, image) {
279279
var object_width;
280280
var object_height;
281281
if ( !width || !height || !unit ){
@@ -300,44 +300,89 @@ function add_floorplan_object(top, left, width, height, unit, fill, rotation, ob
300300
console.log(object_height)
301301
}
302302
document.getElementById(`object_${object_type}_${object_id}`).remove();
303-
var rect = new fabric.Rect({
304-
top: top,
305-
name: "rectangle",
306-
left: left,
307-
width: object_width,
308-
height: object_height,
309-
fill: fill,
310-
opacity: 0.8,
311-
lockRotation: false,
312-
originX: "center",
313-
originY: "center",
314-
cornerSize: 15,
315-
hasRotatingPoint: true,
316-
perPixelTargetFind: true,
317-
minScaleLimit: 1,
318-
maxWidth: canvasWidth,
319-
maxHeight: canvasHeight,
320-
centeredRotation: true,
321-
custom_meta: {
322-
"object_type": object_type,
323-
"object_id": object_id,
324-
"object_name": object_name,
325-
"object_url": "/dcim/" + object_type + "s/" + object_id + "/",
326-
},
327-
});
303+
/* if we have an image, we display the text below, otherwise we display the text within */
304+
var rect, text_offset = 0;
305+
if (!image) {
306+
rect = new fabric.Rect({
307+
top: top,
308+
name: "rectangle",
309+
left: left,
310+
width: object_width,
311+
height: object_height,
312+
fill: fill,
313+
opacity: 0.8,
314+
lockRotation: false,
315+
originX: "center",
316+
originY: "center",
317+
cornerSize: 15,
318+
hasRotatingPoint: true,
319+
perPixelTargetFind: true,
320+
minScaleLimit: 1,
321+
maxWidth: canvasWidth,
322+
maxHeight: canvasHeight,
323+
centeredRotation: true,
324+
custom_meta: {
325+
"object_type": object_type,
326+
"object_id": object_id,
327+
"object_name": object_name,
328+
"object_url": "/dcim/" + object_type + "s/" + object_id + "/",
329+
},
330+
});
331+
} else {
332+
object_height = object_width;
333+
text_offset = object_height/2 + 4;
334+
rect = new fabric.Image(null, {
335+
top: top,
336+
name: "rectangle",
337+
left: left,
338+
width: object_width,
339+
height: object_height,
340+
opacity: 1,
341+
lockRotation: false,
342+
originX: "center",
343+
originY: "center",
344+
cornerSize: 15,
345+
hasRotatingPoint: true,
346+
perPixelTargetFind: true,
347+
minScaleLimit: 1,
348+
maxWidth: canvasWidth,
349+
maxHeight: canvasHeight,
350+
centeredRotation: true,
351+
shadow: new fabric.Shadow({
352+
color: "red",
353+
blur: 15,
354+
}),
355+
custom_meta: {
356+
"object_type": object_type,
357+
"object_id": object_id,
358+
"object_name": object_name,
359+
"object_url": "/dcim/" + object_type + "s/" + object_id + "/",
360+
},
361+
});
362+
rect.setSrc("/media/" + image, function(img){
363+
img.scaleX = object_width / img.width;
364+
img.scaleY = object_height / img.height;
365+
canvas.renderAll();
366+
});
367+
}
328368

329-
var text = new fabric.IText(object_name, {
369+
var text = new fabric.Textbox(object_name, {
330370
fontFamily: "Courier New",
331371
fontSize: 16,
372+
splitByGrapheme: text_offset? null : true,
332373
fill: "#FFFF",
374+
width: object_width,
333375
textAlign: "center",
334376
originX: "center",
335377
originY: "center",
336378
left: left,
337-
top: top,
379+
top: top + text_offset,
338380
excludeFromExport: false,
339381
includeDefaultValues: true,
340382
centeredRotation: true,
383+
stroke: "#000",
384+
strokeWidth: 2,
385+
paintFirst: 'stroke',
341386
custom_meta: {
342387
"text_type": "name",
343388
}
@@ -352,10 +397,14 @@ function add_floorplan_object(top, left, width, height, unit, fill, rotation, ob
352397
originX: "center",
353398
originY: "center",
354399
left: left,
355-
top: top + 16,
400+
top: top + text_offset + 16,
356401
excludeFromExport: false,
357402
includeDefaultValues: true,
358403
centeredRotation: true,
404+
shadow: text_offset? new fabric.Shadow({
405+
color: '#FFF',
406+
blur: 1
407+
}) : null,
359408
custom_meta: {
360409
"text_type": "status",
361410
}

netbox_floorplan/tables.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class FloorplanDeviceTable(NetBoxTable):
5454
name = tables.LinkColumn()
5555

5656
actions = tables.TemplateColumn(template_code="""
57-
<a type="button" class="btn btn-sm btn-outline-info" onclick="add_floorplan_object(30, 50, 60, 91, '{{ record.outer_unit }}', '#ea8fe', 30, '{{ record.id }}', '{{ record.name }}', 'device', '{{ record.status }}')">Add Device
57+
<a type="button" class="btn btn-sm btn-outline-info" onclick="add_floorplan_object(30, 50, 60, 60, '{{ record.outer_unit }}', '#ea8fe', 30, '{{ record.id }}', '{{ record.name }}', 'device', '{{ record.status }}', '{{ record.device_type.front_image }}')">Add Device
5858
</a>
5959
""")
6060

0 commit comments

Comments
 (0)