"use strict"; (function () { 'use strict'; angular.module('feedModule') .controller('UpdateFeedController', ['$scope', '$filter', 'feedModel', 'metadataModel', 'configurationModel', '$interval', '$window', 'attachmentService', 'userModel', 'updateFeedFiltersVisibilityValidatorService', 'events', function ($scope, $filter, feedModel, metadataModel, configurationModel, $interval, $window, attachmentService, userModel, updateFeedFiltersVisibilityValidatorService, events) { var keyCode = { space: 32 }, feedRefreshInterval, newFeedsPromise, isTabInFocus = true, selectedFilterList = []; $scope.feedModel = feedModel; $scope.followCount = null; $scope.filters = _.filter(configurationModel.get('feed').update.filter, function (filterItem) { if (_.isUndefined(filterItem.display) || updateFeedFiltersVisibilityValidatorService[filterItem.display]()) { filterItem.localizedLabel = $filter('i18n')('feed.filter.optionName.' + filterItem.label); return true; } return false; }); $scope.filters = _.sortBy($scope.filters, 'localizedLabel'); metadataModel.getMetadataByType('global').then(function () { if (configurationModel.comaroundEnabled) { _.remove($scope.filters, { name: "knowledgeArticleUpdates" }); } }); $scope.pendingFilterUpdate = false; $scope.userModel = { isAccessibleUser: userModel.isAccessibleUser }; $scope.filterMenuToggled = function (open) { if (!open && $scope.pendingFilterUpdate) { _.each($scope.filters, function (filter) { var filterOption = _.find(selectedFilterList, { 'name': filter.name }); filter.selected = _.isUndefined(filterOption) ? false : true; }); $scope.pendingFilterUpdate = false; } }; var syncTime = '', feed = [], state = { loadingFeeds: true, loadingMoreFeeds: false, allFeedsLoaded: false }, feedItem = { last: {} }, setInitialState = function () { state.allFeedsLoaded = false; feed.length = 0; }, getUpdateFeedItems = function (params) { return feedModel.getUpdateFeedItems(params || {}).then(function (result) { populateUpdateFeed(result); }); }, populateUpdateFeed = function (result) { if (!_.isEmpty(result.items)) { Array.prototype.push.apply(feed, result.items); feedItem.last = _.last(result.items); if (!syncTime) { syncTime = result.syncTime; } } else { state.allFeedsLoaded = true; } if (result.followCount) { $scope.followCount = result.followCount; } _.forEach(feed, function (feedItem) { if (feedItem.relatedObject && feedItem.relatedObject.type && feedItem.relatedObject.type !== EntityVO.TYPE_BROADCAST && feedItem.relatedObject.type !== EntityVO.TYPE_OUTAGE) { if (feedItem.relatedObject.type === EntityVO.TYPE_PERSON) { feedItem.isAppEnabled = true; } else { feedItem.isAppEnabled = configurationModel.isServerApplicationEnabled(feedItem.relatedObject.type); } } }); }, refreshKnowledgeTitle = function () { if (feed.length < 12) { $scope.$broadcast(events.ELLIPSIS_EVENT); } }; $scope.getUpdateFeedItems = function () { setInitialState(); state.loadingFeeds = true; getUpdateFeedItems().finally(function () { refreshKnowledgeTitle(); state.loadingFeeds = false; }); }; $scope.getNewUpdateFeedItems = function () { if (!state.loadingFeeds && isTabInFocus) { state.loadingFeeds = true; feedModel.getUpdateFeedItems({ criteria: 'gt,' + syncTime }).then(function (result) { if (!_.isEmpty(result.items)) { syncTime = result.syncTime; _.forEach(result.items.reverse(), function (feedItem) { if (feedItem.relatedObject.type === EntityVO.TYPE_PERSON) { feedItem.isAppEnabled = true; } else { feedItem.isAppEnabled = configurationModel.isServerApplicationEnabled(feedItem.relatedObject.type); } feed.unshift(feedItem); }); refreshKnowledgeTitle(); } }).finally(function () { state.loadingFeeds = false; }); } }; function setFeedRefreshInterval() { feedRefreshInterval = metadataModel.cache && metadataModel.cache.global && metadataModel.cache.global.configurationParameters && metadataModel.cache.global.configurationParameters.feedRefreshInterval ? parseInt(metadataModel.cache.global.configurationParameters.feedRefreshInterval) * 1000 : 600000; newFeedsPromise = $interval($scope.getNewUpdateFeedItems, feedRefreshInterval); } if (metadataModel.cache.global.configurationParameters) { setFeedRefreshInterval(); } else { metadataModel.cache.global.then(function () { setFeedRefreshInterval(); }); } angular.element($window).bind('focus', function () { isTabInFocus = true; }).bind('blur', function () { isTabInFocus = false; }); $scope.loadMoreFeeds = function () { if (!state.loadingMoreFeeds && !state.allFeedsLoaded && !state.loadingFeeds) { state.loadingMoreFeeds = true; var params = { criteria: 'lt,' + feedItem.last.createDate, priority: feedItem.last.priority }; getUpdateFeedItems(params).finally(function () { refreshKnowledgeTitle(); state.loadingMoreFeeds = false; }); } }; $scope.keyPressOnFilterItem = function ($event, filterOption) { if ($event && $event.keyCode === keyCode.space) { $scope.updateFilters($event, filterOption); $event.preventDefault(); $event.stopPropagation(); } }; $scope.updateFilters = function ($event, filterOption) { $event.preventDefault(); $event.stopPropagation(); if (filterOption) { filterOption.selected = !filterOption.selected; } $scope.pendingFilterUpdate = true; }; $scope.applyFilter = function (filterOption) { if (filterOption) { filterOption.selected = !filterOption.selected; } feedModel.buildFeedFilter(feedModel.updateFeed, $scope.filters); selectedFilterList = _.filter($scope.filters, { selected: true }); $scope.pendingFilterUpdate = false; $scope.getUpdateFeedItems(); }; $scope.clearSearchText = function () { feedModel.updateFeed.params.searchQuery = ''; }; var removeWatcher = $scope.$watch('feedModel.updateFeed.params.searchQuery', function (newVal, oldVal) { if (newVal) { $scope.getUpdateFeedItemsDebounced(); } else if (!newVal && oldVal) { feedModel.updateFeed.params.searchQuery = ''; $scope.getUpdateFeedItems(); } }); $scope.getUpdateFeedItemsDebounced = _.debounce($scope.getUpdateFeedItems, 1000); $scope.$on('$destroy', function () { $interval.cancel(newFeedsPromise); feedModel.updateFeed.params.searchQuery = ''; removeWatcher(); }); $scope.$watch(function () { return userModel.isAccessibleUser; }, function (newValue) { $scope.userModel.isAccessibleUser = newValue; }); $scope.handleAttachmentClick = function (type, attachment) { state.loadingFeeds = true; attachmentService.getAttachmentFile(type, attachment) .finally(function () { state.loadingFeeds = false; }); }; $scope.handleUnpinClick = function (unPinnedFeedObj) { feedModel.unpinFeedItem(unPinnedFeedObj).then(function () { }); unPinnedFeedObj.priority = 0; var i; _.remove(feed, function (item) { return item.id === unPinnedFeedObj.id; }); // console.log(unPinnedFeedObj.createDate +" "+ feedItem.last.createDate); // console.log(unPinnedFeedObj.createDate > feedItem.last.createDate); if (((unPinnedFeedObj.createDate > feedItem.last.createDate) && feedItem.last.priority === 0) || feed.length < 20) { for (i = 0; i < feed.length; i++) { if (feed[i].priority === 0) { // console.log(unPinnedFeedObj.createDate +" "+ feed[i].createDate); // console.log(unPinnedFeedObj.createDate < feed[i].createDate); if (unPinnedFeedObj.createDate < feed[i].createDate) { continue; } else { //find the place to chronologically fit the item back to feeds break; } } } feed.splice(i, 0, unPinnedFeedObj); feedItem.last = angular.copy(feed[feed.length - 1]); } }; $scope.state = state; $scope.feed = feed; //$scope.getUpdateFeedItems(); $scope.applyFilter({ data: true }); } ]); })();