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.1 KiB

!function(a,b,c,d){a.fn.chat=function(b,c,e){var f=a.extend(!0,{},a.fn.chat.settings,e),g=arguments||!1;return a(this).each(function(){var e,h,i,j,k,l,m,n=a(this),o=n.find(f.selector.expandButton),p=n.find(f.selector.userListButton),q=n.find(f.selector.userList),r=(n.find(f.selector.room),n.find(f.selector.userCount)),s=n.find(f.selector.log),t=(n.find(f.selector.message),n.find(f.selector.messageInput)),u=n.find(f.selector.messageButton),v=n.data("module"),w=f.className,x=f.namespace,y="",z={};return m={width:{log:s.width(),userList:q.outerWidth()},initialize:function(){return Pusher===d&&m.error(f.errors.pusher),b===d||c===d?(m.error(f.errors.key),!1):f.endpoint.message||f.endpoint.authentication?(l=new Pusher(b),Pusher.channel_auth_endpoint=f.endpoint.authentication,e=l.subscribe(c),e.bind("pusher:subscription_succeeded",m.user.list.create),e.bind("pusher:subscription_error",m.error),e.bind("pusher:member_added",m.user.joined),e.bind("pusher:member_removed",m.user.left),e.bind("update_messages",m.message.receive),a.each(f.customEvents,function(a,b){e.bind(a,b)}),p.on("click."+x,m.event.toggleUserList),o.on("click."+x,m.event.toggleExpand),t.on("keydown."+x,m.event.input.keydown).on("keyup."+x,m.event.input.keyup),u.on("mouseenter."+x,m.event.hover).on("mouseleave."+x,m.event.hover).on("click."+x,m.event.submit),s.animate({scrollTop:s.prop("scrollHeight")},400),n.data("module",m).addClass(w.loading),void 0):(m.error(f.errors.endpoint),!1)},refresh:function(){p.removeClass(w.active),m.width={log:s.width(),userList:q.outerWidth()},p.hasClass(w.active)&&m.user.list.hide(),n.data("module",m)},user:{updateCount:function(){f.userCount&&(z=n.data("users"),j=0,a.each(z,function(){j++}),r.html(f.templates.userCount(j)))},joined:function(b){z=n.data("users"),"anonymous"!=b.id&&z[b.id]===d&&(z[b.id]=b.info,f.randomColor&&b.info.color===d&&(b.info.color=f.templates.color(b.id)),y=f.templates.userList(b.info),b.info.isAdmin?a(y).prependTo(q):a(y).appendTo(q),f.partingMessages&&(s.append(f.templates.joined(b.info)),m.message.scroll.test()),m.user.updateCount())},left:function(a){z=n.data("users"),a!==d&&"anonymous"!==a.id&&(delete z[a.id],n.data("users",z),q.find("[data-id="+a.id+"]").remove(),f.partingMessages&&(s.append(f.templates.left(a.info)),m.message.scroll.test()),m.user.updateCount())},list:{create:function(b){z={},b.each(function(a){"anonymous"!==a.id&&"undefined"!==a.id&&(f.randomColor&&a.info.color===d&&(a.info.color=f.templates.color(a.id)),y=a.info.isAdmin?f.templates.userList(a.info)+y:y+f.templates.userList(a.info),z[a.id]=a.info)}),n.data("users",z).data("user",z[b.me.id]).removeClass(w.loading),q.html(y),m.user.updateCount(),a.proxy(f.onJoin,q.children())()},show:function(){s.animate({width:m.width.log-m.width.userList},{duration:f.speed,easing:f.easing,complete:m.message.scroll.move})},hide:function(){s.stop().animate({width:m.width.log},{duration:f.speed,easing:f.easing,complete:m.message.scroll.move})}}},message:{scroll:{test:function(){k=s.prop("scrollHeight")-s.height(),Math.abs(s.scrollTop()-k)<f.scrollArea&&m.message.scroll.move()},move:function(){k=s.prop("scrollHeight")-s.height(),s.scrollTop(k)}},send:function(b){m.utils.emptyString(b)||a.api({url:f.endpoint.message,method:"POST",data:{chat_message:{content:b,timestamp:(new Date).getTime()}}})},receive:function(a){i=a.data,z=n.data("users"),h=n.data("user"),z[i.userID]!==d&&(h===d||h.id!=i.userID)&&(i.user=z[i.userID],m.message.display(i))},display:function(b){s.append(f.templates.message(b)),m.message.scroll.test(),a.proxy(f.onMessage,s.children().last())()}},expand:function(){n.addClass(w.expand),a.proxy(f.onExpand,n)(),m.refresh()},contract:function(){n.removeClass(w.expand),a.proxy(f.onContract,n)(),m.refresh()},event:{input:{keydown:function(a){13==a.which&&u.addClass(w.down)},keyup:function(a){13==a.which&&(u.removeClass(w.down),m.event.submit())}},submit:function(){var a=t.val(),b=n.data("user");b===d||m.utils.emptyString(a)||(m.message.send(a),m.message.display({user:b,text:a}),m.message.scroll.move(),t.val(""))},toggleExpand:function(){n.hasClass(w.expand)?(o.removeClass(w.active),m.contract()):(o.addClass(w.active),m.expand())},toggleUserList:function(){s.is(":animated")||(p.hasClass(w.active)?(p.removeClass("active"),m.user.list.hide()):(p.addClass(w.active),m.user.list.show()))}},utils:{emptyString:function(a){return"string"==typeof a?-1==a.search(/\S/):!1}},debug:function(a){f.debug&&console.info(f.moduleName+": "+a)},error:function(a){console.warn(f.moduleName+": "+a)},invoke:function(b,c,e){var g;return e=e||Array.prototype.slice.call(arguments,2),"string"==typeof b&&v!==d&&(b=b.split("."),a.each(b,function(b,c){return a.isPlainObject(v[c])?(v=v[c],!0):a.isFunction(v[c])?(g=v[c],!0):(m.error(f.errors.method),!1)})),a.isFunction(g)?g.apply(c,e):!1}},v!==d&&g?("invoke"==g[0]&&(g=Array.prototype.slice.call(g,1)),m.invoke(g[0],this,Array.prototype.slice.call(g,1))):(m.initialize(),void 0)}),this},a.fn.chat.settings={moduleName:"Chat",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(a){return a+" users in chat"},color:function(){var a=["#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 a[Math.floor(Math.random()*a.length)]},message:function(a){var b="";return a.user.isAdmin?(a.user.color="#55356A",b+='<div class="admin message">',b+='<span class="quirky ui flag team"></span>'):b+='<div class="message">',b+="<p>",b+=a.user.color!==d?'<span class="author" style="color: '+a.user.color+';">'+a.user.name+"</span>: ":'<span class="author">'+a.user.name+"</span>: ",b+=""+a.text+" </p>"+"</div>"},joined:function(a){return typeof a.name!==d?'<div class="status">'+a.name+" has joined the chat.</div>":!1},left:function(a){return typeof a.name!==d?'<div class="status">'+a.name+" has left the chat.</div>":!1},userList:function(a){var b="";return a.isAdmin&&(a.color="#55356A"),b+='<div class="user" data-id="'+a.id+'">'+' <div class="image">'+' <img src="'+a.avatarURL+'">'+" </div>",b+=a.color!==d?' <p><a href="/users/'+a.id+'" target="_blank" style="color: '+a.color+';">'+a.name+"</a></p>":' <p><a href="/users/'+a.id+'" target="_blank">'+a.name+"</a></p>",b+="</div>"}}}}(jQuery,window,document);