-
Notifications
You must be signed in to change notification settings - Fork 0
/
baseController.js
173 lines (127 loc) · 4.62 KB
/
baseController.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
var ViewsDirectory = './Views';
var ViewLoader = require('./Views/viewLoader');
ViewLoader = new ViewLoader([ViewsDirectory, __dirname + '/InternalViews']);
var ViewParser = require('./Views/viewParser');
var Dependency = require('./dependency');
var authHandler = require('./Auth/authHandler');
var HtmlHelper = require('./Utilities/html');
module.exports = (function() {
var getHtmlView = function(viewName) {
var view = ViewLoader.loadView(viewName);
if (!view) {
return null;
}
return ViewParser.parse(viewName, view);
};
var applyView = function(view, data, htmlHelper) {
var appliedView = view.view(data, htmlHelper);
if (view.properties.overView) {
var toApply = {
view: appliedView
};
Utils.extend(toApply, data);
for (var prop in view.properties) {
if (prop != 'overView') {
toApply[prop] = view.properties[prop];
}
}
var overView = getHtmlView((view.properties.overView || '').toLowerCase());
return applyView(overView, toApply, htmlHelper);
}
return appliedView;
};
var loadView = function(controller, viewName) {
viewName = viewName.toLowerCase();
var view = getHtmlView(controller._name + '/' + viewName);
if (!view) {
view = getHtmlView(viewName);
}
if (!view) {
view = getHtmlView('shared/' + viewName);
}
return view;
};
var adjustModelContext = function(model) {
return {
model: model || {}
};
};
var getParameters = function(controller, action, params) {
if (typeof controller === 'object') {
controller.controller = controller.controller || this._name;
controller.action = (controller.action || 'index').toLowerCase();
return controller;
}
params = params || action;
if (typeof params !== 'object') {
params = {};
}
if (typeof action !== 'string') {
action = controller;
controller = this._name;
}
params.controller = controller;
params.action = (action || 'index').toLowerCase();
return params;
};
return function BaseController(findRoute) {
this.AsyncFinished = function(data) {
this._promiseCallback(data);
};
this.View = function(view, model) {
var c = adjustModelContext(model);
var view = loadView(this, view);
if (!view) {
return;
}
return applyView(view, c, new HtmlHelper(this));
};
this.Partial = function(view, model) {
var c = adjustModelContext(model);
var view = loadView(this, view);
if (!view) {
return;
}
return view.view(c, new HtmlHelper(this));
};
this.Action = function(controller, action, params) {
var actualParameters = getParameters.call(this, controller, action, params);
var controller = this.classLoader.getController(actualParameters.controller);
var action = actualParameters.action;
if (controller && controller[action]) {
var deferred = new Deferred();
controller._promiseCallback = function(result) {
if (result instanceof Deferred) {
result.onComplete(deferred.complete);
return;
}
deferred.complete(result);
};
var value = controller[action](actualParameters);
if (value && value instanceof Deferred) {
return value;
} else if (value) {
deferred.complete(value);
}
return deferred;
}
return '';
};
this.ActionLink = function(controller, action, params) {
var actualParameters = getParameters.call(this, controller, action, params);
return findRoute(actualParameters);
};
this.Json = function(data) {
return JSON.stringify(data);
};
this.RedirectToAction = function(controller, action, params) {
var link = this.ActionLink(controller, action, params);
return {
_redirect: link
}
};
this.Authenticate = function() {
authHandler.authenticate(this);
};
};
})();