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,a){e.fn.chat=function(t,n,o){var i=e.extend(!0,{},e.fn.chat.settings,o),s=arguments||!1;return e(this).each(function(){var o,r,l,c,u,d,f=e(this),m=f.find(i.selector.expandButton),p=f.find(i.selector.userListButton),g=f.find(i.selector.userList),h=(f.find(i.selector.room),f.find(i.selector.userCount)),v=f.find(i.selector.log),b=(f.find(i.selector.message),f.find(i.selector.messageInput)),y=f.find(i.selector.messageButton),x=f.data("module"),C=i.className,w=i.namespace,T="",k={};return d={channel:!1,width:{log:v.width(),userList:g.outerWidth()},initialize:function(){return Pusher===a&&d.error(i.errors.pusher),t===a||n===a?(d.error(i.errors.key),!1):i.endpoint.message||i.endpoint.authentication?(u=new Pusher(t),Pusher.channel_auth_endpoint=i.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(i.customEvents,function(e,t){d.channel.bind(e,t)}),e.fn.hoverClass!==a&&e.fn.downClass!==a&&(m.hoverClass().downClass(),p.hoverClass().downClass(),y.hoverClass().downClass()),p.on("click."+w,d.event.toggleUserList),m.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),v.animate({scrollTop:v.prop("scrollHeight")},400),f.data("module",d).addClass(C.loading),a):(d.error(i.errors.endpoint),!1)},refresh:function(){p.removeClass(C.active),d.width={log:v.width(),userList:g.outerWidth()},p.hasClass(C.active)&&d.user.list.hide(),f.data("module",d)},user:{updateCount:function(){i.userCount&&(k=f.data("users"),l=0,e.each(k,function(){l++}),h.html(i.templates.userCount(l)))},joined:function(t){k=f.data("users"),"anonymous"!=t.id&&k[t.id]===a&&(k[t.id]=t.info,i.randomColor&&t.info.color===a&&(t.info.color=i.templates.color(t.id)),T=i.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!==a&&g.children().last().preview({type:"user",placement:"left"}),i.partingMessages&&(v.append(i.templates.joined(t.info)),d.message.scroll.test()),d.user.updateCount())},left:function(e){k=f.data("users"),e!==a&&"anonymous"!==e.id&&(delete k[e.id],f.data("users",k),g.find("[data-id="+e.id+"]").remove(),i.partingMessages&&(v.append(i.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&&(i.randomColor&&e.info.color===a&&(e.info.color=i.templates.color(e.id)),T=e.info.isAdmin?i.templates.userList(e.info)+T:T+i.templates.userList(e.info),k[e.id]=e.info)}),f.data("users",k).data("user",k[t.me.id]).removeClass(C.loading),g.html(T),e.fn.preview!==a&&g.children().preview({type:"user",placement:"left"}),d.user.updateCount(),e.proxy(i.onJoin,g.children())()},show:function(){v.animate({width:d.width.log-d.width.userList},{duration:i.speed,easing:i.easing,complete:d.message.scroll.move})},hide:function(){v.stop().animate({width:d.width.log},{duration:i.speed,easing:i.easing,complete:d.message.scroll.move})}}},message:{scroll:{test:function(){c=v.prop("scrollHeight")-v.height(),Math.abs(v.scrollTop()-c)<i.scrollArea&&d.message.scroll.move()},move:function(){c=v.prop("scrollHeight")-v.height(),v.scrollTop(c)}},send:function(t){d.utils.emptyString(t)||e.api({url:i.endpoint.message,method:"POST",data:{chat_message:{content:t,timestamp:(new Date).getTime()}}})},receive:function(e){r=e.data,k=f.data("users"),o=f.data("user"),k[r.userID]!==a&&(o===a||o.id!=r.userID)&&(r.user=k[r.userID],d.message.display(r))},display:function(t){v.append(i.templates.message(t)),d.message.scroll.test(),e.proxy(i.onMessage,v.children().last())()}},expand:function(){f.addClass(C.expand),e.proxy(i.onExpand,f)(),d.refresh()},contract:function(){f.removeClass(C.expand),e.proxy(i.onContract,f)(),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=f.data("user");t===a||d.utils.emptyString(e)||(d.message.send(e),d.message.display({user:t,text:e}),d.message.scroll.move(),b.val(""))},toggleExpand:function(){f.hasClass(C.expand)?(m.removeClass(C.active),d.contract()):(m.addClass(C.active),d.expand())},toggleUserList:function(){v.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){i.debug&&console.info(i.moduleName+": "+e)},error:function(e){console.warn(i.moduleName+": "+e)},invoke:function(t,n,o){var s;return o=o||Array.prototype.slice.call(arguments,2),"string"==typeof t&&x!==a&&(t=t.split("."),e.each(t,function(t,n){return e.isPlainObject(x[n])?(x=x[n],!0):e.isFunction(x[n])?(s=x[n],!0):(d.error(i.errors.method),!1)})),e.isFunction(s)?s.apply(n,o):!1}},x!==a&&s?("invoke"==s[0]&&(s=Array.prototype.slice.call(s,1)),d.invoke(s[0],this,Array.prototype.slice.call(s,1))):(d.initialize(),a)}),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!==a?'<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!==a?'<div class="status">'+e.name+" has joined the chat.</div>":!1},left:function(e){return typeof e.name!==a?'<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!==a?' <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);
|