|
| 1 | +import { |
| 2 | + buildBlock, decorateBlock, decorateIcons, loadBlock, |
| 3 | +} from '../../scripts/aem.js'; |
| 4 | + |
| 5 | +const createResults = async (block) => { |
| 6 | + const section = block.closest('.section'); |
| 7 | + if (section.querySelector('.results-wrapper')) { |
| 8 | + section.querySelector('.results-wrapper').remove(); |
| 9 | + } |
| 10 | + |
| 11 | + const v = block.querySelector('.analyze-box').value; |
| 12 | + const a = document.createElement('a'); |
| 13 | + a.textContent = v; |
| 14 | + a.href = v; |
| 15 | + const results = buildBlock('results', a); |
| 16 | + results.dataset.forceUpdate = block.querySelector('.force-update').checked; |
| 17 | + results.dataset.sitemapUrl = block.querySelector('.sitemap-box').value; |
| 18 | + results.dataset.filterPath = block.querySelector('.path-box').value; |
| 19 | + const wrapper = document.createElement('div'); |
| 20 | + wrapper.style.display = 'none'; |
| 21 | + wrapper.append(results); |
| 22 | + section.append(wrapper); |
| 23 | + decorateBlock(results); |
| 24 | + await loadBlock(results); |
| 25 | + wrapper.style.display = null; |
| 26 | +}; |
| 27 | + |
| 28 | +function restoreFormState(form, powerScoreParams) { |
| 29 | + const analyzeBox = form.querySelector('#analyze-box'); |
| 30 | + analyzeBox.value = powerScoreParams.origin; |
| 31 | + |
| 32 | + const forceCbox = form.querySelector('#force-cbox'); |
| 33 | + forceCbox.checked = powerScoreParams.forceUpdate; |
| 34 | + const sitemapBox = form.querySelector('#sitemap-box'); |
| 35 | + sitemapBox.value = powerScoreParams.sitemapUrl; |
| 36 | + const pathBox = form.querySelector('#path-box'); |
| 37 | + pathBox.value = powerScoreParams.path; |
| 38 | + |
| 39 | + if (powerScoreParams.forceUpdate || powerScoreParams.sitemapUrl || powerScoreParams.path) { |
| 40 | + const settings = form.querySelector('.advanced-settings'); |
| 41 | + settings.setAttribute('aria-expanded', true); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +function restoreState(form) { |
| 46 | + const urlParams = new URLSearchParams(window.location.search); |
| 47 | + const powerScoreUrl = urlParams.get('powerScoreUrl'); |
| 48 | + if (powerScoreUrl) { |
| 49 | + const powerScoreParams = { |
| 50 | + origin: powerScoreUrl, |
| 51 | + sitemapUrl: urlParams.get('sitemapUrl'), |
| 52 | + forceUpdate: urlParams.get('forceUpdate') === 'true', |
| 53 | + path: urlParams.get('filterPath'), |
| 54 | + }; |
| 55 | + restoreFormState(form, powerScoreParams); |
| 56 | + |
| 57 | + // start if query parameter |
| 58 | + if (powerScoreUrl) { |
| 59 | + setTimeout(() => { |
| 60 | + form.submit(); |
| 61 | + }, 500); |
| 62 | + } |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + const sessionStorageState = sessionStorage.getItem('powerScoreParams'); |
| 67 | + if (sessionStorageState) { |
| 68 | + const powerScoreParams = JSON.parse(sessionStorageState); |
| 69 | + restoreFormState(form, powerScoreParams); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * decorate the block |
| 75 | + * @param {Element} block the block element |
| 76 | + */ |
| 77 | +export default function decorate(block) { |
| 78 | + block.innerHTML = ` |
| 79 | + <form id="analyze-url-form"> |
| 80 | + <div class="field-wrapper input-field-wrapper analyze-box-wrapper"> |
| 81 | + <input id="analyze-box" aria-label="URL" class="analyze-box" type="url" required placeholder="Enter site root URL"></input> |
| 82 | + <div class="input-error"> |
| 83 | + <p>Please enter a valid URL</p> |
| 84 | + </div> |
| 85 | + </div> |
| 86 | + <div class="field-wrapper submit-wrapper"> |
| 87 | + <button type="submit">Analyze</button> |
| 88 | + </div> |
| 89 | + <fieldset class="advanced-settings" aria-expanded="false"> |
| 90 | + <legend><span class="icon icon-settings"></span><span>Settings</span></legend> |
| 91 | + <div class="settings-wrapper"> |
| 92 | + <div class="field-wrapper force-wrapper"> |
| 93 | + <label for="force-cbox">Force Update?</label> |
| 94 | + <input id="force-cbox" class="force-update" type="checkbox"></input> |
| 95 | + </div> |
| 96 | + <div class="field-wrapper input-field-wrapper sitemap-box-wrapper"> |
| 97 | + <label for="sitemap-box">Custom Sitemap URL</label> |
| 98 | + <input id="sitemap-box" class="sitemap-box" type="url" placeholder="enter sitemap url if not mentioned in robots.txt"></input> |
| 99 | + </div> |
| 100 | + <div class="field-wrapper input-field-wrapper path-box-wrapper"> |
| 101 | + <label for="path-box">Filter Path</label> |
| 102 | + <input id="path-box" class="path-box" type="text" placeholder="e.g. /blog"></input> |
| 103 | + </div> |
| 104 | + </div> |
| 105 | + </fieldset> |
| 106 | + </div> |
| 107 | +
|
| 108 | + </form> |
| 109 | + `; |
| 110 | + const form = block.querySelector('form'); |
| 111 | + const analyzeBox = form.querySelector('#analyze-box'); |
| 112 | + const settings = form.querySelector('.advanced-settings'); |
| 113 | + const sitemapBox = form.querySelector('#sitemap-box'); |
| 114 | + |
| 115 | + decorateIcons(form); |
| 116 | + settings.querySelector('legend').addEventListener('click', () => { |
| 117 | + const expanded = settings.getAttribute('aria-expanded') === 'true'; |
| 118 | + settings.setAttribute('aria-expanded', !expanded); |
| 119 | + }); |
| 120 | + |
| 121 | + const urlBoxBlur = (e) => { |
| 122 | + const box = e.target; |
| 123 | + if ( |
| 124 | + box.value && box.value.trim() |
| 125 | + && !( |
| 126 | + box.value.startsWith('http://') |
| 127 | + || box.value.startsWith('https://') |
| 128 | + ) |
| 129 | + ) { |
| 130 | + box.value = `https://${box.value}`; |
| 131 | + } |
| 132 | + box.classList.add('visited'); |
| 133 | + }; |
| 134 | + analyzeBox.addEventListener('blur', urlBoxBlur); |
| 135 | + sitemapBox.addEventListener('blur', urlBoxBlur); |
| 136 | + analyzeBox.addEventListener('keyup', (e) => { |
| 137 | + if (e.code === 'Enter') { |
| 138 | + urlBoxBlur(e); |
| 139 | + } |
| 140 | + }); |
| 141 | + |
| 142 | + form.addEventListener('submit', (e) => { |
| 143 | + e.preventDefault(); |
| 144 | + if (form.checkValidity()) { |
| 145 | + createResults(block); |
| 146 | + } |
| 147 | + }); |
| 148 | + restoreState(form); |
| 149 | + block.scrollIntoView(); |
| 150 | +} |
0 commit comments