-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.js
executable file
·137 lines (119 loc) · 3.45 KB
/
helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* Build and absolute url to some path
*
* @param stringUrl
* @param afterUrl
* @returns {string}
*/
window.url = function (stringUrl, afterUrl = null) {
let baseUrl = document.head.querySelector('meta[name="base-url"]').content;
let url = baseUrl + '/' + stringUrl;
if (afterUrl === null) {
return url;
}
afterUrl = afterUrl.toString();
if (afterUrl.startsWith('/') === false) {
afterUrl = `/${ afterUrl }`;
}
return url + afterUrl;
};
/**
* Get csrf-token value present on meta header
*
* @returns string
*/
window.token = function () {
return document.head.querySelector('meta[name="csrf-token"]').content;
};
/**
* Get csrf-token with name and value object
*
* @returns {{"X-CSRF-TOKEN": string}}
*/
window.tokenWithKey = function () {
return {
'X-CSRF-TOKEN': token()
};
};
window.lockScreen = function () {
const loader = document.getElementsByClassName('js-loader')[0];
if (loader) {
loader.style.display = 'flex';
}
};
window.unlockScreen = function () {
const loader = document.getElementsByClassName('js-loader')[0];
if (loader) {
loader.style.display = 'none';
}
};
window.onload = function () {
unlockScreen();
};
/**
* The base config parameter to start datatable
*
* @param config
* @returns {{}}
*/
window.dataTable = function (config) {
const base = {
processing: true,
serverSide: true,
ajax: {
headers: config.ajax.headers,
url: config.ajax.url,
type: 'get',
data: config.ajax.data,
dataType: 'json',
success: config.ajax.success,
error: config.ajax.error || function (err) {
console.log(err);
}
},
columns: config.columns,
columnDefs: config.columnDefs,
responsive: true,
pagingType: 'full_numbers',
language: {
"sProcessing": "Procesando...",
"sLengthMenu": "Mostrar _MENU_ registros",
"sZeroRecords": "No se encontraron resultados",
"sEmptyTable": "Ningún dato disponible en esta tabla",
"sInfo": "Mostrando registros del _START_ al _END_ de un total de _TOTAL_ registros",
"sInfoEmpty": "Mostrando registros del 0 al 0 de un total de 0 registros",
"sInfoFiltered": "(filtrado de un total de _MAX_ registros)",
"sInfoPostFix": "",
"sSearch": "Buscar:",
"sUrl": "",
"sInfoThousands": ",",
"sLoadingRecords": "Cargando...",
"oPaginate": {
"sFirst": "Primero",
"sLast": "Último",
"sNext": "Siguiente",
"sPrevious": "Anterior"
},
"oAria": {
"sSortAscending": ": Activar para ordenar la columna de manera ascendente",
"sSortDescending": ": Activar para ordenar la columna de manera descendente"
}
}
};
return { ...config, ...base };
};
// Confirm submit
document.addEventListener('submit', function (event) {
const form = event.target;
if (form.dataset.lock !== 'false') {
lockScreen();
return;
}
if (form.dataset.confirm === 'true') {
let question = form.dataset.question || '¿Está seguro de continuar?';
let confirmed = confirm(question);
if (!confirmed) {
event.preventDefault();
}
}
});