tree_table.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. /*
  2. * Created by MaiXinRong on 2017/2/9.
  3. * 项目管理 专用树结构
  4. */
  5. (function($) {
  6. var _setting = {
  7. tree: {
  8. id: 'ID',
  9. pid: 'ParentID',
  10. nid: 'NextSiblingID',
  11. btnColumn: 1,
  12. nullId: -1
  13. },
  14. columns: [
  15. {
  16. head: '工程列表',
  17. data: 'name',
  18. static: false,
  19. width: '100%',
  20. event: {
  21. getText: null,
  22. getIcon: null
  23. }
  24. }
  25. ],
  26. dataTemp: {
  27. id: -1,
  28. parentId: -1,
  29. nextId: -1,
  30. isTemp: true
  31. },
  32. viewEvent: {
  33. beforeSelect: null,
  34. onSelectNode: null
  35. }
  36. };
  37. var Node = (function () {
  38. function Node(tree, data) {
  39. this.parent = null;
  40. this.nextSibling = null;
  41. this.children = [];
  42. this.tree = tree;
  43. this.data = data;
  44. this.setting = tree.setting;
  45. this.expanded = true;
  46. this.row = null;
  47. this.expandBtn = null;
  48. }
  49. Node.prototype.firstChild = function() {
  50. return (this.children.length === 0) ? null : this.children[0];
  51. };
  52. Node.prototype.lastChild = function () {
  53. return (this.children.length === 0) ? null : this.children[this.children.length - 1];
  54. }
  55. Node.prototype.deepestRow = function () {
  56. return (this.children.length === 0) ? this.row : this.lastChild().deepestRow();
  57. }
  58. Node.prototype.addChild = function (child, childNext) {
  59. if (childNext){
  60. this.children.push(child);
  61. } else {
  62. if (this.childIndex(childNext) > -1){
  63. this.children.splice(this.childIndex(childNext) - 1, 0, child);
  64. } else {
  65. this.children.push(child);
  66. }
  67. }
  68. return child;
  69. };
  70. Node.prototype.childIndex = function (child) {
  71. return this.children.indexOf(child);
  72. };
  73. Node.prototype.depth = function () {
  74. return this.parent ? this.parent.depth() + 1 : 0;
  75. };
  76. Node.prototype.domId = function () {
  77. return this.data ? this.tree.domId + '_' + this.data[this.setting.tree.id] : '';
  78. };
  79. Node.prototype.expand = function (bool) {
  80. this.expanded = bool;
  81. _view._refreshTreeBtn(this);
  82. if (this.expanded) {
  83. _view._showNodes(this.children);
  84. } else {
  85. _view._hideNodes(this.children);
  86. }
  87. };
  88. Node.prototype.preSibling = function () {
  89. var iIndex = this.parent.childIndex(this);
  90. if (iIndex === -1){
  91. return null;
  92. } else {
  93. return iIndex > 0 ? this.parent.children[iIndex-1] : null;
  94. }
  95. };
  96. Node.prototype.setParent = function (parent) {
  97. if (parent && this.parent !== parent) {
  98. this.parent = parent;
  99. this.data[this.setting.tree.pid] = this.pid();
  100. }
  101. };
  102. Node.prototype.setNextSibling = function (nextSibling) {
  103. if (this.nextSibling !== nextSibling) {
  104. this.nextSibling = nextSibling;
  105. this.data[this.setting.tree.nid] = this.nid();
  106. }
  107. }
  108. Node.prototype.id = function () {
  109. return this.data ? this.data[this.setting.tree.id] : -1;
  110. };
  111. Node.prototype.pid = function () {
  112. return this.parent ? this.parent.id() : -1;
  113. };
  114. Node.prototype.nid = function () {
  115. return this.nextSibling ? this.nextSibling.id() : -1;
  116. };
  117. Node.prototype.propertyJoin = function (dataName) {
  118. return this.parent ? this.parent.propertyJoin(dataName) + ';' + this.data[dataName] : this.data[dataName];
  119. }
  120. return Node;
  121. })();
  122. var Tree = (function () {
  123. function Tree(obj, setting) {
  124. this._root = new Node(this);
  125. this.treeObj = obj;
  126. this.domId = obj.attr('id');
  127. this.treeHeadObj = _view._makeTableHead(this.treeObj, setting);
  128. this.treeBodyObj = _view._makeTableBody(this.treeObj);
  129. this.setting = setting;
  130. var _maxNodeId = 0;
  131. this.newNodeId = function (id) {
  132. if (arguments.length > 0){
  133. _maxNodeId = (id > _maxNodeId) ? id : _maxNodeId;
  134. } else {
  135. _maxNodeId += 1;
  136. return _maxNodeId;
  137. }
  138. };
  139. this.maxNodeId = function (id){
  140. if (arguments.length > 0) {
  141. _maxNodeId = Math.max(id, _maxNodeId);
  142. } else {
  143. return _maxNodeId;
  144. }
  145. }
  146. };
  147. Tree.prototype.firstNode = function (){
  148. return this._root.firstChild();
  149. };
  150. Tree.prototype.traverseDF = function(callback){
  151. var recurse = function (node) {
  152. var i;
  153. if (node !== this._root) {
  154. callback(node);
  155. }
  156. for (i = 0; i < node.children.length; i++){
  157. recurse(node.children[i]);
  158. }
  159. }
  160. recurse(this._root);
  161. };
  162. Tree.prototype.findNode = function (id){
  163. var treenode = null,
  164. callback = function (node) {
  165. if (node.data && node.data[node.setting.tree.id] === id){
  166. treenode = node;
  167. }
  168. };
  169. this.traverseDF.call(this, callback);
  170. return treenode;
  171. };
  172. Tree.prototype.findNodeByNid = function (nid) {
  173. let treenode = null,
  174. callback = function (node) {
  175. if(node.data && node.data[node.setting.tree.nid] === nid){
  176. treenode = node;
  177. }
  178. };
  179. this.traverseDF.call(this, callback);
  180. return treenode;
  181. }
  182. Tree.prototype.findNodeByDomId = function (domId) {
  183. var treenode = null,
  184. callback = function (node) {
  185. if (node.domId === domId) {
  186. treenode = node;
  187. }
  188. };
  189. this.traverseDF.call(this, callback);
  190. return treenode;
  191. };
  192. Tree.prototype.removeNode = function (node) {
  193. var iIndex;
  194. if (node) {
  195. iIndex = node.parent.childIndex(node);
  196. if (iIndex > 0) {
  197. node.parent.children[iIndex - 1].setNextSibling(node.nextSibling);
  198. }
  199. node.parent.children.splice(iIndex, 1);
  200. }
  201. _view._removeNodesRowDom([node]);
  202. };
  203. Tree.prototype.loadData = function (arrData) {
  204. var i, that = this;
  205. var createTempNode = function (id, setting) {
  206. var tempData = {};
  207. tempData[setting.tree.id] = id;
  208. return new Node(that, tempData);
  209. };
  210. var loadNode = function (data, setting) {
  211. var node = that.findNode(data[setting.tree.id]) || null,
  212. parent = that.findNode(data[setting.tree.pid]) || null,
  213. next = that.findNode(data[setting.tree.nid]) || null,
  214. tempData;
  215. if (!node) {
  216. node = new Node(that, data);
  217. }
  218. node.data = data;
  219. that.maxNodeId(node.id());
  220. if (!parent){
  221. if (data[setting.tree.pid] === setting.tree.nullId) {
  222. parent = that._root;
  223. } else {
  224. parent = createTempNode(data[setting.tree.pid], setting);
  225. parent.parent = that._root;
  226. that._root.children.push(parent);
  227. }
  228. }
  229. if (node.parent && node.parent !== parent) {
  230. node.parent.children.splice(node.parent.childIndex(node), 1);
  231. }
  232. node.parent = parent;
  233. if (!next && data[setting.tree.nid] !== setting.tree.nullId) {
  234. next = createTempNode(data[setting.tree.nid], setting);
  235. next.parent = parent;
  236. parent.children.push(next);
  237. }
  238. node.nextSibling = next;
  239. if (parent.childIndex(node) === -1){
  240. if (!next){
  241. parent.children.push(node);
  242. } else if (parent.childIndex(next) === -1){
  243. parent.children.push(node);
  244. parent.children.push(next);
  245. } else {
  246. parent.children.splice(parent.childIndex(next), 0, node);
  247. }
  248. } else if (node.nextSibling && parent.childIndex(node) !== parent.childIndex(node.nextSibling) - 1) {
  249. parent.children.splice(parent.childIndex(node), 1);
  250. parent.children.splice(parent.childIndex(next), 0, node);
  251. };
  252. let pre = that.findNodeByNid(node.data.ID) || null;
  253. if(pre && parent.childIndex(pre) !== parent.childIndex(node) - 1){
  254. parent.children.splice(parent.childIndex(pre), 1);
  255. parent.children.splice(parent.childIndex(node), 0, pre);
  256. }
  257. };
  258. for (i = 0; i < arrData.length; i++){
  259. loadNode(arrData[i], this.setting);
  260. }
  261. };
  262. Tree.prototype.refreshNodesDom = function (nodes, recurse){
  263. var that = this;
  264. nodes.forEach(function (node) {
  265. if (node.row) {
  266. $('td', node.row).remove();
  267. _view._makeTableRowCells(node.row, node);
  268. } else if (node !== that._root) {
  269. _view._makeRowDom(that.treeBodyObj, node);
  270. }
  271. if (recurse) {
  272. that.refreshNodesDom(node.children, recurse);
  273. }
  274. })
  275. };
  276. Tree.prototype.refreshTreeDom = function () {
  277. this.refreshNodesDom(this._root.children, true);
  278. };
  279. Tree.prototype.addNodeData = function (data, parent, nextSibling) {
  280. var node = null;
  281. var pNode = parent ? parent : this._root;
  282. if (!nextSibling || (nextSibling.parent === pNode && pNode.childIndex(nextSibling) > -1)) {
  283. node = new Node(this, data);
  284. this.maxNodeId(data[this.setting.tree.id]);
  285. node.row = _view._makeRowDom(this.treeBodyObj, node);
  286. this.move(node, pNode, nextSibling);
  287. }
  288. return node;
  289. }
  290. Tree.prototype.move = function(node, parent, nextSibling) {
  291. var iIndex = -1, pre;
  292. if (parent && (!nextSibling || (nextSibling.parent === parent && parent.childIndex(nextSibling) > -1))) {
  293. if (node) {
  294. if (node.parent) {
  295. iIndex = node.parent.childIndex(node);
  296. if (iIndex > 0) {
  297. node.parent.children[iIndex - 1].setNextSibling(node.nextSibling);
  298. }
  299. node.parent.children.splice(iIndex, 1);
  300. this.refreshNodesDom([node.parent], false);
  301. }
  302. if (nextSibling) {
  303. iIndex = parent.childIndex(nextSibling);
  304. if (iIndex > 0){
  305. pre = parent.children[iIndex - 1];
  306. pre.setNextSibling(node);
  307. parent.children.splice(iIndex - 1, 0, node);
  308. } else {
  309. parent.children.splice(0, 0, node);
  310. }
  311. } else {
  312. if (parent.children.length > 0){
  313. pre = parent.lastChild();
  314. pre.setNextSibling(node);
  315. }
  316. parent.children.push(node);
  317. }
  318. node.setParent(parent);
  319. node.setNextSibling(nextSibling);
  320. if (node.row) {
  321. if (pre) {
  322. _view._moveRowDom(node, pre.deepestRow());
  323. } else if (parent.id() !== this.setting.tree.nullId) {
  324. _view._moveRowDom(node, parent.row);
  325. } else if (nextSibling) {
  326. _view._moveRowDomBefore(node, nextSibling.row);
  327. }
  328. }
  329. if (node.parent.row) {
  330. this.refreshNodesDom([node.parent], false);
  331. }
  332. }
  333. } else {
  334. this.e.throw('Error: information of moving node has mistake.');
  335. }
  336. };
  337. Tree.prototype.selected = (function () {
  338. var selectNode = null;
  339. var select = function (node) {
  340. if (arguments.length === 0) {
  341. return selectNode;
  342. } else {
  343. if (node) {
  344. if (selectNode && selectNode.row) {
  345. selectNode.row.removeClass('table-active');
  346. }
  347. selectNode = node;
  348. node.row.addClass('table-active');
  349. }
  350. }
  351. }
  352. return select;
  353. })();
  354. return Tree;
  355. })();
  356. _data = {
  357. clone: function (obj) {
  358. if (obj === null) return null;
  359. var o = _data.isArray(obj) ? [] : {};
  360. for (var i in obj) {
  361. o[i] = (obj[i] instanceof Date) ? new Date(obj[i].getTime()) : (typeof obj[i] === "object" ? _data.clone(obj[i]) : obj[i]);
  362. }
  363. return o;
  364. },
  365. isArray: function (arr) {
  366. return Object.prototype.toString.apply(arr) === "[object Array]";
  367. }
  368. },
  369. _event = {
  370. _selectProj: function (node) {
  371. if (node.setting.viewEvent.beforeSelect) {
  372. node.setting.viewEvent.beforeSelect(node.tree.selected());
  373. }
  374. node.tree.selected(node);
  375. if (node.setting.viewEvent.onSelectNode) {
  376. node.setting.viewEvent.onSelectNode(node);
  377. }
  378. }
  379. }
  380. _view = {
  381. _makeTableHeadCell: function (obj, col){
  382. var th;
  383. th = $('<th>');
  384. th.attr('width', col.width);
  385. th.text(col.head);
  386. obj.append(th);
  387. },
  388. _makeTableHead: function (obj, setting){
  389. var thead, tr, i;
  390. thead = $('<thead>');
  391. tr = $('<tr>');
  392. for (i = 0; i < setting.columns.length; i++){
  393. _view._makeTableHeadCell(tr, setting.columns[i]);
  394. }
  395. thead.append(tr);
  396. obj.append(thead);
  397. return thead;
  398. },
  399. _makeTableBody: function (obj){
  400. var tbody;
  401. tbody = $('<tbody>');
  402. obj.append(tbody);
  403. return tbody;
  404. },
  405. _makeTableRowCell: function (obj, node, columns, index) {
  406. var html = [], td, i, url;
  407. html.push('<td ');
  408. if (index === node.setting.tree.btnColumn){
  409. html.push('class=', 'in-', node.depth().toString(), '>');
  410. if (node.children.length !== 0) {
  411. html.push('<a href="#" class="tree-open" title="收起"><i class="fa fa-minus-square-o mr-1"></i></a>');
  412. } else {
  413. html.push('<a href="#" class="tree-open" title="收起"><i></i></a>');
  414. }
  415. } else {
  416. html.push('>');
  417. }
  418. if (columns.event.getIcon) {
  419. columns.event.getIcon(html, node);
  420. }
  421. if (columns.name !== '') {
  422. if (columns.event.getText) {
  423. columns.event.getText(html, node, node.data[columns.data]);
  424. } else {
  425. html.push(node.data[columns.name]);
  426. }
  427. }
  428. html.push('</td>');
  429. td = $(html.join(''));
  430. if (index === node.setting.tree.btnColumn) {
  431. node.expandBtn = $('.tree-open>.fa', td);
  432. node.expandBtn.bind('click', {node: node}, function(e){
  433. e.data.node.expand(!e.data.node.expanded);
  434. });
  435. node.icon = $('.tree-icon', td);
  436. }
  437. if (columns.event.tdBindEvent) {
  438. columns.event.tdBindEvent(td, node);
  439. }
  440. obj.append(td);
  441. return td;
  442. },
  443. _makeTableRowCells: function (obj, node) {
  444. for (i = 0; i < node.setting.columns.length; i++){
  445. _view._makeTableRowCell(obj, node, node.setting.columns[i], i);
  446. }
  447. },
  448. _makeRowDom: function (obj, node) {
  449. var tr, i;
  450. tr = $('<tr>');
  451. obj.append(tr);
  452. tr.attr('id', node.domId());
  453. node.row = tr;
  454. _view._makeTableRowCells(tr, node);
  455. tr.click(function(){
  456. _event._selectProj(node);
  457. });
  458. /*$(tr).draggable({
  459. helper: "clone",
  460. opacity: .75,
  461. refreshPositions: true, // Performance?
  462. revert: "invalid",
  463. revertDuration: 300,
  464. scroll: true
  465. });
  466. tr.droppable({
  467. drop: function (e, ui) {
  468. var target = node.tree.findNodeByDomId($(this).attr('id')),
  469. drag = node.tree.findNodeByDomId($(ui).attr('id'));
  470. tree.move(drag, target);
  471. }
  472. }); */
  473. return tr;
  474. },
  475. _moveRowDomBefore: function (node, destination) {
  476. var moveNodesRowDomBefore = function (nodes) {
  477. nodes.forEach(function (node) {
  478. node.row.insertBefore(destination);
  479. moveNodesRowDomBefore(node.children);
  480. });
  481. }
  482. moveNodesRowDomBefore([node]);
  483. _view._refreshNodesLevelCss([node]);
  484. },
  485. _moveRowDom: function (node, destination) {
  486. var pre = destination,
  487. moveNodeRowDom = function (node) {
  488. node.row.insertAfter(pre);
  489. pre = node.row;
  490. moveChildrenRowDom(node);
  491. },
  492. moveChildrenRowDom = function(node){
  493. var i;
  494. for (i = 0; i < node.children.length; i++){
  495. moveNodeRowDom(node.children[i]);
  496. }
  497. };
  498. moveNodeRowDom(node);
  499. _view._refreshNodesLevelCss([node]);
  500. },
  501. _refreshTreeBtn: function (node) {
  502. var i;
  503. if (node.children.length === 0) {
  504. node.expandBtn.hide();
  505. } else {
  506. node.expandBtn.show();
  507. if (node.expanded) {
  508. node.expandBtn.removeClass('fa-plus-square-o');
  509. node.expandBtn.addClass('fa-minus-square-o');
  510. } else {
  511. node.expandBtn.removeClass('fa-minus-square-o');
  512. node.expandBtn.addClass('fa-plus-square-o');
  513. }
  514. }
  515. },
  516. _refreshNodesLevelCss: function (nodes) {
  517. nodes.forEach(function(node){
  518. var td = $('td:eq(' + node.setting.tree.btnColumn.toString() + ')', node.row);
  519. td.attr('class', 'in-'+ node.depth().toString());
  520. _view._refreshNodesLevelCss(node.children);
  521. })
  522. },
  523. _hideNodes: function (nodes) {
  524. var i, node;
  525. for (i = 0; i < nodes.length; i++){
  526. node = nodes[i];
  527. node.row.hide();
  528. _view._hideNodes(node.children);
  529. }
  530. },
  531. _showNodes: function (nodes) {
  532. var i, node;
  533. for (i = 0; i < nodes.length; i++){
  534. node = nodes[i];
  535. node.row.show();
  536. if (node.expanded) {
  537. _view._showNodes(node.children);
  538. }
  539. }
  540. },
  541. _removeNodesRowDom: function (nodes) {
  542. var i, node;
  543. for (i = 0; i < nodes.length; i++){
  544. node = nodes[i];
  545. _view._removeNodesRowDom(node.children);
  546. if (node.row) {
  547. node.row.remove();
  548. }
  549. }
  550. }
  551. };
  552. $.fn.treeTable = {
  553. init: function(obj, setting, arrData){
  554. var _treeSetting, _tree;
  555. _treeSetting = _data.clone(_setting);
  556. $.extend(true, _treeSetting, setting);
  557. var _tree = new Tree(obj, _treeSetting);
  558. _tree.loadData(arrData);
  559. _tree.refreshTreeDom();
  560. return _tree;
  561. }
  562. }
  563. })(jQuery);