diff options
author | 2018-07-05 22:18:51 -0700 | |
---|---|---|
committer | 2018-07-05 22:18:51 -0700 | |
commit | 53deb0b8cd1899ec325eca93631b3e137bdd3ec3 (patch) | |
tree | 23894ed57040ea689e9f60243656e1889d39a275 /ui/static/js/unread_counter_handler.js | |
parent | e1c56b2e53ba3c6f48d5e159d18ae59c180cc388 (diff) | |
download | v2-53deb0b8cd1899ec325eca93631b3e137bdd3ec3.tar.gz v2-53deb0b8cd1899ec325eca93631b3e137bdd3ec3.tar.zst v2-53deb0b8cd1899ec325eca93631b3e137bdd3ec3.zip |
Refactor assets bundler and split Javascript files
Diffstat (limited to 'ui/static/js/unread_counter_handler.js')
-rw-r--r-- | ui/static/js/unread_counter_handler.js | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/ui/static/js/unread_counter_handler.js b/ui/static/js/unread_counter_handler.js new file mode 100644 index 00000000..87a1aaaf --- /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; + } + ); + } + } +} |