From a3886577aa0a2427a0da97a4a4564bbae6acdf14 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Mon, 9 Nov 2015 15:20:41 -0600 Subject: [PATCH] A few tweaks on the level up page --- assets/js/B_cookies.js | 39 ++++++++++++++++++++++++++++++ assets/js/levelup_main.js | 48 +++++++++++++++++++++++++++---------- processor_levelupachieve.go | 15 ++++-------- statbot.go | 2 +- statbotweb.go | 1 + 5 files changed, 82 insertions(+), 23 deletions(-) create mode 100644 assets/js/B_cookies.js diff --git a/assets/js/B_cookies.js b/assets/js/B_cookies.js new file mode 100644 index 0000000..c055f5e --- /dev/null +++ b/assets/js/B_cookies.js @@ -0,0 +1,39 @@ +var docCookies = { + getItem: function (sKey) { + if (!sKey) { return null; } + return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null; + }, + setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) { + if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) { return false; } + var sExpires = ""; + if (vEnd) { + switch (vEnd.constructor) { + case Number: + sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + vEnd; + break; + case String: + sExpires = "; expires=" + vEnd; + break; + case Date: + sExpires = "; expires=" + vEnd.toUTCString(); + break; + } + } + document.cookie = encodeURIComponent(sKey) + "=" + encodeURIComponent(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : ""); + return true; + }, + removeItem: function (sKey, sPath, sDomain) { + if (!this.hasItem(sKey)) { return false; } + document.cookie = encodeURIComponent(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : ""); + return true; + }, + hasItem: function (sKey) { + if (!sKey) { return false; } + return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie); + }, + keys: function () { + var aKeys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/); + for (var nLen = aKeys.length, nIdx = 0; nIdx < nLen; nIdx++) { aKeys[nIdx] = decodeURIComponent(aKeys[nIdx]); } + return aKeys; + } +}; diff --git a/assets/js/levelup_main.js b/assets/js/levelup_main.js index 99e120d..6f4c847 100644 --- a/assets/js/levelup_main.js +++ b/assets/js/levelup_main.js @@ -1,31 +1,55 @@ +var sortMode = "ALPHA"; + document.addEventListener("DOMContentLoaded", function(event) { - sortByAlpha(); - buildList(); + sortMode = docCookies.getItem("levelup-sort") + updateScreen(); B('#btnAlphaSort').on('click', function() { if(sortMode == "ALPHA") { sortMode = "ALPHA-REV"; - B('#btnAlphaSort>i').removeClass('fa-sort-asc').addClass('fa-sort-desc'); } else { sortMode = "ALPHA"; - B('#btnAlphaSort>i').removeClass('fa-sort-desc').addClass('fa-sort-asc'); } - sortUsers(); - buildList(); + docCookies.setItem("levelup-sort", sortMode); + updateScreen(); }); B('#btnXpSort').on('click', function() { if(sortMode == "XP") { sortMode = "XP-REV"; - B('#btnXpSort>i').removeClass('fa-sort-desc').addClass('fa-sort-asc'); } else { sortMode = "XP"; - B('#btnXpSort>i').removeClass('fa-sort-asc').addClass('fa-sort-desc'); } - sortUsers(); - buildList(); + docCookies.setItem("levelup-sort", sortMode); + updateScreen(); }); -}) +}); -var sortMode = "ALPHA"; +function updateScreen() { + sortUsers(); + styleButtons(); + buildList(); +} + +function styleButtons() { + var alphaBtn = B('#btnAlphaSort>i'), + xpBtn = B('#btnXpSort>i'); + alphaBtn.removeClass('fa-sort-asc').removeClass('fa-sort-desc'); + xpBtn.removeClass('fa-sort-asc').removeClass('fa-sort-desc'); + switch(sortMode) { + case "ALPHA": + alphaBtn.addClass('fa-sort-desc'); + break; + case "ALPHA-REV": + alphaBtn.addClass('fa-sort-asc'); + break; + case "XP": + xpBtn.addClass('fa-sort-desc'); + break; + default: + sortMode = "XP-REV"; + xpBtn.addClass('fa-sort-asc'); + break; + } +} function clearList() { B(".levelup-user").remove(); diff --git a/processor_levelupachieve.go b/processor_levelupachieve.go index 2f971a8..4718209 100644 --- a/processor_levelupachieve.go +++ b/processor_levelupachieve.go @@ -1,10 +1,6 @@ package main -import ( - "net/http" - - "github.com/gorilla/mux" -) +import "net/http" type levelUpAchieveStatProcessor struct { Achievements []levelUpAchievement @@ -38,15 +34,14 @@ func (p *levelUpAchieveStatProcessor) ProcessMessage(m *Message) { } var u *User var err error - vars := mux.Vars(req) if u, err = getUserInfo(m.User); err != nil { return } - p := UserLevelUpStats{Name: u.Name} - p.Xp, _ = getUserStat(u.ID, "levelup-xp") - p.ChannelStats = getAllLevelUpChannelXp(u.ID) - p.OtherStats = getAllNonLevelUpStats(u.ID) + userStat := UserLevelUpStats{Name: u.Name} + userStat.Xp, _ = getUserStat(u.ID, "levelup-xp") + userStat.ChannelStats = getAllLevelUpChannelXp(u.ID) + userStat.OtherStats = getAllNonLevelUpStats(u.ID) } func (p *levelUpAchieveStatProcessor) ProcessAdminMessage(m *Message) {} diff --git a/statbot.go b/statbot.go index 6a19f96..724def9 100644 --- a/statbot.go +++ b/statbot.go @@ -65,7 +65,7 @@ func statBotMain(slack *Slack) { registerStatProcessor(new(levelUpStatProcessor)) registerStatProcessor(new(generalStatProcessor)) - levelUpAchievements := new(levelUpAchieveStatProcessor) + //levelUpAchievements := new(levelUpAchieveStatProcessor) // Register Achievements registerMessageProcessor(new(generalProcessor)) diff --git a/statbotweb.go b/statbotweb.go index 41a5cdc..9158421 100644 --- a/statbotweb.go +++ b/statbotweb.go @@ -92,6 +92,7 @@ func initRequest(w http.ResponseWriter, req *http.Request) { site.Scripts = make([]string, 0, 0) site.Scripts = append(site.Scripts, "/assets/js/highcharts.js") site.Scripts = append(site.Scripts, "/assets/js/B.js") + site.Scripts = append(site.Scripts, "/assets/js/B_cookies.js") site.InlineScript = ""