@@ -76,7 +76,7 @@ var cb = {
7676
7777 // (B4) CHANGE "LOCAL" PAGE
7878 // num : int, page number (1 to 5)
79- page : num => { for ( let i in cb . hPages ) {
79+ page : num => { num -- ; for ( let i in cb . hPages ) {
8080 if ( i == num ) { cb . hPages [ i ] . classList . remove ( "d-none" ) ; }
8181 else { cb . hPages [ i ] . classList . add ( "d-none" ) ; }
8282 } } ,
@@ -88,15 +88,13 @@ var cb = {
8888 // url : string, target URL
8989 // data : optional object, data to send
9090 // loading : boolean, show "now loading" screen? default true.
91- // debug : boolean, debug mode. default false.
9291 // onpass : function, run this function on server response
9392 // onerr : optional function, run this function on error
9493 ajax : opt => {
9594 // (C1) CHECKS & DEFAULTS
9695 if ( opt . url === undefined ) { cb . modal ( "AJAX ERROR" , "Target URL is not set!" ) ; return false ; }
9796 if ( opt . onpass === undefined ) { cb . modal ( "AJAX ERROR" , "Function to call on onpass is not set!" ) ; return false ; }
9897 if ( opt . loading === undefined ) { opt . loading = true ; }
99- if ( opt . debug === undefined ) { opt . debug = false ; }
10098
10199 // (C2) DATA TO SEND
102100 var data = new FormData ( ) ;
@@ -105,19 +103,16 @@ var cb = {
105103 // (C3) AJAX REQUEST
106104 if ( opt . loading ) { cb . loading ( true ) ; }
107105 fetch ( opt . url , { method :"POST" , credentials :"include" , body :data } )
108- . then ( res => {
109- if ( opt . debug ) { console . log ( res ) ; }
106+ . then ( async res => {
110107 if ( res . status == 200 ) { return res . text ( ) ; }
111108 else {
109+ console . error ( await res . text ( ) ) ;
112110 cb . modal ( "SERVER ERROR" , "Bad server response - " + res . status ) ;
113- console . error ( res . status , res ) ;
114111 if ( opt . onerr ) { opt . onerr ( ) ; }
112+ throw new Error ( "Bad server response" ) ;
115113 }
116114 } )
117- . then ( txt => {
118- if ( opt . debug ) { console . log ( txt ) ; }
119- opt . onpass ( txt ) ;
120- } )
115+ . then ( txt => opt . onpass ( txt ) )
121116 . catch ( err => {
122117 cb . modal ( "AJAX ERROR" , err . message ) ;
123118 console . error ( err ) ;
@@ -130,10 +125,9 @@ var cb = {
130125
131126 // (D) AJAX API CALL
132127 // mod : string, module to call
133- // req : string, request
128+ // act : string, action to perform
134129 // data : object, data to send
135130 // loading : boolean, show loading screen?
136- // debug : boolean, optional debug mode. default false.
137131 // passmsg : boolean false to supress toast "success message".
138132 // boolean true to use server response message.
139133 // string to override "OK" message.
@@ -144,34 +138,33 @@ var cb = {
144138 api : opt => {
145139 // (D1) INIT OPTIONS
146140 var options = { } ;
147- options . url = `${ cbhost . api } ${ opt . mod } /${ opt . req } /` ;
141+ options . url = `${ cbhost . api } ${ opt . mod } /${ opt . act } /` ;
148142 if ( opt . data ) { options . data = opt . data ; }
149143 if ( opt . loading != undefined ) { options . loading = opt . loading ; }
150- if ( opt . debug != undefined ) { options . debug = opt . debug ; }
151144 if ( opt . onerr ) { options . onerr = opt . onerr ; }
152145 if ( opt . passmsg === undefined ) { opt . passmsg = "OK" ; }
153146 if ( opt . nofail === undefined ) { opt . nofail = false ; }
154147
155148 // (D2) ON AJAX LOAD
156149 options . onpass = res => {
157150 // (D2-1) PARSE RESULTS
158- try { var res = JSON . parse ( res ) ; }
151+ try { var r = JSON . parse ( res ) ; }
159152 catch ( err ) {
160153 console . error ( res ) ;
161154 cb . modal ( "AJAX ERROR" , "Failed to parse JSON data." ) ;
162155 return false ;
163156 }
164157
165158 // (D2-2) RESULTS FEEBACK
166- if ( res . status == "E" ) { location . href = cbhost . base + "login/" ; }
167- else if ( res . status ) {
159+ if ( r . status == "E" ) { location . href = cbhost . base + "login/" ; }
160+ else if ( r . status ) {
168161 if ( opt . passmsg !== false ) {
169- cb . toast ( 1 , "Success" , opt . passmsg === true ? res . message : opt . passmsg ) ;
162+ cb . toast ( 1 , "Success" , opt . passmsg === true ? r . message : opt . passmsg ) ;
170163 }
171- if ( opt . onpass ) { opt . onpass ( res ) ; }
164+ if ( opt . onpass ) { opt . onpass ( r ) ; }
172165 } else {
173- if ( ! opt . nofail ) { cb . modal ( "ERROR" , res . message ) ; }
174- if ( opt . onfail ) { opt . onfail ( res . message ) ; }
166+ if ( ! opt . nofail ) { cb . modal ( "ERROR" , r . message ) ; }
167+ if ( opt . onfail ) { opt . onfail ( r . message ) ; }
175168 }
176169 } ;
177170
@@ -184,23 +177,21 @@ var cb = {
184177 // target : string, ID of target HTML element
185178 // data : object, data to send
186179 // loading : boolean, show loading screen? Default false.
187- // debug : boolean, optional debug mode. default false.
188180 // onload : optional function, do this on loaded
189181 // onerr : optional function, do this on ajax error
190182 load : opt => {
191183 // (E1) INIT OPTIONS
192184 var options = { } ;
193185 options . url = `${ cbhost . base } ${ opt . page } /` ;
194186 if ( opt . loading != undefined ) { options . loading = opt . loading ; }
195- if ( opt . debug != undefined ) { options . debug = opt . debug ; }
196187 if ( opt . onerr ) { options . onerr = opt . onerr ; }
197188 if ( opt . data ) {
198189 opt . data [ "ajax" ] = 1 ;
199190 options . data = opt . data ;
200191 } else { options . data = { "ajax" : 1 } ; }
201192
202193 // (E2) ON AJAX LOAD
203- options . onpass = ( res ) => {
194+ options . onpass = res => {
204195 if ( res == "E" ) { location . href = cbhost . base + "login/" ; }
205196 else {
206197 document . getElementById ( opt . target ) . innerHTML = res ;
@@ -214,7 +205,7 @@ var cb = {
214205
215206 // (F) SIGN OFF
216207 bye : ( ) => cb . modal ( "Please Confirm" , "Sign off?" , ( ) => cb . api ( {
217- mod : "session" , req : "logout" , passmsg : false ,
208+ mod : "session" , act : "logout" , passmsg : false ,
218209 onpass : ( ) => location . href = cbhost . base + "login/"
219210 } ) ) ,
220211
0 commit comments