@@ -68,15 +68,15 @@ class ClassHelper extends HTMLElement {
6868 this . addEventListener ( "click" , openPopUpClosure ) ;
6969
7070 const nextPageClosure = ( event ) => {
71- this . pageChange ( properties , properties ) . catch ( error => {
71+ this . pageChange ( event . detail . value , properties ) . catch ( error => {
7272 // Top level error handling for nextPage method.
7373 this . removeEventListener ( "click" , nextPageClosure ) ;
7474 } ) ;
7575 }
7676 this . addEventListener ( "nextPage" , nextPageClosure ) ;
7777
7878 const prevPageClosure = ( event ) => {
79- this . pageChange ( properties , properties ) . catch ( error => {
79+ this . pageChange ( event . detail . value , properties ) . catch ( error => {
8080 // Top level error handling for prevPage method.
8181 this . removeEventListener ( "click" , prevPageClosure ) ;
8282 } ) ;
@@ -556,26 +556,14 @@ class ClassHelper extends HTMLElement {
556556 */
557557 async openPopUp ( apiURL , props ) {
558558 // Find preselected values
559- const input = document . getElementsByName ( props . formProperty ) [ 0 ] ;
559+ const input = document . getElementsByName ( props . formProperty ) . item ( 0 ) ;
560560 let preSelectedValues = [ ] ;
561561 if ( input . value ) {
562562 preSelectedValues = input . value . split ( ',' ) ;
563563 }
564564
565565 const popupFeatures = `popup=yes,width=${ props . width } ,height=${ props . height } ` ;
566- const popupWindow = window . open ( "about:blank" , "_blank" , popupFeatures ) ;
567- this . popupRef = popupWindow ;
568-
569- // This does not create a closure as this event is only triggered once.
570- popupWindow . addEventListener ( "load" , ( event ) => {
571- /** @type {Document } */
572- const doc = event . target ;
573- const css = doc . createElement ( "link" ) ;
574- css . rel = "stylesheet" ;
575- css . type = "text/css" ;
576- css . href = props . origin + '/' + props . tracker + '/' + "@@file/classhelper.css" ;
577- doc . head . appendChild ( css ) ;
578- } ) ;
566+ this . popupRef = window . open ( "about:blank" , "_blank" , popupFeatures ) ;
579567
580568 let resp ;
581569 try {
@@ -594,40 +582,52 @@ class ClassHelper extends HTMLElement {
594582 }
595583
596584 const data = json . data ;
597- let prevURL = data [ "@links" ] . prev ;
598- if ( prevURL ) {
599- prevURL = prevURL [ 0 ] . uri ;
600- }
585+ const links = json . data [ "@links" ] ;
586+
587+ let prevPageURL , nextPageURL ;
601588
602- let nextURL = data [ "@links" ] . next ;
603- if ( nextURL ) {
604- nextURL = nextURL [ 0 ] . uri ;
589+ if ( links . prev && links . prev . length > 0 ) {
590+ prevPageURL = links . prev [ 0 ] . uri ;
605591 }
592+ if ( links . next && links . next . length > 0 ) {
593+ nextPageURL = links . next [ 0 ] . uri ;
594+ }
595+
596+ const popupDocument = this . popupRef . document ;
597+ const popupBody = popupDocument . body ;
598+ const popupHead = popupDocument . head ;
606599
607- const container = document . createElement ( "div" ) ;
608- container . setAttribute ( "class" , "flexcontainer" ) ;
600+ // Add external classhelper css to head
601+ const css = popupDocument . createElement ( "link" ) ;
602+ css . rel = "stylesheet" ;
603+ css . type = "text/css" ;
604+ css . href = props . origin + '/' + props . tracker + '/' + "@@file/classhelper.css" ;
605+ popupHead . appendChild ( css ) ;
606+
607+ popupBody . classList . add ( "flex-container" ) ;
609608
610- const b = this . popupRef . document . body ;
611609 if ( this . getAttribute ( "searchWith" ) ) {
612- container . appendChild ( this . getSearchFragment ( ) ) ;
610+ const searchFrag = this . getSearchFragment ( ) ;
611+ popupBody . appendChild ( searchFrag ) ;
613612 }
614- container . appendChild ( this . getPaginationFragment ( prevURL , nextURL , props . pageIndex , props . pageSize ) ) ;
615613
616- const tableFragment = this . getTableFragment ( props . fields , data . collection , preSelectedValues ) ;
617- const popupTable = document . createElement ( "div" ) ;
618- popupTable . appendChild ( tableFragment ) ;
619- popupTable . setAttribute ( "class" , "popup-table" ) ;
620- container . appendChild ( popupTable ) ;
614+ const paginationFrag = this . getPaginationFragment ( prevPageURL , nextPageURL , props . pageIndex , props . pageSize ) ;
615+ popupBody . appendChild ( paginationFrag ) ;
616+
617+ const tableFrag = this . getTableFragment ( props . fields , data . collection , preSelectedValues ) ;
618+ popupBody . appendChild ( tableFrag ) ;
619+
621620 const separator = document . createElement ( "div" ) ;
622- separator . setAttribute ( "class" , "separator" ) ;
623- container . appendChild ( separator ) ;
624- container . appendChild ( this . getAccumulatorFragment ( preSelectedValues ) ) ;
625- b . appendChild ( container ) ;
621+ separator . classList . add ( "separator" ) ;
622+ popupBody . appendChild ( separator ) ;
623+
624+ const accumulatorFrag = this . getAccumulatorFragment ( preSelectedValues ) ;
625+ popupBody . appendChild ( accumulatorFrag ) ;
626626 }
627627
628628 /** method when next or previous button is clicked */
629629 async pageChange ( apiURL , props ) {
630- let preSelectedValues = this . popupRef . document . getElementById ( "popup-preview" ) . value . split ( "," ) ;
630+ let preSelectedValues = this . popupRef . document . getElementsByClassName ( "popup-preview" ) . item ( 0 ) . value . split ( "," ) ;
631631
632632 fetch ( apiURL ) . then ( resp => resp . json ( ) ) . then ( ( { data } ) => {
633633 const b = this . popupRef . document . body ;
@@ -642,9 +642,9 @@ class ClassHelper extends HTMLElement {
642642 let selfUrl = new URL ( data [ "@links" ] . self [ 0 ] . uri ) ;
643643 props . pageIndex = selfUrl . searchParams . get ( "@page_index" ) ;
644644
645- let oldPagination = this . popupRef . document . getElementById ( "popup-pagination" ) ;
645+ let oldPagination = this . popupRef . document . getElementsByClassName ( "popup-pagination" ) . item ( 0 ) ;
646646 oldPagination . parentElement . replaceChild ( this . getPaginationFragment ( prevURL , nextURL , props . pageIndex , props . pageSize ) , oldPagination ) ;
647- let oldTable = this . popupRef . document . getElementById ( "popup-table" ) ;
647+ let oldTable = this . popupRef . document . getElementsByClassName ( "popup-table" ) . item ( 0 ) ;
648648 let ancestor = oldTable . parentElement . parentElement ;
649649 ancestor . replaceChild ( this . getTableFragment ( props . fields , data . collection , preSelectedValues ) , oldTable . parentElement ) ;
650650 } ) ;
0 commit comments