1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.Ng1ViewConfig = exports.ng1ViewsBuilder = exports.getNg1ViewConfigFactory = void 0;
|
4 |
|
5 | var core_1 = require("@uirouter/core");
|
6 |
|
7 | function 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 | }
|
14 | exports.getNg1ViewConfigFactory = getNg1ViewConfigFactory;
|
15 |
|
16 | var hasAnyKey = function (keys, obj) { return keys.reduce(function (acc, key) { return acc || core_1.isDefined(obj[key]); }, false); };
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | function ng1ViewsBuilder(state) {
|
29 |
|
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 |
|
34 |
|
35 |
|
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 |
|
45 | name = name || '$default';
|
46 |
|
47 | if (core_1.isString(config))
|
48 | config = { component: config };
|
49 |
|
50 | config = core_1.extend({}, config);
|
51 |
|
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 | }
|
66 | exports.ng1ViewsBuilder = ng1ViewsBuilder;
|
67 |
|
68 | var id = 0;
|
69 |
|
70 | var Ng1ViewConfig = (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);
|
97 | return _this;
|
98 | });
|
99 | };
|
100 | |
101 |
|
102 |
|
103 |
|
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 | }());
|
116 | exports.Ng1ViewConfig = Ng1ViewConfig;
|
117 |
|
\ | No newline at end of file |