test_dom_steps.html 12 KB

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