Sin descripción

webhtml.go 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. // Copyright 2017 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package driver
  15. import "html/template"
  16. var graphTemplate = template.Must(template.New("graph").Parse(
  17. `<!DOCTYPE html>
  18. <html>
  19. <head>
  20. <meta charset="utf-8">
  21. <title>{{.Title}}</title>
  22. <style type="text/css">
  23. html, body {
  24. height: 100%;
  25. min-height: 100%;
  26. margin: 0px;
  27. }
  28. body {
  29. width: 100%;
  30. overflow: hidden;
  31. }
  32. h1 {
  33. font-weight: normal;
  34. font-size: 24px;
  35. padding: 0em;
  36. margin-top: 5px;
  37. margin-bottom: 5px;
  38. }
  39. #page {
  40. display: flex;
  41. flex-direction: column;
  42. height: 100%;
  43. min-height: 100%;
  44. width: 100%;
  45. min-width: 100%;
  46. margin: 0px;
  47. }
  48. #header {
  49. flex: 0 0 auto;
  50. width: 100%;
  51. }
  52. #leftbuttons {
  53. float: left;
  54. }
  55. #rightbuttons {
  56. float: right;
  57. display: table-cell;
  58. vertical-align: middle;
  59. }
  60. #rightbuttons label {
  61. vertical-align: middle;
  62. }
  63. #scale {
  64. vertical-align: middle;
  65. }
  66. #graph {
  67. flex: 1 1 auto;
  68. overflow: hidden;
  69. }
  70. svg {
  71. width: 100%;
  72. height: auto;
  73. border: 1px solid black;
  74. }
  75. button {
  76. margin-top: 5px;
  77. margin-bottom: 5px;
  78. }
  79. #reset, #scale {
  80. margin-left: 10px;
  81. }
  82. #detailtext {
  83. display: none;
  84. position: absolute;
  85. background-color: #ffffff;
  86. min-width: 160px;
  87. border-top: 1px solid black;
  88. box-shadow: 2px 2px 2px 0px #aaa;
  89. z-index: 1;
  90. }
  91. </style>
  92. </head>
  93. <body>
  94. <h1>{{.Title}}</h1>
  95. <div id="page">
  96. <div id="errors">{{range .Errors}}<div>{{.}}</div>{{end}}</div>
  97. <div id="header">
  98. <div id="leftbuttons">
  99. <button id="details">&#x25b7; Details</button>
  100. <div id="detailtext">
  101. {{range .Legend}}<div>{{.}}</div>{{end}}
  102. </div>
  103. <button id="list">List</button>
  104. <button id="disasm">Disasm</button>
  105. <input id="searchbox" type="text" placeholder="Search regexp" autocomplete="off" autocapitalize="none" size=40>
  106. <button id="focus">Focus</button>
  107. <button id="ignore">Ignore</button>
  108. <button id="hide">Hide</button>
  109. <button id="show">Show</button>
  110. <button id="reset">Reset</button>
  111. </div>
  112. <div id="rightbuttons">
  113. </div>
  114. </div>
  115. <div id="graph">
  116. {{.Svg}}
  117. </div>
  118. </div>
  119. <script>
  120. // Make svg pannable and zoomable.
  121. // Call clickHandler(t) if a click event is caught by the pan event handlers.
  122. function initPanAndZoom(svg, clickHandler) {
  123. 'use strict';
  124. // Current mouse/touch handling mode
  125. const IDLE = 0
  126. const MOUSEPAN = 1
  127. const TOUCHPAN = 2
  128. const TOUCHZOOM = 3
  129. let mode = IDLE
  130. // State needed to implement zooming.
  131. let currentScale = 1.0
  132. const initWidth = svg.viewBox.baseVal.width
  133. const initHeight = svg.viewBox.baseVal.height
  134. // State needed to implement panning.
  135. let panLastX = 0 // Last event X coordinate
  136. let panLastY = 0 // Last event Y coordinate
  137. let moved = false // Have we seen significant movement
  138. let touchid = null // Current touch identifier
  139. // State needed for pinch zooming
  140. let touchid2 = null // Second id for pinch zooming
  141. let initGap = 1.0 // Starting gap between two touches
  142. let initScale = 1.0 // currentScale when pinch zoom started
  143. let centerPoint = null // Center point for scaling
  144. // Convert event coordinates to svg coordinates.
  145. function toSvg(x, y) {
  146. const p = svg.createSVGPoint()
  147. p.x = x
  148. p.y = y
  149. let m = svg.getCTM()
  150. if (m == null) m = svg.getScreenCTM() // Firefox workaround.
  151. return p.matrixTransform(m.inverse())
  152. }
  153. // Change the scaling for the svg to s, keeping the point denoted
  154. // by u (in svg coordinates]) fixed at the same screen location.
  155. function rescale(s, u) {
  156. // Limit to a good range.
  157. if (s < 0.2) s = 0.2
  158. if (s > 10.0) s = 10.0
  159. currentScale = s
  160. // svg.viewBox defines the visible portion of the user coordinate
  161. // system. So to magnify by s, divide the visible portion by s,
  162. // which will then be stretched to fit the viewport.
  163. const vb = svg.viewBox
  164. const w1 = vb.baseVal.width
  165. const w2 = initWidth / s
  166. const h1 = vb.baseVal.height
  167. const h2 = initHeight / s
  168. vb.baseVal.width = w2
  169. vb.baseVal.height = h2
  170. // We also want to adjust vb.baseVal.x so that u.x remains at same
  171. // screen X coordinate. In other words, want to change it from x1 to x2
  172. // so that:
  173. // (u.x - x1) / w1 = (u.x - x2) / w2
  174. // Simplifying that, we get
  175. // (u.x - x1) * (w2 / w1) = u.x - x2
  176. // x2 = u.x - (u.x - x1) * (w2 / w1)
  177. vb.baseVal.x = u.x - (u.x - vb.baseVal.x) * (w2 / w1)
  178. vb.baseVal.y = u.y - (u.y - vb.baseVal.y) * (h2 / h1)
  179. }
  180. function handleWheel(e) {
  181. if (e.deltaY == 0) return
  182. // Change scale factor by 1.1 or 1/1.1
  183. rescale(currentScale * (e.deltaY < 0 ? 1.1 : (1/1.1)),
  184. toSvg(e.offsetX, e.offsetY))
  185. }
  186. function setMode(m) {
  187. mode = m
  188. touchid = null
  189. touchid2 = null
  190. }
  191. function panStart(x, y) {
  192. moved = false
  193. panLastX = x
  194. panLastY = y
  195. }
  196. function panMove(x, y) {
  197. let dx = x - panLastX
  198. let dy = y - panLastY
  199. if (Math.abs(dx) <= 2 && Math.abs(dy) <= 2) return // Ignore tiny moves
  200. moved = true
  201. panLastX = x
  202. panLastY = y
  203. // Firefox workaround: get dimensions from parentNode.
  204. const swidth = svg.clientWidth || svg.parentNode.clientWidth
  205. const sheight = svg.clientHeight || svg.parentNode.clientHeight
  206. // Convert deltas from screen space to svg space.
  207. dx *= (svg.viewBox.baseVal.width / swidth)
  208. dy *= (svg.viewBox.baseVal.height / sheight)
  209. svg.viewBox.baseVal.x -= dx
  210. svg.viewBox.baseVal.y -= dy
  211. }
  212. function handleScanStart(e) {
  213. if (e.button != 0) return // Do not catch right-clicks etc.
  214. setMode(MOUSEPAN)
  215. panStart(e.clientX, e.clientY)
  216. e.preventDefault()
  217. svg.addEventListener("mousemove", handleScanMove)
  218. }
  219. function handleScanMove(e) {
  220. if (mode == MOUSEPAN) panMove(e.clientX, e.clientY)
  221. }
  222. function handleScanEnd(e) {
  223. if (mode == MOUSEPAN) panMove(e.clientX, e.clientY)
  224. setMode(IDLE)
  225. svg.removeEventListener("mousemove", handleScanMove)
  226. if (!moved) clickHandler(e.target)
  227. }
  228. // Find touch object with specified identifier.
  229. function findTouch(tlist, id) {
  230. for (const t of tlist) {
  231. if (t.identifier == id) return t
  232. }
  233. return null
  234. }
  235. // Return distance between two touch points
  236. function touchGap(t1, t2) {
  237. const dx = t1.clientX - t2.clientX
  238. const dy = t1.clientY - t2.clientY
  239. return Math.hypot(dx, dy)
  240. }
  241. function handleTouchStart(e) {
  242. if (mode == IDLE && e.changedTouches.length == 1) {
  243. // Start touch based panning
  244. const t = e.changedTouches[0]
  245. setMode(TOUCHPAN)
  246. touchid = t.identifier
  247. panStart(t.clientX, t.clientY)
  248. e.preventDefault()
  249. } else if (mode == TOUCHPAN && e.touches.length == 2) {
  250. // Start pinch zooming
  251. setMode(TOUCHZOOM)
  252. const t1 = e.touches[0]
  253. const t2 = e.touches[1]
  254. touchid = t1.identifier
  255. touchid2 = t2.identifier
  256. initScale = currentScale
  257. initGap = touchGap(t1, t2)
  258. centerPoint = toSvg((t1.clientX + t2.clientX) / 2,
  259. (t1.clientY + t2.clientY) / 2)
  260. e.preventDefault()
  261. }
  262. }
  263. function handleTouchMove(e) {
  264. if (mode == TOUCHPAN) {
  265. const t = findTouch(e.changedTouches, touchid)
  266. if (t == null) return
  267. if (e.touches.length != 1) {
  268. setMode(IDLE)
  269. return
  270. }
  271. panMove(t.clientX, t.clientY)
  272. e.preventDefault()
  273. } else if (mode == TOUCHZOOM) {
  274. // Get two touches; new gap; rescale to ratio.
  275. const t1 = findTouch(e.touches, touchid)
  276. const t2 = findTouch(e.touches, touchid2)
  277. if (t1 == null || t2 == null) return
  278. const gap = touchGap(t1, t2)
  279. rescale(initScale * gap / initGap, centerPoint)
  280. e.preventDefault()
  281. }
  282. }
  283. function handleTouchEnd(e) {
  284. if (mode == TOUCHPAN) {
  285. const t = findTouch(e.changedTouches, touchid)
  286. if (t == null) return
  287. panMove(t.clientX, t.clientY)
  288. setMode(IDLE)
  289. e.preventDefault()
  290. if (!moved) clickHandler(t.target)
  291. } else if (mode == TOUCHZOOM) {
  292. setMode(IDLE)
  293. e.preventDefault()
  294. }
  295. }
  296. svg.addEventListener("mousedown", handleScanStart)
  297. svg.addEventListener("mouseup", handleScanEnd)
  298. svg.addEventListener("touchstart", handleTouchStart)
  299. svg.addEventListener("touchmove", handleTouchMove)
  300. svg.addEventListener("touchend", handleTouchEnd)
  301. svg.addEventListener("wheel", handleWheel, true)
  302. }
  303. function dotviewer(nodes) {
  304. 'use strict';
  305. // Elements
  306. const detailsButton = document.getElementById("details")
  307. const detailsText = document.getElementById("detailtext")
  308. const listButton = document.getElementById("list")
  309. const disasmButton = document.getElementById("disasm")
  310. const resetButton = document.getElementById("reset")
  311. const focusButton = document.getElementById("focus")
  312. const showButton = document.getElementById("show")
  313. const ignoreButton = document.getElementById("ignore")
  314. const hideButton = document.getElementById("hide")
  315. const search = document.getElementById("searchbox")
  316. const graph0 = document.getElementById("graph0")
  317. const svg = graph0.parentElement
  318. let currentRe = ""
  319. let selected = new Map()
  320. let origFill = new Map()
  321. let searchAlarm = null
  322. let buttonsEnabled = true
  323. function handleDetails() {
  324. if (detailtext.style.display == "block") {
  325. detailtext.style.display = "none"
  326. detailsButton.innerText = "\u25b7 Details"
  327. } else {
  328. detailtext.style.display = "block"
  329. detailsButton.innerText = "\u25bd Details"
  330. }
  331. }
  332. function handleReset() { window.location.href = "/" }
  333. function handleList() { navigate("/weblist", "f", true) }
  334. function handleDisasm() { navigate("/disasm", "f", true) }
  335. function handleFocus() { navigate("/", "f", false) }
  336. function handleShow() { navigate("/", "s", false) }
  337. function handleIgnore() { navigate("/", "i", false) }
  338. function handleHide() { navigate("/", "h", false) }
  339. function handleSearch() {
  340. // Delay processing so a flurry of key strokes is handled once.
  341. if (searchAlarm != null) {
  342. clearTimeout(searchAlarm)
  343. }
  344. searchAlarm = setTimeout(doSearch, 300)
  345. }
  346. function doSearch() {
  347. searchAlarm = null
  348. let re = null
  349. if (search.value != "") {
  350. try {
  351. re = new RegExp(search.value)
  352. } catch (e) {
  353. // TODO: Display error state in search box
  354. return
  355. }
  356. }
  357. currentRe = search.value
  358. function match(text) {
  359. return re != null && re.test(text)
  360. }
  361. // drop currently selected items that do not match re.
  362. selected.forEach(function(v, n) {
  363. if (!match(nodes[n])) {
  364. unselect(n, document.getElementById("node" + n))
  365. }
  366. })
  367. // add matching items that are not currently selected.
  368. for (let n = 0; n < nodes.length; n++) {
  369. if (!selected.has(n) && match(nodes[n])) {
  370. select(n, document.getElementById("node" + n))
  371. }
  372. }
  373. updateButtons()
  374. }
  375. function toggleSelect(elem) {
  376. // Walk up to immediate child of graph0
  377. while (elem != null && elem.parentElement != graph0) {
  378. elem = elem.parentElement
  379. }
  380. if (!elem) return
  381. // Disable regexp mode.
  382. currentRe = ""
  383. const n = nodeId(elem)
  384. if (n < 0) return
  385. if (selected.has(n)) {
  386. unselect(n, elem)
  387. } else {
  388. select(n, elem)
  389. }
  390. updateButtons()
  391. }
  392. function unselect(n, elem) {
  393. if (elem == null) return
  394. selected.delete(n)
  395. setBackground(elem, false)
  396. }
  397. function select(n, elem) {
  398. if (elem == null) return
  399. selected.set(n, true)
  400. setBackground(elem, true)
  401. }
  402. function nodeId(elem) {
  403. const id = elem.id
  404. if (!id) return -1
  405. if (!id.startsWith("node")) return -1
  406. const n = parseInt(id.slice(4), 10)
  407. if (isNaN(n)) return -1
  408. if (n < 0 || n >= nodes.length) return -1
  409. return n
  410. }
  411. function setBackground(elem, set) {
  412. const p = findPolygon(elem)
  413. if (p != null) {
  414. if (set) {
  415. origFill.set(p, p.style.fill)
  416. p.style.fill = "#ccccff"
  417. } else if (origFill.has(p)) {
  418. p.style.fill = origFill.get(p)
  419. }
  420. }
  421. }
  422. function findPolygon(elem) {
  423. if (elem.localName == "polygon") return elem
  424. for (const c of elem.children) {
  425. const p = findPolygon(c)
  426. if (p != null) return p
  427. }
  428. return null
  429. }
  430. // Navigate to specified path with current selection reflected
  431. // in the named parameter.
  432. function navigate(path, param, newWindow) {
  433. // The selection can be in one of two modes: regexp-based or
  434. // list-based. Construct regular expression depending on mode.
  435. let re = currentRe
  436. if (re == "") {
  437. selected.forEach(function(v, key) {
  438. if (re != "") re += "|"
  439. re += nodes[key]
  440. })
  441. }
  442. const url = new URL(window.location.href)
  443. url.pathname = path
  444. url.hash = ""
  445. if (re != "") {
  446. // For focus/show, forget old parameter. For others, add to re.
  447. const params = url.searchParams
  448. if (param != "f" && param != "s" && params.has(param)) {
  449. const old = params.get(param)
  450. if (old != "") {
  451. re += "|" + old
  452. }
  453. }
  454. params.set(param, re)
  455. }
  456. if (newWindow) {
  457. window.open(url.toString(), "_blank")
  458. } else {
  459. window.location.href = url.toString()
  460. }
  461. }
  462. function updateButtons() {
  463. const enable = (currentRe != "" || selected.size != 0)
  464. if (buttonsEnabled == enable) return
  465. buttonsEnabled = enable
  466. listButton.disabled = !enable
  467. disasmButton.disabled = !enable
  468. focusButton.disabled = !enable
  469. showButton.disabled = !enable
  470. ignoreButton.disabled = !enable
  471. hideButton.disabled = !enable
  472. }
  473. // Initialize button states
  474. updateButtons()
  475. // Setup event handlers
  476. initPanAndZoom(svg, toggleSelect)
  477. function bindButtons(evt) {
  478. detailsButton.addEventListener(evt, handleDetails)
  479. listButton.addEventListener(evt, handleList)
  480. disasmButton.addEventListener(evt, handleDisasm)
  481. resetButton.addEventListener(evt, handleReset)
  482. focusButton.addEventListener(evt, handleFocus)
  483. showButton.addEventListener(evt, handleShow)
  484. ignoreButton.addEventListener(evt, handleIgnore)
  485. hideButton.addEventListener(evt, handleHide)
  486. }
  487. bindButtons("click")
  488. bindButtons("touchstart")
  489. search.addEventListener("input", handleSearch)
  490. }
  491. dotviewer({{.Nodes}})
  492. </script>
  493. </body>
  494. </html>
  495. `))