-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
executable file
·134 lines (103 loc) · 3.81 KB
/
index.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
var createProperty = require('./property'),
createBinding = require('./binding'),
BaseComponent = require('./baseComponent'),
Enti = require('enti'),
objectAssign = require('object-assign'),
is = require('./is');
function inflateProperties(component, settings){
for(var key in settings){
var setting = settings[key],
property = component[key];
if(is.property(settings[key])){
if(is.property(property)){
property.destroy();
}
setting.addTo(component, key);
}else if(is.property(property)){
if(is.binding(setting)){
property.binding(setting);
}else{
property(setting);
}
property.addTo(component, key);
}
}
}
function validateExpectedComponents(components, componentName, expectedComponents){
expectedComponents = expectedComponents.filter(function(componentName){
return !(componentName in components);
});
if(expectedComponents.length){
console.warn([
'fastn("' + componentName + '") uses some components that have not been registered with fastn',
'Expected component constructors: ' + expectedComponents.join(', ')
].join('\n\n'));
}
}
module.exports = function(components, debug){
if(!components || typeof components !== 'object'){
throw new Error('fastn must be initialised with a components object');
}
components._container = components._container || require('./containerComponent');
function fastn(type){
var args = Array.prototype.slice.call(arguments);
var settings = args[1],
childrenIndex = 2,
settingsChild = fastn.toComponent(args[1]);
if(Array.isArray(args[1]) || settingsChild || !args[1]){
if(args.length > 1){
args[1] = settingsChild || args[1];
}
childrenIndex--;
settings = null;
}
settings = objectAssign({}, settings || {});
var types = typeof type === 'string' ? type.split(':') : Array.isArray(type) ? type : [type],
baseType,
children = args.slice(childrenIndex),
component = fastn.base(type, settings, children);
while(component && (baseType = types.shift())){
component = component.extend(baseType, settings, children);
}
if(!component){
// Type was not a component.
return;
}
component._properties = {};
inflateProperties(component, settings);
return component;
}
fastn.toComponent = function(component){
if(component == null || Array.isArray(component)){
return;
}
if(is.component(component)){
return component;
}
if(typeof component !== 'object' || component instanceof Date){
return fastn('text', { text: component }, component);
}
return fastn(component)
};
fastn.debug = debug;
fastn.property = createProperty.bind(fastn);
fastn.binding = createBinding(fastn);
fastn.isComponent = is.component;
fastn.isBinding = is.binding;
fastn.isDefaultBinding = is.defaultBinding;
fastn.isBindingObject = is.bindingObject;
fastn.isProperty = is.property;
fastn.components = components;
fastn.Model = Enti;
fastn.isModel = Enti.isEnti.bind(Enti);
fastn.base = function(type, settings, children){
return new BaseComponent(fastn, type, settings, children);
};
for(var key in components){
var componentConstructor = components[key];
if(componentConstructor.expectedComponents){
validateExpectedComponents(components, key, componentConstructor.expectedComponents);
}
}
return fastn;
};