/
Alloy.Require.js
executable file
·119 lines (105 loc) · 3.59 KB
/
Alloy.Require.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
var path = require('path'),
fs = require('fs'),
_ = require('../../../lib/alloy/underscore')._,
CU = require('../compilerUtils'),
U = require('../../../utils'),
styler = require('../styler'),
CONST = require('../../../common/constants'),
moduleRoot = path.join(__dirname,'..','..','..','..'),
TYPES = ['view','widget'];
exports.parse = function(node, state) {
return require('./base').parse(node, state, parse);
};
function parse(node, state, args) {
var code = '',
type = node.getAttribute('type') || CONST.REQUIRE_TYPE_DEFAULT,
src = node.getAttribute('src'),
name = node.getAttribute('name') || CONST.NAME_WIDGET_DEFAULT,
method;
// validate src
if (!src) {
U.die('<Require> elements must have a "src" attribute.');
} else if (U.XML.getElementsFromNodes(node.childNodes).length !== 0) {
U.die('<Require> elements may not have child elements.');
}
// determine which Alloy method to use
var extraArgs = '';
var config = CU.getCompilerConfig();
var appPath = config.dir.home;
var paths = [];
var platform;
if (config && config.alloyConfig && config.alloyConfig.platform) {
platform = config.alloyConfig.platform;
}
switch(type) {
case 'view':
method = 'createController';
platform && paths.push(path.join(appPath,CONST.DIR.VIEW,platform,src));
paths.push(path.join(appPath,CONST.DIR.VIEW,src));
break;
case 'widget':
method = 'createWidget';
extraArgs = "'" + name + "',";
platform && paths.push(path.join(appPath,CONST.DIR.WIDGET,src,CONST.DIR.VIEW,platform,name));
paths.push(path.join(appPath,CONST.DIR.WIDGET,src,CONST.DIR.VIEW,name));
platform && paths.push(path.join(moduleRoot,'widgets',src,CONST.DIR.VIEW,platform,name));
paths.push(path.join(moduleRoot,'widgets',src,CONST.DIR.VIEW,name));
break;
default:
U.die('Invalid <Require> type "' + type + '"');
}
// check the extensions on the paths to check
var regex = new RegExp('\\.' + CONST.FILE_EXT.VIEW + '$');
var found = false;
for (var i = 0; i < paths.length; i++) {
var fullpath = paths[i];
fullpath += regex.test(fullpath) ? '' : '.' + CONST.FILE_EXT.VIEW;
if (fs.existsSync(fullpath)) {
found = true;
break;
}
}
// abort if there's no view to be found
if (!found) {
U.die([
type + ' "' + src + '" ' + (type === 'widget' ? 'view "' + name + '" ' : '') +
'does not exist.',
'The following paths were inspected:'
].concat(paths));
}
// Remove <Require>-specific attributes from createArgs
delete args.createArgs.type;
delete args.createArgs.src;
delete args.createArgs.name;
// make symbol a local variable if necessary, used for binding
if (state.local) {
args.symbol = CU.generateUniqueId();
}
// add extra createArgs if present
var xArgs = {};
state.model && (xArgs[CONST.BIND_MODEL_VAR] = '__ALLOY_EXPR__--'+state.model);
state.parent && state.parent.symbol &&
(xArgs[CONST.PARENT_SYMBOL_VAR] = '__ALLOY_EXPR__--'+state.parent.symbol);
state.templateObject && (xArgs[CONST.ITEM_TEMPLATE_VAR] = '__ALLOY_EXPR__--'+state.templateObject);
args.createArgs = _.extend(args.createArgs || {}, xArgs);
// Generate runtime code
code += (state.local ? 'var ' : '') + args.symbol + " = Alloy." + method + "('" + src + "'," + extraArgs + styler.generateStyleParams(
state.styles,
args.classes,
args.id,
type === 'widget' ? 'Alloy.Widget' : 'Alloy.Require',
args.createArgs,
state
) + ");\n";
if (args.parent.symbol && !state.templateObject) {
code += args.symbol + '.setParent(' + args.parent.symbol + ');\n';
}
return {
parent: {
node: node,
symbol: args.symbol + '.getViewEx({recurse:true})'
},
styles: state.styles,
code: code
}
};