Skip to content

Commit

Permalink
Merge branch 'release/v0.6.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
fratsloos committed Oct 31, 2022
2 parents 377a099 + 40e8dbb commit 3d52477
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 182 deletions.
2 changes: 1 addition & 1 deletion dist/fr24_card.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/fr24_card.js

Large diffs are not rendered by default.

203 changes: 115 additions & 88 deletions src/javascript/fr24_card.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ window.fr24db = [];
class Fr24Card extends HTMLElement {
set hass(hass) {
this._hass = hass;
this._isStateUndefined = false;

// Update the card
if (!this._config) {
Expand Down Expand Up @@ -217,6 +218,7 @@ class Fr24Card extends HTMLElement {
this._hass.states[this._config.entity].attributes[this._config.attribute];

if (typeof states === "undefined") {
this._isStateUndefined = true;
return;
}

Expand Down Expand Up @@ -287,11 +289,6 @@ class Fr24Card extends HTMLElement {
* Renders the HTML table with the aircrafts in it
*/
_renderTable() {
// Check for data
if (this._aircrafts.length === 0) {
throw new Error("No data found, check configuration and JSON output");
}

// Create a new table
const table = new Table();
const needsUnits = this._config.units_in_table === true;
Expand Down Expand Up @@ -342,14 +339,63 @@ class Fr24Card extends HTMLElement {
// Add header row
table.row(headerCells, "thead");

// Body
let i = 0;
for (let aircraft of this._aircrafts) {
// First aircraft add units in table
if (needsUnits && !hasUnits) {
hasUnits = true;
if (this._aircrafts.length > 0) {
// Body
let i = 0;
for (let aircraft of this._aircrafts) {
// First aircraft add units in table
if (needsUnits && !hasUnits) {
hasUnits = true;

let unitCells = [];

this._config.columns.forEach((key) => {
// Get column from the available columns
let column = this._availableColumns[key];

// Check if column is visible
if (column.show === false) {
return;
}

// Content of the cell
let value = aircraft.units[key] ?? "";

// Styles of the cell
let styles = column.styles ?? null;

// Inline style
let style = "";
if (this._config.colors.table_units_bg !== null) {
style +=
"background-color:" +
this._config.colors.table_units_bg +
" !important;";
}
if (this._config.colors.table_units_text !== null) {
style +=
"color:" +
this._config.colors.table_units_text +
" !important;";
}

// Attributes
let attrs = [];
if (style.length > 0) {
attrs["style"] = style;
}

// Push header cell
let cell = table.cell(value, styles, "td", attrs);
unitCells.push(cell);
});

// Add units row
table.row(unitCells, "thead");
}

let unitCells = [];
// Add aircraft
let cells = [];

this._config.columns.forEach((key) => {
// Get column from the available columns
Expand All @@ -360,23 +406,23 @@ class Fr24Card extends HTMLElement {
return;
}

// Content of the cell
let value = aircraft.units[key] ?? "";

// Styles of the cell
let styles = column.styles ?? null;

// Inline style
let style = "";
if (this._config.colors.table_units_bg !== null) {
style +=
"background-color:" +
this._config.colors.table_units_bg +
" !important;";
}
if (this._config.colors.table_units_text !== null) {
style +=
"color:" + this._config.colors.table_units_text + " !important;";
if (i % 2 === 1) {
if (this._config.colors.table_even_row_bg !== null) {
style +=
"background-color:" +
this._config.colors.table_even_row_bg +
" !important;";
}
if (this._config.colors.table_even_row_text !== null) {
style +=
"color:" +
this._config.colors.table_even_row_text +
" !important;";
}
} else if (this._config.colors.table_text !== null) {
style += "color:" + this._config.colors.table_text + " !important;";
}

// Attributes
Expand All @@ -386,81 +432,62 @@ class Fr24Card extends HTMLElement {
}

// Push header cell
let cell = table.cell(value, styles, "td", attrs);
unitCells.push(cell);
let cell = table.cell(
aircraft.value(key),
column.styles ?? null,
"td",
attrs
);
cells.push(cell);
});

// Add units row
table.row(unitCells, "thead");
}
// Attributes of the row
let attrs = [];
if (this._config.popup) {
attrs["data-hex"] = aircraft.hex;
}

// Add aircraft
let cells = [];
// Add body row
table.row(cells, null, attrs);

this._config.columns.forEach((key) => {
// Get column from the available columns
let column = this._availableColumns[key];
// Update iterator
i++;

// Check if column is visible
if (column.show === false) {
return;
}

// Inline style
let style = "";
if (i % 2 === 1) {
if (this._config.colors.table_even_row_bg !== null) {
style +=
"background-color:" +
this._config.colors.table_even_row_bg +
" !important;";
}
if (this._config.colors.table_even_row_text !== null) {
style +=
"color:" +
this._config.colors.table_even_row_text +
" !important;";
}
} else if (this._config.colors.table_text !== null) {
style += "color:" + this._config.colors.table_text + " !important;";
// Check for limit
if (Number.isInteger(this._config.limit) && this._config.limit === i) {
break;
}
}
}

// Attributes
let attrs = [];
if (style.length > 0) {
attrs["style"] = style;
}
// Get content
let html = table.getHtml();

// Push header cell
let cell = table.cell(
aircraft.value(key),
column.styles ?? null,
"td",
attrs
);
cells.push(cell);
});

// Attributes of the row
let attrs = [];
if (this._config.popup) {
attrs["data-hex"] = aircraft.hex;
// Add warning when no aircrafts detected
if (this._aircrafts.length === 0) {
let style = "";
if (this._config.colors.table_even_row_bg !== null) {
style +=
"background-color:" +
this._config.colors.table_even_row_bg +
" !important;";
}
if (this._config.colors.table_even_row_text !== null) {
style +=
"color:" + this._config.colors.table_even_row_text + " !important;";
}

// Add body row
table.row(cells, null, attrs);

// Update iterator
i++;
let error = this._isStateUndefined
? this._lang.content.table.data.undefined
: this._lang.content.table.data.none;

// Check for limit
if (Number.isInteger(this._config.limit) && this._config.limit === i) {
break;
}
html += `<div class="no-data"${
style !== "" ? ` style="${style}"` : ""
}>${error}</div>`;
}

// Set content
this.contentDiv.innerHTML = table.getHtml();
this.contentDiv.innerHTML = html;

// Add popup if configured
if (this._config.popup) {
Expand Down
Loading

0 comments on commit 3d52477

Please sign in to comment.