Treegrid-dnd.js

Source: Internet
Author: User

(function ($) {
$.extend ($.fn.treegrid.defaults, {
Onbeforedrag:function (Row) {},//return false to deny drag
Onstartdrag:function (Row) {},
Onstopdrag:function (Row) {},
Ondragenter:function (Targetrow, Sourcerow) {},//return false to deny drop
Ondragover:function (Targetrow, Sourcerow) {},//return false to deny drop
Ondragleave:function (Targetrow, Sourcerow) {},
Onbeforedrop:function (Targetrow, Sourcerow, point) {},
Ondrop:function (Targetrow, Sourcerow, point) {}//point: ' Append ', ' top ', ' bottom '
});
$.extend ($.fn.treegrid.methods, {
Disablednd:function (Jq,id) {
Return Jq.each (function () {
var target = this;
var state = $.data (this, ' Treegrid ');
State.disablednodes = [];
var t = $ (this);
var opts = state.options;
if (ID) {
var nodes = Opts.finder.getTr (target, id);
var rows = T.treegrid (' GetChildren ', id);
for (var i = 0; i < rows.length; i++) {
nodes = Nodes.Add (Opts.finder.getTr (target, Rows[i][opts.idfield]);
}
} else {
var nodes = T.treegrid (' Getpanel '). Find (' tr[node-id] ');
}
Nodes.draggable ({
Disabled:true,
Revert:true,
Cursor: ' pointer '});
});
}
});
$.extend ($.fn.treegrid.methods, {
Enablednd:function (JQ, id) {
if (!$ (' #treegrid-dnd-style '). Length) {
$ (' head '). Append (
' <style id= ' treegrid-dnd-style ' > ' +
'. Treegrid-row-top td{border-top:1px Solid Red} ' +
'. Treegrid-row-bottom td{border-bottom:1px Solid Red} ' +
'. Treegrid-row-append. tree-title{border:1px solid Red} ' +
' </style> '
);
}
Return Jq.each (function () {
var target = this;
var state = $.data (this, ' Treegrid ');
State.disablednodes = [];
var t = $ (this);
var opts = state.options;
if (ID) {
var nodes = Opts.finder.getTr (target, id);
var rows = T.treegrid (' GetChildren ', id);
for (var i=0; i<rows.length; i++) {
nodes = Nodes.Add (Opts.finder.getTr (target, Rows[i][opts.idfield]);
}
} else {
var nodes = T.treegrid (' Getpanel '). Find (' tr[node-id] ');
}
Nodes.draggable ({
Disabled:false,
Revert:true,
Cursor: ' pointer ',
Proxy:function (source) {
var row = T.treegrid (' Find ', $ (source). attr (' Node-id '));
var p = $ (' <div class= "Tree-node-proxy" ></div> "). AppendTo (' body ');
P.html (' <span class= ' Tree-dnd-icon tree-dnd-no ' >&nbsp;</span> ' +row[opts.treefield]);
P.hide ();
return p;
},
Deltax:15,
Deltay:15,
Onbeforedrag:function (e) {
if (Opts.onBeforeDrag.call (target, GetRow (this)) = = False) {return false}
if ($ (e.target). Hasclass (' tree-hit ') | | $ (e.target). Parent (). Hasclass (' Datagrid-cell-check ')) {return false;}
if (E.which! = 1) {return false;}
$ (this). Next (' Tr.treegrid-tr-tree '). Find (' Tr[node-id] '). Droppable ({accept: ' no-accept '});
var tr = opts.finder.getTr (target, $ (this). attr (' Node-id '));
var treetitle = tr.find (' Span.tree-title ');
E.data.startx = Treetitle.offset (). Left;
E.data.starty = Treetitle.offset (). Top;
e.data.offsetwidth = 0;
e.data.offsetheight = 0;
},
Onstartdrag:function () {
$ (this). Draggable (' proxy '). CSS ({
left:-10000,
top:-10000
});
var row = GetRow (this);
Opts.onStartDrag.call (target, row);
State.draggingnodeid = Row[opts.idfield];
},
Ondrag:function (e) {
var X1=e.pagex,y1=e.pagey,x2=e.data.startx,y2=e.data.starty;
var d = math.sqrt ((x1-x2) * (X1-X2) + (y1-y2) * (Y1-y2));
if (d>3) {//When drag a little distance, show the proxy object
$ (this). Draggable (' proxy '). Show ();
var tr = opts.finder.getTr (target, $ (this). attr (' Node-id '));
var treetitle = tr.find (' Span.tree-title ');
E.data.startx = Treetitle.offset (). Left;
E.data.starty = Treetitle.offset (). Top;
e.data.offsetwidth = 0;
e.data.offsetheight = 0;
}
This.pagey = E.pagey;
},
Onstopdrag:function () {
$ (this). Next (' Tr.treegrid-tr-tree '). Find (' Tr[node-id] '). Droppable ({accept: ' Tr[node-id] '});
for (var i=0; i<state.disablednodes.length; i++) {
var tr = opts.finder.getTr (target, state.disablednodes[i]);
Tr.droppable (' Enable ');
}
State.disablednodes = [];
var row = T.treegrid (' Find ', State.draggingnodeid);
Opts.onStopDrag.call (target, row);
}
}). droppable ({
Accept: ' Tr[node-id] ',
Ondragenter:function (E, source) {
if (Opts.onDragEnter.call (target, GetRow (this), GetRow (source)) = = False) {
AllowDrop (source, false);
var tr = opts.finder.getTr (target, $ (this). attr (' Node-id '));
Tr.removeclass (' treegrid-row-append treegrid-row-top treegrid-row-bottom ');
Tr.droppable (' Disable ');
State.disabledNodes.push ($ (this). attr (' Node-id '));
}
},
Ondragover:function (E,source) {
var nodeId = $ (this). attr (' Node-id ');
if ($.inarray (NodeId, State.disablednodes) >= 0) {return}
var pagey = Source.pagey;
var top = $ (this). Offset (). Top;
var bottom = top + $ (this). Outerheight ();

AllowDrop (source, true);
var tr = opts.finder.getTr (target, nodeId);
Tr.removeclass (' treegrid-row-append treegrid-row-top treegrid-row-bottom ');
if (Pagey > Top + (bottom-top)/2) {
if (Bottom-pagey < 5) {
Tr.addclass (' Treegrid-row-bottom ');
} else {
Tr.addclass (' treegrid-row-append ');
}
} else {
if (Pagey-top < 5) {
Tr.addclass (' treegrid-row-top ');
} else {
Tr.addclass (' treegrid-row-append ');
}
}
if (Opts.onDragOver.call (target, GetRow (this), GetRow (source)) = = False) {
AllowDrop (source, false);
Tr.removeclass (' treegrid-row-append treegrid-row-top treegrid-row-bottom ');
Tr.droppable (' Disable ');
State.disabledNodes.push (NODEID);
}
},
Ondragleave:function (E,source) {
AllowDrop (source, false);
var tr = opts.finder.getTr (target, $ (this). attr (' Node-id '));
Tr.removeclass (' treegrid-row-append treegrid-row-top treegrid-row-bottom ');
Opts.onDragLeave.call (Target, GetRow (this), GetRow (source));
},
Ondrop:function (E,source) {
var dest = this;
var action, point;
var tr = opts.finder.getTr (target, $ (this). attr (' Node-id '));
if (Tr.hasclass (' treegrid-row-append ')) {
action = append;
Point = ' append ';
} else {
action = insert;
Point = Tr.hasclass (' Treegrid-row-top ')? ' Top ': ' Bottom ';
}

var drow = GetRow (this);
var SRow = GetRow (source);
if (Opts.onBeforeDrop.call (target, Drow, sRow, point) = = False) {
Tr.removeclass (' treegrid-row-append treegrid-row-top treegrid-row-bottom ');
Return
}
Action (SRow, Drow, point);
Tr.removeclass (' treegrid-row-append treegrid-row-top treegrid-row-bottom ');
}
});

function AllowDrop (source, allowed) {
var icon = $ (source). draggable (' proxy '). Find (' Span.tree-dnd-icon ');
Icon.removeclass (' Tree-dnd-yes tree-dnd-no '). addclass (allowed?) ' Tree-dnd-yes ': ' tree-dnd-no ');
}
function GetRow (TR) {
var nodeId = $ (tr). attr (' Node-id ');
Return T.treegrid (' Find ', nodeId);
}
function append (SRow, drow) {
Doappend ();
if (drow.state = = ' closed ') {
T.treegrid (' expand ', Drow[opts.idfield]);
}

function Doappend () {
var data = T.treegrid (' Pop ', Srow[opts.idfield]);
T.treegrid (' append ', {
Parent:drow[opts.idfield],
Data: [Data]
});
Opts.onDrop.call (target, drow, data, ' append ');
}
}
function Insert (SRow, Drow, point) {
var param = {};
if (point = = ' top ') {
Param.before = Drow[opts.idfield];
} else {
Param.after = Drow[opts.idfield];
}

var data = T.treegrid (' Pop ', Srow[opts.idfield]);
Param.data = data;
T.treegrid (' Insert ', param);
Opts.onDrop.call (target, drow, data, point);
}
});
}
});
}) (JQuery);

Treegrid-dnd.js

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.