From 53deb0b8cd1899ec325eca93631b3e137bdd3ec3 Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Thu, 5 Jul 2018 22:18:51 -0700 Subject: Refactor assets bundler and split Javascript files --- ui/static/js/unread_counter_handler.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 ui/static/js/unread_counter_handler.js (limited to 'ui/static/js/unread_counter_handler.js') diff --git a/ui/static/js/unread_counter_handler.js b/ui/static/js/unread_counter_handler.js new file mode 100644 index 0000000..87a1aaa --- /dev/null +++ b/ui/static/js/unread_counter_handler.js @@ -0,0 +1,33 @@ +class UnreadCounterHandler { + static decrement(n) { + this.updateValue((current) => { + return current - n; + }); + } + + static increment(n) { + this.updateValue((current) => { + return current + n; + }); + } + + static updateValue(callback) { + let counterElements = document.querySelectorAll("span.unread-counter"); + counterElements.forEach((element) => { + let oldValue = parseInt(element.textContent, 10); + element.innerHTML = callback(oldValue); + }); + + if (window.location.href.endsWith('/unread')) { + let oldValue = parseInt(document.title.split('(')[1], 10); + let newValue = callback(oldValue); + + document.title = document.title.replace( + /(.*?)\(\d+\)(.*?)/, + function (match, prefix, suffix, offset, string) { + return prefix + '(' + newValue + ')' + suffix; + } + ); + } + } +} -- cgit v1.2.3