3
3
* https://github.com/dariok/wdbplus
4
4
*/
5
5
/* jshint browser: true */
6
+ /* globals Cookies */
6
7
"use strict" ;
7
8
8
9
const wdb = ( function ( ) {
@@ -11,6 +12,9 @@ const wdb = (function() {
11
12
for ( let m of document . getElementsByTagName ( "meta" ) ) {
12
13
meta [ m . name ] = m . content ;
13
14
}
15
+
16
+ // will be used to store headers
17
+ let restHeaderVal = { } ;
14
18
15
19
// parsed query parameters; URLSearchParams is not supported by Edge < 17 and IE
16
20
/* TODO https://github.com/dariok/wdbplus/issues/429
@@ -24,15 +28,24 @@ const wdb = (function() {
24
28
// unique IDs
25
29
let internalUniqueId = 0 ; // basis for globally unique IDs
26
30
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
+ }
28
41
} ;
29
42
30
43
/* Login and logout */
31
- let login = function ( event , reload ) {
44
+ let login = function ( event , reload ) {
32
45
event . preventDefault ( ) ;
33
46
34
- let username = $ ( '#user' ) . val ( ) ,
35
- password = $ ( '#password' ) . val ( ) ;
47
+ let username = $ ( '#user' ) . val ( )
48
+ , password = $ ( '#password' ) . val ( ) ;
36
49
wdb . report ( "info" , "login request" ) ;
37
50
Cookies . remove ( 'wdbplus' ) ;
38
51
@@ -56,7 +69,7 @@ const wdb = (function() {
56
69
if ( reload ) {
57
70
location . reload ( ) ;
58
71
}
59
- } catch ( e ) {
72
+ } catch ( e ) {
60
73
wdb . report ( "error" , "error logging in" , e ) ;
61
74
}
62
75
} ,
@@ -95,15 +108,6 @@ const wdb = (function() {
95
108
/* globals Cookies */
96
109
/* TODO when modules are available, import js.cookie.mjs via CDN; current support 90.5% */
97
110
// 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
- } ;
107
111
setAuthorizationHeader ( ) ;
108
112
109
113
return {
@@ -369,8 +373,7 @@ const wdbDocument = {
369
373
// generic laoding function
370
374
loadContent : function ( url , target , me ) {
371
375
if ( $ ( '#' + target ) . css ( 'display' ) == 'none' ) {
372
- $ . ajax (
373
- {
376
+ $ . ajax ( {
374
377
url : url ,
375
378
headers : wdb . restHeaders ,
376
379
dataType : 'html' ,
@@ -382,8 +385,7 @@ const wdbDocument = {
382
385
error : function ( xhr , status , error ) {
383
386
wdb . report ( "error" , "Error loading " + url + " : " + status , error ) ;
384
387
}
385
- }
386
- ) ;
388
+ } ) ;
387
389
} else {
388
390
$ ( '#' + target ) . slideToggle ( ) ;
389
391
if ( me . length > 0 ) {
0 commit comments