-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathscript.js
194 lines (185 loc) · 8.06 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright 2023 The TensorFlow Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ============================================================================
//
// Display warning banner if use of analytics cookies hasn't been acknowledged
if (location.hostname !== "" && !Cookies.get("tf-cookies-accepted")) {
$(".tf-cookie-warning").removeClass("d-none")
}
$("#tf-accept-cookies").click(function() {
$(".tf-cookie-warning").addClass("d-none")
Cookies.set("tf-cookies-accepted", true, { expires: 365 })
})
// Navbar toggle switches
// When the page loads or when the navbar toggles are switched, toggle the
// class on the HTML <body> tag that controls those features (colorblind or
// show-the-build-cop-section support).
if (localStorage.getItem('tf-colorblind') == 'true') {
$('#tf-colorblind').prop('checked', true)
$('body').toggleClass('tf-colorblind')
}
if (localStorage.getItem('tf-show-buildcop') == 'true') {
$('#tf-show-buildcop').prop('checked', true)
$('body').toggleClass('tf-show-buildcop')
}
// Note: $(this) in jQuery yields the element upon which this event is called
$('#tf-colorblind').change(function () {
localStorage.setItem('tf-colorblind', $(this).prop('checked'))
$('body').toggleClass('tf-colorblind')
})
$('#tf-show-buildcop').change(function () {
localStorage.setItem('tf-show-buildcop', $(this).prop('checked'))
$('body').toggleClass('tf-show-buildcop')
})
// Update the human-readable timestamp once per minute and when the page loads
// Uses moment.js, which is included in the top of the HTML page
function humanizeTimestamp () {
const time_ago = moment($('#tf-now').attr('data-isonow'), moment.ISO_8601).fromNow()
$('#tf-ago').text(`(${time_ago})`)
}
setInterval(humanizeTimestamp, 60500)
humanizeTimestamp()
// Check every minute if it's time to refresh the page, which is controlled
// by a flag that gets set after five minutes. Delay the refresh if there is
// currently a modal open, because that means someone is probably interacting
// with the page right now and would be annoyed their view changed (because of
// the way the line highlighting works, no job is highlighted when the page
// refreshes, even though the same modal pops up).
let autoRefreshASAP = false
let modalIsOpen = false
function refreshIfNeeded () {
if (!modalIsOpen && autoRefreshASAP) {
location.reload()
}
}
setInterval(refreshIfNeeded, 60500)
setInterval(function () { autoRefreshASAP = true }, 300000)
// MODAL HANDLING
//
// We hook into the modals' open and close events to track whether any modal
// is open (to avoid refreshing the page, see above) and to highlight the
// clicked-on job (which is a very nice quality-of-life feature).
//
// See:
// - https://getbootstrap.com/docs/5.2/components/modal/#via-javascript
// - https://getbootstrap.com/docs/5.2/components/modal/#methods
// - https://getbootstrap.com/docs/5.2/components/modal/#events
//
// NOTE: These all go into jQuery's $(function() {}), which executes the content
// only after the whole page has loaded and the DOM is ready. This allows us
// to have all of the scripts in one file: since the script is loaded just
// after the navbar is placed, the code above can modify the page for styles
// and card placement before the cards and modals (which take up most of the
// page size) are loaded. This means there's generally no flashing when the page
// loads.
lastClicked = null
$(function () {
// Highlight the clicked job when a modal appears (doesn't work when a commit
// is linked directly).
$('.tf-commit-modal').on('show.bs.modal', function (e) {
modalIsOpen = true
// Outline the last-clicked dot so you can see which you looked at last
if (lastClicked !== null) {
lastClicked.removeClass("tf-last-clicked")
}
lastClicked = $(e.relatedTarget)
lastClicked.addClass("tf-last-clicked")
job_name = $(lastClicked).closest('.card').attr('data-name')
$(this).find('td span').filter(function () {
return $(this).text() === job_name
}).closest('tr').toggleClass('tf-selected-row')
$(this).attr('data-tf-job-name', job_name)
// Set the window location hash to this commit ID without corrupting history
history.replaceState(undefined, undefined, '#' + $(this).attr('id'))
})
// Undo the previous when the modal is hidden
$('.tf-commit-modal').on('hidden.bs.modal', function () {
modalIsOpen = false
const job_name = $(this).attr('data-tf-job-name')
$(this).find('td span').filter(function () {
return $(this).text() === job_name
}).closest('tr').toggleClass('tf-selected-row')
// Set the window location hash to nothing (remove the #...) without
// corrupting history
history.replaceState(null, null, ' ')
})
// When the page loads, check the location hash, and...
// - If there's a matching category on the page, scroll to it
// - If there is a commit ID in the location hash, show that commit
// - If there is a CL in the location hash, show that CL's commit
// - If there is a PR in the location hash, show that PR's commit
unescaped = decodeURIComponent(window.location.hash).replace("#", "").replace("+", "")
if (window.location.hash.length <= 1) {
// Nothing to do if no hash in the URL
// If the hash is exactly 41 chars (hash sign # plus a 40-char sha hash),
// just show that modal.
} else if (window.location.hash.length == 41) {
if ($(window.location.hash).length) {
new bootstrap.Modal(window.location.hash).show()
} else {
new bootstrap.Modal("#tf-no-commit-modal").show()
}
// If the hash matches a Category on the page, then scroll to it.
} else if (document.getElementById(unescaped)) {
// Google Chrome, at least, does not scroll if the tab is opened in the
// background, so we try and scroll a moment after the first time the tab
// gets focus, or immediately if it has focus already.
if (document.hasFocus()) {
document.getElementById(unescaped).scrollIntoView();
} else {
let scrolled = false
$(window).focus(function() {
if (!scrolled) {
setTimeout(function () {
document.getElementById(unescaped).scrollIntoView();
}, 100);
scrolled = true
}
})
}
// And if it's not a commit sha, it's either a PR or a CL, so try and find a
// modal matching that PR number if the length is short (CLs will always
// be nine or more characters)
} else if (window.location.hash.length < 10) {
let pr = $(".modal p:first-child").filter(function() {
return $(this).text().indexOf("Merge pull request " + window.location.hash) >= 0;
})
const modal_id = "#" + pr.closest(".modal").attr('id')
if ($(modal_id).length) {
new bootstrap.Modal(modal_id).show()
} else {
new bootstrap.Modal("#tf-no-pr-modal").show()
}
// And if it's neither, it's a CL, so try and find a modal
// matching that CL number
} else {
const cl = window.location.hash.substring(1).replace("cl/", "")
const modal_id = '#' + $(`.modal[data-cl=${cl}]`).attr('id')
if ($(modal_id).length) {
new bootstrap.Modal(modal_id).show()
} else {
new bootstrap.Modal("#tf-no-cl-modal").show()
}
}
// When a user clicks a "Reveal All" button in a commit view, reveal all
// badges on all job cards that refer to that same commit.
let revealed = null
$(".tf-reveal").click(function() {
if (revealed !== null) {
$(".badge[data-bs-target='" + revealed + "']").removeClass("tf-revealed")
}
revealed = $(this).attr("data-tf-reveal")
$(".badge[data-bs-target='" + revealed + "']").removeClass("tf-last-clicked").addClass("tf-revealed")
})
})