-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathui.js
227 lines (209 loc) · 6.42 KB
/
ui.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// var sys = require('sys');
var fs = require('fs');
var _ = require('lodash');
var cwd = process.cwd();
var config = require('./config');
// default ui config
var uiConfig = config.ui;
function inline(module, page) {
var embed = {};
var cssSource, jsSource;
var path = 'views/ui/' + module + '/';
try {
cssSource = fs.readFileSync(path + page + '.css', 'utf-8');
embed['__style'] = cssSource;
} catch(e) {
embed['__style'] = '';
}
try {
jsSource = fs.readFileSync(path + page + '.js', 'utf-8');
embed['__script'] = jsSource;
} catch(e) {
embed['__script'] = '';
}
return embed;
}
function getKey(path) {
var __key;
if (path && _.isString(path)) {
__key = path.replace(/^.*[\\\/]templates[\\\/](.+)[\\\/]screen[\\\/](.+)\.vm$/, '$1,$2');
}
return __key;
}
function putOn(resource, type) {
var str = '';
if ('css' === type) {
resource.forEach(function(item) {
str += '<link rel="stylesheet" href="/assets/' + item + '" />\n';
});
} else if ('js' === type) {
resource.forEach(function(item) {
str += '<script src="/assets/' + item + '"></script>\n';
});
}
return str;
}
function readJson(module) {
var source;
var path = module === undefined ?
'views/ui/config' : 'views/ui/' + module;
try {
source = fs.readFileSync(path + '/config.json','utf-8');
return JSON.parse( source );
} catch(e) {
return null;
// throw new Error(e);
}
}
function getModule(key) {
return key ? key.replace(/[\\]/g, '/').split(',') : [];
}
function parseConfig(key) {
var tempobj;
var css, js;
var i, j, k, tgc = {}, tc = {};
var mp = getModule(key);
var module, page, config, globalConfig;
module = mp[0];
page = mp[1];
globalConfig = readJson();
config = readJson(module);
if (globalConfig) {
for (i in globalConfig) {
if ('vars' === i) {
for (j in globalConfig[i]) {
tgc[j] = globalConfig[i][j];
}
continue;
}
tgc[i] = globalConfig[i];
}
}
if (config) {
for (i in config) {
if ('default' === i) {
// css, js, head, foot and other vars.
for (j in config[i]) {
if ('vars' === j) {
for (k in config[i][j]) {
tc[k] = config[i][j][k];
}
continue;
}
tc[j] = config[i][j];
}
}
if ('page' === i) {
if (config[i][page]) {
// this page config
for (k in config[i][page]) {
if ('css' === k || 'js' === k) {
// combo css & js
if (tc[k]) {
tc[k] = tc[k].concat( config[i][page][k] );
//delete tc[k];
continue;
}
}
if ('vars' === k) {
for (j in config[i][page][k]) {
tc[j] = config[i][page][k][j];
}
continue;
}
// head, foot and other vars.
tc[k] = config[i][page][k];
}
}
}
}
}
// combo page inline css and js
// __style, __script
_.merge( tc, inline(module, page) );
if (tc['css']) {
tgc['css'] = tgc['css'].concat( tc['css'] );
delete tc['css'];
}
if (tc['js']) {
tgc['js'] = tgc['js'].concat( tc['js'] );
delete tc['js'];
}
tempobj = _.extend(tgc, tc);
tempobj['js'] = tempobj['js'] ? putOn(tempobj['js'], 'js') : '';
tempobj['css'] = tempobj['css'] ? putOn(tempobj['css'], 'css') : '';
if (tempobj['__style'].trim() !== '') {
tempobj['__style'] = '<style>\n' + tempobj['__style'] + '\n</style>';
}
if (tempobj['__script'].trim() !== '') {
tempobj['__script'] = '<script>\n' + tempobj['__script'] + '\n</script>';
}
if (!tempobj['layout']) {
tempobj['layout'] = 'default';
}
if (!tempobj['head']) {
tempobj['head'] = 'theme/default';
}
if (!tempobj['foot']) {
tempobj['foot'] = 'theme/default';
}
return tempobj;
}
var UIObject = {
config: function(path) {
var key = getKey(path);
if (key === undefined) return uiConfig;
var __obj = parseConfig(key);
uiConfig = _.extend(uiConfig, __obj);
uiConfig = _.merge(uiConfig, {
__head: uiConfig.head,
__screen: '',
__foot: uiConfig.foot,
module: getModule(key)[0],
body: getModule(key)[1],
layout: uiConfig.layout,
keywords: config.keywords,
description: config.description
});
return uiConfig;
},
util : {
replaceWith: function(tpl, sub) {
var i = 0;
var symbol = '*';
var str = tpl;
if (_.isString(sub)) {
return str.replace(symbol, sub);
}
if (_.isArray(sub)) {
while (i < sub.length) {
var p = str.indexOf(symbol);
var l = symbol.length;
var h = str.substring(0, p);
var f = str.substring(p+l);
str = h + sub[i] + f;
i++;
}
return str;
}
return '';
},
getHead: function(str, ext) {
var tpl = 'views/ui/*/head.' + ext;
return this.replaceWith(tpl, str);
},
getScreen: function(str, ext) {
var tpl = 'views/templates/*/screen/*.' + ext;
return this.replaceWith(tpl, str);
},
getFoot: function(str, ext) {
var tpl = 'views/ui/*/foot.' + ext;
return this.replaceWith(tpl, str);
},
getLayout: function(str, ext) {
var tpl = '*/views/templates/*/layout/*.' + ext;
return this.replaceWith(tpl, str);
}
}
};
module.exports = UIObject;