function ensurePcTypeSelectionModalOnBody() { var modals = document.querySelectorAll('#pcTypeSelectionModal'); if (modals.length > 1) { for (var di = 1; di < modals.length; di++) { modals[di].parentNode.removeChild(modals[di]); } } var modalEl = document.getElementById('pcTypeSelectionModal'); if (modalEl && modalEl.parentElement !== document.body) { document.body.appendChild(modalEl); } } window.ensurePcTypeSelectionModalOnBody = ensurePcTypeSelectionModalOnBody; function bindPcTypeSelectionFocusTrap($modal) { $modal.off('keydown.pmsPcTypeFocusTrap'); $modal.on('keydown.pmsPcTypeFocusTrap', function(event) { if (event.key !== 'Tab') { return; } var $focusable = $modal.find( 'button:not([disabled]), [href], input:not([disabled]):not([type="hidden"]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])' ).filter(':visible'); if (!$focusable.length) { return; } var first = $focusable.first()[0]; var last = $focusable.last()[0]; if (event.shiftKey && document.activeElement === first) { event.preventDefault(); last.focus(); } else if (!event.shiftKey && document.activeElement === last) { event.preventDefault(); first.focus(); } }); } function isPcTypeSelectionStackedContext() { return $('#projectDetailsModal').hasClass('show'); } window.pmsOpenPcTypeSelectionModal = function(options) { options = options || {}; ensurePcTypeSelectionModalOnBody(); var $modal = $('#pcTypeSelectionModal'); var stacked = options.stacked === true || (options.stacked !== false && isPcTypeSelectionStackedContext()); if (stacked) { document.body.classList.add('pc-type-selection-stacked-open'); } else { document.body.classList.remove('pc-type-selection-stacked-open'); } if ($modal.hasClass('show')) { return; } $modal.modal({ backdrop: true, keyboard: true, focus: true, show: true }); }; function setPcTypeSelectionLoadingState(message) { window.__pmsPcRevFlags = null; $('input[name="pc_type_choice"]').prop('checked', false); $('#pcTypeSelectionProceed').prop('disabled', true); $('#pcTypeSelectionHelp').text(''); // Hide create cards until eligibility returns — avoids flash of PC-II…PC-V on UR/revised schemes. $('#pcTypeSelectionModal .pc-type-card-item').hide(); setPcTypeCardEnabled($('#pcTypeOptionConcept'), false); setPcTypeCardEnabled($('#pcTypeOption1'), false); setPcTypeCardEnabled($('#pcTypeOption2'), false); setPcTypeCardEnabled($('#pcTypeOption3'), false); setPcTypeCardEnabled($('#pcTypeOption4'), false); setPcTypeCardEnabled($('#pcTypeOption5'), false); } /** * Prepare project context and open Select PC (scheme details → Generate PC). * * @param {string|number|null} projectId */ window.pmsPrepareAndOpenPcTypeSelectionModal = function(projectId) { ensurePcTypeSelectionModalOnBody(); $('input[name="pc_type_choice"]').prop('checked', false); $('#pcTypeSelectionProceed').prop('disabled', true); if (projectId != null && String(projectId) !== '') { $('#pcTypeSelectionProjectId').val(String(projectId)); // Scheme-scoped: do not enable all cards before eligibility API applies. setPcTypeSelectionLoadingState(); } else { $('#pcTypeSelectionProjectId').val(''); $('#pcTypeSelectionHelp').text(''); resetPcTypeSelectionModalLabels(); } window.pmsOpenPcTypeSelectionModal({ stacked: isPcTypeSelectionStackedContext() }); }; $('#pcTypeSelectionModal').on('show.bs.modal', function() { document.body.classList.add('pc-type-selection-modal-active'); if (isPcTypeSelectionStackedContext()) { document.body.classList.add('pc-type-selection-stacked-open'); } }); function syncPcTypeModalBackdropStack() { var $backdrops = $('body > .modal-backdrop'); var modalCount = $('.modal.show').length; $backdrops.each(function(index) { var $backdrop = $(this); $backdrop.css('z-index', ''); if (document.body.classList.contains('pc-type-selection-stacked-open')) { $backdrop.css('z-index', index === 0 ? 1045 : 1080); } else if (index === $backdrops.length - 1) { $backdrop.css('z-index', 1045); } }); if (modalCount > 0 && $backdrops.length > modalCount) { $backdrops.slice(modalCount).remove(); } } function cleanupPcTypeModalUiState() { document.body.classList.remove('pc-type-selection-modal-active'); document.body.classList.remove('pc-type-selection-stacked-open'); $('#pcTypeSelectionModal').css('z-index', '').off('keydown.pmsPcTypeFocusTrap'); var openModals = $('.modal.show').length; var backdrops = $('body > .modal-backdrop').length; if (openModals === 0 && backdrops > 0) { $('body > .modal-backdrop').remove(); $('body').removeClass('modal-open').css({ overflow: '', 'padding-right': '' }); } else if (openModals > 0 && backdrops > openModals) { $('body > .modal-backdrop').slice(openModals).remove(); } if ($('#projectDetailsModal').hasClass('show')) { $('body').addClass('modal-open'); } } $('#pcTypeSelectionModal').on('shown.bs.modal', function() { var $modal = $('#pcTypeSelectionModal'); if (isPcTypeSelectionStackedContext()) { document.body.classList.add('pc-type-selection-stacked-open'); } syncPcTypeModalBackdropStack(); bindPcTypeSelectionFocusTrap($modal); var $firstChoice = $modal.find('.pc-type-card-input:not(:disabled)').first(); if ($firstChoice.length) { $firstChoice.focus(); } else { $modal.find('.close').trigger('focus'); } }); $('#pcTypeSelectionModal').on('hidden.bs.modal.pmsPcTypeUi', function() { cleanupPcTypeModalUiState(); }); var PMS_CAN_CREATE_CONCEPT_NOTE =