Files
grocy/grocy.js

104 lines
1.7 KiB
JavaScript
Raw Normal View History

2017-04-15 23:16:20 +02:00
var Grocy = {};
$(function()
{
2017-04-16 23:11:03 +02:00
var menuItem = $('.nav').find("[data-nav-for-page='" + Grocy.ContentPage + "']");
2017-04-15 23:16:20 +02:00
menuItem.addClass('active');
2017-04-16 23:11:03 +02:00
$.timeago.settings.allowFuture = true;
$('time.timeago').timeago();
2017-04-15 23:16:20 +02:00
});
Grocy.FetchJson = function(url, success, error)
{
var xhr = new XMLHttpRequest();
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.PostJson = function(url, jsonData, success, error)
{
var xhr = new XMLHttpRequest();
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
};
Grocy.EmptyElementWhenMatches = function(selector, text)
{
if ($(selector).text() === text)
{
$(selector).text('');
}
};
String.prototype.contains = function(search)
{
return this.toLowerCase().indexOf(search.toLowerCase()) !== -1;
};
Grocy.GetUriParam = function(key)
{
var currentUri = decodeURIComponent(window.location.search.substring(1));
var vars = currentUri.split('&');
2017-04-21 12:30:08 +02:00
for (i = 0; i < vars.length; i++)
{
var currentParam = vars[i].split('=');
if (currentParam[0] === key)
{
return currentParam[1] === undefined ? true : currentParam[1];
}
}
};
Grocy.Wait = function(ms)
{
return new Promise(resolve => setTimeout(resolve, ms));
}