lineage_query.html 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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. </style>
  251. {% endblock %}
  252. {% block content %}
  253. <div class="container-fluid">
  254. <!-- Header -->
  255. <div class="row mb-6">
  256. <div class="col-md-12">
  257. <div class="d-flex justify-content-between align-items-center">
  258. <h2 class="page-header">世系查询</h2>
  259. <div class="search-box">
  260. <div class="input-group">
  261. <input type="text" id="searchInput" class="form-control" placeholder="搜索成员姓名(支持简繁)" />
  262. <button class="btn btn-primary" onclick="searchMember()">
  263. <i class="bi bi-search"></i>
  264. </button>
  265. </div>
  266. </div>
  267. </div>
  268. </div>
  269. </div>
  270. <!-- Main Content -->
  271. <div class="row">
  272. <div class="col-md-12">
  273. <div id="treeContent">
  274. <!-- Empty State -->
  275. <div class="empty-state" id="emptyState">
  276. <i class="bi bi-search text-6xl mb-4"></i>
  277. <p class="text-lg">请在右上角搜索框中输入成员姓名</p>
  278. <p class="text-sm mt-2">支持简体和繁体姓名搜索</p>
  279. </div>
  280. <!-- Tree View -->
  281. <div id="treeView" style="display: none;" class="tree-container">
  282. <!-- Ancestors Section -->
  283. <div class="ancestors-section" id="ancestorsTree"></div>
  284. <!-- Siblings and Center Person Section -->
  285. <div class="siblings-section" id="siblingsTree"></div>
  286. <!-- Children Section -->
  287. <div class="children-section" id="childrenTree"></div>
  288. </div>
  289. </div>
  290. </div>
  291. </div>
  292. </div>
  293. <script>
  294. // Search member
  295. async function searchMember() {
  296. const keyword = document.getElementById('searchInput').value.trim();
  297. if (!keyword) {
  298. alert('请输入搜索关键词');
  299. return;
  300. }
  301. // Show loading state
  302. const searchBtn = document.querySelector('.search-box button');
  303. const originalBtnHtml = searchBtn.innerHTML;
  304. searchBtn.disabled = true;
  305. searchBtn.innerHTML = '<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>';
  306. try {
  307. const response = await fetch('/manager/api/search_member', {
  308. method: 'POST',
  309. headers: { 'Content-Type': 'application/json' },
  310. body: JSON.stringify({ keyword: keyword }),
  311. credentials: 'include'
  312. });
  313. const result = await response.json();
  314. if (result.success && result.members) {
  315. if (result.members.length === 1) {
  316. // Only one match, load directly
  317. document.getElementById('emptyState').style.display = 'none';
  318. document.getElementById('treeView').style.display = 'block';
  319. await loadLineage(result.members[0].id);
  320. } else {
  321. // Multiple matches, show selection dialog
  322. showMemberSelection(result.members);
  323. }
  324. } else {
  325. alert('未找到匹配的成员');
  326. }
  327. } finally {
  328. // Restore button
  329. searchBtn.disabled = false;
  330. searchBtn.innerHTML = originalBtnHtml;
  331. }
  332. }
  333. // Show member selection dialog
  334. function showMemberSelection(members) {
  335. // Remove existing dialog
  336. const existingDialog = document.getElementById('memberSelectionDialog');
  337. if (existingDialog) {
  338. existingDialog.remove();
  339. }
  340. // Store members in sessionStorage
  341. sessionStorage.setItem('searchMembers', JSON.stringify(members));
  342. // Create dialog
  343. const dialog = document.createElement('div');
  344. dialog.id = 'memberSelectionDialog';
  345. dialog.style.cssText = `
  346. position: fixed;
  347. top: 50%;
  348. left: 50%;
  349. transform: translate(-50%, -50%);
  350. background: #1a1a2e;
  351. border: 2px solid #4a69bd;
  352. border-radius: 12px;
  353. padding: 24px;
  354. z-index: 10000;
  355. min-width: 400px;
  356. box-shadow: 0 10px 40px rgba(0,0,0,0.5);
  357. `;
  358. // Dialog content
  359. dialog.innerHTML = `
  360. <h3 style="color: #ffd700; margin-bottom: 16px; text-align: center;">找到多个匹配成员,请输入编号选择:</h3>
  361. <div style="margin-bottom: 16px; max-height: 200px; overflow-y: auto;">
  362. ${members.map((member, index) => `
  363. <div style="padding: 10px; border-bottom: 1px solid rgba(255,255,255,0.1);">
  364. <span style="color: #ffd700; margin-right: 10px;">${index + 1}.</span>
  365. <span style="color: #fff; font-weight: 600;">${member.name}</span>
  366. ${member.simplified_name && member.simplified_name !== member.name ?
  367. `<span style="color: rgba(255,255,255,0.7);">(${member.simplified_name})</span>` : ''}
  368. </div>
  369. `).join('')}
  370. </div>
  371. <input type="text" id="selectionInput"
  372. placeholder="请输入编号选择(1-${members.length})"
  373. style="width: 100%; padding: 10px; margin-bottom: 16px;
  374. background: #2d3436; border: 1px solid #4a69bd;
  375. border-radius: 8px; color: #fff; text-align: center;
  376. font-size: 16px;" />
  377. <div style="display: flex; justify-content: center; gap: 16px;">
  378. <button onclick="selectMember()"
  379. style="padding: 10px 30px; background: linear-gradient(135deg, #4a69bd, #2d3436);
  380. border: 2px solid #4a69bd; border-radius: 8px; color: #fff;
  381. cursor: pointer; font-weight: 600;">确定</button>
  382. <button onclick="closeSelectionDialog()"
  383. style="padding: 10px 30px; background: #2d3436;
  384. border: 2px solid #666; border-radius: 8px; color: #aaa;
  385. cursor: pointer;">取消</button>
  386. </div>
  387. `;
  388. // Add overlay
  389. const overlay = document.createElement('div');
  390. overlay.id = 'dialogOverlay';
  391. overlay.style.cssText = `
  392. position: fixed;
  393. top: 0;
  394. left: 0;
  395. width: 100%;
  396. height: 100%;
  397. background: rgba(0,0,0,0.7);
  398. z-index: 9999;
  399. `;
  400. overlay.onclick = closeSelectionDialog;
  401. document.body.appendChild(overlay);
  402. document.body.appendChild(dialog);
  403. // Focus input
  404. document.getElementById('selectionInput').focus();
  405. // Enter key
  406. document.getElementById('selectionInput').addEventListener('keypress', function(e) {
  407. if (e.key === 'Enter') {
  408. selectMember();
  409. }
  410. });
  411. }
  412. // Close selection dialog
  413. function closeSelectionDialog() {
  414. document.getElementById('memberSelectionDialog')?.remove();
  415. document.getElementById('dialogOverlay')?.remove();
  416. }
  417. // Select member
  418. function selectMember() {
  419. const input = document.getElementById('selectionInput');
  420. const index = parseInt(input.value) - 1;
  421. const membersStr = sessionStorage.getItem('searchMembers');
  422. if (!membersStr) {
  423. alert('数据错误,请重新搜索');
  424. closeSelectionDialog();
  425. return;
  426. }
  427. const members = JSON.parse(membersStr);
  428. if (index >= 0 && index < members.length) {
  429. // Load lineage for selected member
  430. document.getElementById('emptyState').style.display = 'none';
  431. document.getElementById('treeView').style.display = 'block';
  432. loadLineage(members[index].id);
  433. // Close dialog
  434. closeSelectionDialog();
  435. } else {
  436. alert(`请输入有效的编号(1-${members.length})`);
  437. }
  438. }
  439. // Load lineage data
  440. async function loadLineage(memberId) {
  441. // Show loading state - preserve container elements
  442. document.getElementById('ancestorsTree').innerHTML = `
  443. <div style="display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 40px;">
  444. <div class="spinner-border text-yellow-400" style="width: 3rem; height: 3rem;" role="status">
  445. <span class="visually-hidden">Loading...</span>
  446. </div>
  447. <p class="mt-4 text-white text-lg">正在加载祖先数据...</p>
  448. </div>
  449. `;
  450. document.getElementById('siblingsTree').innerHTML = `
  451. <div style="display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 40px;">
  452. <div class="spinner-border text-yellow-400" style="width: 3rem; height: 3rem;" role="status">
  453. <span class="visually-hidden">Loading...</span>
  454. </div>
  455. <p class="mt-4 text-white text-lg">正在加载同辈数据...</p>
  456. </div>
  457. `;
  458. document.getElementById('childrenTree').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. try {
  467. const response = await fetch(`/manager/api/get_lineage/${memberId}`, {
  468. credentials: 'include'
  469. });
  470. const result = await response.json();
  471. if (result.success) {
  472. console.log('Lineage data received:', result.data);
  473. // Use requestAnimationFrame for smoother rendering
  474. requestAnimationFrame(() => {
  475. renderLineage(result.data);
  476. });
  477. } else {
  478. console.error('API error:', result.message);
  479. document.getElementById('ancestorsTree').innerHTML = '';
  480. document.getElementById('siblingsTree').innerHTML = `
  481. <div style="display: flex; flex-direction: column; align-items: center; justify-content: center; min-height: 200px;">
  482. <i class="bi bi-x-circle text-red-500 text-6xl mb-4"></i>
  483. <p class="text-white text-lg">加载失败</p>
  484. <p class="text-gray-400 text-sm">${result.message || '服务器错误,请稍后重试'}</p>
  485. </div>
  486. `;
  487. document.getElementById('childrenTree').innerHTML = '';
  488. }
  489. } catch (error) {
  490. console.error('Fetch error:', error);
  491. document.getElementById('ancestorsTree').innerHTML = '';
  492. document.getElementById('siblingsTree').innerHTML = `
  493. <div style="display: flex; flex-direction: column; align-items: center; justify-content: center; min-height: 200px;">
  494. <i class="bi bi-x-circle text-red-500 text-6xl mb-4"></i>
  495. <p class="text-white text-lg">加载失败</p>
  496. <p class="text-gray-400 text-sm">网络错误: ${error.message}</p>
  497. </div>
  498. `;
  499. document.getElementById('childrenTree').innerHTML = '';
  500. }
  501. }
  502. // Render lineage tree
  503. function renderLineage(data) {
  504. console.log('Rendering lineage:', data);
  505. try {
  506. const generationsHtml = renderGenerations(data.generations);
  507. console.log('Generations HTML generated:', generationsHtml.length);
  508. document.getElementById('ancestorsTree').innerHTML = generationsHtml;
  509. } catch (e) {
  510. console.error('Error rendering generations:', e);
  511. }
  512. try {
  513. const siblingsWithCenterHtml = renderSiblingsWithCenter(data.center, data.siblings);
  514. console.log('Siblings HTML generated:', siblingsWithCenterHtml.length);
  515. document.getElementById('siblingsTree').innerHTML = siblingsWithCenterHtml;
  516. } catch (e) {
  517. console.error('Error rendering siblings:', e);
  518. }
  519. try {
  520. const childrenHtml = renderChildrenTree(data.children);
  521. console.log('Children HTML generated:', childrenHtml.length);
  522. document.getElementById('childrenTree').innerHTML = childrenHtml;
  523. } catch (e) {
  524. console.error('Error rendering children:', e);
  525. }
  526. }
  527. // Render generations with ancestors and their siblings
  528. function renderGenerations(generations) {
  529. if (!generations || generations.length === 0) return '';
  530. let html = '<div class="text-center mb-6"><span class="generation-label">祖先谱系</span></div>';
  531. html += '<div class="tree-wrapper">';
  532. const reversedGenerations = [...generations].reverse();
  533. reversedGenerations.forEach((gen, index) => {
  534. if (index > 0) {
  535. html += '<div class="connection-line vertical-line"></div>';
  536. }
  537. const leftSiblings = gen.siblings.slice(0, Math.floor(gen.siblings.length / 2));
  538. const rightSiblings = gen.siblings.slice(Math.floor(gen.siblings.length / 2));
  539. html += `
  540. <div class="generation-row">
  541. <div class="children-container">
  542. ${leftSiblings.map(sibling => `
  543. <div class="child-group">
  544. <div class="connection-line horizontal"></div>
  545. ${renderTreeNode(sibling, false, false)}
  546. ${sibling.has_children ? `
  547. <button class="expand-btn" onclick="toggleChildren(this, ${sibling.id})">+</button>
  548. <div class="children-container" style="display: none;" data-parent-id="${sibling.id}">
  549. </div>
  550. ` : ''}
  551. </div>
  552. `).join('')}
  553. <div class="child-group direct-line">
  554. <div class="connection-line horizontal main-line"></div>
  555. ${renderTreeNode(gen.ancestor, false, true)}
  556. ${gen.ancestor.show_expand ? `
  557. <button class="expand-btn" onclick="toggleChildren(this, ${gen.ancestor.id})">+</button>
  558. <div class="children-container" style="display: none;" data-parent-id="${gen.ancestor.id}">
  559. </div>
  560. ` : ''}
  561. </div>
  562. ${rightSiblings.map(sibling => `
  563. <div class="child-group">
  564. <div class="connection-line horizontal"></div>
  565. ${renderTreeNode(sibling, false, false)}
  566. ${sibling.has_children ? `
  567. <button class="expand-btn" onclick="toggleChildren(this, ${sibling.id})">+</button>
  568. <div class="children-container" style="display: none;" data-parent-id="${sibling.id}">
  569. </div>
  570. ` : ''}
  571. </div>
  572. `).join('')}
  573. </div>
  574. </div>
  575. `;
  576. });
  577. html += '</div>';
  578. return html;
  579. }
  580. // Render center person
  581. function renderCenterPerson(person) {
  582. return `
  583. <div class="connection-line"></div>
  584. <div class="tree-node center">
  585. <div class="node-name">${person.name}</div>
  586. ${person.simplified_name && person.simplified_name !== person.name ? `<div class="node-name simplified">(${person.simplified_name})</div>` : ''}
  587. <div class="node-info">
  588. ${person.name_word ? `${person.name_word} · ` : ''}
  589. ${person.name_word_generation || ''}
  590. </div>
  591. </div>
  592. `;
  593. }
  594. // Render siblings with center person in the middle
  595. function renderSiblingsWithCenter(center, siblings) {
  596. const allSiblings = siblings || [];
  597. // Insert center person at the middle position
  598. const middleIndex = Math.floor(allSiblings.length / 2);
  599. const items = [];
  600. for (let i = 0; i < allSiblings.length; i++) {
  601. if (i === middleIndex) {
  602. items.push({ type: 'center', person: center });
  603. }
  604. items.push({ type: 'sibling', person: allSiblings[i] });
  605. }
  606. // If no siblings, just show center
  607. if (allSiblings.length === 0) {
  608. items.push({ type: 'center', person: center });
  609. }
  610. return `
  611. <div class="text-center mt-6 mb-4"><span class="generation-label">同辈兄弟姐妹</span></div>
  612. <div class="generation-row">
  613. <div class="children-container">
  614. ${items.map(item => `
  615. <div class="child-group ${item.type === 'center' ? 'center-child' : ''}">
  616. <div class="connection-line horizontal"></div>
  617. ${renderTreeNode(item.person, item.type === 'center')}
  618. ${item.type !== 'center' && item.person.has_children ? `
  619. <button class="expand-btn" onclick="toggleChildren(this, ${item.person.id})">+</button>
  620. <div class="children-container" style="display: none;" data-parent-id="${item.person.id}">
  621. </div>
  622. ` : ''}
  623. </div>
  624. `).join('')}
  625. </div>
  626. </div>
  627. `;
  628. }
  629. // Render children tree
  630. function renderChildrenTree(children) {
  631. if (!children || children.length === 0) return '';
  632. return `
  633. <div class="text-center mt-6 mb-4"><span class="generation-label">后代谱系</span></div>
  634. <div class="generation-row">
  635. ${renderChildrenRecursive(children)}
  636. </div>
  637. `;
  638. }
  639. // Render children recursively
  640. function renderChildrenRecursive(children, level = 0) {
  641. if (!children || children.length === 0) return '';
  642. return `
  643. <div class="children-container">
  644. ${children.map(child => `
  645. <div class="child-group">
  646. <div class="connection-line"></div>
  647. ${renderTreeNode(child, false)}
  648. ${child.has_children ? `
  649. <button class="expand-btn" onclick="toggleChildren(this, ${child.id})">+</button>
  650. <div class="children-container" style="display: none;" data-parent-id="${child.id}">
  651. </div>
  652. ` : ''}
  653. </div>
  654. `).join('')}
  655. </div>
  656. `;
  657. }
  658. // Render tree node
  659. function renderTreeNode(person, isCenter = false, isDirectLine = false) {
  660. let className = 'clickable ';
  661. if (isCenter) className += 'center';
  662. else if (isDirectLine) className += 'direct-ancestor';
  663. else className = className.trim();
  664. return `
  665. <div class="tree-node ${className}" data-id="${person.id}" onclick="openPersonDetail(${person.id})">
  666. <div class="node-name">${person.name}</div>
  667. ${person.simplified_name && person.simplified_name !== person.name ? `<div class="node-name simplified">(${person.simplified_name})</div>` : ''}
  668. <div class="node-info">
  669. ${person.name_word ? `${person.name_word} · ` : ''}
  670. ${person.name_word_generation || ''}
  671. </div>
  672. </div>
  673. `;
  674. }
  675. // Open person detail in new tab
  676. function openPersonDetail(personId) {
  677. window.open(`/manager/member_detail/${personId}`, '_blank');
  678. }
  679. // Toggle children visibility with lazy loading
  680. async function toggleChildren(btn, parentId) {
  681. const container = btn.nextElementSibling;
  682. const isExpanded = container.style.display !== 'none';
  683. if (isExpanded) {
  684. container.style.display = 'none';
  685. btn.innerHTML = '+';
  686. } else {
  687. // Check if children are already loaded
  688. if (container.innerHTML.trim() === '') {
  689. // Load children lazily
  690. btn.innerHTML = '<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>';
  691. // Get already displayed descendant IDs to exclude
  692. const excludedIds = getExcludedDescendantIds(btn);
  693. const excludeParam = excludedIds.length > 0 ? `?exclude=${excludedIds.join(',')}` : '';
  694. console.log(`[ToggleChildren] Parent ID: ${parentId}, Excluded IDs: ${excludedIds}, URL: /manager/api/get_descendants/${parentId}${excludeParam}`);
  695. try {
  696. const response = await fetch(`/manager/api/get_descendants/${parentId}${excludeParam}`, {
  697. credentials: 'include'
  698. });
  699. const result = await response.json();
  700. if (result.success && result.children) {
  701. // Render children
  702. container.innerHTML = renderChildrenRecursive(result.children);
  703. }
  704. } catch (error) {
  705. console.error('Failed to load children:', error);
  706. } finally {
  707. btn.innerHTML = '−';
  708. }
  709. }
  710. container.style.display = 'flex';
  711. btn.innerHTML = '−';
  712. }
  713. }
  714. // Get descendant IDs that are already displayed in the tree (to avoid duplicates)
  715. function getExcludedDescendantIds(btn) {
  716. const excluded = new Set();
  717. // Helper function to extract IDs from tree nodes
  718. const extractIdsFromNodes = (container) => {
  719. if (!container) return;
  720. const nodes = container.querySelectorAll('.tree-node');
  721. nodes.forEach(node => {
  722. const id = node.getAttribute('data-id');
  723. if (id && !isNaN(parseInt(id))) {
  724. excluded.add(parseInt(id));
  725. }
  726. });
  727. };
  728. // Get IDs from ancestors tree
  729. const ancestorsTree = document.getElementById('ancestorsTree');
  730. extractIdsFromNodes(ancestorsTree);
  731. // Get IDs from siblings tree (center person and siblings)
  732. const siblingsTree = document.getElementById('siblingsTree');
  733. extractIdsFromNodes(siblingsTree);
  734. // Get IDs from children tree
  735. const childrenTree = document.getElementById('childrenTree');
  736. extractIdsFromNodes(childrenTree);
  737. console.log('Excluded IDs:', Array.from(excluded));
  738. return Array.from(excluded);
  739. }
  740. // Enter key search
  741. document.getElementById('searchInput').addEventListener('keypress', function(e) {
  742. if (e.key === 'Enter') {
  743. searchMember();
  744. }
  745. });
  746. </script>
  747. {% endblock %}