Skip to content

Commit 71c6f58

Browse files
committed
Add Service $TreeTableConvert
Add function $TreeTableConvert.line2tree() -> tree vaild. Add function $TreeTableConvert.tree2tree() -> tree vaild.
1 parent ab4c083 commit 71c6f58

File tree

1 file changed

+80
-33
lines changed

1 file changed

+80
-33
lines changed

src/ng-tree-table.js

Lines changed: 80 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
replace: true,
4848
controller: [
4949
'$scope', '$element', '$attrs', 'tgConfig', function ($scope, $element, $attrs, tgConfig) {
50-
5150
$scope.$type = 'TreeTable';
5251
$scope.dragEnabled = true;
5352
$scope.dragDelay = 0;
@@ -125,11 +124,17 @@
125124
beforeDrop: function (event) {
126125
return true;
127126
},
128-
calsIndent: function (level) {
127+
calsIndent: function (level, skipUnit) {
128+
var _unit = $scope.indent_unit ? $scope.indent_unit : 'px';
129+
if (!skipUnit) {
130+
_unit = $scope.indent_unit ? $scope.indent_unit : 'px';
131+
} else {
132+
_unit = '';
133+
}
129134
if (level - 1 < 1) {
130-
return $scope.indent_plus + ($scope.indent_unit ? $scope.indent_unit : 'px');
135+
return $scope.indent_plus + _unit;
131136
} else {
132-
return ($scope.indent * (level - 1)) + $scope.indent_plus + ($scope.indent_unit ? $scope.indent_unit : 'px');
137+
return ($scope.indent * (level - 1)) + $scope.indent_plus + _unit;
133138
}
134139
},
135140
dragEnabled: function () {
@@ -184,6 +189,12 @@
184189
},
185190
link: function (scope, element, attrs) {
186191

192+
scope.$watch(
193+
attrs.treeData, function (newValue) {
194+
console.log('hre', newValue)
195+
}
196+
);
197+
187198
scope.$watch(
188199
attrs.callbacks, function (optCallbacks) {
189200
angular.forEach(
@@ -895,15 +906,6 @@
895906
dragElm.css('width', ($helper.width(scope.$element) + 10) + 'px');
896907
dragElm.css('z-index', 9999);
897908

898-
// Prevents cursor to change rapidly in Opera 12.16 and IE when dragging an element
899-
var hStyle = (scope.$element[0].querySelector('.tree-table-handle') || scope.$element[0]).currentStyle;
900-
if (hStyle) {
901-
document.body.setAttribute(
902-
'tree-table-cursor', $document.find('body').css('cursor') || ''
903-
);
904-
$document.find('body').css({'cursor': hStyle.cursor + '!important'});
905-
}
906-
907909
var _tbody = angular.element($window.document.createElement('tbody'));
908910
// moving item with descendant
909911

@@ -921,6 +923,7 @@
921923
element = element.next();
922924
}
923925
}
926+
924927
drag_descendant(scope.$element, scope.node().__dept__);
925928
dragElm.append(_tbody);
926929

@@ -992,7 +995,6 @@
992995
if ((leftElmPos + 10) > document_width) {
993996
leftElmPos = document_width - 10;
994997
}
995-
996998
dragElm.css(
997999
{
9981000
'left': leftElmPos + 'px',
@@ -1189,13 +1191,6 @@
11891191
dragInfo = null;
11901192
}
11911193

1192-
// Restore cursor in Opera 12.16 and IE
1193-
var oldCur = document.body.getAttribute('tree-table-cursor');
1194-
if (oldCur !== null) {
1195-
$document.find('body').css({'cursor': oldCur});
1196-
document.body.removeAttribute('tree-table-cursor');
1197-
}
1198-
11991194
angular.element($document).unbind('touchend', dragEndEvent); // Mobile
12001195
angular.element($document).unbind('touchcancel', dragEndEvent); // Mobile
12011196
angular.element($document).unbind('touchmove', dragMoveEvent); // Mobile
@@ -1313,32 +1308,32 @@
13131308
},
13141309
dragInfo: function (scope) {
13151310
return {
1316-
node: scope.node(),
1317-
scope: scope,
1318-
level: scope.node().__level__,
1319-
target: {
1311+
node: scope.node(),
1312+
scope: scope,
1313+
level: scope.node().__level__,
1314+
target: {
13201315
node: scope.prev(),
13211316
pos: 1,
13221317
},
1323-
isDirty: function (index) {
1318+
isDirty: function (index) {
13241319
return this.node.__index_real__ <= index && index <= this.node.__index_real__ + this.node.__dept__ - 1;
13251320
},
1326-
isTargetEmpty: function(){
1321+
isTargetEmpty: function () {
13271322
return !this.target.node.__expanded__ || (this.target.node.__index_real__ == this.node.__parent__ && this.target.node.__children__.length == 1);
13281323
},
1329-
next: function () {
1324+
next: function () {
13301325
if (this.node.__index_real__ < this.source.length - 1) {
13311326
return this.source[this.node.__index_real__ + 1];
13321327
}
13331328
return null;
13341329
},
1335-
prev: function () {
1330+
prev: function () {
13361331
if (this.node.__index_real__ > 0) {
13371332
return this.source[this.node.__index_real__ - 1];
13381333
}
13391334
return null;
13401335
},
1341-
attach: function (node, pos) {
1336+
attach: function (node, pos) {
13421337
this.target.node = node;
13431338
this.target.pos = pos;
13441339
}
@@ -1409,10 +1404,12 @@
14091404
}
14101405
pos.dirAx = newAx;
14111406
},
1412-
replaceIndent: function (e, indent) {
1413-
angular.element(e.children()[0]).css('left', this.calsIndent(indent));
1407+
replaceIndent: function (e, indent, attr) {
1408+
attr = attr ? attr : 'left';
1409+
angular.element(e.children()[0]).css(attr, this.calsIndent(indent));
14141410
},
1415-
hidden: function (e) {
1411+
1412+
hidden: function (e) {
14161413
if (angular.isFunction(e.hide)) {
14171414
e.hide();
14181415
} else {
@@ -1422,6 +1419,56 @@
14221419
}
14231420
};
14241421
}]
1422+
).factory(
1423+
'$TreeTableConvert', function () {
1424+
return {
1425+
line2tree: function (data, primaryKey, parentKey) {
1426+
if (!data || data.length == 0 || !primaryKey || !parentKey) {
1427+
return [];
1428+
}
1429+
1430+
var tree = [], rootIds = [], item = data[0], _primary = item[primaryKey], treeObjs = {}, parentId, parent, len = data.length, i = 0;
1431+
while (i < len) {
1432+
item = data[i++];
1433+
_primary = item[primaryKey];
1434+
treeObjs[_primary] = item;
1435+
parentId = item[parentKey];
1436+
if (parentId) {
1437+
parent = treeObjs[parentId];
1438+
if (parent.__children__) {
1439+
parent.__children__.push(item);
1440+
} else {
1441+
parent.__children__ = [item];
1442+
}
1443+
} else {
1444+
rootIds.push(_primary);
1445+
}
1446+
}
1447+
len = rootIds.length;
1448+
for (i = 0; i < len; i++) {
1449+
tree.push(treeObjs[rootIds[i]]);
1450+
}
1451+
return tree;
1452+
},
1453+
tree2tree: function (data, parentKey) {
1454+
var access_child = function (data) {
1455+
var _tree = [];
1456+
var _i, _len = data.length, _copy, _child;
1457+
for (_i = 0; _i < _len; _i++) {
1458+
_copy = angular.copy(data[_i]);
1459+
if (angular.isArray(_copy[parentKey]) && _copy[parentKey].length > 0) {
1460+
_child = access_child(_copy[parentKey]);
1461+
delete(_copy[parentKey]);
1462+
_copy.__children__ = _child;
1463+
}
1464+
_tree.push(_copy);
1465+
}
1466+
return _tree;
1467+
}
1468+
return access_child(data);
1469+
}
1470+
}
1471+
}
14251472
).constant(
14261473
'tgConfig', {
14271474
treeClass: 'tree-table',

0 commit comments

Comments
 (0)