test_dom_steps.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. <style>
  9. .row {
  10. display: flex;
  11. flex-wrap: wrap;
  12. gap: 10px;
  13. align-items: center;
  14. }
  15. .btn {
  16. appearance: none;
  17. border: 1px solid rgba(255,255,255,0.12);
  18. background: rgba(255,255,255,0.04);
  19. color: #fff;
  20. border-radius: 999px;
  21. padding: 10px 14px;
  22. font-weight: 650;
  23. cursor: pointer;
  24. transition: border-color 0.15s ease, background 0.15s ease;
  25. }
  26. .btn:hover { border-color: rgba(255,255,255,0.22); background: rgba(255,255,255,0.06); }
  27. .btn.primary { border-color: rgba(63,195,238,0.55); background: rgba(63,195,238,0.12); }
  28. .btn.success { border-color: rgba(165,220,134,0.55); background: rgba(165,220,134,0.12); }
  29. .demo-visual { padding: 22px 24px; }
  30. .hint { font-size: 12px; color: #8c8c8c; line-height: 1.5; margin-top: 10px; }
  31. /* The DOM blocks below are hidden by default. Layer will move them into popup and temporarily show them. */
  32. .hidden-dom { display: none; }
  33. .form {
  34. width: 100%;
  35. text-align: left;
  36. display: grid;
  37. gap: 10px;
  38. }
  39. .field label {
  40. display: block;
  41. font-size: 12px;
  42. color: #777;
  43. margin-bottom: 6px;
  44. }
  45. .field input, .field select {
  46. width: 100%;
  47. box-sizing: border-box;
  48. padding: 10px 12px;
  49. border-radius: 10px;
  50. border: 1px solid rgba(0,0,0,0.12);
  51. outline: none;
  52. background: #fff;
  53. color: #111;
  54. font-size: 14px;
  55. }
  56. .error {
  57. color: #c0392b;
  58. font-size: 12px;
  59. min-height: 16px;
  60. }
  61. pre {
  62. width: 100%;
  63. padding: 12px;
  64. border-radius: 12px;
  65. background: rgba(0,0,0,0.06);
  66. overflow: auto;
  67. }
  68. </style>
  69. </head>
  70. <body>
  71. <div class="container">
  72. <div class="page-top">
  73. <div class="crumb">UI › LAYER</div>
  74. <div class="since">DOM + STEPS</div>
  75. </div>
  76. <h1>DOM content + Step flow</h1>
  77. <p class="description">
  78. 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>.
  79. </p>
  80. <div class="box-container">
  81. <div class="box-header">
  82. <div class="box-title">Example</div>
  83. <div class="tabs">
  84. <div class="tab active" onclick="switchTab('js')">JavaScript</div>
  85. <div class="tab" onclick="switchTab('html')">HTML</div>
  86. <div class="tab" onclick="switchTab('css')">CSS</div>
  87. </div>
  88. </div>
  89. <pre id="js-code" class="code-view active">// 1) Single popup: show a hidden DOM block inside Layer
  90. xjs.layer({
  91. title: 'DOM content',
  92. dom: '#demo_dom_block',
  93. showCancelButton: true,
  94. confirmButtonText: 'OK',
  95. cancelButtonText: 'Close'
  96. });
  97. // 2) Step flow (wizard)
  98. xjs.Layer.$({
  99. icon: 'info',
  100. closeOnClickOutside: false,
  101. closeOnEsc: false,
  102. confirmButtonColor: '#3085d6',
  103. cancelButtonColor: '#444'
  104. })
  105. .step({
  106. title: 'Step 1: Basic info',
  107. dom: '#wizard_step_1',
  108. showCancelButton: true,
  109. cancelButtonText: 'Cancel',
  110. preConfirm(popup) {
  111. const name = popup.querySelector('input[name="name"]').value.trim();
  112. const err = popup.querySelector('[data-error]');
  113. if (!name) { err.textContent = 'Please enter your name.'; return false; }
  114. err.textContent = '';
  115. return { name };
  116. }
  117. })
  118. .step({
  119. title: 'Step 2: Preferences',
  120. dom: '#wizard_step_2',
  121. preConfirm(popup) {
  122. const plan = popup.querySelector('select[name="plan"]').value;
  123. return { plan };
  124. }
  125. })
  126. .fire()
  127. .then((res) =&gt; {
  128. if (res.isConfirmed) {
  129. xjs.layer({
  130. title: 'Done',
  131. icon: 'success',
  132. html: '&lt;pre&gt;' + JSON.stringify(res.value, null, 2) + '&lt;/pre&gt;'
  133. });
  134. }
  135. });</pre>
  136. <pre id="html-code" class="html-view">&lt;button class="btn primary" onclick="openDom()"&gt;Open DOM popup&lt;/button&gt;
  137. &lt;button class="btn success" onclick="openWizard()"&gt;Open step wizard&lt;/button&gt;
  138. &lt;!-- Hidden DOM blocks (default display:none) --&gt;
  139. &lt;div id="demo_dom_block" class="hidden-dom"&gt;...&lt;/div&gt;
  140. &lt;div id="wizard_step_1" class="hidden-dom"&gt;...&lt;/div&gt;
  141. &lt;div id="wizard_step_2" class="hidden-dom"&gt;...&lt;/div&gt;</pre>
  142. <pre id="css-code" class="css-view">/* This page hides DOM blocks by default:
  143. .hidden-dom { display: none; }
  144. Layer will move the element into the popup while open,
  145. then restore it back (and restore display/hidden state) on close. */</pre>
  146. <div class="feature-desc">
  147. <strong>功能说明:</strong>
  148. <code class="inline">dom</code> 支持把指定 DOM(可默认隐藏)挂进弹窗并在关闭时还原;步骤流通过同一弹窗内平滑切换实现“分步填写”体验,最终 <code class="inline">res.value</code> 返回每一步的 <code class="inline">preConfirm</code> 结果数组。
  149. </div>
  150. <div class="demo-visual">
  151. <div class="row">
  152. <button class="btn primary" onclick="openDom()">Open DOM popup</button>
  153. <button class="btn success" onclick="openWizard()">Open step wizard</button>
  154. </div>
  155. <div class="hint">
  156. Tip: step wizard disables backdrop/ESC close to avoid accidental dismiss.
  157. </div>
  158. </div>
  159. </div>
  160. <!-- Hidden DOM content (these are mounted into Layer) -->
  161. <div id="demo_dom_block" class="hidden-dom">
  162. <div class="form">
  163. <div class="field">
  164. <label>Quick note</label>
  165. <input placeholder="This input lives in a hidden DOM block" value="Hello from DOM!">
  166. </div>
  167. <div class="hint" style="margin-top: -4px;">
  168. This DOM node is moved into the popup and restored on close.
  169. </div>
  170. </div>
  171. </div>
  172. <div id="wizard_step_1" class="hidden-dom">
  173. <div class="form">
  174. <div class="field">
  175. <label>Name</label>
  176. <input name="name" placeholder="Your name">
  177. </div>
  178. <div class="error" data-error></div>
  179. </div>
  180. </div>
  181. <div id="wizard_step_2" class="hidden-dom">
  182. <div class="form">
  183. <div class="field">
  184. <label>Plan</label>
  185. <select name="plan">
  186. <option value="free">Free</option>
  187. <option value="pro">Pro</option>
  188. <option value="team">Team</option>
  189. </select>
  190. </div>
  191. <div class="hint">Confirm will finish the wizard.</div>
  192. </div>
  193. </div>
  194. <div class="doc-nav" aria-label="Previous and next navigation">
  195. <a href="#" id="prevLink" onclick="goPrev(); return false;">
  196. <span><span class="nav-label">Previous</span><br><span class="nav-title" id="prevTitle">—</span></span>
  197. <span aria-hidden="true">←</span>
  198. </a>
  199. <div class="nav-center" id="navCenter">Layer</div>
  200. <a href="#" id="nextLink" onclick="goNext(); return false;">
  201. <span><span class="nav-label">Next</span><br><span class="nav-title" id="nextTitle">—</span></span>
  202. <span aria-hidden="true">→</span>
  203. </a>
  204. </div>
  205. </div>
  206. <script src="../highlight_css.js"></script>
  207. <!-- NOTE: xjs.js bundles an older Layer in some builds.
  208. For local testing, load standalone layer.js last to ensure this page uses the edited Layer implementation. -->
  209. <script src="../../xjs.js?v=dev"></script>
  210. <script src="../../layer.js?v=dev"></script>
  211. <script>
  212. const CURRENT = 'layer/test_dom_steps.html';
  213. function switchTab(tab) {
  214. document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
  215. document.querySelectorAll('.code-view, .html-view, .css-view').forEach(v => v.classList.remove('active'));
  216. if (tab === 'js') {
  217. document.querySelector('.tabs .tab:nth-child(1)')?.classList.add('active');
  218. document.getElementById('js-code')?.classList.add('active');
  219. } else if (tab === 'html') {
  220. document.querySelector('.tabs .tab:nth-child(2)')?.classList.add('active');
  221. document.getElementById('html-code')?.classList.add('active');
  222. } else {
  223. document.querySelector('.tabs .tab:nth-child(3)')?.classList.add('active');
  224. document.getElementById('css-code')?.classList.add('active');
  225. }
  226. }
  227. function openDom() {
  228. xjs.layer({
  229. title: 'DOM content',
  230. dom: '#demo_dom_block',
  231. showCancelButton: true,
  232. confirmButtonText: 'OK',
  233. cancelButtonText: 'Close'
  234. });
  235. }
  236. function openWizard() {
  237. xjs.Layer.$({
  238. icon: 'info',
  239. closeOnClickOutside: false,
  240. closeOnEsc: false,
  241. confirmButtonColor: '#3085d6',
  242. cancelButtonColor: '#444'
  243. })
  244. .step({
  245. title: 'Step 1: Basic info',
  246. dom: '#wizard_step_1',
  247. showCancelButton: true,
  248. cancelButtonText: 'Cancel',
  249. preConfirm(popup) {
  250. const name = popup.querySelector('input[name="name"]').value.trim();
  251. const err = popup.querySelector('[data-error]');
  252. if (!name) { err.textContent = 'Please enter your name.'; return false; }
  253. err.textContent = '';
  254. return { name };
  255. }
  256. })
  257. .step({
  258. title: 'Step 2: Preferences',
  259. dom: '#wizard_step_2',
  260. preConfirm(popup) {
  261. const plan = popup.querySelector('select[name="plan"]').value;
  262. return { plan };
  263. }
  264. })
  265. .fire()
  266. .then((res) => {
  267. if (res.isConfirmed) {
  268. xjs.layer({
  269. title: 'Done',
  270. icon: 'success',
  271. html: '<pre>' + JSON.stringify(res.value, null, 2) + '</pre>'
  272. });
  273. } else {
  274. xjs.layer({ title: 'Dismissed', icon: 'info', text: 'Flow cancelled' });
  275. }
  276. });
  277. }
  278. function goPrev() { window.parent?.docNavigatePrev?.(CURRENT); }
  279. function goNext() { window.parent?.docNavigateNext?.(CURRENT); }
  280. function syncNavLabels() {
  281. const api = window.parent?.docGetPrevNext;
  282. if (typeof api !== 'function') return;
  283. const { prev, next, current } = api(CURRENT) || {};
  284. const prevLink = document.getElementById('prevLink');
  285. const nextLink = document.getElementById('nextLink');
  286. if (prev) document.getElementById('prevTitle').textContent = prev.title || prev.url;
  287. else { prevLink.style.visibility = 'hidden'; }
  288. if (next) document.getElementById('nextTitle').textContent = next.title || next.url;
  289. else { nextLink.style.visibility = 'hidden'; }
  290. document.getElementById('navCenter').textContent = (current && current.group) ? current.group : 'Layer';
  291. }
  292. try { window.parent?.setMiddleActive?.(CURRENT); } catch {}
  293. syncNavLabels();
  294. </script>
  295. </body>
  296. </html>