This repository has been archived by the owner on Dec 11, 2017. It is now read-only.
/
shoutbox.min.js
1 lines (1 loc) · 2.71 KB
/
shoutbox.min.js
1
var shoutBox=function(){function o(){var o=["Small","Blue","Fatty","Pink"],e=["Bear","Potato","Banana","Kiwi"],t=Math.floor(Math.random()*o.length),s=Math.floor(Math.random()*e.length);return o[t]+e[s]}function e(){return void 0===$rootScope.appConfig||$rootScope.appConfig.loggedIn===!1?(setTimeout(function(){e()},5e3),!1):(console.log("shoutBox module loaded"),void $(function(){n(),$(".shoutbox").show(),$(".shoutbox__header").on("click",function(o){t()}),$("#shoutbox__new_message").keypress(function(o){if(13==o.which){var e=$(this).val();l(e),$(this).val("")}}),c=$rootScope.appConfig.login||o()}))}function t(){var o=s();$(".shoutbox__body").slideToggle(200,function(){"open"==o?(a({shoutbox_is_open:!1}),$(".shoutbox__header i").attr("class","icon-circle-arrow-up")):(a({shoutbox_is_open:!0,unread_message:!1}),$(".shoutbox__header i").attr("class","icon-circle-arrow-down"))})}function s(){return"block"===$(".shoutbox__body").css("display")?"open":"closed"}function n(){var o={volume_is_on:!0,shoutbox_is_open:!0,unread_message:!1};if("undefined"==typeof localStorage||null===localStorage.getItem("shoutbox.settings"))a(o);else{console.log("loading existing settings");var e=JSON.parse(localStorage.getItem("shoutbox.settings"));console.log(e);var t=$.extend({},o,e);a(t)}}function a(o){if(console.log("Settings saved"),console.log(o),"object"==typeof o&&null!==o){for(var e in o)g[e]=o[e];if("undefined"!=typeof localStorage){var t=JSON.parse(localStorage.getItem("shoutbox.settings"));angular.equals(t,g)||localStorage.setItem("shoutbox.settings",JSON.stringify(g))}i()}}function i(){g.shoutbox_is_open!==("open"===s())&&t(),g.unread_message!==$(".shoutbox__header").hasClass("unread")&&$(".shoutbox__header").toggleClass("unread")}function r(o,e){var t=(new Date).toLocaleTimeString().toString(),s='<div class="shoutbox__message"><img src="http://test2.dataiku.com:6500/dip/api/image/get-image?type=USER&id='+o+"&size=20x20&hash="+$rootScope.userPicturesHash+'" alt="" /><span class="name">'+o+'</span><span class="time">'+t+"</span>"+e+"</div>";$(".shoutbox__messages").append(s).children(":last").hide().fadeIn();var n=$(".shoutbox__messages")[0].scrollHeight;$(".shoutbox__messages").scrollTop(n)}function l(o){r(c,o),Notification.broadcastToOtherSessions("shoutbox-receive-message",{from:c,message:o})}function u(){$("#shoutbox__sound").trigger("play")}var c="Unknown",g={};return window.addEventListener("storage",function(o){"shoutbox.settings"===o.key&&(console.log("Settings updated (from another window)"),console.log(o.newValue),g=JSON.parse(o.newValue),i())}),Notification.registerEvent("shoutbox-receive-message",function(o,e){r(e.from,e.message),e.from!==c&&(u(),"closed"==s()&&a({unread_message:!0}))}),{init:e,toggleDisplay:t}}();shoutBox.init();