added tags for timesheets (#604)
This commit is contained in:
@@ -5,15 +5,19 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/*!
|
||||
* [KIMAI] KimaiAPILink
|
||||
*
|
||||
* allows to assign the given selector to any element, which then is used as click-handler
|
||||
* calling an API method and trigger the event from data-event attribute afterwards
|
||||
*/
|
||||
|
||||
import KimaiClickHandlerReducedInTableRow from "./KimaiClickHandlerReducedInTableRow";
|
||||
|
||||
/**
|
||||
* Needs to be initialized with a class name.
|
||||
*
|
||||
* A link like <a href=# class=remoteLink> can be activated with:
|
||||
* new KimaiAPILink('remoteLink')
|
||||
*
|
||||
* Allows to assign the given selector to any element, which then is used as click-handler
|
||||
* calling an API method and trigger the event from data-event attribute afterwards.
|
||||
*
|
||||
* @param selector
|
||||
*/
|
||||
export default class KimaiAPILink extends KimaiClickHandlerReducedInTableRow {
|
||||
|
||||
constructor(selector) {
|
||||
@@ -26,7 +30,7 @@ export default class KimaiAPILink extends KimaiClickHandlerReducedInTableRow {
|
||||
document.addEventListener('click', function(event) {
|
||||
let target = event.target;
|
||||
while (!target.matches('body')) {
|
||||
if (target.matches(self.selector)) {
|
||||
if (target.classList.contains(self.selector)) {
|
||||
const attributes = target.dataset;
|
||||
|
||||
let url = attributes['href'];
|
||||
@@ -34,28 +38,12 @@ export default class KimaiAPILink extends KimaiClickHandlerReducedInTableRow {
|
||||
url = target.getAttribute('href');
|
||||
}
|
||||
|
||||
const method = attributes['method'];
|
||||
const eventName = attributes['event'];
|
||||
const api = self.getContainer().getPlugin('api');
|
||||
const eventing = self.getContainer().getPlugin('event');
|
||||
const alert = self.getContainer().getPlugin('alert');
|
||||
|
||||
if (method === 'PATCH') {
|
||||
api.patch(url, function(result) {
|
||||
eventing.trigger(eventName);
|
||||
if (attributes.msgSuccess) {
|
||||
alert.success(attributes.msgSuccess);
|
||||
}
|
||||
}, function(xhr, err) {
|
||||
let message = 'action.update.error';
|
||||
if (attributes.msgError) {
|
||||
message = attributes.msgError;
|
||||
}
|
||||
if (xhr.responseJSON && xhr.responseJSON.message) {
|
||||
err = xhr.responseJSON.message;
|
||||
}
|
||||
alert.error(message, err);
|
||||
if (attributes.question !== undefined) {
|
||||
self.getContainer().getPlugin('alert').question(attributes.question, function() {
|
||||
self._callApi(url, attributes);
|
||||
});
|
||||
} else {
|
||||
self._callApi(url, attributes);
|
||||
}
|
||||
|
||||
event.preventDefault();
|
||||
@@ -67,4 +55,35 @@ export default class KimaiAPILink extends KimaiClickHandlerReducedInTableRow {
|
||||
});
|
||||
}
|
||||
|
||||
_callApi(url, attributes)
|
||||
{
|
||||
const method = attributes['method'];
|
||||
const eventName = attributes['event'];
|
||||
const api = this.getContainer().getPlugin('api');
|
||||
const eventing = this.getContainer().getPlugin('event');
|
||||
const alert = this.getContainer().getPlugin('alert');
|
||||
const successHandle = function(result) {
|
||||
eventing.trigger(eventName);
|
||||
if (attributes.msgSuccess) {
|
||||
alert.success(attributes.msgSuccess);
|
||||
}
|
||||
};
|
||||
const errorHandle = function(xhr, err) {
|
||||
let message = 'action.update.error';
|
||||
if (attributes.msgError) {
|
||||
message = attributes.msgError;
|
||||
}
|
||||
if (xhr.responseJSON && xhr.responseJSON.message) {
|
||||
err = xhr.responseJSON.message;
|
||||
}
|
||||
alert.error(message, err);
|
||||
};
|
||||
|
||||
if (method === 'PATCH') {
|
||||
api.patch(url, successHandle, errorHandle);
|
||||
} else if (method === 'DELETE') {
|
||||
api.delete(url, successHandle, errorHandle);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user