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.

10 lines
8.5 KiB

10 years ago
10 years ago
  1. /*
  2. * # Semantic UI
  3. * https://github.com/Semantic-Org/Semantic-UI
  4. * http://beta.semantic-ui.com/
  5. *
  6. * Copyright 2014 Contributors
  7. * Released under the MIT license
  8. * http://opensource.org/licenses/MIT
  9. *
  10. */
  11. !function(e,n,t,s){"use strict";e.fn.chatroom=function(n){var t,o=e(this),a=o.selector||"",i=(new Date).getTime(),r=[],u=arguments[0],l="string"==typeof u,c=[].slice.call(arguments,1);return e(this).each(function(){var t,o,d,m,p,g,f,h=e.extend(!0,{},e.fn.chatroom.settings,n),v=h.className,C=h.namespace,y=h.selector,b=h.error,w=e(this),x=w.find(y.expandButton),k=w.find(y.userListButton),T=w.find(y.userList),L=(w.find(y.room),w.find(y.userCount)),E=w.find(y.log),A=(w.find(y.message),w.find(y.messageInput)),j=w.find(y.messageButton),P=w.data("module"),_=this,M="",B={};f={width:{log:E.width(),userList:T.outerWidth()},initialize:function(){return Pusher===s&&f.error(b.pusher),h.key===s||h.channelName===s?(f.error(b.key),!1):h.endpoint.message||h.endpoint.authentication?(g=new Pusher(h.key),Pusher.channel_auth_endpoint=h.endpoint.authentication,t=g.subscribe(h.channelName),t.bind("pusher:subscription_succeeded",f.user.list.create),t.bind("pusher:subscription_error",f.error),t.bind("pusher:member_added",f.user.joined),t.bind("pusher:member_removed",f.user.left),t.bind("update_messages",f.message.receive),e.each(h.customEvents,function(e,n){t.bind(e,n)}),k.on("click."+C,f.event.toggleUserList),x.on("click."+C,f.event.toggleExpand),A.on("keydown."+C,f.event.input.keydown).on("keyup."+C,f.event.input.keyup),j.on("mouseenter."+C,f.event.hover).on("mouseleave."+C,f.event.hover).on("click."+C,f.event.submit),E.animate({scrollTop:E.prop("scrollHeight")},400),void w.data("module",f).addClass(v.loading)):(f.error(b.endpoint),!1)},refresh:function(){k.removeClass(v.active),f.width={log:E.width(),userList:T.outerWidth()},k.hasClass(v.active)&&f.user.list.hide(),w.data("module",f)},user:{updateCount:function(){h.userCount&&(B=w.data("users"),m=0,e.each(B,function(){m++}),L.html(h.templates.userCount(m)))},joined:function(n){B=w.data("users"),"anonymous"!=n.id&&B[n.id]===s&&(B[n.id]=n.info,h.randomColor&&n.info.color===s&&(n.info.color=h.templates.color(n.id)),M=h.templates.userList(n.info),n.info.isAdmin?e(M).prependTo(T):e(M).appendTo(T),h.partingMessages&&(E.append(h.templates.joined(n.info)),f.message.scroll.test()),f.user.updateCount())},left:function(e){B=w.data("users"),e!==s&&"anonymous"!==e.id&&(delete B[e.id],w.data("users",B),T.find("[data-id="+e.id+"]").remove(),h.partingMessages&&(E.append(h.templates.left(e.info)),f.message.scroll.test()),f.user.updateCount())},list:{create:function(n){B={},n.each(function(e){"anonymous"!==e.id&&"undefined"!==e.id&&(h.randomColor&&e.info.color===s&&(e.info.color=h.templates.color(e.id)),M=e.info.isAdmin?h.templates.userList(e.info)+M:M+h.templates.userList(e.info),B[e.id]=e.info)}),w.data("users",B).data("user",B[n.me.id]).removeClass(v.loading),T.html(M),f.user.updateCount(),e.proxy(h.onJoin,T.children())()},show:function(){E.animate({width:f.width.log-f.width.userList},{duration:h.speed,easing:h.easing,complete:f.message.scroll.move})},hide:function(){E.stop().animate({width:f.width.log},{duration:h.speed,easing:h.easing,complete:f.message.scroll.move})}}},message:{scroll:{test:function(){p=E.prop("scrollHeight")-E.height(),Math.abs(E.scrollTop()-p)<h.scrollArea&&f.message.scroll.move()},move:function(){p=E.prop("scrollHeight")-E.height(),E.scrollTop(p)}},send:function(n){f.utils.emptyString(n)||e.api({url:h.endpoint.message,method:"POST",data:{message:{content:n,timestamp:(new Date).getTime()}}})},receive:function(e){d=e.data,B=w.data("users"),o=w.data("user"),B[d.userID]!==s&&(o===s||o.id!=d.userID)&&(d.user=B[d.userID],f.message.display(d))},display:function(n){E.append(h.templates.message(n)),f.message.scroll.test(),e.proxy(h.onMessage,E.children().last())()}},expand:function(){w.addClass(v.expand),e.proxy(h.onExpand,w)(),f.refresh()},contract:function(){w.removeClass(v.expand),e.proxy(h.onContract,w)(),f.refresh()},event:{input:{keydown:function(e){13==e.which&&j.addClass(v.down)},keyup:function(e){13==e.which&&(j.removeClass(v.down),f.event.submit())}},submit:function(){var e=A.val(),n=w.data("user");n===s||f.utils.emptyString(e)||(f.message.send(e),f.message.display({user:n,text:e}),