diff --git a/assets/js/job/index.js b/assets/js/job/index.js index 97ecee1..259e436 100644 --- a/assets/js/job/index.js +++ b/assets/js/job/index.js @@ -1,12 +1,10 @@ import { Modal } from 'bootstrap'; import * as image from '../../images/ajax-loader.gif'; -import Utils from "../utils"; document.addEventListener("readystatechange", event => { if(event.target.readyState === 'complete') { initDeleteButtons(); initRunNowButtons(); - initTags(); } }); @@ -26,32 +24,6 @@ function initDeleteButtons() { })); } -function initTags() { - var tags = JSON.parse(localStorage.getItem('tags')) ?? new Object(); - var collected = Object.keys(tags); - document.querySelectorAll('.job-name').forEach(elem => { - let matches = elem.textContent.matchAll(/\[([A-Za-z0-9 \-]+)\]/g) - for (const tag of matches) { - if (typeof tag != 'undefined') { - if(collected.indexOf(tag[1]) == -1) { - let color = '#'+tag[1].hashCode().toString(16).substr(1,6)// ; (0x1000000+Math.random()*0xffffff).toString(16).substr(1,6) - collected.push(tag[1]); - tags[tag[1]] = color; - } - let tagcolor = tags[tag[1]]; - let newelem = document.createElement('span') - newelem.classList.add('tag'); - newelem.innerHTML = tag[1]; - newelem.style.backgroundColor = tagcolor; - newelem.style.color = Utils.lightOrDark(tagcolor) == 'dark' ? '#ffffff' : '#000000'; - elem.innerHTML = elem.innerHTML.replace(tag[0], newelem.outerHTML); - } - } - }) - localStorage.setItem('tags', JSON.stringify(tags)); -} - - function initRunNowButtons() { document.querySelectorAll('.runnow').forEach(elem => elem.addEventListener("click", event => { let me = event.currentTarget; diff --git a/assets/js/job/view.js b/assets/js/job/view.js index a873dfd..3efe111 100644 --- a/assets/js/job/view.js +++ b/assets/js/job/view.js @@ -1,5 +1,4 @@ import 'bootstrap'; -import Utils from "../utils"; document.addEventListener("readystatechange", event => { if(event.target.readyState === 'complete') { diff --git a/assets/js/utils.js b/assets/js/utils.js deleted file mode 100644 index 9434a61..0000000 --- a/assets/js/utils.js +++ /dev/null @@ -1,57 +0,0 @@ -let Utils = new Object(); - -Utils.lightOrDark = function (color) { - - // Variables for red, green, blue values - var r, g, b, hsp; - - // Check the format of the color, HEX or RGB? - if (color.match(/^rgb/)) { - - // If RGB --> store the red, green, blue values in separate variables - color = color.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+(?:\.\d+)?))?\)$/); - - r = color[1]; - g = color[2]; - b = color[3]; - } - else { - - // If hex --> Convert it to RGB: http://gist.github.com/983661 - color = +("0x" + color.slice(1).replace( - color.length < 5 && /./g, '$&$&')); - - r = color >> 16; - g = color >> 8 & 255; - b = color & 255; - } - - // HSP (Highly Sensitive Poo) equation from http://alienryderflex.com/hsp.html - hsp = Math.sqrt( - 0.299 * (r * r) + - 0.587 * (g * g) + - 0.114 * (b * b) - ); - - // Using the HSP value, determine whether the color is light or dark - if (hsp>127.5) { - - return 'light'; - } - else { - - return 'dark'; - } -} - -String.prototype.hashCode = function() { - var hash = 111111; - for (var i = 0; i < this.length; i++) { - var char = this.charCodeAt(i); - hash = ((hash<<5)-hash)+char; - hash = hash & hash; // Convert to 32bit integer - } - return hash; -} - -export default Utils; \ No newline at end of file diff --git a/lib/Framework/Twig.php b/lib/Framework/Twig.php index f8b514e..7b215ad 100644 --- a/lib/Framework/Twig.php +++ b/lib/Framework/Twig.php @@ -70,5 +70,41 @@ class Twig return "{$days}d {$hours}h {$minutes}m {$seconds}s"; }); $this->environment->addFilter($secondsToInterval); + + $parseTags = new TwigFilter('parsetags', function(string $text) { + $results = []; + preg_match_all('/\[([A-Za-z0-9 \-]+)\]/', $text, $results); + foreach ($results[0] as $key=>$result) { + $background = substr(md5($results[0][$key]), 0, 6); + $color = $this->lightOrDark($background) == 'dark' ? 'ffffff' : '000000'; + $text = str_replace($results[0][$key], '' . $results[1][$key] . '', $text); + } + return $text; + }); + + $this->environment->addFilter($parseTags); + + } + + private function lightOrDark ($color) { + $color = str_split($color, 2); + foreach($color as &$value) { + $value = hexdec($value); + } + + // HSP (Highly Sensitive Poo) equation from http://alienryderflex.com/hsp.html + $hsp = sqrt( + 0.299 * ($color[0] * $color[0]) + + 0.587 * ($color[1] * $color[1]) + + 0.114 * ($color[2] * $color[2]) + ); + + + // Using the HSP value, determine whether the color is light or dark + if ($hsp>140) { + return 'light'; + } else { + return 'dark'; + } } } \ No newline at end of file diff --git a/templates/job/index.html.twig b/templates/job/index.html.twig index 418d8b5..72d431e 100644 --- a/templates/job/index.html.twig +++ b/templates/job/index.html.twig @@ -20,7 +20,7 @@ {% if job.needschecking %}{% endif %} {% if job.needschecking %}{% endif %} - {{ job.name }} + {{ job.name | parsetags | raw }} {{ attribute(job, 'host-displayname') }} {{ job.interval | interval }} {{ job.nextrun | date("d/m/Y H:i:s") }} diff --git a/templates/job/view.html.twig b/templates/job/view.html.twig index f35733f..b6737d6 100644 --- a/templates/job/view.html.twig +++ b/templates/job/view.html.twig @@ -1,7 +1,7 @@ {% extends "base.html.twig" %} {% block title %}Overview of run for {{ job.name }}{% endblock %} {% block content %} -

Overview of runs for {{ job.name }}

+

Overview of runs for {{ job.name | parsetags | raw }}

Edit job {% if allruns %} | Only show failed runs