@@ -31,6 +31,7 @@ const CLASSHELPER_POPUP_URL = "about:blank";
3131const CLASSHELPER_POPUP_TARGET = "_blank" ;
3232
3333const CLASSHELPER_TABLE_SELECTION_NONE = "table-selection-none" ;
34+ const CLASSHELPER_TRANSLATION_KEYWORDS = [ "apply" , "cancel" , "next" , "prev" , "search" , "reset" ] ;
3435
3536const ALTERNATIVE_DROPDOWN_PATHNAMES = {
3637 "roles" : "/rest/roles"
@@ -149,12 +150,6 @@ class ClassHelper extends HTMLElement {
149150
150151 const initialRequestURL = ClassHelper . getRestURL ( this . trackerBaseURL , this . helpurlProps ) ;
151152
152- ClassHelper . fetchTranslations ( )
153- . catch ( error => {
154- console . warn ( "Classhelper failed in translating." )
155- console . error ( error ) ;
156- } ) ;
157-
158153 this . fetchDropdownsData ( )
159154 . catch ( error => {
160155 // Top level handling for dropdowns errors.
@@ -280,20 +275,39 @@ class ClassHelper extends HTMLElement {
280275 return ;
281276 }
282277
283- let translations = {
284- "Apply" : "Apply" ,
285- "Cancel" : "Cancel" ,
286- "Next" : "Next" ,
287- "Prev" : "Prev" ,
288- "Search" : "Search" ,
289- "Reset" : "Reset"
290- } ;
291- ClassHelper . translations = translations ;
278+ const keys = new Set ( ) ;
279+
280+ const classhelpers = document . getElementsByTagName ( CLASSHELPER_TAG_NAME ) ;
281+ for ( let classhelper of classhelpers ) {
282+ if ( classhelper . dataset . searchWith ) {
283+ classhelper . dataset . searchWith
284+ . split ( ',' )
285+ . forEach ( param => {
286+ keys . add ( param . split ( "[]" ) [ 0 ] ) ;
287+ } ) ;
288+ }
289+
290+ const a = classhelper . querySelector ( "a" ) ;
291+ if ( a && a . dataset . helpurl ) {
292+ let searchParams = new URLSearchParams ( a . dataset . helpurl . split ( "?" ) [ 1 ] ) ;
293+ let properties = searchParams . get ( "properties" ) ;
294+ if ( properties ) {
295+ properties . split ( ',' ) . forEach ( key => keys . add ( key ) ) ;
296+ }
297+ }
298+ }
299+
300+ CLASSHELPER_TRANSLATION_KEYWORDS . forEach ( key => keys . add ( key ) ) ;
301+
302+ ClassHelper . translations = { } ;
303+ for ( let key of keys ) {
304+ ClassHelper . translations [ key ] = key ;
305+ }
292306
293307 let tracker = window . location . pathname . split ( '/' ) [ 1 ] ;
294308 let url = new URL ( window . location . origin + "/" + tracker ) ;
295309 url . searchParams . append ( "@template" , "translation" ) ;
296- url . searchParams . append ( "properties" , Object . keys ( translations ) . join ( ',' ) ) ;
310+ url . searchParams . append ( "properties" , Array . from ( keys . values ( ) ) . join ( ',' ) ) ;
297311
298312 let resp , json ;
299313
@@ -321,7 +335,9 @@ class ClassHelper extends HTMLElement {
321335 throw new Error ( message ) ;
322336 }
323337
324- ClassHelper . translations = json ;
338+ for ( let entry of Object . entries ( json ) ) {
339+ ClassHelper . translations [ entry [ 0 ] ] = entry [ 1 ] ;
340+ }
325341 }
326342
327343 async fetchDropdownsData ( ) {
@@ -547,7 +563,7 @@ class ClassHelper extends HTMLElement {
547563 const label = document . createElement ( "label" ) ;
548564 label . classList . add ( "search-label" ) ; // Add class for styling
549565 label . setAttribute ( "for" , param ) ;
550- label . textContent = param + ":" ;
566+ label . textContent = ClassHelper . translations [ param ] + ":" ;
551567
552568 let input ;
553569 if ( this . dropdownsData [ param ] ) {
@@ -602,7 +618,7 @@ class ClassHelper extends HTMLElement {
602618 buttonCell . colSpan = 1 ;
603619
604620 const search = document . createElement ( "button" ) ;
605- search . textContent = ClassHelper . translations [ "Search " ] ;
621+ search . textContent = ClassHelper . translations [ "search " ] ;
606622 search . classList . add ( "search-button" ) ; // Add class for styling
607623 search . addEventListener ( "click" , ( e ) => {
608624 e . preventDefault ( ) ;
@@ -615,7 +631,7 @@ class ClassHelper extends HTMLElement {
615631 } ) ;
616632
617633 const reset = document . createElement ( "button" ) ;
618- reset . textContent = ClassHelper . translations [ "Reset " ] ;
634+ reset . textContent = ClassHelper . translations [ "reset " ] ;
619635 reset . classList . add ( "reset-button" ) ; // Add class for styling
620636 reset . addEventListener ( "click" , ( e ) => {
621637 e . preventDefault ( ) ;
@@ -665,7 +681,7 @@ class ClassHelper extends HTMLElement {
665681
666682 const prev = document . createElement ( "button" ) ;
667683 prev . innerHTML = "<" ;
668- prev . setAttribute ( "aria-label" , ClassHelper . translations [ "Prev " ] ) ;
684+ prev . setAttribute ( "aria-label" , ClassHelper . translations [ "prev " ] ) ;
669685 prev . setAttribute ( "disabled" , "disabled" ) ;
670686 if ( prevUrl ) {
671687 prev . removeAttribute ( "disabled" ) ;
@@ -680,7 +696,7 @@ class ClassHelper extends HTMLElement {
680696
681697 const next = document . createElement ( "button" ) ;
682698 next . innerHTML = ">" ;
683- next . setAttribute ( "aria-label" , ClassHelper . translations [ "Next " ] ) ;
699+ next . setAttribute ( "aria-label" , ClassHelper . translations [ "next " ] ) ;
684700 next . setAttribute ( "disabled" , "disabled" ) ;
685701 if ( nextUrl ) {
686702 next . removeAttribute ( "disabled" ) ;
@@ -714,7 +730,7 @@ class ClassHelper extends HTMLElement {
714730 }
715731
716732 const cancel = document . createElement ( "button" ) ;
717- cancel . textContent = ClassHelper . translations [ "Cancel " ] ;
733+ cancel . textContent = ClassHelper . translations [ "cancel " ] ;
718734 cancel . addEventListener ( "click" , ( ) => {
719735 this . dispatchEvent ( new CustomEvent ( "valueSelected" , {
720736 detail : {
@@ -726,7 +742,7 @@ class ClassHelper extends HTMLElement {
726742 const apply = document . createElement ( "button" ) ;
727743 apply . id = "popup-apply" ;
728744 apply . classList . add ( "popup-apply" ) ;
729- apply . textContent = ClassHelper . translations [ "Apply " ] ;
745+ apply . textContent = ClassHelper . translations [ "apply " ] ;
730746 apply . addEventListener ( "click" , ( ) => {
731747 this . dispatchEvent ( new CustomEvent ( "valueSelected" , {
732748 detail : {
@@ -774,7 +790,7 @@ class ClassHelper extends HTMLElement {
774790
775791 headers . forEach ( header => {
776792 const th = document . createElement ( 'th' ) ;
777- th . textContent = header ;
793+ th . textContent = ClassHelper . translations [ header ] ;
778794 headerRow . appendChild ( th ) ;
779795 } ) ;
780796 thead . appendChild ( headerRow ) ;
@@ -830,19 +846,7 @@ class ClassHelper extends HTMLElement {
830846 }
831847
832848 // Create table footer with the same column values as headers
833- const footerRow = document . createElement ( 'tr' ) ;
834-
835- if ( includeCheckbox ) {
836- let footThx = document . createElement ( "th" ) ;
837- footThx . textContent = "X" ;
838- footerRow . appendChild ( footThx ) ;
839- }
840-
841- headers . forEach ( header => {
842- const th = document . createElement ( 'th' ) ;
843- th . textContent = header ;
844- footerRow . appendChild ( th ) ;
845- } ) ;
849+ const footerRow = headerRow . cloneNode ( true ) ;
846850 tfoot . appendChild ( footerRow ) ;
847851
848852 // Assemble the table
@@ -1268,7 +1272,7 @@ function enableClassHelper() {
12681272 return ;
12691273 }
12701274
1271- /**@todo - make api call? get 404 then early return? */
1275+ /** make api call if error then do not register */
12721276 // http://localhost/demo/rest
12731277
12741278 fetch ( "rest" )
@@ -1279,6 +1283,11 @@ function enableClassHelper() {
12791283 return ;
12801284 }
12811285 customElements . define ( CLASSHELPER_TAG_NAME , ClassHelper ) ;
1286+ ClassHelper . fetchTranslations ( )
1287+ . catch ( error => {
1288+ console . warn ( "Classhelper failed in translating." )
1289+ console . error ( error ) ;
1290+ } ) ;
12821291 } ) . catch ( err => {
12831292 console . error ( err ) ;
12841293 } ) ;
0 commit comments