jhanink
10/2/2013 - 5:13 PM

relatedReportDashboardControllers.js

controllers.controller('ReportChartContainerCtrl', ['$scope', '$rootScope', '$http', '$timeout', 'MacAppUtils', function ($scope, $rootScope, $http, $timeout, MacAppUtils)
//--------------------------------------------------------------------
{
    $scope.chart = {
        metricType: Const.Value.NONE,
        data: [-1, -1, -1],
        type: Const.Charts.Type.GRAPH,
        size: Const.Charts.Size.NORMAL,
        templateUrl:'app/partials/reports/components/dashboard/EmptyComponent.html',
        tableTemplateUrl:'app/partials/reports/components/dashboard/TableComponentSmall.html',
        state: {
            categoryId_initialized: false,
            dateRange_initialized: false,
            isLoading: true
        }
    };

    $scope.ccHelper =
    {
        setMetricType: function (metricType)
        {
            $scope.chart.metricType = metricType;
        },
        getMetricType: function() {
            return $scope.chart.metricType;
        },
        toggleChartSize:function()
        {
            $scope.chart.size = ($scope.chart.size === Const.Charts.Size.NORMAL?Const.Charts.Size.LARGE:Const.Charts.Size.NORMAL);
            if (this.isChartMinimized()) {
                $scope.rdState.currentlyMaximizedChart = Const.Value.NONE;
            } else {
                $scope.rdState.currentlyMaximizedChart = this.getMetricType();
            }
            $scope.chart.tableTemplateUrl = (this.isChartMinimized()?'app/partials/reports/components/dashboard/TableComponentSmall.html':'app/partials/reports/components/dashboard/TableComponentLarge.html');
            this.forceUpdateChartSize();
        },
        forceRedrawChart: function()
        {
            var chartState = this.getChartState();
            chartState.forceRedrawChart = Math.random();
        },
        forceUpdateChartSize: function()
        {
            var chartState = this.getChartState();
            chartState.forceUpdateChartSize = Math.random();
            this.forceRedrawChart();
        },
        getChartSize: function()
        {
            return $scope.chart.size;
        },
        isChartMinimized: function()
        {
            return this.getChartSize() === Const.Charts.Size.NORMAL;
        },
        isChartMaximized: function()
        {
            return this.getChartSize() === Const.Charts.Size.LARGE;
        },
        getChartHeight: function()
        {
            return this.getChartSize() === Const.Charts.Size.NORMAL?170:470;
        },
        getChartWidth: function()
        {
            return this.getChartSize() === Const.Charts.Size.NORMAL?310:970;
        },
        toggleChartType: function()
        {
            this.setChartType(($scope.chart.type === Const.Charts.Type.GRAPH)?Const.Charts.Type.TABLE:Const.Charts.Type.GRAPH);
        },
        getChartState: function()
        {
            return $scope.rdState.charts[$scope.instance];
        },
        setChartType: function(type)
        {
            if (type === Const.Value.NO_DATA) {
                gLog.noop('no-data');
                $scope.chart.templateUrl = 'app/partials/reports/components/dashboard/DataUnavailableComponent.html';
                return;
            }

            if (type === Const.Value.EMPTY_STRING) {
                $scope.chart.templateUrl = 'app/partials/reports/components/dashboard/EmptyComponent.html';
                return;
            }

            if (type === Const.Charts.Type.GRAPH || type === Const.Charts.Type.TABLE) {
                $scope.chart.type = type;
                gLog.noop('$scope.ccHelper.getChartState()', this.getChartState());
                this.getChartState().chartType = type;
            }

            // force update
            $scope.chart.templateUrl = ($scope.chart.type == "graph"?'app/partials/reports/components/dashboard/GraphComponent.html':'app/partials/reports/components/dashboard/TableComponent.html');
        },
        getMetricDisplayName: function(metricName) {
            return MacAppUtils.getMetricDisplayName(metricName, 'mls');
        }
    };

    $scope.init = function(instance)
    {
        this.instance = instance;
        for (var tempMetric in this.maps.KEY_METRICS_TYPE_MAP) {
            gLog.noop('setting chart metric type ('+tempMetric+') for instance ('+instance+')');

            // NOTE: This sets the order of the charts (prefs.chartsOrder)
            if ($scope.rdHelper.getInstanceForMetric(tempMetric) === instance) {
                $scope.ccHelper.setMetricType(tempMetric);
                break;
            }
        }
    };
}]);


//---------------------------------------
controllers.controller('ReportGraphComponentCtrl', ['$scope',function ($scope)
{
    var GraphComponent = {
        componentName: "GraphComponent"
    };

    $scope.c = GraphComponent;
    $scope.init = function() {};
}]);


