annotate mercurial/templates/static/followlines.js @ 32995:7c82bfd55d47

hgweb: parameterize the tag name of elements holding followlines selection While plugging followlines.js into "annotate" view, we'll need to walk a different DOM structure from that of "filerevision" view. In particular, the selectable source line element is a <tr> in annotate view (in contrast with a <span> in filerevision view). So make this tag name a parameter of followlines.js script by passing its value as a "selectabletag" data attribute of <pre class="sourcelines"> element. As <pre class="sourcelines"> tags are getting quite long in templates, rewrite them on several lines.
author Denis Laxalde <denis.laxalde@logilab.fr>
date Wed, 21 Jun 2017 17:07:51 +0200
parents 1cf0b651fa70
children 32331f54930c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
31785
d15c9feb4399 hgweb: rename linerangelog.js as followlines.js
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31784
diff changeset
1 // followlines.js - JavaScript utilities for followlines UI
31758
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
2 //
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
3 // Copyright 2017 Logilab SA <contact@logilab.fr>
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
4 //
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
5 // This software may be used and distributed according to the terms of the
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
6 // GNU General Public License version 2 or any later version.
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
7
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
8 //** Install event listeners for line block selection and followlines action */
31783
70377de005a0 hgweb: use a function expression for the install listener of followlines UI
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31758
diff changeset
9 document.addEventListener('DOMContentLoaded', function() {
31758
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
10 var sourcelines = document.getElementsByClassName('sourcelines')[0];
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
11 if (typeof sourcelines === 'undefined') {
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
12 return;
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
13 }
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
14 // URL to complement with "linerange" query parameter
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
15 var targetUri = sourcelines.dataset.logurl;
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
16 if (typeof targetUri === 'undefined') {
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
17 return;
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
18 }
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
19
32995
7c82bfd55d47 hgweb: parameterize the tag name of elements holding followlines selection
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 32071
diff changeset
20 // Tag of children of "sourcelines" element on which to add "line
7c82bfd55d47 hgweb: parameterize the tag name of elements holding followlines selection
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 32071
diff changeset
21 // selection" style.
7c82bfd55d47 hgweb: parameterize the tag name of elements holding followlines selection
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 32071
diff changeset
22 var selectableTag = sourcelines.dataset.selectabletag;
7c82bfd55d47 hgweb: parameterize the tag name of elements holding followlines selection
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 32071
diff changeset
23 if (typeof selectableTag === 'undefined') {
7c82bfd55d47 hgweb: parameterize the tag name of elements holding followlines selection
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 32071
diff changeset
24 return;
7c82bfd55d47 hgweb: parameterize the tag name of elements holding followlines selection
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 32071
diff changeset
25 }
7c82bfd55d47 hgweb: parameterize the tag name of elements holding followlines selection
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 32071
diff changeset
26
32070
a298f5c61b34 hgweb: do not show "descending" link in followlines UI for filelog heads
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31940
diff changeset
27 var isHead = parseInt(sourcelines.dataset.ishead || "0");
a298f5c61b34 hgweb: do not show "descending" link in followlines UI for filelog heads
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31940
diff changeset
28
31848
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
29 // tooltip to invite on lines selection
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
30 var tooltip = document.createElement('div');
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
31 tooltip.id = 'followlines-tooltip';
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
32 tooltip.classList.add('hidden');
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
33 var initTooltipText = 'click to start following lines history from here';
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
34 tooltip.textContent = initTooltipText;
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
35 sourcelines.appendChild(tooltip);
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
36
31849
5c1abb4bd3ee hgweb: position the "followlines" box close to latest cursor position
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31848
diff changeset
37 //* position "element" on top-right of cursor */
5c1abb4bd3ee hgweb: position the "followlines" box close to latest cursor position
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31848
diff changeset
38 function positionTopRight(element, event) {
5c1abb4bd3ee hgweb: position the "followlines" box close to latest cursor position
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31848
diff changeset
39 var x = (event.clientX + 10) + 'px',
5c1abb4bd3ee hgweb: position the "followlines" box close to latest cursor position
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31848
diff changeset
40 y = (event.clientY - 20) + 'px';
5c1abb4bd3ee hgweb: position the "followlines" box close to latest cursor position
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31848
diff changeset
41 element.style.top = y;
5c1abb4bd3ee hgweb: position the "followlines" box close to latest cursor position
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31848
diff changeset
42 element.style.left = x;
5c1abb4bd3ee hgweb: position the "followlines" box close to latest cursor position
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31848
diff changeset
43 }
5c1abb4bd3ee hgweb: position the "followlines" box close to latest cursor position
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31848
diff changeset
44
31848
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
45 var tooltipTimeoutID;
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
46 //* move the "tooltip" with cursor (top-right) and show it after 1s */
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
47 function moveAndShowTooltip(e) {
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
48 if (typeof tooltipTimeoutID !== 'undefined') {
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
49 // avoid accumulation of timeout callbacks (blinking)
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
50 window.clearTimeout(tooltipTimeoutID);
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
51 }
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
52 tooltip.classList.add('hidden');
31849
5c1abb4bd3ee hgweb: position the "followlines" box close to latest cursor position
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31848
diff changeset
53 positionTopRight(tooltip, e);
31848
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
54 tooltipTimeoutID = window.setTimeout(function() {
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
55 tooltip.classList.remove('hidden');
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
56 }, 1000);
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
57 }
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
58
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
59 // on mousemove, show tooltip close to cursor position
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
60 sourcelines.addEventListener('mousemove', moveAndShowTooltip);
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
61
32995
7c82bfd55d47 hgweb: parameterize the tag name of elements holding followlines selection
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 32071
diff changeset
62 // retrieve all direct *selectable* children of class="sourcelines"
7c82bfd55d47 hgweb: parameterize the tag name of elements holding followlines selection
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 32071
diff changeset
63 // element
7c82bfd55d47 hgweb: parameterize the tag name of elements holding followlines selection
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 32071
diff changeset
64 var selectableElements = Array.prototype.filter.call(
31758
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
65 sourcelines.children,
32995
7c82bfd55d47 hgweb: parameterize the tag name of elements holding followlines selection
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 32071
diff changeset
66 function(x) { return x.tagName === selectableTag });
31758
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
67
31784
02eb52e9d413 hgweb: rely on a specific class to change cursor type in followlines UI
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31783
diff changeset
68 // add a "followlines-select" class to change cursor type in CSS
32995
7c82bfd55d47 hgweb: parameterize the tag name of elements holding followlines selection
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 32071
diff changeset
69 for (var i = 0; i < selectableElements.length; i++) {
7c82bfd55d47 hgweb: parameterize the tag name of elements holding followlines selection
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 32071
diff changeset
70 selectableElements[i].classList.add('followlines-select');
31784
02eb52e9d413 hgweb: rely on a specific class to change cursor type in followlines UI
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31783
diff changeset
71 }
02eb52e9d413 hgweb: rely on a specific class to change cursor type in followlines UI
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31783
diff changeset
72
31758
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
73 var lineSelectedCSSClass = 'followlines-selected';
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
74
32995
7c82bfd55d47 hgweb: parameterize the tag name of elements holding followlines selection
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 32071
diff changeset
75 //** add CSS class on selectable elements in `from`-`to` line range */
31758
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
76 function addSelectedCSSClass(from, to) {
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
77 for (var i = from; i <= to; i++) {
32995
7c82bfd55d47 hgweb: parameterize the tag name of elements holding followlines selection
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 32071
diff changeset
78 selectableElements[i].classList.add(lineSelectedCSSClass);
31758
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
79 }
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
80 }
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
81
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
82 //** remove CSS class from previously selected lines */
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
83 function removeSelectedCSSClass() {
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
84 var elements = sourcelines.getElementsByClassName(
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
85 lineSelectedCSSClass);
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
86 while (elements.length) {
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
87 elements[0].classList.remove(lineSelectedCSSClass);
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
88 }
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
89 }
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
90
32995
7c82bfd55d47 hgweb: parameterize the tag name of elements holding followlines selection
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 32071
diff changeset
91 // ** return the element of type "selectableTag" parent of `element` */
7c82bfd55d47 hgweb: parameterize the tag name of elements holding followlines selection
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 32071
diff changeset
92 function selectableParent(element) {
31758
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
93 var parent = element.parentElement;
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
94 if (parent === null) {
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
95 return null;
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
96 }
32995
7c82bfd55d47 hgweb: parameterize the tag name of elements holding followlines selection
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 32071
diff changeset
97 if (element.tagName == selectableTag && parent.isSameNode(sourcelines)) {
31758
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
98 return element;
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
99 }
32995
7c82bfd55d47 hgweb: parameterize the tag name of elements holding followlines selection
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 32071
diff changeset
100 return selectableParent(parent);
31758
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
101 }
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
102
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
103 //** event handler for "click" on the first line of a block */
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
104 function lineSelectStart(e) {
32995
7c82bfd55d47 hgweb: parameterize the tag name of elements holding followlines selection
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 32071
diff changeset
105 var startElement = selectableParent(e.target);
31758
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
106 if (startElement === null) {
32995
7c82bfd55d47 hgweb: parameterize the tag name of elements holding followlines selection
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 32071
diff changeset
107 // not a "selectable" element (maybe <a>): abort, keeping event
7c82bfd55d47 hgweb: parameterize the tag name of elements holding followlines selection
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 32071
diff changeset
108 // listener registered for other click with a "selectable" target
31758
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
109 return;
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
110 }
31848
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
111
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
112 // update tooltip text
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
113 tooltip.textContent = 'click again to terminate line block selection here';
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
114
31758
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
115 var startId = parseInt(startElement.id.slice(1));
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
116 startElement.classList.add(lineSelectedCSSClass); // CSS
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
117
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
118 // remove this event listener
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
119 sourcelines.removeEventListener('click', lineSelectStart);
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
120
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
121 //** event handler for "click" on the last line of the block */
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
122 function lineSelectEnd(e) {
32995
7c82bfd55d47 hgweb: parameterize the tag name of elements holding followlines selection
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 32071
diff changeset
123 var endElement = selectableParent(e.target);
31758
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
124 if (endElement === null) {
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
125 // not a <span> (maybe <a>): abort, keeping event listener
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
126 // registered for other click with <span> target
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
127 return;
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
128 }
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
129
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
130 // remove this event listener
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
131 sourcelines.removeEventListener('click', lineSelectEnd);
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
132
31848
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
133 // hide tooltip and disable motion tracking
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
134 tooltip.classList.add('hidden');
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
135 sourcelines.removeEventListener('mousemove', moveAndShowTooltip);
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
136 window.clearTimeout(tooltipTimeoutID);
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
137
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
138 //* restore initial "tooltip" state */
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
139 function restoreTooltip() {
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
140 tooltip.textContent = initTooltipText;
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
141 sourcelines.addEventListener('mousemove', moveAndShowTooltip);
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
142 }
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
143
31758
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
144 // compute line range (startId, endId)
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
145 var endId = parseInt(endElement.id.slice(1));
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
146 if (endId == startId) {
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
147 // clicked twice the same line, cancel and reset initial state
31848
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
148 // (CSS, event listener for selection start, tooltip)
31758
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
149 removeSelectedCSSClass();
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
150 sourcelines.addEventListener('click', lineSelectStart);
31848
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
151 restoreTooltip();
31758
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
152 return;
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
153 }
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
154 var inviteElement = endElement;
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
155 if (endId < startId) {
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
156 var tmp = endId;
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
157 endId = startId;
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
158 startId = tmp;
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
159 inviteElement = startElement;
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
160 }
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
161
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
162 addSelectedCSSClass(startId - 1, endId -1); // CSS
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
163
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
164 // append the <div id="followlines"> element to last line of the
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
165 // selection block
32070
a298f5c61b34 hgweb: do not show "descending" link in followlines UI for filelog heads
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31940
diff changeset
166 var divAndButton = followlinesBox(targetUri, startId, endId, isHead);
31758
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
167 var div = divAndButton[0],
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
168 button = divAndButton[1];
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
169 inviteElement.appendChild(div);
31849
5c1abb4bd3ee hgweb: position the "followlines" box close to latest cursor position
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31848
diff changeset
170 // set position close to cursor (top-right)
5c1abb4bd3ee hgweb: position the "followlines" box close to latest cursor position
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31848
diff changeset
171 positionTopRight(div, e);
31758
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
172
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
173 //** event handler for cancelling selection */
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
174 function cancel() {
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
175 // remove invite box
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
176 div.parentNode.removeChild(div);
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
177 // restore initial event listeners
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
178 sourcelines.addEventListener('click', lineSelectStart);
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
179 sourcelines.removeEventListener('click', cancel);
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
180 // remove styles on selected lines
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
181 removeSelectedCSSClass();
31848
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
182 // restore tooltip element
7160bdd56b84 hgweb: add a floating tooltip to invite on followlines action
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31785
diff changeset
183 restoreTooltip();
31758
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
184 }
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
185
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
186 // bind cancel event to click on <button>
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
187 button.addEventListener('click', cancel);
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
188 // as well as on an click on any source line
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
189 sourcelines.addEventListener('click', cancel);
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
190 }
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
191
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
192 sourcelines.addEventListener('click', lineSelectEnd);
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
193
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
194 }
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
195
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
196 sourcelines.addEventListener('click', lineSelectStart);
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
197
31783
70377de005a0 hgweb: use a function expression for the install listener of followlines UI
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31758
diff changeset
198 //** return a <div id="followlines"> and inner cancel <button> elements */
32070
a298f5c61b34 hgweb: do not show "descending" link in followlines UI for filelog heads
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31940
diff changeset
199 function followlinesBox(targetUri, fromline, toline, isHead) {
31783
70377de005a0 hgweb: use a function expression for the install listener of followlines UI
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31758
diff changeset
200 // <div id="followlines">
70377de005a0 hgweb: use a function expression for the install listener of followlines UI
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31758
diff changeset
201 var div = document.createElement('div');
70377de005a0 hgweb: use a function expression for the install listener of followlines UI
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31758
diff changeset
202 div.id = 'followlines';
31758
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
203
31783
70377de005a0 hgweb: use a function expression for the install listener of followlines UI
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31758
diff changeset
204 // <div class="followlines-cancel">
70377de005a0 hgweb: use a function expression for the install listener of followlines UI
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31758
diff changeset
205 var buttonDiv = document.createElement('div');
70377de005a0 hgweb: use a function expression for the install listener of followlines UI
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31758
diff changeset
206 buttonDiv.classList.add('followlines-cancel');
31758
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
207
31783
70377de005a0 hgweb: use a function expression for the install listener of followlines UI
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31758
diff changeset
208 // <button>x</button>
70377de005a0 hgweb: use a function expression for the install listener of followlines UI
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31758
diff changeset
209 var button = document.createElement('button');
70377de005a0 hgweb: use a function expression for the install listener of followlines UI
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31758
diff changeset
210 button.textContent = 'x';
70377de005a0 hgweb: use a function expression for the install listener of followlines UI
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31758
diff changeset
211 buttonDiv.appendChild(button);
70377de005a0 hgweb: use a function expression for the install listener of followlines UI
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31758
diff changeset
212 div.appendChild(buttonDiv);
31758
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
213
31783
70377de005a0 hgweb: use a function expression for the install listener of followlines UI
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31758
diff changeset
214 // <div class="followlines-link">
70377de005a0 hgweb: use a function expression for the install listener of followlines UI
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31758
diff changeset
215 var aDiv = document.createElement('div');
70377de005a0 hgweb: use a function expression for the install listener of followlines UI
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31758
diff changeset
216 aDiv.classList.add('followlines-link');
31940
6ce09d2cc2db hgweb: add a link to followlines in descending direction
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31849
diff changeset
217 aDiv.textContent = 'follow history of lines ' + fromline + ':' + toline + ':';
6ce09d2cc2db hgweb: add a link to followlines in descending direction
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31849
diff changeset
218 var linesep = document.createElement('br');
6ce09d2cc2db hgweb: add a link to followlines in descending direction
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31849
diff changeset
219 aDiv.appendChild(linesep);
6ce09d2cc2db hgweb: add a link to followlines in descending direction
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31849
diff changeset
220 // link to "ascending" followlines
6ce09d2cc2db hgweb: add a link to followlines in descending direction
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31849
diff changeset
221 var aAsc = document.createElement('a');
31783
70377de005a0 hgweb: use a function expression for the install listener of followlines UI
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31758
diff changeset
222 var url = targetUri + '?patch=&linerange=' + fromline + ':' + toline;
31940
6ce09d2cc2db hgweb: add a link to followlines in descending direction
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31849
diff changeset
223 aAsc.setAttribute('href', url);
32071
1cf0b651fa70 hgweb: change text of followlines links to "older / newer"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 32070
diff changeset
224 aAsc.textContent = 'older';
31940
6ce09d2cc2db hgweb: add a link to followlines in descending direction
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31849
diff changeset
225 aDiv.appendChild(aAsc);
32070
a298f5c61b34 hgweb: do not show "descending" link in followlines UI for filelog heads
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31940
diff changeset
226
a298f5c61b34 hgweb: do not show "descending" link in followlines UI for filelog heads
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31940
diff changeset
227 if (!isHead) {
a298f5c61b34 hgweb: do not show "descending" link in followlines UI for filelog heads
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31940
diff changeset
228 var sep = document.createTextNode(' / ');
a298f5c61b34 hgweb: do not show "descending" link in followlines UI for filelog heads
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31940
diff changeset
229 aDiv.appendChild(sep);
a298f5c61b34 hgweb: do not show "descending" link in followlines UI for filelog heads
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31940
diff changeset
230 // link to "descending" followlines
a298f5c61b34 hgweb: do not show "descending" link in followlines UI for filelog heads
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31940
diff changeset
231 var aDesc = document.createElement('a');
a298f5c61b34 hgweb: do not show "descending" link in followlines UI for filelog heads
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31940
diff changeset
232 aDesc.setAttribute('href', url + '&descend=');
32071
1cf0b651fa70 hgweb: change text of followlines links to "older / newer"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 32070
diff changeset
233 aDesc.textContent = 'newer';
32070
a298f5c61b34 hgweb: do not show "descending" link in followlines UI for filelog heads
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31940
diff changeset
234 aDiv.appendChild(aDesc);
a298f5c61b34 hgweb: do not show "descending" link in followlines UI for filelog heads
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31940
diff changeset
235 }
31940
6ce09d2cc2db hgweb: add a link to followlines in descending direction
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31849
diff changeset
236
31783
70377de005a0 hgweb: use a function expression for the install listener of followlines UI
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31758
diff changeset
237 div.appendChild(aDiv);
31758
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
238
31783
70377de005a0 hgweb: use a function expression for the install listener of followlines UI
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31758
diff changeset
239 return [div, button];
70377de005a0 hgweb: use a function expression for the install listener of followlines UI
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31758
diff changeset
240 }
31758
04ec317b8128 hgweb: expose a followlines UI in filerevision view
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
241
31783
70377de005a0 hgweb: use a function expression for the install listener of followlines UI
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31758
diff changeset
242 }, false);