tree_table.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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.findNodeByDomId = function (domId) {
  173. var treenode = null,
  174. callback = function (node) {
  175. if (node.domId === domId) {
  176. treenode = node;
  177. }
  178. };
  179. this.traverseDF.call(this, callback);
  180. return treenode;
  181. };
  182. Tree.prototype.removeNode = function (node) {
  183. var iIndex;
  184. if (node) {
  185. iIndex = node.parent.childIndex(node);
  186. if (iIndex > 0) {
  187. node.parent.children[iIndex - 1].setNextSibling(node.nextSibling);
  188. }
  189. node.parent.children.splice(iIndex, 1);
  190. }
  191. _view._removeNodesRowDom([node]);
  192. };
  193. Tree.prototype.loadData = function (arrData) {
  194. var i, that = this;
  195. var createTempNode = function (id, setting) {
  196. var tempData = {};
  197. tempData[setting.tree.id] = id;
  198. return new Node(that, tempData);
  199. };
  200. var loadNode = function (data, setting) {
  201. var node = that.findNode(data[setting.tree.id]) || null,
  202. parent = that.findNode(data[setting.tree.pid]) || null,
  203. next = that.findNode(data[setting.tree.nid]) || null,
  204. tempData;
  205. if (!node) {
  206. node = new Node(that, data);
  207. }
  208. node.data = data;
  209. that.maxNodeId(node.id());
  210. if (!parent){
  211. if (data[setting.tree.pid] === setting.tree.nullId) {
  212. parent = that._root;
  213. } else {
  214. parent = createTempNode(data[setting.tree.pid], setting);
  215. parent.parent = that._root;
  216. that._root.children.push(parent);
  217. }
  218. }
  219. if (node.parent && node.parent !== parent) {
  220. node.parent.children.splice(node.parent.childIndex(node), 1);
  221. }
  222. node.parent = parent;
  223. if (!next && data[setting.tree.nid] !== setting.tree.nullId) {
  224. next = createTempNode(data[setting.tree.nid], setting);
  225. next.parent = parent;
  226. parent.children.push(next);
  227. }
  228. node.nextSibling = next;
  229. if (parent.childIndex(node) === -1){
  230. if (!next){
  231. parent.children.push(node);
  232. } else if (parent.childIndex(next) === -1){
  233. parent.children.push(node);
  234. parent.children.push(next);
  235. } else {
  236. parent.children.splice(parent.childIndex(next), 0, node);
  237. }
  238. } else if (parent.childIndex(node) !== parent.childIndex(node.next) - 1) {
  239. parent.children.splice(parent.childIndex(node), 1);
  240. parent.children.splice(parent.childIndex(next), 0, node);
  241. };
  242. };
  243. for (i = 0; i < arrData.length; i++){
  244. loadNode(arrData[i], this.setting);
  245. }
  246. };
  247. Tree.prototype.refreshNodesDom = function (nodes, recurse){
  248. var that = this;
  249. nodes.forEach(function (node) {
  250. if (node.row) {
  251. $('td', node.row).remove();
  252. _view._makeTableRowCells(node.row, node);
  253. } else if (node !== that._root) {
  254. _view._makeRowDom(that.treeBodyObj, node);
  255. }
  256. if (recurse) {
  257. that.refreshNodesDom(node.children, recurse);
  258. }
  259. })
  260. };
  261. Tree.prototype.refreshTreeDom = function () {
  262. this.refreshNodesDom(this._root.children, true);
  263. };
  264. Tree.prototype.addNodeData = function (data, parent, nextSibling) {
  265. var node = null;
  266. var pNode = parent ? parent : this._root;
  267. if (!nextSibling || (nextSibling.parent === pNode && pNode.childIndex(nextSibling) > -1)) {
  268. node = new Node(this, data);
  269. this.maxNodeId(data[this.setting.tree.id]);
  270. node.row = _view._makeRowDom(this.treeBodyObj, node);
  271. this.move(node, pNode, nextSibling);
  272. }
  273. return node;
  274. }
  275. Tree.prototype.move = function(node, parent, nextSibling) {
  276. var iIndex = -1, pre;
  277. if (parent && (!nextSibling || (nextSibling.parent === parent && parent.childIndex(nextSibling) > -1))) {
  278. if (node) {
  279. if (node.parent) {
  280. iIndex = node.parent.childIndex(node);
  281. if (iIndex > 0) {
  282. node.parent.children[iIndex - 1].setNextSibling(node.nextSibling);
  283. }
  284. node.parent.children.splice(iIndex, 1);
  285. this.refreshNodesDom([node.parent], false);
  286. }
  287. if (nextSibling) {
  288. iIndex = parent.childIndex(nextSibling);
  289. if (iIndex > 0){
  290. pre = parent.children[iIndex - 1];
  291. pre.setNextSibling(node);
  292. parent.children.splice(iIndex - 1, 0, node);
  293. } else {
  294. parent.children.splice(0, 0, node);
  295. }
  296. } else {
  297. if (parent.children.length > 0){
  298. pre = parent.lastChild();
  299. pre.setNextSibling(node);
  300. }
  301. parent.children.push(node);
  302. }
  303. node.setParent(parent);
  304. node.setNextSibling(nextSibling);
  305. if (node.row) {
  306. if (pre) {
  307. _view._moveRowDom(node, pre.deepestRow());
  308. } else if (parent.id() !== this.setting.tree.nullId) {
  309. _view._moveRowDom(node, parent.row);
  310. } else if (nextSibling) {
  311. _view._moveRowDomBefore(node, nextSibling.row);
  312. }
  313. }
  314. if (node.parent.row) {
  315. this.refreshNodesDom([node.parent], false);
  316. }
  317. }
  318. } else {
  319. this.e.throw('Error: information of moving node has mistake.');
  320. }
  321. };
  322. Tree.prototype.selected = (function () {
  323. var selectNode = null;
  324. var select = function (node) {
  325. if (arguments.length === 0) {
  326. return selectNode;
  327. } else {
  328. if (node) {
  329. if (selectNode && selectNode.row) {
  330. selectNode.row.removeClass('table-active');
  331. }
  332. selectNode = node;
  333. node.row.addClass('table-active');
  334. }
  335. }
  336. }
  337. return select;
  338. })();
  339. return Tree;
  340. })();
  341. _data = {
  342. clone: function (obj) {
  343. if (obj === null) return null;
  344. var o = _data.isArray(obj) ? [] : {};
  345. for (var i in obj) {
  346. o[i] = (obj[i] instanceof Date) ? new Date(obj[i].getTime()) : (typeof obj[i] === "object" ? _data.clone(obj[i]) : obj[i]);
  347. }
  348. return o;
  349. },
  350. isArray: function (arr) {
  351. return Object.prototype.toString.apply(arr) === "[object Array]";
  352. }
  353. },
  354. _event = {
  355. _selectProj: function (node) {
  356. if (node.setting.viewEvent.beforeSelect) {
  357. node.setting.viewEvent.beforeSelect(node.tree.selected());
  358. }
  359. node.tree.selected(node);
  360. if (node.setting.viewEvent.onSelectNode) {
  361. node.setting.viewEvent.onSelectNode(node);
  362. }
  363. }
  364. }
  365. _view = {
  366. _makeTableHeadCell: function (obj, col){
  367. var th;
  368. th = $('<th>');
  369. th.attr('width', col.width);
  370. th.text(col.head);
  371. obj.append(th);
  372. },
  373. _makeTableHead: function (obj, setting){
  374. var thead, tr, i;
  375. thead = $('<thead>');
  376. tr = $('<tr>');
  377. for (i = 0; i < setting.columns.length; i++){
  378. _view._makeTableHeadCell(tr, setting.columns[i]);
  379. }
  380. thead.append(tr);
  381. obj.append(thead);
  382. return thead;
  383. },
  384. _makeTableBody: function (obj){
  385. var tbody;
  386. tbody = $('<tbody>');
  387. obj.append(tbody);
  388. return tbody;
  389. },
  390. _makeTableRowCell: function (obj, node, columns, index) {
  391. var html = [], td, i, url;
  392. html.push('<td ');
  393. if (index === node.setting.tree.btnColumn){
  394. html.push('class=', 'in-', node.depth().toString(), '>');
  395. if (node.children.length !== 0) {
  396. html.push('<a href="#" class="tree-open" title="收起"><i class="fa fa-minus-square-o mr-1"></i></a>');
  397. } else {
  398. html.push('<a href="#" class="tree-open" title="收起"><i></i></a>');
  399. }
  400. } else {
  401. html.push('>');
  402. }
  403. if (columns.event.getIcon) {
  404. columns.event.getIcon(html, node);
  405. }
  406. if (columns.name !== '') {
  407. if (columns.event.getText) {
  408. columns.event.getText(html, node, node.data[columns.data]);
  409. } else {
  410. html.push(node.data[columns.name]);
  411. }
  412. }
  413. html.push('</td>');
  414. td = $(html.join(''));
  415. if (index === node.setting.tree.btnColumn) {
  416. node.expandBtn = $('.tree-open>.fa', td);
  417. node.expandBtn.bind('click', {node: node}, function(e){
  418. e.data.node.expand(!e.data.node.expanded);
  419. });
  420. node.icon = $('.tree-icon', td);
  421. }
  422. if (columns.event.tdBindEvent) {
  423. columns.event.tdBindEvent(td, node);
  424. }
  425. obj.append(td);
  426. return td;
  427. },
  428. _makeTableRowCells: function (obj, node) {
  429. for (i = 0; i < node.setting.columns.length; i++){
  430. _view._makeTableRowCell(obj, node, node.setting.columns[i], i);
  431. }
  432. },
  433. _makeRowDom: function (obj, node) {
  434. var tr, i;
  435. tr = $('<tr>');
  436. obj.append(tr);
  437. tr.attr('id', node.domId());
  438. node.row = tr;
  439. _view._makeTableRowCells(tr, node);
  440. tr.click(function(){
  441. _event._selectProj(node);
  442. });
  443. /*$(tr).draggable({
  444. helper: "clone",
  445. opacity: .75,
  446. refreshPositions: true, // Performance?
  447. revert: "invalid",
  448. revertDuration: 300,
  449. scroll: true
  450. });
  451. tr.droppable({
  452. drop: function (e, ui) {
  453. var target = node.tree.findNodeByDomId($(this).attr('id')),
  454. drag = node.tree.findNodeByDomId($(ui).attr('id'));
  455. tree.move(drag, target);
  456. }
  457. }); */
  458. return tr;
  459. },
  460. _moveRowDomBefore: function (node, destination) {
  461. var moveNodesRowDomBefore = function (nodes) {
  462. nodes.forEach(function (node) {
  463. node.row.insertBefore(destination);
  464. moveNodesRowDomBefore(node.children);
  465. });
  466. }
  467. moveNodesRowDomBefore([node]);
  468. _view._refreshNodesLevelCss([node]);
  469. },
  470. _moveRowDom: function (node, destination) {
  471. var pre = destination,
  472. moveNodeRowDom = function (node) {
  473. node.row.insertAfter(pre);
  474. pre = node.row;
  475. moveChildrenRowDom(node);
  476. },
  477. moveChildrenRowDom = function(node){
  478. var i;
  479. for (i = 0; i < node.children.length; i++){
  480. moveNodeRowDom(node.children[i]);
  481. }
  482. };
  483. moveNodeRowDom(node);
  484. _view._refreshNodesLevelCss([node]);
  485. },
  486. _refreshTreeBtn: function (node) {
  487. var i;
  488. if (node.children.length === 0) {
  489. node.expandBtn.hide();
  490. } else {
  491. node.expandBtn.show();
  492. if (node.expanded) {
  493. node.expandBtn.removeClass('fa-plus-square-o');
  494. node.expandBtn.addClass('fa-minus-square-o');
  495. } else {
  496. node.expandBtn.removeClass('fa-minus-square-o');
  497. node.expandBtn.addClass('fa-plus-square-o');
  498. }
  499. }
  500. },
  501. _refreshNodesLevelCss: function (nodes) {
  502. nodes.forEach(function(node){
  503. var td = $('td:eq(' + node.setting.tree.btnColumn.toString() + ')', node.row);
  504. td.attr('class', 'in-'+ node.depth().toString());
  505. _view._refreshNodesLevelCss(node.children);
  506. })
  507. },
  508. _hideNodes: function (nodes) {
  509. var i, node;
  510. for (i = 0; i < nodes.length; i++){
  511. node = nodes[i];
  512. node.row.hide();
  513. _view._hideNodes(node.children);
  514. }
  515. },
  516. _showNodes: function (nodes) {
  517. var i, node;
  518. for (i = 0; i < nodes.length; i++){
  519. node = nodes[i];
  520. node.row.show();
  521. if (node.expanded) {
  522. _view._showNodes(node.children);
  523. }
  524. }
  525. },
  526. _removeNodesRowDom: function (nodes) {
  527. var i, node;
  528. for (i = 0; i < nodes.length; i++){
  529. node = nodes[i];
  530. _view._removeNodesRowDom(node.children);
  531. if (node.row) {
  532. node.row.remove();
  533. }
  534. }
  535. }
  536. };
  537. $.fn.treeTable = {
  538. init: function(obj, setting, arrData){
  539. var _treeSetting, _tree;
  540. _treeSetting = _data.clone(_setting);
  541. $.extend(true, _treeSetting, setting);
  542. var _tree = new Tree(obj, _treeSetting);
  543. _tree.loadData(arrData);
  544. _tree.refreshTreeDom();
  545. return _tree;
  546. }
  547. }
  548. })(jQuery);