app-route.js 5.48 KB
/// <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
        };
    }
]);