|
| 1 | +package dotty.tools.scaladoc |
| 2 | + |
| 3 | +import scala.concurrent.ExecutionContext.Implicits.global |
| 4 | +import scala.concurrent.Future |
| 5 | +import scala.util.{Success,Failure} |
| 6 | + |
| 7 | +import org.scalajs.dom._ |
| 8 | +import org.scalajs.dom.ext._ |
| 9 | +import scala.scalajs.js.annotation.JSExportTopLevel |
| 10 | +import org.scalajs.dom.ext.Ajax |
| 11 | +import scala.scalajs.js |
| 12 | +import scala.scalajs.js.JSON |
| 13 | + |
| 14 | +trait Versions extends js.Object: |
| 15 | + def versions: js.Dictionary[String] |
| 16 | + |
| 17 | +class DropdownHandler: |
| 18 | + |
| 19 | + val KEY = "versions-json" |
| 20 | + val UNDEFINED_VERSIONS = "undefined_versions" |
| 21 | + |
| 22 | + private def addVersionsList(json: String) = |
| 23 | + val ver = JSON.parse(json).asInstanceOf[Versions] |
| 24 | + val ddc = document.getElementById("dropdown-content") |
| 25 | + for (k, v) <- ver.versions do |
| 26 | + var child = document.createElement("a").asInstanceOf[html.Anchor] |
| 27 | + child.href = v |
| 28 | + child.text = k |
| 29 | + ddc.appendChild(child) |
| 30 | + |
| 31 | + private def disableButton() = |
| 32 | + val btn = document.getElementById("dropdown-button").asInstanceOf[html.Button] |
| 33 | + btn.disabled = true |
| 34 | + btn.classList.remove("dropdownbtnactive") |
| 35 | + |
| 36 | + private def getURLContent(url: String): Future[String] = Ajax.get(url).map(_.responseText) |
| 37 | + |
| 38 | + window.localStorage.getItem(KEY) match |
| 39 | + case null => // If no key, returns null |
| 40 | + Globals.versionsDictionaryUrl match |
| 41 | + case null => // global property not defined |
| 42 | + // do nothing |
| 43 | + case url: String => |
| 44 | + getURLContent(url).onComplete { |
| 45 | + case Success(json: String) => |
| 46 | + window.localStorage.setItem(KEY, json) |
| 47 | + addVersionsList(json) |
| 48 | + case Failure(_) => |
| 49 | + window.localStorage.setItem(KEY, UNDEFINED_VERSIONS) |
| 50 | + disableButton() |
| 51 | + } |
| 52 | + case value => value match |
| 53 | + case UNDEFINED_VERSIONS => |
| 54 | + disableButton() |
| 55 | + case json => |
| 56 | + addVersionsList(json) |
| 57 | + |
| 58 | + |
| 59 | + document.onclick = (e: Event) => { |
| 60 | + if e.target.asInstanceOf[html.Element].id != "dropdown-button" then |
| 61 | + document.getElementById("dropdown-content").classList.remove("show") |
| 62 | + } |
| 63 | + |
| 64 | + document.getElementById("version").asInstanceOf[html.Span].onclick = (e: Event) => { |
| 65 | + e.stopPropagation |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | +@JSExportTopLevel("dropdownHandler") |
| 70 | +def dropdownHandler() = |
| 71 | + if document.getElementById("dropdown-content").getElementsByTagName("a").size > 0 && |
| 72 | + window.getSelection.toString.length == 0 then |
| 73 | + document.getElementById("dropdown-content").classList.toggle("show") |
| 74 | + |
| 75 | +@JSExportTopLevel("filterFunction") |
| 76 | +def filterFunction() = |
| 77 | + val input = document.getElementById("dropdown-input").asInstanceOf[html.Input] |
| 78 | + val filter = input.value.toUpperCase |
| 79 | + val div = document.getElementById("dropdown-content") |
| 80 | + val a = div.getElementsByTagName("a") |
| 81 | + for i <- 0 until a.length do |
| 82 | + val txtValue = a(i).innerText |
| 83 | + val disp = if txtValue.toUpperCase.indexOf(filter) > -1 then |
| 84 | + "" |
| 85 | + else |
| 86 | + "none" |
| 87 | + a(i).asInstanceOf[html.Anchor].style.display = disp |
0 commit comments