text: "use strict";angular.module("angularTalk",[]).directive("angularTalk",["$http","$timeout","windowStatus",function(n,e,i){return{restrict:"AC",scope:!0,templateUrl:"angularTalk/room.html",link:function(t,o,r){function a(n){if(n.id){if(s(n.id))return;n.id>f&&(f=n.id),(n.id<g||angular.isUndefined(g))&&(g=n.id)}n.$replies=[],n.replyToID?l[n.id]=n:t.messages.push(n);do{var e=0;angular.forEach(l,function(n,i){var t=s(n.replyToID);t&&(t.$replies.push(n),delete l[i],e++)})}while(e>0);t.$emit("messageReceived",n)}function s(n,e){var i=!1;return angular.forEach(e||t.messages,function(e){e.id&&e.id==n&&(i=e),!i&&e.$replies&&(i=s(n,e.$replies))}),i}function d(e,o,r){t.loading=!0,n.get(c.ajaxEndpoint,{params:angular.extend({method:"messages",since:f,dir:"ASC",count:25},e)}).success(function(n){n.data&&(angular.forEach(n.data,a),n.data.length>0&&m&&i.hidden&&m.play(),o&&o(n)),t.loading=!1}).error(function(){t.loading=!1,o&&o(!1)})}function u(){t.settings.updateInterval&&e(function(){d({},u)},t.settings.updateInterval)}var c=t.settings={};if(angular.forEach(r,function(n,e){""==n?t.settings[e]=!0:["strings","sender","soundOnNew"].indexOf(e)>=0?t.settings[e]=t.$eval(n):t.settings[e]=n}),!c.ajaxEndpoint)throw new Error("Invalid ajax endpoint");t.messages=[],t.message={};var l={};t.save=function(e){e.isSending=!0,e.isError=!1,e.isEditing=!1;var i={};angular.forEach(e,function(n,e){"$"!=e[0]&&0!==e.indexOf("is")&&(i[e]=n)}),n.post(c.ajaxEndpoint,i,{params:{method:e.id?"update":"submit"}}).success(function(n){e.isSending=!1,n.data&&(e.isError=!1,angular.extend(e,n.data),t.$emit("messageSend",e))}).error(function(){e.isSending=!1,e.isError=!0}),t.content=""},t.submit=function(){var n=angular.extend(t.message,{author:c.sender,date:new Date/1e3|0});a(n),t.save(n),t.message={}},t.current={},t.messageKeyPress=function(n){return c.submitOnEnter&&13==n.keyCode?(t.submit(),n.preventDefault(),!1):void(27==n.keyCode&&(t.cancelEdit(t.current.message),t.current.message=null))},t.reply=function(n){n.$replies.push({author:c.sender,isEditing:!0,content:"",date:new Date/1e3|0,replyToID:n.id})},t.edit=function(n){n.isEditing=!0,n.originalContent=n.content},t.cancelEdit=function(n){n.isEditing=!1,n.content=n.originalContent,n.id||t["delete"](n)},t["delete"]=function(e){function i(){function n(e,i){var t=i.indexOf(e);t>=0&&i.splice(t,1),angular.forEach(i,function(i){i.$replies&&n(e,i.$replies)})}n(e,t.messages)}e.id?confirm(c.strings.delete_confirm)&&n.post(c.ajaxEndpoint,e,{params:{method:"delete"}}).success(i):i()},t.previousMessage=function(n){var e=void 0;return angular.forEach(t.messages,function(i){i.replyToID==n.replyToID&&i.id!=n.id&&i.date<=n.date&&(!e||e.date<i.date)&&(e=i)}),e};var g,f=0,m=null;c.soundOnNew&&(angular.isString(c.soundOnNew)?m=new Audio(c.soundOnNew):(m=document.createElement("audio"),angular.forEach(c.soundOnNew,function(n,e){var i=document.createElement("source");i.type=e,i.src=n,m.appendChild(i)}))),t.loadingOlder=!1,t.disableOlder=!1,t.onScroll=function(n){t.disableOlder||0!=n||(console.log("Load old messages"),t.loadingOlder=!0,d({since:g,dir:"DESC"},function(n){0==n.data.length&&(t.disableOlder=!0),t.loadingOlder=!1}))},d({},u,!0),t.$on("$destroy",function(){t.settings.updateInterval=!1})}}}]).directive("angularTalkScroll",function(){return{priority:1,scope:{onScroll:"&"},restrict:"A",link:function(n,e){function i(){o.scrollTop=o.scrollHeight}function t(){return o.scrollTop+o.clientHeight+1>=o.scrollHeight}var o=e[0],r=!0;n.$watch(function(){r&&i()}),e.bind("scroll",function(){r=t(),n.onScroll({offset:o.scrollTop})})}}}).directive("init",function(){return{priority:0,compile:function(){return{pre:function(n,e,i){n.$eval(i.init)}}}}}).service("windowStatus",function(){function n(n){var t={focus:!0,focusin:!0,pageshow:!0,blur:!1,focusout:!1,pagehide:!1};n=n||window.event,n.type in t?e.visible=t[n.type]:e.visible=this[i]?!1:!0,e.hidden=!e.visible}var e={visible:!0,hidden:!1},i="hidden";return i in document?document.addEventListener("visibilitychange",n):(i="mozHidden")in document?document.addEventListener("mozvisibilitychange",n):(i="webkitHidden")in document?document.addEventListener("webkitvisibilitychange",n):(i="msHidden")in document?document.addEventListener("msvisibilitychange",n):"onfocusin"in document?document.onfocusin=document.onfocusout=n:window.onpageshow=window.onpagehide=window.onfocus=window.onblur=n,void 0!==document[i]&&n({type:document[i]?"blur":"focus"}),e}).run(["windowStatus",function(){}]);