You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1 lines
7.5 KiB
1 lines
7.5 KiB
(function(e,t,n,o){e.fn.chat=function(t,n,i){var a=e.extend(!0,{},e.fn.chat.settings,i),r=arguments||!1;return e(this).each(function(){var i,s,c,l,u,d,m=e(this),f=m.find(a.selector.expandButton),p=m.find(a.selector.userListButton),g=m.find(a.selector.userList),v=(m.find(a.selector.room),m.find(a.selector.userCount)),h=m.find(a.selector.log),b=(m.find(a.selector.message),m.find(a.selector.messageInput)),y=m.find(a.selector.messageButton),x=m.data("module"),C=a.className,w=a.namespace,T="",k={};return d={channel:!1,width:{log:h.width(),userList:g.outerWidth()},initialize:function(){return Pusher===o&&d.error(a.errors.pusher),t===o||n===o?(d.error(a.errors.key),!1):a.endpoint.message||a.endpoint.authentication?(u=new Pusher(t),Pusher.channel_auth_endpoint=a.endpoint.authentication,d.channel=u.subscribe(n),d.channel.bind("pusher:subscription_succeeded",d.user.list.create),d.channel.bind("pusher:subscription_error",d.error),d.channel.bind("pusher:member_added",d.user.joined),d.channel.bind("pusher:member_removed",d.user.left),d.channel.bind("update_messages",d.message.receive),e.each(a.customEvents,function(e,t){d.channel.bind(e,t)}),e.fn.hoverClass!==o&&e.fn.downClass!==o&&(f.hoverClass().downClass(),p.hoverClass().downClass(),y.hoverClass().downClass()),p.on("click."+w,d.event.toggleUserList),f.on("click."+w,d.event.toggleExpand),b.on("keydown."+w,d.event.input.keydown).on("keyup."+w,d.event.input.keyup),y.on("mouseenter."+w,d.event.hover).on("mouseleave."+w,d.event.hover).on("click."+w,d.event.submit),h.animate({scrollTop:h.prop("scrollHeight")},400),m.data("module",d).addClass(C.loading),o):(d.error(a.errors.endpoint),!1)},refresh:function(){p.removeClass(C.active),d.width={log:h.width(),userList:g.outerWidth()},p.hasClass(C.active)&&d.user.list.hide(),m.data("module",d)},user:{updateCount:function(){a.userCount&&(k=m.data("users"),c=0,e.each(k,function(){c++}),v.html(a.templates.userCount(c)))},joined:function(t){k=m.data("users"),"anonymous"!=t.id&&k[t.id]===o&&(k[t.id]=t.info,a.randomColor&&t.info.color===o&&(t.info.color=a.templates.color(t.id)),T=a.templates.userList(t.info),t.info.isAdmin?e(T).prependTo(g).preview({type:"user",placement:"left"}):e(T).appendTo(g).preview({type:"user",placement:"left"}),e.fn.preview!==o&&g.children().last().preview({type:"user",placement:"left"}),a.partingMessages&&(h.append(a.templates.joined(t.info)),d.message.scroll.test()),d.user.updateCount())},left:function(e){k=m.data("users"),e!==o&&"anonymous"!==e.id&&(delete k[e.id],m.data("users",k),g.find("[data-id="+e.id+"]").remove(),a.partingMessages&&(h.append(a.templates.left(e.info)),d.message.scroll.test()),d.user.updateCount())},list:{create:function(t){k={},t.each(function(e){"anonymous"!==e.id&&"undefined"!==e.id&&(a.randomColor&&e.info.color===o&&(e.info.color=a.templates.color(e.id)),T=e.info.isAdmin?a.templates.userList(e.info)+T:T+a.templates.userList(e.info),k[e.id]=e.info)}),m.data("users",k).data("user",k[t.me.id]).removeClass(C.loading),g.html(T),e.fn.preview!==o&&g.children().preview({type:"user",placement:"left"}),d.user.updateCount(),e.proxy(a.onJoin,g.children())()},show:function(){h.animate({width:d.width.log-d.width.userList},{duration:a.speed,easing:a.easing,complete:d.message.scroll.move})},hide:function(){h.stop().animate({width:d.width.log},{duration:a.speed,easing:a.easing,complete:d.message.scroll.move})}}},message:{scroll:{test:function(){l=h.prop("scrollHeight")-h.height(),Math.abs(h.scrollTop()-l)<a.scrollArea&&d.message.scroll.move()},move:function(){l=h.prop("scrollHeight")-h.height(),h.scrollTop(l)}},send:function(t){d.utils.emptyString(t)||e.api({url:a.endpoint.message,method:"POST",data:{chat_message:{content:t,timestamp:(new Date).getTime()}}})},receive:function(e){s=e.data,k=m.data("users"),i=m.data("user"),k[s.userID]!==o&&(i===o||i.id!=s.userID)&&(s.user=k[s.userID],d.message.display(s))},display:function(t){h.append(a.templates.message(t)),d.message.scroll.test(),e.proxy(a.onMessage,h.children().last())()}},expand:function(){m.addClass(C.expand),e.proxy(a.onExpand,m)(),d.refresh()},contract:function(){m.removeClass(C.expand),e.proxy(a.onContract,m)(),d.refresh()},event:{input:{keydown:function(e){13==e.which&&y.addClass(C.down)},keyup:function(e){13==e.which&&(y.removeClass(C.down),d.event.submit())}},submit:function(){var e=b.val(),t=m.data("user");t===o||d.utils.emptyString(e)||(d.message.send(e),d.message.display({user:t,text:e}),d.message.scroll.move(),b.val(""))},toggleExpand:function(){m.hasClass(C.expand)?(f.removeClass(C.active),d.contract()):(f.addClass(C.active),d.expand())},toggleUserList:function(){h.is(":animated")||(p.hasClass(C.active)?(p.removeClass("active"),d.user.list.hide()):(p.addClass(C.active),d.user.list.show()))}},utils:{emptyString:function(e){return"string"==typeof e?-1==e.search(/\S/):!1}},debug:function(e){a.debug&&console.info(a.moduleName+": "+e)},error:function(e){console.warn(a.moduleName+": "+e)},invoke:function(t,n,i){var r;return i=i||Array.prototype.slice.call(arguments,2),"string"==typeof t&&x!==o&&(t=t.split("."),e.each(t,function(t,n){return e.isPlainObject(x[n])?(x=x[n],!0):e.isFunction(x[n])?(r=x[n],!0):(d.error(a.errors.method),!1)})),e.isFunction(r)?r.apply(n,i):!1}},x!==o&&r?("invoke"==r[0]&&(r=Array.prototype.slice.call(r,1)),d.invoke(r[0],this,Array.prototype.slice.call(r,1))):(d.initialize(),o)}),this},e.fn.chat.settings={moduleName:"Chat Module",debug:!1,namespace:"chat",onJoin:function(){},onMessage:function(){},onExpand:function(){},onContract:function(){},customEvents:{},partingMessages:!1,userCount:!0,randomColor:!0,speed:300,easing:"easeOutQuint",scrollArea:9999,endpoint:{message:!1,authentication:!1},errors:{method:"The method you called is not defined",endpoint:"Please define a message and authentication endpoint.",key:"You must specify a pusher key and channel.",pusher:"You must include the Pusher library."},className:{expand:"expand",active:"active",hover:"hover",down:"down",loading:"loading"},selector:{userCount:".actions .message",userListButton:".actions .button.user-list",expandButton:".actions .button.expand",room:".room",userList:".room .user-list",log:".room .log",message:".room .log .message",author:".room log .message .author",messageInput:".talk input",messageButton:".talk .send.button"},templates:{userCount:function(e){return e+" users in chat"},color:function(){var e=["#000000","#333333","#666666","#999999","#CC9999","#CC6666","#CC3333","#993333","#663333","#CC6633","#CC9966","#CC9933","#999966","#CCCC66","#99CC66","#669933","#669966","#33A3CC","#336633","#33CCCC","#339999","#336666","#336699","#6666CC","#9966CC","#333399","#663366","#996699","#993366","#CC6699"];return e[Math.floor(Math.random()*e.length)]},message:function(e){var t="";return e.user.isAdmin?(e.user.color="#55356A",t+='<div class="admin message">',t+='<span class="quirky ui flag team"></span>'):t+='<div class="message">',t+="<p>",t+=e.user.color!==o?'<span class="author" style="color: '+e.user.color+';">'+e.user.name+"</span>: ":'<span class="author">'+e.user.name+"</span>: ",t+=""+e.text+" </p>"+"</div>"},joined:function(e){return typeof e.name!==o?'<div class="status">'+e.name+" has joined the chat.</div>":!1},left:function(e){return typeof e.name!==o?'<div class="status">'+e.name+" has left the chat.</div>":!1},userList:function(e){var t="";return e.isAdmin&&(e.color="#55356A"),t+='<div class="user" data-id="'+e.id+'">'+' <div class="image">'+' <img src="'+e.avatarURL+'">'+" </div>",t+=e.color!==o?' <p><a href="/users/'+e.id+'" target="_blank" style="color: '+e.color+';">'+e.name+"</a></p>":' <p><a href="/users/'+e.id+'" target="_blank">'+e.name+"</a></p>",t+="</div>"}}}})(jQuery,window,document);
|