1- function setToggle ( elementClass , targetElement , classToAdd ) {
1+ function setToggle ( elementClass , targetElement , classToAdd , lastIndex ) {
22 const allDays = document . getElementsByClassName ( elementClass ) ;
3- for ( let i = window [ ' lastIndex' ] ; i < allDays . length ; ++ i ) {
3+ for ( let i = lastIndex ; i < allDays . length ; ++ i ) {
44 allDays [ i ] . addEventListener ( "click" , function ( ) {
55 const target = document . getElementById ( targetElement ) ;
66 target . classList . toggle ( classToAdd ) ;
77 } )
88 }
9- window [ 'lastIndex' ] += allDays . length - window [ 'lastIndex' ] ;
109}
1110
12-
1311document . addEventListener (
1412 'DOMContentLoaded' , function ( ) {
15- window [ 'lastIndex' ] = 0 ;
16- window [ 'last' ] = 0 ;
17- setToggle ( "day" , "day-view" , "day-view-visible" ) ;
13+ setToggle ( "day" , "day-view" , "day-view-visible" , 0 ) ;
1814 }
1915)
2016
21- function loadWeek ( lastDay ) {
22- if ( lastDay === window [ 'last' ] ) {
17+ function loadWeek ( lastDay , index ) {
18+ if ( lastDay . dataset . last === "false" ) {
2319 return false ;
2420 }
25- const path = '/calendar/month/' + lastDay ;
21+ lastDay . dataset . last = false ;
22+ const path = '/calendar/month/' + lastDay . id ;
23+ alert ( path ) ;
2624 const newDays = document . createElement ( 'html' ) ;
27- window [ 'last' ] = lastDay ;
2825 fetch ( path ) . then ( function ( response ) {
2926 return response . text ( ) ;
3027 } ) . then ( function ( html ) {
3128 const newDiv = document . createElement ( "div" ) ;
3229 newDays . innerHTML = html
3330 newDiv . appendChild ( newDays ) ;
3431 document . getElementById ( "calender-grid" ) . append ( newDays ) ;
35- setToggle ( "day" , "day-view" , "day-view-visible" ) ;
32+ setToggle ( "day" , "day-view" , "day-view-visible" , index ) ;
3633 } ) ;
3734}
3835
3936window . addEventListener (
4037 'scroll' , function ( ) {
41- const tolerance = 1 ;
38+ const tolerance = 100 ;
4239 if ( window . scrollY + window . innerHeight + tolerance < document . documentElement . scrollHeight ) {
4340 return false ;
4441 }
4542 const allDays = document . querySelectorAll ( '.day' ) ;
46- const lastDay = allDays [ allDays . length - 1 ] . id ;
47- loadWeek ( lastDay ) ;
43+ const lastDay = allDays [ allDays . length - 1 ] ;
44+ loadWeek ( lastDay , allDays . length ) ;
4845 }
4946)
0 commit comments