<?php

/* ========================================================================
 * $Id: group.php 4485 2017-10-21 08:40:49Z onez $
 * 
 * Email: www@onez.cn
 * QQ: 6200103
 * HomePage: http://www.onezphp.com
 * ========================================================================
 * Copyright 2016-2017 佳蓝科技.
 * 
 * ======================================================================== */

!defined('IN_ONEZ') && exit('Access Denied');

?>
<link rel="stylesheet" href="<?php echo onez('chat.group.plugin.at')->url?>/images/group.css" />
<script type="text/javascript">
onezgroup.prototype.old_ubbtohtml=group.ubbtohtml;
onezgroup.prototype.ubbtohtml=function(fdata){
  fdata=this.old_ubbtohtml(fdata);
  fdata=fdata.replace(new RegExp('\\[at=([^\\]]+)\\]@(.+?)\\[\/at\\]','gi'),'<em class="onez-at" data-at="$1" data-at-uname="@$2" onclick="group.at_select(\'$1\',\'$2\')" style="cursor:pointer">@$2</em>');
  return fdata;
};
$(function(){
  $('.chat').bind('message-print',function(e,data){
    var that=group;
    var item=data.item;
    var msg=data.msg;
    if(typeof msg.isnew!='undefined' && msg.isnew){
      item.find('[data-at]').each(function(){
        var myLi=item.clone();
        myLi.find('img.emote').replaceWith('<span>[表情]</span>');
        myLi.find('img').replaceWith('<span>[图片]</span>');
        var at=$(this).attr('data-at');
        var text='<strong class="uname" onclick="group.at_select(\''+msg.userid+'\',\''+msg.nickname+'\');group.at_close(this,true)">'+msg.nickname+'</strong> : '+myLi.find('.im_content').text()+' [<strong class="reply" onclick="group.at_select(\''+msg.userid+'\',\''+msg.nickname+'\');group.at_close(this,true)">回复</strong>]';
        if(at=='all'){
          that.show_at('top',text);
        }else if(at==that.userid){
          that.show_at('bottom',text);
        }
      });
    }
  });
});
function _at2(){
  var uid=$('.admin-btns').attr('data-uid');
  if(uid=='all' || uid=='undefined'){
    return;
  }
  var uname=$('.admin-btns').attr('data-uname');
  group.at_select(uid,uname);
};
group.at_index=0;
onezgroup.prototype.toLast=function(){
  var obj=$('.inputbox').get(0);
  obj.focus();
  if (window.getSelection) {
    var max_Len=obj.value.length;
    obj.setSelectionRange(max_Len, max_Len);
  }else if (document.selection) {
    var range = obj.createTextRange();
    range.collapse(false);
    range.select();
  }
};
onezgroup.prototype.at_select=function(uid,uname){
  $('.inputbox').val('[at='+uid+']@'+uname+'[/at] ');
  this.toLast();
  $('.admin-btns').hide();
};
onezgroup.prototype.show_at=function(pos,text){
  var that=this;
  that.at_index++;
  var box=that.box.find('.show-at-'+pos);
  if(box.length<1){
    box=$('<ul class="show-at show-at-'+pos+'"><li class="more">您有<em></em>条未读消息</li></ul>').appendTo(that.box.find('.showbox'));
    box.find('li.more').bind('click',function(){
      $(this).parents('.show-at').addClass('showmore');
    });
  }
  var obj=$('<li class="item"><span>'+text+'</span> <i class="glyphicon glyphicon-remove"" onclick="group.at_close(this)"></i></li>');
  if(pos=='top'){
    obj.appendTo(box);
    var n=box.find('li.item').length;
    if(n>5){
      if(!box.hasClass('showmore')){
        box.addClass('is-hasmore');
        box.find('li.item').removeClass('is-new');
        for(var i=n-5;i<n;i++){
          box.find('li.item').eq(i).addClass('is-new');
        }
        box.find('li.more em').html(n-5);
      }
    }else{
      box.removeClass('is-hasmore showmore');
      box.find('li.item').addClass('is-new');
    }
  }else if(pos=='bottom'){
    obj.appendTo(box);
    var n=box.find('li.item').length;
    if(n>5){
      if(!box.hasClass('showmore')){
        box.addClass('is-hasmore');
        box.find('li.item').removeClass('is-new');
        for(var i=n-5;i<n;i++){
          box.find('li.item').eq(i).addClass('is-new');
        }
        box.find('li.more em').html(n-5);
      }
    }else{
      box.removeClass('is-hasmore showmore');
      box.find('li.item').addClass('is-new');
    }
    box.css({bottom:$('.chat-footer').height()+'px'});
  }
  that.at_update();
};
onezgroup.prototype.at_close=function(obj,delay){
  var that=this;
  if(typeof delay!='undefined' && delay){
    $(obj).parents('li').delay(5000).fadeOut(null,null,function(){
      $(this).remove();
      that.at_update();
    });
    return;
  }
  $(obj).parents('li').remove();
  that.at_update();
};
onezgroup.prototype.at_update=function(){
  var that=this;
  var box=$('.show-at-top');
  var n=box.find('li.item').length;
  if(n<=5){
    box.removeClass('is-hasmore showmore');
    box.find('li.item').addClass('is-new');
  }
  var box=$('.show-at-bottom');
  var n=box.find('li.item').length;
  if(n<=5){
    box.removeClass('is-hasmore showmore');
    box.find('li.item').addClass('is-new');
  }
  $(window).trigger('resize');
};
</script>