From patchwork Thu Jun 22 14:40:11 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [2, of, 3] hgweb: parameterize the tag name of elements holding followlines selection From: Denis Laxalde X-Patchwork-Id: 21611 Message-Id: To: mercurial-devel@mercurial-scm.org Date: Thu, 22 Jun 2017 16:40:11 +0200 # HG changeset patch # User Denis Laxalde # Date 1498057671 -7200 # Wed Jun 21 17:07:51 2017 +0200 # Node ID d568b7b353ea6d8ba9c30b1c2d976a88f2962245 # Parent a67c2d37caf130131d7a5c659c9ec1428ee75451 # Available At http://hg.logilab.org/users/dlaxalde/hg # hg pull http://hg.logilab.org/users/dlaxalde/hg -r d568b7b353ea 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 in annotate view (in contrast with a in filerevision view). So make this tag name a parameter of followlines.js script by passing its value as a "selectabletag" data attribute of
 element.

As 
 tags are getting quite long in templates, rewrite
them on several lines.

diff --git a/mercurial/templates/gitweb/filerevision.tmpl b/mercurial/templates/gitweb/filerevision.tmpl
--- a/mercurial/templates/gitweb/filerevision.tmpl
+++ b/mercurial/templates/gitweb/filerevision.tmpl
@@ -66,7 +66,12 @@ file |
 
 
 
-
{text%fileline}
+
+{text%fileline}
+
diff --git a/mercurial/templates/paper/filerevision.tmpl b/mercurial/templates/paper/filerevision.tmpl --- a/mercurial/templates/paper/filerevision.tmpl +++ b/mercurial/templates/paper/filerevision.tmpl @@ -67,7 +67,12 @@
line wrap: on
line source
-
{text%fileline}
+
+{text%fileline}
+
diff --git a/mercurial/templates/static/followlines.js b/mercurial/templates/static/followlines.js --- a/mercurial/templates/static/followlines.js +++ b/mercurial/templates/static/followlines.js @@ -17,6 +17,13 @@ document.addEventListener('DOMContentLoa return; } + // Tag of children of "sourcelines" element on which to add "line + // selection" style. + var selectableTag = sourcelines.dataset.selectabletag; + if (typeof selectableTag === 'undefined') { + return; + } + var isHead = parseInt(sourcelines.dataset.ishead || "0"); // tooltip to invite on lines selection @@ -52,22 +59,23 @@ document.addEventListener('DOMContentLoa // on mousemove, show tooltip close to cursor position sourcelines.addEventListener('mousemove', moveAndShowTooltip); - // retrieve all direct children of
-    var spans = Array.prototype.filter.call(
+    // retrieve all direct *selectable* children of class="sourcelines"
+    // element
+    var selectableElements = Array.prototype.filter.call(
         sourcelines.children,
-        function(x) { return x.tagName === 'SPAN' });
+        function(x) { return x.tagName === selectableTag });
 
     // add a "followlines-select" class to change cursor type in CSS
-    for (var i = 0; i < spans.length; i++) {
-        spans[i].classList.add('followlines-select');
+    for (var i = 0; i < selectableElements.length; i++) {
+        selectableElements[i].classList.add('followlines-select');
     }
 
     var lineSelectedCSSClass = 'followlines-selected';
 
-    //** add CSS class on  element in `from`-`to` line range */
+    //** add CSS class on selectable elements in `from`-`to` line range */
     function addSelectedCSSClass(from, to) {
         for (var i = from; i <= to; i++) {
-            spans[i].classList.add(lineSelectedCSSClass);
+            selectableElements[i].classList.add(lineSelectedCSSClass);
         }
     }
 
@@ -80,24 +88,24 @@ document.addEventListener('DOMContentLoa
         }
     }
 
-    // ** return the  element parent of `element` */
-    function findParentSpan(element) {
+    // ** return the element of type "selectableTag" parent of `element` */
+    function selectableParent(element) {
         var parent = element.parentElement;
         if (parent === null) {
             return null;
         }
-        if (element.tagName == 'SPAN' && parent.isSameNode(sourcelines)) {
+        if (element.tagName == selectableTag && parent.isSameNode(sourcelines)) {
             return element;
         }
-        return findParentSpan(parent);
+        return selectableParent(parent);
     }
 
     //** event handler for "click" on the first line of a block */
     function lineSelectStart(e) {
-        var startElement = findParentSpan(e.target);
+        var startElement = selectableParent(e.target);
         if (startElement === null) {
-            // not a  (maybe ): abort, keeping event listener
-            // registered for other click with  target
+            // not a "selectable" element (maybe ): abort, keeping event
+            // listener registered for other click with a "selectable" target
             return;
         }
 
@@ -112,7 +120,7 @@ document.addEventListener('DOMContentLoa
 
         //** event handler for "click" on the last line of the block */
         function lineSelectEnd(e) {
-            var endElement = findParentSpan(e.target);
+            var endElement = selectableParent(e.target);
             if (endElement === null) {
                 // not a  (maybe ): abort, keeping event listener
                 // registered for other click with  target
diff --git a/tests/test-hgweb-commands.t b/tests/test-hgweb-commands.t
--- a/tests/test-hgweb-commands.t
+++ b/tests/test-hgweb-commands.t
@@ -1347,8 +1347,13 @@ File-related
   
line source
-
-  foo
+
+  
+  foo
+  
@@ -1476,8 +1481,13 @@ File-related
line wrap: on
line source
-
-  another
+
+  
+  another
+