test_dom_steps.html 14 KB

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