|
| 1 | +api.controller = function($scope, $interval, spModal, $window) { |
| 2 | + var c = this; |
| 3 | + |
| 4 | + // Initialize |
| 5 | + c.counts = {}; |
| 6 | + c.changes = {}; |
| 7 | + c.lastUpdate = new Date(); |
| 8 | + c.isRefreshing = false; |
| 9 | + c.autoRefresh = true; |
| 10 | + c.soundEnabled = true; |
| 11 | + c.newCritical = false; |
| 12 | + var refreshInterval; |
| 13 | + |
| 14 | + // Load initial data |
| 15 | + c.$onInit = function() { |
| 16 | + c.counts = c.data.counts || {}; |
| 17 | + c.previousCounts = angular.copy(c.counts); |
| 18 | + c.startAutoRefresh(); |
| 19 | + }; |
| 20 | + |
| 21 | + // Refresh data |
| 22 | + c.refresh = function() { |
| 23 | + c.isRefreshing = true; |
| 24 | + |
| 25 | + c.server.get().then(function(response) { |
| 26 | + var newCounts = response.data.counts; |
| 27 | + |
| 28 | + // Calculate changes |
| 29 | + c.changes = { |
| 30 | + critical: (newCounts.critical || 0) - (c.counts.critical || 0), |
| 31 | + high: (newCounts.high || 0) - (c.counts.high || 0), |
| 32 | + medium: (newCounts.medium || 0) - (c.counts.medium || 0), |
| 33 | + low: (newCounts.low || 0) - (c.counts.low || 0) |
| 34 | + }; |
| 35 | + |
| 36 | + // Check for new critical tickets |
| 37 | + if (c.changes.critical > 0) { |
| 38 | + c.newCritical = true; |
| 39 | + if (c.soundEnabled) { |
| 40 | + c.playAlertSound(); |
| 41 | + } |
| 42 | + |
| 43 | + // Remove pulse animation after 3 seconds |
| 44 | + $interval(function() { |
| 45 | + c.newCritical = false; |
| 46 | + }, 3000, 1); |
| 47 | + } |
| 48 | + |
| 49 | + // Update counts |
| 50 | + c.counts = newCounts; |
| 51 | + c.lastUpdate = new Date(); |
| 52 | + c.isRefreshing = false; |
| 53 | + }); |
| 54 | + }; |
| 55 | + |
| 56 | + // Auto-refresh toggle |
| 57 | + c.toggleAutoRefresh = function() { |
| 58 | + if (c.autoRefresh) { |
| 59 | + c.startAutoRefresh(); |
| 60 | + } else { |
| 61 | + c.stopAutoRefresh(); |
| 62 | + } |
| 63 | + }; |
| 64 | + |
| 65 | + // Start auto-refresh |
| 66 | + c.startAutoRefresh = function() { |
| 67 | + if (refreshInterval) { |
| 68 | + $interval.cancel(refreshInterval); |
| 69 | + } |
| 70 | + |
| 71 | + refreshInterval = $interval(function() { |
| 72 | + c.refresh(); |
| 73 | + }, 30000); // 30 seconds |
| 74 | + }; |
| 75 | + |
| 76 | + // Stop auto-refresh |
| 77 | + c.stopAutoRefresh = function() { |
| 78 | + if (refreshInterval) { |
| 79 | + $interval.cancel(refreshInterval); |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + // Play sound alert |
| 84 | + c.playAlertSound = function() { |
| 85 | + var audio = new Audio('data:audio/wav;base64,UklGRnoGAABXQVZFZm10IBAAAAABAAEAQB8AAEAfAAABAAgAZGF0YQoGAACBhYqFbF1fdJivrJBhNjVgodDbq2EcBj+a2/LDciUFLIHO8tiJNwgZaLvt559NEAxQp+PwtmMcBjiR1/LMeSwFJHfH8N2QQAoUXrTp66hVFApGn+DyvmwhBSqA0fPTgjMGHm7A7+OZRQ0PVq/m77BdGAg+ltryxnMpBSl+zPLaizsIGGS57OibUBELTqXh8bllHAU2jdXwyH0vBSZ8yfDajkULEFau5u+wXRgIPpXa8sZzKQUpfszy2Ys7CBhkuezom1ARDEyl4fG5ZRwFNo3V8Mh9LwUmfMnw2o5FDBFWrebvsF0YCD6V2vLGcykFKX7M8tmLOwgYZLns6JtQEQxMpeHxuWUcBTaN1fDIfS8FJnzJ8NqORQwRVq3m77BdGAg+ldryx3MpBSl+zPLaizsIGGS57OmbUBEMTKXh8bllHAU2jdXwyH0vBSZ8yfDajkUMEVat5u+wXRgIPpXa8sZzKQUpfszy2Ys7CBhkuezom1ARDEyl4fG5ZRwFNo3V8Mh9LwUmfMnw2o5FDBFWrebvsF0YCD6V2vLGcykFKX7M8tmLOwgYZLns6JtQEQxMpeHxuWUcBTaN1fDIfS8FJnzJ8NqORQwRVq3m77BdGAg='); |
| 86 | + audio.play().catch(function(e) { |
| 87 | + console.log('Could not play sound:', e); |
| 88 | + }); |
| 89 | + }; |
| 90 | + |
| 91 | + // Toggle sound |
| 92 | + c.toggleSound = function() { |
| 93 | + c.soundEnabled = !c.soundEnabled; |
| 94 | + if (c.soundEnabled) { |
| 95 | + spModal.alert('🔊 Sound alerts enabled'); |
| 96 | + } else { |
| 97 | + spModal.alert('🔇 Sound alerts disabled'); |
| 98 | + } |
| 99 | + }; |
| 100 | + |
| 101 | + // View tickets by priority |
| 102 | + c.viewTickets = function(priority) { |
| 103 | + var priorityNames = { |
| 104 | + '1': 'Critical', |
| 105 | + '2': 'High', |
| 106 | + '3': 'Medium', |
| 107 | + '4': 'Low' |
| 108 | + }; |
| 109 | + |
| 110 | + // Navigate to filtered list |
| 111 | + $window.location.href = '/incident_list.do?sysparm_query=priority=' + priority + '^active=true'; |
| 112 | + }; |
| 113 | + |
| 114 | + // Cleanup on destroy |
| 115 | + c.$onDestroy = function() { |
| 116 | + c.stopAutoRefresh(); |
| 117 | + }; |
| 118 | +}; |
0 commit comments