Andy Green | fe54918 | 2018-06-21 16:56:36 +0800 | [diff] [blame] | 1 | /* cgit.css: javacript functions for cgit |
| 2 | * |
| 3 | * Copyright (C) 2006-2018 cgit Development Team <cgit@lists.zx2c4.com> |
| 4 | * |
| 5 | * Licensed under GNU General Public License v2 |
| 6 | * (see COPYING for full license text) |
| 7 | */ |
| 8 | |
| 9 | (function () { |
| 10 | |
| 11 | function collect_offsetTop(e1) |
| 12 | { |
| 13 | var t = 0; |
| 14 | |
| 15 | while (e1) { |
| 16 | if (e1.offsetTop) |
| 17 | t += e1.offsetTop; |
| 18 | e1 = e1.offsetParent; |
| 19 | } |
| 20 | |
| 21 | return t; |
| 22 | } |
| 23 | |
| 24 | function find_parent_of_type(e, type) |
| 25 | { |
| 26 | while (e.tagName.toLowerCase() != type) |
| 27 | e = e.parentNode; |
| 28 | |
| 29 | return e; |
| 30 | } |
| 31 | |
| 32 | function line_range_highlight() |
| 33 | { |
| 34 | var h = window.location.hash, l1 = 0, l2 = 0, e, t; |
| 35 | |
Andy Green | 1915feb | 2018-06-23 06:07:45 +0800 | [diff] [blame] | 36 | e = document.getElementById("cgit-line-range"); |
| 37 | if (e) { |
| 38 | l1 = e.l1; |
| 39 | while (l1 <= e.l2) { |
| 40 | var e1; |
| 41 | e1 = document.getElementById('n' + l1++); |
| 42 | e1.style.backgroundColor = null; |
| 43 | } |
| 44 | |
| 45 | e.remove(); |
| 46 | } |
| 47 | |
Andy Green | fe54918 | 2018-06-21 16:56:36 +0800 | [diff] [blame] | 48 | l1 = parseInt(h.substring(2)); |
| 49 | if (!l1) |
| 50 | return; |
| 51 | |
| 52 | t = h.indexOf("-"); |
| 53 | l2 = l1; |
| 54 | if (t >= 1) |
| 55 | l2 = parseInt(h.substring(t + 1)); |
| 56 | |
| 57 | if (l2 < l1) |
| 58 | l2 = l1; |
| 59 | |
Andy Green | 0fcc450 | 2018-06-23 06:41:47 +0800 | [diff] [blame^] | 60 | var lh, etable, etr, de, n, hl, v; |
Andy Green | fe54918 | 2018-06-21 16:56:36 +0800 | [diff] [blame] | 61 | |
| 62 | e = document.getElementById('n' + l1); |
| 63 | if (!e) |
| 64 | return; |
| 65 | |
| 66 | de = document.createElement("DIV"); |
| 67 | |
| 68 | de.className = "selected-lines"; |
| 69 | de.style.bottom = e.style.bottom; |
| 70 | de.style.top = collect_offsetTop(e) + 'px'; |
Andy Green | 1915feb | 2018-06-23 06:07:45 +0800 | [diff] [blame] | 71 | de.id = "cgit-line-range"; |
Andy Green | fe54918 | 2018-06-21 16:56:36 +0800 | [diff] [blame] | 72 | de.l1 = l1; |
| 73 | de.l2 = l2; |
| 74 | |
| 75 | /* we will tack the highlight div at the parent tr */ |
| 76 | etr = find_parent_of_type(e, "tr"); |
| 77 | |
| 78 | de.style.width = etr.offsetWidth + 'px'; |
| 79 | |
| 80 | /* the table is offset from the left, the highlight |
| 81 | * needs to follow it */ |
| 82 | etable = find_parent_of_type(etr, "table"); |
| 83 | |
| 84 | de.style.left = etable.offsetLeft + 'px'; |
| 85 | de.style.height = ((l2 - l1 + 1) * e.offsetHeight) + 'px'; |
| 86 | |
| 87 | etr.insertBefore(de, etr.firstChild); |
| 88 | |
| 89 | setTimeout(function() { |
| 90 | de.style.backgroundColor = "rgba(255, 255, 0, 0.2)"; |
| 91 | }, 1); |
| 92 | |
| 93 | n = l1; |
| 94 | while (n <= l2) |
| 95 | document.getElementById('n' + n++).style.backgroundColor = "yellow"; |
| 96 | |
Andy Green | 0fcc450 | 2018-06-23 06:41:47 +0800 | [diff] [blame^] | 97 | hl = (window.innerHeight / (e.offsetHeight + 1)); |
| 98 | v = (l1 + ((l2 - l1) / 2)) - (hl / 2); |
| 99 | if (v > l1) |
| 100 | v = l1; |
| 101 | if (v < 1) |
| 102 | v = 1; |
| 103 | |
| 104 | t = document.getElementById('n' + Math.round(v)); |
| 105 | if (!t) |
| 106 | t = e; |
| 107 | |
| 108 | t.scrollIntoView(true); |
Andy Green | fe54918 | 2018-06-21 16:56:36 +0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /* we have to use load, because header images can push the layout vertically */ |
| 112 | window.addEventListener("load", function() { |
| 113 | line_range_highlight(); |
| 114 | }, false); |
| 115 | |
Andy Green | 1915feb | 2018-06-23 06:07:45 +0800 | [diff] [blame] | 116 | window.addEventListener("hashchange", function() { |
| 117 | line_range_highlight(); |
| 118 | }, false); |
| 119 | |
Andy Green | fe54918 | 2018-06-21 16:56:36 +0800 | [diff] [blame] | 120 | })(); |