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 | |
| 36 | l1 = parseInt(h.substring(2)); |
| 37 | if (!l1) |
| 38 | return; |
| 39 | |
| 40 | t = h.indexOf("-"); |
| 41 | l2 = l1; |
| 42 | if (t >= 1) |
| 43 | l2 = parseInt(h.substring(t + 1)); |
| 44 | |
| 45 | if (l2 < l1) |
| 46 | l2 = l1; |
| 47 | |
| 48 | var lh, etable, etr, de, n; |
| 49 | |
| 50 | e = document.getElementById('n' + l1); |
| 51 | if (!e) |
| 52 | return; |
| 53 | |
| 54 | de = document.createElement("DIV"); |
| 55 | |
| 56 | de.className = "selected-lines"; |
| 57 | de.style.bottom = e.style.bottom; |
| 58 | de.style.top = collect_offsetTop(e) + 'px'; |
| 59 | de.l1 = l1; |
| 60 | de.l2 = l2; |
| 61 | |
| 62 | /* we will tack the highlight div at the parent tr */ |
| 63 | etr = find_parent_of_type(e, "tr"); |
| 64 | |
| 65 | de.style.width = etr.offsetWidth + 'px'; |
| 66 | |
| 67 | /* the table is offset from the left, the highlight |
| 68 | * needs to follow it */ |
| 69 | etable = find_parent_of_type(etr, "table"); |
| 70 | |
| 71 | de.style.left = etable.offsetLeft + 'px'; |
| 72 | de.style.height = ((l2 - l1 + 1) * e.offsetHeight) + 'px'; |
| 73 | |
| 74 | etr.insertBefore(de, etr.firstChild); |
| 75 | |
| 76 | setTimeout(function() { |
| 77 | de.style.backgroundColor = "rgba(255, 255, 0, 0.2)"; |
| 78 | }, 1); |
| 79 | |
| 80 | n = l1; |
| 81 | while (n <= l2) |
| 82 | document.getElementById('n' + n++).style.backgroundColor = "yellow"; |
| 83 | |
| 84 | e.scrollIntoView(true); |
| 85 | } |
| 86 | |
| 87 | /* we have to use load, because header images can push the layout vertically */ |
| 88 | window.addEventListener("load", function() { |
| 89 | line_range_highlight(); |
| 90 | }, false); |
| 91 | |
| 92 | })(); |