test_dom_steps.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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) - no icon during steps; final summary is allowed
  98. xjs.Layer.$({
  99. closeOnClickOutside: false,
  100. closeOnEsc: false,
  101. confirmButtonColor: '#3085d6',
  102. cancelButtonColor: '#444'
  103. })
  104. .step({
  105. title: 'Step 1: Basic info',
  106. dom: '#wizard_step_1',
  107. showCancelButton: true,
  108. cancelButtonText: 'Cancel',
  109. preConfirm(popup) {
  110. const name = popup.querySelector('input[name="name"]').value.trim();
  111. const err = popup.querySelector('[data-error]');
  112. if (!name) { err.textContent = 'Please enter your name.'; return false; }
  113. err.textContent = '';
  114. return { name };
  115. }
  116. })
  117. .step({
  118. title: 'Step 2: Preferences',
  119. dom: '#wizard_step_2',
  120. preConfirm(popup) {
  121. const plan = popup.querySelector('select[name="plan"]').value;
  122. return { plan };
  123. }
  124. })
  125. .step({
  126. title: 'Summary',
  127. icon: 'success',
  128. isSummary: true,
  129. html: '&lt;div class="hint"&gt;Click OK to finish.&lt;/div&gt;'
  130. })
  131. .fire()
  132. .then((res) =&gt; {
  133. if (res.isConfirmed) {
  134. xjs.layer({
  135. title: 'Done',
  136. icon: 'success',
  137. html: '&lt;pre&gt;' + JSON.stringify(res.value, null, 2) + '&lt;/pre&gt;'
  138. });
  139. }
  140. });</pre>
  141. <pre id="html-code" class="html-view">&lt;button class="btn primary" onclick="openDom()"&gt;Open DOM popup&lt;/button&gt;
  142. &lt;button class="btn success" onclick="openWizard()"&gt;Open step wizard&lt;/button&gt;
  143. &lt;!-- Hidden DOM blocks (default display:none) --&gt;
  144. &lt;div id="demo_dom_block" class="hidden-dom"&gt;...&lt;/div&gt;
  145. &lt;div id="wizard_step_1" class="hidden-dom"&gt;...&lt;/div&gt;
  146. &lt;div id="wizard_step_2" class="hidden-dom"&gt;...&lt;/div&gt;</pre>
  147. <pre id="css-code" class="css-view">/* This page hides DOM blocks by default:
  148. .hidden-dom { display: none; }
  149. Layer will move the element into the popup while open,
  150. then restore it back (and restore display/hidden state) on close. */</pre>
  151. <div class="feature-desc">
  152. <strong>功能说明:</strong>
  153. <code class="inline">dom</code> 支持把指定 DOM(可默认隐藏)挂进弹窗并在关闭时还原;步骤流通过同一弹窗内平滑切换实现“分步填写”体验,最终 <code class="inline">res.value</code> 返回每一步的 <code class="inline">preConfirm</code> 结果数组。
  154. </div>
  155. <div class="demo-visual">
  156. <div class="row">
  157. <button class="btn primary" onclick="openDom()">Open DOM popup</button>
  158. <button class="btn success" onclick="openWizard()">Open step wizard</button>
  159. </div>
  160. <div class="hint">
  161. Tip: step wizard disables backdrop/ESC close to avoid accidental dismiss.
  162. </div>
  163. </div>
  164. </div>
  165. <!-- Hidden DOM content (these are mounted into Layer) -->
  166. <div id="demo_dom_block" class="hidden-dom">
  167. <div class="form">
  168. <div class="field">
  169. <label>Quick note</label>
  170. <input placeholder="This input lives in a hidden DOM block" value="Hello from DOM!">
  171. </div>
  172. <div class="hint" style="margin-top: -4px;">
  173. This DOM node is moved into the popup and restored on close.
  174. </div>
  175. </div>
  176. </div>
  177. <div id="wizard_step_1" class="hidden-dom">
  178. <div class="form">
  179. <div class="field">
  180. <label>Name</label>
  181. <input name="name" placeholder="Your name">
  182. </div>
  183. <div class="error" data-error></div>
  184. </div>
  185. </div>
  186. <div id="wizard_step_2" class="hidden-dom">
  187. <div class="form">
  188. <div class="field">
  189. <label>Plan</label>
  190. <select name="plan">
  191. <option value="free">Free</option>
  192. <option value="pro">Pro</option>
  193. <option value="team">Team</option>
  194. </select>
  195. </div>
  196. <div class="hint">Confirm will finish the wizard.</div>
  197. </div>
  198. </div>
  199. <div class="doc-nav" aria-label="Previous and next navigation">
  200. <a href="#" id="prevLink" onclick="goPrev(); return false;">
  201. <span><span class="nav-label">Previous</span><br><span class="nav-title" id="prevTitle">—</span></span>
  202. <span aria-hidden="true">←</span>
  203. </a>
  204. <div class="nav-center" id="navCenter">Layer</div>
  205. <a href="#" id="nextLink" onclick="goNext(); return false;">
  206. <span><span class="nav-label">Next</span><br><span class="nav-title" id="nextTitle">—</span></span>
  207. <span aria-hidden="true">→</span>
  208. </a>
  209. </div>
  210. </div>
  211. <script src="../highlight_css.js"></script>
  212. <script>
  213. const CURRENT = 'layer/test_dom_steps.html';
  214. // =========================================================
  215. // Dev loader (cache-bust) for local debugging
  216. // - Ensure this page always uses the latest workspace `xjs.js` + `layer.js`
  217. // - Avoid "no change" caused by browser caching `?v=dev`
  218. // =========================================================
  219. const DEV_CACHE_BUST = Date.now();
  220. function loadScript(src) {
  221. return new Promise((resolve, reject) => {
  222. const s = document.createElement('script');
  223. s.src = src;
  224. s.async = false; // preserve order
  225. s.onload = () => resolve();
  226. s.onerror = () => reject(new Error('Failed to load: ' + src));
  227. (document.head || document.documentElement).appendChild(s);
  228. });
  229. }
  230. function switchTab(tab) {
  231. document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
  232. document.querySelectorAll('.code-view, .html-view, .css-view').forEach(v => v.classList.remove('active'));
  233. if (tab === 'js') {
  234. document.querySelector('.tabs .tab:nth-child(1)')?.classList.add('active');
  235. document.getElementById('js-code')?.classList.add('active');
  236. } else if (tab === 'html') {
  237. document.querySelector('.tabs .tab:nth-child(2)')?.classList.add('active');
  238. document.getElementById('html-code')?.classList.add('active');
  239. } else {
  240. document.querySelector('.tabs .tab:nth-child(3)')?.classList.add('active');
  241. document.getElementById('css-code')?.classList.add('active');
  242. }
  243. }
  244. function openDom() {
  245. xjs.layer({
  246. title: 'DOM content',
  247. dom: '#demo_dom_block',
  248. popupAnimation: true,
  249. showCancelButton: true,
  250. confirmButtonText: 'OK',
  251. cancelButtonText: 'Close'
  252. });
  253. }
  254. function openWizard() {
  255. xjs.Layer.$({
  256. closeOnClickOutside: false,
  257. closeOnEsc: false,
  258. popupAnimation: true,
  259. confirmButtonColor: '#3085d6',
  260. cancelButtonColor: '#444'
  261. })
  262. .step({
  263. title: 'Step 1: Basic info',
  264. dom: '#wizard_step_1',
  265. showCancelButton: true,
  266. cancelButtonText: 'Cancel',
  267. preConfirm(popup) {
  268. const name = popup.querySelector('input[name="name"]').value.trim();
  269. const err = popup.querySelector('[data-error]');
  270. if (!name) { err.textContent = 'Please enter your name.'; return false; }
  271. err.textContent = '';
  272. return { name };
  273. }
  274. })
  275. .step({
  276. title: 'Step 2: Preferences',
  277. dom: '#wizard_step_2',
  278. preConfirm(popup) {
  279. const plan = popup.querySelector('select[name="plan"]').value;
  280. return { plan };
  281. }
  282. })
  283. .step({
  284. title: 'Summary',
  285. icon: 'success',
  286. isSummary: true,
  287. html: '<div class="hint">Click OK to finish.</div>'
  288. })
  289. .fire()
  290. .then((res) => {
  291. if (res.isConfirmed) {
  292. xjs.layer({
  293. title: 'Done',
  294. icon: 'success',
  295. html: '<pre>' + JSON.stringify(res.value, null, 2) + '</pre>'
  296. });
  297. } else {
  298. xjs.layer({ title: 'Dismissed', icon: 'info', text: 'Flow cancelled' });
  299. }
  300. });
  301. }
  302. function goPrev() { window.parent?.docNavigatePrev?.(CURRENT); }
  303. function goNext() { window.parent?.docNavigateNext?.(CURRENT); }
  304. function syncNavLabels() {
  305. const api = window.parent?.docGetPrevNext;
  306. if (typeof api !== 'function') return;
  307. const { prev, next, current } = api(CURRENT) || {};
  308. const prevLink = document.getElementById('prevLink');
  309. const nextLink = document.getElementById('nextLink');
  310. if (prev) document.getElementById('prevTitle').textContent = prev.title || prev.url;
  311. else { prevLink.style.visibility = 'hidden'; }
  312. if (next) document.getElementById('nextTitle').textContent = next.title || next.url;
  313. else { nextLink.style.visibility = 'hidden'; }
  314. document.getElementById('navCenter').textContent = (current && current.group) ? current.group : 'Layer';
  315. }
  316. try { window.parent?.setMiddleActive?.(CURRENT); } catch {}
  317. syncNavLabels();
  318. </script>
  319. <script>
  320. // NOTE: xjs.js bundles an older Layer in some builds.
  321. // For local testing, load standalone layer.js last to ensure this page uses the edited Layer implementation.
  322. (async () => {
  323. const base = '../../';
  324. // Important:
  325. // - Do NOT load `xjs.js` dev-loader here, because it internally loads `layer.js` again
  326. // (without cache-busting) and may overwrite the fresh one after our load finishes.
  327. // - Load sources directly with cache-bust to guarantee we use the current workspace code.
  328. await loadScript(base + 'animal.js?_=' + DEV_CACHE_BUST);
  329. try { if (window.animal && !window.xjs) window.xjs = window.animal; } catch {}
  330. await loadScript(base + 'layer.js?_=' + DEV_CACHE_BUST);
  331. })().catch((e) => {
  332. try { console.error(e); } catch {}
  333. });
  334. </script>
  335. </body>
  336. </html>