tree_table.js 16 KB

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