var data = {} , inservice = false , distip = false , ontip = false;
/******
id:"",
****/
var dialogue_state=sessionStorage.getItem("dialogueState");
var mx = 0,my = 0; //鼠标x、y轴坐标(相对于left,top)
var dx = 0,dy = 0; //对话框坐标(同上)
var isDraging = false; //不可拖动
var hidedialogue=false;
var closedialogue=false;
var closeEnquiry=false; //关闭询问
dialogueState()
function dialogueState(){
if(dialogue_state){
if(dialogue_state==true||dialogue_state=='true'){
if(getEle('visitor')){
chater.openChatDialogWithURL(chater.chat);
}else{
setTimeout(function () {
dialogueState()
},1000)
}
}
}
}
var OnMessage = function (e) {
if(e.data.connect==true||e.data.connect==false){
closeEnquiry=e.data.connect
sessionStorage.setItem("dialogueState", e.data.connect);
}
if(e.data.estimate==true){
closeEnquiry=false
closeDialogue()
}
}
function init() {
if (window.addEventListener) {
window.addEventListener("message", OnMessage, [false,true]);
} else {
if (window.attachEvent) { // IE before version 9
window.attachEvent("onmessage", OnMessage);
}
}
};
init();
var setMessage;
function sendMessage(link,status){ //iframe接通
var iframe=getEle("visitorform")
dialogue_state=sessionStorage.getItem("dialogueState");
let key = dialogue_state;
try{
if(link && link.toString().indexOf('imgText') > -1){
key = link
}
}catch(e){}
if (iframe.attachEvent){
iframe.attachEvent("onload", function(){
iframe.contentWindow.postMessage({link:key,state:status},"*")
clearInterval(setMessage)
return
});
} else {
iframe.onload = function(){
iframe.contentWindow.postMessage({link:key,state:status},"*")
clearInterval(setMessage)
return
};
}
setMessage=setTimeout(function () {
sendMessage(link,status)
},200)
}
function openSession(url,type,callback){
var pageW = document.documentElement.clientWidth;
var pageH = document.documentElement.clientHeight;
if(pageW>500){
getEle('visitor').style.right='100px';
getEle('visitor').style.left='auto';
getEle('visitor').style.bottom='30px'
getEle('visitor').style.top='auto'
}else{
getEle('visitor').style.bottom='0'
getEle('visitor').style.width='100%'
getEle('visitor').style.height='90%'
getEle('visitor').style.right='0'
}
if(hidedialogue==true){
getEle('visitor').style.display='block'
}else if(closedialogue==true){
closedialogue=false;
hidedialogue=true;
getEle('inquiry').style.display='none'
getEle('visitor').style.display='block';
getEle("visitorform").src=url;
getEle("drag").style.cursor='move';
sendMessage(true,pageW);
}else{
drag()
getEle("visitorform").src=url
getEle("drag").style.cursor='move'
getEle('visitor').style.display='block'
closedialogue=false
hidedialogue=true
sendMessage(true,pageW)
}
callback()
}
function getEle(id){return document.getElementById(id)};
function drag(){
//鼠标按下
getEle('drag').addEventListener('mousedown',function(e){
var e = e || window.event;
mx = e.pageX; //点击时鼠标X坐标
my = e.pageY; //点击时鼠标Y坐标
dx = getEle('visitor').offsetLeft;
dy = getEle('visitor').offsetTop;
isDraging = true; //标记对话框可拖动
document.onmousemove = function(e){
if(isDraging!= true){ //判断对话框能否拖动
return
};
var e = e || event;
var x = e.pageX; //移动时鼠标X坐标
var y = e.pageY; //移动时鼠标Y坐标
var pageW = document.documentElement.clientWidth;
var pageH = document.documentElement.clientHeight;
var dialogW = getEle('visitor').offsetWidth;
var dialogH = getEle('visitor').offsetHeight;
var moveX = dx + x - mx; //移动后对话框新的left值
var moveY = dy + y - my; //移动后对话框新的top值
var maxX = pageW - dialogW; //X轴可拖动最大值
var maxY = pageH - dialogH; //Y轴可拖动最大值
moveX = Math.min(Math.max(0,moveX),maxX); //X轴可拖动范围
moveY = Math.min(Math.max(0,moveY),maxY); //Y轴可拖动范围
if(moveX<0){ //不能出左边
moveX=0;
}
if(moveY<0){
moveY=0;
}
getEle('visitor').style.left = moveX +'px'; //重新设置对话框的left
getEle('visitor').style.top = moveY +'px'; //重新设置对话框的top
};
});
//鼠标离开
getEle('drag').addEventListener('mouseup',function(){
isDraging = false;
});
getEle('visitorform').addEventListener('mouseout',function(){
isDraging = false;
});
}
function getparent(id){
return window.parent.document.getElementById(id)
}
function hideDialogue(){
closedialogue=false
hidedialogue=true
localStorage.setItem("closedialogue", false);
localStorage.setItem("hidedialogue", true);
getEle('visitor').style.display='none';
sessionStorage.setItem("dialogueState", 'hide');
}
function closeDialogue(){
if(closeEnquiry==true||closeEnquiry=='true'){
getEle('inquiry').style.display='flex'
}else{
hidedialogue=false
closedialogue=true
getEle('visitor').style.display='none'
getEle("visitorform").src=''
getEle("drag").style.cursor='context-menu'
localStorage.setItem("hidedialogue", false);
localStorage.setItem("closedialogue", true);
sessionStorage.setItem("dialogueState", false);
}
}
function enquiry(text){
if(text==false){
getEle('inquiry').style.display='none'
}else{
getEle('inquiry').style.display='none'
getEle("visitorform").contentWindow.postMessage({link:'',state:'',estimate:true},"*")
hidedialogue=false
closedialogue=true
getEle("drag").style.cursor='context-menu'
sessionStorage.setItem("dialogueState", false);
localStorage.setItem("hidedialogue", false);
localStorage.setItem("closedialogue", true);
}
}