This repository has been archived by the owner on Oct 30, 2018. It is now read-only.
/
deploy.server.js
187 lines (148 loc) · 6.45 KB
/
deploy.server.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
/*
* Copyright (c) 2011-2012, Yahoo! Inc. All rights reserved.
* Copyrights licensed under the New BSD License.
* See the accompanying LICENSE file for terms.
*/
/*jslint anon:true, sloppy:true, nomen:true, node: true*/
/*global YUI*/
/**
* @module ActionContextAddon
*/
YUI.add('mojito-deploy-addon', function(Y, NAME) {
'use strict';
/**
* <strong>Access point:</strong> <em>ac.deploy.*</em>
* Provides ability to create client runtime deployment HTML
* @class Deploy.server
*/
function Addon(command, adapter, ac) {
this.instance = command.instance;
this.scripts = {};
this.ac = ac;
this.rs = null;
}
Addon.prototype = {
namespace: 'deploy',
/**
* Declaration of store requirement.
* @method setStore
* @private
* @param {ResourceStore} rs The resource store instance.
*/
setStore: function(rs) {
this.rs = rs;
},
/**
* Builds up the browser Mojito runtime.
* @method constructMojitoClientRuntime
* @param {AssetHandler} assetHandler asset handler used to add scripts
* to the DOM under construction.
* @param {object} binderMap information about the binders that will be
* deployed to the client.
*/
constructMojitoClientRuntime: function(assetHandler, binderMap) {
//Y.log('Constructing Mojito client runtime', 'debug', NAME);
var store = this.rs,
contextServer = this.ac.context,
appConfigServer = store.getAppConfig(contextServer),
appGroupConfig = store.yui.getAppGroupConfig(),
seedFiles = store.yui.getAppSeedFiles(contextServer),
contextClient,
appConfigClient,
yuiConfig = {},
yuiConfigEscaped,
yuiConfigStr,
viewId,
i,
clientConfig = {},
clientConfigEscaped,
clientConfigStr,
initialModuleList = {},
initializer, // script for YUI initialization
pathToRoot;
contextClient = Y.mojito.util.copy(contextServer);
contextClient.runtime = 'client';
appConfigClient = store.getAppConfig(contextClient);
clientConfig.context = contextClient;
yuiConfig = Y.merge({
fetchCSS: true,
combine: true,
// the base path for non-combo paths
base: 'http://yui.yahooapis.com/' + Y.version + '/build/',
// the path to the combo service
comboBase: 'http://yui.yahooapis.com/combo?',
// a fragment to prepend to the path attribute when
// when building combo urls
root: Y.version + '/build/',
groups: {
app: appGroupConfig
}
}, ((appConfigClient.yui && appConfigClient.yui.config) || {}), {
lang: contextServer.lang // same as contextClient.lang
});
// yui.config goes through a different channel (yuiConfig),
// so we should remove it from the appConfigClient.
if (appConfigClient.yui.config) {
delete appConfigClient.yui.config;
}
// Set the YUI seed file to use on the client. This has to be done
// before any other scripts are added and can be controlled through
// application.json->yui->config->seed in a form of
// a array with the list of modules or fullpath urls.
if (appGroupConfig.combine === false) {
// if the combo is disabled, then we need to insert one by one
// this is useful for offline and hybrid apps where the combo
// does not work.
for (i = 0; i < seedFiles.length; i += 1) {
assetHandler.addAsset('js', 'top', appGroupConfig.base +
appGroupConfig.root + seedFiles[i]);
}
} else {
// using combo for the seed files
assetHandler.addAsset('js', 'top', appGroupConfig.comboBase +
appGroupConfig.root + seedFiles.join(appGroupConfig.comboSep +
appGroupConfig.root));
}
// adding the default module for the Y.use statement in the client
initialModuleList['mojito-client'] = true;
// add binders' dependencies
for (viewId in binderMap) {
if (binderMap.hasOwnProperty(viewId)) {
if (binderMap[viewId].name) {
initialModuleList[binderMap[viewId].name] = true;
}
}
}
clientConfig.binderMap = binderMap;
// we need the app config on the client for log levels (at least)
clientConfig.appConfig = appConfigClient;
// this is mainly used by html5app
pathToRoot = this.ac.http.getHeader('x-mojito-build-path-to-root');
if (pathToRoot) {
clientConfig.pathToRoot = pathToRoot;
}
clientConfig.routes = this.ac.url.getRouteMaker().getComputedRoutes();
// Unicode escape the various strings in the config data to help
// fight against possible script injection attacks.
yuiConfigEscaped = Y.mojito.util.cleanse(yuiConfig);
yuiConfigStr = JSON.stringify(yuiConfigEscaped);
clientConfigEscaped = Y.mojito.util.cleanse(clientConfig);
clientConfigStr = JSON.stringify(clientConfigEscaped);
initialModuleList = "'" + Y.Object.keys(initialModuleList).join("','") + "'";
initializer = '<script type="text/javascript">\n' +
' YUI.applyConfig(' + yuiConfigStr + ');\n' +
' YUI().use(' + initialModuleList + ', function(Y) {\n' +
' window.YMojito = { client: new Y.mojito.Client(' +
clientConfigStr + ') };\n' +
' });\n' +
'</script>\n';
// Add the boot script
assetHandler.addAsset('blob', 'bottom', initializer);
}
};
Y.namespace('mojito.addons.ac').deploy = Addon;
}, '0.1.0', {requires: [
'mojito-util',
'mojito-http-addon',
'mojito-url-addon'
]});