# HG changeset patch # User Gregory Szorc # Date 2017-09-30 08:01:36 # Node ID 6797f1fbc6426c7ee691f3ee21610d33e4825c61 # Parent f6492f482c60d486e9bdcd9346c5581ed20d5799 hgweb: add HTML elements to control whitespace settings for annotate Building on top of the new URL query string arguments to control whitespace settings for annotate, this commit adds HTML checkboxes reflecting the values of these arguments to the paper and gitweb themes. The actual diff settings are now exported to the templating layer. The HTML templates add these as data-* attributes so they are accessible to the DOM. A new
with various elements is added. The is initially hidden via CSS. A shared JavaScript function (which runs after the has been rendered but before the annotate HTML (because annotate HTML could take a while to load and we want the form to render quickly) takes care of setting the checked state of each box from the data-* attributes. It also registers an event handler to modify the URL and refresh the page whenever the checkbox state is changed. I'm using the URLSearchParams interface to perform URL manipulation. https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams tells me this may not be supported on older web browsers. Yes, apparently the web API didn't have a standard API to parse and format query strings until recently. Hence the check for the presence of this feature in the JavaScript. If the browser doesn't support the feature, the will remain hidden and behavior will like it currently is. We could polyfill this feature or implement our own query string parsing. But I'm lazy and this could be done as a follow-up if people miss it. We could certainly expand this feature to support more diff options (such as lines of context). That's why the potentially reusable code is stored in a reusable place. It is also certainly possible to add diff controls to other pages that display diffs. But since Mozillians are making noise about controlling which revisions annotate shows, I figured I'd start there. .. feature:: Control whitespace settings for annotation on hgweb /annotate URLs on hgweb now accept query string arguments to influence how whitespace changes impact results. The arguments "ignorews," "ignorewsamount," "ignorewseol," and "ignoreblanklines" now have the same meaning as their [annotate] config section counterparts. Any provided setting overrides the server default. HTML checkboxes have been added to the paper and gitweb themes to expose current whitespace settings and to easily modify the current view. Differential Revision: https://phab.mercurial-scm.org/D850 diff --git a/mercurial/hgweb/webcommands.py b/mercurial/hgweb/webcommands.py --- a/mercurial/hgweb/webcommands.py +++ b/mercurial/hgweb/webcommands.py @@ -930,6 +930,9 @@ def annotate(web, req, tmpl): "linenumber": "% 6d" % (lineno + 1), "revdate": f.date()} + diffopts = webutil.difffeatureopts(req, web.repo.ui, 'annotate') + diffopts = {k: getattr(diffopts, k) for k in diffopts.defaults} + return tmpl("fileannotate", file=f, annotate=annotate, @@ -938,6 +941,7 @@ def annotate(web, req, tmpl): rename=webutil.renamelink(fctx), permissions=fctx.manifest().flags(f), ishead=int(ishead), + diffopts=diffopts, **webutil.commonentry(web.repo, fctx)) @webcommand('filelog') diff --git a/mercurial/templates/gitweb/fileannotate.tmpl b/mercurial/templates/gitweb/fileannotate.tmpl --- a/mercurial/templates/gitweb/fileannotate.tmpl +++ b/mercurial/templates/gitweb/fileannotate.tmpl @@ -62,6 +62,13 @@ annotate |
{desc|strip|escape|websub|nonempty}
+ +{diffoptsform} + + +
+ Ignore whitespace changes - + Everywhere: + + Within whitespace: + + At end of lines: + + + ' diff --git a/mercurial/templates/paper/fileannotate.tmpl b/mercurial/templates/paper/fileannotate.tmpl --- a/mercurial/templates/paper/fileannotate.tmpl +++ b/mercurial/templates/paper/fileannotate.tmpl @@ -65,6 +65,12 @@
+{diffoptsform} + + +
diff --git a/mercurial/templates/paper/map b/mercurial/templates/paper/map --- a/mercurial/templates/paper/map +++ b/mercurial/templates/paper/map @@ -251,3 +251,18 @@ searchform = ' ' searchhint = 'Find changesets by keywords (author, files, the commit message), revision number or hash, or revset expression.' + +diffoptsform = ' + + Ignore whitespace changes - + Everywhere: + + Within whitespace: + + At end of lines: + + ' diff --git a/mercurial/templates/static/mercurial.js b/mercurial/templates/static/mercurial.js --- a/mercurial/templates/static/mercurial.js +++ b/mercurial/templates/static/mercurial.js @@ -434,6 +434,56 @@ function ajaxScrollInit(urlFormat, scrollHandler(); } +function renderDiffOptsForm() { + // We use URLSearchParams for query string manipulation. Old browsers don't + // support this API. + if (!("URLSearchParams" in window)) { + return; + } + + var form = document.getElementById("diffopts-form"); + + var KEYS = [ + "ignorews", + "ignorewsamount", + "ignorewseol", + "ignoreblanklines", + ]; + + var urlParams = new URLSearchParams(window.location.search); + + function updateAndRefresh(e) { + var checkbox = e.target; + var name = checkbox.id.substr(0, checkbox.id.indexOf("-")); + urlParams.set(name, checkbox.checked ? "1" : "0"); + window.location.search = urlParams.toString(); + } + + var allChecked = form.getAttribute("data-ignorews") == "1"; + + for (var i = 0; i < KEYS.length; i++) { + var key = KEYS[i]; + + var checkbox = document.getElementById(key + "-checkbox"); + if (!checkbox) { + continue; + } + + currentValue = form.getAttribute("data-" + key); + checkbox.checked = currentValue != "0"; + + // ignorews implies ignorewsamount and ignorewseol. + if (allChecked && (key == "ignorewsamount" || key == "ignorewseol")) { + checkbox.checked = true; + checkbox.disabled = true; + } + + checkbox.addEventListener("change", updateAndRefresh, false); + } + + form.style.display = 'block'; +} + document.addEventListener('DOMContentLoaded', function() { process_dates(); }, false); diff --git a/mercurial/templates/static/style-gitweb.css b/mercurial/templates/static/style-gitweb.css --- a/mercurial/templates/static/style-gitweb.css +++ b/mercurial/templates/static/style-gitweb.css @@ -97,6 +97,12 @@ div.annotate-info { } div.annotate-info a { color: #0000FF; text-decoration: underline; } td.annotate:hover div.annotate-info { display: inline; } + +#diffopts-form { + padding-left: 8px; + display: none; +} + .linenr { color:#999999; text-decoration:none } div.rss_logo { float: right; white-space: nowrap; } div.rss_logo a { diff --git a/mercurial/templates/static/style-paper.css b/mercurial/templates/static/style-paper.css --- a/mercurial/templates/static/style-paper.css +++ b/mercurial/templates/static/style-paper.css @@ -226,6 +226,13 @@ div.annotate-info { div.annotate-info a { color: #0000FF; } td.annotate:hover div.annotate-info { display: inline; } +#diffopts-form { + font-size: smaller; + color: #424242; + padding-bottom: 10px; + display: none; +} + .source, .sourcefirst { font-family: monospace; white-space: pre; diff --git a/tests/test-hgweb.t b/tests/test-hgweb.t --- a/tests/test-hgweb.t +++ b/tests/test-hgweb.t @@ -340,7 +340,7 @@ static file $ get-with-headers.py --twice localhost:$HGPORT 'static/style-gitweb.css' - date etag server 200 Script output follows - content-length: 9007 + content-length: 9066 content-type: text/css body { font-family: sans-serif; font-size: 12px; border:solid #d9d8d1; border-width:1px; margin:10px; background: white; color: black; } @@ -442,6 +442,12 @@ static file } div.annotate-info a { color: #0000FF; text-decoration: underline; } td.annotate:hover div.annotate-info { display: inline; } + + #diffopts-form { + padding-left: 8px; + display: none; + } + .linenr { color:#999999; text-decoration:none } div.rss_logo { float: right; white-space: nowrap; } div.rss_logo a { diff --git a/tests/test-highlight.t b/tests/test-highlight.t --- a/tests/test-highlight.t +++ b/tests/test-highlight.t @@ -284,6 +284,25 @@ hgweb fileannotate, html
+ +
+ Ignore whitespace changes - + Everywhere: + + Within whitespace: + + At end of lines: + +
+ + +