Files
grocy/public/js/grocy.js

103 lines
1.8 KiB
JavaScript
Raw Normal View History

2018-04-16 19:11:32 +02:00
L = function(text, ...placeholderValues)
2017-04-15 23:16:20 +02:00
{
2018-04-16 19:11:32 +02:00
var localizedText = Grocy.LocalizationStrings[text];
if (localizedText === undefined)
{
localizedText = text;
}
for (var i = 0; i < placeholderValues.length; i++)
2018-04-12 21:13:38 +02:00
{
2018-04-16 19:11:32 +02:00
localizedText = localizedText.replace('#' + (i + 1), placeholderValues[i]);
}
return localizedText;
}
U = function(relativePath)
{
return Grocy.BaseUrl.replace(/\/$/, '') + relativePath;
}
2018-04-16 19:11:32 +02:00
if (!Grocy.ActiveNav.isEmpty())
{
var menuItem = $('.nav').find("[data-nav-for-page='" + Grocy.ActiveNav + "']");
menuItem.addClass('active');
}
2017-04-16 23:11:03 +02:00
2018-04-16 19:11:32 +02:00
$.timeago.settings.allowFuture = true;
RefreshContextualTimeago = function()
{
$('time.timeago').timeago();
}
RefreshContextualTimeago();
2017-04-15 23:16:20 +02:00
2018-04-19 21:12:01 +02:00
toastr.options = {
toastClass: 'alert',
closeButton: true,
timeOut: 20000,
extendedTimeOut: 5000
};
Grocy.Api = { };
Grocy.Api.Get = function(apiFunction, success, error)
2017-04-15 23:16:20 +02:00
{
var xhr = new XMLHttpRequest();
var url = U('/api/' + apiFunction);
2017-04-15 23:16:20 +02:00
xhr.onreadystatechange = function()
{
if (xhr.readyState === XMLHttpRequest.DONE)
{
if (xhr.status === 200)
{
if (success)
{
success(JSON.parse(xhr.responseText));
}
}
else
{
if (error)
{
error(xhr);
}
}
}
};
xhr.open('GET', url, true);
xhr.send();
2017-04-16 23:11:03 +02:00
};
2017-04-15 23:16:20 +02:00
Grocy.Api.Post = function(apiFunction, jsonData, success, error)
2017-04-15 23:16:20 +02:00
{
var xhr = new XMLHttpRequest();
var url = U('/api/' + apiFunction);
2017-04-15 23:16:20 +02:00
xhr.onreadystatechange = function()
{
if (xhr.readyState === XMLHttpRequest.DONE)
{
if (xhr.status === 200)
{
if (success)
{
success(JSON.parse(xhr.responseText));
}
}
else
{
if (error)
{
error(xhr);
}
}
}
};
xhr.open('POST', url, true);
xhr.setRequestHeader('Content-type', 'application/json');
xhr.send(JSON.stringify(jsonData));
2017-04-16 23:11:03 +02:00
};