cy-sidebar.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*!
  2. * cy-sidebar.js · 模式感知常驻侧栏(共享组件)
  3. * ────────────────────────────────────────────────────────────────
  4. * 用途:为独立可售模块(IC / CRS / CFSI …)提供统一的常驻左侧栏,
  5. * 一处升级、全模块生效(CLAUDE.md §2 共享 JS 杠杆点)。
  6. *
  7. * 双模式(见 IC 设计文档 §5.1):
  8. * - 平台模式(window.CY_PLATFORM === true,由 data/cy-config.js 置位):
  9. * 渲染完整 9 分组平台导航;每个 navJumpTo 项渲染前校验
  10. * window.CY_PAGES[target] 存在,不存在则跳过 → 死链物理隔离。
  11. * - 独立模式(默认 · fail-safe):只渲染 品牌 + 模块名 + CTA(约演示/联系顾问),
  12. * 零跨模块链接。
  13. *
  14. * 用法:
  15. * <div id="cy-sidebar-mount"></div> ← 挂载点
  16. * CYSidebar.render({ mount: '#cy-sidebar-mount', activeKey: 'insurance',
  17. * moduleName: '保额计算器', moduleEn: 'Insurance Calculator' });
  18. *
  19. * 依赖:navJumpTo / navAssetTab / CY_PAGES(均来自 cy-renderer.js)。
  20. * 新增于 2026-07-11 · IC v5.0 app-shell 化 Phase 1
  21. */
  22. (function () {
  23. 'use strict';
  24. // ============== 平台 9 分组导航数据(对齐 Dashboard v2.4)==============
  25. // route: 走 navJumpTo(route),渲染前校验 CY_PAGES[route] 存在
  26. // asset: 走 navAssetTab(asset)
  27. // href: 直接跳转(PlanningOverview 等平台内页)
  28. // 无动作项:静态展示(评测总览/家族画像库等占位)
  29. var NAV_GROUPS = [
  30. { key: 'overview', icon: '◉', label: '总览', en: 'Overview', items: [
  31. { key: 'dashboard', icon: '◉', label: '我的看板', route: 'dashboard' }
  32. ]},
  33. { key: 'clients', icon: '👥', label: '客户管理', en: 'Clients', items: [
  34. { key: 'clientlist', icon: '⌂', label: '客户列表', route: 'clientlist', badge: '528' },
  35. { key: 'clientprofile', icon: '◔', label: '客户 360 画像', route: 'clientprofile' },
  36. { key: 'newclient', icon: '+', label: '新增客户', route: 'newclient' }
  37. ]},
  38. { key: 'cfas', icon: '🎯', label: '测评系统', en: 'Assessment Hub', items: [
  39. { icon: '▦', label: '评测总览' },
  40. { key: 'cfsi', icon: '◆', label: 'CFSI 家族传承', route: 'cfsi', badge: 'Quick Scan' },
  41. { key: 'cfpi', icon: '◇', label: 'CFPI 家族保障', route: 'cfpi' },
  42. { key: 'insurance', icon: '🛡', label: '保额计算器', route: 'insurance' },
  43. { key: 'crs', icon: '🌐', label: 'CRS 合规自查器', route: 'crs' },
  44. { icon: '◈', label: 'CFHI 家族健康', dev: true, badge: '开发中' },
  45. { icon: '⚏', label: '家族画像库' }
  46. ]},
  47. { key: 'assets', icon: '💼', label: '资产配置', en: 'Assets', items: [
  48. { icon: '⊞', label: '持仓总览', asset: 'holdings' },
  49. { icon: '▤', label: '资产检视台', asset: 'inspector' },
  50. { icon: '⛨', label: '保单检视台', asset: 'policy' },
  51. { icon: '⌗', label: '标签工作台', asset: 'tags' },
  52. { icon: '⇌', label: 'KYC-KYP 匹配', asset: 'match' },
  53. { icon: '▥', label: '产品池', asset: 'catalog' }
  54. ]},
  55. { key: 'simulators', icon: '🔮', label: '推演工具', en: 'Simulators', items: [
  56. { key: 'letssee', icon: '📊', label: "Let's See 推演", route: 'letssee' },
  57. { key: 'whatif', icon: '⇄', label: 'What If 场景', route: 'whatif' }
  58. ]},
  59. { key: 'reports', icon: '📄', label: '报告中心', en: 'Reports', items: [
  60. { icon: '▤', label: '报告模板库' },
  61. { icon: '📄', label: '我的报告' },
  62. { icon: '🎨', label: '主题切换' }
  63. ]},
  64. { key: 'settings', icon: '⚙️', label: '设置', en: 'Settings', items: [
  65. { icon: '⌥', label: '个人资料' },
  66. { icon: '⚙', label: '系统设置' }
  67. ]}
  68. ];
  69. function esc(s) { return String(s == null ? '' : s).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;'); }
  70. // 单个 nav-item 的 HTML(含 target 校验:route 不在 CY_PAGES 则不接 onclick 也不显示)
  71. function renderItem(it, activeKey) {
  72. // route 项:校验 CY_PAGES 存在,否则整项跳过(死链物理隔离)
  73. if (it.route) {
  74. var ok = window.CY_PAGES && window.CY_PAGES[it.route];
  75. if (!ok) return ''; // 兄弟模块未随附 → 不渲染,杜绝死链
  76. }
  77. var cls = 'nav-item' + (it.dev ? ' dev' : '') + (it.key && it.key === activeKey ? ' active' : '');
  78. var onclick = '';
  79. if (it.route && !it.dev) onclick = ' onclick="navJumpTo(\'' + it.route + '\')" style="cursor:pointer"';
  80. else if (it.asset) onclick = ' onclick="navAssetTab(\'' + it.asset + '\')" style="cursor:pointer"';
  81. else if (it.href) onclick = ' onclick="window.location.href=\'' + esc(it.href) + '\'" style="cursor:pointer"';
  82. var badge = it.badge ? '<span class="badge"' + (it.dev ? '' : ' style="background:var(--gold-soft);color:var(--gold);font-size:9px"') + '>' + esc(it.badge) + '</span>' : '';
  83. return '<div class="' + cls + '"' + onclick + '><span class="icon">' + esc(it.icon) + '</span><span class="label">' + esc(it.label) + '</span>' + badge + '</div>';
  84. }
  85. function renderPlatformNav(activeKey) {
  86. return NAV_GROUPS.map(function (g) {
  87. var itemsHtml = g.items.map(function (it) { return renderItem(it, activeKey); }).join('');
  88. if (!itemsHtml.trim()) return ''; // 整组被过滤空则不显示
  89. var open = g.items.some(function (it) { return it.key === activeKey; }) ? ' open' : '';
  90. var en = g.en ? '<span class="en">' + esc(g.en) + '</span>' : '';
  91. var visCount = g.items.filter(function (it) { return !it.route || (window.CY_PAGES && window.CY_PAGES[it.route]); }).length;
  92. return '<div class="nav-section' + open + '" data-section="' + g.key + '">' +
  93. '<div class="nav-section-header" onclick="cySidebarToggle(this)">' +
  94. '<span class="sec-icon">' + esc(g.icon) + '</span>' +
  95. '<span class="sec-label">' + esc(g.label) + en + '</span>' +
  96. '<span class="sec-count">' + visCount + '</span>' +
  97. '<span class="caret">▸</span></div>' +
  98. '<div class="nav-section-body">' + itemsHtml + '</div></div>';
  99. }).join('');
  100. }
  101. // 独立模式:品牌 + 模块名 + CTA
  102. function renderStandalone(opts) {
  103. return '<div class="cy-sb-standalone">' +
  104. '<div class="cy-sb-module">' +
  105. '<div class="cy-sb-module-icon">🛡</div>' +
  106. '<div class="cy-sb-module-name">' + esc(opts.moduleName || '保额计算器') +
  107. '<span class="cy-sb-module-en">' + esc(opts.moduleEn || 'Insurance Calculator') + '</span>' +
  108. '</div>' +
  109. '</div>' +
  110. '<div class="cy-sb-cta-wrap">' +
  111. '<a class="cy-sb-cta primary" href="' + esc(opts.demoUrl || '#') + '">📅 预约演示</a>' +
  112. '<a class="cy-sb-cta" href="' + esc(opts.contactUrl || '#') + '">💬 联系顾问</a>' +
  113. '</div>' +
  114. '<div class="cy-sb-foot">Benchmark Wealth · 独立部署版</div>' +
  115. '</div>';
  116. }
  117. function brandBlock() {
  118. var rects = '';
  119. for (var a = 0; a < 360; a += 30) {
  120. rects += '<rect x="46" y="6" width="8" height="38" rx="4" transform="rotate(' + a + ' 50 50)"/>';
  121. }
  122. var svg = '<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Benchmark Wealth logo">' +
  123. '<defs><linearGradient id="g-wealth-sb" x1="0" y1="0" x2="1" y2="1"><stop offset="0%" stop-color="#D97757"/><stop offset="100%" stop-color="#a95d44"/></linearGradient></defs>' +
  124. '<g fill="url(#g-wealth-sb)">' + rects + '</g></svg>';
  125. return '<div class="brand"><div class="brand-mark">' + svg + '</div>' +
  126. '<div class="brand-text">Benchmark Wealth<span class="accent">Family Wealth Platform</span></div></div>';
  127. }
  128. // ============== 注入 CSS(含 IC 缺失变量 fallback)==============
  129. var CSS = [
  130. '.cy-app{display:grid;grid-template-columns:240px 1fr;min-height:100vh}',
  131. '.cy-app>.cy-main{min-width:0}', // 防子元素撑破
  132. // z-index:200 · 盖住主区 band 的 box-shadow:100vmax 全宽出血(否则色条向左溢出到侧栏);isolation 建独立层叠上下文,稳态压过 .container z-index
  133. '.sidebar{background:linear-gradient(180deg,#2C2218 0%,#0A0806 100%);border-right:1px solid rgba(201,168,76,0.15);display:flex;flex-direction:column;position:sticky;top:0;height:100vh;overflow-y:auto;z-index:200;isolation:isolate}',
  134. '.sidebar::-webkit-scrollbar{width:4px}.sidebar::-webkit-scrollbar-thumb{background:rgba(201,168,76,0.25);border-radius:3px}',
  135. '.sidebar .brand{padding:18px 20px 16px;border-bottom:1px solid rgba(201,168,76,0.2);display:flex;align-items:center;gap:10px}',
  136. '.sidebar .brand-mark{width:36px;height:36px;flex-shrink:0;display:flex;align-items:center;justify-content:center}',
  137. '.sidebar .brand-mark svg{width:100%;height:100%;display:block}',
  138. '.sidebar .brand-text{font-family:"Fraunces","Noto Serif SC",Georgia,serif!important;font-size:15px;font-weight:600;line-height:1.25;color:rgba(245,237,216,0.92);min-width:0;flex:1}',
  139. '.sidebar .brand-text .accent{display:block;font-family:"Fraunces","Noto Serif SC",Georgia,serif;font-size:8.5px;color:#D97757;text-transform:uppercase;letter-spacing:.8px;font-weight:700;margin-top:5px;white-space:nowrap}',
  140. '.sidebar .nav-section{border-top:1px solid rgba(255,255,255,0.06)}.sidebar .nav-section:first-of-type{border-top:none}',
  141. '.sidebar .nav-section-header{padding:14px 16px;display:flex;align-items:center;gap:10px;cursor:pointer;user-select:none;transition:background .15s}',
  142. '.sidebar .nav-section-header:hover{background:rgba(201,168,76,0.1)}',
  143. '.sidebar .sec-icon{width:18px;text-align:center;font-size:13px;color:rgba(201,168,76,0.7);flex-shrink:0}',
  144. '.sidebar .sec-label{flex:1;font-size:12px;color:rgba(245,237,216,0.85);font-weight:600;letter-spacing:.4px}',
  145. '.sidebar .sec-label .en{display:block;font-size:9.5px;color:rgba(245,237,216,0.35);text-transform:uppercase;letter-spacing:1.4px;font-weight:500;margin-top:1px}',
  146. '.sidebar .sec-count{font-size:10px;color:rgba(245,237,216,0.35);font-family:var(--font-mono);background:rgba(255,255,255,0.07);padding:1px 6px;border-radius:8px;margin-right:4px}',
  147. '.sidebar .caret{font-size:9px;color:rgba(245,237,216,0.3);transition:transform .2s}',
  148. '.sidebar .nav-section.open .caret{transform:rotate(90deg);color:var(--gold-3,#C9A84C)}',
  149. '.sidebar .nav-section.open .nav-section-header{background:rgba(201,168,76,0.12)}',
  150. '.sidebar .nav-section.open .sec-label{color:var(--gold-3,#C9A84C)}',
  151. '.sidebar .nav-section-body{max-height:0;overflow:hidden;transition:max-height .25s ease-out}',
  152. '.sidebar .nav-section.open .nav-section-body{max-height:600px;padding-bottom:8px}',
  153. '.sidebar .nav-item{padding:9px 18px 9px 44px;display:flex;align-items:center;gap:10px;color:rgba(245,237,216,0.55);font-size:12.5px;cursor:default;border-left:2px solid transparent;transition:all .15s;position:relative}',
  154. '.sidebar .nav-item:hover{background:rgba(201,168,76,0.1);color:rgba(245,237,216,0.9)}',
  155. '.sidebar .nav-item.active{background:rgba(201,168,76,0.16);color:#E8D5A0;border-left-color:var(--gold-3,#C9A84C);font-weight:500}',
  156. '.sidebar .nav-item .icon{width:16px;text-align:center;font-size:12px;flex-shrink:0;color:rgba(245,237,216,0.3)}',
  157. '.sidebar .nav-item.active .icon{color:var(--gold-3,#C9A84C)}',
  158. '.sidebar .nav-item .label{flex:1}',
  159. '.sidebar .nav-item .badge{font-size:10px;background:var(--gold,#9B7C3A);color:#fff;border-radius:10px;padding:1px 7px;font-family:var(--font-mono);font-weight:500}',
  160. '.sidebar .nav-item.dev{opacity:.4;cursor:not-allowed}.sidebar .nav-item.dev:hover{background:transparent;color:rgba(245,237,216,0.55)}',
  161. '.sidebar .nav-item.dev .badge{background:transparent;color:rgba(201,168,76,0.6);border:1px solid rgba(201,168,76,0.25);font-size:9px;padding:0 6px}',
  162. // 独立模式样式
  163. '.cy-sb-standalone{display:flex;flex-direction:column;flex:1;padding:24px 20px;gap:20px}',
  164. '.cy-sb-module{display:flex;align-items:center;gap:12px;padding-bottom:18px;border-bottom:1px solid rgba(201,168,76,0.15)}',
  165. '.cy-sb-module-icon{font-size:26px}',
  166. '.cy-sb-module-name{font-family:var(--font-serif);font-size:16px;font-weight:600;color:rgba(245,237,216,0.95);line-height:1.3}',
  167. '.cy-sb-module-en{display:block;font-size:10px;color:var(--gold,#9B7C3A);font-style:italic;letter-spacing:.5px;margin-top:2px}',
  168. '.cy-sb-cta-wrap{display:flex;flex-direction:column;gap:10px;margin-top:8px}',
  169. '.cy-sb-cta{display:block;text-align:center;padding:11px 14px;border-radius:8px;font-size:13px;font-weight:600;text-decoration:none;transition:all .15s;border:1px solid rgba(201,168,76,0.3);color:rgba(245,237,216,0.85)}',
  170. '.cy-sb-cta:hover{background:rgba(201,168,76,0.12)}',
  171. '.cy-sb-cta.primary{background:linear-gradient(135deg,var(--gold-3,#C9A84C),var(--gold,#9B7C3A));color:#fff;border:none}',
  172. '.cy-sb-cta.primary:hover{filter:brightness(1.08)}',
  173. '.cy-sb-foot{margin-top:auto;font-size:10px;color:rgba(245,237,216,0.3);text-align:center;letter-spacing:.3px}',
  174. // 窄屏降级:侧栏置顶单栏
  175. '@media(max-width:1000px){.cy-app{grid-template-columns:1fr}.sidebar{position:relative;height:auto;flex-direction:row;flex-wrap:wrap;overflow:visible}}'
  176. ].join('\n');
  177. function injectCSS() {
  178. if (document.getElementById('cy-sidebar-css')) return;
  179. var st = document.createElement('style');
  180. st.id = 'cy-sidebar-css';
  181. st.textContent = CSS;
  182. document.head.appendChild(st);
  183. }
  184. // 折叠切换(避免与页面既有 toggleSection 命名冲突,用独立名)
  185. window.cySidebarToggle = function (headerEl) {
  186. var sec = headerEl && headerEl.closest ? headerEl.closest('.nav-section') : null;
  187. if (sec) sec.classList.toggle('open');
  188. };
  189. // ============== 主入口 ==============
  190. var CYSidebar = {
  191. render: function (opts) {
  192. opts = opts || {};
  193. injectCSS();
  194. var mount = typeof opts.mount === 'string' ? document.querySelector(opts.mount) : opts.mount;
  195. if (!mount) { console.warn('[cy-sidebar] 挂载点未找到:', opts.mount); return; }
  196. var isPlatform = !!window.CY_PLATFORM;
  197. var inner = isPlatform
  198. ? (brandBlock() + renderPlatformNav(opts.activeKey))
  199. : (brandBlock() + renderStandalone(opts));
  200. mount.className = 'sidebar';
  201. mount.innerHTML = inner;
  202. console.log('[cy-sidebar] 渲染完成 · 模式=' + (isPlatform ? 'platform' : 'standalone') + ' · active=' + (opts.activeKey || '-'));
  203. }
  204. };
  205. window.CYSidebar = CYSidebar;
  206. })();