app.controller('masterScenarioCalendarController', ['$scope', '$http', '$location', '$timeout', function ($scope, $http, $location, $timeout) { $scope.collapsedIcon = 'fa-plus-square'; $scope.nonCollapsedIcon = 'fa-minus-square'; $scope.checkedExpenditures = []; $scope.calendarFilters = { IsTableModeQuantity: true, ShowActuals: false, IsViewModeMonth: true, IsUOMHours: null, ProjectId: null }; $scope.$watch('calendarFilters.IsUOMHours', function (oldValue, newValue) { if (oldValue != newValue) { savePagePreferences(); $scope.getCalendar(); } }); $scope.$watch('calendarFilters.IsTableModeQuantity', function (oldValue, newValue) { if (oldValue != newValue) { savePagePreferences(); } }); $scope.$watch('calendarFilters.IsViewModeMonth', function (oldValue, newValue) { if (oldValue != newValue) { savePagePreferences(); $scope.switchViewMode(); } }); $scope.$watch('calendarFilters.ShowActuals', function (oldValue, newValue) { if (oldValue != newValue) { savePagePreferences(); $scope.getCalendar(); } }); $scope.init = function (data) { if (!data) return; $scope.calendarFilters.ProjectId = data.ProjectId; if (data.PagePreferences && data.PagePreferences.length > 0) { var pagePrefArray = JSON.parse(data.PagePreferences); for (var i = 0; i < pagePrefArray.length; i++) { switch (pagePrefArray[i].Key) { case "uomMode": $scope.calendarFilters.IsUOMHours = pagePrefArray[i].Value; break; case "chMode": $scope.calendarFilters.IsTableModeQuantity = pagePrefArray[i].Value; break; case "monthWeekMode": $scope.calendarFilters.IsViewModeMonth = pagePrefArray[i].Value; break; case "actualsMode": $scope.calendarFilters.ShowActuals = pagePrefArray[i].Value; break; default: console.log("Restore page preferences (Master Scenario Details): data key not found in parsing loaded preferences (datakey=" + pagePrefArray[i].Key + ", dataSection=" + data.DataSection + ")"); } } } if ($scope.calendarFilters.IsUOMHours == null || $scope.calendarFilters.IsUOMHours == undefined) $scope.calendarFilters.IsUOMHours = data.IsUOMHours; $scope.getCalendar(); }; $scope.getCalendar = function () { $scope.data = null; blockUI(); var data = { ProjectId: $scope.calendarFilters.ProjectId, IsUOMHours: $scope.calendarFilters.IsUOMHours, ShowActuals: $scope.calendarFilters.ShowActuals }; $http.post('/Scenarios/GetMasterScenarioCalendar', JSON.stringify(data)) .success(function (data, status, headers, config) { if (!data || !data.Headers || data.Headers.length < 1) { unblockUI(); return; } $scope.data = data; $scope.checkedExpenditures = []; $scope.switchViewMode(); unblockUI(); }) .error(function (data, status, headers, config) { console.log("an error occurred while loading master scenario calendar"); unblockUI(); }); } $scope.onMonthHeaderClick = function (header) { header.Collapsed = !header.Collapsed; header.Show = !header.Show; header.CollapsedClass = header.Collapsed ? $scope.collapsedIcon : $scope.nonCollapsedIcon; if (header.Weeks && header.Weeks.length > 0) for (var i = 0; i < header.Weeks.length; i++) { $scope.data.Headers[header.Weeks[i]].Collapsed = header.Collapsed; $scope.data.Headers[header.Weeks[i]].Show = !header.Collapsed; } $scope.resizeFreezAng(); } $scope.collectchecked = function (obj) { $scope.checkedExpenditures = []; if ($scope.data == null || $scope.data.ScenarioCalendar == null || obj == null) return; for (var i = 1; i < $scope.data.ScenarioCalendar.length; i++) { if (($scope.data.ScenarioCalendar[i].Checked && obj.ExpCatId != $scope.data.ScenarioCalendar[i].ExpCatId) || (!obj.Checked && obj.ExpCatId == $scope.data.ScenarioCalendar[i].ExpCatId)) $scope.checkedExpenditures.push($scope.data.ScenarioCalendar[i]); } $scope.applyChecked(); }; $scope.checkAll = function () { $scope.checkedExpenditures = []; var checked = !$scope.data.ScenarioCalendar[0].Checked; if ($scope.data == null || $scope.data.ScenarioCalendar == null) return; for (var i = 1; i < $scope.data.ScenarioCalendar.length; i++) { $scope.data.ScenarioCalendar[i].Checked = checked; if (checked) $scope.checkedExpenditures.push($scope.data.ScenarioCalendar[i]); } $scope.applyChecked(); }; $scope.onExpCatClick = function (row) { // skip collapsing in actuals mode if ($scope.calendarFilters.ShowActuals) return; row.Collapsed = !row.Collapsed; row.CollapsedClass = row.Collapsed ? $scope.collapsedIcon : $scope.nonCollapsedIcon; }; $scope.resizeFreezAng = function () { setTimeout(function () { resizeFreez('master-scenario-table'); }, 0); }; $scope.applyChecked = function () { $scope.data.ScenarioCalendar[0].Checked = ($scope.checkedExpenditures.length == ($scope.data.ScenarioCalendar.length - 1)); }; $scope.switchViewMode = function () { for (var i = 0; i < $scope.data.Headers.length; i++) { if ($scope.data.Headers[i].IsMonth) { $scope.data.Headers[i].Collapsed = $scope.calendarFilters.IsViewModeMonth; $scope.data.Headers[i].Show = $scope.calendarFilters.IsViewModeMonth; $scope.data.Headers[i].CollapsedClass = $scope.calendarFilters.IsViewModeMonth ? $scope.collapsedIcon : $scope.nonCollapsedIcon; } else { $scope.data.Headers[i].Collapsed = $scope.calendarFilters.IsViewModeMonth; $scope.data.Headers[i].Show = !$scope.calendarFilters.IsViewModeMonth; } } $scope.resizeFreezAng(); }; $scope.getDisabledShowGraph = function () { return $scope.checkedExpenditures.length <= 0 ? "disabled" : ""; }; $scope.graphData = []; $scope.maxGraphValue = 0; $scope.showGraph = function () { // prepare data $scope.maxGraphValue = 0; $scope.graphData = []; var startDate = new Date($scope.data.StartDate); var endDate = new Date($scope.data.EndDate); var days = (endDate - startDate) / (1000 * 60 * 60 * 24); var i; for (i = 1; i < $scope.data.ScenarioCalendar.length; i++) { // skip this row if it is not checked if (!$scope.data.ScenarioCalendar[i].Checked) continue; var expCatData = new Array(); for (var colIndex = 0; colIndex < $scope.data.Headers.length; colIndex++) { if ($scope.data.Headers[colIndex].IsMonth) continue; expCatData[expCatData.length] = [$scope.data.Headers[colIndex].Milliseconds, $scope.data.ScenarioCalendar[i].QuantityValues[colIndex]]; if (true !== $scope.calendarFilters.IsTableModeQuantity) { expCatData[expCatData.length - 1][1] = $scope.data.ScenarioCalendar[i].CostValues[colIndex]; } if (expCatData[expCatData.length - 1][1] > $scope.maxGraphValue) $scope.maxGraphValue = expCatData[expCatData.length - 1][1]; expCatData[expCatData.length - 1][1] = expCatData[expCatData.length - 1][1].toFixed(2); } $scope.graphData[$scope.graphData.length] = { label: $scope.data.ScenarioCalendar[i].ExpCatName, data: expCatData, lines: { show: true, fill: true, steps: false }, filledPoints: true, stack: true }; } $timeout(function () { $scope.initGraph() }); }; $scope.initGraph = function () { // Init Chart $('#DetailsGraphContainer').html('
'); $("#modalGraph").modal("show"); // SA. ENV-574 var C_MAX_GRAPH_TICK_LABELS = 8; var maxSeriePoints = 0; if ($scope.graphData && $scope.graphData.length > 0) { $.each($scope.graphData, function (index, data) { if (data.data && (data.data.length > 0)) { maxSeriePoints = Math.max(maxSeriePoints, data.data.length); } }); } var tickStep = Math.round((maxSeriePoints / 4) / C_MAX_GRAPH_TICK_LABELS); tickStep = (tickStep > 0) ? tickStep : 1; $('#divDetailsGraph').pixelPlot($scope.graphData, { series: { points: { show: false }, lines: { show: true, } }, xaxis: { mode: "time", tickSize: [tickStep, "month"] } }, { height: 405, width: 405, tooltipText: "y + ' units at ' + (new Date(x)).toString()" }); }; }]);