Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix clock if authentication is enabled
Add .npmignore file
  • Loading branch information
GermanBluefox committed Aug 6, 2019
commit 3c259146d67c99c613b90d4b16a6bc3d350e9e3b
26 changes: 26 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
.github
bin
docs
node_modules
testing
tests
.deployment
.dockerignore
.eslintrc.js
.gitattributes
.gitignore
.jsbeautifyrc
.nvmrc
.travis.yml
.npmignore
azuredeploy.json
CONTRIBUTING.md
deploy.sh
Dockerfile.example
example-profiles.md
Makefile
my.env.template
prep_repo_release.sh
Procfile
setup.sh
Vagrantfile
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ function create (env, ctx) {
if (fs.existsSync(process.cwd() + '/tmp/cacheBusterToken')) {
tmpFiles = express.static('tmp', {
maxAge: maxAge
});
});
} else {
Comment thread
PieterGit marked this conversation as resolved.
tmpFiles = express.static(__dirname + '/tmp', {
maxAge: maxAge
Expand Down
45 changes: 31 additions & 14 deletions lib/client/clock-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,27 @@ client.settings = browserSettings(client, window.serverSettings, $);

client.query = function query () {
console.log('query');
$.ajax('/api/v1/entries.json?count=3', {
var parts = (location.search || '?').substring(1).split('&');
var token = '';
parts.forEach(function (val) {
if (val.startsWith('token=')) {
token = val.substring('token='.length);
}
});

var secret = localStorage.getItem('apisecrethash');
var src = '/api/v1/entries.json?count=3&t=' + new Date().getTime();

if (secret) {
src += '&secret=' + secret;
} else if (token) {
src += '&token=' + token;
}

$.ajax(src, {
Comment thread
PieterGit marked this conversation as resolved.
success: client.render
});
}
};
Comment thread
PieterGit marked this conversation as resolved.

client.render = function render (xhr) {
console.log('got data', xhr);
Expand All @@ -32,7 +49,7 @@ client.render = function render (xhr) {
let now = new Date();

// Convert BG to mmol/L if necessary.
if (window.serverSettings.settings.units == 'mmol') {
if (window.serverSettings.settings.units === 'mmol') {
var displayValue = window.Nightscout.units.mgdlToMMOL(rec.sgv);
Comment thread
PieterGit marked this conversation as resolved.
} else {
displayValue = rec.sgv;
Expand All @@ -42,7 +59,7 @@ client.render = function render (xhr) {
$('#bgnow').html(displayValue);

// Insert the trend arrow.
$('#arrow').attr('src', '/images/' + rec.direction + '.svg');
$('#arrow').attr('src', '/images/' + (!rec.direction || rec.direction === 'NOT COMPUTABLE' ? 'NONE' : rec.direction) + '.svg');
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If calculation cannot be done, so invalid icon was shown. This will fix the icon.


// Time before data considered stale.
let staleMinutes = 13;
Expand All @@ -52,13 +69,13 @@ client.render = function render (xhr) {
$('#bgnow').toggleClass('stale', (now - last > threshold));

// Generate and insert the clock.
let timeDivisor = (client.settings.timeFormat) ? client.settings.timeFormat : 12;
let timeDivisor = parseInt(client.settings.timeFormat ? client.settings.timeFormat : 12, 10);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just number validation.

let today = new Date()
, h = today.getHours() % timeDivisor;
if (timeDivisor == 12) {
h = (h == 0) ? 12 : h; // In the case of 00:xx, change to 12:xx for 12h time
, h = today.getHours() % timeDivisor;
if (timeDivisor === 12) {
h = h || 12; // In the case of 00:xx, change to 12:xx for 12h time
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not only formatting. For h == 4 this will be changed to h==12. Please restore old behaviour, perhaps with === instead of ==

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed on my branch

}
if (timeDivisor == 24) {
if (timeDivisor === 24) {
h = (h < 10) ? ("0" + h) : h; // Pad the hours with a 0 in 24h time
}
let m = today.getMinutes();
Expand All @@ -67,14 +84,14 @@ client.render = function render (xhr) {

var queryDict = {};
location.search.substr(1).split("&").forEach(function(item) { queryDict[item.split("=")[0]] = item.split("=")[1] });

if (!window.serverSettings.settings.showClockClosebutton || !queryDict['showClockClosebutton']) {
$('#close').css('display', 'none');
}

// defined in the template this is loaded into
// eslint-disable-next-line no-undef
if (clockFace == 'clock-color') {
if (clockFace === 'clock-color') {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting


var bgHigh = window.serverSettings.settings.thresholds.bgHigh;
var bgLow = window.serverSettings.settings.thresholds.bgLow;
Expand Down Expand Up @@ -138,12 +155,12 @@ client.render = function render (xhr) {
$('#arrow').css('filter', 'brightness(100%)');
}
}
}
};

client.init = function init () {
console.log('init');
client.query();
setInterval(client.query, 1 * 60 * 1000);
}
};

module.exports = client;
module.exports = client;
49 changes: 35 additions & 14 deletions views/clockviews/shared.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="apple-mobile-web-app-capable" content="yes">

<title>Nightscout</title>

<link href="/images/round1.png" rel="icon" id="favicon" type="image/png" />
<link rel="apple-touch-icon" sizes="57x57" href="/images/apple-touch-icon-57x57.png">
<link rel="apple-touch-icon" sizes="60x60" href="/images/apple-touch-icon-60x60.png">
Expand All @@ -16,11 +16,11 @@
<link rel="apple-touch-icon" sizes="144x144" href="/images/apple-touch-icon-144x144.png">
<link rel="apple-touch-icon" sizes="152x152" href="/images/apple-touch-icon-152x152.png">
<link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-180x180.png">

<style type="text/css">
@import url("//fonts.googleapis.com/css?family=Open+Sans:700");
@import url("//fonts.googleapis.com/css?family=Open+Sans:700");
<%- include(face + '.css', {}); %>

</style>
</head>

Expand All @@ -30,19 +30,40 @@
<div class="inner">
<div id="trend">
<div id="bgnow"></div>
<div id="arrowDiv"><img id="arrow" src=""/></div>
<div id="arrowDiv"><img id="arrow" src="" alt="arrow"/></div>
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a HTML w3c requirement. Alt should be in every "img"

</div>
<div id="clock"></div>
<div id="staleTime"></div>
</div>
</main>

<script src="/api/v1/status.js"></script>
<script src="<%= locals.bundle %>/js/bundle.clock.js?v=<%= locals.cachebuster %>"></script>

<script src="/<%= locals.bundle %>/js/bundle.clock.js?v=<%= locals.cachebuster %>"></script>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

locals.bundle is /bundle by default, so I get error:
image
remove / in front of it to make it work

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please cherry pick PieterGit@e143142


<script type="text/javascript">
let clockFace = "<%= face %>"; // can now be used in scripts
window.Nightscout.client.init( );
var clockFace = "<%= face %>"; // can now be used in scripts

var parts = (location.search || '?').substring(1).split('&');
var token = '';
parts.forEach(function (val) {
if (val.startsWith('token=')) {
token = val.substring('token='.length);
}
});

var secret = typeof localStorage !== 'undefined' ? localStorage.getItem('apisecrethash') : '';
var src = '/api/v1/status.js?t=' + Date.now();

if (secret) {
src += '&secret=' + secret;
} else if (token) {
src += '&token=' + token;
}

var script = document.createElement('script');
script.onload = function () {
window.Nightscout.client.init( );
};
script.src = src;

document.head.appendChild(script); //or something of the likes
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Load JSON with token or secret for the case that authentication is enabled.

There is still an open case if the user starts directly to work with clock view without calling the index.html. In this case no authentication dialog will be shown. But I don't know such a use case. In this case the user can use token.

</script>
</body>
</html>
</html>