(function() { 'use strict'; // Methode 1: Suche das Script-Tag mit data-widget-id function getWidgetId() { var scripts = document.querySelectorAll('script[data-widget-id]'); if (scripts.length > 0) { // Finde das Script, das tww-widget-loader.js lädt for (var i = 0; i < scripts.length; i++) { if (scripts[i].src && scripts[i].src.indexOf('tww-widget-loader.js') !== -1) { return scripts[i].getAttribute('data-widget-id'); } } // Falls nicht gefunden, nimm das letzte mit data-widget-id return scripts[scripts.length - 1].getAttribute('data-widget-id'); } // Methode 2: document.currentScript (moderne Browser) if (document.currentScript && document.currentScript.getAttribute('data-widget-id')) { return document.currentScript.getAttribute('data-widget-id'); } // Methode 3: Suche in allen Script-Tags nach diesem Script var allScripts = document.getElementsByTagName('script'); for (var j = allScripts.length - 1; j >= 0; j--) { if (allScripts[j].src && allScripts[j].src.indexOf('tww-widget-loader.js') !== -1) { return allScripts[j].getAttribute('data-widget-id'); } } return null; } var widgetId = getWidgetId(); if (!widgetId) { console.error('TWW Widget: No widget ID specified. Please add data-widget-id="YOUR_ID" to the script tag.'); console.log('Example: '); return; } console.log('TWW Widget: Loading widget ID ' + widgetId); // Widget namespace window.TWWWidget = { widgetId: widgetId, config: null, container: null, overlay: null, minimizedButton: null, isVisible: false, isMinimized: false, init: function(config) { this.config = config; console.log('TWW Widget: Config loaded', config); console.log('TWW Widget: minimize_on_close =', config.minimize_on_close); // Check if widget should be shown (cookie check) if (this.shouldShowWidget()) { this.loadStyles(); this.createWidget(); this.setupEventListeners(); this.scheduleShow(); } }, shouldShowWidget: function() { if (this.config.cookie_duration === 0) { return true; } var cookieName = 'tww_widget_' + this.widgetId + '_closed'; var cookies = document.cookie.split(';'); for (var i = 0; i < cookies.length; i++) { var cookie = cookies[i].trim(); if (cookie.indexOf(cookieName + '=') === 0) { return false; } } return true; }, setCookie: function() { if (this.config.cookie_duration > 0) { var date = new Date(); date.setTime(date.getTime() + (this.config.cookie_duration * 60 * 60 * 1000)); var expires = "expires=" + date.toUTCString(); document.cookie = 'tww_widget_' + this.widgetId + '_closed=1;' + expires + ';path=/'; } }, loadStyles: function() { var link = document.createElement('link'); link.rel = 'stylesheet'; // Cache-Busting: Füge Timestamp hinzu um Browser-Cache zu umgehen link.href = 'https://go.tww.ch/tww-widget-style/' + this.widgetId + '.css?v=' + new Date().getTime(); document.head.appendChild(link); }, createWidget: function() { var self = this; // Create overlay for modal/popup types if (this.config.widget_type === 'popup') { this.overlay = document.createElement('div'); this.overlay.className = 'tww-widget-overlay'; this.overlay.style.display = 'none'; document.body.appendChild(this.overlay); this.overlay.addEventListener('click', function() { self.hideWidget(); }); } // Create main widget container this.container = document.createElement('div'); this.container.className = 'tww-widget tww-widget-' + this.config.widget_type + ' tww-position-' + this.config.position; this.container.setAttribute('data-animation', this.config.animation); this.container.style.display = 'none'; // Build widget content var content = '
'; // Close button if (this.config.close_button) { content += ''; } // Image - nur als Block wenn NICHT als Hintergrund if (this.config.image_url && this.config.image_as_background != 1) { content += '
'; } // Content area content += '
'; if (this.config.title_text) { content += '

' + this.escapeHtml(this.config.title_text) + '

'; } if (this.config.description_text) { content += '

' + this.escapeHtml(this.config.description_text) + '

'; } content += '' + this.escapeHtml(this.config.button_text) + ''; content += '
'; this.container.innerHTML = content; document.body.appendChild(this.container); }, setupEventListeners: function() { var self = this; // Close button if (this.config.close_button) { var closeBtn = this.container.querySelector('.tww-widget-close'); if (closeBtn) { closeBtn.addEventListener('click', function(e) { e.preventDefault(); e.stopPropagation(); self.hideWidget(); }); } } // Track clicks on main button var button = this.container.querySelector('.tww-widget-button'); if (button) { button.addEventListener('click', function() { self.setCookie(); }); } }, scheduleShow: function() { var self = this; setTimeout(function() { self.showWidget(); }, this.config.show_delay || 2000); }, showWidget: function() { if (this.isVisible) return; // Show overlay if needed if (this.overlay) { this.overlay.style.display = 'block'; setTimeout(function() { this.overlay.classList.add('tww-widget-overlay-visible'); }.bind(this), 10); } // Show widget with animation this.container.style.display = 'block'; setTimeout(function() { this.container.classList.add('tww-widget-visible'); this.isVisible = true; }.bind(this), 10); }, hideWidget: function() { if (!this.isVisible) return; console.log('TWW Widget: hideWidget called'); console.log('TWW Widget: minimize_on_close value =', this.config.minimize_on_close); console.log('TWW Widget: minimize_on_close type =', typeof this.config.minimize_on_close); // Konvertiere zu Boolean für sichere Überprüfung // Akzeptiere: 1, "1", true, "true" var shouldMinimize = this.config.minimize_on_close == 1 || this.config.minimize_on_close === '1' || this.config.minimize_on_close === true || this.config.minimize_on_close === 'true'; console.log('TWW Widget: shouldMinimize =', shouldMinimize); // Prüfe ob Widget minimiert werden soll statt komplett zu schließen if (shouldMinimize) { console.log('TWW Widget: Minimizing widget'); this.minimizeWidget(); } else { console.log('TWW Widget: Closing widget completely'); this.closeWidget(); } }, minimizeWidget: function() { this.container.classList.remove('tww-widget-visible'); if (this.overlay) { this.overlay.classList.remove('tww-widget-overlay-visible'); } setTimeout(function() { this.container.style.display = 'none'; if (this.overlay) { this.overlay.style.display = 'none'; } this.isVisible = false; this.isMinimized = true; // Zeige minimierten Button this.showMinimizedButton(); }.bind(this), 300); }, closeWidget: function() { this.container.classList.remove('tww-widget-visible'); if (this.overlay) { this.overlay.classList.remove('tww-widget-overlay-visible'); } setTimeout(function() { this.container.style.display = 'none'; if (this.overlay) { this.overlay.style.display = 'none'; } this.isVisible = false; }.bind(this), 300); this.setCookie(); }, showMinimizedButton: function() { console.log('TWW Widget: showMinimizedButton called'); if (this.minimizedButton) { console.log('TWW Widget: Minimized button already exists, showing it'); this.minimizedButton.style.display = 'flex'; setTimeout(function() { this.minimizedButton.classList.add('tww-minimized-visible'); console.log('TWW Widget: Added tww-minimized-visible class'); }.bind(this), 10); return; } console.log('TWW Widget: Creating new minimized button'); // Erstelle minimierten Button mit anpassbarem Text this.minimizedButton = document.createElement('div'); this.minimizedButton.className = 'tww-widget-minimized'; // Verwende anpassbaren Text oder Fallback var buttonText = this.config.minimized_button_text || 'Widget anzeigen'; this.minimizedButton.innerHTML = '' + this.escapeHtml(buttonText) + ''; var self = this; this.minimizedButton.addEventListener('click', function() { console.log('TWW Widget: Minimized button clicked'); self.restoreWidget(); }); document.body.appendChild(this.minimizedButton); console.log('TWW Widget: Minimized button appended to body'); // Debug: Prüfe ob Button im DOM ist var buttonInDom = document.querySelector('.tww-widget-minimized'); if (buttonInDom) { console.log('TWW Widget: ✓ Button found in DOM'); console.log('TWW Widget: Button position:', window.getComputedStyle(buttonInDom).position); console.log('TWW Widget: Button z-index:', window.getComputedStyle(buttonInDom).zIndex); console.log('TWW Widget: Button display:', window.getComputedStyle(buttonInDom).display); console.log('TWW Widget: Button bottom:', window.getComputedStyle(buttonInDom).bottom); console.log('TWW Widget: Button right:', window.getComputedStyle(buttonInDom).right); } else { console.error('TWW Widget: ✗ Button NOT found in DOM!'); } // Animation setTimeout(function() { this.minimizedButton.classList.add('tww-minimized-visible'); console.log('TWW Widget: Added tww-minimized-visible class'); // Prüfe nach Animation setTimeout(function() { var computedOpacity = window.getComputedStyle(this.minimizedButton).opacity; var computedDisplay = window.getComputedStyle(this.minimizedButton).display; console.log('TWW Widget: After animation - opacity:', computedOpacity, 'display:', computedDisplay); if (computedDisplay === 'none' || computedOpacity === '0') { console.error('TWW Widget: ⚠️ Button is not visible! Check CSS!'); } else { console.log('TWW Widget: ✓ Button should be visible now'); } }.bind(this), 350); }.bind(this), 10); }, restoreWidget: function() { if (!this.isMinimized) return; // Verstecke minimierten Button if (this.minimizedButton) { this.minimizedButton.classList.remove('tww-minimized-visible'); setTimeout(function() { this.minimizedButton.style.display = 'none'; }.bind(this), 300); } this.isMinimized = false; // Zeige Widget wieder setTimeout(function() { this.showWidget(); }.bind(this), 100); }, escapeHtml: function(text) { var map = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }; return text.replace(/[&<>"']/g, function(m) { return map[m]; }); } }; // Load widget data var dataScript = document.createElement('script'); // Cache-Busting: Füge Timestamp hinzu um Browser-Cache zu umgehen dataScript.src = 'https://go.tww.ch/tww-widget-data/' + widgetId + '.js?v=' + new Date().getTime(); dataScript.async = true; document.head.appendChild(dataScript); })();