 /**
  * v-drag 容器拖拽指令
  * {
  *     dom, 允许拖拽d的容器类名或ID名 不传则默认绑定v-drag的容器
  *     drapStop, 拖动停止的回调方法
  *     enable,是否启用拖拽 (tevs内部自己调用，不开放给用户)
  * }
  * 
  */

 export default {
     mounted(el, binding, vnode) {
         const {
             value
         } = binding

         if (!value.enable) return;

         let popupTitle = null;

         if (!value.dom) {
             popupTitle = el;
         } else {
             popupTitle = el.querySelector(value.dom);
         }

         if (popupTitle === null) return;

         // 添加鼠标按下事件
         popupTitle.onmousedown = function (e) {
             let disx = e.clientX - el.offsetLeft;
             let disy = e.clientY - el.offsetTop;
             let maxleft = document.body.clientWidth - el.clientWidth;
             let maxTop = document.body.clientHeight - el.clientHeight;
             let flag = false;
             document.onmousemove = function (e) {
                 let left = e.clientX - disx;
                 let top = e.clientY - disy;
                 left < 0 && (left = 0);
                 left > maxleft && (left = maxleft);
                 top < 0 && (top = 0);
                 top > maxTop && (top = maxTop);
                 el.style.left = left + "px";
                 el.style.top = top + "px";
                 flag = true;
             };
             document.onmouseup = function (e) {
                 document.onmousemove = document.onmouseup = null;
                 if (flag) {
                     value.drapStop && value.drapStop(e)
                 }
             };
         };

     }
 }