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.
 
 
 

11 lines
8.5 KiB

/*
* # Semantic UI
* https://github.com/Semantic-Org/Semantic-UI
* http://beta.semantic-ui.com/
*
* Copyright 2014 Contributors
* Released under the MIT license
* http://opensource.org/licenses/MIT
*
*/
!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}),f.message.scroll.move(),A.val(""))},toggleExpand:function(){w.hasClass(v.expand)?(x.removeClass(v.active),f.contract()):(x.addClass(v.active),f.expand())},toggleUserList:function(){E.is(":animated")||(k.hasClass(v.active)?(k.removeClass("active"),f.user.list.hide()):(k.addClass(v.active),f.user.list.show()))}},utils:{emptyString:function(e){return"string"==typeof e?-1==e.search(/\S/):!1}},setting:function(n,t){return t===s?h[n]:void(e.isPlainObject(n)?e.extend(!0,h,n):h[n]=t)},internal:function(n,t){if(e.isPlainObject(n))e.extend(!0,f,n);else{if(t===s)return f[n];f[n]=t}},debug:function(){h.debug&&(h.performance?f.performance.log(arguments):(f.debug=Function.prototype.bind.call(console.info,console,h.name+":"),f.debug.apply(console,arguments)))},verbose:function(){h.verbose&&h.debug&&(h.performance?f.performance.log(arguments):(f.verbose=Function.prototype.bind.call(console.info,console,h.name+":"),f.verbose.apply(console,arguments)))},error:function(){f.error=Function.prototype.bind.call(console.error,console,h.name+":"),f.error.apply(console,arguments)},performance:{log:function(e){var n,t,s;h.performance&&(n=(new Date).getTime(),s=i||n,t=n-s,i=n,r.push({Element:_,Name:e[0],Arguments:[].slice.call(e,1)||"","Execution Time":t})),clearTimeout(f.performance.timer),f.performance.timer=setTimeout(f.performance.display,100)},display:function(){var n=h.name+":",t=0;i=!1,clearTimeout(f.performance.timer),e.each(r,function(e,n){t+=n["Execution Time"]}),n+=" "+t+"ms",a&&(n+=" '"+a+"'"),(console.group!==s||console.table!==s)&&r.length>0&&(console.groupCollapsed(n),console.table?console.table(r):e.each(r,function(e,n){console.log(n.Name+": "+n["Execution Time"]+"ms")}),console.groupEnd()),r=[]}},invoke:function(n,t,o){var a,i;return t=t||c,o=_||o,"string"==typeof n&&P!==s&&(n=n.split(/[\. ]/),a=n.length-1,e.each(n,function(t,o){e.isPlainObject(P[o])&&t!=a?P=P[o]:P[o]!==s?i=P[o]:f.error(b.method,n)})),e.isFunction(i)?i.apply(o,t):i||!1}},l?(P===s&&f.initialize(),f.invoke(u)):(P!==s&&f.destroy(),f.initialize())}),t!==s?t:this},e.fn.chatroom.settings={name:"Chat",namespace:"chat",debug:!1,channel:"present-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},error:{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 .list.button",expandButton:".actions .expand.button",room:".room",userList:".room .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 n="";return e.user.isAdmin?(e.user.color="#55356A",n+='<div class="admin message">',n+='<span class="quirky ui flag team"></span>'):n+='<div class="message">',n+="<p>",n+=e.user.color!==s?'<span class="author" style="color: '+e.user.color+';">'+e.user.name+"</span>: ":'<span class="author">'+e.user.name+"</span>: ",n+=""+e.text+" </p></div>"},joined:function(e){return typeof e.name!==s?'<div class="status">'+e.name+" has joined the chat.</div>":!1},left:function(e){return typeof e.name!==s?'<div class="status">'+e.name+" has left the chat.</div>":!1},userList:function(e){var n="";return e.isAdmin&&(e.color="#55356A"),n+='<div class="user" data-id="'+e.id+'"> <div class="image"> <img src="'+e.avatarURL+'"> </div>',n+=e.color!==s?' <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>",n+="</div>"}}}}(jQuery,window,document);