| @@ -1,5 +1,10 @@ | ||
| 1 | -(function ($) { | |
| 1 | +(function (factory) { | |
| 2 | + if (typeof window.jQuery === 'undefined') { | |
| 3 | + return; | |
| 4 | + } | |
| 5 | + factory(window.jQuery); | |
| 6 | +})(function ($) { | |
| 2 | 7 | 'use strict'; |
| 3 | 8 | |
| 4 | 9 | const $rulesContainer = $('#ka-woo-rules'); |
| 5 | 10 | if (!$rulesContainer.length || typeof $.fn.select2 === 'undefined') { |
| @@ -20,8 +25,9 @@ | ||
| 20 | 25 | const endpoints = { |
| 21 | 26 | product_in: {url: restRoot + 'kingaddons/v1/ajaxselect2/getPostsByPostType/', params: {query_slug: 'product'}}, |
| 22 | 27 | product_cat_in: {url: restRoot + 'kingaddons/v1/ajaxselect2/getTaxonomies/', params: {query_slug: 'product_cat'}}, |
| 23 | 28 | product_cat_archive_in: {url: restRoot + 'kingaddons/v1/ajaxselect2/getTaxonomies/', params: {query_slug: 'product_cat'}}, |
| 29 | + product_tag_archive_in: {url: restRoot + 'kingaddons/v1/ajaxselect2/getTaxonomies/', params: {query_slug: 'product_tag'}}, | |
| 24 | 30 | product_tag_in: {url: restRoot + 'kingaddons/v1/ajaxselect2/getTaxonomies/', params: {query_slug: 'product_tag'}}, |
| 25 | 31 | products: {url: restRoot + 'kingaddons/v1/ajaxselect2/getPostsByPostType/', params: {query_slug: 'product'}}, |
| 26 | 32 | product_categories: {url: restRoot + 'kingaddons/v1/ajaxselect2/getTaxonomies/', params: {query_slug: 'product_cat'}}, |
| 27 | 33 | product_cat_archives: {url: restRoot + 'kingaddons/v1/ajaxselect2/getTaxonomies/', params: {query_slug: 'product_cat'}}, |
| @@ -131,28 +137,52 @@ | ||
| 131 | 137 | } |
| 132 | 138 | }); |
| 133 | 139 | |
| 134 | 140 | rebuildIndexes(); |
| 135 | -})(jQuery); | |
| 141 | +}); | |
| 136 | 142 | |
| 137 | 143 | (function () { |
| 138 | 144 | 'use strict'; |
| 139 | 145 | |
| 140 | - const defaultPanels = ['#customer_details', '.woocommerce-checkout-payment', '.woocommerce-checkout-review-order']; | |
| 146 | + const defaultPanels = [ | |
| 147 | + '.woocommerce-billing-fields', | |
| 148 | + '.woocommerce-shipping-fields', | |
| 149 | + '#payment', | |
| 150 | + '.woocommerce-checkout-review-order-table', | |
| 151 | + ]; | |
| 141 | 152 | |
| 142 | 153 | const resolvePanels = (container) => { |
| 143 | - const customSelectors = Array.from(container.querySelectorAll('.ka-woo-checkout-step')).map((step) => step.dataset.target || ''); | |
| 144 | - const mapped = customSelectors.some(Boolean) ? customSelectors : defaultPanels; | |
| 145 | - return mapped.map((selector, idx) => ({ selector, idx })); | |
| 154 | + const steps = Array.from(container.querySelectorAll('.ka-woo-checkout-step')); | |
| 155 | + return steps.map((step, idx) => ({ | |
| 156 | + selector: (step.dataset.target || '').trim() || defaultPanels[idx] || '', | |
| 157 | + idx, | |
| 158 | + })); | |
| 146 | 159 | }; |
| 147 | 160 | |
| 161 | + const revealWithAncestors = (el) => { | |
| 162 | + if (!el) { | |
| 163 | + return; | |
| 164 | + } | |
| 165 | + el.classList.remove('ka-woo-hidden'); | |
| 166 | + let parent = el.parentElement; | |
| 167 | + while (parent && parent !== document.body) { | |
| 168 | + parent.classList.remove('ka-woo-hidden'); | |
| 169 | + parent = parent.parentElement; | |
| 170 | + } | |
| 171 | + }; | |
| 172 | + | |
| 148 | 173 | const togglePanels = (stepsMap, activeIndex, scroll) => { |
| 174 | + const activeSelector = stepsMap[activeIndex] ? stepsMap[activeIndex].selector : ''; | |
| 175 | + const activeEls = activeSelector ? Array.from(document.querySelectorAll(activeSelector)) : []; | |
| 176 | + | |
| 149 | 177 | stepsMap.forEach((item, idx) => { |
| 150 | 178 | if (!item.selector) { |
| 151 | 179 | return; |
| 152 | 180 | } |
| 153 | 181 | document.querySelectorAll(item.selector).forEach((el) => { |
| 154 | - if (idx === activeIndex) { | |
| 182 | + const isActive = idx === activeIndex; | |
| 183 | + const isAncestorOfActive = activeEls.some((active) => el.contains(active)); | |
| 184 | + if (isActive || isAncestorOfActive) { | |
| 155 | 185 | el.classList.remove('ka-woo-hidden'); |
| 156 | 186 | } else { |
| 157 | 187 | el.classList.add('ka-woo-hidden'); |
| 158 | 188 | } |
| @@ -158,11 +188,12 @@ | ||
| 158 | 188 | } |
| 159 | 189 | }); |
| 160 | 190 | }); |
| 161 | 191 | |
| 192 | + activeEls.forEach(revealWithAncestors); | |
| 193 | + | |
| 162 | 194 | if (scroll && scroll === 'yes') { |
| 163 | - const targetSelector = stepsMap[activeIndex] ? stepsMap[activeIndex].selector : null; | |
| 164 | - const target = targetSelector ? document.querySelector(targetSelector) : null; | |
| 195 | + const target = activeEls[0] || null; | |
| 165 | 196 | if (target) { |
| 166 | 197 | target.scrollIntoView({ behavior: 'smooth', block: 'start' }); |
| 167 | 198 | } |
| 168 | 199 | } |
| @@ -169,8 +200,12 @@ | ||
| 169 | 200 | }; |
| 170 | 201 | |
| 171 | 202 | const initSteps = () => { |
| 172 | 203 | document.querySelectorAll('.ka-woo-checkout-steps--pro').forEach((container) => { |
| 204 | + if (container.dataset.kaCheckoutStepsInit === '1') { | |
| 205 | + return; | |
| 206 | + } | |
| 207 | + container.dataset.kaCheckoutStepsInit = '1'; | |
| 173 | 208 | const steps = Array.from(container.querySelectorAll('.ka-woo-checkout-step')); |
| 174 | 209 | if (!steps.length) { |
| 175 | 210 | return; |
| 176 | 211 | } |
| @@ -206,12 +241,15 @@ | ||
| 206 | 241 | }); |
| 207 | 242 | return valid; |
| 208 | 243 | }; |
| 209 | 244 | |
| 210 | - const setActive = (idx) => { | |
| 211 | - current = idx; | |
| 212 | - steps.forEach((step, i) => step.classList.toggle('is-active', i === current)); | |
| 213 | - togglePanels(stepsMap, current, scrollTop); | |
| 245 | + const applyCurrent = (scroll) => { | |
| 246 | + const map = resolvePanels(container); | |
| 247 | + steps.forEach((step, i) => { | |
| 248 | + step.classList.toggle('is-active', i === current); | |
| 249 | + step.classList.toggle('is-complete', i < current); | |
| 250 | + }); | |
| 251 | + togglePanels(map, current, scroll); | |
| 214 | 252 | if (enableNav && navPrev && navNext) { |
| 215 | 253 | navPrev.disabled = current === 0; |
| 216 | 254 | const isLast = current >= steps.length - 1; |
| 217 | 255 | navNext.dataset.isLast = isLast ? 'true' : 'false'; |
| @@ -225,8 +263,15 @@ | ||
| 225 | 263 | } |
| 226 | 264 | steps.forEach((step) => step.classList.remove(errorClass)); |
| 227 | 265 | }; |
| 228 | 266 | |
| 267 | + const setActive = (idx) => { | |
| 268 | + current = idx; | |
| 269 | + applyCurrent(scrollTop); | |
| 270 | + }; | |
| 271 | + | |
| 272 | + container.kaApplyCheckoutStep = () => applyCurrent('no'); | |
| 273 | + | |
| 229 | 274 | steps.forEach((step, idx) => { |
| 230 | 275 | step.addEventListener('click', () => setActive(idx)); |
| 231 | 276 | }); |
| 232 | 277 | |
| @@ -399,10 +444,19 @@ | ||
| 399 | 444 | initSteps(); |
| 400 | 445 | initSticky(); |
| 401 | 446 | initPayments(); |
| 402 | 447 | }); |
| 448 | + | |
| 449 | + if (window.jQuery) { | |
| 450 | + window.jQuery(document.body).on('updated_checkout', function () { | |
| 451 | + document.querySelectorAll('.ka-woo-checkout-steps--pro').forEach((container) => { | |
| 452 | + if (typeof container.kaApplyCheckoutStep === 'function') { | |
| 453 | + container.kaApplyCheckoutStep(); | |
| 454 | + } | |
| 455 | + }); | |
| 456 | + }); | |
| 457 | + } | |
| 403 | 458 | })(); |
| 404 | - | |
| 405 | 459 | |
| 406 | 460 | |
| 407 | 461 | |
| 408 | 462 | |