7
async pushUUID(model, uuid) {
8
let modelList = this.temp[model]
11
this.temp[model].push(uuid)
13
if (!this.temp[model].includes(uuid)) {
14
this.temp[model].push(uuid)
19
console.log('Бесконечно идем в цикл');
22
for (let model in this.temp) {
23
if (this.temp[model].length > 0) {
24
ids_str = this.temp[model].join(',')
25
const items = fetch('/base/get_by_ids?model=' + model + '&id__in=' + encodeURIComponent(ids_str));
32
for (let i = 0; i < promises.length; i++) {
33
const item = await promises[i]
34
const results = await item.json();
35
for (let i = 0; i < results.length; i++) {
36
this.results[results[i].value] = results[i].label
39
await new Promise(r => setTimeout(r, 300));
40
if (this.results.length > 2000) {
41
this.results.splice(0, 100);
48
Object.freeze(Singleton);
52
function getCookieValue(name) {
53
const nameString = name + "="
55
const values = document.cookie.split(";").filter(item => {
56
return item.includes(nameString)
60
for (let val in values) {
61
let is_value = values[val].split('=').filter(item => {
62
return item.replace(' ', '') === name
64
if (is_value.length) {
70
return value.substring(nameString.length, value.length).replace('=', '')
77
function removeCookie(sKey, sPath, sDomain) {
78
document.cookie = encodeURIComponent(sKey) +
79
"=; expires=Thu, 01 Jan 1970 00:00:00 GMT" +
80
(sDomain ? "; domain=" + sDomain : "") +
81
(sPath ? "; path=" + sPath : "/");
84
function parseJwt(token) {
85
var base64Url = token.split('.')[1];
86
var base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
87
var jsonPayload = decodeURIComponent(window.atob(base64).split('').map(function (c) {
88
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
91
return JSON.parse(jsonPayload);
100
htmx.on("htmx:confirm", (e) => {
101
let authToken = getCookieValue('token');
103
if (authToken == null && !window.location.pathname.includes('login')) {
106
window.history.pushState('Login', 'Login', '/basic/user/login');
107
console.log('redirect login')
108
document.location.replace('/basic/user/login')
113
async function refreshToken() {
115
token: getCookieValue('token').replace('=', ''),
116
refresh_token: getCookieValue('refresh_token').replace('=', '')
118
console.log('refreshing token')
119
let response = await fetch('/basic/user/refresh', {
122
'Content-Type': 'application/json'
124
body: JSON.stringify(user)
126
let result = await response.json();
127
const parsed = parseJwt(result.token)
128
const expires = await new Date(parsed.exp * 1000)
129
document.cookie = "token=" + result.token + "; expires=" + expires + ";path=/;"
130
document.cookie = "refresh_token=" + result.token + "; expires=" + expires + ";path=/;"
135
async function checkAuth() {
136
var authToken = getCookieValue('token')
138
authToken = getCookieValue('refresh_token')
140
authToken = refreshToken()
141
authToken = getCookieValue('token')
147
var tokenData = parseJwt(authToken)
148
var now = Date.now() / 1000
149
if (tokenData.exp <= now) {
156
htmx.on("htmx:configRequest", (e) => {
159
console.log('check cookie')
160
const res = checkAuth()
161
e.detail.headers["Authorization"] = getCookieValue('token')
163
htmx.on("htmx:afterRequest", (e) => {
166
if (authToken == null) {
168
if (e.detail.xhr.status || 200) {
169
e.detail.shouldSwap = true;
170
e.detail.isError = false;
174
htmx.on('htmx:beforeSwap', function (evt) {
176
if (evt.detail.xhr.status === 200) {
178
} else if (evt.detail.xhr.status === 404) {
180
var myToast = Toastify({
181
text: evt.detail.pathInfo.requestPath + ' + ' + evt.detail.xhr.responseText,
185
background: "#e94e1d",
190
} else if (evt.detail.xhr.status === 403) {
193
var message = evt.detail.xhr.responseText.replace(/\\n/g, '\n')
194
var myToast = Toastify({
202
} else if (evt.detail.xhr.status === 401 && !window.location.pathname.includes('login')) {
205
window.history.pushState('Login', 'Login', '/basic/user/login' + "?next=" + window.location.pathname);
206
htmx.ajax('GET', '/basic/user/login', {
207
target: '#htmx_content', headers: {
208
'HX-Replace-Url': 'true'
211
} else if (evt.detail.xhr.status === 418) {
214
evt.detail.shouldSwap = true;
215
evt.detail.target = htmx.find("#teapot");
218
var myToast = Toastify({
219
text: evt.detail.xhr.responseText,
223
background: "#e94e1d",
235
htmx.on('htmx:wsConfigReceive', (e) => {
236
console.log('wsConfigReceive')
238
e.detail.headers["Authorization"] = getCookieValue('token')
240
htmx.on('htmx:wsConfigSend', (e) => {
241
console.log('wsConfigSend')
243
e.detail.headers["Authorization"] = getCookieValue('token')