@@ -5,22 +5,19 @@ import io.udash.web.commons.config.ExternalUrls
55import io .udash .web .commons .styles .GlobalStyles
66import io .udash .web .commons .styles .attributes .Attributes
77import io .udash .web .commons .styles .components .{HeaderButtonsStyles , HeaderNavStyles , MobileMenuStyles }
8- import io .udash .web .homepage .Context ._
9- import io .udash .web .homepage .{ HelloState , IndexState }
8+ import io .udash .web .homepage .Context .*
9+ import io .udash .web .homepage .HelloState
1010import io .udash .web .homepage .styles .partials .{HeaderStyles , HomepageStyles }
11- import io .udash .wrappers .jquery .{ JQueryEvent , _ }
12- import org .scalajs .dom .Element
13- import scalatags .JsDom .all ._
11+ import io .udash .wrappers .jquery .*
12+ import org .scalajs .dom .{ Element , document }
13+ import scalatags .JsDom .all .*
1414
1515object Header extends HeaderButtons with HeaderNav {
16- import io .udash .css .CssView ._
16+ import io .udash .css .CssView .*
1717
1818 override val buttonStyles : HeaderButtonsStyles = HeaderStyles
1919 override val navStyles : HeaderNavStyles = HeaderStyles
2020
21- private val window = jQ(org.scalajs.dom.window)
22- window.on(" scroll" , onScroll)
23-
2421 private lazy val btnMobileMenu = a(href := " #" , MobileMenuStyles .btnMobile, HeaderStyles .btnMobile)(
2522 div(MobileMenuStyles .btnMobileLines)(
2623 span(MobileMenuStyles .btnMobileLineTop),
@@ -46,35 +43,34 @@ object Header extends HeaderButtons with HeaderNav {
4643 )
4744 ).render
4845
49- private lazy val jqNav = jQ(navElement)
50- private lazy val jqMobileButton = jQ(btnMobileMenu )
46+ locally {
47+ jQ(org.scalajs.dom.window).on( " scroll " , onScroll )
5148
52- jqMobileButton.on(EventName .click, (_ : Element , jqEvent : JQueryEvent ) => {
53- jqEvent.preventDefault()
54- toggleBooleanAttribute(jqNav, Attributes .data(Attributes .Active ))
55- toggleBooleanAttribute(jqMobileButton, Attributes .data(Attributes .Active ))
56- })
49+ jQ(btnMobileMenu).on(EventName .click, (_ : Element , jqEvent : JQueryEvent ) => {
50+ jqEvent.preventDefault()
51+ toggleBooleanAttribute(navElement, Attributes .data(Attributes .Active ))
52+ toggleBooleanAttribute(btnMobileMenu, Attributes .data(Attributes .Active ))
53+ })
54+ }
5755
5856 private def onScroll (el : Element , ev : JQueryEvent ): Unit = {
5957 val pinnedAttr : String = Attributes .data(Attributes .Pinned )
6058
61- val pin = jQ(template).attr(pinnedAttr).getOrElse(" false" ).toBoolean
62- val scrollTop = jQ(org.scalajs.dom.window).scrollTop()
63- val introHeight = jQ(s " . ${HomepageStyles .sectionIntro.className}" ).height()
64-
59+ val pin = template.attributes.get(pinnedAttr).exists(_.value == " true" )
60+ val scrollTop = org.scalajs.dom.window.scrollY
61+ val introHeight = document.querySelector(s " . ${HomepageStyles .sectionIntro.className}" ).getBoundingClientRect().height
6562 if (scrollTop >= introHeight && ! pin) {
66- jQ( template).attr (pinnedAttr, " true" )
63+ template.setAttribute (pinnedAttr, " true" )
6764 } else if (scrollTop < introHeight && pin) {
68- jQ( template).attr (pinnedAttr, " false" )
65+ template.setAttribute (pinnedAttr, " false" )
6966 }
7067 }
7168
7269 def getTemplate : Modifier = template
7370
74- private def toggleBooleanAttribute (jqElement : JQuery , attribute : String ): Unit = {
75- val activeOption = jqElement.attr(attribute)
76- val newValue = if (activeOption.isEmpty || ! activeOption.get.toBoolean) true else false
77-
78- jqElement.attr(Attributes .data(Attributes .Active ), newValue.toString)
71+ private def toggleBooleanAttribute (element : Element , attribute : String ): Unit = {
72+ val activeOption = element.attributes.get(attribute).map(_.value)
73+ val newValue = activeOption.isEmpty || ! activeOption.get.toBoolean
74+ element.setAttribute(attribute, newValue.toString)
7975 }
8076}
0 commit comments