app-nav-tab.ctrl.js 4.63 KB
Newer Older
eddie.woo's avatar
eddie.woo committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
/// <reference path="../../../Scripts/underscore.js" />
/// <reference path="../../common/utils/enums.js" />
/// <reference path="../nav-bar-util.js" />

// AppNavBarController controller for the navigation bar of the application. This controller is responsible for 
// manage the different types of the tabs and corresponding menus.
frameworkModule.controller('AppNavTabController', ['$scope', '$rootScope', '$log', 'enums', 'appTabManager', 'appRoute', '$timeout', 
function ($scope, $rootScope, $log, enums, appTabManager, appRoute, $timeout)
{
        'use strict';
        $log.debug('AppNavTabController.ctor()...');

        var triggeredbyUser = false;
        // select the specified tab from UI or code
        var selectTab = function (tab) {
            $log.debug('AppNavTabController.selectTab(): ' + JSON.stringify({
                tabId: tab.tabId,
                tabName: tab.tabName
            }));

            if ($scope.selectedTab !== tab) {
                appRoute.navigateFromTab(tab, triggeredbyUser);
                triggeredbyUser = false
                $scope.selectedTab = tab;
                appTabManager.selectOpenedTab(tab);
            }           
        };

        // click the specified tab from UI
        var clickTab = function (tab) {
            $log.debug('AppNavTabController.clickTab(): ' + JSON.stringify({
                tabId: tab.tabId,
                tabName: tab.tabName
            }));

            triggeredbyUser = true;

           
        };

        var navigate = function (tabType, eventData) {
            $timeout(function () {
                appRoute.navigate(tabType, eventData);
            }, 0);
        };

        (function initialize() {

            $log.debug('AppNavTabController.initialize()...');

            // properties
            $scope.selectedTab = null;
            $scope.tabs = appTabManager.tabs;

            // methods
            $scope.selectTab = selectTab;
            $scope.clickTab = clickTab;
            $scope.closeTab = function (index) {
                $timeout(function () {
                    appTabManager.closeTabByIndex(index);

                    appRoute.navigateFromTab($scope.tabs[0], triggeredbyUser);
                    triggeredbyUser = false
                    $scope.selectedTab = $scope.tabs[0];
                    appTabManager.selectOpenedTab($scope.tabs[0]);
                }, 0);
            };
            
            // register events
            $rootScope.$on('event:editChart', function (e, d)
            {
                navigate(enums.tabType.chartEdit,
                new appRoute.chartSettingEventData({
                    needActivate: true,
                    chartId: d
                }));
                $scope.selectedTab = null;
            });

            // register events
            $rootScope.$on('event:closeCurrentTab', function ()
            {
                appTabManager.closeCurrentTab();

                appRoute.navigateFromTab($scope.tabs[0], triggeredbyUser);
                triggeredbyUser = false
                $scope.selectedTab = $scope.tabs[0];
                appTabManager.selectOpenedTab($scope.tabs[0]);
            });

            // always load worklist as primary tab at the very beginning

            
            navigate(enums.tabType.dashboard, 
                new appRoute.comonEventData({
                    needActivate: true
                }));
            

            // register events
            $scope.$on('$stateChangeStart',
                function (event, toState, toParams)
                {
                    // transitionTo() promise will be rejected with 
                    // a 'transition prevented' error
                   
                    if (toState == null) {
                        return;
                    }

                    var url = toState.url;

                    var paramList = Object.keys(toParams);

                    _.forEach(paramList, function (item)
                    {
                        url = url.replace("{" + item + "}", toParams[item]);
                    });
                    
                    if (typeof (toState.name) !== 'undefined')
                    {
                       

                        $log.debug("state name:" + toState.name);

                        navigate(enums.tabType.commonUrl,
                        new appRoute.comonEventData({
                            needActivate: true,
                            stateName:toState.name,
                            url: url,
                            param: toParams.param
                        }));
                    }
                });

        }());
    }
]);