Skip to content

Commit

Permalink
Major: Help button on instances show help markup from modules
Browse files Browse the repository at this point in the history
  • Loading branch information
haakonnessjoen committed Jan 11, 2019
1 parent 4a74c97 commit 86680c5
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 10 deletions.
39 changes: 39 additions & 0 deletions lib/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var system;
var debug = require('debug')('lib/instance');
var shortid = require('shortid');
var fs = require('fs');
var marked = require('marked');

function instance(system) {
var self = this;
Expand Down Expand Up @@ -114,6 +115,21 @@ function instance(system) {

});

self.system.on('http_req', function (req, res, done) {
var match;

if (match = req.url.match(/^\/help\/([^/]+)\/(.+?)(\?.+)?$/)) {
var path = require('app-root-path') + '/lib/module';
var module = match[1].replace(/\.\.+/g,'');
var file = match[2].replace(/\.\.+/g,'');

if (file.match(/\.(jpe?g|gif|png|pdf)$/) && fs.existsSync(path +'/'+ module + '/' + file)) {
done();
res.sendFile(path +'/'+ module + '/' + file);
}
}
});

system.on('instance_init', function() {

debug('instance_init', self.store.db);
Expand All @@ -131,6 +147,11 @@ function instance(system) {
var packagefile = fs.readFileSync(path +'/'+ folder + '/package.json');
var moduleconfig = JSON.parse(packagefile);
var mod = require(path + '/' + folder + '/' + moduleconfig.main);
if (fs.existsSync(path +'/'+ folder + '/HELP.md')) {
moduleconfig.help = true;
} else {
moduleconfig.help = false;
}

self.store.module.push(moduleconfig);
self.modules[folder] = mod;
Expand Down Expand Up @@ -505,6 +526,24 @@ instance.prototype.connect = function (client) {
self.system.emit('instance_add', module);
});

client.on('instance_get_help', function (module) {
if (self.modules[module] !== undefined) {
var path = require('app-root-path') + '/lib/module';

if (fs.existsSync(path +'/'+ module + '/HELP.md')) {
try {
var help = fs.readFileSync(path +'/'+ module + '/HELP.md');
help = marked(help.toString(), { baseUrl: '/int/help/' + module + '/' });
client.emit('instance_get_help:result', null, help);
} catch (e) {
debug('Error loading help for ' + module, path +'/'+ module + '/HELP.md');
debug(e);
client.emit('instance_get_help:result', 'nofile');
}
}
}
});

};

exports = module.exports = function (system) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"jquery": "3.3.1",
"json-parser": "^1.1.5",
"lodash": "^4.17.10",
"marked": "^0.6.0",
"mkdirp": "^0.5.1",
"moment": "^2.22.2",
"node-rest-client": "^3.1.0",
Expand Down
14 changes: 14 additions & 0 deletions public/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,20 @@ label {
font-size: 12px;
}

.instance_help {
float: right;
cursor: pointer;
cursor: hand;
}

.help-dialog {
max-width: 70%;
}

.help-body img {
max-width: 95%;
}

.btn-file {
position: relative;
overflow: hidden;
Expand Down
20 changes: 20 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,26 @@ <h5 class="modal-title" id="deviceModalLabel">Device settings</h5>
</div>
</div>

<!-- Help modal -->
<div class="modal fade" id="helpModal" tabindex="-1" role="dialog" aria-labelledby="helpModalLabel" aria-hidden="true">
<div class="modal-dialog help-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="helpModalLabel">Module help</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body help-body">

</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>

<!-- Bootstrap and necessary plugins-->
<script src="js/jquery/jquery.min.js"></script>
<script src="js/popper/popper.min.js"></script>
Expand Down
37 changes: 27 additions & 10 deletions public/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,18 @@ $(function() {
var $button_disable = $("<button type='button' data-id='"+n+"' class='instance-disable btn btn-sm btn-ghost-warning'>disable</button>");
var $button_enable = $("<button type='button' data-id='"+n+"' class='instance-enable btn btn-sm btn-ghost-success'>enable</button>");

if (i.instance_type != 'bitfocus-companion') {
$td_actions.append($button_delete)
$td_actions.append($("<span>&nbsp;</span>"));
}
$td_actions.append($button_delete)
$td_actions.append($("<span>&nbsp;</span>"));

if (i.instance_type != 'bitfocus-companion' && (i.enabled === undefined || i.enabled === true)) {
if (i.enabled === undefined || i.enabled === true) {
$td_actions.append($button_disable)
$button_edit.show();
}
else if (i.instance_type != 'bitfocus-companion') {
else {
$td_actions.append($button_enable);
$button_edit.hide();
}


$td_actions.append($("<span>&nbsp;</span>"));

$td_actions.append($button_edit);
Expand Down Expand Up @@ -137,7 +134,11 @@ $(function() {

for (var x in instance.module) {
if (instance.module[x].name == list[n].instance_type) {
$td_id.html("<b>"+instance.module[x].shortname+"</b>" + "<br>" + instance.module[x].manufacturer);
var help = '';
if (instance.module[x].help) {
help = '<div class="instance_help"><i class="fa fa-question-circle"></i></div>';
}
$td_id.html(help + "<b>"+instance.module[x].shortname+"</b>" + "<br>" + instance.module[x].manufacturer);
}
}

Expand All @@ -151,6 +152,24 @@ $(function() {
$tr.append($td_status);
$tr.append($td_actions);

(function (name) {
$tr.find('.instance_help').click(function () {
socket.emit('instance_get_help', name);
socket.once('instance_get_help:result', function (err, result) {
if (err) {
alert('Error getting help text');
return;
}
if (result) {
var $helpModal = $('#helpModal');
$helpModal.find('.modal-title').html('Help for ' + name);
$helpModal.find('.modal-body').html(result);
$helpModal.modal();
}
});
});
})(list[n].instance_type);

$il.append($tr);

}
Expand Down Expand Up @@ -373,8 +392,6 @@ $(function() {
$icv.show();
$icvl.html('');

console.log()

for (var i in instance_variables[current_instance]) {
var variable = instance_variables[current_instance][i];
$icvl.append('<tr data-id="' + current_instance + ':' + variable.name + '"><td>$(' + current_instance + ':' + variable.name + ')</td><td>' + variable.label + '</td><td>' + instance_variabledata[current_instance + ':' + variable.name] + '</td></tr>');
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2112,6 +2112,11 @@ map-visit@^1.0.0:
dependencies:
object-visit "^1.0.0"

marked@^0.6.0:
version "0.6.0"
resolved "https://registry.yarnpkg.com/marked/-/marked-0.6.0.tgz#a18d01cfdcf8d15c3c455b71c8329e5e0f01faa1"
integrity sha512-HduzIW2xApSXKXJSpCipSxKyvMbwRRa/TwMbepmlZziKdH8548WSoDP4SxzulEKjlo8BE39l+2fwJZuRKOln6g==

[email protected]:
version "0.3.0"
resolved "https://registry.yarnpkg.com/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748"
Expand Down

0 comments on commit 86680c5

Please sign in to comment.