diff mercurial/templates/static/mercurial.js @ 19430:5ec5097b4c0f

hgweb: add line wrapping switch to file source view This uses classList property, which is well-supported now: both Chromium 8.0+, Firefox 3.6+ and Opera 11.5+ support it, as well as relatively modern versions of other browsers.
author Alexander Plavin <me@aplavin.ru>
date Fri, 12 Jul 2013 15:58:13 +0400
parents c3cdba6e5d7f
children a63d3ff0d765
line wrap: on
line diff
--- a/mercurial/templates/static/mercurial.js	Tue Jul 09 02:08:24 2013 +0400
+++ b/mercurial/templates/static/mercurial.js	Fri Jul 12 15:58:13 2013 +0400
@@ -271,3 +271,29 @@
     document.getElementById('diffstatdetails').style.display = curexpand;
     document.getElementById('diffstatexpand').style.display = curdetails;
 }
+
+function toggleLinewrap() {
+    function getLinewrap() {
+        var nodes = document.getElementsByClassName('sourcelines');
+        // if there are no such nodes, error is thrown here
+        return nodes[0].classList.contains('wrap');
+    }
+
+    function setLinewrap(enable) {
+        var nodes = document.getElementsByClassName('sourcelines');
+        for (var i = 0; i < nodes.length; i++) {
+            if (enable) {
+                nodes[i].classList.add('wrap');
+            } else {
+                nodes[i].classList.remove('wrap');
+            }
+        }
+
+        var links = document.getElementsByClassName('linewraplink');
+        for (var i = 0; i < links.length; i++) {
+            links[i].innerHTML = enable ? 'on' : 'off';
+        }
+    }
+
+    setLinewrap(!getLinewrap());
+}