(function () { "use strict"; var streamApp = angular.module('streamApp'); streamApp.directive('notificationsDirective', ['streamService', 'userService', '$timeout', '$location', '$routeParams', '$window', NotificationsDirective]); let version = 0; function NotificationsDirective(streamService, userService, $timeout, $location, $routeParams, $window) { return { restrict: 'E', scope: { userName: '=' }, templateUrl: '/App/Directives/notifications/notificationsDirective.html?v=6.0.0c', link: function (scope, element, attrs, tabsCtrl) { scope.showNotifications = function () { scope.notificationsVisible = !scope.notificationsVisible; }; scope.goto = function (notification) { switch (notification.notificationType) { case 5: scope.remove(notification); $location.url('/timelines/' + notification.dropId); break; case 4: scope.remove(notification); $location.url('/memory/add?questionId=' + notification.dropId); break; case 3: $location.url('/suggestions'); break; case 2: scope.remove(notification); $location.url('/sharing'); break; default: scope.remove(notification); $location.url('/memory?dropId=' + notification.dropId); } /* if (notification.notificationType === 4) { scope.remove(notification); $location.url('/memory/add?questionId=' + notification.dropId); } else if (notification.notificationType === 3) { $location.url('/suggestions'); } else if (notification.notificationType === 2) { scope.remove(notification); $location.url('/sharing'); } else { scope.remove(notification); $location.url('/memory?dropId=' + notification.dropId); } */ }; scope.remove = function (notification) { streamService.viewNotification(notification.dropId); notification.Viewed = true; updateNew(); }; scope.clearAll = function () { updateNew(); streamService.removeAllNotification().then(function () { scope.notifications = []; }); }; var notificationHash = ''; scope.notifications = []; checkForNotifications(); function updateNew() { scope.newNotificaitons = scope.notifications.some(n => !n.Viewed); } const notificationDetail = { 0: { "message": "shared", "icon": "mdi-image-size-select-actual" }, 1: { "message": "commented", "icon": "mdi-comment-text-outline" }, 2: { "message": "wants to share with you", "icon": "mdi-account-supervisor" }, 3: { "message": "You have sharing suggestions", "icon": "mdi-account-supervisor" }, 4: { "message": "asked you a question", "icon": "mdi-map-marker-question" }, 5: { "message": "wants your help building a timeline", "icon": "mdi-clock-outline" } }; $window.document.addEventListener( "visibilitychange" , () => { if (document.hidden) { // do nothing } else { checkForNotifications(); } } ); function checkForNotifications() { streamService.checkForNotifications().then( function (data) { const notificationRoot = data.data.notifications; if (version && version !== data.data.Version) { $window.location.reload(); } /* if (!data.data.hasPremiumPlan) { $location.url('/plan'); } */ version = data.data.Version; if (notificationRoot.notificationHash !== notificationHash) { notificationHash = notificationRoot.notificationHash; scope.notifications = []; if (notificationRoot.notifications && notificationRoot.notifications.length > 0) { for (var i = 0; i < notificationRoot.notifications.length; i++) { const notification = notificationRoot.notifications[i]; var dateTime = new Date(Date.parse(notification.createdAt)); notification.message = notificationDetail[notification.notificationType].message; notification.icon = notificationDetail[notification.notificationType].icon; if (notification.notificationType < 2) { notification.date = dateTime.toLocaleString().slice(0, -6) + dateTime.toLocaleString().slice(-3); } scope.notifications.push(notification); } updateNew(); } } $timeout(checkForNotifications, 60000); }).catch(() => userService.isAuthenticated($routeParams.link) // check if you got logged out ).catch(); } } }; } }());