Skip to content

Commit eb853b6

Browse files
committed
minor refactoring
1 parent 1b7b1c1 commit eb853b6

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

edoc/resources/scripts/function.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* https://github.com/dariok/wdbplus
44
*/
55
/* jshint browser: true */
6+
/* globals Cookies */
67
"use strict";
78

89
const wdb = (function() {
@@ -11,6 +12,9 @@ const wdb = (function() {
1112
for (let m of document.getElementsByTagName("meta")) {
1213
meta[m.name] = m.content;
1314
}
15+
16+
// will be used to store headers
17+
let restHeaderVal = { };
1418

1519
// parsed query parameters; URLSearchParams is not supported by Edge < 17 and IE
1620
/* TODO https://github.com/dariok/wdbplus/issues/429
@@ -24,15 +28,24 @@ const wdb = (function() {
2428
// unique IDs
2529
let internalUniqueId = 0; // basis for globally unique IDs
2630
let getUniqueId = function () {
27-
return 'wdb' + ('000' + internalUniqueId++).substr(-4);
31+
return 'wdb' + ('000' + internalUniqueId++).substring(-4);
32+
};
33+
34+
function setAuthorizationHeader () {
35+
let cred = Cookies.get("wdbplus");
36+
if ( typeof cred === "undefined" || cred.length === 0 ) {
37+
delete restHeaderVal.Authorization;
38+
} else {
39+
restHeaderVal.Authorization = "Basic " + cred;
40+
}
2841
};
2942

3043
/* Login and logout */
31-
let login = function (event, reload) {
44+
let login = function ( event, reload ) {
3245
event.preventDefault();
3346

34-
let username = $('#user').val(),
35-
password = $('#password').val();
47+
let username = $('#user').val()
48+
, password = $('#password').val();
3649
wdb.report("info", "login request");
3750
Cookies.remove('wdbplus');
3851

@@ -56,7 +69,7 @@ const wdb = (function() {
5669
if ( reload ) {
5770
location.reload();
5871
}
59-
} catch (e) {
72+
} catch ( e ) {
6073
wdb.report("error", "error logging in", e);
6174
}
6275
},
@@ -95,15 +108,6 @@ const wdb = (function() {
95108
/* globals Cookies */
96109
/* TODO when modules are available, import js.cookie.mjs via CDN; current support 90.5% */
97110
// function to set REST headers
98-
let restHeaderVal = { };
99-
let setAuthorizationHeader = function () {
100-
let cred = Cookies.get("wdbplus");
101-
if ( typeof cred == "undefined" || cred.length == 0 ) {
102-
delete restHeaderVal.Authorization;
103-
} else {
104-
restHeaderVal.Authorization = "Basic " + cred;
105-
}
106-
};
107111
setAuthorizationHeader();
108112

109113
return {
@@ -369,8 +373,7 @@ const wdbDocument = {
369373
// generic laoding function
370374
loadContent: function ( url, target, me ) {
371375
if ($('#' + target).css('display') == 'none') {
372-
$.ajax(
373-
{
376+
$.ajax({
374377
url: url,
375378
headers: wdb.restHeaders,
376379
dataType: 'html',
@@ -382,8 +385,7 @@ const wdbDocument = {
382385
error: function (xhr, status, error) {
383386
wdb.report("error", "Error loading " + url + " : " + status, error);
384387
}
385-
}
386-
);
388+
});
387389
} else {
388390
$('#' + target).slideToggle();
389391
if (me.length > 0) {

0 commit comments

Comments
 (0)