Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor moment to be loaded from ctx #7331

Merged
merged 5 commits into from
Jan 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 8 additions & 2 deletions bundle/bundle.source.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@ require('../node_modules/flot/jquery.flot.time');
require('../node_modules/flot/jquery.flot.pie');
require('../node_modules/flot/jquery.flot.fillbetween');

window.moment = require('moment-timezone');
const moment = require('moment-timezone');

window.moment = moment;

window.Nightscout = window.Nightscout || {};

var ctx = {
moment: moment
};

window.Nightscout = {
client: require('../lib/client'),
units: require('../lib/units')(),
admin_plugins: require('../lib/admin_plugins/')()
admin_plugins: require('../lib/admin_plugins/')(ctx)
};

window.Nightscout.report_plugins_preinit = require('../lib/report_plugins/');
Expand Down
5 changes: 3 additions & 2 deletions lib/admin_plugins/cleanentriesdb.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
'use strict';

var moment = require('moment');
var moment;

var cleanentriesdb = {
name: 'cleanentriesdb'
, label: 'Clean Mongo entries (glucose entries) database'
, pluginType: 'admin'
};

function init() {
function init(ctx) {
moment = ctx.moment;
return cleanentriesdb;
}

Expand Down
5 changes: 3 additions & 2 deletions lib/admin_plugins/cleanstatusdb.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
'use strict';

var moment = require('moment');
var moment;

var cleanstatusdb = {
name: 'cleanstatusdb'
, label: 'Clean Mongo status database'
, pluginType: 'admin'
};

function init () {
function init (ctx) {
moment = ctx.moment;
return cleanstatusdb;
}

Expand Down
5 changes: 3 additions & 2 deletions lib/admin_plugins/cleantreatmentsdb.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
'use strict';

var moment = require('moment');
var moment;

var cleantreatmentsdb = {
name: 'cleantreatmentsdb'
, label: 'Clean Mongo treatments database'
, pluginType: 'admin'
};

function init() {
function init(ctx) {
moment = ctx.moment;
return cleantreatmentsdb;
}

Expand Down
14 changes: 7 additions & 7 deletions lib/admin_plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
var _find = require('lodash/find');
var _each = require('lodash/each');

function init() {
function init(ctx) {
var allPlugins = [
require('./subjects')()
, require('./roles')()
, require('./cleanstatusdb')()
, require('./cleantreatmentsdb')()
, require('./cleanentriesdb')()
, require('./futureitems')()
require('./subjects')(ctx)
, require('./roles')(ctx)
, require('./cleanstatusdb')(ctx)
, require('./cleantreatmentsdb')(ctx)
, require('./cleanentriesdb')(ctx)
, require('./futureitems')(ctx)
];

function plugins(name) {
Expand Down
12 changes: 5 additions & 7 deletions lib/client/boluscalc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

var _ = require('lodash');
var moment = require('moment-timezone');
var times = require('../times');
var Storages = require('js-storage');

Expand Down Expand Up @@ -46,9 +45,9 @@ function init (client, $) {
}

function setDateAndTime (time) {
time = time || moment();
eventTime.val(time.format('HH:mm'));
eventDate.val(time.format('YYYY-MM-DD'));
time = time || new Date();
eventTime.val(time.getHours() + ":" + time.getMinutes());
eventDate.val(time.toISOString().split('T')[0]);
}

function mergeDateAndTime () {
Expand Down Expand Up @@ -125,16 +124,15 @@ function init (client, $) {

boluscalc.calculateInsulin();
maybePrevent(event);
// Nightscout.utils.updateBrushToTime(moment.toDate());
};

boluscalc.eventTimeTypeChange = function eventTimeTypeChange (event) {
if ($('#bc_othertime').is(':checked')) {
$('#bc_eventTimeValue').focus();
$('#bc_retro').css('display', '');
if (mergeDateAndTime() < moment()) {
if (mergeDateAndTime() < Date.now()) {
$('#bc_retro').css('background-color', 'red').text(translate('RETRO MODE'));
} else if (mergeDateAndTime() > moment()) {
} else if (mergeDateAndTime() > Date.now()) {
$('#bc_retro').css('background-color', 'blue').text(translate('IN THE FUTURE'));
} else {
$('#bc_retro').css('display', 'none');
Expand Down
7 changes: 3 additions & 4 deletions lib/client/careportal.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

var moment = require('moment-timezone');
var _ = require('lodash');
var parse_duration = require('parse-duration'); // https://www.npmjs.com/package/parse-duration
var times = require('../times');
Expand All @@ -18,9 +17,9 @@ function init (client, $) {
var eventDate = $('#eventDateValue');

function setDateAndTime (time) {
time = time || moment();
eventTime.val(time.format('HH:mm'));
eventDate.val(time.format('YYYY-MM-DD'));
time = time || client.ctx.moment();
eventTime.val(time.hours() + ":" + time.minutes());
eventDate.val(time.toISOString().split('T')[0]);
}

function mergeDateAndTime () {
Expand Down
9 changes: 8 additions & 1 deletion lib/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ var Storages = require('js-storage');

var language = require('../language')();
var sandbox = require('../sandbox')();
var profile = require('../profilefunctions')();
var units = require('../units')();
var levels = require('../levels');
var times = require('../times');
Expand All @@ -18,6 +17,8 @@ var receiveDData = require('./receiveddata');
var brushing = false;

var browserSettings;
var moment = window.moment;
var timezones = moment.tz.names();

var client = {};

Expand Down Expand Up @@ -203,13 +204,15 @@ client.load = function load (serverSettings, callback) {
, extendedSettings: client.settings.extendedSettings
, language: language
, levels: levels
, moment: moment
}).registerClientDefaults();

browserSettings.loadPluginSettings(client);

client.utils = require('../utils')({
settings: client.settings
, language: language
, moment: moment
});

client.rawbg = client.plugins('rawbg');
Expand All @@ -223,6 +226,8 @@ client.load = function load (serverSettings, callback) {
, bus: require('../bus')(client.settings, client.ctx)
, settings: client.settings
, pluginBase: client.plugins.base(majorPills, minorPills, statusPills, bgStatus, client.tooltip, Storages.localStorage)
, moment: moment
, timezones: timezones
};

client.ctx.language = language;
Expand Down Expand Up @@ -298,6 +303,8 @@ client.load = function load (serverSettings, callback) {
client.careportal = require('./careportal')(client, $);
client.boluscalc = require('./boluscalc')(client, $);

var profile = require('../profilefunctions')(null, client.ctx);

client.profilefunctions = profile;

client.editMode = false;
Expand Down
1 change: 0 additions & 1 deletion lib/data/dataloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ function init(env, ctx) {
});

console.info('Load Complete:\n\t', counts.join(', '));

done(err, result);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/ar2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

var _ = require('lodash');
var times = require('../times');
var moment = require('moment');

var BG_REF = 140; //Central tendency
var BG_MIN = 36; //Not 39, but why?
Expand All @@ -17,6 +16,7 @@ var AR2_COLOR = 'cyan';

function init (ctx) {
var translate = ctx.language.translate;
var moment = ctx.moment;

var ar2 = {
name: 'ar2'
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/basalprofile.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';
var times = require('../times');
var moment = require('moment');
var consts = require('../constants');
var _ = require('lodash');

function init (ctx) {
var moment = ctx.moment;

var translate = ctx.language.translate;

Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/batteryage.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

var _ = require('lodash');
var moment = require('moment');

function init(ctx) {
var moment = ctx.moment;
var translate = ctx.language.translate;
var levels = ctx.levels;

Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/bgnow.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use strict';

var _ = require('lodash');
var moment = require('moment');
var times = require('../times');

var offset = times.mins(2.5).msecs;
var bucketFields = ['index', 'fromMills', 'toMills'];

function init (ctx) {

var moment = ctx.moment;
var translate = ctx.language.translate;
var utils = require('../utils')(ctx);

Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/cannulaage.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

var _ = require('lodash');
var moment = require('moment');

function init(ctx) {
var moment = ctx.moment;
var translate = ctx.language.translate;
var levels = ctx.levels;

Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/cob.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';

var _ = require('lodash')
, moment = require('moment')
, times = require('../times');

function init (ctx) {
var moment = ctx.moment;
var translate = ctx.language.translate;
var iob = require('./iob')(ctx);

Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/insulinage.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

var _ = require('lodash');
var moment = require('moment');

function init(ctx) {
var moment = ctx.moment;
var translate = ctx.language.translate;
var levels = ctx.levels;

Expand Down
6 changes: 3 additions & 3 deletions lib/plugins/iob.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';

var _ = require('lodash')
, moment = require('moment')
, times = require('../times');
const _ = require('lodash')
const times = require('../times');

function init(ctx) {
var moment = ctx.moment;
var translate = ctx.language.translate;
var utils = require('../utils')(ctx);

Expand Down
3 changes: 2 additions & 1 deletion lib/plugins/loop.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
'use strict';

var _ = require('lodash');
var moment = require('moment');
var times = require('../times');

// var ALL_STATUS_FIELDS = ['status-symbol', 'status-label', 'iob', 'freq', 'rssi']; Unused variable

function init (ctx) {
var moment = ctx.moment;

var utils = require('../utils')(ctx);
var translate = ctx.language.translate;
var levels = ctx.levels;
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/openaps.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict';

var _ = require('lodash');
var moment = require('moment');
var times = require('../times');
var consts = require('../constants');

// var ALL_STATUS_FIELDS = ['status-symbol', 'status-label', 'iob', 'meal-assist', 'freq', 'rssi']; Unused variable

function init (ctx) {
var moment = ctx.moment;
var utils = require('../utils')(ctx);
var openaps = {
name: 'openaps'
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/pump.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict';

var _ = require('lodash');
var moment = require('moment');
var times = require('../times');

var ALL_STATUS_FIELDS = ['reservoir', 'battery', 'clock', 'status', 'device'];

function init (ctx) {
var moment = ctx.moment;
var translate = ctx.language.translate;
var timeago = require('./timeago')(ctx);
var openaps = require('./openaps')(ctx);
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/sensorage.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';

var _ = require('lodash');
var moment = require('moment');
var times = require('../times');

function init(ctx) {
var moment = ctx.moment;
var translate = ctx.language.translate;
var levels = ctx.levels;

Expand Down
3 changes: 2 additions & 1 deletion lib/plugins/virtAsstBase.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict';

var moment = require('moment');
var _each = require('lodash/each');

function init(env, ctx) {
var moment = ctx.moment;

function virtAsstBase() {
return virtAsstBase;
}
Expand Down
Loading