test_dom_steps.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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.run({
  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.run({
  113. closeOnClickOutside: false,
  114. closeOnEsc: false,
  115. popupAnimation: false
  116. })
  117. .step({
  118. title: 'Step 1: Basic info',
  119. dom: '#wizard_step_1',
  120. showCancelButton: true,
  121. cancelButtonText: 'Cancel',
  122. nextIcon: '&gt;',
  123. preConfirm(popup) {
  124. const name = popup.querySelector('input[name="name"]').value.trim();
  125. const err = popup.querySelector('[data-error]');
  126. if (!name) { err.textContent = 'Please enter your name.'; return false; }
  127. err.textContent = '';
  128. return { name };
  129. }
  130. })
  131. .step({
  132. title: 'Step 2: Preferences',
  133. dom: '#wizard_step_2',
  134. prevIcon: '&lt;',
  135. nextIcon: '&gt;',
  136. preConfirm(popup) {
  137. const plan = popup.querySelector('select[name="plan"]').value;
  138. return { plan };
  139. }
  140. })
  141. .step({
  142. title: 'Summary',
  143. icon: 'success',
  144. isSummary: true,
  145. prevIcon: '&lt;',
  146. html: '&lt;div class="hint"&gt;Click OK to finish.&lt;/div&gt;'
  147. })
  148. .run()
  149. .then((res) =&gt; {
  150. if (res.isConfirmed) {
  151. Layer.run({
  152. title: 'Done',
  153. icon: 'success',
  154. popupAnimation: false,
  155. replace: true,
  156. html: '&lt;pre&gt;' + JSON.stringify(res.value, null, 2) + '&lt;/pre&gt;'
  157. });
  158. }
  159. });
  160. });
  161. // 3) Step container shorthand (auto steps + form data)
  162. $('#openStepContainer').click(function () {
  163. Layer.run({
  164. step: '#step_container',
  165. stepItem: '.stepItem',
  166. title: 'Create task',
  167. showCancelButton: true
  168. }).then((res) =&gt; {
  169. if (res.isConfirmed) {
  170. Layer.run({
  171. title: 'Collected data',
  172. icon: 'success',
  173. popupAnimation: false,
  174. replace: true,
  175. html: '&lt;pre&gt;' + JSON.stringify(res.data, null, 2) + '&lt;/pre&gt;'
  176. });
  177. }
  178. });
  179. });</pre>
  180. <pre id="html-code" class="html-view">&lt;button class="btn primary" onclick="openDom()"&gt;Open DOM popup&lt;/button&gt;
  181. &lt;button class="btn success" onclick="openWizard()"&gt;Open step wizard&lt;/button&gt;
  182. &lt;button class="btn" onclick="openStepContainer()"&gt;Open container steps&lt;/button&gt;
  183. &lt;!-- Hidden DOM blocks (default display:none) --&gt;
  184. &lt;div id="demo_dom_block" class="hidden-dom"&gt;
  185. &lt;div class="form"&gt;
  186. &lt;div class="field"&gt;
  187. &lt;label&gt;Quick note&lt;/label&gt;
  188. &lt;input placeholder="This input lives in a hidden DOM block" value="Hello from DOM!"&gt;
  189. &lt;/div&gt;
  190. &lt;div class="hint" style="margin-top: -4px;"&gt;
  191. This DOM node is moved into the popup and restored on close.
  192. &lt;/div&gt;
  193. &lt;/div&gt;
  194. &lt;/div&gt;
  195. &lt;div id="wizard_step_1" class="hidden-dom"&gt;
  196. &lt;div class="form"&gt;
  197. &lt;div class="field"&gt;
  198. &lt;label&gt;Name&lt;/label&gt;
  199. &lt;input name="name" placeholder="Your name"&gt;
  200. &lt;/div&gt;
  201. &lt;div class="error" data-error&gt;&lt;/div&gt;
  202. &lt;/div&gt;
  203. &lt;/div&gt;
  204. &lt;div id="wizard_step_2" class="hidden-dom"&gt;
  205. &lt;div class="form"&gt;
  206. &lt;div class="field"&gt;
  207. &lt;label&gt;Plan&lt;/label&gt;
  208. &lt;select name="plan"&gt;
  209. &lt;option value="free"&gt;Free&lt;/option&gt;
  210. &lt;option value="pro"&gt;Pro&lt;/option&gt;
  211. &lt;option value="team"&gt;Team&lt;/option&gt;
  212. &lt;/select&gt;
  213. &lt;/div&gt;
  214. &lt;div class="hint"&gt;Confirm will finish the wizard.&lt;/div&gt;
  215. &lt;/div&gt;
  216. &lt;/div&gt;
  217. &lt;div id="step_container" class="hidden-dom"&gt;
  218. &lt;div class="stepItem" data-step-title="Step 1: Basics"&gt;
  219. &lt;div class="form"&gt;
  220. &lt;div class="field"&gt;
  221. &lt;label&gt;Project&lt;/label&gt;
  222. &lt;input name="project" placeholder="Project name"&gt;
  223. &lt;/div&gt;
  224. &lt;div class="field"&gt;
  225. &lt;label&gt;Owner&lt;/label&gt;
  226. &lt;input name="owner" placeholder="Owner name"&gt;
  227. &lt;/div&gt;
  228. &lt;/div&gt;
  229. &lt;/div&gt;
  230. &lt;div class="stepItem" data-step-title="Step 2: Priority"&gt;
  231. &lt;div class="form"&gt;
  232. &lt;div class="field"&gt;
  233. &lt;label&gt;Priority&lt;/label&gt;
  234. &lt;select name="priority"&gt;
  235. &lt;option value="low"&gt;Low&lt;/option&gt;
  236. &lt;option value="normal"&gt;Normal&lt;/option&gt;
  237. &lt;option value="high"&gt;High&lt;/option&gt;
  238. &lt;/select&gt;
  239. &lt;/div&gt;
  240. &lt;/div&gt;
  241. &lt;/div&gt;
  242. &lt;/div&gt;</pre>
  243. <pre id="css-code" class="css-view">/* This page hides DOM blocks by default:
  244. .hidden-dom { display: none; }
  245. Layer will move the element into the popup while open,
  246. then restore it back (and restore display/hidden state) on close. */</pre>
  247. <div class="feature-desc">
  248. <strong>功能说明:</strong>
  249. <code class="inline">dom</code> 支持把指定 DOM(可默认隐藏)挂进弹窗并在关闭时还原;步骤流通过同一弹窗内平滑切换实现“分步填写”体验,最终 <code class="inline">res.value</code> 返回每一步的 <code class="inline">preConfirm</code> 结果数组。容器式 steps 会自动汇总表单数据到 <code class="inline">res.data</code>。
  250. </div>
  251. </div>
  252. <!-- Hidden DOM content (these are mounted into Layer) -->
  253. <div id="demo_dom_block" class="hidden-dom">
  254. <div class="form">
  255. <div class="field">
  256. <label>Quick note</label>
  257. <input placeholder="This input lives in a hidden DOM block" value="Hello from DOM!">
  258. </div>
  259. <div class="hint" style="margin-top: -4px;">
  260. This DOM node is moved into the popup and restored on close.
  261. </div>
  262. </div>
  263. </div>
  264. <div id="wizard_step_1" class="hidden-dom">
  265. <div class="form">
  266. <div class="field">
  267. <label>Name</label>
  268. <input name="name" placeholder="Your name">
  269. </div>
  270. <div class="error" data-error></div>
  271. </div>
  272. </div>
  273. <div id="wizard_step_2" class="hidden-dom">
  274. <div class="form">
  275. <div class="field">
  276. <label>Plan</label>
  277. <select name="plan">
  278. <option value="free">Free</option>
  279. <option value="pro">Pro</option>
  280. <option value="team">Team</option>
  281. </select>
  282. </div>
  283. <div class="hint">Confirm will finish the wizard.</div>
  284. </div>
  285. </div>
  286. <div id="step_container" class="hidden-dom">
  287. <div class="stepItem" data-step-title="Step 1: Basics">
  288. <div class="form">
  289. <div class="field">
  290. <label>Project</label>
  291. <input name="project" placeholder="Project name">
  292. </div>
  293. <div class="field">
  294. <label>Owner</label>
  295. <input name="owner" placeholder="Owner name">
  296. </div>
  297. </div>
  298. </div>
  299. <div class="stepItem" data-step-title="Step 2: Priority">
  300. <div class="form">
  301. <div class="field">
  302. <label>Priority</label>
  303. <select name="priority">
  304. <option value="low">Low</option>
  305. <option value="normal">Normal</option>
  306. <option value="high">High</option>
  307. </select>
  308. </div>
  309. </div>
  310. </div>
  311. </div>
  312. <div class="doc-nav" aria-label="Previous and next navigation">
  313. <a href="#" id="prevLink" onclick="goPrev(); return false;">
  314. <span><span class="nav-label">Previous</span><br><span class="nav-title" id="prevTitle">—</span></span>
  315. <span aria-hidden="true">←</span>
  316. </a>
  317. <div class="nav-center" id="navCenter">Layer</div>
  318. <a href="#" id="nextLink" onclick="goNext(); return false;">
  319. <span><span class="nav-label">Next</span><br><span class="nav-title" id="nextTitle">—</span></span>
  320. <span aria-hidden="true">→</span>
  321. </a>
  322. </div>
  323. </div>
  324. <script src="../highlight_css.js"></script>
  325. <script>
  326. const CURRENT = 'layer/test_dom_steps.html';
  327. // =========================================================
  328. // Dev loader (cache-bust) for local debugging
  329. // - Ensure this page always uses the latest workspace `xjs.js` + `layer.js`
  330. // - Avoid "no change" caused by browser caching `?v=dev`
  331. // =========================================================
  332. const DEV_CACHE_BUST = Date.now();
  333. function loadScript(src) {
  334. return new Promise((resolve, reject) => {
  335. const s = document.createElement('script');
  336. s.src = src;
  337. s.async = false; // preserve order
  338. s.onload = () => resolve();
  339. s.onerror = () => reject(new Error('Failed to load: ' + src));
  340. (document.head || document.documentElement).appendChild(s);
  341. });
  342. }
  343. function switchTab(tab) {
  344. document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
  345. document.querySelectorAll('.code-view, .html-view, .css-view').forEach(v => v.classList.remove('active'));
  346. if (tab === 'js') {
  347. document.querySelector('.tabs .tab:nth-child(1)')?.classList.add('active');
  348. document.getElementById('js-code')?.classList.add('active');
  349. } else if (tab === 'html') {
  350. document.querySelector('.tabs .tab:nth-child(2)')?.classList.add('active');
  351. document.getElementById('html-code')?.classList.add('active');
  352. } else {
  353. document.querySelector('.tabs .tab:nth-child(3)')?.classList.add('active');
  354. document.getElementById('css-code')?.classList.add('active');
  355. }
  356. }
  357. function bindDemoHandlers() {
  358. if (typeof $ !== 'function') return;
  359. $('#openDom').click(function () {
  360. Layer.run({
  361. title: 'DOM content',
  362. dom: '#demo_dom_block',
  363. popupAnimation: true,
  364. showCancelButton: true,
  365. confirmButtonText: 'OK',
  366. cancelButtonText: 'Close'
  367. });
  368. });
  369. $('#openWizard').click(function () {
  370. Layer.run({
  371. closeOnClickOutside: false,
  372. closeOnEsc: false,
  373. popupAnimation: false
  374. })
  375. .step({
  376. title: 'Step 1: Basic info',
  377. dom: '#wizard_step_1',
  378. showCancelButton: true,
  379. cancelButtonText: 'Cancel',
  380. nextIcon: '>',
  381. preConfirm(popup) {
  382. const name = popup.querySelector('input[name="name"]').value.trim();
  383. const err = popup.querySelector('[data-error]');
  384. if (!name) { err.textContent = 'Please enter your name.'; return false; }
  385. err.textContent = '';
  386. return { name };
  387. }
  388. })
  389. .step({
  390. title: 'Step 2: Preferences',
  391. dom: '#wizard_step_2',
  392. prevIcon: '<',
  393. nextIcon: '>',
  394. preConfirm(popup) {
  395. const plan = popup.querySelector('select[name="plan"]').value;
  396. return { plan };
  397. }
  398. })
  399. .step({
  400. title: 'Summary',
  401. icon: 'success',
  402. isSummary: true,
  403. prevIcon: '<',
  404. html: '<div class="hint">Click OK to finish.</div>'
  405. })
  406. .run()
  407. .then((res) => {
  408. if (res.isConfirmed) {
  409. Layer.run({
  410. title: 'Done',
  411. icon: 'success',
  412. popupAnimation: false,
  413. replace: true,
  414. html: '<pre>' + JSON.stringify(res.value, null, 2) + '</pre>'
  415. });
  416. } else {
  417. Layer.run({ title: 'Dismissed', icon: 'info', popupAnimation: false, replace: true, text: 'Flow cancelled' });
  418. }
  419. });
  420. });
  421. $('#openStepContainer').click(function () {
  422. Layer.run({
  423. step: '#step_container',
  424. stepItem: '.stepItem',
  425. title: 'Create task',
  426. showCancelButton: true
  427. }).then((res) => {
  428. if (res.isConfirmed) {
  429. Layer.run({
  430. title: 'Collected data',
  431. icon: 'success',
  432. popupAnimation: false,
  433. replace: true,
  434. html: '<pre>' + JSON.stringify(res.data, null, 2) + '</pre>'
  435. });
  436. }
  437. });
  438. });
  439. }
  440. function goPrev() { window.parent?.docNavigatePrev?.(CURRENT); }
  441. function goNext() { window.parent?.docNavigateNext?.(CURRENT); }
  442. function syncNavLabels() {
  443. const api = window.parent?.docGetPrevNext;
  444. if (typeof api !== 'function') return;
  445. const { prev, next, current } = api(CURRENT) || {};
  446. const prevLink = document.getElementById('prevLink');
  447. const nextLink = document.getElementById('nextLink');
  448. if (prev) document.getElementById('prevTitle').textContent = prev.title || prev.url;
  449. else { prevLink.style.visibility = 'hidden'; }
  450. if (next) document.getElementById('nextTitle').textContent = next.title || next.url;
  451. else { nextLink.style.visibility = 'hidden'; }
  452. document.getElementById('navCenter').textContent = (current && current.group) ? current.group : 'Layer';
  453. }
  454. try { window.parent?.setMiddleActive?.(CURRENT); } catch {}
  455. syncNavLabels();
  456. </script>
  457. <script>
  458. // NOTE: xjs.js bundles an older Layer in some builds.
  459. // For local testing, load standalone layer.js last to ensure this page uses the edited Layer implementation.
  460. (async () => {
  461. const base = '../../';
  462. // Important:
  463. // - Do NOT load `xjs.js` dev-loader here, because it internally loads `layer.js` again
  464. // (without cache-busting) and may overwrite the fresh one after our load finishes.
  465. // - Load sources directly with cache-bust to guarantee we use the current workspace code.
  466. await loadScript(base + 'animal.js?_=' + DEV_CACHE_BUST);
  467. try {
  468. if (window.animal && !window.xjs) window.xjs = window.animal;
  469. if (window.xjs && !window.$) window.$ = window.xjs;
  470. } catch {}
  471. await loadScript(base + 'layer.js?_=' + DEV_CACHE_BUST);
  472. bindDemoHandlers();
  473. })().catch((e) => {
  474. try { console.error(e); } catch {}
  475. });
  476. </script>
  477. </body>
  478. </html>