'use strict'; app.controller('activityCalendarLiteController', ['$scope', '$http', '$location', '$timeout', function($scope, $http, $location, $timeout) { $scope.CollapsedIcon = 'fa-plus-square'; $scope.NonCollapsedIcon = 'fa-minus-square'; $scope.id = $location.absUrl().substr($location.absUrl().lastIndexOf('ls/') + 3, 36); $scope.dataSection = ""; $scope.pageTitle = ""; // SA. ENV-905 $scope.preferences = []; $scope.calendarFilters = { StartDate: null, EndDate: null, TeamId: null, IsUOMHours: null, PreferredTotalsDisplaying: null, IsBarMode: true, ShowAvgTotals: true, IsCapacityModeActuals: true, sortOrder: false, sortBy: 'Name', ShowCapacity: 1 }; $scope.SpreadType = { Notspecified: -1, Equal: 0, Less: 1, Over: 2 }; $scope.data = null; $scope.grandtotalrow = null; function compareValues(val1, val2) { if (!isNaN(parseFloat(val1)) && !isNaN(parseFloat(val2))) { var val1_ = Math.round(parseFloat(val1) * 1000) / 1000; var val2_ = Math.round(parseFloat(val2) * 1000) / 1000; if ((-0.005 > (val1_ - val2_))) return -1; else if ((0.005 < (val1_ - val2_))) return 1; else return 0; } } function updateCSSClass(row, resRow, colIndex) { if (resRow == null) { // updating project/scenario/category row if (row == null || colIndex == null || row.SpreadVal == null || row.SpreadVal.length <= colIndex) return; if (!row.ReadOnly[colIndex]) { if (row.SpreadVal[colIndex] == $scope.SpreadType.Equal) row.CSSClass[colIndex] = 'cellequal'; else if (row.SpreadVal[colIndex] == $scope.SpreadType.Over) row.CSSClass[colIndex] = 'cellover'; else if (row.SpreadVal[colIndex] == $scope.SpreadType.Less) row.CSSClass[colIndex] = 'cellless'; if (row.RowType == 0) { if (row.Color != null && row.Color.length > 0 && $scope.calendarFilters.IsBarMode) row.BarStyle[colIndex] = "background-color: " + row.Color + ";border-right-color: " + row.Color + " !important;"; //"{'background-color': '"+ row.Color +"','border-right-color': '"+ row.Color +" !important'}"; //{"background-color": "' + row.Color + '","border-right-color": "' + row.Color + ' !important;"}'; //row.BarStyle[colIndex] = 'background-color: ' + row.Color + ';border-right-color: ' + row.Color + ' !important;'; else row.BarStyle[colIndex] = null; } } //TODO: assign more classes if needed } else { // updating resource row if (row == null || colIndex == null || row.ReadOnly[colIndex]) return; if (row.RowType == 1) { // top part of the grid //if(!$scope.data.Headers[colIndex].IsMonth && resRow.QuantityValues[colIndex] > resRow.CapacityQuantityValues[colIndex]) // resRow.CSSClass[colIndex] = 'cellover'; } else { // bottom part of the grid if (resRow.SpreadVal == null || resRow.SpreadVal.length <= colIndex) return; if (resRow.SpreadVal[colIndex] == $scope.SpreadType.Equal) resRow.CSSClass[colIndex] = 'cellequal'; else if (resRow.SpreadVal[colIndex] == $scope.SpreadType.Over) resRow.CSSClass[colIndex] = 'cellover'; else if (resRow.SpreadVal[colIndex] == $scope.SpreadType.Less) resRow.CSSClass[colIndex] = 'cellless'; } //TODO: assign more classes if needed } } function refreshTotalSpreadVal(expCatTotal) { if (expCatTotal.RowType == 7) { for (var colIndex = 0; colIndex < $scope.data.Headers.length; colIndex++) { var resValue = expCatTotal.QuantityTotalResValue[colIndex]; var total = 0; var allocated = 0; if ($scope.calendarFilters.ShowCapacity == 1) { allocated = expCatTotal.QuantityValues[colIndex]; total = expCatTotal.QuantityTotalResValue[colIndex]; } else if ($scope.calendarFilters.ShowCapacity == 2) { // collection name changed just for compatibility in the names allocated = expCatTotal.QuantityTotalAllocatedValue[colIndex]; total = expCatTotal.QuantityTotalResValue[colIndex]; } else { allocated = expCatTotal.QuantityValues[colIndex]; // collection name changed just for compatibility in the names total = expCatTotal.QuantityTotalAllocatedValue[colIndex]; } var compareRes = compareValues(allocated, total); expCatTotal.SpreadVal[colIndex] = compareRes > 0 ? $scope.SpreadType.Over : compareRes < 0 ? $scope.SpreadType.Less : compareRes == 0 ? $scope.SpreadType.Equal : $scope.SpreadType.Notspecified; var expCatSpread = $scope.SpreadType.Equal; if (expCatTotal.SpreadVal[colIndex] == $scope.SpreadType.Over) { expCatSpread = $scope.SpreadType.Over; } else if (expCatTotal.SpreadVal[colIndex] == $scope.SpreadType.Less) { expCatSpread = $scope.SpreadType.Less; } expCatTotal.SpreadVal[colIndex] = expCatSpread; updateCSSClass(expCatTotal, null, colIndex); } } //updateCSSClass(expCatTotal, null, colIndex); } function initCmMenuItems() { var menu = $('#' + $scope.calendarFilters.MenuId); menu.html(""); $.each($("#menuCalendarOptionsPrint li"), function (i, o) { $(o).appendTo(menu); }); $.each($("#menuCalendarOptions li"), function (i, o) { $(o).clone(true).appendTo(menu).change(function () { if (typeof onCMPreferencesItemClick === 'function') onCMPreferencesItemClick(menu); }); }); // SA. ENV-815 $("#menuCalendarOptions").find('*[data-key]').removeAttr('data-key'); } $scope.init = function (initData) { $scope.calendarFilters.IsUOMHours = initData.IsUOMHours; $scope.calendarFilters.PreferredTotalsDisplaying = initData.PreferredTotalsDisplaying; $scope.calendarFilters.IsBarMode = initData.IsBarMode; $scope.calendarFilters.IsCapacityModeActuals = initData.IsCapacityModeActuals; $scope.calendarFilters.StartDate = initData.StartDate; $scope.calendarFilters.EndDate = initData.EndDate; $scope.calendarFilters.ModelType = initData.ModelType; $scope.calendarFilters.MenuId = initData.MenuId; $scope.dataSection = initData.DataSection; $scope.calendarFilters.ShowCapacity = (initData.ShowCapacity) ? initData.ShowCapacity : 1; $scope.pageTitle = initData.PageTitle; // SA. ENV-905 initCmMenuItems(); var pagePrefArray; if (initData.PagePreferences && initData.PagePreferences.length > 0) { // SA. ENV-799 var filterModeInit = ""; pagePrefArray = JSON.parse(initData.PagePreferences); $scope.preferences = pagePrefArray; for (var i = 0; i < pagePrefArray.length; i++) { switch (pagePrefArray[i].Key) { case "uomMode": $scope.calendarFilters.IsUOMHours = pagePrefArray[i].Value; break; case "capBarMode": $scope.calendarFilters.IsBarMode = pagePrefArray[i].Value; break; case "capacityView": $scope.calendarFilters.IsCapacityModeActuals = pagePrefArray[i].Value; break; case "sortBy": $scope.calendarFilters.sortBy = pagePrefArray[i].Value; break; case "showOption": $scope.calendarFilters.ShowCapacity = pagePrefArray[i].Value != null ? pagePrefArray[i].Value : 1; break; case "sortOrder": $scope.calendarFilters.sortOrder = pagePrefArray[i].Value; break; default: console.log("Restore page preferences (Activity calendar): data key not found in parsing loaded preferences (datakey=" + pagePrefArray[i].Key + ", dataSection=" + $scope.dataSection + ")"); } } } pagePrefArray = []; pagePrefArray.push({ Key: "uomMode", Value: $scope.calendarFilters.IsUOMHours }); pagePrefArray.push({ Key: "capBarMode", Value: $scope.calendarFilters.IsBarMode }); pagePrefArray.push({ Key: "showOption", Value: $scope.calendarFilters.ShowCapacity }); pagePrefArray.push({ Key: "capacityView", Value: $scope.calendarFilters.IsCapacityModeActuals }); restorePreferences($scope.dataSection, pagePrefArray); $('#' + $scope.calendarFilters.MenuId).click(function (event) { event.stopPropagation(); }); if (initData.TeamId && (initData.TeamId.length > 0)) { $scope.calendarFilters.TeamId = initData.TeamId; } // SA. ENV-799 $scope.loadCalendarData(); }; $scope.IsEmpty = function (value) { return !value || (value == null) || (value == ""); } $scope.getCalendar = function () { $("#acLoader").hide(); if ($scope.IsEmpty($scope.calendarFilters.TeamId)) { return; } $scope.data = null; $("#acLoader").show(); var postData = JSON.parse(JSON.stringify($scope.calendarFilters)); $http.post('/CapacityManagement/LoadJsonCalendar', postData). success(function (data, status, headers, config) { if (data == null) { $("#acLoader").hide(); $('#capacity-table-empty').show(); } if (data != null && data.Headers == null || data.Headers.length < 1) { $("#acLoader").hide(); $('#capacity-table-empty').show(); return; } $scope.data = data; $scope.calendarFilters.ShowAvgTotals = data.PreferredTotalsDisplaying; $scope.calendarFilters.IsUOMHours = data.IsUOMHours; swithViewMode(); // initial setup css classes for (var rowIndex = 0; rowIndex < $scope.data.Calendar.length; rowIndex++) { if ($scope.data.Calendar[rowIndex].CSSClass == null) $scope.data.Calendar[rowIndex].CSSClass = new Array($scope.data.Headers.length); if ($scope.data.Calendar[rowIndex].BarStyle == null) $scope.data.Calendar[rowIndex].BarStyle = new Array($scope.data.Headers.length); refreshTotalSpreadVal($scope.data.Calendar[rowIndex]); for (var colIndex = 0; colIndex < $scope.data.Headers.length; colIndex++) { if ($scope.data.Calendar[rowIndex].RowType != 7) updateCSSClass($scope.data.Calendar[rowIndex], null, colIndex); if ($scope.data.Calendar[rowIndex].Resources != null && $scope.data.Calendar[rowIndex].Resources.length > 0) { for (var resIndex = 0; resIndex < $scope.data.Calendar[rowIndex].Resources.length; resIndex++) { if ($scope.data.Calendar[rowIndex].Resources[resIndex].CSSClass == null) $scope.data.Calendar[rowIndex].Resources[resIndex].CSSClass = new Array($scope.data.Headers.length); updateCSSClass($scope.data.Calendar[rowIndex], $scope.data.Calendar[rowIndex].Resources[resIndex], colIndex); } } } } for (var i = 0; i < $scope.data.Calendar.length; i++) { if ($scope.data.Calendar[i].IsTotals && $scope.data.Calendar[i].RowType == 5) { $scope.grandtotalrow = $scope.data.Calendar[i]; break; } } $("#acLoader").hide(); $('#capacity-table-empty').show(); }). error(function (data, status, headers, config) { // called asynchronously if an error occurs // or server returns response with an error status. console.log("an error occurred while loading calendar"); $("#acLoader").hide(); $('#capacity-table-empty').show(); }); }; $scope.switchUOMMode = function (value) { var newValue = value != null ? value : !$scope.calendarFilters.IsUOMHours; $scope.calendarFilters.IsUOMHours = newValue; $scope.getCalendar(); }; $scope.switchCapacityVew = function (value) { var newValue = value != null ? value : !$scope.calendarFilters.IsCapacityModeActuals; $scope.calendarFilters.IsCapacityModeActuals = newValue; $scope.getCalendar(); }; $scope.changeSortBy = function (value) { $scope.calendarFilters.sortBy = value; $scope.getCalendar(); }; $scope.switchSortOrder = function () { var newValue = !$scope.calendarFilters.sortOrder; $scope.calendarFilters.sortOrder = newValue; $scope.getCalendar(); }; $scope.changeCapacity = function (value) { $scope.calendarFilters.ShowCapacity = value; if (!$scope.data) return; for (var i = 0; i < $scope.data.Calendar.length; i++) { if ($scope.data.Calendar[i].RowType == 7) { var expCatTotal = $scope.data.Calendar[i]; refreshTotalSpreadVal(expCatTotal); } } // force digest cycle as this method could being called from JS $scope.$digest(); }; function swithViewMode() { if (!$scope.data) return; var newValue = true; // $scope.calendarFilters.IsViewModeMonth; var j; for (j = 0; j < $scope.data.YearHeaders.length; j++) { $scope.data.YearHeaders[j].SpanCount = 0; } for (var i = 0; i < $scope.data.Headers.length; i++) { var header = $scope.data.Headers[i]; if (header.IsMonth) { header.Collapsed = newValue; header.Show = newValue; header.CollapsedClass = newValue ? $scope.CollapsedIcon : $scope.NonCollapsedIcon; for (j = 0; j < $scope.data.YearHeaders.length; j++) { if ($scope.data.YearHeaders[j].Title == header.Year) { if (newValue) { $scope.data.YearHeaders[j].SpanCount++; // -= header.Weeks.length - 1; } else { $scope.data.YearHeaders[j].SpanCount += header.Weeks.length; } //break; } } } else { header.Collapsed = newValue; header.Show = !newValue; } header.Initialized = header.Initialized || header.Show; } getVisibleCellCount(); } function getVisibleCellCount() { var count = 0; for (var i = 0; i < $scope.data.Headers.length; i++) { var header = $scope.data.Headers[i]; if (header.Show) count++; } $scope.data.VisibleCellCount = count; } $scope.switchBarMode = function () { var newValue = !$scope.calendarFilters.IsBarMode; $scope.calendarFilters.IsBarMode = newValue; if (!$scope.data) return; // update project rows CSS for (var rowIndex = 0; rowIndex < $scope.data.Calendar.length; rowIndex++) { if ($scope.data.Calendar[rowIndex].RowType == 0) for (var colIndex = 0; colIndex < $scope.data.Headers.length; colIndex++) { updateCSSClass($scope.data.Calendar[rowIndex], null, colIndex); } } // force digest cycle as this method could being called from JS $scope.$digest(); }; $scope.CalcRemainingCapacity = function (capacity, index) { return (capacity - $scope.grandtotalrow.QuantityValues[index]); // - $scope.vacationrow.QuantityValues[index] - $scope.trainingrow.QuantityValues[index]); }; function getEventTargetId($event) { if (!$event.target.id || $event.target.id === '') return $event.target.parentElement.id; return $event.target.id; } $scope.loadCalendarData = function () { $scope.getCalendar(); } }]);