|
2 | 2 |
|
3 | 3 | angular
|
4 | 4 | .module('openshiftConsole')
|
5 |
| - .directive('notificationCounter', function() { |
6 |
| - return { |
7 |
| - restrict: 'AE', |
8 |
| - scope: {}, |
9 |
| - controller: |
10 |
| - function($scope, $routeParams, $rootScope, notifications) { |
11 |
| - $scope.count = 0; |
12 |
| - $scope.countDisplay = $scope.count; |
| 5 | + .component('notificationCounter', { |
| 6 | + templateUrl: 'views/directives/notifications/notification-counter.html', |
| 7 | + bindings: {}, |
| 8 | + controller: [ |
| 9 | + '$routeParams', |
| 10 | + '$rootScope', |
| 11 | + 'Constants', |
| 12 | + 'DataService', |
| 13 | + function($routeParams, $rootScope, Constants, DataService) { |
| 14 | + var ENABLE_EVENTS = _.get(Constants, 'ENABLE_TECH_PREVIEW_FEATURE.events_in_notification_drawer'); |
| 15 | + var counter = this; |
13 | 16 | var drawerHidden = true;
|
14 | 17 |
|
15 |
| - $scope.onClick = function() { |
16 |
| - $scope.$applyAsync(function() { |
17 |
| - $scope.count = 0; |
| 18 | + var rootScopeWatches = []; |
| 19 | + // this one is treated separately from the rootScopeWatches as |
| 20 | + // it may need to be updated outside of the lifecycle of init/destroy |
| 21 | + var notificationListener; |
| 22 | + |
| 23 | + var eventsWatcher; |
| 24 | + |
| 25 | + var deregisterEventsWatch = function() { |
| 26 | + if(eventsWatcher) { |
| 27 | + DataService.unwatch(eventsWatcher); |
| 28 | + } |
| 29 | + }; |
| 30 | + |
| 31 | + var watchEvents = function(projectName, cb) { |
| 32 | + if(projectName && ENABLE_EVENTS) { |
| 33 | + eventsWatcher = DataService.watch('events', {namespace: projectName}, _.debounce(cb, 50), { skipDigest: true }); |
| 34 | + } |
| 35 | + }; |
| 36 | + |
| 37 | + var watchNotifications = function(projectName, cb) { |
| 38 | + if(!projectName) { |
| 39 | + return; |
| 40 | + } |
| 41 | + notificationListener = $rootScope.$on('NotificationsService.onNotificationAdded', cb); |
| 42 | + }; |
| 43 | + |
| 44 | + var deregisterNotificationListener = function() { |
| 45 | + notificationListener && notificationListener(); |
| 46 | + }; |
| 47 | + |
| 48 | + var deregisterRootScopeWatches = function() { |
| 49 | + _.each(rootScopeWatches, function(deregister) { |
| 50 | + deregister(); |
18 | 51 | });
|
| 52 | + }; |
| 53 | + |
| 54 | + var toggleVisibility = function(projectName) { |
| 55 | + if(!projectName) { |
| 56 | + counter.hideCounter = true; |
| 57 | + } |
| 58 | + counter.showNewNotificationIndicator = true; |
| 59 | + }; |
| 60 | + |
| 61 | + counter.onClick = function() { |
19 | 62 | drawerHidden = !drawerHidden;
|
| 63 | + counter.showNewNotificationIndicator = false; |
20 | 64 | $rootScope.$emit('notification-drawer:show', {
|
21 | 65 | drawerHidden: drawerHidden
|
22 | 66 | });
|
23 | 67 | };
|
24 | 68 |
|
25 |
| - // TODO: just need to know if there is a "new" notification worth viewing, |
26 |
| - // don't need to give the user a ping for every time the watch returns data |
27 |
| - var subscription = notifications.subscribe($routeParams.project, function(groups) { |
28 |
| - $scope.groups = groups; |
29 |
| - $scope.count++; |
30 |
| - $scope.countDisplay = $scope.count < 100 ? |
31 |
| - $scope.count: |
32 |
| - '! !'; // TODO: a "too many!" indicator |
33 |
| - }); |
34 |
| - |
35 |
| - $scope.$on('$destroy', function() { |
36 |
| - notifications.unsubscribe(subscription); |
37 |
| - }); |
38 |
| - }, |
39 |
| - templateUrl: 'views/directives/notifications/notification-counter.html' |
40 |
| - }; |
| 69 | + var genericEventCallback = function() { |
| 70 | + counter.showNewNotificationIndicator = true; |
| 71 | + }; |
| 72 | + |
| 73 | + var reset = function() { |
| 74 | + deregisterEventsWatch(); |
| 75 | + deregisterNotificationListener(); |
| 76 | + watchEvents($routeParams.project, genericEventCallback); |
| 77 | + watchNotifications($routeParams.project, genericEventCallback); |
| 78 | + toggleVisibility($routeParams.project); |
| 79 | + }; |
| 80 | + |
| 81 | + counter.$onInit = function() { |
| 82 | + reset(); |
| 83 | + rootScopeWatches.push($rootScope.$on("$routeChangeSuccess", function () { |
| 84 | + reset(); |
| 85 | + })); |
| 86 | + }; |
| 87 | + |
| 88 | + counter.$onDestroy = function() { |
| 89 | + deregisterEventsWatch(); |
| 90 | + deregisterNotificationListener(); |
| 91 | + deregisterRootScopeWatches(); |
| 92 | + }; |
| 93 | + } |
| 94 | + ] |
41 | 95 | });
|
0 commit comments