UNPKG

5.83 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.Ng1ViewConfig = exports.ng1ViewsBuilder = exports.getNg1ViewConfigFactory = void 0;
4/** @publicapi @module ng1 */ /** */
5var core_1 = require("@uirouter/core");
6/** @internalapi */
7function getNg1ViewConfigFactory() {
8 var templateFactory = null;
9 return function (path, view) {
10 templateFactory = templateFactory || core_1.services.$injector.get('$templateFactory');
11 return [new Ng1ViewConfig(path, view, templateFactory)];
12 };
13}
14exports.getNg1ViewConfigFactory = getNg1ViewConfigFactory;
15/** @internalapi */
16var hasAnyKey = function (keys, obj) { return keys.reduce(function (acc, key) { return acc || core_1.isDefined(obj[key]); }, false); };
17/**
18 * This is a [[StateBuilder.builder]] function for angular1 `views`.
19 *
20 * When the [[StateBuilder]] builds a [[StateObject]] object from a raw [[StateDeclaration]], this builder
21 * handles the `views` property with logic specific to @uirouter/angularjs (ng1).
22 *
23 * If no `views: {}` property exists on the [[StateDeclaration]], then it creates the `views` object
24 * and applies the state-level configuration to a view named `$default`.
25 *
26 * @internalapi
27 */
28function ng1ViewsBuilder(state) {
29 // Do not process root state
30 if (!state.parent)
31 return {};
32 var tplKeys = ['templateProvider', 'templateUrl', 'template', 'notify', 'async'], ctrlKeys = ['controller', 'controllerProvider', 'controllerAs', 'resolveAs'], compKeys = ['component', 'bindings', 'componentProvider'], nonCompKeys = tplKeys.concat(ctrlKeys), allViewKeys = compKeys.concat(nonCompKeys);
33 // Do not allow a state to have both state-level props and also a `views: {}` property.
34 // A state without a `views: {}` property can declare properties for the `$default` view as properties of the state.
35 // However, the `$default` approach should not be mixed with a separate `views: ` block.
36 if (core_1.isDefined(state.views) && hasAnyKey(allViewKeys, state)) {
37 throw new Error("State '" + state.name + "' has a 'views' object. " +
38 "It cannot also have \"view properties\" at the state level. " +
39 "Move the following properties into a view (in the 'views' object): " +
40 (" " + allViewKeys.filter(function (key) { return core_1.isDefined(state[key]); }).join(', ')));
41 }
42 var views = {}, viewsObject = state.views || { $default: core_1.pick(state, allViewKeys) };
43 core_1.forEach(viewsObject, function (config, name) {
44 // Account for views: { "": { template... } }
45 name = name || '$default';
46 // Account for views: { header: "headerComponent" }
47 if (core_1.isString(config))
48 config = { component: config };
49 // Make a shallow copy of the config object
50 config = core_1.extend({}, config);
51 // Do not allow a view to mix props for component-style view with props for template/controller-style view
52 if (hasAnyKey(compKeys, config) && hasAnyKey(nonCompKeys, config)) {
53 throw new Error("Cannot combine: " + compKeys.join('|') + " with: " + nonCompKeys.join('|') + " in stateview: '" + name + "@" + state.name + "'");
54 }
55 config.resolveAs = config.resolveAs || '$resolve';
56 config.$type = 'ng1';
57 config.$context = state;
58 config.$name = name;
59 var normalized = core_1.ViewService.normalizeUIViewTarget(config.$context, config.$name);
60 config.$uiViewName = normalized.uiViewName;
61 config.$uiViewContextAnchor = normalized.uiViewContextAnchor;
62 views[name] = config;
63 });
64 return views;
65}
66exports.ng1ViewsBuilder = ng1ViewsBuilder;
67/** @hidden */
68var id = 0;
69/** @internalapi */
70var Ng1ViewConfig = /** @class */ (function () {
71 function Ng1ViewConfig(path, viewDecl, factory) {
72 var _this = this;
73 this.path = path;
74 this.viewDecl = viewDecl;
75 this.factory = factory;
76 this.$id = id++;
77 this.loaded = false;
78 this.getTemplate = function (uiView, context) {
79 return _this.component
80 ? _this.factory.makeComponentTemplate(uiView, context, _this.component, _this.viewDecl.bindings)
81 : _this.template;
82 };
83 }
84 Ng1ViewConfig.prototype.load = function () {
85 var _this = this;
86 var $q = core_1.services.$q;
87 var context = new core_1.ResolveContext(this.path);
88 var params = this.path.reduce(function (acc, node) { return core_1.extend(acc, node.paramValues); }, {});
89 var promises = {
90 template: $q.when(this.factory.fromConfig(this.viewDecl, params, context)),
91 controller: $q.when(this.getController(context)),
92 };
93 return $q.all(promises).then(function (results) {
94 core_1.trace.traceViewServiceEvent('Loaded', _this);
95 _this.controller = results.controller;
96 core_1.extend(_this, results.template); // Either { template: "tpl" } or { component: "cmpName" }
97 return _this;
98 });
99 };
100 /**
101 * Gets the controller for a view configuration.
102 *
103 * @returns {Function|Promise.<Function>} Returns a controller, or a promise that resolves to a controller.
104 */
105 Ng1ViewConfig.prototype.getController = function (context) {
106 var provider = this.viewDecl.controllerProvider;
107 if (!core_1.isInjectable(provider))
108 return this.viewDecl.controller;
109 var deps = core_1.services.$injector.annotate(provider);
110 var providerFn = core_1.isArray(provider) ? core_1.tail(provider) : provider;
111 var resolvable = new core_1.Resolvable('', providerFn, deps);
112 return resolvable.get(context);
113 };
114 return Ng1ViewConfig;
115}());
116exports.Ng1ViewConfig = Ng1ViewConfig;
117//# sourceMappingURL=views.js.map
\No newline at end of file