This repository has been archived by the owner on Jan 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
routeToComponents.js
107 lines (89 loc) · 2.69 KB
/
routeToComponents.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
(function(){
'use strict';
angular
.module('ui.router.components', ['ui.router'])
.config(registerComponentDecorator);
var args2Array = function args2Array(_args) {
return Array.prototype.slice.call(_args);
};
function bindResolves() {
var injectNames = controller.$inject = ['$scope'].concat(args2Array(arguments));
function controller($scope) {
var injectValues = args2Array(arguments);
for (var i = 1; i < injectValues.length; i++) {
$scope[injectNames[i]] = injectValues[i];
}
}
return controller;
}
var getCompInputs = function getCompInputs($injector, componentName) {
return $injector.get(componentName + "Directive").map(function (directive) {
return Object.keys(directive.bindToController);
}).reduce(unnestR, []);
};
var getAttrs = function getAttrs($injector, componentName) {
return $injector.get(componentName + "Directive").map(function (directive) {
var inputs = [];
for (var key in directive.bindToController) {
if (directive.bindToController.hasOwnProperty(key)) {
var binding = directive.bindToController[key];
if (!binding || !binding.length) {
continue;
}
if (binding.length === 1) {
inputs.push({
key: key,
value: key
});
} else {
inputs.push({
key: binding.substring(1),
value: key
});
}
}
}
return inputs;
}).reduce(unnestR, []);
};
var unnestR = function unnestR(acc, array) {
return acc.concat(array);
};
function setStateDef(stateDef) {
stateDef.controllerProvider = function ($injector) {
return bindResolves.apply(null, getCompInputs($injector, stateDef.component));
};
stateDef.controllerProvider.$inject = ['$injector'];
stateDef.templateProvider = function ($injector) {
var attrs = getAttrs($injector, stateDef.component).map(function (object) {
var key = object.key;
var value = object.value;
return kebobString(key) + '="' + value + '"';
}).join(' ');
var kebobName = kebobString(stateDef.component);
return '<' + kebobName + ' ' + attrs + '></' + kebobName + '>';
};
stateDef.templateProvider.$inject = ['$injector'];
}
function kebobString(str) {
return str.replace(/([A-Z])/g, function ($1) {
return '-' + $1.toLowerCase();
});
}
function registerComponentDecorator($stateProvider) {
$stateProvider.decorator('component', function (stateDef, parent) {
if (stateDef.component) {
setStateDef(stateDef);
}
if (stateDef.views) {
for (var view in stateDef.views) {
if (stateDef.views[view].component) {
setStateDef(stateDef.views[view]);
}
}
}
return stateDef.component;
});
}
registerComponentDecorator.$inject = ['$stateProvider'];
}());