Implement colourful tags

This commit is contained in:
Helena Rasche
2018-12-04 17:26:59 +01:00
parent aaa8d1a61c
commit eeffb26423
@@ -1,5 +1,14 @@
function _templateNametag(tag) {
return `<span class="badge badge-primary badge-tags">${_.escape(tag.slice(5))}</span>`;
return `<span style="background-color: ${generateTagColor(tag.slice(5))}" class="badge badge-primary badge-tags">${_.escape(tag.slice(5))}</span>`;
}
function generateTagColor(tagValue) {
var hash = hashFnv32a(tagValue);
var r = ((hash & 0xf) % 10),
g = (((hash & 0xf0) >> 4) % 10),
b = (((hash & 0xf00) >> 8) % 10);
return `#${r}${g}${b}`;
}
function nametagTemplate(historyItem) {
@@ -14,3 +23,23 @@ function nametagTemplate(historyItem) {
export default {
nametagTemplate: nametagTemplate
};
/**
* Calculate a 32 bit FNV-1a hash
* Found here: https://gist.github.com/vaiorabbit/5657561
* Ref.: http://isthe.com/chongo/tech/comp/fnv/
*
* @param {string} str the input value
* @returns {integer}
*/
function hashFnv32a(str) {
/*jshint bitwise:false */
var i, l,
hval = 0x811c9dc5;
for (i = 0, l = str.length; i < l; i++) {
hval ^= str.charCodeAt(i);
hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24);
}
return hval >>> 0;
}