Skip to content

Commit

Permalink
Bye bye viewsetup.js! 👋
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew McNamara committed Feb 18, 2015
1 parent fedb4e1 commit d1920e9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 23 deletions.
32 changes: 12 additions & 20 deletions lib/bundle_views/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var filteredConfig = configHelper.getFilteredConfig();
var cheerio = require('cheerio');
var fs = require('fs');
var path = require('path');
var util = require('util');
var log = require('../logger')('nodecg/lib/bundle_views');
var Bundles = require('../bundles');

Expand All @@ -16,24 +17,6 @@ log.trace('Adding Express routes');

app.set('views', path.resolve(__dirname, '../..'));

app.get('/viewsetup.js', function(req, res) {
var bundleName = req.query.bundle || '';

// Don't send the script if the query string is missing
if (bundleName === '') {
res.send(404);
return;
}

var bundle = Bundles.find(bundleName);
res.type('.js');
res.render('lib/client_includes/viewsetup.js.ejs', {
bundleName: bundle.name,
ncgConfig: JSON.stringify(filteredConfig),
bundleConfig: JSON.stringify(bundle.config)
});
});

app.get('/view/:bundleName*', function(req, res, next) {
var bundleName = req.params.bundleName;

Expand Down Expand Up @@ -71,8 +54,17 @@ app.get('/view/:bundleName*', function(req, res, next) {
if (resName.endsWith('.html')) {
var file = fs.readFileSync(fileLocation);
var $ = cheerio.load(file);
var scriptNode = '<script src="/viewsetup.js?bundle='+ bundle.name +'"></script>';
$('body').append(scriptNode);

var scripts = '<script src="/socket.io/socket.io.js"></script>' +
'<script src="/nodecg-api.js"></script>' +
'<script>var socket = io("//%s:%s/");' +
'window.nodecg = new NodeCG("%s", %s, %s, socket);</script>';

scripts = util.format(scripts, filteredConfig.host, filteredConfig.port,
bundle.name, JSON.stringify(filteredConfig), JSON.stringify(bundle.config));

var currentHead = $('head').html();
$('head').html(scripts + currentHead);

res.send($.html());
} else {
Expand Down
4 changes: 1 addition & 3 deletions test/setup/test-bundle/view/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
</head>
<body>
<script>
document.addEventListener("ncgReady", function() {
window.viewApi = nodecg;
})
window.viewApi = nodecg;
</script>
</body>
</html>

0 comments on commit d1920e9

Please sign in to comment.