summaryrefslogtreecommitdiff
path: root/examples/demos/Controller.scala
blob: e17cde9df78de769865beb300d121934bb6bebc6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import japgolly.scalajs.react.React
import japgolly.scalajs.react.vdom.VDomBuilder

import scala.scalajs.js
import scala.scalajs.js.annotation.JSExport
import org.scalajs.dom
import org.scalajs.dom.extensions._
import scala.collection.mutable
import scalatags.JsDom.all._


case class Tree[T](name: T, children: Vector[Tree[T]])
@JSExport
object Controller{

  def munge(name: String) = {
    name.replace(" ", "")
  }
  def addClass(el: dom.HTMLElement, cls: String) = {
    removeClass(el, cls)
    el.className = el.className + " " + cls
  }
  def removeClass(el: dom.HTMLElement, cls: String) = {
    el.className = el.className.split(' ').filter(_ != cls).mkString(" ")
  }
  def toggleClass(el: dom.HTMLElement, cls: String) = {
    val frags = el.className.split(' ')
    if (!frags.contains(cls)) addClass(el, cls)
    else removeClass(el, cls)
  }
  @JSExport
  def main(data: scala.scalajs.js.Any) = {

    val structure = upickle.readJs[Tree[String]](upickle.json.readJs(data))
    var i = 0
    def recurse(t: Tree[String]): Tree[(String, String, Int)] = {
      val curr = (t.name, munge(t.name), i)
      i += 1
      val children = t.children.map(recurse)
      Tree(curr, children)
    }


    val Seq(main, menu, layout, menuLink) = Seq(
      "main", "menu", "layout", "menuLink"
    ).map(dom.document.getElementById)

    val snippets = dom.document.getElementsByClassName("highlight-me")

    snippets.foreach(js.Dynamic.global.hljs.highlightBlock(_))

    val headers = {
      def offset(el: dom.HTMLElement, parent: dom.HTMLElement): Double = {
        if (el == parent) 0
        else el.offsetTop + offset(el.offsetParent.asInstanceOf[dom.HTMLElement], parent)
      }
      val menuItems = {
        def rec(current: Tree[String]): Seq[String] = {
          current.name +: current.children.flatMap(rec)
        }
        rec(structure).tail
      }
      menuItems.map(munge)
        .map(dom.document.getElementById)
        .map(offset(_, main))
        .toVector
    }


    val menuBar = React.renderComponent(
      Menu(recurse(structure)),
      menu
    )
    menuLink.onclick = (e: dom.MouseEvent) => {
      toggleClass(layout, "active")
      toggleClass(menu, "active")
      toggleClass(menuLink, "active")
    }

    var scrolling = false
    main.onscroll = (e: dom.UIEvent) => {
      if (!scrolling){
        scrolling = true
        dom.requestAnimationFrame{(d: Double) =>
          scrolling = false
          val threshold = main.scrollTop + main.clientHeight

          var index = 0
          while(index < headers.length && index >= 0){
            index += 1
            if (headers(index) > threshold) index *= -1
          }
          index = -index
          menuBar.setState(index)
        }
      }
    }
  }
  def isElementInViewport(el: dom.HTMLElement) = {
    val rect = el.getBoundingClientRect()
    rect.top >= 0 && rect.bottom <= dom.innerHeight
  }

  import japgolly.scalajs.react._ // React
  import vdom.ReactVDom._         // Scalatags → React virtual DOM
  import vdom.ReactVDom.all._     // Scalatags html & css (div, h1, textarea, etc.)

  val Menu = ReactComponentB[Tree[(String, String, Int)]]("Menu")
                            .getInitialState(_ => 0)
                            .render{ (structure, _, index) =>

    val contentList = {
      var i = 0
      def rec1(current: Tree[(String, String, Int)]): Tree[(String, String, Int, Boolean)] = {
        val initialI = i
        i += 1
        val children = current.children.map(rec1)

        val win = i >= index && initialI < index
        Tree(
          (current.name._1, current.name._2, current.name._3, win),
          children
        )
      }
      def rec(current: Tree[(String, String, Int, Boolean)],
              depth: Int,
              classes: String): Iterator[Tag] = {

        val winIndex = current.children.indexWhere(_.name._4)
        val (before, after) = current.children.splitAt(winIndex)

        val myCls =
          "menu-item" +
          (if (depth <= 1) " menu-item-divided" else "")

        val (name, munged, currIndex, win) = current.name
        val frag =
          li(paddingLeft := s"${depth * 15}px")(
            a(
              name,
              href:="#"+munged,
              cls:=myCls
            ),
            cls:=classes + (if (win) " pure-menu-selected" else "")
          )
        val afterCls = if (!win) "hide" else ""
        Iterator(frag) ++
        before.flatMap(rec(_, depth+1, "lined")) ++
        after.flatMap(rec(_, depth+1, afterCls))
      }
      rec(rec1(structure), 0, "")
    }

    val frag = div(cls:="pure-menu  pure-menu-open")(
      a(cls:="pure-menu-heading", href:="#")(
        "Contents"
      ),
      ul(cls:="menu-item-list")(
        contentList.drop(1).toVector
      )
    )
    frag
  }.build
}