//-----------------------------------
controllers.controller('ReportTableComponentCtrl', ['$scope', '$filter', 'MacAppUtils', function ($scope, $filter, MacAppUtils)
//-----------------------------------
{
    var TableComponent = {
        componentName: "TableComponent"
    };
    $scope.c = TableComponent;

    var mls = $filter('mls');

    var metricTypeName = $scope.chart.metricType;

    var metricTypeDisplayName = MacAppUtils.getMetricDisplayName(metricTypeName, 'mls');

    gLog.noop('rdData.trendMetrics['+metricTypeName+'].tableData', $scope.rdData.trendMetrics[metricTypeName].tableData);

    $scope.tableData = $scope.rdData.trendMetrics[metricTypeName].tableData;

    $scope.rtcHelper = {};

    $scope.rtcHelper.metricColumnSortFunction = function (a,b) {
        if (a === Const.Value.DOUBLE_DASH) {
            a = 0;
        }
        if (b === Const.Value.DOUBLE_DASH) {
            b = 0;
        }

        // coerce to numbers
        var x = ''+a;
        var y = ''+b;

        x = MacAppUtils.numberFunctions.numericContents(x);
        y = MacAppUtils.numberFunctions.numericContents(y);

        return x - y;
    };

    // custom sort function for Weekly (e.g. 'Week 10') string values that need to be chronologically ordered
    $scope.rtcHelper.weekColumnSortFunction = function(a,b) {
        var weekNoA = MacAppUtils.numberFunctions.numericContents(a);
        var weekNoB = MacAppUtils.numberFunctions.numericContents(b);
        return weekNoA - weekNoB;
    };

    $scope.rtcHelper.dateColumnSortFunction = function(a,b) {
        var dateA = MacAppUtils.numberFunctions.numericContents(a);
        var dateB = MacAppUtils.numberFunctions.numericContents(b);
        return dateA - dateB;
    };

    // custom sort function for Monthly (e.g. 'Jan 2013') string values that need to be chronologically ordered
    $scope.rtcHelper.monthlyDateColumnSortFunction = function(a,b) {
        return $scope.rdState.reverseMonthLookup[a] - $scope.rdState.reverseMonthLookup[b];
    };

    $scope.init = function() {
        $scope.gridOptions = {
            data:'tableData',
            canSelectRows: 0,
            showSelectionCheckbox: false,
            enableRowSelection:false,
            headerRowHeight:23,
            rowHeight:20,
            footerRowHeight:30,
            enableColumnReordering:true,
            columnDefs: []
        };

        if ($scope.rdHelper.isTrendView(Const.DAILY)) {
            $scope.gridOptions.columnDefs.push (
                {field: 'date',displayName:mls('date.date')}
            );
        } else if ($scope.rdHelper.isTrendView(Const.WEEKLY)) {
            if ($scope.rdHelper.isShowYearOverYear()) {
                $scope.gridOptions.columnDefs.push (
                    {field: 'week',displayName:mls('date.week'), sortFn:$scope.rtcHelper.weekColumnSortFunction}
                );
                $scope.gridOptions.columnDefs.push (
                    {field: 'date', displayName:mls('date.beg_date'), sortFn:$scope.rtcHelper.dateColumnSortFunction}
                );
            } else {
                $scope.gridOptions.columnDefs.push (
                    {field: 'week',displayName:mls('date.week'), sortFn:$scope.rtcHelper.weekColumnSortFunction}
                );
            }
        } else { // must be MONTHLY
            $scope.gridOptions.columnDefs.push (
                {field: 'date',displayName:mls('date.date'), sortFn: $scope.rtcHelper.monthlyDateColumnSortFunction}
            );
        }

        $scope.gridOptions.columnDefs.push (
            {field: 'currentYear', displayName: metricTypeDisplayName, sortFn: $scope.rtcHelper.metricColumnSortFunction}
        );

        if ($scope.rdHelper.isShowYearOverYear()) {
            // DON"T SHOW LAST YEAR IN THE TABLE VIEW ANYMORE, BUT KEEP AROUND FOR DEBUGGING
            /*$scope.gridOptions.columnDefs.push (
             {field: 'lastYear', displayName: 'Last Year', sortFn: metricSortFunction}
             );*/
            $scope.gridOptions.columnDefs.push (
                {field: 'YoY', displayName: 'Y/Y', cellTemplate:'<span ng-class="{rpdYoYNegativeValues: row.getProperty(col.field).indexOf(\'-\') > -1}">{{row.getProperty(col.field)}}</span>'}
            );
        }
    };

    $scope.init();
}]);