| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146 |
- /**
- * 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 = "<link id='dragcss' rel='stylesheet'
- * href='/wd/js/drag/drag.css' /><link id='dragcss' rel='stylesheet'
- * href='drag.css' />";
- */
- var dragcss = "<link id='dragcss' rel='stylesheet' href='/ss/js/drag.css'/>"; // ='/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 = $("<input type='" + options.checkbox.type + "' name='" +
- // options.checkbox.name.replace("{#index}", index).replace("{#itemid}",
- // "item" + itemid) + "' class='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('<td colspan="' + ghost.find("td").length +
- '"> </td>');
- } 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("<div class = 'dasheddiv' nowrap > <!--拖选正在测试中……<br/>有问题就反馈下<br/>目前遇到的问题是:滚动条--></div>")
- }
- 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 = $("<div style='display:none' id='startpoint'/>");
- items.eq(0).before(drager.ghost).before(drager.startpoint);
- drager.list = items.eq(0).parents(".dragList").eq(0).clone().html("")
- .removeClass("dragList");
- drager.list.append($("style", doc).clone());
- drager.list.css({
- "z-index": "99999",
- "background": "white ",
- "border": "1px dashed black ",
- "border-radius": ".3em ",
- 'position': "fixed",
- "padding": ".3em",
- "height": "auto"
- });
- drager.list.addClass("itemList");
- drager.list.width(drager.ghost.width())
- drager.list
- .append("<div style = 'clear:both;position:absolute;top:0;left:0;right:0;bottom:0;z-index:100000' class = 'panel' / > "); // 遮罩层
- drager.list.on("mousewheel DOMMouseScroll", function (e) { // 拖拽的时候滚动div
- var delta = (e.originalEvent.wheelDelta && (e.originalEvent.wheelDelta > 0 ? 1 : -1)) || // chrome & ie
- (e.originalEvent.detail && (e.originalEvent.detail > 0 ? -1 : 1)); // firefox
- findObjScrollParent(drager.ghost)[0].scrollTop -= delta * parseInt(drager.items.css("height").replace("px", ""));
- });
- if (items.eq(0).is('tr')) {
- var tb = $("<table class='list'></table>");
- tb.append(items);
- drager.list.append(tb);
- } else {
- drager.list.append(items);
- }
- $("body", doc).append(drager.list);
- };
- /**
- *
- * @param {MouseEvent}
- * event
- */
- function moveList(event) {
- justDragEnd = false;
- itemDraging = true;
- drager.list.css('left', (event.clientX - drager.left) + "px");
- drager.list.css('top', (event.clientY - drager.top) + "px");
- clearSlct();
- setGhost(event);
- };
- function setGhost(event, element) {
- var items = new Array();
- if (element) {
- if ($(element).is(".dragList")) {
- items = [element];
- } else {
- items = $(element).find(".dragList:not('.item')");
- }
- } else {
- items = $(".dragList:not('.item')", doc);
- }
- $.each(items, function () {
- var result = true;
- var theList = ($(this).children('tbody').length > 0) ? $(
- this).children('tbody') :
- $(this);
- if (theList.isMouseOver(event) > 0) {
- drager.list.children(".panel")
- .css("cursor", "move");
- if (theList.children('#ghost').length < 1) {
- theList.append(drager.ghost);
- }
- if (theList.is(yx)) {
- drager.ghost.slideDown();
- } else {
- drager.ghost.slideUp();
- }
- $.each(theList.children('.item'),
- function () {
- // drager.ghost.lineMode();
- var flag = true;
- var item = $(this);
- if ($(this).isMouseOver(event) > 0) { // 这里触发item的鼠标进出事件, 以后可能会调整, 同时这里的逻辑有点问题但是能用, 以后处理
- if (!this.isEnter) { // 从未进来过,这次进来了
- $(this).trigger(
- "itemEnter");
- this.isEnter = true; // 进来了
- }
- } else {
- if (this.isEnter) { // 曾经是进来过的
- $(this).trigger("itemOut");
- this.isEnter = false; // 出去了
- }
- }
- if (window.options &&
- window.options.alignMode != 'horizontal' || window.options && window.options.alignMode != 'hor') { // 如果不是水平分布,就上下拖动排序
- switch ($(this).isMouseOver(event)) {
- case 3: // 左上
- case 6: // 右上
- if (!item.prev().is(
- "#ghost")) {
- item
- .before(drager.ghost);
- }
- console.log("上半部分")
- flag = false;
- break;
- case 4:
- case 8:
- if (!item.next().is(
- "#ghost")) {
- item
- .after(drager.ghost);
- }
- console.log("下半部分")
- flag = false;
- break;
- }
- } else { // 不然就左右分布
- switch ($(this).isMouseOver(event)) {
- case 3: // 左上
- case 4: // 左下
- if (!item.prev().is("#ghost")) {
- item.before(drager.ghost);
- }
- console.log("左半部分")
- flag = false;
- break;
- case 6: // 右上
- case 8: // 右下
- if (!item.next().is("#ghost")) {
- item.after(drager.ghost);
- }
- console.log("半部分")
- flag = false;
- break;
- }
- result = false;
- }
- if (item.is(".dragList") && item.is(".expand")) {
- if (drager.list.children(".item").find(".item").length < 1) {
- setGhost(event, this);
- drager.list.children(".panel").css("cursor", "move");
- drager.list.children(".panel").attr("title", "");
- drager.ghost.dashMode();
- } else {
- drager.list.children(".panel").css("cursor", "not-allowed");
- drager.list.children(".panel").attr("title", "选中项存在子元素,不得放入该列表");
- drager.ghost.lineMode();
- }
- } else {
- drager.list.children(".panel").css("cursor", "move");
- drager.list.children(".panel").attr("title", "");
- drager.ghost.lineMode();
- }
- return flag
- });
- } else {}
- return result; // return true = continue;return false = break;
- });
- focusList(drager.ghost);
- }
- function drawDashed(event) {
- // 画虚线框
- dashedDrawing = true;
- if ($(".focused").length < 1) {
- return;
- }
- var nowScrollPoint = {
- top: $(".focused")[0].scrollTop +
- findObjScrollParent(".focused")[0].scrollTop,
- left: $(".focused")[0].scrollLeft +
- findObjScrollParent(".focused")[0].scrollLeft
- };
- dashedEndPoint = getMouseCoordinateOf('.focused', event);
- dashedEndPoint.x += nowScrollPoint.left;
- dashedEndPoint.y += nowScrollPoint.top;
- var xdiff = dashedStartPoint.x - dashedEndPoint.x; // 计算两个点的横坐标之差
- var ydiff = dashedStartPoint.y - dashedEndPoint.y; // 计算两个点的纵坐标之差
- if (Math.pow((xdiff * xdiff + ydiff * ydiff), 0.5) < 8) {
- return false;
- }
- // 重新定义起点终点坐标
- var fieldSize = $(".focused").getSize();
- start = {
- x: dashedStartPoint.x,
- y: dashedStartPoint.y
- };
- var end = {
- x: dashedEndPoint.x,
- y: dashedEndPoint.y
- };
- var scrollwidth = $(".focused")[0].scrollWidth;
- var scrollheight = $(".focused")[0].scrollHeight;
- // 让虚线框保持在draglist里面
- start.x = start.x < 0 ? 0 : ((start.x > scrollwidth) ? scrollwidth :
- start.x);
- start.y = start.y < 0 ? 0 : ((start.y > scrollheight) ? scrollheight :
- start.y);
- end.x = end.x < 0 ? 0 : ((end.x > scrollwidth) ? scrollwidth : end.x);
- end.y = end.y < 0 ? 0 : ((end.y > scrollheight) ? scrollheight : end.y);
- updateDashed(start, end);
- var ds = $(".dasheddiv").getSize();
- $(".selected", doc).removeClass("selected");
- $.each($(".focused").find(".item"), function (index) {
- var is = $(this).getSize();
- // 判断是否重叠
- if ((is.top >= ds.top && (is.top) <= (ds.top + ds.height)) ||
- (is.top + is.height) >= ds.top &&
- (is.top + is.height) <= (ds.top + ds.height)) { // item的y在拖选匡的y里面
- if ((is.left >= ds.left && is.left <= (ds.left + ds.width)) ||
- ((is.left + is.width) >= ds.left && is.left < ds.left))
- $(this).addClass("selected");
- } else if (is.top <= ds.top &&
- (is.top + is.height) >= (ds.top) ||
- is.top <= (ds.top + ds.height) &&
- (is.top + is.height) >= (ds.top + ds.height)) {
- if ((is.left >= ds.left && is.left <= (ds.left + ds.width)) ||
- ((is.left + is.width) >= ds.left && (is.left + is.width) <= (ds.left +
- ds.width))) {
- $(this).addClass("selected");
- }
- }
- });
- clearSlct();
- };
- /**
- *
- * @param {MouseEvent}
- * event
- * @param {拖拽结束的回调}
- * callback
- */
- function _dragEnd(event, callback) {
- if (drager && drager.list) { // 清除拖拽列
- $(".F,.L,.N").removeClass("F L N");
- drager.ghost.parent().find(".shadow").each(function () {
- var shadow = this;
- drager.items.each(function () {
- if (shadow == this.shadow[0]) {
- $(shadow).slideUp();
- }
- });
- });
- drager.ghost.before(drager.items);
- $('.dragList', doc).css('background', "");
- $('.dragList tbody', doc).css('background', "");
- $.each(drager.items, function () {
- if ($(this).hasClass("dragList_")) {
- $(this).addClass("dragList");
- $(this).removeClass("dragList_");
- }
- });
- var result = true;
- if (itemDraging) { // 回调
- justDragEnd = true;
- itemDraging = false;
- if (callback) {
- try {
- result = callback(event);
- } catch (e) {
- console.err(e.message);
- }
- }
- }
- if (result == false || !drager.ghost.is(":visible")) { // 取消移动
- $.each(drager.items, function () {
- $(this.shadow).before(this);
- this.shadow == null;
- });
- }
- drager.list.remove();
- drager.ghost.remove();
- drager.startpoint.remove();
- drager = {};
- distnic();
- $(".shadow").slideUp('fast', function () {
- $(".shadow").remove();
- });
- }
- if (dashedDrawing) {
- dashedDrawing = false;
- dashedDrawEnd = true;
- }
- $(".dasheddiv").remove();
- handleDown = false;
- itemDraging = false;
- dragItemDown = dragListDown = false;
- dashedEndPoint = dashedStartPoint = null;
- }
- /**
- * 获取最靠近的带滚动条的父级
- *
- * @param {Object}
- * obj
- * @param {Object}
- * i
- */
- function findObjScrollParent(obj, i) {
- i = i || 0;
- i++;
- // console.log($(obj)[0].tagName + ":" + ($(obj)[0].tagName != "HTML"))
- var par = $(obj).parent();
- if ($(obj)[0].tagName == "HTML") {
- return $(obj).find("body");
- }
- if (isCanScroll(par)) {
- return $(par);
- } else {
- return findObjScrollParent(par, i);
- }
- }
- /**
- * 判断是否带滚动条
- *
- * @param {Object}
- * obj
- */
- function isCanScroll(obj) {
- if (obj) {
- // console.log(obj)
- var of = $(obj).css("overflow-y");
- return ("scroll|auto".indexOf( of ) > -1) && (obj[0].scrollHeight > 0);
- } else {
- console.groupEnd()
- return false
- }
- }
- /**
- * 查找最近的.dragList
- *
- * @param {Object}
- * obj
- */
- function findClosestDragList(obj) {
- var o = $(obj);
- if (o.is("td")) {
- // -td tr tbody table
- return o.parent().parent().parent();
- } else if (o.is("tr")) {
- return o.parent().parent();
- } else if (o.is("body")) {
- return o.parent();
- } else if (o.is(".dragList")) {
- return o;
- // } else if (o.is(".item") || o.is("#ghost")) {
- // return o.parent();
- // } else if (o.is(".handle")) {
- // return o.parent().parent();
- } else {
- return o.parents(".dragList").eq(0);
- }
- }
- /**
- * 查找最近的父级的selector 元素
- *
- * @param {Object}
- * obj
- * @param {Object}
- * selector
- */
- function findClosestObj(obj, selector) {
- // try {
- // console.log(obj);
- // var par = $(obj).parent();
- // if (par.is(selector)) {
- // return par;
- // } else {
- // return findClosestObj(par, selector);
- // }
- // } catch (e) {
- // console.log(e.message)
- // return {
- // length: 0
- // }
- // }
- return $(obj).parents(selector).eq(0);
- }
- /**
- * 获取鼠标在selector的相对位置
- *
- * @param {Object}
- * selector
- * @param {Object}
- * event
- */
- function getMouseCoordinateOf(selector, event) {
- var top, left, oDiv;
- if ($(selector).length < 1)
- return;
- oDiv = $(selector)[0];
- top = getY(oDiv);
- left = getX(oDiv);
- function getX(obj) {
- var parObj = obj;
- var left = obj.offsetLeft;
- while (parObj = parObj.offsetParent) {
- left += parObj.offsetLeft;
- }
- return left;
- }
- function getY(obj) {
- var parObj = obj;
- var top = obj.offsetTop;
- while (parObj = parObj.offsetParent) {
- top += parObj.offsetTop;
- }
- return top;
- }
- var result = {
- x: (event.clientX - left + document.body.scrollLeft) - 2,
- y: (event.clientY - top + document.body.scrollTop) - 2
- }
- return result;
- }
- /**
- * 设置list的焦点样式
- *
- * @param {Object}
- * obj
- */
- function focusList(obj) {
- $(".focused").removeClass("focused");
- findClosestDragList(obj).addClass("focused");
- };
- /**
- * 注销掉拖拽
- */
- function destroyDrag() {
- _dragEnd(event);
- $.each($(".dragList", doc), function () {
- $(this).before($(this).clone())
- $(this).remove();
- })
- doc.off("selectstart keydown keyup mousedown mouseup mousemove click");
- $("#dragcss").remove();
- $(".dCheck").remove();
- $("#dragcss").remove();
- }
- /**
- * 清除选中的文字
- */
- var clearSlct = "getSelection" in window ? function () {
- window.getSelection().removeAllRanges();
- } : function () {
- document.selection.empty();
- };
- function setUpTriangle(dragList) {
- var span = $("<span/>");
- var list = $(dragList);
- span.isShow = false;
- span.addClass("triangle");
- var defaulthigh = list.height();
- list.on("hideT", function () {
- if (!span.isShow)
- return;
- span.isShow = false;
- $(this).css("overflow", "hidden");
- $(this).animate({
- height: defaulthigh
- }, "fast");
- $(this).removeClass("expand");
- focusList($(this).parents(".dragList").eq(0));
- });
- list.on("showT", function () {
- if (span.isShow)
- return;
- span.isShow = true;
- $(this).addClass("expand");
- if ($(this).children(".item").length == 0) {
- $(this).animate({
- height: $(this).height() * 2,
- "overflow": "visible"
- }, "fast");
- } else {
- $(this).css({
- "overflow": "visible"
- });
- $(this).autoHeightAnimate("fast", function () {
- $(this).height("auto");
- });
- }
- focusList(this);
- });
- span.click(function (event) {
- if (span.isShow) {
- list.trigger("hideT");
- } else {
- list.trigger("showT");
- }
- event.stopPropagation();
- return false;
- });
- list.prepend(span);
- list.css("overflow", "hidden");
- list.css("height", list.children(".item").eq(0).height());
- var n = span.next();
- n.css("display", "inline-block");
- n.css("padding-left", 15);
- span.animate({
- "top": (n.height() - 10) / 2 + "px"
- }, "fast");
- dragList.triangle = span[0];
- }
- jQuery.fn.autoHeightAnimate = function (time, callback) {
- var element = this;
- this.curHeight = element.height(); // Get Default Height
- element.height('auto'); // Get Auto Height
- this.autoHeight = element.height()
- element.height(this.curHeight); // Reset to Default Height
- if (this.autoHeight)
- element.stop().animate({
- height: this.autoHeight
- }, time, null, callback); // Animate to Auto Height
- }
- jQuery.fn.isChildOf = function (b) {
- return this.parent()[0] == $(b)[0];
- };
- jQuery.fn.getSize = function () {
- var obj = $(this);
- var xy = {};
- if (!isDom(obj)) {
- console.group("------------------------");
- console.log(obj);
- console.error(obj + "不是DOM对象");
- console.groupEnd("------------------------");
- return xy;
- }
- var offset = obj.offset();
- if (offset) {
- xy.top = offset.top - $("body", doc)[0].scrollTop; // 距离浏览器上边距的高 top
- xy.left = offset.left; // 左
- xy.width = obj.outerWidth(); // 宽
- xy.height = obj.outerHeight(); // 高
- xy.margin = parseInt(obj.css('margin').replace("px", "")) || 0;
- xy.margin = isNaN(xy.margin) ? 0 : xy.margin;
- }
- function isDom(obj) {
- obj = obj[0] || obj;
- if (typeof HTMLElement === 'object')
- return obj instanceof HTMLElement;
- else
- return obj && typeof obj === 'object' && obj.nodeType === 1 &&
- typeof obj.nodeName === 'string';
- }
- return xy;
- };
|