function rangeIsOk(sContainer, eContainer){
}
$('html, body').on('mousedown', function(e){
if(!$(e.target).is('.tip-entity-list *')){
userSelection && userSelection.removeAllRanges();
selTip.hide();
}else if($(e.target).is('*[unselectable=on]')){
e.preventDefault();
return false;
}
});
$('body').on('mouseup', function(e){
var me = $(this);
if(window.getSelection()){
userSelection = window.getSelection();
selText = userSelection.toString();
}
if(!selText.length) return false;
rangeObject = userSelection;
if (userSelection && userSelection.getRangeAt) {
rangeObject = userSelection.getRangeAt(0);
}
if(!rangeObject.collapsed){
var sContainer = rangeObject.startContainer.parentNode;
var eContainer = rangeObject.endContainer.parentNode;
if(rangeIsOk(sContainer, eContainer)){
var rangeRects = rangeObject.getClientRects();
var maxTop = 0, num = 0, pHeight = 20;
for(var i = 0, len = rangeRects.length; i < len; i++){
if(rangeRects[i].height <= pHeight && rangeRects[i].width && maxTop < rangeRects[i].top){
maxTop = rangeRects[i].top;
num = i;
}
}
var lastLeft = Math.floor(rangeRects[num].left + rangeRects[num].width);
var lastTop = Math.ceil(rangeRects[num].top + rangeRects[num].height);
tipX = lastLeft;
tipY = lastTop + $(window).scrollTop();
var tipEntityLen = $('.tip-entity-item').length;
if(tipEntityLen){
if($.trim(selText)){
tipShow(selText, tipX, tipY);
}
}else{
showTipPanel($("#messPanel"), "没有待选项");
}
}
}
});
function tipShow(text, tipX, tipY){
selTip.css('left',tipX).css('top',tipY).show();
}