2018-10-12 03:11:57 +00:00
|
|
|
if (typeof jQuery === 'undefined') {
|
|
|
|
throw new Error('Baby Buddy requires jQuery.')
|
|
|
|
}
|
2020-01-27 22:56:37 +00:00
|
|
|
if (typeof moment === 'undefined') {
|
|
|
|
throw new Error('Baby Buddy requires moment.js.')
|
|
|
|
}
|
2018-10-12 03:11:57 +00:00
|
|
|
|
2020-01-27 22:56:37 +00:00
|
|
|
/**
|
|
|
|
* Baby Buddy Namespace
|
|
|
|
*
|
|
|
|
* Default namespace for the Baby Buddy app.
|
2020-02-07 20:26:48 +00:00
|
|
|
*
|
|
|
|
* @type {{}}
|
2020-01-27 22:56:37 +00:00
|
|
|
*/
|
2018-10-12 03:11:57 +00:00
|
|
|
var BabyBuddy = function () {
|
2020-01-27 22:56:37 +00:00
|
|
|
return {};
|
2018-10-12 03:11:57 +00:00
|
|
|
}();
|
|
|
|
|
2020-01-27 22:56:37 +00:00
|
|
|
/**
|
2020-02-07 20:26:48 +00:00
|
|
|
* Datetime Picker.
|
2020-01-27 22:56:37 +00:00
|
|
|
*
|
|
|
|
* Provides modifications and defaults for the base datetime picker widget.
|
2020-02-07 20:26:48 +00:00
|
|
|
*
|
|
|
|
* @type {{init: BabyBuddy.DatetimePicker.init}}
|
2020-01-27 22:56:37 +00:00
|
|
|
*/
|
|
|
|
BabyBuddy.DatetimePicker = function ($, moment) {
|
|
|
|
return {
|
|
|
|
init: function (element, options) {
|
|
|
|
var defaultOptions = {
|
2020-01-27 22:59:40 +00:00
|
|
|
buttons: { showToday: true, showClose: true },
|
2020-01-27 22:56:37 +00:00
|
|
|
defaultDate: 'now',
|
2022-02-26 14:14:40 +00:00
|
|
|
focusOnShow: false,
|
2020-01-27 22:56:37 +00:00
|
|
|
format: 'L LT',
|
|
|
|
ignoreReadonly: true,
|
|
|
|
locale: moment.locale(),
|
2021-09-04 03:17:38 +00:00
|
|
|
useCurrent: false,
|
|
|
|
icons: {
|
|
|
|
time: 'icon-clock',
|
|
|
|
date: 'icon-calendar',
|
|
|
|
up: 'icon-arrow-up',
|
|
|
|
down: 'icon-arrow-down',
|
|
|
|
previous: 'icon-angle-circled-left',
|
|
|
|
next: 'icon-angle-circled-right',
|
|
|
|
today: 'icon-today',
|
|
|
|
clear: 'icon-delete',
|
|
|
|
close: 'icon-cancel'
|
|
|
|
},
|
2021-09-04 03:45:21 +00:00
|
|
|
viewMode: 'times',
|
2020-01-27 22:56:37 +00:00
|
|
|
};
|
|
|
|
element.datetimepicker($.extend(defaultOptions, options));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}(jQuery, moment);
|
|
|
|
|
2020-02-07 20:26:48 +00:00
|
|
|
/**
|
|
|
|
* Pull to refresh.
|
|
|
|
*
|
|
|
|
* @type {{init: BabyBuddy.PullToRefresh.init, onRefresh: BabyBuddy.PullToRefresh.onRefresh}}
|
|
|
|
*/
|
|
|
|
BabyBuddy.PullToRefresh = function(ptr) {
|
|
|
|
return {
|
|
|
|
init: function () {
|
|
|
|
ptr.init({
|
|
|
|
mainElement: 'body',
|
|
|
|
onRefresh: this.onRefresh
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
onRefresh: function() {
|
|
|
|
window.location.reload();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}(PullToRefresh);
|
|
|
|
|
2020-10-28 17:58:47 +00:00
|
|
|
/**
|
|
|
|
* Fix for duplicate form submission from double pressing submit
|
|
|
|
*/
|
2022-07-20 11:34:42 +00:00
|
|
|
function preventDoubleSubmit() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$('form').off("submit", preventDoubleSubmit);
|
2020-10-28 17:58:47 +00:00
|
|
|
$("form").on("submit", function() {
|
2022-07-20 11:34:42 +00:00
|
|
|
$(this).on("submit", preventDoubleSubmit);
|
2020-10-28 17:58:47 +00:00
|
|
|
});
|
2022-07-20 11:34:42 +00:00
|
|
|
|
2018-10-12 03:11:57 +00:00
|
|
|
/* Baby Buddy Timer
|
|
|
|
*
|
|
|
|
* Uses a supplied ID to run a timer. The element using the ID must have
|
|
|
|
* three children with the following classes:
|
|
|
|
* * timer-seconds
|
|
|
|
* * timer-minutes
|
|
|
|
* * timer-hours
|
|
|
|
*/
|
|
|
|
BabyBuddy.Timer = function ($) {
|
|
|
|
var runIntervalId = null;
|
|
|
|
var timerId = null;
|
|
|
|
var timerElement = null;
|
|
|
|
var lastUpdate = moment();
|
2019-08-23 03:35:52 +00:00
|
|
|
var hidden = null;
|
2018-10-12 03:11:57 +00:00
|
|
|
|
|
|
|
var Timer = {
|
|
|
|
run: function(timer_id, element_id) {
|
|
|
|
timerId = timer_id;
|
|
|
|
timerElement = $('#' + element_id);
|
|
|
|
|
|
|
|
if (timerElement.length == 0) {
|
|
|
|
console.error('BBTimer: Timer element not found.');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timerElement.find('.timer-seconds').length == 0
|
|
|
|
|| timerElement.find('.timer-minutes').length == 0
|
|
|
|
|| timerElement.find('.timer-hours').length == 0) {
|
|
|
|
console.error('BBTimer: Element does not contain expected children.');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
runIntervalId = setInterval(this.tick, 1000);
|
|
|
|
|
|
|
|
// If the page just came in to view, update the timer data with the
|
|
|
|
// current actual duration. This will (potentially) help mobile
|
|
|
|
// phones that lock with the timer page open.
|
2019-08-23 03:35:52 +00:00
|
|
|
if (typeof document.hidden !== "undefined") {
|
|
|
|
hidden = "hidden";
|
|
|
|
}
|
|
|
|
else if (typeof document.msHidden !== "undefined") {
|
|
|
|
hidden = "msHidden";
|
|
|
|
}
|
|
|
|
else if (typeof document.webkitHidden !== "undefined") {
|
|
|
|
hidden = "webkitHidden";
|
|
|
|
}
|
|
|
|
window.addEventListener('focus', Timer.handleVisibilityChange, false);
|
|
|
|
},
|
|
|
|
|
|
|
|
handleVisibilityChange: function() {
|
|
|
|
if (!document[hidden] && moment().diff(lastUpdate) > 10000) {
|
|
|
|
Timer.update();
|
|
|
|
}
|
2018-10-12 03:11:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
tick: function() {
|
|
|
|
var s = timerElement.find('.timer-seconds');
|
|
|
|
var seconds = Number(s.text());
|
|
|
|
if (seconds < 59) {
|
|
|
|
s.text(seconds + 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
s.text(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
var m = timerElement.find('.timer-minutes');
|
|
|
|
var minutes = Number(m.text());
|
|
|
|
if (minutes < 59) {
|
|
|
|
m.text(minutes + 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
m.text(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
var h = timerElement.find('.timer-hours');
|
|
|
|
var hours = Number(h.text());
|
|
|
|
h.text(hours + 1);
|
|
|
|
},
|
|
|
|
|
|
|
|
update: function() {
|
|
|
|
$.get('/api/timers/' + timerId + '/', function(data) {
|
|
|
|
if (data && 'duration' in data) {
|
|
|
|
clearInterval(runIntervalId);
|
|
|
|
var duration = moment.duration(data.duration);
|
|
|
|
timerElement.find('.timer-hours').text(duration.hours());
|
|
|
|
timerElement.find('.timer-minutes').text(duration.minutes());
|
|
|
|
timerElement.find('.timer-seconds').text(duration.seconds());
|
|
|
|
lastUpdate = moment();
|
|
|
|
|
|
|
|
if (data['active']) {
|
|
|
|
runIntervalId = setInterval(Timer.tick, 1000);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
timerElement.addClass('timer-stopped');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return Timer;
|
|
|
|
}(jQuery);
|
|
|
|
|
|
|
|
/* Baby Buddy Dashboard
|
|
|
|
*
|
|
|
|
* Provides a "watch" function to update the dashboard at one minute intervals
|
|
|
|
* and/or on visibility state changes.
|
|
|
|
*/
|
|
|
|
BabyBuddy.Dashboard = function ($) {
|
|
|
|
var runIntervalId = null;
|
|
|
|
var dashboardElement = null;
|
|
|
|
var hidden = null;
|
|
|
|
|
|
|
|
var Dashboard = {
|
|
|
|
watch: function(element_id, refresh_rate) {
|
|
|
|
dashboardElement = $('#' + element_id);
|
|
|
|
|
|
|
|
if (dashboardElement.length == 0) {
|
|
|
|
console.error('Baby Buddy: Dashboard element not found.');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof document.hidden !== "undefined") {
|
|
|
|
hidden = "hidden";
|
|
|
|
}
|
|
|
|
else if (typeof document.msHidden !== "undefined") {
|
|
|
|
hidden = "msHidden";
|
|
|
|
}
|
|
|
|
else if (typeof document.webkitHidden !== "undefined") {
|
|
|
|
hidden = "webkitHidden";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof window.addEventListener === "undefined" || typeof document.hidden === "undefined") {
|
|
|
|
if (refresh_rate) {
|
|
|
|
runIntervalId = setInterval(this.update, refresh_rate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
window.addEventListener('focus', Dashboard.handleVisibilityChange, false);
|
2021-07-13 22:49:09 +00:00
|
|
|
if (refresh_rate) {
|
|
|
|
runIntervalId = setInterval(Dashboard.handleVisibilityChange, refresh_rate);
|
|
|
|
}
|
2018-10-12 03:11:57 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
handleVisibilityChange: function() {
|
|
|
|
if (!document[hidden]) {
|
|
|
|
Dashboard.update();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
update: function() {
|
|
|
|
// TODO: Someday maybe update in place?
|
|
|
|
location.reload();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return Dashboard;
|
|
|
|
}(jQuery);
|