2019-07-02 14:38:42 +02:00
|
|
|
import Rails from '@rails/ujs';
|
2018-10-09 11:32:27 +02:00
|
|
|
import $ from 'jquery';
|
|
|
|
import debounce from 'debounce';
|
|
|
|
|
|
|
|
export { debounce };
|
2019-05-22 12:00:05 +02:00
|
|
|
export const { fire, ajax } = Rails;
|
2018-10-09 11:32:27 +02:00
|
|
|
|
2019-11-20 11:33:56 +01:00
|
|
|
export function show(el) {
|
|
|
|
el && el.classList.remove('hidden');
|
2018-09-06 19:23:27 +02:00
|
|
|
}
|
|
|
|
|
2019-11-20 11:33:56 +01:00
|
|
|
export function hide(el) {
|
|
|
|
el && el.classList.add('hidden');
|
2018-09-06 19:23:27 +02:00
|
|
|
}
|
|
|
|
|
2019-11-20 11:33:56 +01:00
|
|
|
export function toggle(el) {
|
|
|
|
el && el.classList.toggle('hidden');
|
2018-09-06 19:23:27 +02:00
|
|
|
}
|
2018-10-09 11:32:27 +02:00
|
|
|
|
2019-11-19 17:55:30 +01:00
|
|
|
export function enable(el) {
|
|
|
|
el && (el.disabled = false);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function disable(el) {
|
|
|
|
el && (el.disabled = true);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function hasClass(el, cssClass) {
|
|
|
|
return el && el.classList.contains(cssClass);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function addClass(el, cssClass) {
|
|
|
|
el && el.classList.add(cssClass);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function removeClass(el, cssClass) {
|
|
|
|
el && el.classList.remove(cssClass);
|
|
|
|
}
|
|
|
|
|
2018-10-09 11:32:27 +02:00
|
|
|
export function delegate(eventNames, selector, callback) {
|
|
|
|
eventNames
|
|
|
|
.split(' ')
|
|
|
|
.forEach(eventName =>
|
|
|
|
Rails.delegate(document, selector, eventName, callback)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getJSON(url, data, method = 'get') {
|
2019-06-04 15:06:49 +02:00
|
|
|
incrementActiveRequestsCount();
|
2018-10-09 11:32:27 +02:00
|
|
|
data = method !== 'get' ? JSON.stringify(data) : data;
|
2019-06-04 15:06:49 +02:00
|
|
|
return Promise.resolve(
|
|
|
|
$.ajax({
|
|
|
|
method,
|
|
|
|
url,
|
|
|
|
data,
|
|
|
|
contentType: 'application/json',
|
|
|
|
dataType: 'json'
|
|
|
|
})
|
|
|
|
).finally(decrementActiveRequestsCount);
|
2018-10-09 11:32:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function scrollTo(container, scrollTo) {
|
2018-10-09 11:43:38 +02:00
|
|
|
container.scrollTop =
|
|
|
|
offset(scrollTo).top - offset(container).top + container.scrollTop;
|
2018-10-09 11:32:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function scrollToBottom(container) {
|
2018-10-09 11:43:38 +02:00
|
|
|
container.scrollTop = container.scrollHeight;
|
2018-10-09 11:32:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function on(selector, eventName, fn) {
|
|
|
|
[...document.querySelectorAll(selector)].forEach(element =>
|
|
|
|
element.addEventListener(eventName, event => fn(event, event.detail))
|
|
|
|
);
|
|
|
|
}
|
2018-10-09 11:43:38 +02:00
|
|
|
|
2019-03-20 14:29:33 +01:00
|
|
|
export function to(promise) {
|
|
|
|
return promise.then(result => [result]).catch(error => [null, error]);
|
|
|
|
}
|
|
|
|
|
2018-10-09 11:43:38 +02:00
|
|
|
function offset(element) {
|
|
|
|
const rect = element.getBoundingClientRect();
|
|
|
|
return {
|
|
|
|
top: rect.top + document.body.scrollTop,
|
|
|
|
left: rect.left + document.body.scrollLeft
|
|
|
|
};
|
|
|
|
}
|
2019-06-04 15:06:49 +02:00
|
|
|
|
|
|
|
const DATA_ACTIVE_REQUESTS_COUNT = 'data-active-requests-count';
|
|
|
|
|
|
|
|
function incrementActiveRequestsCount() {
|
|
|
|
const count = document.body.getAttribute(DATA_ACTIVE_REQUESTS_COUNT) || '0';
|
|
|
|
document.body.setAttribute(DATA_ACTIVE_REQUESTS_COUNT, parseInt(count) + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
function decrementActiveRequestsCount() {
|
|
|
|
const count = document.body.getAttribute(DATA_ACTIVE_REQUESTS_COUNT) || '0';
|
|
|
|
document.body.setAttribute(DATA_ACTIVE_REQUESTS_COUNT, parseInt(count) - 1);
|
|
|
|
}
|