diff --git a/puppetboard/static/js/lists.js b/puppetboard/static/js/lists.js deleted file mode 100644 index fd69b5bb4..000000000 --- a/puppetboard/static/js/lists.js +++ /dev/null @@ -1,30 +0,0 @@ -/** - * Transform date string with tz to shorter string. - * If localisation enabled in config, date is timezoned with browser - * If localisation disabled in config, date is UTC - */ -$.fn.transformDatetime = function (format = "MMM DD YYYY - HH:mm:ss") { - this.each(function () { - let $el = $(this) - let localise = $el.data("localise") - let dt = moment($el.text()) - - if (!dt.isValid()) return - - if (localise) { - $el.text(dt.format("MMM DD YYYY - HH:mm:ss")) - } else { - $el.text(dt.utc().format("MMM DD YYYY - HH:mm:ss")) - } - }) - return this -} - -$(document).ready(function () { - $("[data-localise]").transformDatetime() - - let dataTable = $("#main-table").DataTable() - $("#main-table-search").on("input", function () { - dataTable.search(this.value).draw() - }) -}) diff --git a/puppetboard/static/js/utils.js b/puppetboard/static/js/utils.js new file mode 100644 index 000000000..ab161df8c --- /dev/null +++ b/puppetboard/static/js/utils.js @@ -0,0 +1,91 @@ +/** + * Simple debounce function + */ +debounce = function(cb, delay = 250) { + let timeout + + return (...args) => { + clearTimeout(timeout) + timeout = setTimeout(() => { + cb(...args) + }, delay) + } +} + +/** + * Transform date string with tz to shorter string. + * If localisation enabled in config, date is timezoned with browser + * If localisation disabled in config, date is UTC + */ +$.fn.transformDatetime = function (format = "MMM DD YYYY - HH:mm:ss") { + this.each(function () { + let $el = $(this) + let localise = $el.data("localise") + let dt = moment($el.text()) + + if (!dt.isValid()) return + + if (localise) { + $el.text(dt.format("MMM DD YYYY - HH:mm:ss")) + } else { + $el.text(dt.utc().format("MMM DD YYYY - HH:mm:ss")) + } + }) + return this +} + + +/** + * Enable filtering on fact list + * + * If text is entered in input, it will use this text to create a regexp and + * test againts every fact name. + * If match is found, ensure fact name is visible. + * If match is not found, ensure fact name is hidden. + * Then when all fact names has been tested, we hide all letter segment + * who have not at least one visible fact name + */ +$.fn.factList = function () { + this.each(function() { + const $el = $(this) + const $input = $el.find("input[name=filter]") + const $segments = $el.find(".segment") + const $facts = $el.find(".segment li") + + $input.on("input", debounce(function (e) { + const text = $input.val() + + if (!text) { + $segments.show() + $facts.show() + return + } + + const pattern = new RegExp(text, "i") + + $facts.each(function () { + const $fact = $(this) + + if (pattern.test($fact.text())) { + $fact.show() + } else { + $fact.hide() + } + }) + $segments.not(':has(li:visible)').hide() + }, 250)) + }) + + return this +} + +$(document).ready(function () { + $("[data-localise]").transformDatetime() + + let dataTable = $("#main-table").DataTable() + $("#main-table-search").on("input", function () { + dataTable.search(this.value).draw() + }) + + $('#fact-list').factList() +}) diff --git a/puppetboard/templates/facts.html b/puppetboard/templates/facts.html index e943d2b77..624aac368 100644 --- a/puppetboard/templates/facts.html +++ b/puppetboard/templates/facts.html @@ -1,24 +1,26 @@ {% extends 'layout.html' %} {% block content %} -
- -
-
- {%- for column in facts_columns %} -
- {%- for letter in column %} - {%- if letter is not none %} -
- {{ letter[0][0]|upper }} -
    - {%- for fact in letter %} -
  • {{ fact }}
  • - {%- endfor %} -
-
- {%- endif %} +
+
+ +
+
+ {%- for column in facts_columns %} +
+ {%- for letter in column %} + {%- if letter is not none %} +
+ {{ letter[0][0]|upper }} +
    + {%- for fact in letter %} +
  • {{ fact }}
  • + {%- endfor %} +
+
+ {%- endif %} + {%- endfor %} +
{%- endfor %}
- {%- endfor %}
{% endblock content %} diff --git a/puppetboard/templates/layout.html b/puppetboard/templates/layout.html index 747f5f7ed..a6bfd4565 100644 --- a/puppetboard/templates/layout.html +++ b/puppetboard/templates/layout.html @@ -18,7 +18,7 @@ {#- JS LOCAL #} - +