test_dom_steps.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Layer DOM Content + Steps</title>
  7. <link rel="stylesheet" href="../demo.css">
  8. <link rel="stylesheet" href="../../xjs.css">
  9. <style>
  10. .row {
  11. display: flex;
  12. flex-wrap: wrap;
  13. gap: 10px;
  14. align-items: center;
  15. }
  16. .btn {
  17. appearance: none;
  18. border: 1px solid rgba(255,255,255,0.12);
  19. background: rgba(255,255,255,0.04);
  20. color: #fff;
  21. border-radius: 999px;
  22. padding: 10px 14px;
  23. font-weight: 650;
  24. cursor: pointer;
  25. transition: border-color 0.15s ease, background 0.15s ease;
  26. }
  27. .btn:hover { border-color: rgba(255,255,255,0.22); background: rgba(255,255,255,0.06); }
  28. .btn.primary { border-color: rgba(63,195,238,0.55); background: rgba(63,195,238,0.12); }
  29. .btn.success { border-color: rgba(165,220,134,0.55); background: rgba(165,220,134,0.12); }
  30. .demo-visual { padding: 22px 24px; }
  31. .hint { font-size: 12px; color: #8c8c8c; line-height: 1.5; margin-top: 10px; }
  32. /* The DOM blocks below are hidden by default. Layer will move them into popup and temporarily show them. */
  33. .hidden-dom { display: none; }
  34. .form {
  35. width: 100%;
  36. text-align: left;
  37. display: grid;
  38. gap: 10px;
  39. }
  40. .field label {
  41. display: block;
  42. font-size: 12px;
  43. color: #777;
  44. margin-bottom: 6px;
  45. }
  46. .field input, .field select {
  47. width: 100%;
  48. box-sizing: border-box;
  49. padding: 10px 12px;
  50. border-radius: 10px;
  51. border: 1px solid rgba(0,0,0,0.12);
  52. outline: none;
  53. background: #fff;
  54. color: #111;
  55. font-size: 14px;
  56. }
  57. .error {
  58. color: #c0392b;
  59. font-size: 12px;
  60. min-height: 16px;
  61. }
  62. pre {
  63. width: 100%;
  64. padding: 12px;
  65. border-radius: 12px;
  66. background: rgba(0,0,0,0.06);
  67. overflow: auto;
  68. }
  69. </style>
  70. </head>
  71. <body>
  72. <div class="container">
  73. <div class="page-top">
  74. <div class="crumb">UI › LAYER</div>
  75. <div class="since">DOM + STEPS</div>
  76. </div>
  77. <h1>DOM content + Step flow</h1>
  78. <p class="description">
  79. Demonstrates <code class="inline">dom</code> (mount/restore hidden DOM into popup) and a multi-step wizard using <code class="inline">step()</code> / <code class="inline">steps()</code>.
  80. </p>
  81. <div class="box-container">
  82. <div class="demo-visual">
  83. <div class="row">
  84. <button id="openDom" class="btn primary">Open DOM popup</button>
  85. <button id="openWizard" class="btn success">Open step wizard</button>
  86. <button id="openStepContainer" class="btn">Open container steps</button>
  87. </div>
  88. <div class="hint">
  89. Tip: step wizard disables backdrop/ESC close to avoid accidental dismiss.
  90. </div>
  91. </div>
  92. <div class="box-header">
  93. <div class="box-title">Example</div>
  94. <div class="tabs">
  95. <div class="tab active" onclick="switchTab('js')">JavaScript</div>
  96. <div class="tab" onclick="switchTab('html')">HTML</div>
  97. <div class="tab" onclick="switchTab('css')">CSS</div>
  98. </div>
  99. </div>
  100. <pre id="js-code" class="code-view active">// 1) Single popup: show a hidden DOM block inside Layer
  101. $('#openDom').click(function () {
  102. $.layer({
  103. title: 'DOM content',
  104. dom: '#demo_dom_block',
  105. showCancelButton: true,
  106. confirmButtonText: 'OK',
  107. cancelButtonText: 'Close'
  108. });
  109. });
  110. // 2) Step flow (wizard) - no icon during steps; final summary is allowed
  111. $('#openWizard').click(function () {
  112. $.layer({
  113. closeOnClickOutside: false,
  114. closeOnEsc: false,
  115. popupAnimation: false,
  116. confirmButtonColor: '#3085d6',
  117. cancelButtonColor: '#444'
  118. })
  119. .step({
  120. title: 'Step 1: Basic info',
  121. dom: '#wizard_step_1',
  122. showCancelButton: true,
  123. cancelButtonText: 'Cancel',
  124. preConfirm(popup) {
  125. const name = popup.querySelector('input[name="name"]').value.trim();
  126. const err = popup.querySelector('[data-error]');
  127. if (!name) { err.textContent = 'Please enter your name.'; return false; }
  128. err.textContent = '';
  129. return { name };
  130. }
  131. })
  132. .step({
  133. title: 'Step 2: Preferences',
  134. dom: '#wizard_step_2',
  135. preConfirm(popup) {
  136. const plan = popup.querySelector('select[name="plan"]').value;
  137. return { plan };
  138. }
  139. })
  140. .step({
  141. title: 'Summary',
  142. icon: 'success',
  143. isSummary: true,
  144. html: '&lt;div class="hint"&gt;Click OK to finish.&lt;/div&gt;'
  145. })
  146. .run()
  147. .then((res) =&gt; {
  148. if (res.isConfirmed) {
  149. $.layer({
  150. title: 'Done',
  151. icon: 'success',
  152. popupAnimation: false,
  153. replace: true,
  154. html: '&lt;pre&gt;' + JSON.stringify(res.value, null, 2) + '&lt;/pre&gt;'
  155. });
  156. }
  157. });
  158. });
  159. // 3) Step container shorthand (auto steps + form data)
  160. $('#openStepContainer').click(function () {
  161. $.layer({
  162. step: '#step_container',
  163. stepItem: '.stepItem',
  164. title: 'Create task',
  165. showCancelButton: true
  166. }).then((res) =&gt; {
  167. if (res.isConfirmed) {
  168. $.layer({
  169. title: 'Collected data',
  170. icon: 'success',
  171. popupAnimation: false,
  172. replace: true,
  173. html: '&lt;pre&gt;' + JSON.stringify(res.data, null, 2) + '&lt;/pre&gt;'
  174. });
  175. }
  176. });
  177. });</pre>
  178. <pre id="html-code" class="html-view">&lt;button class="btn primary" onclick="openDom()"&gt;Open DOM popup&lt;/button&gt;
  179. &lt;button class="btn success" onclick="openWizard()"&gt;Open step wizard&lt;/button&gt;
  180. &lt;button class="btn" onclick="openStepContainer()"&gt;Open container steps&lt;/button&gt;
  181. &lt;!-- Hidden DOM blocks (default display:none) --&gt;
  182. &lt;div id="demo_dom_block" class="hidden-dom"&gt;
  183. &lt;div class="form"&gt;
  184. &lt;div class="field"&gt;
  185. &lt;label&gt;Quick note&lt;/label&gt;
  186. &lt;input placeholder="This input lives in a hidden DOM block" value="Hello from DOM!"&gt;
  187. &lt;/div&gt;
  188. &lt;div class="hint" style="margin-top: -4px;"&gt;
  189. This DOM node is moved into the popup and restored on close.
  190. &lt;/div&gt;
  191. &lt;/div&gt;
  192. &lt;/div&gt;
  193. &lt;div id="wizard_step_1" class="hidden-dom"&gt;
  194. &lt;div class="form"&gt;
  195. &lt;div class="field"&gt;
  196. &lt;label&gt;Name&lt;/label&gt;
  197. &lt;input name="name" placeholder="Your name"&gt;
  198. &lt;/div&gt;
  199. &lt;div class="error" data-error&gt;&lt;/div&gt;
  200. &lt;/div&gt;
  201. &lt;/div&gt;
  202. &lt;div id="wizard_step_2" class="hidden-dom"&gt;
  203. &lt;div class="form"&gt;
  204. &lt;div class="field"&gt;
  205. &lt;label&gt;Plan&lt;/label&gt;
  206. &lt;select name="plan"&gt;
  207. &lt;option value="free"&gt;Free&lt;/option&gt;
  208. &lt;option value="pro"&gt;Pro&lt;/option&gt;
  209. &lt;option value="team"&gt;Team&lt;/option&gt;
  210. &lt;/select&gt;
  211. &lt;/div&gt;
  212. &lt;div class="hint"&gt;Confirm will finish the wizard.&lt;/div&gt;
  213. &lt;/div&gt;
  214. &lt;/div&gt;
  215. &lt;div id="step_container" class="hidden-dom"&gt;
  216. &lt;div class="stepItem" data-step-title="Step 1: Basics"&gt;
  217. &lt;div class="form"&gt;
  218. &lt;div class="field"&gt;
  219. &lt;label&gt;Project&lt;/label&gt;
  220. &lt;input name="project" placeholder="Project name"&gt;
  221. &lt;/div&gt;
  222. &lt;div class="field"&gt;
  223. &lt;label&gt;Owner&lt;/label&gt;
  224. &lt;input name="owner" placeholder="Owner name"&gt;
  225. &lt;/div&gt;
  226. &lt;/div&gt;
  227. &lt;/div&gt;
  228. &lt;div class="stepItem" data-step-title="Step 2: Priority"&gt;
  229. &lt;div class="form"&gt;
  230. &lt;div class="field"&gt;
  231. &lt;label&gt;Priority&lt;/label&gt;
  232. &lt;select name="priority"&gt;
  233. &lt;option value="low"&gt;Low&lt;/option&gt;
  234. &lt;option value="normal"&gt;Normal&lt;/option&gt;
  235. &lt;option value="high"&gt;High&lt;/option&gt;
  236. &lt;/select&gt;
  237. &lt;/div&gt;
  238. &lt;/div&gt;
  239. &lt;/div&gt;
  240. &lt;/div&gt;</pre>
  241. <pre id="css-code" class="css-view">/* This page hides DOM blocks by default:
  242. .hidden-dom { display: none; }
  243. Layer will move the element into the popup while open,
  244. then restore it back (and restore display/hidden state) on close. */</pre>
  245. <div class="feature-desc">
  246. <strong>功能说明:</strong>
  247. <code class="inline">dom</code> 支持把指定 DOM(可默认隐藏)挂进弹窗并在关闭时还原;步骤流通过同一弹窗内平滑切换实现“分步填写”体验,最终 <code class="inline">res.value</code> 返回每一步的 <code class="inline">preConfirm</code> 结果数组。容器式 steps 会自动汇总表单数据到 <code class="inline">res.data</code>。
  248. </div>
  249. </div>
  250. <!-- Hidden DOM content (these are mounted into Layer) -->
  251. <div id="demo_dom_block" class="hidden-dom">
  252. <div class="form">
  253. <div class="field">
  254. <label>Quick note</label>
  255. <input placeholder="This input lives in a hidden DOM block" value="Hello from DOM!">
  256. </div>
  257. <div class="hint" style="margin-top: -4px;">
  258. This DOM node is moved into the popup and restored on close.
  259. </div>
  260. </div>
  261. </div>
  262. <div id="wizard_step_1" class="hidden-dom">
  263. <div class="form">
  264. <div class="field">
  265. <label>Name</label>
  266. <input name="name" placeholder="Your name">
  267. </div>
  268. <div class="error" data-error></div>
  269. </div>
  270. </div>
  271. <div id="wizard_step_2" class="hidden-dom">
  272. <div class="form">
  273. <div class="field">
  274. <label>Plan</label>
  275. <select name="plan">
  276. <option value="free">Free</option>
  277. <option value="pro">Pro</option>
  278. <option value="team">Team</option>
  279. </select>
  280. </div>
  281. <div class="hint">Confirm will finish the wizard.</div>
  282. </div>
  283. </div>
  284. <div id="step_container" class="hidden-dom">
  285. <div class="stepItem" data-step-title="Step 1: Basics">
  286. <div class="form">
  287. <div class="field">
  288. <label>Project</label>
  289. <input name="project" placeholder="Project name">
  290. </div>
  291. <div class="field">
  292. <label>Owner</label>
  293. <input name="owner" placeholder="Owner name">
  294. </div>
  295. </div>
  296. </div>
  297. <div class="stepItem" data-step-title="Step 2: Priority">
  298. <div class="form">
  299. <div class="field">
  300. <label>Priority</label>
  301. <select name="priority">
  302. <option value="low">Low</option>
  303. <option value="normal">Normal</option>
  304. <option value="high">High</option>
  305. </select>
  306. </div>
  307. </div>
  308. </div>
  309. </div>
  310. <div class="doc-nav" aria-label="Previous and next navigation">
  311. <a href="#" id="prevLink" onclick="goPrev(); return false;">
  312. <span><span class="nav-label">Previous</span><br><span class="nav-title" id="prevTitle">—</span></span>
  313. <span aria-hidden="true">←</span>
  314. </a>
  315. <div class="nav-center" id="navCenter">Layer</div>
  316. <a href="#" id="nextLink" onclick="goNext(); return false;">
  317. <span><span class="nav-label">Next</span><br><span class="nav-title" id="nextTitle">—</span></span>
  318. <span aria-hidden="true">→</span>
  319. </a>
  320. </div>
  321. </div>
  322. <script src="../highlight_css.js"></script>
  323. <script>
  324. const CURRENT = 'layer/test_dom_steps.html';
  325. // =========================================================
  326. // Dev loader (cache-bust) for local debugging
  327. // - Ensure this page always uses the latest workspace `xjs.js` + `layer.js`
  328. // - Avoid "no change" caused by browser caching `?v=dev`
  329. // =========================================================
  330. const DEV_CACHE_BUST = Date.now();
  331. function loadScript(src) {
  332. return new Promise((resolve, reject) => {
  333. const s = document.createElement('script');
  334. s.src = src;
  335. s.async = false; // preserve order
  336. s.onload = () => resolve();
  337. s.onerror = () => reject(new Error('Failed to load: ' + src));
  338. (document.head || document.documentElement).appendChild(s);
  339. });
  340. }
  341. function switchTab(tab) {
  342. document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
  343. document.querySelectorAll('.code-view, .html-view, .css-view').forEach(v => v.classList.remove('active'));
  344. if (tab === 'js') {
  345. document.querySelector('.tabs .tab:nth-child(1)')?.classList.add('active');
  346. document.getElementById('js-code')?.classList.add('active');
  347. } else if (tab === 'html') {
  348. document.querySelector('.tabs .tab:nth-child(2)')?.classList.add('active');
  349. document.getElementById('html-code')?.classList.add('active');
  350. } else {
  351. document.querySelector('.tabs .tab:nth-child(3)')?.classList.add('active');
  352. document.getElementById('css-code')?.classList.add('active');
  353. }
  354. }
  355. function bindDemoHandlers() {
  356. if (typeof $ !== 'function') return;
  357. $('#openDom').click(function () {
  358. $.layer({
  359. title: 'DOM content',
  360. dom: '#demo_dom_block',
  361. popupAnimation: true,
  362. showCancelButton: true,
  363. confirmButtonText: 'OK',
  364. cancelButtonText: 'Close'
  365. });
  366. });
  367. $('#openWizard').click(function () {
  368. $.layer({
  369. closeOnClickOutside: false,
  370. closeOnEsc: false,
  371. popupAnimation: false,
  372. confirmButtonColor: '#3085d6',
  373. cancelButtonColor: '#444'
  374. })
  375. .step({
  376. title: 'Step 1: Basic info',
  377. dom: '#wizard_step_1',
  378. showCancelButton: true,
  379. cancelButtonText: 'Cancel',
  380. preConfirm(popup) {
  381. const name = popup.querySelector('input[name="name"]').value.trim();
  382. const err = popup.querySelector('[data-error]');
  383. if (!name) { err.textContent = 'Please enter your name.'; return false; }
  384. err.textContent = '';
  385. return { name };
  386. }
  387. })
  388. .step({
  389. title: 'Step 2: Preferences',
  390. dom: '#wizard_step_2',
  391. preConfirm(popup) {
  392. const plan = popup.querySelector('select[name="plan"]').value;
  393. return { plan };
  394. }
  395. })
  396. .step({
  397. title: 'Summary',
  398. icon: 'success',
  399. isSummary: true,
  400. html: '<div class="hint">Click OK to finish.</div>'
  401. })
  402. .run()
  403. .then((res) => {
  404. if (res.isConfirmed) {
  405. $.layer({
  406. title: 'Done',
  407. icon: 'success',
  408. popupAnimation: false,
  409. replace: true,
  410. html: '<pre>' + JSON.stringify(res.value, null, 2) + '</pre>'
  411. });
  412. } else {
  413. $.layer({ title: 'Dismissed', icon: 'info', popupAnimation: false, replace: true, text: 'Flow cancelled' });
  414. }
  415. });
  416. });
  417. $('#openStepContainer').click(function () {
  418. $.layer({
  419. step: '#step_container',
  420. stepItem: '.stepItem',
  421. title: 'Create task',
  422. showCancelButton: true
  423. }).then((res) => {
  424. if (res.isConfirmed) {
  425. $.layer({
  426. title: 'Collected data',
  427. icon: 'success',
  428. popupAnimation: false,
  429. replace: true,
  430. html: '<pre>' + JSON.stringify(res.data, null, 2) + '</pre>'
  431. });
  432. }
  433. });
  434. });
  435. }
  436. function goPrev() { window.parent?.docNavigatePrev?.(CURRENT); }
  437. function goNext() { window.parent?.docNavigateNext?.(CURRENT); }
  438. function syncNavLabels() {
  439. const api = window.parent?.docGetPrevNext;
  440. if (typeof api !== 'function') return;
  441. const { prev, next, current } = api(CURRENT) || {};
  442. const prevLink = document.getElementById('prevLink');
  443. const nextLink = document.getElementById('nextLink');
  444. if (prev) document.getElementById('prevTitle').textContent = prev.title || prev.url;
  445. else { prevLink.style.visibility = 'hidden'; }
  446. if (next) document.getElementById('nextTitle').textContent = next.title || next.url;
  447. else { nextLink.style.visibility = 'hidden'; }
  448. document.getElementById('navCenter').textContent = (current && current.group) ? current.group : 'Layer';
  449. }
  450. try { window.parent?.setMiddleActive?.(CURRENT); } catch {}
  451. syncNavLabels();
  452. </script>
  453. <script>
  454. // NOTE: xjs.js bundles an older Layer in some builds.
  455. // For local testing, load standalone layer.js last to ensure this page uses the edited Layer implementation.
  456. (async () => {
  457. const base = '../../';
  458. // Important:
  459. // - Do NOT load `xjs.js` dev-loader here, because it internally loads `layer.js` again
  460. // (without cache-busting) and may overwrite the fresh one after our load finishes.
  461. // - Load sources directly with cache-bust to guarantee we use the current workspace code.
  462. await loadScript(base + 'animal.js?_=' + DEV_CACHE_BUST);
  463. try {
  464. if (window.animal && !window.xjs) window.xjs = window.animal;
  465. if (window.xjs && !window.$) window.$ = window.xjs;
  466. } catch {}
  467. await loadScript(base + 'layer.js?_=' + DEV_CACHE_BUST);
  468. bindDemoHandlers();
  469. })().catch((e) => {
  470. try { console.error(e); } catch {}
  471. });
  472. </script>
  473. </body>
  474. </html>