SmartIT_Extensions/BMC/smart-it-full-helix/scripts/app/console/console-directive.js

956 lines
61 KiB
JavaScript

"use strict";
// jscs:disable requireMultipleVarDecl
(function () {
'use strict';
angular.module('consoleModule')
.directive('console', ['$window', '$timeout', '$rootScope', 'configurationModel', 'searchModel', 'personModel', 'knowledgeConsoleModel', 'ticketConsoleModel', 'assetConsoleModel', '$modal', '$q', '$filter', 'systemAlertService', 'events', 'consoleService', 'consolePresetModel', 'metadataModel', 'utilityFunctions',
function ($window, $timeout, $rootScope, configurationModel, searchModel, personModel, knowledgeConsoleModel, ticketConsoleModel, assetConsoleModel, $modal, $q, $filter, systemAlertService, events, consoleService, consolePresetModel, metadataModel, utilityFunctions) {
return {
restrict: 'E',
replace: true,
scope: {
defaultFilters: '=',
defaultCriteria: '=',
consoleType: '@'
},
templateUrl: 'views/console/console.html',
controller: 'ConsoleController',
link: function (scope, iElement, iAttrs) {
var displayMetric = angular.isDefined(iAttrs.displayMetric) ? scope.$eval(iAttrs.displayMetric) : false, displayFilter = angular.isDefined(iAttrs.displayFilter) ? scope.$eval(iAttrs.displayFilter) : false, selectedUserFilter = {}, consoleModel = null;
scope.maxFilterPills = configurationModel.get('ticketConsole.config.maxFilterPills');
scope.showNeedAttentionStat = false;
scope.showSecurityTickets = false;
switch (scope.consoleType) {
case 'ticket':
consoleModel = ticketConsoleModel;
break;
case 'knowledge':
consoleModel = knowledgeConsoleModel;
break;
case 'asset':
consoleModel = assetConsoleModel;
break;
}
scope.$watch('filter.isOpen', function () {
if (!scope.filter.isOpen) {
scope.cancelFilter();
}
});
scope.openSearch = function () {
$('.tc__search_bar .tc__search_icon:not(".open_search")').toggle();
$('.tc__search_bar_input').toggle('slide');
};
if (scope.defaultCriteria) {
angular.extend(scope.criteria, scope.defaultCriteria);
}
if (displayFilter) {
scope.filter = {
isOpen: false
};
scope.appliedSelectedFilters = [];
scope.isApplyClicked = false;
scope.isRemoveClicked = false;
scope.filterCount = 0;
var applyDynamicFilterOption = function (filter, filterOption) {
if (angular.isObject(_.find(filter.options, { name: filterOption.name }))) { //option was already added to list
if (filterOption.criteria.name !== "affectedBusinessServices") {
return;
}
else {
var isFilterExists = false;
_.forEach(filter.options, function (existingFilter) {
if (existingFilter.criteria && (existingFilter.criteria.value[0].reconciliationId === filterOption.criteria.value[0].reconciliationId)) {
isFilterExists = true;
}
});
if (isFilterExists) {
return;
}
}
}
filter.options.push(filterOption);
scope.updateFilterCriteria(_.last(filter.options));
}, isUserFilterPresetApplied = function (userFilterPreset) {
var isApplied = false;
if (userFilterPreset.value.length === consoleModel.selectedFilters.length) {
isApplied = true;
for (var i = 0, l = userFilterPreset.value.length; i < l; i++) {
var presetFilterItem = userFilterPreset.value[i], selectedFilter = _.find(consoleModel.selectedFilters, { name: presetFilterItem.option, filterName: presetFilterItem.filter });
if (!selectedFilter) {
isApplied = false;
break;
}
}
}
return isApplied;
};
scope.expandFilterItem = function (filter) {
if (filter.expanded) {
filter.expanded = false;
return;
}
_.forEach(scope.filterConfig, function (item) {
item.expanded = false;
});
filter.expanded = true;
};
scope.searchFilterOption = function (searchParams, searchText, searchFilter) {
var filterItem = searchFilter || {}, excludeSelf = false, searchFunc = scope.$eval(scope.consoleType === EntityVO.TYPE_ASSET && filterItem.selectedSearchMode ? filterItem.selectedSearchMode.method : searchParams.method, {
searchModel: searchModel,
personModel: personModel,
consoleModel: consoleModel
}), checkForSelfExclusion = function (item) {
return !(excludeSelf && item.name === userOrg);
}, formatFilterOptionResult = function (items, searchParams) {
var keyField = scope.consoleType === EntityVO.TYPE_ASSET && filterItem.selectedSearchMode ? filterItem.selectedSearchMode.fields.key : searchParams.fields.key, valueField = scope.consoleType === EntityVO.TYPE_ASSET && filterItem.selectedSearchMode ? filterItem.selectedSearchMode.fields.value : searchParams.fields.value;
if (items.length && !_.isEmpty(items[0])) {
var mapItems = [];
_.forEach(items, function (item) {
if ((scope.userModel.userFullData && scope.userModel.userFullData.loginId !== item.loginId) && checkForSelfExclusion(item)) {
mapItems.push({
key: item[keyField],
value: scope.$eval(searchParams.valueFormat, _.merge({ value: _.get(item, valueField) }, item.attributeMap)),
realObject: item
});
}
});
return mapItems;
}
};
// TODO: Make this dynamic for all the search filters
if (searchFilter && searchFilter.name === "organizations") {
excludeSelf = true;
var userOrg = scope.userModel.userFullData.organization;
}
return searchFunc(searchText).then(function (items) {
return formatFilterOptionResult(items, searchParams);
});
};
scope.onFilterOptionSelect = function (filter, option, $item) {
//fix for SW00501486 based on the backend fix
//in case of support groups, send the entire support group object and remove the properties added by UI
var item = null, itemName;
if (filter.name === 'assignedSupportGroups' || filter.name === 'changeManagerGroup' || filter.name === 'requestManagerGroups') {
item = _.cloneDeep($item.realObject);
delete item.companyName;
delete item.companyAndOrganization;
}
else {
item = $item.value;
}
if (_.has($item, 'realObject.attributeMap.companyName')) {
itemName = $item.key + ': ' + $item.realObject.attributeMap.companyName;
}
else if (_.has($item, 'realObject.companyName')) {
itemName = $item.key + ': ' + $item.realObject.companyName;
}
else if (_.has($item, 'realObject.company.name')) {
itemName = $item.key + ': ' + $item.realObject.company.name;
}
else {
itemName = $item.key;
}
if (filter.name === 'assignedSupportGroups' || filter.name === 'changeManagerGroup'
|| filter.name === 'problemCoordinatorGroups' || filter.name === 'releaseCoordinatorGroup' || filter.name === 'requestManagerGroups') {
if (_.has($item, 'realObject.companyAndOrganization')) {
itemName = $item.key + ': ' + $item.realObject.companyAndOrganization;
}
else if (_.has($item, 'realObject.attributeMap.companyAndOrganization')) {
itemName = $item.key + ': ' + $item.realObject.attributeMap.companyAndOrganization;
}
}
var filterOption = {
name: itemName,
active: true,
type: 'dynamic',
criteria: {
name: filter.name,
value: [item]
}
};
option.searchText = '';
applyDynamicFilterOption(filter, filterOption);
};
scope.applyAdvancedFilterOption = function (filter, option, $item) {
if (option.searchText === "") {
return;
}
option.active = !option.active;
var filterActive = _.some(filter.criteriaKeys, function (criteriaKey) {
return criteriaKey.active;
});
scope.onFilterOptionKeySelect(filter, option, $item, filterActive);
};
scope.onFilterOptionKeySelect = function (filter, option, $item, active) {
var filterOption = {
active: active,
type: 'dynamic',
criteria: {
name: filter.name
}
}, selectedNames = [], selectedValues = {};
if ($item) {
option.selectedValue = $item.value;
option.searchText = $item.key;
if (!_.isEmpty($item.value.categorizations)) { //set product name as key
option.selectedValue = option.searchText = $item.value.categorizations[0].tiers.productName;
}
}
if (!option.searchText) {
option.active = false;
}
_.forEach(filter.criteriaKeys, function (criteriaKey) {
selectedValues[criteriaKey.name] = {
operator: criteriaKey.selectedOperator.id,
value: criteriaKey.type === 'search' ? criteriaKey.selectedValue : criteriaKey.searchText
};
if (!selectedValues[criteriaKey.name].value || !criteriaKey.active) {
delete selectedValues[criteriaKey.name];
}
else {
selectedNames.push(selectedValues[criteriaKey.name].value);
}
});
if (_.isEmpty(selectedValues)) {
var selectedFilter = _.find(consoleModel.selectedFilters, { filterName: filter.name });
if (selectedFilter) {
selectedFilter.active = false;
filterOption.name = selectedFilter.name;
scope.updateFilterCriteria(filterOption);
}
return;
}
filterOption.criteria.value = [selectedValues];
filterOption.name = selectedNames.join(', ');
scope.updateFilterCriteria(filterOption);
};
scope.resetRangeTypeFilter = function () {
scope.rangeValues = {};
};
scope.removeFilter = function (filterOption, isConsoleLoading) {
filterOption.active = false;
if (filterOption.criteria.name === 'ticketTypes') {
scope.updateFilterGrouping(filterOption.name); //Filters shown and hidden as per ticket type selected
}
else if (filterOption.criteria.name === 'assetTypes') {
scope.updateAssetFilterGrouping(filterOption.name);
}
else if (filterOption.criteria.name === 'operatingSystem' || filterOption.criteria.name === 'processor') {
consoleModel.clearCriteriaKeyValues(filterOption.criteria.name);
}
else if (filterOption.criteria.name === 'cpu') {
scope.resetRangeTypeFilter();
}
if (filterOption.filterType === "customFilter" && filterOption.subtype === "range") {
scope.rangeValues = {};
}
scope.isRemoveClicked = true;
if (!isConsoleLoading) {
scope.clearQuickSearch();
}
consoleModel.applyFilter(filterOption);
};
scope.handleKeydown = function ($event, filterOption) {
if ($event.keyCode === 32) { // space key
scope.addFilter(filterOption);
$event.preventDefault();
$event.stopPropagation();
}
};
scope.addFilter = function (filterOption) {
filterOption.active = !filterOption.active;
if (filterOption.criteria.name === 'ticketTypes') {
$timeout(function () {
scope.updateFilterGrouping(filterOption.name); //Filters shown and hidden as per ticket type selected
});
}
else if (filterOption.criteria.name === 'assetTypes') {
scope.updateAssetFilterGrouping(filterOption.name);
}
if (filterOption.type === 'timeStampRange') {
return;
}
scope.currentFilter = filterOption;
scope.updateFilterCriteria(filterOption);
};
scope.updateFilterCriteria = function (filterOption) {
var foundForUpdate = false;
var i = 0;
for (var index in scope.appliedSelectedFilters) {
var filter = scope.appliedSelectedFilters[index];
if (filter.name == filterOption.name && filter.criteria.name == filterOption.criteria.name) {
if (filterOption.active) {
scope.filterCount += 1;
}
else {
scope.filterCount -= 1;
}
scope.appliedSelectedFilters.splice(i, 1);
foundForUpdate = true;
break;
}
i++;
}
if (!foundForUpdate) {
if (filterOption.active) {
scope.filterCount += 1;
}
else {
scope.filterCount -= 1;
}
scope.appliedSelectedFilters.push(filterOption);
}
};
scope.applyFilter = function () {
var i = 0;
for (var index in scope.appliedSelectedFilters) {
var filter = scope.appliedSelectedFilters[index];
consoleModel.applyFilter(filter);
i++;
}
if (i > 0) {
scope.isApplyClicked = true;
scope.filter.isOpen = false;
scope.clearQuickSearch();
}
scope.appliedSelectedFilters = [];
scope.filterCount = 0;
};
var checkForCriteriaKeyChanges = function (appliedFilter) {
var ind = _.findIndex(scope.filterConfig, { name: appliedFilter.criteria.name });
if (ind > -1 && !_.isUndefined(scope.filterConfig[ind].criteriaKeys)) {
var criteriaKeys = scope.filterConfig[ind].criteriaKeys;
_.each(criteriaKeys, function (key) {
if (key.selectedValue === appliedFilter.name || key.searchText === appliedFilter.name) {
key.active = appliedFilter.active;
}
});
}
};
scope.cancelFilter = function () {
scope.filter.isOpen = false;
scope.selectedFilters = consoleModel.selectedFilters;
for (var index in scope.appliedSelectedFilters) {
scope.appliedSelectedFilters[index].active = !(scope.appliedSelectedFilters[index].active);
checkForCriteriaKeyChanges(scope.appliedSelectedFilters[index]);
if (scope.appliedSelectedFilters[index].criteria && scope.appliedSelectedFilters[index].criteria.name === 'ticketTypes') {
scope.updateFilterGrouping(scope.appliedSelectedFilters[index].name);
}
}
scope.resetRangeTypeFilter();
scope.appliedSelectedFilters = [];
scope.filterCount = 0;
};
scope.refreshConsole = function () {
scope.refreshing = true;
scope.getNewItemList(true).then(function () {
scope.refreshing = false;
}).catch(function () {
scope.refreshing = false;
});
if (displayMetric) {
scope.getMetrics();
}
};
scope.saveFilterPreset = function () {
scope.savePresetName = '';
var modal = $modal.open({
templateUrl: 'views/console/console-save-filter-preset-action-blade.html',
controller: ['$scope', '$modalInstance', function ($scope, $modalInstance) {
$scope.advancedQualenabled = scope.advancedQualenabled;
$scope.consoleType = scope.consoleType;
$scope.advancedQualification = scope.advancedQualenabled ? scope.advancedQualification : {};
$scope.selectedFilters = consoleModel.selectedFilters;
$scope.gridColumns = angular.copy(_.sortBy(_.filter(consoleModel.columnsConfig, 'visible'), 'order'));
$scope.filterPresetName = '';
$scope.saveFitlerCheckbox = $scope.selectedFilters.length > 0;
$scope.saveColumnCheckbox = $scope.gridColumns.length > 0;
$scope.savePreset = function (name) {
scope.savePresetName = name;
//Prevent user from using columns as it is a reserved word
if (name === "columns") {
systemAlertService.warning({
text: $filter('i18n')('console.saveFilter.reservedName'),
hide: 10000
});
return;
}
//Todo: Need to drop the check for usersavedfilterpreset and usersavedcolumnpreset as they should be part of usersavedpresets
if (_.find(consoleModel.userSavedFilterPresets, { name: name })
|| _.find(consoleModel.userSavedColumnPresets, { name: name })
|| _.find(consoleModel.userSavedPresets, { name: name })) {
systemAlertService.warning({
text: $filter('i18n')('console.saveFilter.nameConflict'),
hide: 10000
});
return;
}
$scope.savingFilterPreset = true;
var promises = [], saveFilterPresetPromise, saveColumnPresetPromise, columnsToUpdate;
if ($scope.saveFitlerCheckbox) {
consoleModel.saveFilterPreset(name).then(function () {
saveFilterPresetPromise = consoleService.saveCIFilterConfiguration(name, consoleModel.criteria.filterCriteria);
promises.push(saveFilterPresetPromise);
if ($scope.saveColumnCheckbox && consoleModel.saveColumnPreset) {
columnsToUpdate = {};
_.forEach($scope.gridColumns, function (column, idx) {
var columnIndex = ++idx;
columnsToUpdate[column.name] = {
visible: true,
order: columnIndex
};
});
columnsToUpdate.newColumnType = true;
saveColumnPresetPromise = consoleModel.saveColumnPreset(name, columnsToUpdate);
promises.push(saveColumnPresetPromise);
}
$q.all(promises).then(function () {
$modalInstance.close();
}).finally(function () {
$scope.savingFilterPreset = false;
scope.init();
});
}).catch(function () {
systemAlertService.warning({
text: $filter('i18n')('error.unknown'),
hide: 10000
});
$scope.savingFilterPreset = false;
return;
});
}
};
}],
windowClass: 'action-blade'
});
modal.result.then(function () {
consoleModel.addUserSavedPresets(scope.savePresetName);
scope.applyUserFilterPreset(_.find(consoleModel.userSavedPresets, { name: scope.savePresetName }));
scope.clearQuickSearch();
});
};
scope.clearAllFilters = function (isConsoleLoading) {
var filters = scope.selectedFilters.concat([]); //filters will remain unchanged during all _.forEach
_.forEach(filters, function (filterOption) {
scope.removeFilter(filterOption, isConsoleLoading);
});
if (scope.advancedQualification && scope.advancedQualification.value) {
scope.advancedQualification.value = '';
consoleModel.criteria.filterCriteria.advanceSearchQuery = '';
}
if (!isConsoleLoading) {
scope.clearQuickSearch();
}
};
scope.datePickerOptions = {
formatYear: 'yy',
startingDay: configurationModel.getWeekStartingDay(),
'show-weeks': false,
'format-day': 'd',
showMeridian: $window.showMeridian,
minDate: moment().year(1970).month(0).date(2),
maxDate: moment().year(2038).month(0).date(18)
};
scope.openDatePicker = function ($event, datepicker) {
if ($event) {
$event.preventDefault();
$event.stopPropagation();
}
datepicker.isOpen = true;
};
scope.closeDatePickers = function (filterOption) {
filterOption.startDatePicker.isOpen = false;
filterOption.endDatePicker.isOpen = false;
};
scope.onDateRangeSubmit = function (filter, filterOption) {
function getMilliOfTime(d) {
var h = d.getHours(), m = d.getMinutes();
return ((h * 60 * 60 * 1000) + (m * 60 * 1000));
}
function getLocalOffsetInMilliseconds() {
if ($rootScope.timezone) {
//timezone value expected format: America/Los_Angeles
var timezoneOffset = moment.tz($rootScope.timezone).utcOffset() * 60 * 1000;
return timezoneOffset; //offset in millis
}
else {
return new Date().getTimezoneOffset() * 60000; //getTimezoneOffset returns minutes
}
}
var rangeStart = filterOption.startDatePicker.date.getTime(), rangeEnd = filterOption.endDatePicker.date.getTime();
//Switch to user preference timezone
if ($rootScope.timezone) {
rangeStart = utilityFunctions.convertToUserPreferenceTimezone(rangeStart, $rootScope.timezone).getTime();
rangeEnd = utilityFunctions.convertToUserPreferenceTimezone(rangeEnd, $rootScope.timezone).getTime();
}
if (filter.type === 'custom') {
var timeRangeStart = getMilliOfTime(filterOption.startDatePicker.date);
var timeRangeEnd = getMilliOfTime(filterOption.endDatePicker.date);
}
var customRangeFilterOption = _.cloneDeep(filterOption);
filterOption.active = false;
customRangeFilterOption = {
name: $filter('datePreConfigTimezone')(rangeStart, 'mediumDate') + ' ' + $filter('datePreConfigTimezone')(rangeStart, 'shortTime') + ' - ' + $filter('datePreConfigTimezone')(rangeEnd, 'mediumDate') + ' ' + $filter('datePreConfigTimezone')(rangeEnd, 'shortTime'),
active: true,
type: 'dynamic',
criteria: {
name: customRangeFilterOption.criteria.name,
type: 'date',
value: [{ start: rangeStart, end: rangeEnd }]
}
};
if (filter.type === 'custom') {
customRangeFilterOption.filterType = 'customFilter';
if (filter.oldFields && filter.oldFields.dataType == 'time') {
customRangeFilterOption.name = $filter('date')(rangeStart, 'shortTime') + ' - ' + $filter('date')(rangeEnd, 'shortTime');
customRangeFilterOption.criteria.value = [{ start: timeRangeStart, end: timeRangeEnd }];
}
else if (filter.oldFields && filter.oldFields.dataType == 'date') {
customRangeFilterOption.criteria.value = [{
start: rangeStart - getLocalOffsetInMilliseconds(),
end: rangeEnd - getLocalOffsetInMilliseconds()
}];
}
}
applyDynamicFilterOption(filter, customRangeFilterOption);
};
scope.applyKeywordFilter = function (filter, option, text) {
var searchText = text || option.searchText;
if (!searchText) {
return;
}
var filterOption = {
name: searchText,
active: true,
type: 'dynamic',
criteria: {
name: filter.name,
value: filter.name === 'rack' ? [{ id: searchText }] : [searchText]
}
};
if (filter.type === 'custom' && option.filterType === 'customFilter') {
filterOption.filterType = 'customFilter';
}
applyDynamicFilterOption(filter, filterOption);
option.searchText = '';
};
scope.onNumberRangeSubmit = function (filter) {
if (_.isEmpty(scope.rangeValues) || (isNaN(scope.rangeValues.min) && isNaN(scope.rangeValues.max))) {
return;
}
var autoMin = scope.rangeValues.min, autoMax = scope.rangeValues.max;
if (!scope.rangeValues.min && scope.rangeValues.min !== 0) {
scope.rangeValues.min = scope.rangeValues.max;
autoMin = autoMax;
}
if ((!scope.rangeValues.max && scope.rangeValues.max !== 0) || scope.rangeValues.max < scope.rangeValues.min) {
scope.rangeValues.max = scope.rangeValues.min;
autoMax = autoMin;
}
var filterOption = {
name: autoMin + ' - ' + autoMax,
active: true,
type: 'dynamic',
range: { min: scope.rangeValues.min, max: scope.rangeValues.max },
criteria: {
name: filter.name,
value: [{ min: scope.rangeValues.min, max: scope.rangeValues.max }]
}
};
if (filter.type === 'custom') {
filterOption.filterType = 'customFilter';
filterOption.subtype = 'range';
}
applyDynamicFilterOption(filter, filterOption);
scope.rangeValues = {};
};
scope.applyRangeFilter = function (filter) {
if (_.isEmpty(scope.rangeValues)) {
return;
}
var alreadySelected = filter.options[0].range ? filter.options[0].range : undefined;
if (alreadySelected) {
scope.filterCount -= 1;
if (scope.rangeValues.min === alreadySelected.min && scope.rangeValues.max === alreadySelected.max) {
scope.filterCount += 1;
return;
}
}
var autoMin = scope.rangeValues.min, autoMax = scope.rangeValues.max;
if (!scope.rangeValues.min || scope.rangeValues.max < 0) {
scope.rangeValues.min = 0;
autoMin = scope.rangeValues.min; //$filter('i18n')('console.filter.optionName.auto');
}
if (!scope.rangeValues.max || scope.rangeValues.max < scope.rangeValues.min) {
scope.rangeValues.max = scope.rangeValues.min;
autoMax = autoMin;
if (scope.rangeValues.max === 0) {
scope.rangeValues.max = 1;
autoMax = 1;
}
}
var filterOption = {
name: autoMin + ' - ' + autoMax,
active: true,
type: 'dynamic',
range: { min: scope.rangeValues.min, max: scope.rangeValues.max },
criteria: {
name: filter.name,
value: [{ min: scope.rangeValues.min, max: scope.rangeValues.max }]
}
};
filter.options[0] = filterOption;
scope.updateFilterCriteria(filterOption);
};
scope.advancedFilterType = function (filterOption) {
if (scope.consoleType !== EntityVO.TYPE_ASSET) {
return false;
}
return consoleModel.advancedFilterType(filterOption);
};
scope.checkForRangeFilter = function (filterOption) {
if (scope.consoleType !== EntityVO.TYPE_TICKET) {
return false;
}
return consoleModel.checkForRangeFilter(filterOption);
};
scope.validateRangeMinVal = function (filter, val) {
if (filter.range.min && filter.range.min > val) {
scope.rangeValues.min = filter.range.min;
}
};
scope.validateRangeMaxVal = function (filter, val) {
if (filter.range.max && filter.range.max < val) {
scope.rangeValues.max = filter.range.max;
}
};
scope.checkForTimeOnlyFilter = function (filter) {
if (scope.consoleType !== EntityVO.TYPE_TICKET || filter.type !== 'custom') {
return true;
}
return !(filter.oldFields && filter.oldFields.dataType === 'time');
};
scope.checkForDateOnlyFilter = function (filter) {
if (scope.consoleType !== EntityVO.TYPE_TICKET || filter.type !== 'custom') {
return true;
}
return !(filter.oldFields && filter.oldFields.dataType === 'date');
};
scope.showCustomRangeCheck = function (option, filter) {
if (scope.consoleType !== 'ticket' || filter.type !== 'custom') {
return true;
}
return !(filter.type === 'custom' && option.type === 'timeStampRange' && (filter.oldFields.dataType === 'date' || filter.oldFields.dataType === 'time'));
};
scope.rangeValues = {};
}
scope.applyFilterSet = function (filterSet, fromMetric) {
scope.isApplyClicked = true;
consoleModel.applyFilterSet(filterSet, fromMetric);
};
scope.applyUserFilterPreset = function (userFilterPreset, isConsoleLoading) {
var reapplyPreset = false;
if (angular.isDefined(consoleModel.isKcsCoachPreset)) {
consoleModel.isKcsCoachPreset = userFilterPreset.id === 'myTeamKnowledge';
}
scope.advancedQualification.value = '';
if (consoleModel.criteria && consoleModel.criteria.filterCriteria && consoleModel.criteria.filterCriteria.advanceSearchQuery) {
consoleModel.criteria.filterCriteria.advanceSearchQuery = '';
reapplyPreset = true;
}
if (!_.isEmpty(userFilterPreset.columnValue)) {
consoleModel.applyColumnSet(userFilterPreset.columnValue);
scope.$emit(events.UPDATE_GRID_COLUMNS);
scope.appliedUserFilter = userFilterPreset;
consoleModel.appliedPreset = scope.appliedUserFilter;
}
else {
consoleModel.presetColumns = null;
scope.appliedUserFilter = userFilterPreset;
consoleModel.appliedPreset = scope.appliedUserFilter;
consoleModel.populateAttributeNames();
scope.$emit(events.UPDATE_GRID_COLUMNS);
}
if (userFilterPreset.value) {
if (!isUserFilterPresetApplied(userFilterPreset) || reapplyPreset) {
// This check is for when user removes all the filters and selects one preset from the dropdown
if (consoleModel.selectedFilters.length === 0) {
scope.isRemoveClicked = true;
}
//When user preset is changed then move the scroll to top
//otherwise if scroll is at bottom then it fires ng grid scroll event
//which can be fired multiple times(known issue) which leads to set the start index more than chunk size
//so that 0 result sent by server.
if (scope.gridOptions && scope.gridOptions.ngGrid && scope.gridOptions.ngGrid.$viewport) {
scope.gridOptions.ngGrid.$viewport.scrollTop(0);
}
scope.clearAllFilters(isConsoleLoading);
consoleModel.applyFilterSet(userFilterPreset.value);
selectedUserFilter = userFilterPreset;
_.forEach(userFilterPreset.value, function (presetFilter) {
if (presetFilter.filter === 'ticketTypes') {
scope.updateFilterGrouping(presetFilter.option);
}
else if (presetFilter.filter === 'assetTypes') {
scope.updateAssetFilterGrouping(presetFilter.option);
}
else if (presetFilter.filter === 'advanceSearchQuery') {
scope.advancedQualification.value = presetFilter.option;
}
});
}
else {
scope.appliedUserFilter = userFilterPreset;
consoleModel.appliedPreset = scope.appliedUserFilter;
}
}
scope.consoleItems.allSelected = false;
if (!isConsoleLoading) {
scope.clearQuickSearch();
}
};
scope.removeUserFilterPreset = function (userFilterPreset) {
var modalInstance = systemAlertService.modal({
title: $filter('i18n')('common.notification.delete.title'),
text: $filter('i18n')('search.filter.optionName.notification.delete.message'),
buttons: [
{
text: $filter('i18n')('common.labels.yes'),
data: true
},
{
text: $filter('i18n')('common.labels.no'),
data: false
}
]
});
modalInstance.result.then(function (data) {
if (data) {
if (scope.appliedUserFilter && (scope.appliedUserFilter.id === userFilterPreset.id) && scope.appliedUserFilter.id) {
scope.clearAllFilters();
}
consoleModel.removeUserFilterPreset(userFilterPreset);
_.remove(scope.userSavedFilterPresets, { id: userFilterPreset.id });
scope.applyUserFilterPreset(scope.userSavedFilterPresets[0]);
}
});
};
scope.getDefaultFromList = function () {
//this is dirty code need some improvements
//find values for each pre-sets
var defaultPresets = [], userPresets = [], duplicatesPresets = [];
_.each(consoleModel.userSavedPresets, function (preset) {
if (!preset.createDate) {
defaultPresets.push(preset);
}
if (preset.createDate && preset.systemgenerated) {
duplicatesPresets.push(preset);
}
if (!preset.systemgenerated) {
userPresets.push(preset);
}
});
//find id for default preset and match them
_.each(defaultPresets, function (preset) {
var temp = _.find(duplicatesPresets, {
name: preset.name,
systemgenerated: true
});
if (temp) {
preset.id = temp.id;
preset.defaultpreset = temp.defaultpreset;
}
});
return defaultPresets.concat(userPresets);
};
scope.editUserFilterPreset = function (userFilterPreset) {
consolePresetModel.init(scope.consoleType, userFilterPreset);
var modal = $modal.open({
templateUrl: 'views/console/console-edit-filter-preset-action-blade.html',
controller: 'ConsolePresetController',
windowClass: 'action-blade',
keyboard: 'custom',
backdrop: 'custom'
});
modal.result.then(function () {
scope.applyUserFilterPreset(_.find(consoleModel.userSavedPresets, { name: userFilterPreset.name }));
});
};
var configAndFilterFunction = function () {
$q.all([consoleModel.populateFilters(), consoleModel.populateConfiguration(), metadataModel.getMetadataByType(EntityVO.TYPE_GLOBAL)])
.then(function (res) {
if (!configurationModel.showNeedsAttentionFlag) {
scope.ribbonConfig = _.reject(scope.ribbonConfig, { name: 'bulk-need-attention' });
}
else {
scope.showNeedAttentionStat = true;
}
if (configurationModel.showSecurityTickets) {
scope.showSecurityTickets = true;
}
ticketConsoleModel.quickSearchHighlightedColumn = (res[2] && res[2].quickSearchHighlightedCols) || [];
ticketConsoleModel.removeDateRestriction = (res[2] && res[2].removeDateRestriction) || [];
scope.quickSearchColumns = ticketConsoleModel.quickSearchHighlightedColumn;
scope.advancedQualenabled = configurationModel.enableAdvanceSearchInConsole;
var prestsLists = scope.getDefaultFromList();
var setUserPrestOnLoad = _.find(prestsLists, { defaultpreset: true });
if (setUserPrestOnLoad) {
consoleModel.userSavedFilterPresets[0] = setUserPrestOnLoad;
}
if (!consoleModel.areFiltersPopulated) {
scope.applyUserFilterPreset(consoleModel.userSavedFilterPresets[0], true);
scope.appliedUserFilter = selectedUserFilter;
selectedUserFilter = {};
consoleModel.areFiltersPopulated = true;
consoleModel.appliedPreset = scope.appliedUserFilter;
}
else if (consoleModel.kcsFilters && consoleModel.enableKcsFilters()) { //KCS specific conditions
if (consoleModel.kcsFilters.assessmentMode) {
var authorFilter = consoleModel.selectedFilters.length && consoleModel.selectedFilters[0].filterName === 'authors';
scope.clearAllFilters(true);
consoleModel.kcsFilters.teamMemberFilter = authorFilter;
consoleModel.kcsFilters.coachFilter = !authorFilter;
}
else {
scope.clearAllFilters(true);
}
scope.appliedUserFilter = consoleModel.kcsFilters.coachFilter ? consoleModel.userSavedFilterPresets[0] : null;
consoleModel.appliedPreset = scope.appliedUserFilter;
consoleModel.applyKcsFilter();
}
else if (consoleModel.appliedPreset && consoleModel.appliedPreset.name) {
scope.applyUserFilterPreset(consoleModel.appliedPreset, true);
consoleModel.areFiltersPopulated = true;
consoleModel.appliedPreset = scope.appliedUserFilter;
}
else {
var selectedFiltersCopy = _.cloneDeep(consoleModel.selectedFilters), filtersToApply = _.map(selectedFiltersCopy.reverse(), function (filterOption) {
return {
filter: filterOption.filterName || filterOption.criteria.name,
option: filterOption.name
};
});
scope.clearAllFilters(true);
consoleModel.applyFilterSet(filtersToApply);
if (consoleModel.criteria && consoleModel.criteria.filterCriteria && consoleModel.criteria.filterCriteria.advanceSearchQuery) {
scope.advancedQualification.value = consoleModel.criteria.filterCriteria.advanceSearchQuery;
}
}
return scope.init();
})
.finally(function () {
scope.$watch('criteria', function (newValue, oldValue) {
var minimumFilterCount = 2;
if (scope.refreshing) {
return;
}
if (newValue.filterCriteria['statusMappings'] && newValue.filterCriteria['ticketSpecificStatuses']) {
minimumFilterCount = 3;
}
if (newValue.filterCriteria.hasOwnProperty('advanceSearchQuery')) {
minimumFilterCount++;
}
if (Object.keys(newValue.filterCriteria).length < minimumFilterCount) {
consoleModel.setSearchEnabled(false);
}
else {
consoleModel.setSearchEnabled(true);
}
if (newValue.filterCriteria.customColumnRemoved) {
delete newValue.filterCriteria.customColumnRemoved;
scope.getNewItemList();
}
if (!angular.equals(newValue.filterCriteria, oldValue.filterCriteria)) {
if (scope.isApplyClicked || scope.isRemoveClicked
|| scope.quickSearch.isQuickSearchTriggerd) {
scope.getNewItemList();
if (displayMetric) {
scope.getMetrics();
}
}
scope.isApplyClicked = false;
scope.isRemoveClicked = false;
scope.quickSearch.isQuickSearchTriggerd = false;
scope.appliedUserFilter = selectedUserFilter.id ? selectedUserFilter : {};
consoleModel.appliedPreset = scope.appliedUserFilter;
selectedUserFilter = {};
}
else if (!angular.equals(newValue.sortInfo, oldValue.sortInfo) || !angular.equals(newValue.attributeNames, oldValue.attributeNames)) {
scope.getNewItemList();
}
else if (scope.consoleType === EntityVO.TYPE_TICKET && !angular.equals(newValue.customAttributeNames, oldValue.customAttributeNames)) {
scope.getNewItemList();
}
else if (!angular.equals(newValue.chunkInfo, oldValue.chunkInfo)) {
if (refreshNeeded(newValue.chunkInfo.startIndex, oldValue.chunkInfo.startIndex)) {
scope.loadMoreTickets();
}
}
}, true);
scope.openColumnConfigModal();
});
};
if (consoleModel && typeof consoleModel.populateMetadataAndColumns === "function") {
consoleModel.populateMetadataAndColumns().then(function () {
configAndFilterFunction();
});
}
else {
configAndFilterFunction();
}
function refreshNeeded(newStartIndex, oldStartIndex) {
if (newStartIndex < oldStartIndex) {
return false;
}
else {
return scope.itemList[newStartIndex] ? false : true;
}
}
scope.getFilterLabel = function (filter) {
var filterLabel = '';
var filterName = '';
if (filter.filterType === "customFilter") {
filterLabel = (filter.filterLabel) ? (filter.filterLabel + ': ') : '';
filterName = (filter.label ? filter.label : filter.name);
}
else if (filter.criteria && filter.criteria.type === 'date' && filter.criteria.value && filter.criteria.value.length) {
filterName = $filter('datePreConfigTimezone')(filter.criteria.value[0].start, 'mediumDate') + ' ' + $filter('datePreConfigTimezone')(filter.criteria.value[0].start, 'shortTime') +
' - ' + $filter('datePreConfigTimezone')(filter.criteria.value[0].end, 'mediumDate') + ' ' + $filter('datePreConfigTimezone')(filter.criteria.value[0].end, 'shortTime');
filterLabel = filter.filterLabel ? $filter('i18n')('console.filter.name.' + filter.filterLabel) + ': ' : '';
}
else {
filterName = filter.label ? $filter('i18n')('console.filter.optionName.' + filter.label) : filter.name;
filterLabel = filter.filterLabel ? $filter('i18n')('console.filter.name.' + filter.filterLabel) + ': ' : '';
}
return filterLabel + filterName;
};
scope.getMoreSelectedFilterLabel = function (filter) {
var filterLabel = '';
var filterName = '';
if (filter.filterType === "customFilter") {
filterLabel = (filter.filterLabel) ? (filter.filterLabel + ': ') : '';
filterName = (filter.label ? filter.label : filter.name);
}
else {
filterName = filter.label ? ($filter('i18n')('console.filter.optionName.' + filter.label) + (filter.subText ? filter.subText : '')) : filter.name;
filterLabel = filter.filterLabel ? $filter('i18n')('console.filter.name.' + filter.filterLabel) + ': ' : '';
}
return filterLabel + filterName;
};
scope.focusQuickSearch = function () {
$timeout(function () {
angular.element('#console-filter-quicksearch-box').focus();
});
};
scope.displayMetric = displayMetric;
scope.displayFilter = displayFilter;
scope.$on("$stateChangeStart", function (event, toState, toParams, fromState, fromParams) {
if (scope.filter.isOpen) {
scope.cancelFilter();
}
});
}
};
}]);
})();