app-route.js 5.48 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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
/// <reference path="../app.js" />
/// <reference path="../../Scripts/underscore.js" />

// Responsible for application route from tab and menu
frameworkModule.factory('appRoute', ['$log', 'enums', '$window', '$location', 'appTabManager', '$route', 
    function ($log, enums, $window, $location, appTabManager, $route) {
        'use strict';
        $log.debug('appRoute.ctor()...');

        var hasValue = function (value) {
            return !_.isUndefined(value) && !_.isNull(value) && (value !== '');
        };

        //
        // constructors of event data
        //
        var comonEventData = function (eventData)
        {
            this.needActivate = typeof (eventData) === "undefined" ? true : eventData.needActivate;
            if (eventData != null) {
                this.param = eventData.param || null;
                this.url = eventData.url || null;
                this.stateName = eventData.stateName || null;
            }
           
        };

        var chartSettingEventData = function (eventData)
        {
            this.needActivate = typeof (eventData) === "undefined" ? true : eventData.needActivate;
            if (eventData != null) {
                this.param = eventData.param || null;
                this.url = eventData.url || null;
                this.chartId = eventData.chartId || null;
            }
        };

        var setLocationPathToRouteUrl = function (routeUrl, isNew) {
            if (!hasValue(isNew)) {
                // when navigate from tab we need update the path
                $location.path(routeUrl).search('isNew', null);
            } else {
                // when navigate from tab we need update the path
                $location.path(routeUrl).search('isNew', isNew);
            }

            $log.debug('$locatoin.path: ' + $location.path());
        };


        /// <summary>
        /// contains tabType and eventData information for current route of the navigate is called each time
        /// you can pass param by eventData for route and check appRoute.current.eventData.Param
        /// </summary>
        var currentRoute = {
            // Indicate whether current route is internal navigation or navigation from UI.
            isInternal: false,
            tabType: null,
            eventData: null,
            // set the data for internal navigation
            set: function (tabType, eventData) {
                currentRoute.isInternal = true;
                currentRoute.tabType = tabType;
                currentRoute.eventData = eventData;
            },
            // reset the data for navigation from UI
            reset: function () {
                currentRoute.isInternal = false;
                currentRoute.tabType = null;
                currentRoute.eventData = null;
            }
        };

        var navigateFromTab = function (tab, triggeredbyUser) {
            /// <summary>
            /// navigate from specified tab, this method should only be called when tab clicked on UI
            /// </summary>
            /// <param name="tab">the clicked tab</param>

            $log.debug('appRoute.navigateFromTab(): ' + (tab ? JSON.stringify(tab.tabName) : 'null'));

            if (!tab) {
                return;
            }

            if (triggeredbyUser) {
                currentRoute.reset();
            }

            // when navigate from tab we need update the path
            setLocationPathToRouteUrl(tab.url, tab.isNew);


        };

        // add new tab or active the tab if the tab is already opened
        var loadTab = function (tabType, eventData, onTabLoaded) {
            if (tabType === enums.tabType.main) {
                appTabManager.loadMainTab(eventData, onTabLoaded);
            }
            else if (tabType === enums.tabType.dashboard)
            {
                appTabManager.loadDashboardTab(eventData, onTabLoaded);
            }
            else if (tabType === enums.tabType.chartEdit)
            {
                appTabManager.loadChartEditTab(eventData, onTabLoaded);
            }
            else if (tabType === enums.tabType.commonUrl)
            {
                appTabManager.loadCommonUrlTab(eventData, onTabLoaded);
            }
            else
            {
                $log.error(tabType + ' is not supported!');
            }
        };
        var routeEvents = {
            navigated: 'app-route-navigated'
        };
        var navigate = function (tabType, eventData) {
            currentRoute.set(tabType, eventData);

            loadTab(tabType, eventData, function (tab, eventData) {
                $log.debug('appRoute.onTabLoaded(): ' + tab.tabName);

                // set active for current tab
                tab.active = eventData.needActivate;

                if (tab.url !== '/') {
                    setLocationPathToRouteUrl(tab.url, tab.isNew);

                    if ($route.current) {
                        $route.current.scope.$broadcast(routeEvents.navigated);
                    }
                } else {
                    appTabManager.closeTab(tab.tabId);
                }
            });
        };


      

        (function initialize() {
            $log.debug('appRoute.initialize()...');
        })();

        // public properties and methods of appRoute
        return {
            navigateFromTab: navigateFromTab,
            navigate: navigate,
            comonEventData: comonEventData,
            chartSettingEventData: chartSettingEventData,
            current: currentRoute,
            routeEvents: routeEvents
        };
    }
]);