mybuddy/babybuddy/static_src/js/babybuddy.js

97 lines
2.7 KiB
JavaScript

if (typeof jQuery === 'undefined') {
throw new Error('Baby Buddy requires jQuery.')
}
if (typeof moment === 'undefined') {
throw new Error('Baby Buddy requires moment.js.')
}
/**
* Baby Buddy Namespace
*
* Default namespace for the Baby Buddy app.
*
* @type {{}}
*/
var BabyBuddy = function () {
return {};
}();
/**
* Datetime Picker.
*
* Provides modifications and defaults for the base datetime picker widget.
*
* @type {{init: BabyBuddy.DatetimePicker.init}}
*/
BabyBuddy.DatetimePicker = function ($, moment) {
return {
init: function (element, options) {
var defaultOptions = {
buttons: { showToday: true, showClose: true },
defaultDate: 'now',
focusOnShow: false,
format: 'L LT',
ignoreReadonly: true,
locale: moment.locale(),
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'
},
viewMode: 'times',
};
// Provide the initial date value from the form, if available. This is not
// strictly necessary but acts as a workaround for a bug with certain
// locales e.g., Portuguese (pt), that causes the default value to be lost
// in the form.
// @see https://github.com/tempusdominus/bootstrap-4/issues/189#issuecomment-590791416
var id = element.attr('id');
var input = element.find(`input[data-target="#${id}"]`)
if (input.length === 1) {
defaultOptions.date = input.val()
}
element.datetimepicker($.extend(defaultOptions, options));
}
};
}(jQuery, moment);
/**
* 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);
/**
* Fix for duplicate form submission from double pressing submit
*/
function preventDoubleSubmit() {
return false;
}
$('form').off("submit", preventDoubleSubmit);
$("form").on("submit", function() {
$(this).on("submit", preventDoubleSubmit);
});