/** * option:handleClick,handledbclick,dragend,itemClick,dragListClick,onSelectedClick,onItemSelect,ondblclick */ var sc = document.createElement("script"); sc.src = '/ss/js/wdDrag.js'; // = '/wd/js/wdDrag.js'。Lin document.head.appendChild(sc); $(function () { if (wd.configurableTab) { console.warn("这一段引入自动引入wdDrag只是应急处理,说不定哪一天就干掉了,看到这段信息,就表示你要引用新的wdDrag(tab需要重新发布)(重要的话要说三遍)"); console.warn("这一段引入自动引入wdDrag只是应急处理,说不定哪一天就干掉了,看到这段信息,就表示你要引用新的wdDrag(tab需要重新发布)(重要的话要说三遍)"); console.warn("这一段引入自动引入wdDrag只是应急处理,说不定哪一天就干掉了,看到这段信息,就表示你要引用新的wdDrag(tab需要重新发布)(重要的话要说三遍)"); } }) // console.log("旧的drag"); // throw "这个脚本已经废除,现在帮你引入新的脚本"; var drager = dragStartPoint = dragNowPoint = options = null; var justDragEnd = itemDraging = dragItemDown = dragListDown = dashedDrawing = dashedDrawEnd = draginited = false; var clickedField, startScrollPoint, dashedStartPoint, dashedEndPoint, doc, wdw, currentMouseDownItem; var prepareDrag = []; // http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js /* * var dragcss = ""; */ var dragcss = ""; // ='/wd/js/drag.css'。Lin var yx = '.dragList', bx = '.content-right'; $(document).ready(function () { for (var name in prepareDrag) { var drager = DragManager.getInstance(name); } }); // function initTagDrag(opt, _window) { // options = { // dashedField: " ", // alignMode: 'vertical' // }; // for (var k in opt) { // options[k.toString()] = opt[k]; // } // initDrag(options, _window); // } function prepareDrag(name, opt, _window) { prepareDrag[name] = {}; prepareDrag[name].options = opt || {}; }; function initDrag(name, opt, _window) { // if (typeof (isWriteOut) == "undefined") { // alert("注意下,这是旧的写法,以后不会再有自动初始化拖拽了,\n记得新的拖拽需要通过commonwrite输出^_^"); // return; // } options = opt || {}; options.dragListSelector = options.dragListSelector || ".dragList" options.dashedField = options.dashedField || ".dragList" options.alignMode = options.alignMode || 'vertical'; console.log(options); drager = {}; distnic(); _window = _window || window; wdw = $(_window); doc = $(_window.document); // //初始化iframe的drag // if ($("iframe", _window.document).length > 0) { // $.each($("iframe", _window.document), function(index) { // this.onload = function() { // initDrag(options, this.contentWindow) // } // }); // } var dragList_ = $(".dragList", doc); var item_ = $(".dragList>.item", doc); /* * 初始化item.dragList的三角 */ $(".dragList.item", doc).each(function () { if (!$(this).children(".triangle").length > 0) { $(this).prepend(setUpTriangle(this)); } }); var handle_ = $(".dragList>.item>.handle", doc); doc.off("selectstart").on('selectstart', function () { return false; }); doc.off("keydown").on("keydown", function (event) { functionKeyDown = event.ctrlKey || event.altKey || event.shiftKey; if (event.ctrlKey && event.keyCode === 65) { // 全选 if ($(".dragList.focused").length < 1 && $(".L").length > 0) {} $(".dragList.focused .item").addClass("selected"); doc.trigger("mouseup"); } }); doc.off("keyup").on("keyup", function (event) {}); doc.off("mousedown").on("mousedown", function (event) {}); doc.off("mouseup").on("mouseup", function (event) { currentMouseDownItem = null; _dragEnd(event, options.dragend); }); doc.off("click").on("click", function (event) {}); doc.mousemove(function (event) { // 模拟mouseentermouseout $("body").find(".mousedata").html( "left:" + event.clientX + ",top:" + event.clientY); if (!drager.list && dragItemDown) { // 将item拖拽出来 itemToList(event); } if (drager.list && dragItemDown) { // 移动List moveList(event); } }); // dragList_.off("scroll").on("scroll", function() { // doc.trigger("mousemove"); // }); $(".dragList,.item").off("mousedown mouseup click dblclick"); dragList_.off("scroll").on("scroll", function () { doc.mousemove(); }); dragList_.on("click", function (event) { // 列表的点击事件 if (options.dragListClick) { options.dragListClick(this, event); } if ($(this).is(".item")) { event.stopPropagation(); doc.click(); } }); dragList_.on("mousedown", function (event) { // 列表的点击事件 focusList(this); if ($(this).is(".item")) { event.stopPropagation(); doc.mousedown(); } }); dragList_.on("mouseup", function (event) { // 列表的点击事件 if ($(this).is(".item")) { event.stopPropagation(); doc.mouseup(); } }); dragList_.off("selectAll").on("selectAll", function () {}); item_.on("dblclick", function () {}); item_.on("click", function (event) { if (options.itemClick) { console.log("itemclick_id:" + this.id); options.itemClick(this, event); } }); item_.on("mousedown", function (event) { currentMouseDownItem = this; $(".L", doc).removeClass("L"); $(this).addClass("ready").addClass("L"); if (event.shiftKey) { if ($(".F").length > 0) { if ($(this).isChildOf($(".F").eq(0).parent())) { shiftSelect(); } else { $(this).removeClass("L"); return false; } } } else if (event.ctrlKey) { // 按下ctrl多选 } else {} { if ($(this).is(".selected")) { // 第二次或以上选中 if (options.onSelectedClick) { options.onSelectedClick(this); } } else { // 第一次选中 if (options.onItemSelect) { options.onItemSelect(this); } } } if ($(this).is(".dragList.expand")) { focusList(this); } else { focusList($(this).parents(".dragList").eq(0)); } dragItemDown = true; if (drager.list != null) { return false; } dragStartPoint = { x: event.clientX, y: event.clientY }; event.stopPropagation(); return false; }); item_.on("mouseup", function (event) { if (event.shiftKey) { if ($(".F").length > 0) { if ($(this).isChildOf($(".F").eq(0).parent())) {} else { return false; } } } dragItemDown = false; $(".F").removeClass("F"); $(this).removeClass("L").addClass("F"); if (dashedDrawing) { return; } // 鼠标弹起,主要是标记跟取消标记 if ($(".focused .selected").length < 2 && !event.shiftKey && !event.ctrlKey) { $(".selected", doc).not(".L").removeClass("selected"); } if (event.shiftKey || event.ctrlKey) { if (!event.shiftKey) { $(".L", doc).toggleClass("selected dselected"); } } else { $(".dragList>.item", doc).not(".ready").removeClass("selected"); } $(".ready", doc).addClass("selected").removeClass("ready"); event.stopPropagation(); return false; }); item_.on("click", function () {}); item_.off("itemOut").on("itemOut", function (event) { if ($(this).is(".dragList")) { if (this.intervalID) clearInterval(this.intervalID); } }); item_.off("itemEnter").on( "itemEnter", function (event) { if ($(this).is(".dragList")) { var this_ = $(this); var id = setInterval(function () { if (dragItemDown && !this_.is(".dragList .dragList .dragList")) { this_.trigger("showT"); } clearInterval(id); this_[0].intervalID == null; }, 1000); this.intervalID = id; } }); handle_.off("click").on("click", function (event) { if (options.handleClick && !justDragEnd) { try { console.log("handle_click_id:" + this.id); options.handleClick(event, this); } catch (e) { console.error(e.message); } } if (justDragEnd) { justDragEnd = false; } }); handle_.off("dblclick").on("dblclick", function (event) { if (options.handledbclick) { options.handledbclick(event, this); } }); handle_.off("mouseup").on("mouseup", function (event) { if (options.handleMouseUp) { try { options.handleMouseUp(event, this); } catch (e) { console.error(e.message); } } }); handle_.hover(function (event) { $(this).css("cursor", 'move'); if (event.shiftKey) { if ($(".F").length > 0) { if ($(this).parents(".item").eq(0).isChildOf( $(".F").eq(0).parent())) {} else { $(this).css("cursor", 'not-allowed'); } } } }, function (event) { $(this).css("cursor", 'move'); }); handle_.off("mousedown").on("mousedown", function (event) { if (options.handleMouseDown) { try { options.handleMouseDown(event, this); } catch (e) { console.error(e.message) } } }); $("form", doc).on("keydown", function (event) { if (event.keyCode === 13) { // 回车提交表单 return false; } }); window.options = options; // 其他初始化 if ($("#dragcss").length < 1) { $("head", doc).append(dragcss); } { // 设置拖选虚线框 if (options.dashedField && $(options.dashedField).length > 0) { // setDashedField(options.dashedField); } } // { //设置handle前面的checkbox // if (options.selectBox) { // $(".dCheck").remove(); // $.each($(".dragList .item"), function (index) { // var firstHandle = $(this).children(".handle").eq(0); // var itemid = firstHandle.parent().attr("id"); // var dcheck = $(""); // firstHandle.prepend(dcheck); // }); // } // //绑定checkbox的事件 // $(".dCheck").off("mousedown click mouseup").on("mousedown click mouseup", // function (event) { // var handle = $(this).parents(".handle").eq(0); // var item =$(this).parents(".item").eq(0);// findClosestObj(this, // ".item"); // //focusList(handle); // if (this.checked) { // item.addClass("selected dselected"); // } else { // item.removeClass("selected dselected"); // } // doc.mouseup(); // event.stopPropagation(); // }); // } clearSlct(); draginited = true; }; /** * * shift选择 */ function shiftSelect() { var f = $(".F", doc); if (f.length <= 0 || $(".L", doc).length <= 0) { return; } $(".selected", doc).removeClass("selected"); $(".F", doc).nextAll(".item").addClass("selected"); $(".L", doc).nextAll(".item").toggleClass("selected"); $(".F", doc).addClass("selected"); $(".L", doc).addClass("selected"); }; /** * 根据ID查找targetList里面跟item重复的内容 * */ function distnic() { $.each($(".dragList", doc), function (index) { result = true; var par = $(this); $.each($(this).find(".item"), function (index) { var id = $(this).attr("id"); if (id && par.find(" [id=" + id + "]").length > 1) { result = false; if (id && par.find(" [id=" + id + "]").length > 1) { par.find(" [id=" + id + "]").not(":first").remove(); } } }); }) }; /** * 查看鼠标是否在目标上方 * * @param {目标} * target * @param {MouseEvent} * event */ jQuery.fn.isMouseOver = function (event) { var target = this; if ($(target).length < 1 || $(target).is(".ghost")) return; // console.log("判断鼠标是不是在目标上方"); var xy = $(target).getSize(); // console.log("---------------------------"); // console.log(getSize($(".item.dragList"))); // console.log($(".item.dragList")); // console.log("==========================="); var result = 1; // var tx = ""; try { if ((event.clientX >= xy.left) && (event.clientX <= (xy.left + xy.width + xy.margin)) && (event.clientY >= xy.top) && (event.clientY <= (xy.top + xy.height + xy.margin))) { if (event.clientX >= (xy.left - xy.margin) && event.clientX <= (xy.left + (xy.width + xy.margin) / 2)) { result *= 1; // 左边 } else if (event.clientX >= (xy.left + (xy.margin + xy.width) / 2) && event.clientX <= (xy.left + xy.width + xy.margin)) { result *= 2; // 右边 } else { result *= 0; // 不在上面 } if (event.clientY >= (xy.top - xy.margin) && event.clientY <= (xy.top + xy.height / 3)) { result *= 3; // 上半部分 } else if (event.clientY > (xy.top + xy.height / 3) && event.clientY <= (xy.top + xy.height + xy.margin)) { result *= 4; // 下半部分 } else { result *= 0; // 不在上面 } } else { result = 0 } } catch (e) { console.log(e) } // console.group(); // console.log(xy); // console.log(event.clientX+":"+(event.clientX>=xy.left && // event.clientX<=(xy.left + xy.width + xy.margin)) + "-" + event.clientY + ":" + (event.clientY >= xy.top && event.clientY <= (xy.top + xy.height + xy.margin))); // console.log((xy.left)+":"+(xy.left + xy.width + // xy.margin)+"-"+xy.top+":"+(xy.top+xy.height+xy.margin)); // console.groupEnd(); // 左上1*3=3,右上2*3=6,左下1*4=4,右下2*4=8,不在0*0 return result; }; /** * 产生一个空白div */ function generateGhost() { var firstItemSize = $(".selected", doc).eq(0).getSize(); var ghost = $(".selected", doc).eq(0).clone(); ghost.attr('id', 'ghost'); if (ghost.is("tr")) { ghost .html(' '); } else { ghost.html(''); } ghost.dashMode = function () { ghost.css({ "width": firstItemSize.width, "height": firstItemSize.height, "border": "1px dashed red", "border-radius": "5px", "margin": firstItemSize.margin, "background": "white" }); } ghost.lineMode = function () { if (options.alignMode == 'vertical') { ghost.css({ "width": "auto", "height": "2px", "border-width": "2px", "border-style": "solid", "border-color": "transparent red", "margin": 0, "background": "padding-box red" }); } else { ghost.css({ "width": "2px", "height": "auto", "border-width": "2px", "border-style": "solid", "border-color": "red transparent", "margin": 0, "background": "padding-box red" }); } } ghost.removeClass("selected").removeClass("dragList"); return ghost; }; function setDashedField(item) { $(item) /* .off("mousedown") */ .on("mousedown", function (event) { $(this).data("position", $(this).css("position")); $(this).css({ "position": "relative" }); dashedStartPoint = getMouseCoordinateOf(this, event); startScrollPoint = { top: $(this)[0].scrollTop + findObjScrollParent(this)[0].scrollTop, left: $(this)[0].scrollLeft + findObjScrollParent(this)[0].scrollLeft }; dashedEndPoint = dashedStartPoint = { x: dashedStartPoint.x + startScrollPoint.left, y: dashedStartPoint.y + startScrollPoint.top } if ($(".dasheddiv").length < 1) { $(this).append("
") } var fieldSize = $(this).getSize(); clickedField = $(this); dragListDown = true; }); $(doc).on("mousemove", function () { if (dragListDown) { drawDashed(event); } }); $(item) /* .off("mouseup") */ .on("mouseup", function (event) { $(this).css("position", $(this).data("position")); $(".dasheddiv").remove(); dragListDown = false; startScrollPoint = dashedEndPoint = dashedStartPoint = {}; }); }; function updateDashed(s, e) { e = e || s; if (!s.x || !s.y) return; var div = $(".dasheddiv"); if (s.x > e.x) { // 交换x坐标 s.x = [e.x, e.x = s.x][0]; // s.x ^= e.x; // e.x ^= s.x; // s.x ^= e.x; } if (s.y > e.y) { // 交换y坐标 s.y = [e.y, e.y = s.y][0]; // s.y ^= e.y; // e.y ^= s.y; // s.y ^= e.y; } div.css({ display: "block", left: s.x, top: s.y, width: Math.abs(e.x - s.x - 5), height: Math.abs(e.y - s.y - 5) }); } function itemToList(event) { dragNowPoint = { x: event.clientX, y: event.clientY }; var xdiff = dragStartPoint.x - dragNowPoint.x; // 计算两个点的横坐标之差 var ydiff = dragStartPoint.y - dragNowPoint.y; // 计算两个点的纵坐标之差 if (Math.pow((xdiff * xdiff + ydiff * ydiff), 0.5) < 10) { return false; } // console.log("将ITEM复制到LIST") drager.xy = $(".item.L", doc).eq(0).getSize(); drager.left = event.clientX - drager.xy.left; drager.top = event.clientY - drager.xy.top; if ($(".selected").length < 2) { $(".selected").removeClass("selected"); } if ($(currentMouseDownItem).is(".dragList")) { focusList($(currentMouseDownItem).parents(".dragList").eq(0)); } $(".ready", doc).removeClass("ready").addClass("selected"); // 取消父节点下子节点的选中状态,父节点移动就不会把子节点放出来 $(".selected.item.dragList").find(".item").removeClass("selected"); var items = $(".focused.dragList .selected,.focused.dragList .dselected", doc); if (items.length < 1) { drager = {}; return; } $(".dragList:not(.focused)").children(".item").removeClass("selected"); if (!drager.ghost) { drager.ghost = generateGhost(); } // createshadows $.each(items, function () { var clone_ = $(this).clone(); clone_.css("opacity", "0.5"); // clone_.attr("class", "item shadow"); clone_.addClass("shadow"); clone_.removeClass("expand selected"); // clone_.attr("id", ""); $(this).after(clone_) this.shadow = clone_; if ($(this).hasClass("dragList")) { $(this).addClass("dragList_"); $(this).removeClass("dragList"); } }); drager.items = items; drager.startpoint = $("