Aucune description

webhtml.go 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  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. // addTemplates adds a set of template definitions to templates.
  17. func addTemplates(templates *template.Template) {
  18. template.Must(templates.Parse(`
  19. {{define "css"}}
  20. <style type="text/css">
  21. html {
  22. height: 100%;
  23. min-height: 100%;
  24. margin: 0px;
  25. }
  26. body {
  27. margin: 0px;
  28. width: 100%;
  29. height: 100%;
  30. min-height: 100%;
  31. overflow: hidden;
  32. }
  33. #graphcontainer {
  34. display: flex;
  35. flex-direction: column;
  36. height: 100%;
  37. min-height: 100%;
  38. width: 100%;
  39. min-width: 100%;
  40. margin: 0px;
  41. }
  42. #graph {
  43. flex: 1 1 auto;
  44. overflow: hidden;
  45. }
  46. svg {
  47. width: 100%;
  48. height: auto;
  49. }
  50. button {
  51. margin-top: 5px;
  52. margin-bottom: 5px;
  53. }
  54. #detailtext {
  55. display: none;
  56. position: fixed;
  57. top: 20px;
  58. right: 10px;
  59. background-color: #ffffff;
  60. min-width: 160px;
  61. border: 1px solid #888;
  62. box-shadow: 4px 4px 4px 0px rgba(0,0,0,0.2);
  63. z-index: 1;
  64. }
  65. #closedetails {
  66. float: right;
  67. margin: 2px;
  68. }
  69. #home {
  70. font-size: 14pt;
  71. padding-left: 0.5em;
  72. padding-right: 0.5em;
  73. float: right;
  74. }
  75. .menubar {
  76. display: inline-block;
  77. background-color: #f8f8f8;
  78. border: 1px solid #ccc;
  79. width: 100%;
  80. }
  81. .menu-header {
  82. position: relative;
  83. display: inline-block;
  84. padding: 2px 2px;
  85. cursor: default;
  86. font-size: 14pt;
  87. }
  88. .menu {
  89. display: none;
  90. position: absolute;
  91. background-color: #f8f8f8;
  92. border: 1px solid #888;
  93. box-shadow: 4px 4px 4px 0px rgba(0,0,0,0.2);
  94. z-index: 1;
  95. margin-top: 2px;
  96. left: 0px;
  97. min-width: 5em;
  98. }
  99. .menu hr {
  100. background-color: #fff;
  101. margin-top: 0px;
  102. margin-bottom: 0px;
  103. }
  104. .menu a, .menu button {
  105. display: block;
  106. width: 100%;
  107. margin: 0px;
  108. padding: 2px 0px 2px 0px;
  109. text-align: left;
  110. text-decoration: none;
  111. color: #000;
  112. background-color: #f8f8f8;
  113. font-size: 12pt;
  114. border: none;
  115. }
  116. .menu-header:hover {
  117. background-color: #ccc;
  118. }
  119. .menu-header:hover .menu {
  120. display: block;
  121. }
  122. .menu a:hover, .menu button:hover {
  123. background-color: #ccc;
  124. }
  125. .menu a.disabled {
  126. color: gray;
  127. pointer-events: none;
  128. }
  129. #searchbox {
  130. margin-left: 10pt;
  131. }
  132. #bodycontainer {
  133. width: 100%;
  134. height: 100%;
  135. max-height: 100%;
  136. overflow: scroll;
  137. padding-top: 5px;
  138. }
  139. #toptable {
  140. border-spacing: 0px;
  141. width: 100%;
  142. padding-bottom: 1em;
  143. }
  144. #toptable tr th {
  145. border-bottom: 1px solid black;
  146. text-align: right;
  147. padding-left: 1em;
  148. padding-top: 0.2em;
  149. padding-bottom: 0.2em;
  150. }
  151. #toptable tr td {
  152. padding-left: 1em;
  153. font: monospace;
  154. text-align: right;
  155. white-space: nowrap;
  156. cursor: default;
  157. }
  158. #toptable tr th:nth-child(6),
  159. #toptable tr th:nth-child(7),
  160. #toptable tr td:nth-child(6),
  161. #toptable tr td:nth-child(7) {
  162. text-align: left;
  163. }
  164. #toptable tr td:nth-child(6) {
  165. max-width: 30em; // Truncate very long names
  166. overflow: hidden;
  167. }
  168. #flathdr1, #flathdr2, #cumhdr1, #cumhdr2, #namehdr {
  169. cursor: ns-resize;
  170. }
  171. .hilite {
  172. background-color: #ccf;
  173. }
  174. </style>
  175. {{end}}
  176. {{define "header"}}
  177. <div id="detailtext">
  178. <button id="closedetails">Close</button>
  179. {{range .Legend}}<div>{{.}}</div>{{end}}
  180. </div>
  181. <div class="menubar">
  182. <div class="menu-header">
  183. View
  184. <div class="menu">
  185. <a title="{{.Help.top}}" href="/top" id="topbtn">Top</a>
  186. <a title="{{.Help.graph}}" href="/" id="graphbtn">Graph</a>
  187. <a title="{{.Help.peek}}" href="/peek" id="peek">Peek</a>
  188. <a title="{{.Help.list}}" href="/source" id="list">Source</a>
  189. <a title="{{.Help.disasm}}" href="/disasm" id="disasm">Disassemble</a>
  190. <hr>
  191. <button title="{{.Help.details}}" id="details">Details</button>
  192. </div>
  193. </div>
  194. <div class="menu-header">
  195. Refine
  196. <div class="menu">
  197. <a title="{{.Help.focus}}" href="{{.BaseURL}}" id="focus">Focus</a>
  198. <a title="{{.Help.ignore}}" href="{{.BaseURL}}" id="ignore">Ignore</a>
  199. <a title="{{.Help.hide}}" href="{{.BaseURL}}" id="hide">Hide</a>
  200. <a title="{{.Help.show}}" href="{{.BaseURL}}" id="show">Show</a>
  201. <hr>
  202. <a title="{{.Help.reset}}" href="{{.BaseURL}}">Reset</a>
  203. </div>
  204. </div>
  205. <input id="searchbox" type="text" placeholder="Search regexp" autocomplete="off" autocapitalize="none" size=40>
  206. <span id="home">{{.Title}}</span>
  207. </div> <!-- menubar -->
  208. <div id="errors">{{range .Errors}}<div>{{.}}</div>{{end}}</div>
  209. {{end}}
  210. {{define "graph" -}}
  211. <!DOCTYPE html>
  212. <html>
  213. <head>
  214. <meta charset="utf-8">
  215. <title>{{.Title}}</title>
  216. {{template "css" .}}
  217. </head>
  218. <body>
  219. {{template "header" .}}
  220. <div id="graphcontainer">
  221. <div id="graph">
  222. {{.HTMLBody}}
  223. </div>
  224. </div>
  225. {{template "script" .}}
  226. <script>viewer({{.BaseURL}}, {{.Nodes}})</script>
  227. </body>
  228. </html>
  229. {{end}}
  230. {{define "script"}}
  231. <script>
  232. // Make svg pannable and zoomable.
  233. // Call clickHandler(t) if a click event is caught by the pan event handlers.
  234. function initPanAndZoom(svg, clickHandler) {
  235. 'use strict';
  236. // Current mouse/touch handling mode
  237. const IDLE = 0
  238. const MOUSEPAN = 1
  239. const TOUCHPAN = 2
  240. const TOUCHZOOM = 3
  241. let mode = IDLE
  242. // State needed to implement zooming.
  243. let currentScale = 1.0
  244. const initWidth = svg.viewBox.baseVal.width
  245. const initHeight = svg.viewBox.baseVal.height
  246. // State needed to implement panning.
  247. let panLastX = 0 // Last event X coordinate
  248. let panLastY = 0 // Last event Y coordinate
  249. let moved = false // Have we seen significant movement
  250. let touchid = null // Current touch identifier
  251. // State needed for pinch zooming
  252. let touchid2 = null // Second id for pinch zooming
  253. let initGap = 1.0 // Starting gap between two touches
  254. let initScale = 1.0 // currentScale when pinch zoom started
  255. let centerPoint = null // Center point for scaling
  256. // Convert event coordinates to svg coordinates.
  257. function toSvg(x, y) {
  258. const p = svg.createSVGPoint()
  259. p.x = x
  260. p.y = y
  261. let m = svg.getCTM()
  262. if (m == null) m = svg.getScreenCTM() // Firefox workaround.
  263. return p.matrixTransform(m.inverse())
  264. }
  265. // Change the scaling for the svg to s, keeping the point denoted
  266. // by u (in svg coordinates]) fixed at the same screen location.
  267. function rescale(s, u) {
  268. // Limit to a good range.
  269. if (s < 0.2) s = 0.2
  270. if (s > 10.0) s = 10.0
  271. currentScale = s
  272. // svg.viewBox defines the visible portion of the user coordinate
  273. // system. So to magnify by s, divide the visible portion by s,
  274. // which will then be stretched to fit the viewport.
  275. const vb = svg.viewBox
  276. const w1 = vb.baseVal.width
  277. const w2 = initWidth / s
  278. const h1 = vb.baseVal.height
  279. const h2 = initHeight / s
  280. vb.baseVal.width = w2
  281. vb.baseVal.height = h2
  282. // We also want to adjust vb.baseVal.x so that u.x remains at same
  283. // screen X coordinate. In other words, want to change it from x1 to x2
  284. // so that:
  285. // (u.x - x1) / w1 = (u.x - x2) / w2
  286. // Simplifying that, we get
  287. // (u.x - x1) * (w2 / w1) = u.x - x2
  288. // x2 = u.x - (u.x - x1) * (w2 / w1)
  289. vb.baseVal.x = u.x - (u.x - vb.baseVal.x) * (w2 / w1)
  290. vb.baseVal.y = u.y - (u.y - vb.baseVal.y) * (h2 / h1)
  291. }
  292. function handleWheel(e) {
  293. if (e.deltaY == 0) return
  294. // Change scale factor by 1.1 or 1/1.1
  295. rescale(currentScale * (e.deltaY < 0 ? 1.1 : (1/1.1)),
  296. toSvg(e.offsetX, e.offsetY))
  297. }
  298. function setMode(m) {
  299. mode = m
  300. touchid = null
  301. touchid2 = null
  302. }
  303. function panStart(x, y) {
  304. moved = false
  305. panLastX = x
  306. panLastY = y
  307. }
  308. function panMove(x, y) {
  309. let dx = x - panLastX
  310. let dy = y - panLastY
  311. if (Math.abs(dx) <= 2 && Math.abs(dy) <= 2) return // Ignore tiny moves
  312. moved = true
  313. panLastX = x
  314. panLastY = y
  315. // Firefox workaround: get dimensions from parentNode.
  316. const swidth = svg.clientWidth || svg.parentNode.clientWidth
  317. const sheight = svg.clientHeight || svg.parentNode.clientHeight
  318. // Convert deltas from screen space to svg space.
  319. dx *= (svg.viewBox.baseVal.width / swidth)
  320. dy *= (svg.viewBox.baseVal.height / sheight)
  321. svg.viewBox.baseVal.x -= dx
  322. svg.viewBox.baseVal.y -= dy
  323. }
  324. function handleScanStart(e) {
  325. if (e.button != 0) return // Do not catch right-clicks etc.
  326. setMode(MOUSEPAN)
  327. panStart(e.clientX, e.clientY)
  328. e.preventDefault()
  329. svg.addEventListener("mousemove", handleScanMove)
  330. }
  331. function handleScanMove(e) {
  332. if (e.buttons == 0) {
  333. // Missed an end event, perhaps because mouse moved outside window.
  334. setMode(IDLE)
  335. svg.removeEventListener("mousemove", handleScanMove)
  336. return
  337. }
  338. if (mode == MOUSEPAN) panMove(e.clientX, e.clientY)
  339. }
  340. function handleScanEnd(e) {
  341. if (mode == MOUSEPAN) panMove(e.clientX, e.clientY)
  342. setMode(IDLE)
  343. svg.removeEventListener("mousemove", handleScanMove)
  344. if (!moved) clickHandler(e.target)
  345. }
  346. // Find touch object with specified identifier.
  347. function findTouch(tlist, id) {
  348. for (const t of tlist) {
  349. if (t.identifier == id) return t
  350. }
  351. return null
  352. }
  353. // Return distance between two touch points
  354. function touchGap(t1, t2) {
  355. const dx = t1.clientX - t2.clientX
  356. const dy = t1.clientY - t2.clientY
  357. return Math.hypot(dx, dy)
  358. }
  359. function handleTouchStart(e) {
  360. if (mode == IDLE && e.changedTouches.length == 1) {
  361. // Start touch based panning
  362. const t = e.changedTouches[0]
  363. setMode(TOUCHPAN)
  364. touchid = t.identifier
  365. panStart(t.clientX, t.clientY)
  366. e.preventDefault()
  367. } else if (mode == TOUCHPAN && e.touches.length == 2) {
  368. // Start pinch zooming
  369. setMode(TOUCHZOOM)
  370. const t1 = e.touches[0]
  371. const t2 = e.touches[1]
  372. touchid = t1.identifier
  373. touchid2 = t2.identifier
  374. initScale = currentScale
  375. initGap = touchGap(t1, t2)
  376. centerPoint = toSvg((t1.clientX + t2.clientX) / 2,
  377. (t1.clientY + t2.clientY) / 2)
  378. e.preventDefault()
  379. }
  380. }
  381. function handleTouchMove(e) {
  382. if (mode == TOUCHPAN) {
  383. const t = findTouch(e.changedTouches, touchid)
  384. if (t == null) return
  385. if (e.touches.length != 1) {
  386. setMode(IDLE)
  387. return
  388. }
  389. panMove(t.clientX, t.clientY)
  390. e.preventDefault()
  391. } else if (mode == TOUCHZOOM) {
  392. // Get two touches; new gap; rescale to ratio.
  393. const t1 = findTouch(e.touches, touchid)
  394. const t2 = findTouch(e.touches, touchid2)
  395. if (t1 == null || t2 == null) return
  396. const gap = touchGap(t1, t2)
  397. rescale(initScale * gap / initGap, centerPoint)
  398. e.preventDefault()
  399. }
  400. }
  401. function handleTouchEnd(e) {
  402. if (mode == TOUCHPAN) {
  403. const t = findTouch(e.changedTouches, touchid)
  404. if (t == null) return
  405. panMove(t.clientX, t.clientY)
  406. setMode(IDLE)
  407. e.preventDefault()
  408. if (!moved) clickHandler(t.target)
  409. } else if (mode == TOUCHZOOM) {
  410. setMode(IDLE)
  411. e.preventDefault()
  412. }
  413. }
  414. svg.addEventListener("mousedown", handleScanStart)
  415. svg.addEventListener("mouseup", handleScanEnd)
  416. svg.addEventListener("touchstart", handleTouchStart)
  417. svg.addEventListener("touchmove", handleTouchMove)
  418. svg.addEventListener("touchend", handleTouchEnd)
  419. svg.addEventListener("wheel", handleWheel, true)
  420. }
  421. function viewer(baseUrl, nodes) {
  422. 'use strict';
  423. // Elements
  424. const search = document.getElementById("searchbox")
  425. const graph0 = document.getElementById("graph0")
  426. const svg = (graph0 == null ? null : graph0.parentElement)
  427. const toptable = document.getElementById("toptable")
  428. let regexpActive = false
  429. let selected = new Map()
  430. let origFill = new Map()
  431. let searchAlarm = null
  432. let buttonsEnabled = true
  433. function handleDetails() {
  434. const detailsText = document.getElementById("detailtext")
  435. if (detailsText != null) detailsText.style.display = "block"
  436. }
  437. function handleCloseDetails() {
  438. const detailsText = document.getElementById("detailtext")
  439. if (detailsText != null) detailsText.style.display = "none"
  440. }
  441. function handleKey(e) {
  442. if (e.keyCode != 13) return
  443. window.location.href =
  444. updateUrl(new URL({{.BaseURL}}, window.location.href), "f")
  445. e.preventDefault()
  446. }
  447. function handleSearch() {
  448. // Delay expensive processing so a flurry of key strokes is handled once.
  449. if (searchAlarm != null) {
  450. clearTimeout(searchAlarm)
  451. }
  452. searchAlarm = setTimeout(selectMatching, 300)
  453. regexpActive = true
  454. updateButtons()
  455. }
  456. function selectMatching() {
  457. searchAlarm = null
  458. let re = null
  459. if (search.value != "") {
  460. try {
  461. re = new RegExp(search.value)
  462. } catch (e) {
  463. // TODO: Display error state in search box
  464. return
  465. }
  466. }
  467. function match(text) {
  468. return re != null && re.test(text)
  469. }
  470. // drop currently selected items that do not match re.
  471. selected.forEach(function(v, n) {
  472. if (!match(nodes[n])) {
  473. unselect(n, document.getElementById("node" + n))
  474. }
  475. })
  476. // add matching items that are not currently selected.
  477. for (let n = 0; n < nodes.length; n++) {
  478. if (!selected.has(n) && match(nodes[n])) {
  479. select(n, document.getElementById("node" + n))
  480. }
  481. }
  482. updateButtons()
  483. }
  484. function toggleSvgSelect(elem) {
  485. // Walk up to immediate child of graph0
  486. while (elem != null && elem.parentElement != graph0) {
  487. elem = elem.parentElement
  488. }
  489. if (!elem) return
  490. // Disable regexp mode.
  491. regexpActive = false
  492. const n = nodeId(elem)
  493. if (n < 0) return
  494. if (selected.has(n)) {
  495. unselect(n, elem)
  496. } else {
  497. select(n, elem)
  498. }
  499. updateButtons()
  500. }
  501. function unselect(n, elem) {
  502. if (elem == null) return
  503. selected.delete(n)
  504. setBackground(elem, false)
  505. }
  506. function select(n, elem) {
  507. if (elem == null) return
  508. selected.set(n, true)
  509. setBackground(elem, true)
  510. }
  511. function nodeId(elem) {
  512. const id = elem.id
  513. if (!id) return -1
  514. if (!id.startsWith("node")) return -1
  515. const n = parseInt(id.slice(4), 10)
  516. if (isNaN(n)) return -1
  517. if (n < 0 || n >= nodes.length) return -1
  518. return n
  519. }
  520. function setBackground(elem, set) {
  521. // Handle table row highlighting.
  522. if (elem.nodeName == "TR") {
  523. elem.classList.toggle("hilite", set)
  524. return
  525. }
  526. // Handle svg element highlighting.
  527. const p = findPolygon(elem)
  528. if (p != null) {
  529. if (set) {
  530. origFill.set(p, p.style.fill)
  531. p.style.fill = "#ccccff"
  532. } else if (origFill.has(p)) {
  533. p.style.fill = origFill.get(p)
  534. }
  535. }
  536. }
  537. function findPolygon(elem) {
  538. if (elem.localName == "polygon") return elem
  539. for (const c of elem.children) {
  540. const p = findPolygon(c)
  541. if (p != null) return p
  542. }
  543. return null
  544. }
  545. // convert a string to a regexp that matches that string.
  546. function quotemeta(str) {
  547. return str.replace(/([\\\.?+*\[\](){}|^$])/g, '\\$1')
  548. }
  549. // Update id's href to reflect current selection whenever it is
  550. // liable to be followed.
  551. function makeLinkDynamic(id) {
  552. const elem = document.getElementById(id)
  553. if (elem == null) return
  554. // Most links copy current selection into the "f" parameter,
  555. // but Refine menu links are different.
  556. let param = "f"
  557. if (id == "ignore") param = "i"
  558. if (id == "hide") param = "h"
  559. if (id == "show") param = "s"
  560. // We update on mouseenter so middle-click/right-click work properly.
  561. elem.addEventListener("mouseenter", updater)
  562. elem.addEventListener("touchstart", updater)
  563. function updater() {
  564. elem.href = updateUrl(new URL(elem.href), param)
  565. }
  566. }
  567. // Update URL to reflect current selection.
  568. function updateUrl(url, param) {
  569. url.hash = ""
  570. // The selection can be in one of two modes: regexp-based or
  571. // list-based. Construct regular expression depending on mode.
  572. let re = regexpActive
  573. ? search.value
  574. : Array.from(selected.keys()).map(key => quotemeta(nodes[key])).join("|")
  575. // Copy params from this page's URL.
  576. const params = url.searchParams
  577. for (const p of new URLSearchParams(window.location.search)) {
  578. params.set(p[0], p[1])
  579. }
  580. if (re != "") {
  581. // For focus/show, forget old parameter. For others, add to re.
  582. if (param != "f" && param != "s" && params.has(param)) {
  583. const old = params.get(param)
  584. if (old != "") {
  585. re += "|" + old
  586. }
  587. }
  588. params.set(param, re)
  589. }
  590. return url.toString()
  591. }
  592. function handleTopClick(e) {
  593. // Walk back until we find TR and then get the Name column (index 5)
  594. let elem = e.target
  595. while (elem != null && elem.nodeName != "TR") {
  596. elem = elem.parentElement
  597. }
  598. if (elem == null || elem.children.length < 6) return
  599. e.preventDefault()
  600. const tr = elem
  601. const td = elem.children[5]
  602. if (td.nodeName != "TD") return
  603. const name = td.innerText
  604. const index = nodes.indexOf(name)
  605. if (index < 0) return
  606. // Disable regexp mode.
  607. regexpActive = false
  608. if (selected.has(index)) {
  609. unselect(index, elem)
  610. } else {
  611. select(index, elem)
  612. }
  613. updateButtons()
  614. }
  615. function updateButtons() {
  616. const enable = (search.value != "" || selected.size != 0)
  617. if (buttonsEnabled == enable) return
  618. buttonsEnabled = enable
  619. for (const id of ["focus", "ignore", "hide", "show"]) {
  620. const link = document.getElementById(id)
  621. if (link != null) {
  622. link.classList.toggle("disabled", !enable)
  623. }
  624. }
  625. }
  626. // Initialize button states
  627. updateButtons()
  628. // Setup event handlers
  629. if (svg != null) {
  630. initPanAndZoom(svg, toggleSvgSelect)
  631. }
  632. if (toptable != null) {
  633. toptable.addEventListener("mousedown", handleTopClick)
  634. toptable.addEventListener("touchstart", handleTopClick)
  635. }
  636. const ids = ["topbtn", "graphbtn", "peek", "list", "disasm",
  637. "focus", "ignore", "hide", "show"]
  638. ids.forEach(makeLinkDynamic)
  639. // Bind action to button with specified id.
  640. function addAction(id, action) {
  641. const btn = document.getElementById(id)
  642. if (btn != null) {
  643. btn.addEventListener("click", action)
  644. btn.addEventListener("touchstart", action)
  645. }
  646. }
  647. addAction("details", handleDetails)
  648. addAction("closedetails", handleCloseDetails)
  649. search.addEventListener("input", handleSearch)
  650. search.addEventListener("keydown", handleKey)
  651. // Give initial focus to main container so it can be scrolled using keys.
  652. const main = document.getElementById("bodycontainer")
  653. if (main) {
  654. main.focus()
  655. }
  656. }
  657. </script>
  658. {{end}}
  659. {{define "top" -}}
  660. <!DOCTYPE html>
  661. <html>
  662. <head>
  663. <meta charset="utf-8">
  664. <title>{{.Title}}</title>
  665. {{template "css" .}}
  666. <style type="text/css">
  667. </style>
  668. </head>
  669. <body>
  670. {{template "header" .}}
  671. <div id="bodycontainer">
  672. <table id="toptable">
  673. <tr>
  674. <th id="flathdr1">Flat
  675. <th id="flathdr2">Flat%
  676. <th>Sum%
  677. <th id="cumhdr1">Cum
  678. <th id="cumhdr2">Cum%
  679. <th id="namehdr">Name
  680. <th>Inlined?</tr>
  681. <tbody id="rows">
  682. </tbody>
  683. </table>
  684. </div>
  685. {{template "script" .}}
  686. <script>
  687. function makeTopTable(total, entries) {
  688. const rows = document.getElementById("rows")
  689. if (rows == null) return
  690. // Store initial index in each entry so we have stable node ids for selection.
  691. for (let i = 0; i < entries.length; i++) {
  692. entries[i].Id = "node" + i
  693. }
  694. // Which column are we currently sorted by and in what order?
  695. let currentColumn = ""
  696. let descending = false
  697. sortBy("Flat")
  698. function sortBy(column) {
  699. // Update sort criteria
  700. if (column == currentColumn) {
  701. descending = !descending // Reverse order
  702. } else {
  703. currentColumn = column
  704. descending = (column != "Name")
  705. }
  706. // Sort according to current criteria.
  707. function cmp(a, b) {
  708. const av = a[currentColumn]
  709. const bv = b[currentColumn]
  710. if (av < bv) return -1
  711. if (av > bv) return +1
  712. return 0
  713. }
  714. entries.sort(cmp)
  715. if (descending) entries.reverse()
  716. function addCell(tr, val) {
  717. const td = document.createElement('td')
  718. td.textContent = val
  719. tr.appendChild(td)
  720. }
  721. function percent(v) {
  722. return (v * 100.0 / total).toFixed(2) + "%"
  723. }
  724. // Generate rows
  725. const fragment = document.createDocumentFragment()
  726. let sum = 0
  727. for (const row of entries) {
  728. const tr = document.createElement('tr')
  729. tr.id = row.Id
  730. sum += row.Flat
  731. addCell(tr, row.FlatFormat)
  732. addCell(tr, percent(row.Flat))
  733. addCell(tr, percent(sum))
  734. addCell(tr, row.CumFormat)
  735. addCell(tr, percent(row.Cum))
  736. addCell(tr, row.Name)
  737. addCell(tr, row.InlineLabel)
  738. fragment.appendChild(tr)
  739. }
  740. rows.textContent = '' // Remove old rows
  741. rows.appendChild(fragment)
  742. }
  743. // Make different column headers trigger sorting.
  744. function bindSort(id, column) {
  745. const hdr = document.getElementById(id)
  746. if (hdr == null) return
  747. const fn = function() { sortBy(column) }
  748. hdr.addEventListener("click", fn)
  749. hdr.addEventListener("touch", fn)
  750. }
  751. bindSort("flathdr1", "Flat")
  752. bindSort("flathdr2", "Flat")
  753. bindSort("cumhdr1", "Cum")
  754. bindSort("cumhdr2", "Cum")
  755. bindSort("namehdr", "Name")
  756. }
  757. viewer({{.BaseURL}}, {{.Nodes}})
  758. makeTopTable({{.Total}}, {{.Top}})
  759. </script>
  760. </body>
  761. </html>
  762. {{end}}
  763. {{define "sourcelisting" -}}
  764. <!DOCTYPE html>
  765. <html>
  766. <head>
  767. <meta charset="utf-8">
  768. <title>{{.Title}}</title>
  769. {{template "css" .}}
  770. {{template "weblistcss" .}}
  771. {{template "weblistjs" .}}
  772. </head>
  773. <body>
  774. {{template "header" .}}
  775. <div id="bodycontainer">
  776. {{.HTMLBody}}
  777. </div>
  778. {{template "script" .}}
  779. <script>viewer({{.BaseURL}}, null)</script>
  780. </body>
  781. </html>
  782. {{end}}
  783. {{define "plaintext" -}}
  784. <!DOCTYPE html>
  785. <html>
  786. <head>
  787. <meta charset="utf-8">
  788. <title>{{.Title}}</title>
  789. {{template "css" .}}
  790. </head>
  791. <body>
  792. {{template "header" .}}
  793. <div id="bodycontainer">
  794. <pre>
  795. {{.TextBody}}
  796. </pre>
  797. </div>
  798. {{template "script" .}}
  799. <script>viewer({{.BaseURL}}, null)</script>
  800. </body>
  801. </html>
  802. {{end}}
  803. `))
  804. }