|
import $ from 'jquery';
|
|
import URI from 'urijs';
|
|
|
|
export function showSpinner() {
|
|
$('#turbolinks-progress').show();
|
|
}
|
|
|
|
export function hideSpinner() {
|
|
$('#turbolinks-progress').hide();
|
|
}
|
|
|
|
export function iconText(name, innerText, iconClass) {
|
|
let icon = `<span class="${iconClass} ${iconClass}-${name}"/>`;
|
|
|
|
if (innerText !== '') {
|
|
icon += `<strong>${innerText}</strong>`;
|
|
}
|
|
return icon;
|
|
}
|
|
|
|
export function activateDatatables() {
|
|
$('[data-table=inline]')
|
|
.not('.dataTable')
|
|
.DataTable({
|
|
language: {
|
|
searchPlaceholder: __('Filter...'),
|
|
},
|
|
dom: "<'row'<'col-md-6'f>r>t<'row'<'col-md-6'i><'col-md-6'p>>",
|
|
});
|
|
|
|
$('[data-table=server]')
|
|
.not('.dataTable')
|
|
.each((i, el) => {
|
|
const url = el.getAttribute('data-source');
|
|
|
|
$(el).DataTable({
|
|
language: {
|
|
searchPlaceholder: __('Filter...'),
|
|
},
|
|
processing: true,
|
|
serverSide: true,
|
|
ordering: false,
|
|
ajax: url,
|
|
dom: "<'row'<'col-md-6'f>r>t<'row'<'col-md-6'><'col-md-6'p>>",
|
|
});
|
|
});
|
|
}
|
|
|
|
export function activateTooltips(elParam = 'body') {
|
|
const el = $(elParam);
|
|
el.find('[rel="twipsy"]').tooltip({ container: 'body' });
|
|
el.find('.ellipsis').tooltip({
|
|
container: 'body',
|
|
title() {
|
|
return this.scrollWidth > this.clientWidth ? this.textContent : null;
|
|
},
|
|
});
|
|
el
|
|
.find('*[title]')
|
|
.not('*[rel]')
|
|
.tooltip({ container: 'body' });
|
|
$(document).on('page:restore', () => {
|
|
$('.tooltip.in').remove();
|
|
});
|
|
}
|
|
|
|
/* eslint-disable no-console, max-len */
|
|
export function deprecate(oldMethod, newMethod, version = '1.19') {
|
|
console.warn(`DEPRECATION WARNING: you are using deprecated ${oldMethod}, it will be removed in Foreman ${
|
|
version
|
|
}. Use ${newMethod} instead.`);
|
|
}
|
|
|
|
export function initTypeAheadSelect(input) {
|
|
input.select2({
|
|
ajax: {
|
|
url: input.data('url'),
|
|
dataType: 'json',
|
|
quietMillis: 250,
|
|
data: (term, page) => ({
|
|
q: term,
|
|
scope: input.data('scope'),
|
|
}),
|
|
results: data => ({ results: data.map(({ id, name }) => ({ id, text: name })) }),
|
|
cache: true,
|
|
},
|
|
initSelection(element, callback) {
|
|
$.ajax(input.data('url'), {
|
|
data: {
|
|
scope: input.data('scope'),
|
|
},
|
|
dataType: 'json',
|
|
}).done((data) => {
|
|
if (data.length > 0) {
|
|
callback({ id: data[0].id, text: data[0].name });
|
|
}
|
|
});
|
|
},
|
|
width: '400px',
|
|
});
|
|
}
|
|
|
|
// handle table updates via turoblinks
|
|
export function updateTable(element) {
|
|
const uri = new URI(window.location.href);
|
|
|
|
const values = { };
|
|
|
|
|
|
if (['per_page', 'search-form'].includes(element.id)) {
|
|
values.page = '1';
|
|
} else {
|
|
values.page = $('#cur_page_num').val();
|
|
}
|
|
|
|
const searchTerm = $(element).find('.autocomplete-input').val();
|
|
if (searchTerm !== undefined) {
|
|
values.search = searchTerm.trim();
|
|
}
|
|
values.per_page = $('#per_page').val();
|
|
uri.setSearch(values);
|
|
|
|
/* eslint-disable no-undef */
|
|
Turbolinks.visit(uri.toString());
|
|
return false;
|
|
}
|