|
| 1 | +// Copyright 2015 Google Inc. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import ChromeController from './chrome_controller'; |
| 16 | +import {stateName, namespaceParam} from './chrome_state'; |
| 17 | + |
| 18 | +/** |
| 19 | + * Namespace is an abstract state with no path, but with one parameter ?namespace= that |
| 20 | + * is always accepted (since namespace is above all). |
| 21 | + * |
| 22 | + * This state must always be the root in a state tree. This is enforced during app startup. |
| 23 | + * |
| 24 | + * @param {!ui.router.$stateProvider|kdUiRouter.$stateProvider} $stateProvider |
| 25 | + * @ngInject |
| 26 | + */ |
| 27 | +export default function stateConfig($stateProvider) { |
| 28 | + $stateProvider.state(stateName, { |
| 29 | + url: `?${namespaceParam}`, |
| 30 | + abstract: true, |
| 31 | + templateUrl: 'chrome/chrome.html', |
| 32 | + controller: ChromeController, |
| 33 | + controllerAs: 'ctrl', |
| 34 | + }); |
| 35 | + $stateProvider.decorator('parent', requireParentState); |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * @param {!Object} stateExtend |
| 40 | + * @param {function(?):!ui.router.$state} parentFn |
| 41 | + * @return {!ui.router.$state} |
| 42 | + */ |
| 43 | +function requireParentState(stateExtend, parentFn) { |
| 44 | + /** @type {!ui.router.$state} */ |
| 45 | + let state = stateExtend['self']; |
| 46 | + if (!state.parent && state.name !== stateName) { |
| 47 | + throw new Error( |
| 48 | + `State "${state.name}" requires parent state to be set to ` + |
| 49 | + `${stateName}. This is likely a programming error.`); |
| 50 | + } |
| 51 | + return parentFn(stateExtend); |
| 52 | +} |
0 commit comments