UNPKG

pip-webui

Version:

HTML5 UI for LOB applications

151 lines (122 loc) 6.01 kB
/** * @file Application router extended from ui.router * @copyright Digital Living Software Corp. 2014-2016 */ /* global angular */ (function () { 'use strict'; var thisModule = angular.module('pipState', ['ui.router', 'pipTranslate', 'pipAssert']); thisModule.config( function($locationProvider, $httpProvider, pipTranslateProvider) { // Switch to HTML5 routing mode //$locationProvider.html5Mode(true); pipTranslateProvider.translations('en', { 'ERROR_SWITCHING': 'Error while switching route. Try again.' }); pipTranslateProvider.translations('ru', { 'ERROR_SWITCHING': 'Ошибка при переходе. Попробуйте ещё раз.' }); } ); thisModule.run( function($rootScope, pipTranslate, $state) { $rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) { if ($rootScope.$user && $rootScope.$user.id && toState.params && toState.params.access) { if (toParams && toParams.party_id) { var party = _.find($rootScope.$user.party_access, {party_id: toParams.party_id}); if (party) { if (toState.params.access == 'manager' && !party.manager || toState.params.access == 'contributor' && !party.contributor) { if (toState.params.toState) { event.preventDefault(); $state.go(toState.params.toState, toParams); return; } } } else { if (toParams.party_id != $rootScope.$user.id && toState.params.toState) { event.preventDefault(); $state.go(toState.params.toState, toParams); return; } } } } // Unset routing variable to disable page transition $rootScope.$routing = false; // Record current and previous state $rootScope.$state = {name: toState.name, url: toState.url, params: toParams}; $rootScope.$prevState = {name: fromState.name, url: fromState.url, params: fromParams}; } ); // Intercept route error $rootScope.$on('$stateChangeError', function(event, toState, toParams, fromState, fromParams, error) { // Unset routing variable to disable page transition $rootScope.$routing = false; console.error('Error while switching route to ' + toState.name); console.error(error); } ); // Intercept route error $rootScope.$on('$stateNotFound', function(event, unfoundState, fromState, fromParams) { event.preventDefault(); // todo make configured error state name $state.go('errors_missing_route', { unfoundState: unfoundState, fromState : { to: fromState ? fromState.name : '', fromParams: fromParams } } ); $rootScope.$routing = false; } ); } ); thisModule.provider('pipState', function($stateProvider, pipAssertProvider) { // Configuration of redirected states var redirectedStates = {}; this.redirect = setRedirect; this.state = $stateProvider.state; this.$get = function ($state, $timeout, pipAssert) { $state.redirect = redirect; return $state; //------------------------ function redirect(event, state, params, $rootScope) { pipAssert.contains(state, 'name', "$state.redirect: state should contains name prop"); pipAssert.isObject(params, "$state.redirect: params should be an object"); var toState; $rootScope.$routing = true; toState = redirectedStates[state.name]; if (_.isFunction(toState)) { toState = toState(state.name, params, $rootScope); if (_.isNull(toState)) { $rootScope.$routing = false; throw new Error('Redirected toState cannot be null'); } } if (!!toState) { $timeout(function() { event.preventDefault(); $state.transitionTo(toState, params, {location: 'replace'}); }); return true; } return false; } }; return; //------------------ // Specify automatic redirect from one state to another function setRedirect(fromState, toState) { pipAssertProvider.isNotNull(fromState, "pipState.redirect: fromState cannot be null"); pipAssertProvider.isNotNull(toState, "pipState.redirect: toState cannot be null"); redirectedStates[fromState] = toState; return this; }; }); })();