@@ -13,7 +13,6 @@ export interface ITemplateStrategy {
1313 removeBufferElements ( element : Element , topBuffer : Element , bottomBuffer : Element ) : void ;
1414 getFirstElement ( topBuffer : Element ) : Element ;
1515 getLastElement ( bottomBuffer : Element ) : Element ;
16- getLastView ( bottomBuffer : Element ) : Element ;
1716 getTopBufferDistance ( topBuffer : Element ) : number ;
1817}
1918
@@ -27,6 +26,9 @@ export class TemplateStrategyLocator {
2726 this . container = container ;
2827 }
2928
29+ /**
30+ * Selects the template strategy based on element hosting `virtual-repeat` custom attribute
31+ */
3032 getStrategy ( element : Element ) : ITemplateStrategy {
3133 if ( element . parentNode && ( element . parentNode as Element ) . tagName === 'TBODY' ) {
3234 return this . container . get ( TableStrategy ) ;
@@ -39,18 +41,18 @@ export class TableStrategy implements ITemplateStrategy {
3941
4042 static inject = [ DomHelper ] ;
4143
42- tableCssReset = '\
43- display: block;\
44- width: auto;\
45- height: auto;\
46- margin: 0;\
47- padding: 0;\
48- border: none;\
49- border-collapse: inherit;\
50- border-spacing: 0;\
51- background-color: transparent;\
52- -webkit-border-horizontal-spacing: 0;\
53- -webkit-border-vertical-spacing: 0;' ;
44+ // tableCssReset = '\
45+ // display: block;\
46+ // width: auto;\
47+ // height: auto;\
48+ // margin: 0;\
49+ // padding: 0;\
50+ // border: none;\
51+ // border-collapse: inherit;\
52+ // border-spacing: 0;\
53+ // background-color: transparent;\
54+ // -webkit-border-horizontal-spacing: 0;\
55+ // -webkit-border-vertical-spacing: 0;';
5456
5557 domHelper : DomHelper ;
5658
@@ -63,7 +65,7 @@ export class TableStrategy implements ITemplateStrategy {
6365 }
6466
6567 moveViewFirst ( view : View , topBuffer : Element ) : void {
66- const tbody = this . _getTbodyElement ( topBuffer . nextSibling as Element ) ;
68+ const tbody = this . _getFirstTbody ( topBuffer . nextSibling as HTMLTableElement ) ;
6769 const tr = tbody . firstChild ;
6870 const firstElement = DOM . nextElementSibling ( tr ) ;
6971 insertBeforeNode ( view , firstElement ) ;
@@ -98,45 +100,41 @@ export class TableStrategy implements ITemplateStrategy {
98100 }
99101
100102 getFirstElement ( topBuffer : Element ) : Element {
101- const tbody = this . _getTbodyElement ( DOM . nextElementSibling ( topBuffer ) ) ;
102- const tr = tbody . firstChild as HTMLTableRowElement ;
103+ const tbody = this . _getFirstTbody ( DOM . nextElementSibling ( topBuffer ) as HTMLTableElement ) ;
104+ const tr = tbody . firstElementChild as HTMLTableRowElement ;
103105 // since the buffer is outside table, first element _is_ first element.
104106 return tr ;
105107 }
106108
107109 getLastElement ( bottomBuffer : Element ) : Element {
108- const tbody = this . _getTbodyElement ( bottomBuffer . previousSibling as Element ) ;
109- const trs = tbody . children ;
110- return trs [ trs . length - 1 ] ;
110+ const tbody = this . _getLastTbody ( bottomBuffer . previousSibling as HTMLTableElement ) ;
111+ return tbody . lastElementChild as HTMLTableRowElement ;
111112 }
112113
113114 getTopBufferDistance ( topBuffer : Element ) : number {
114- const tbody = this . _getTbodyElement ( topBuffer . nextSibling as Element ) ;
115+ const tbody = this . _getFirstTbody ( topBuffer . nextSibling as HTMLTableElement ) ;
115116 return this . domHelper . getElementDistanceToTopOfDocument ( tbody ) - this . domHelper . getElementDistanceToTopOfDocument ( topBuffer ) ;
116117 }
117118
118- getLastView ( bottomBuffer : Element ) : Element {
119- throw new Error ( 'Method getLastView() not implemented.' ) ;
119+ private _getFirstTbody ( tableElement : HTMLTableElement ) : HTMLTableSectionElement {
120+ let child = tableElement . firstElementChild ;
121+ while ( child !== null && child . tagName !== 'TBODY' ) {
122+ child = child . nextElementSibling ;
123+ }
124+ return child . tagName === 'TBODY' ? child as HTMLTableSectionElement : null ;
120125 }
121126
122- private _getTbodyElement ( tableElement : Element ) : Element {
123- let tbodyElement : Element ;
124- const children = tableElement . children ;
125- for ( let i = 0 , ii = children . length ; i < ii ; ++ i ) {
126- if ( children [ i ] . localName === 'tbody' ) {
127- tbodyElement = children [ i ] ;
128- break ;
129- }
127+ private _getLastTbody ( tableElement : HTMLTableElement ) : HTMLTableSectionElement {
128+ let child = tableElement . lastElementChild ;
129+ while ( child !== null && child . tagName !== 'TBODY' ) {
130+ child = child . previousElementSibling ;
130131 }
131- return tbodyElement ;
132+ return child . tagName === 'TBODY' ? child as HTMLTableSectionElement : null ;
132133 }
133134}
134135
135136export class DefaultTemplateStrategy implements ITemplateStrategy {
136137
137- getLastView ( bottomBuffer : Element ) : Element {
138- throw new Error ( 'Method getLastView() not implemented.' ) ;
139- }
140138 getScrollContainer ( element : Element ) : HTMLElement {
141139 return element . parentNode as HTMLElement ;
142140 }
0 commit comments