lineage_query.html 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  1. {% extends "layout.html" %}
  2. {% block title %}世系查询 - 家谱管理系统{% endblock %}
  3. {% block extra_css %}
  4. <style>
  5. .empty-state {
  6. display: flex;
  7. flex-direction: column;
  8. align-items: center;
  9. justify-content: center;
  10. height: 60vh;
  11. color: rgba(255,255,255,0.5);
  12. }
  13. .tree-container {
  14. padding: 20px;
  15. min-height: 60vh;
  16. background: #1a1a2e;
  17. border-radius: 12px;
  18. border: 1px solid rgba(255,215,0,0.2);
  19. }
  20. /* Tree node styles */
  21. .tree-wrapper {
  22. display: flex;
  23. flex-direction: column;
  24. align-items: center;
  25. padding: 20px 0;
  26. }
  27. .tree-node {
  28. background: linear-gradient(135deg, #2d3436, #1e272e);
  29. border: 2px solid #4a69bd;
  30. border-radius: 8px;
  31. padding: 12px 24px;
  32. margin: 10px 0;
  33. text-align: center;
  34. min-width: 160px;
  35. transition: all 0.3s;
  36. cursor: pointer;
  37. box-shadow: 0 4px 15px rgba(0,0,0,0.3);
  38. }
  39. .tree-node:hover {
  40. background: linear-gradient(135deg, #4a69bd, #2d3436);
  41. transform: scale(1.05);
  42. box-shadow: 0 6px 20px rgba(74, 105, 189, 0.4);
  43. }
  44. .tree-node.center {
  45. background: linear-gradient(135deg, #ffd700, #ff8c00);
  46. border-color: #ffd700;
  47. box-shadow: 0 0 30px rgba(255,215,0,0.5);
  48. }
  49. .tree-node.center .node-name {
  50. color: #1a1a2e !important;
  51. }
  52. .tree-node.center .node-name.simplified {
  53. color: rgba(26,26,46,0.8) !important;
  54. }
  55. .tree-node.center .node-info {
  56. color: rgba(26,26,46,0.8) !important;
  57. }
  58. .tree-node.direct-ancestor {
  59. background: linear-gradient(135deg, #4a90d9, #2d5a87);
  60. border-color: #4a90d9;
  61. box-shadow: 0 0 20px rgba(74, 144, 217, 0.4);
  62. }
  63. .tree-node.direct-ancestor .node-name {
  64. color: #fff !important;
  65. }
  66. .tree-node.direct-ancestor .node-name.simplified {
  67. color: rgba(255,255,255,0.8) !important;
  68. }
  69. .tree-node.direct-ancestor .node-info {
  70. color: rgba(255,255,255,0.9) !important;
  71. }
  72. .tree-node.clickable {
  73. cursor: pointer;
  74. transition: transform 0.2s, box-shadow 0.2s;
  75. }
  76. .tree-node.clickable:hover {
  77. transform: translateY(-3px);
  78. box-shadow: 0 8px 25px rgba(255, 255, 255, 0.15);
  79. }
  80. .node-name {
  81. font-size: 18px;
  82. font-weight: 700;
  83. color: #fff;
  84. margin-bottom: 4px;
  85. }
  86. .node-name.simplified {
  87. font-size: 13px;
  88. color: rgba(255,255,255,0.7);
  89. }
  90. .node-info {
  91. font-size: 12px;
  92. color: rgba(255,255,255,0.8);
  93. font-weight: 500;
  94. }
  95. /* Connection lines */
  96. .connection-line {
  97. width: 4px;
  98. height: 40px;
  99. background: linear-gradient(to bottom, #4a90d9, #2d3436);
  100. margin: 0 auto;
  101. border-radius: 2px;
  102. }
  103. .connection-line.vertical-line {
  104. width: 4px;
  105. height: 40px;
  106. background: linear-gradient(to bottom, #4a90d9, #2d3436);
  107. margin: 0 auto;
  108. border-radius: 2px;
  109. }
  110. .connection-line.horizontal {
  111. width: 60px;
  112. height: 4px;
  113. margin: 8px auto;
  114. background: linear-gradient(to right, #4a69bd, #2d3436);
  115. }
  116. .connection-line.horizontal.main-line {
  117. width: 60px;
  118. height: 4px;
  119. margin: 8px auto;
  120. background: linear-gradient(to right, #4a90d9, #4a90d9);
  121. }
  122. /* Children container */
  123. .children-container {
  124. display: flex;
  125. flex-wrap: nowrap;
  126. justify-content: flex-start;
  127. gap: 30px;
  128. margin-top: 20px;
  129. align-items: flex-start;
  130. min-width: max-content;
  131. }
  132. .child-group {
  133. display: flex;
  134. flex-direction: column;
  135. align-items: center;
  136. flex-shrink: 0;
  137. }
  138. .child-group.direct-line {
  139. display: flex;
  140. flex-direction: column;
  141. align-items: center;
  142. flex-shrink: 0;
  143. position: relative;
  144. }
  145. /* Expand/Collapse button */
  146. .expand-btn {
  147. background: linear-gradient(135deg, #4a69bd, #2d3436);
  148. border: 2px solid #4a69bd;
  149. border-radius: 50%;
  150. width: 36px;
  151. height: 36px;
  152. color: #fff;
  153. font-size: 18px;
  154. font-weight: bold;
  155. cursor: pointer;
  156. display: flex;
  157. align-items: center;
  158. justify-content: center;
  159. margin: 15px auto;
  160. transition: all 0.3s;
  161. box-shadow: 0 2px 10px rgba(0,0,0,0.3);
  162. }
  163. .expand-btn:hover {
  164. background: linear-gradient(135deg, #ffd700, #ff8c00);
  165. border-color: #ffd700;
  166. transform: rotate(90deg);
  167. box-shadow: 0 4px 15px rgba(255,215,0,0.4);
  168. }
  169. /* Generation label */
  170. .generation-label {
  171. color: #ffd700;
  172. font-size: 16px;
  173. font-weight: 700;
  174. margin-bottom: 20px;
  175. text-align: center;
  176. padding: 10px 20px;
  177. background: rgba(255,215,0,0.1);
  178. border: 1px solid rgba(255,215,0,0.3);
  179. border-radius: 25px;
  180. display: inline-block;
  181. }
  182. /* Search box */
  183. .search-box {
  184. max-width: 350px;
  185. }
  186. /* Section styling */
  187. .ancestors-section, .children-section {
  188. margin: 30px 0;
  189. }
  190. /* Header styling */
  191. .page-header {
  192. color: #ffd700;
  193. font-size: 24px;
  194. font-weight: 700;
  195. text-shadow: 0 0 10px rgba(255,215,0,0.3);
  196. }
  197. /* Tree container with scrollable area */
  198. .tree-container {
  199. background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
  200. border-radius: 16px;
  201. padding: 30px;
  202. overflow: auto;
  203. position: relative;
  204. box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  205. width: calc(100% + 2px);
  206. margin: 0 -1px;
  207. }
  208. .tree-container::-webkit-scrollbar {
  209. width: 8px;
  210. height: 8px;
  211. }
  212. .tree-container::-webkit-scrollbar-track {
  213. background: rgba(255, 255, 255, 0.1);
  214. border-radius: 4px;
  215. }
  216. .tree-container::-webkit-scrollbar-thumb {
  217. background: rgba(255, 215, 0, 0.5);
  218. border-radius: 4px;
  219. }
  220. .tree-container::-webkit-scrollbar-thumb:hover {
  221. background: rgba(255, 215, 0, 0.7);
  222. }
  223. /* Tree wrapper for horizontal scrolling */
  224. .tree-wrapper {
  225. width: 100%;
  226. overflow-x: auto;
  227. padding-bottom: 10px;
  228. scrollbar-width: thin;
  229. scrollbar-color: rgba(255,215,0,0.5) rgba(255,255,255,0.1);
  230. }
  231. .tree-wrapper::-webkit-scrollbar {
  232. width: 8px;
  233. height: 8px;
  234. }
  235. .tree-wrapper::-webkit-scrollbar-track {
  236. background: rgba(255, 255, 255, 0.1);
  237. border-radius: 4px;
  238. }
  239. .tree-wrapper::-webkit-scrollbar-thumb {
  240. background: rgba(255, 215, 0, 0.5);
  241. border-radius: 4px;
  242. }
  243. /* Generation row wrapper */
  244. .generation-row {
  245. display: flex;
  246. justify-content: center;
  247. width: 100%;
  248. overflow-x: auto;
  249. }
  250. /* Adoption styles */
  251. .tree-node.adopted-out {
  252. border: 2px dashed #ff6b6b !important;
  253. background: rgba(255, 107, 107, 0.1) !important;
  254. }
  255. .tree-node.adopted-in {
  256. border: 2px solid #4ecdc4 !important;
  257. background: rgba(78, 205, 196, 0.1) !important;
  258. }
  259. .adoption-label {
  260. font-size: 10px;
  261. color: #ff6b6b;
  262. font-weight: bold;
  263. margin-top: 4px;
  264. text-align: center;
  265. }
  266. </style>
  267. {% endblock %}
  268. {% block content %}
  269. <div class="container-fluid">
  270. <!-- Header -->
  271. <div class="row mb-6">
  272. <div class="col-md-12">
  273. <div class="d-flex justify-content-between align-items-center">
  274. <h2 class="page-header">世系查询</h2>
  275. <div class="search-box">
  276. <div class="input-group">
  277. <input type="text" id="searchInput" class="form-control" placeholder="搜索成员姓名(支持简繁)" />
  278. <button class="btn btn-primary" onclick="searchMember()">
  279. <i class="bi bi-search"></i>
  280. </button>
  281. </div>
  282. </div>
  283. </div>
  284. </div>
  285. </div>
  286. <!-- Main Content -->
  287. <div class="row">
  288. <div class="col-md-12">
  289. <div id="treeContent">
  290. <!-- Empty State -->
  291. <div class="empty-state" id="emptyState">
  292. <i class="bi bi-search text-6xl mb-4"></i>
  293. <p class="text-lg">请在右上角搜索框中输入成员姓名</p>
  294. <p class="text-sm mt-2">支持简体和繁体姓名搜索</p>
  295. </div>
  296. <!-- Tree View -->
  297. <div id="treeView" style="display: none;" class="tree-container">
  298. <!-- Ancestors Section -->
  299. <div class="ancestors-section" id="ancestorsTree"></div>
  300. <!-- Siblings and Center Person Section -->
  301. <div class="siblings-section" id="siblingsTree"></div>
  302. <!-- Children Section -->
  303. <div class="children-section" id="childrenTree"></div>
  304. </div>
  305. </div>
  306. </div>
  307. </div>
  308. </div>
  309. <script>
  310. // Search member
  311. async function searchMember() {
  312. const keyword = document.getElementById('searchInput').value.trim();
  313. if (!keyword) {
  314. alert('请输入搜索关键词');
  315. return;
  316. }
  317. // Show loading state
  318. const searchBtn = document.querySelector('.search-box button');
  319. const originalBtnHtml = searchBtn.innerHTML;
  320. searchBtn.disabled = true;
  321. searchBtn.innerHTML = '<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>';
  322. try {
  323. const response = await fetch('/manager/api/search_member', {
  324. method: 'POST',
  325. headers: { 'Content-Type': 'application/json' },
  326. body: JSON.stringify({ keyword: keyword }),
  327. credentials: 'include'
  328. });
  329. const result = await response.json();
  330. if (result.success && result.members) {
  331. if (result.members.length === 1) {
  332. // Only one match, load directly
  333. document.getElementById('emptyState').style.display = 'none';
  334. document.getElementById('treeView').style.display = 'block';
  335. await loadLineage(result.members[0].id);
  336. } else {
  337. // Multiple matches, show selection dialog
  338. showMemberSelection(result.members);
  339. }
  340. } else {
  341. alert('未找到匹配的成员');
  342. }
  343. } finally {
  344. // Restore button
  345. searchBtn.disabled = false;
  346. searchBtn.innerHTML = originalBtnHtml;
  347. }
  348. }
  349. // Show member selection dialog
  350. function showMemberSelection(members) {
  351. // Remove existing dialog
  352. const existingDialog = document.getElementById('memberSelectionDialog');
  353. if (existingDialog) {
  354. existingDialog.remove();
  355. }
  356. // Store members in sessionStorage
  357. sessionStorage.setItem('searchMembers', JSON.stringify(members));
  358. // Create dialog
  359. const dialog = document.createElement('div');
  360. dialog.id = 'memberSelectionDialog';
  361. dialog.style.cssText = `
  362. position: fixed;
  363. top: 50%;
  364. left: 50%;
  365. transform: translate(-50%, -50%);
  366. background: #1a1a2e;
  367. border: 2px solid #4a69bd;
  368. border-radius: 12px;
  369. padding: 24px;
  370. z-index: 10000;
  371. min-width: 400px;
  372. box-shadow: 0 10px 40px rgba(0,0,0,0.5);
  373. `;
  374. // Dialog content
  375. dialog.innerHTML = `
  376. <h3 style="color: #ffd700; margin-bottom: 16px; text-align: center;">找到多个匹配成员,请输入编号选择:</h3>
  377. <div style="margin-bottom: 16px; max-height: 200px; overflow-y: auto;">
  378. ${members.map((member, index) => `
  379. <div style="padding: 10px; border-bottom: 1px solid rgba(255,255,255,0.1);">
  380. <span style="color: #ffd700; margin-right: 10px;">${index + 1}.</span>
  381. <span style="color: #fff; font-weight: 600;">${member.name}</span>
  382. ${member.simplified_name && member.simplified_name !== member.name ?
  383. `<span style="color: rgba(255,255,255,0.7);">(${member.simplified_name})</span>` : ''}
  384. </div>
  385. `).join('')}
  386. </div>
  387. <input type="text" id="selectionInput"
  388. placeholder="请输入编号选择(1-${members.length})"
  389. style="width: 100%; padding: 10px; margin-bottom: 16px;
  390. background: #2d3436; border: 1px solid #4a69bd;
  391. border-radius: 8px; color: #fff; text-align: center;
  392. font-size: 16px;" />
  393. <div style="display: flex; justify-content: center; gap: 16px;">
  394. <button onclick="selectMember()"
  395. style="padding: 10px 30px; background: linear-gradient(135deg, #4a69bd, #2d3436);
  396. border: 2px solid #4a69bd; border-radius: 8px; color: #fff;
  397. cursor: pointer; font-weight: 600;">确定</button>
  398. <button onclick="closeSelectionDialog()"
  399. style="padding: 10px 30px; background: #2d3436;
  400. border: 2px solid #666; border-radius: 8px; color: #aaa;
  401. cursor: pointer;">取消</button>
  402. </div>
  403. `;
  404. // Add overlay
  405. const overlay = document.createElement('div');
  406. overlay.id = 'dialogOverlay';
  407. overlay.style.cssText = `
  408. position: fixed;
  409. top: 0;
  410. left: 0;
  411. width: 100%;
  412. height: 100%;
  413. background: rgba(0,0,0,0.7);
  414. z-index: 9999;
  415. `;
  416. overlay.onclick = closeSelectionDialog;
  417. document.body.appendChild(overlay);
  418. document.body.appendChild(dialog);
  419. // Focus input
  420. document.getElementById('selectionInput').focus();
  421. // Enter key
  422. document.getElementById('selectionInput').addEventListener('keypress', function(e) {
  423. if (e.key === 'Enter') {
  424. selectMember();
  425. }
  426. });
  427. }
  428. // Close selection dialog
  429. function closeSelectionDialog() {
  430. document.getElementById('memberSelectionDialog')?.remove();
  431. document.getElementById('dialogOverlay')?.remove();
  432. }
  433. // Select member
  434. function selectMember() {
  435. const input = document.getElementById('selectionInput');
  436. const index = parseInt(input.value) - 1;
  437. const membersStr = sessionStorage.getItem('searchMembers');
  438. if (!membersStr) {
  439. alert('数据错误,请重新搜索');
  440. closeSelectionDialog();
  441. return;
  442. }
  443. const members = JSON.parse(membersStr);
  444. if (index >= 0 && index < members.length) {
  445. // Load lineage for selected member
  446. document.getElementById('emptyState').style.display = 'none';
  447. document.getElementById('treeView').style.display = 'block';
  448. loadLineage(members[index].id);
  449. // Close dialog
  450. closeSelectionDialog();
  451. } else {
  452. alert(`请输入有效的编号(1-${members.length})`);
  453. }
  454. }
  455. // Load lineage data
  456. async function loadLineage(memberId) {
  457. // Show loading state - preserve container elements
  458. document.getElementById('ancestorsTree').innerHTML = `
  459. <div style="display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 40px;">
  460. <div class="spinner-border text-yellow-400" style="width: 3rem; height: 3rem;" role="status">
  461. <span class="visually-hidden">Loading...</span>
  462. </div>
  463. <p class="mt-4 text-white text-lg">正在加载祖先数据...</p>
  464. </div>
  465. `;
  466. document.getElementById('siblingsTree').innerHTML = `
  467. <div style="display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 40px;">
  468. <div class="spinner-border text-yellow-400" style="width: 3rem; height: 3rem;" role="status">
  469. <span class="visually-hidden">Loading...</span>
  470. </div>
  471. <p class="mt-4 text-white text-lg">正在加载同辈数据...</p>
  472. </div>
  473. `;
  474. document.getElementById('childrenTree').innerHTML = `
  475. <div style="display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 40px;">
  476. <div class="spinner-border text-yellow-400" style="width: 3rem; height: 3rem;" role="status">
  477. <span class="visually-hidden">Loading...</span>
  478. </div>
  479. <p class="mt-4 text-white text-lg">正在加载后代数据...</p>
  480. </div>
  481. `;
  482. try {
  483. const response = await fetch(`/manager/api/get_lineage/${memberId}`, {
  484. credentials: 'include'
  485. });
  486. const result = await response.json();
  487. if (result.success) {
  488. console.log('Lineage data received:', result.data);
  489. // Use requestAnimationFrame for smoother rendering
  490. requestAnimationFrame(() => {
  491. renderLineage(result.data);
  492. });
  493. } else {
  494. console.error('API error:', result.message);
  495. document.getElementById('ancestorsTree').innerHTML = '';
  496. document.getElementById('siblingsTree').innerHTML = `
  497. <div style="display: flex; flex-direction: column; align-items: center; justify-content: center; min-height: 200px;">
  498. <i class="bi bi-x-circle text-red-500 text-6xl mb-4"></i>
  499. <p class="text-white text-lg">加载失败</p>
  500. <p class="text-gray-400 text-sm">${result.message || '服务器错误,请稍后重试'}</p>
  501. </div>
  502. `;
  503. document.getElementById('childrenTree').innerHTML = '';
  504. }
  505. } catch (error) {
  506. console.error('Fetch error:', error);
  507. document.getElementById('ancestorsTree').innerHTML = '';
  508. document.getElementById('siblingsTree').innerHTML = `
  509. <div style="display: flex; flex-direction: column; align-items: center; justify-content: center; min-height: 200px;">
  510. <i class="bi bi-x-circle text-red-500 text-6xl mb-4"></i>
  511. <p class="text-white text-lg">加载失败</p>
  512. <p class="text-gray-400 text-sm">网络错误: ${error.message}</p>
  513. </div>
  514. `;
  515. document.getElementById('childrenTree').innerHTML = '';
  516. }
  517. }
  518. // Render lineage tree
  519. function renderLineage(data) {
  520. console.log('Rendering lineage:', data);
  521. try {
  522. const generationsHtml = renderGenerations(data.generations);
  523. console.log('Generations HTML generated:', generationsHtml.length);
  524. document.getElementById('ancestorsTree').innerHTML = generationsHtml;
  525. } catch (e) {
  526. console.error('Error rendering generations:', e);
  527. }
  528. try {
  529. const siblingsWithCenterHtml = renderSiblingsWithCenter(data.center, data.siblings);
  530. console.log('Siblings HTML generated:', siblingsWithCenterHtml.length);
  531. document.getElementById('siblingsTree').innerHTML = siblingsWithCenterHtml;
  532. } catch (e) {
  533. console.error('Error rendering siblings:', e);
  534. }
  535. try {
  536. const childrenHtml = renderChildrenTree(data.children);
  537. console.log('Children HTML generated:', childrenHtml.length);
  538. document.getElementById('childrenTree').innerHTML = childrenHtml;
  539. } catch (e) {
  540. console.error('Error rendering children:', e);
  541. }
  542. }
  543. // Render generations with ancestors and their siblings
  544. function renderGenerations(generations) {
  545. if (!generations || generations.length === 0) return '';
  546. let html = '<div class="text-center mb-6"><span class="generation-label">祖先谱系</span></div>';
  547. html += '<div class="tree-wrapper">';
  548. const reversedGenerations = [...generations].reverse();
  549. reversedGenerations.forEach((gen, index) => {
  550. if (index > 0) {
  551. html += '<div class="connection-line vertical-line"></div>';
  552. }
  553. const leftSiblings = gen.siblings.slice(0, Math.floor(gen.siblings.length / 2));
  554. const rightSiblings = gen.siblings.slice(Math.floor(gen.siblings.length / 2));
  555. html += `
  556. <div class="generation-row">
  557. <div class="children-container">
  558. ${leftSiblings.map(sibling => `
  559. <div class="child-group">
  560. <div class="connection-line horizontal"></div>
  561. ${renderTreeNode(sibling, false, false)}
  562. ${sibling.has_children ? `
  563. <button class="expand-btn" onclick="toggleChildren(this, ${sibling.id})">+</button>
  564. <div class="children-container" style="display: none;" data-parent-id="${sibling.id}">
  565. </div>
  566. ` : ''}
  567. </div>
  568. `).join('')}
  569. <div class="child-group direct-line">
  570. <div class="connection-line horizontal main-line"></div>
  571. ${renderTreeNode(gen.ancestor, false, true)}
  572. ${gen.ancestor.show_expand ? `
  573. <button class="expand-btn" onclick="toggleChildren(this, ${gen.ancestor.id})">+</button>
  574. <div class="children-container" style="display: none;" data-parent-id="${gen.ancestor.id}">
  575. </div>
  576. ` : ''}
  577. </div>
  578. ${rightSiblings.map(sibling => `
  579. <div class="child-group">
  580. <div class="connection-line horizontal"></div>
  581. ${renderTreeNode(sibling, false, false)}
  582. ${sibling.has_children ? `
  583. <button class="expand-btn" onclick="toggleChildren(this, ${sibling.id})">+</button>
  584. <div class="children-container" style="display: none;" data-parent-id="${sibling.id}">
  585. </div>
  586. ` : ''}
  587. </div>
  588. `).join('')}
  589. </div>
  590. </div>
  591. `;
  592. });
  593. html += '</div>';
  594. return html;
  595. }
  596. // Render center person
  597. function renderCenterPerson(person) {
  598. return `
  599. <div class="connection-line"></div>
  600. <div class="tree-node center">
  601. <div class="node-name">${person.name}</div>
  602. ${person.simplified_name && person.simplified_name !== person.name ? `<div class="node-name simplified">(${person.simplified_name})</div>` : ''}
  603. <div class="node-info">
  604. ${person.name_word ? `${person.name_word} · ` : ''}
  605. ${person.name_word_generation || ''}
  606. </div>
  607. </div>
  608. `;
  609. }
  610. // Render siblings with center person in the middle
  611. function renderSiblingsWithCenter(center, siblings) {
  612. const allSiblings = siblings || [];
  613. // Insert center person at the middle position
  614. const middleIndex = Math.floor(allSiblings.length / 2);
  615. const items = [];
  616. for (let i = 0; i < allSiblings.length; i++) {
  617. if (i === middleIndex) {
  618. items.push({ type: 'center', person: center });
  619. }
  620. items.push({ type: 'sibling', person: allSiblings[i] });
  621. }
  622. // If no siblings, just show center
  623. if (allSiblings.length === 0) {
  624. items.push({ type: 'center', person: center });
  625. }
  626. return `
  627. <div class="text-center mt-6 mb-4"><span class="generation-label">同辈兄弟姐妹</span></div>
  628. <div class="generation-row">
  629. <div class="children-container">
  630. ${items.map(item => `
  631. <div class="child-group ${item.type === 'center' ? 'center-child' : ''}">
  632. <div class="connection-line horizontal"></div>
  633. ${renderTreeNode(item.person, item.type === 'center')}
  634. ${item.type !== 'center' && item.person.has_children ? `
  635. <button class="expand-btn" onclick="toggleChildren(this, ${item.person.id})">+</button>
  636. <div class="children-container" style="display: none;" data-parent-id="${item.person.id}">
  637. </div>
  638. ` : ''}
  639. </div>
  640. `).join('')}
  641. </div>
  642. </div>
  643. `;
  644. }
  645. // Render children tree
  646. function renderChildrenTree(children) {
  647. if (!children || children.length === 0) return '';
  648. return `
  649. <div class="text-center mt-6 mb-4"><span class="generation-label">后代谱系</span></div>
  650. <div class="generation-row">
  651. ${renderChildrenRecursive(children)}
  652. </div>
  653. `;
  654. }
  655. // Render children recursively
  656. function renderChildrenRecursive(children, level = 0) {
  657. if (!children || children.length === 0) return '';
  658. return `
  659. <div class="children-container">
  660. ${children.map(child => `
  661. <div class="child-group">
  662. <div class="connection-line"></div>
  663. ${renderTreeNode(child, false)}
  664. ${child.has_children ? `
  665. <button class="expand-btn" onclick="toggleChildren(this, ${child.id})">+</button>
  666. <div class="children-container" style="display: none;" data-parent-id="${child.id}">
  667. </div>
  668. ` : ''}
  669. </div>
  670. `).join('')}
  671. </div>
  672. `;
  673. }
  674. // Render tree node
  675. function renderTreeNode(person, isCenter = false, isDirectLine = false) {
  676. let className = 'clickable ';
  677. if (isCenter) className += 'center';
  678. else if (isDirectLine) className += 'direct-ancestor';
  679. else className = className.trim();
  680. // Add adoption styles
  681. if (person.sub_relation_type === 2) {
  682. className += ' adopted-out'; // 出继
  683. } else if (person.sub_relation_type === 3) {
  684. className += ' adopted-in'; // 入继
  685. }
  686. // 出继方显示"出继给xxx"
  687. const adoptionLabel = person.sub_relation_type === 2 && person.adoptive_parent_name
  688. ? `<div class="adoption-label">出继给 ${person.adoptive_parent_name}</div>`
  689. : (person.sub_relation_type === 2 ? '<div class="adoption-label">出继</div>' : '');
  690. return `
  691. <div class="tree-node ${className}" data-id="${person.id}" onclick="openPersonDetail(${person.id})">
  692. <div class="node-name">${person.name}</div>
  693. ${person.simplified_name && person.simplified_name !== person.name ? `<div class="node-name simplified">(${person.simplified_name})</div>` : ''}
  694. <div class="node-info">
  695. ${person.name_word ? `${person.name_word} · ` : ''}
  696. ${person.name_word_generation || ''}
  697. </div>
  698. ${adoptionLabel}
  699. </div>
  700. `;
  701. }
  702. // Open person detail in new tab
  703. function openPersonDetail(personId) {
  704. window.open(`/manager/member_detail/${personId}`, '_blank');
  705. }
  706. // Toggle children visibility with lazy loading
  707. async function toggleChildren(btn, parentId) {
  708. const container = btn.nextElementSibling;
  709. const isExpanded = container.style.display !== 'none';
  710. if (isExpanded) {
  711. container.style.display = 'none';
  712. btn.innerHTML = '+';
  713. } else {
  714. // Check if children are already loaded
  715. if (container.innerHTML.trim() === '') {
  716. // Load children lazily
  717. btn.innerHTML = '<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>';
  718. // Get already displayed descendant IDs to exclude
  719. const excludedIds = getExcludedDescendantIds(btn);
  720. const excludeParam = excludedIds.length > 0 ? `?exclude=${excludedIds.join(',')}` : '';
  721. console.log(`[ToggleChildren] Parent ID: ${parentId}, Excluded IDs: ${excludedIds}, URL: /manager/api/get_descendants/${parentId}${excludeParam}`);
  722. try {
  723. const response = await fetch(`/manager/api/get_descendants/${parentId}${excludeParam}`, {
  724. credentials: 'include'
  725. });
  726. const result = await response.json();
  727. if (result.success && result.children) {
  728. // Render children
  729. container.innerHTML = renderChildrenRecursive(result.children);
  730. }
  731. } catch (error) {
  732. console.error('Failed to load children:', error);
  733. } finally {
  734. btn.innerHTML = '−';
  735. }
  736. }
  737. container.style.display = 'flex';
  738. btn.innerHTML = '−';
  739. }
  740. }
  741. // Get descendant IDs that are already displayed in the tree (to avoid duplicates)
  742. function getExcludedDescendantIds(btn) {
  743. const excluded = new Set();
  744. // Helper function to extract IDs from tree nodes
  745. const extractIdsFromNodes = (container) => {
  746. if (!container) return;
  747. const nodes = container.querySelectorAll('.tree-node');
  748. nodes.forEach(node => {
  749. const id = node.getAttribute('data-id');
  750. if (id && !isNaN(parseInt(id))) {
  751. excluded.add(parseInt(id));
  752. }
  753. });
  754. };
  755. // Get IDs from ancestors tree
  756. const ancestorsTree = document.getElementById('ancestorsTree');
  757. extractIdsFromNodes(ancestorsTree);
  758. // Get IDs from siblings tree (center person and siblings)
  759. const siblingsTree = document.getElementById('siblingsTree');
  760. extractIdsFromNodes(siblingsTree);
  761. // Get IDs from children tree
  762. const childrenTree = document.getElementById('childrenTree');
  763. extractIdsFromNodes(childrenTree);
  764. console.log('Excluded IDs:', Array.from(excluded));
  765. return Array.from(excluded);
  766. }
  767. // Enter key search
  768. document.getElementById('searchInput').addEventListener('keypress', function(e) {
  769. if (e.key === 'Enter') {
  770. searchMember();
  771. }
  772. });
  773. </script>
  774. {% endblock %}