import eventBus from "../eventBus.js"; import ApiMixin from '../Api/ApiMixin.js'; import {validate} from "../Plugins/Validation.js"; import {loadStart, loadEnd} from "../Plugins/Loader.js"; const template = ` `; const Modals = { template, mixins: [ApiMixin], data() { return { totalTrackedResources: 0, totalUntrackedResources: 100, openCreateGroupModal: false, openSyncModal: false, openCancelSyncModal: false, openResetModal: false, openDebugLogModal: false, progressPercentage: 0, intervalId: null, createGroupName: "", filteredLogs: [], searchLogs: "", copyMessage: "", resetSyncButtonRef: null, } }, computed: { remainingUntrackedResources() { return this.totalUntrackedResources - this.totalTrackedResources; }, }, mounted() { eventBus.on('show-create-group-modal', (data) => { this.openCreateGroupModal = true }); eventBus.on('show-sync-modal', (data) => { this.openSyncModal = true // TODO - send sync to background and continue setup this.simProgress() }); eventBus.on('show-cancel-sync-modal', (data) => { this.openCancelSyncModal = true }); eventBus.on('show-reset-modal', (ref) => { this.openResetModal = true }); eventBus.on('show-debug-modal', async (ref) => { try { loadStart(ref); const data = await this.getDebugLog() if (data.success) { this.debugLogs = data.log ? data.log.split('\n') : "No Logs Found" this.filteredLogs = [...this.debugLogs]; this.openDebugLogModal = true } else { eventBus.emit('alert-event', { error: 'An error occurred' }); } loadEnd(ref); } catch (err) { this.isLoading = false eventBus.emit('alert-event', { error: 'An error occurred' }); loadEnd(ref); } }); }, methods: { simProgress() { if (this.intervalId) return; this.progressPercentage = 0; this.totalTrackedResources = 0; this.intervalId = setInterval(() => { if (this.totalTrackedResources < this.totalUntrackedResources) { this.totalTrackedResources += 10; this.progressPercentage = Math.round( (this.totalTrackedResources / this.totalUntrackedResources) * 100 ); } else { clearInterval(this.intervalId); // stop once it reaches 100 this.intervalId = null; // move to settings and close modal eventBus.emit('wizard-step-event', 2) this.openSyncModal = false } }, 500); }, async createGroup() { loadStart(this.$refs.createGroup); const groupNameValid = validate(this.$refs.wooMlCreateGroup, ['required', 'minLength:3'], { required: 'Group name is required.', }); if (groupNameValid) { const response = await this.createGroupApi(this.createGroupName) if (response.success) { eventBus.emit('trigger-group-created', response.data) } } loadEnd(this.$refs.createGroup); this.openCreateGroupModal = false }, resetIntegration() { eventBus.emit('reset-integration'); }, filterLogs() { const term = this.searchLogs.toLowerCase(); this.filteredLogs = this.debugLogs.filter(line => line.toLowerCase().includes(term) ); }, // Highlight search term in logs highlightSearch(line) { if (!this.searchLogs) return line; if (typeof line !== 'string') return line; const term = this.searchLogs.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&'); // Escape special characters const regex = new RegExp(`(${term})`, 'gi'); return line.replace(regex, '$1'); }, copyDebugLogToClipboard() { if (navigator.clipboard && window.isSecureContext) { // Modern method (HTTPS only) navigator.clipboard.writeText(this.filteredLogs.join('\n')).then(() => { this.copyMessage = 'Copied!'; setTimeout(() => { this.copyMessage = ""; }, 2000); }).catch(err => { }); } else { const textarea = document.createElement("textarea"); textarea.value = this.filteredLogs.join('\n'); document.body.appendChild(textarea); textarea.select(); document.execCommand("copy"); document.body.removeChild(textarea); this.copyMessage = 'Copied!'; setTimeout(() => { this.copyMessage = ""; }, 2000); } }, } }; export default Modals;