hooks.js
7.04 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
var fs = require("fs");
var path = require("path");
var pluginTmpl = templateFile("/plugin.tmpl");
var configTmpl = templateFile("/config.tmpl");
var configItem = templateFile("/config.item.tmpl");
var inlineTemp = templateFile("/inline.template.tmpl");
var pluginItemTmpl = fs.readFileSync(path.resolve(__dirname, "../", "templates/plugin.item.tmpl"), "utf-8");
function templateFile (filepath) {
return fs.readFileSync(path.join(__dirname, "/../templates", filepath || ""), "utf-8");
}
/**
* @type {{page: Function, markup: Function, client:js: Function, templates: Function}}
*/
module.exports = {
/**
* Create the url config for each section of the ui
* @param hooks
* @param ui
*/
"page": function (hooks, ui) {
var config = hooks
.map(transformConfig)
.reduce(createConfigItem, {});
return {
/**
* pagesConfig - This is the angular configuration such as routes
*/
pagesConfig: configTmpl
.replace("%when%", hooks.reduce(
createAngularRoutes,
""
))
.replace("%pages%", JSON.stringify(
config,
null,
4
)),
/**
* pagesConfig in object form
*/
pagesObj: config,
pageMarkup: function () {
return preAngular(ui.pluginManager.plugins, config, ui);
}
};
},
/**
* Controller markup for each plugin
* @param hooks
* @returns {*}
*/
"markup": function (hooks) {
return hooks.reduce(pluginTemplate, "");
},
/**
* @param hooks
* @param {UI} ui
* @returns {*|string}
*/
"client:js": function (hooks, ui) {
/**
* Add client JS from Browsersync Plugins
*/
ui.bsPlugins.forEach(function (plugin) {
if (plugin.has("client:js")) {
plugin.get("client:js").forEach(function (value) {
hooks.push(value);
});
}
});
var out = hooks.reduce(function (all, item) {
if (typeof item === "string") {
all += ";" + item;
} else if (Array.isArray(item)) {
item.forEach(function (item) {
all += ";" + item;
});
}
return all;
}, "");
return out;
},
/**
* @param hooks
* @param initial
* @param {UI} ui
* @returns {String}
*/
"templates": function (hooks, initial, ui) {
/**
* Add templates from each Browsersync registered plugin
* @type {string}
*/
var pluginDirectives = ui.bsPlugins.reduce(function (all, plugin) {
if (!plugin.has("templates")) {
return all;
}
/**
* Slugify-ish the plugin name
* eg: Test Browsersync Plugin
* = test-browsersync-plugin
* @type {string}
*/
var slug = plugin.get("name")
.trim()
.split(" ")
.map(function (word) {
return word.trim().toLowerCase();
})
.join("-");
/**
* For every plugin that has templates, wrap
* the markup in the <script type="text/ng-template" id="{{slug}}"></script>
* markup to result in the single output string.
*/
plugin.get("templates").forEach(function (value, key) {
all += angularWrap([slug, path.basename(key)].join("/"), value);
});
return all;
}, "");
/**
* Combine the markup from the plugins done above with any
* others registered via hooks + initial
* to create the final markup
*/
return [pluginDirectives, createInlineTemplates(hooks.concat([initial]))].join("");
},
/**
* Allow plugins to register toggle-able elements
* @param hooks
* @returns {{}}
*/
"elements": function (hooks) {
var obj = {};
hooks.forEach(function (elements) {
elements.forEach(function (item) {
if (!obj[item.name]) {
obj[item.name] = item;
}
});
});
return obj;
}
};
/**
* @param hooks
* @returns {String}
*/
function createInlineTemplates (hooks) {
return hooks.reduce(function (combined, item) {
return combined + item.reduce(function (all, filepath) {
return all + angularWrap(
path.basename(filepath),
fs.readFileSync(filepath));
}, "");
}, "");
}
/**
* @param item
* @returns {*}
*/
function transformConfig (item) {
return item;
}
/**
* @param {String} all
* @param {Object} item
* @returns {*}
*/
function createAngularRoutes(all, item) {
return all + configItem.replace(/%(.+)%/g, function () {
var key = arguments[1];
if (item[key]) {
return item[key];
}
});
}
/**
* @param joined
* @param item
* @returns {*}
*/
function createConfigItem (joined, item) {
if (item.path === "/") {
joined["overview"] = item;
} else {
joined[item.path.slice(1)] = item;
}
return joined;
}
/**
* @returns {*}
*/
function pluginTemplate (combined, item) {
return [combined, pluginTmpl.replace("%markup%", item)].join("\n");
}
/**
* @param plugins
* @param config
* @returns {*}
*/
function preAngular (plugins, config, ui) {
return Object.keys(plugins)
.filter(function (key) {
return config[key]; // only work on plugins that have pages
})
.map(function (key) {
if (key === "plugins") {
var pluginMarkup = ui.bsPlugins.reduce(function (all, item, i) {
all += pluginItemTmpl
.replace("%content%", item.get("markup") || "")
.replace(/%index%/g, i)
.replace(/%name%/g, item.get("name"));
return all;
}, "");
plugins[key].hooks.markup = plugins[key].hooks.markup.replace("%pluginlist%", pluginMarkup);
}
return angularWrap(config[key].template, bindOnce(plugins[key].hooks.markup, config[key]));
})
.reduce(function (combined, item) {
return combined + item;
}, "");
}
/**
* @param templateName
* @param markup
* @returns {*}
*/
function angularWrap (templateName, markup) {
return inlineTemp
.replace("%content%", markup)
.replace("%id%", templateName);
}
/**
* @param markup
* @param config
* @returns {*|string}
*/
function bindOnce (markup, config) {
return markup.toString().replace(/\{\{ctrl.section\.(.+?)\}\}/g, function ($1, $2) {
return config[$2] || "";
});
}
module.exports.bindOnce = bindOnce;