Skip to content

Commit

Permalink
chore: initialize repo
Browse files Browse the repository at this point in the history
Signed-off-by: joshuaunity <[email protected]>
  • Loading branch information
joshuaunity committed Sep 27, 2024
1 parent dc3e8df commit a9be25f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 40 deletions.
19 changes: 19 additions & 0 deletions flexmeasures/ui/static/js/data-utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
import 'https://cdn.jsdelivr.net/npm/luxon/build/es6/luxon.js';

const { Duration } = luxon;
// Format ISO 8601 duration using luxon
export function formatResolution(isoDuration) {
const duration = Duration.fromISO(isoDuration);

// Convert to a human-readable string
let result = "";
if (duration.hours) {
result += `${duration.hours} hour${duration.hours > 1 ? 's' : ''}`;
}
if (duration.minutes) {
if (result) result += " and ";
result += `${duration.minutes} minute${duration.minutes > 1 ? 's' : ''}`;
}
return result || isoDuration;
}

// Data source utils

/**
Expand Down
19 changes: 4 additions & 15 deletions flexmeasures/ui/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,11 @@

<!-- loading this earlier so templates can use it -->
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.5.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/luxon/build/global/luxon.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/floatthead/2.2.1/jquery.floatThead.min.js"></script>



{# Div blocks that child pages can reference #}
{% block divs %}

Expand Down Expand Up @@ -236,6 +239,7 @@
<script>
// Define picker as a global variable so that other code on the page can access it, e.g. get the times
var picker;

</script>
<script type="module" type="text/javascript">

Expand Down Expand Up @@ -806,21 +810,6 @@
</script>

<script>
// Format ISO 8601 duration using luxon
function formatResolution(isoDuration) {
const duration = Duration.fromISO(isoDuration);

// Convert to a human-readable string
let result = "";
if (duration.hours) {
result += `${duration.hours} hour${duration.hours > 1 ? 's' : ''}`;
}
if (duration.minutes) {
if (result) result += " and ";
result += `${duration.minutes} minute${duration.minutes > 1 ? 's' : ''}`;
}
return result || isoDuration;
}

function User(
id,
Expand Down
35 changes: 10 additions & 25 deletions flexmeasures/ui/templates/crud/asset.html
Original file line number Diff line number Diff line change
Expand Up @@ -278,14 +278,6 @@ <h3>All sensors for {{ asset.name }}</h3>
<div class="table-responsive">

<table class="table table-striped paginate nav-on-click" title="View data" id="sensorsTable">
<div class="col-2 col-auto ms-auto">
<select class="form-select form-select-sm" id="unitFilterOptions">
<option selected value="">Filter Unit</option>
<option value="m/s">m/s</option>
<option value="°C">°C</option>
<option value="kW/m²">kW/m²</option>
</select>
</div>
</table>
</div>
</div>
Expand Down Expand Up @@ -361,6 +353,7 @@ <h3>All child assets for {{ asset.name }}</h3>
<script src="https://cdn.jsdelivr.net/npm/litepicker/dist/plugins/ranges.js"></script>
<script src="https://cdn.jsdelivr.net/npm/litepicker/dist/plugins/keyboardnav.js"></script>


{% block leftsidepanel %} {{ super() }} {% endblock %}
{% block sensorChartSetup %} {{ super() }} {% endblock %}

Expand Down Expand Up @@ -401,11 +394,9 @@ <h3>All child assets for {{ asset.name }}</h3>
});
</script>

<script src="https://cdn.jsdelivr.net/npm/luxon/build/global/luxon.min.js"></script>
<script>
// Import luxon
const { Duration } = luxon;

<script type="module">
import {formatResolution} from "{{ url_for('flexmeasures_ui.static', filename='js/data-utils.js') }}";

function Sensor(
id,
name,
Expand All @@ -417,15 +408,18 @@ <h3>All child assets for {{ asset.name }}</h3>
this.id = id;
this.name = name;
this.unit = unit;

this.resolution = formatResolution(resolution);

this.entity_address = entity_address;
this.url = `/sensors/${id}`;
}

$(document).ready(function () {
let unit = "";
// Check if DataTable is already initialized
if ($.fn.DataTable.isDataTable("#sensorsTable")) {
// Destroy the existing DataTable before reinitializing
$("#sensorsTable").DataTable().destroy();
}

// Initialize the DataTable
const table = $("#sensorsTable").dataTable({
serverSide: true,
Expand All @@ -449,9 +443,6 @@ <h3>All child assets for {{ asset.name }}</h3>
if (filter.length > 0) {
url = `${url}&filter=${filter}`;
}
if (unit !== "") {
url = `${url}&unit=${unit}`;
}

$.ajax({
type: "get",
Expand Down Expand Up @@ -484,12 +475,6 @@ <h3>All child assets for {{ asset.name }}</h3>
},
});


// Event listener for the select box
$("#unitFilterOptions").change(function () {
unit = this.value;
table.api().ajax.reload();
});
});
</script>

Expand Down

0 comments on commit a9be25f

Please sign in to comment.