List for cgit developers and users
 help / color / mirror / Atom feed
From: andy at warmcat.com (Andy Green)
Subject: [PATCH v6 6/7] line-range-highlight: burger menu and popup menu
Date: Fri, 29 Jun 2018 09:40:44 +0800	[thread overview]
Message-ID: <153023644478.27756.16650094961912982381.stgit@mail.warmcat.com> (raw)
In-Reply-To: <153023597876.27756.7476186508120329174.stgit@mail.warmcat.com>

Clicking on the line numbers to control the highlight,
or visiting a #URL link, causes a burger menu to
appear on the left of either the top of the range
or the bottom if that was last clicked.

Clicking this brings up a popup menu with

Copy Lines (implemented in next patch)
Copy Link (copies complete #URL to clipboard)
View / Remove Blame (changes to view to have or not have blame)

Clicking outside the popup menu clears it, but the burger
menu is sticky.

This is an entirely clientside implementation in
cgit.css and cgit.js only.  If JS disabled at server
or at client, it cannot provide this functionality and
operates with one-line browser URLs as before.

Tested on Linux Chrome 67 + Firefox 60.

Signed-off-by: Andy Green <andy at warmcat.com>
---
 cgit.css |   71 +++++++++++++++++++++
 cgit.js  |  206 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 264 insertions(+), 13 deletions(-)

diff --git a/cgit.css b/cgit.css
index 45ba615..093e0ce 100644
--- a/cgit.css
+++ b/cgit.css
@@ -382,7 +382,76 @@ div#cgit div.selected-lines {
 	pointer-events: none;
 	z-index: 1;
 	background-color: rgba(255, 255, 0, 0);
-	transition: background-color 1.5s;
+	transition: background-color 0.5s;
+}
+
+div#cgit div.selected-line-link-highlight {
+	background-color: yellow;
+}
+
+div#cgit div.selected-lines-popup {
+	position: absolute;
+	text-align: center;
+	font-size: 6px;
+	font-color: black;
+	left: 6px;
+	width: 24px;
+	height: 12px;
+	opacity: 0;
+	z-index: 2;
+	padding-top: 4px;
+	padding-bottom: 0px;
+	cursor: pointer;
+	filter: grayscale(100%);
+	border: 1px solid gray;
+	border-radius: 3px;
+	background-color: rgba(255, 255, 255, 0.0);
+	transition: opacity 0.5s;
+}
+
+div#cgit div.selected-lines-popup:before {
+	content: "\26AB\26AB\26AB";
+}
+
+div#cgit div.popup-menu {
+    display: inline-block;
+	position: relative;
+	text-align: left;
+	font-size: 12px;
+	color: black;
+	left: 8px;
+	opacity: 0;
+	z-index: 3;
+	margin: 3px;
+	padding: 3px;
+	cursor: pointer;
+	border: 1px solid gray;
+	border-radius: 3px;
+	background-color: white;
+	white-space: nowrap;
+	box-shadow: 0px 5px 15px gray;
+	transition: opacity 0.3s;
+}
+
+div#cgit div.popup-menu ul {
+	list-style-type: none;
+	left: 0;
+	margin: 3px;
+	padding: 3px;
+}
+
+div#cgit div.popup-menu li {
+	margin: 3px;
+	padding: 3px;
+	font-size: 14px;
+}
+
+div#cgit div.popup-menu li:active {
+	margin: 3px;
+	padding: 3px;
+	font-size: 14px;
+	background-color: blue;
+	color: white;
 }
 
 div#cgit table.bin-blob {
diff --git a/cgit.js b/cgit.js
index 015dd76..7fae9aa 100644
--- a/cgit.js
+++ b/cgit.js
@@ -8,6 +8,8 @@
 
 (function () {
 
+var burger, menu_popup;
+
 function collect_offsetTop(e1)
 {
 	var t = 0;
@@ -29,7 +31,15 @@ function find_parent_of_type(e, type)
 	return e;
 }
 
-function line_range_highlight()
+/*
+ * This creates an absolute div as a child of the content table.
+ * It's horizontally and vertically aligned and sized according
+ * to the #URL information like #n123-456
+ * 
+ * If the highlight div already exists, it's removed and remade.
+ */
+
+function line_range_highlight(do_burger)
 {
 	var h = window.location.hash, l1 = 0, l2 = 0, e, t;
 
@@ -39,7 +49,8 @@ function line_range_highlight()
 		while (l1 <= e.l2) {
 			var e1;
 			e1 = document.getElementById('n' + l1++);
-			e1.style.backgroundColor = null;
+				e1.classList.remove(
+					'selected-line-link-highlight');
 		}
 
 		e.remove();
@@ -63,6 +74,9 @@ function line_range_highlight()
 	if (!e)
 		return;
 
+	if (do_burger)
+		burger_create(e);
+
 	de = document.createElement("DIV");
 
 	de.className = "selected-lines";
@@ -77,10 +91,8 @@ function line_range_highlight()
 
 	de.style.width = etr.offsetWidth + 'px';
 
-	/* the table is offset from the left, the highlight
-	 * needs to follow it */
+	/* the table is offset from the left, the highlight needs to follow it */
 	etable = find_parent_of_type(etr, "table");
-
 	de.style.left = etable.offsetLeft + 'px';
 	de.style.height = ((l2 - l1 + 1) * e.offsetHeight) + 'px';
 
@@ -92,7 +104,8 @@ function line_range_highlight()
 
 	n = l1;
 	while (n <= l2)
-		document.getElementById('n' + n++).style.backgroundColor = "yellow";
+		document.getElementById('n' + n++).classList.add(
+					'selected-line-link-highlight');
 
 	hl = (window.innerHeight / (e.offsetHeight + 1));
 	v = (l1 + ((l2 - l1) / 2)) - (hl / 2);
@@ -108,15 +121,184 @@ function line_range_highlight()
 	t.scrollIntoView(true);
 }
 
+function copy_clipboard(value)
+{
+	var inp = document.createElement("textarea");
+	var e = document.getElementById("linenumbers");
+
+	inp.type = "text";
+	inp.value = value;
+	/* hidden style stops it working for clipboard */
+	inp.setAttribute('readonly', '');
+	inp.style.position = "absolute";
+	inp.style.left = "-1000px";
+
+	e.appendChild(inp);
+
+	inp.select();
+
+	document.execCommand("copy");
+
+	inp.remove();
+}
+
+/*
+ * An element in the popup menu was clicked, perform the appropriate action
+ */
+function mi_click(e) {
+	var u, n;
+
+	e.stopPropagation();
+	e.preventDefault();
+
+	switch (e.target.id) {
+	case "mi-c-line":
+		/* implemented in next patch */
+		break;
+	case "mi-c-link":
+		copy_clipboard(window.location.href);
+		break;
+	case "mi-c-blame":
+		u = window.location.href;
+		t = u.indexOf("/tree/");
+		if (t)
+			window.location = u.substring(0, t) + "/blame/" +
+				u.substring(t + 6);
+		break;
+	case "mi-c-tree":
+		u = window.location.href;
+		t = u.indexOf("/blame/");
+		if (t)
+			window.location = u.substring(0, t) + "/tree/" +
+				u.substring(t + 7);
+		break;
+	}
+
+	if (!menu_popup)
+		return;
+
+	menu_popup.remove();
+	menu_popup = null;
+}
+
+/* We got a click on the (***) burger menu */
+
+function burger_click(e) {
+	var e1 = e, etable, d = new Date, s = "", n, is_blame,
+	    ar = new Array("mi-c-line", "mi-c-link", "mi-c-blame", "mi-c-tree"),
+	    an = new Array("Copy Lines", "Copy Link",
+			   "View Blame", /* 2: shown in /tree/ */
+			   "Remove Blame" /* 3: shown in /blame/ */);
+
+	e.preventDefault();
+
+	if (menu_popup) {
+		menu_popup.remove();
+		menu_popup = null;
+
+		return;
+	}
+
+	/*
+	 * Create the popup menu
+	 */
+
+	is_blame = !!document.getElementsByClassName("hashes").length;
+
+	menu_popup = document.createElement("DIV");
+	menu_popup.className = "popup-menu";
+	menu_popup.style.top = collect_offsetTop(e1) + e.offsetHeight + "px";
+
+	s = "<ul id='menu-ul'>";
+	for (n = 0; n < an.length; n++)
+		if (n < 2 || is_blame == (n == 3))
+			s += "<li id='" + ar[n] + "' tabindex='" + n + "'>" +
+				an[n] + "</li>";
+		    
+	menu_popup.innerHTML = s;
+
+	burger.insertBefore(menu_popup, null);
+
+        document.getElementById(ar[0]).focus();
+	for (n = 0; n < an.length; n++)
+		if (n < 2 || is_blame == (n == 3))
+			document.getElementById(ar[n]).
+				addEventListener("click", mi_click);
+				
+	setTimeout(function() {
+		menu_popup.style.opacity = "1";
+	}, 1);
+
+	/* detect loss of focus for popup menu */
+	menu_popup.addEventListener("focusout", function(e) {
+		/* if focus went to a child (menu item), ignore */
+		if (e.relatedTarget &&
+		    e.relatedTarget.parentNode.id == "menu-ul")
+			return;
+
+		menu_popup.remove();
+		menu_popup = null;
+	});
+}
+
+function burger_create(e)
+{
+	var e1 = e, etable, d = new Date;
+
+	if (burger)
+		burger.remove();
+
+	burger = document.createElement("DIV");
+	burger.className = "selected-lines-popup";
+	burger.style.top = collect_offsetTop(e1) + "px";
+
+	/* event listener cannot override default browser #URL behaviour */
+	burger.onclick = burger_click;
+
+	etable = find_parent_of_type(e, "table");
+	etable.insertBefore(burger, etable.firstChild);
+	burger_time = d.getTime();
+
+	setTimeout(function() {
+		burger.style.opacity = "1";
+	}, 1);
+}
+
+/*
+ * We got a click on a line number #url
+ *
+ * Create the "burger" menu there.
+ *
+ * Redraw the line range highlight accordingly.
+ */
+
 function line_range_click(e) {
-	var t, m, n = window.location.href.length - window.location.hash.length;
+	var t, elem, m, n = window.location.href.length -
+			    window.location.hash.length;
 
-	/* disable passthru to stop needless scrolling by default browser #URL handler */
+	/* disable passthru to stop scrolling by browser #URL handler */
 	e.stopPropagation();
 	e.preventDefault();
 
+	if (!e.target.id)
+		return;
+
+	if (menu_popup) {
+		menu_popup.remove();
+		menu_popup = null;
+
+		return;
+	}
+
+	elem = document.getElementById(e.target.id);
+	if (!elem)
+		return;
+
+	burger_create(elem);
+
 	if (!window.location.hash ||
-	    window.location.hash.indexOf("-") >= 0)
+	    window.location.hash.indexOf("-") >= 0 ||
+	    e.target.id.substring(1) == window.location.href.substring(n + 2))
 		t = window.location.href.substring(0, n) +
 		    '#n' + e.target.id.substring(1);
 	else {
@@ -131,12 +313,12 @@ function line_range_click(e) {
 
 	window.history.replaceState(null, null, t);
 
-	line_range_highlight();
+	line_range_highlight(0);
 }
 
 /* we have to use load, because header images can push the layout vertically */
 window.addEventListener("load", function() {
-	line_range_highlight();
+	line_range_highlight(1);
 }, false);
 
 document.addEventListener("DOMContentLoaded", function() {
@@ -148,7 +330,7 @@ document.addEventListener("DOMContentLoaded", function() {
 }, false);
 
 window.addEventListener("hashchange", function() {
-	line_range_highlight();
+	line_range_highlight(1);
 }, false);
 
 })();



  parent reply	other threads:[~2018-06-29  1:40 UTC|newest]

Thread overview: 73+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-20  9:39 Highlighting lines or line ranges in tree view andy
2018-06-21  5:42 ` [PATCH 1/3] ui-shared: introduce line range highlight javascript andy
2018-06-21  7:03   ` list
2018-06-21  7:30     ` andy
2018-06-21  5:42 ` [PATCH 2/3] ui-tree: use the line range highlight script andy
2018-06-21  5:43 ` [PATCH 3/3] ui-blame: " andy
2018-06-21  9:34 ` [PATCH v2 0/5] line range highlight andy
2018-06-21  9:34   ` [PATCH v2 1/5] config: add js andy
2018-06-23 10:20     ` john
2018-06-23 10:34       ` andy
2018-06-21  9:34   ` [PATCH v2 2/5] cgit.js: introduce andy
2018-06-23 10:18     ` john
2018-06-21  9:34   ` [PATCH v2 3/5] ui-shared: introduce line range highlight javascript andy
2018-06-23 10:17     ` john
2018-06-24  2:37       ` andy
2018-06-21  9:35   ` [PATCH v2 4/5] ui-tree: use the line range highlight script andy
2018-06-21  9:35   ` [PATCH v2 5/5] ui-blame: " andy
2018-06-22 23:01   ` [PATCH v2 1/2] cgit.js: make line range highlight responsive to url changes andy
2018-06-22 23:02   ` [PATCH v2 2/2] cgit.js: line range highlight: improve vertical scroll logic andy
2018-06-23  7:45   ` [PATCH v2] cgit.js: line range highlight: always hook hashchange in case hash added andy
2018-06-24  2:44 ` [PATCH v3 0/6] line range highlight andy
2018-06-24  2:44   ` [PATCH v3 1/6] config: add js andy
2018-06-24 11:01     ` john
2018-06-24  2:44   ` [PATCH v3 2/6] ui-shared: line range highlight: introduce javascript andy
2018-06-24 11:28     ` john
2018-06-25  2:04       ` andy
2018-06-24  2:44   ` [PATCH v3 3/6] cgit.js: line range highlight: make responsive to url changes andy
2018-06-24  2:44   ` [PATCH v3 4/6] cgit.js: line range highlight: improve vertical scroll logic andy
2018-06-24  2:44   ` [PATCH v3 5/6] line-range-highlight: onclick handler and range selection andy
2018-06-24 11:35     ` john
2018-06-25  2:07       ` andy
2018-06-24  2:44   ` [PATCH v3 6/6] line-range-highlight: copy URL to clipboard on click andy
2018-06-24 11:42     ` john
2018-06-24 12:00       ` andy
2018-06-24 13:39         ` john
2018-06-24 15:06           ` andy
2018-06-24 16:03             ` john
2018-06-25  0:46               ` andy
2018-06-25  5:49 ` [PATCH v4 0/6] line range highlight andy
2018-06-25  5:49   ` [PATCH v4 1/6] config: add js andy
2018-06-26  8:03     ` list
2018-06-25  5:49   ` [PATCH v4 2/6] cgit.js: line range highlight: introduce javascript andy
2018-06-25  5:49   ` [PATCH v4 3/6] cgit.js: line range highlight: make responsive to url changes andy
2018-06-25  5:50   ` [PATCH v4 4/6] cgit.js: line range highlight: improve vertical scroll logic andy
2018-06-25  5:50   ` [PATCH v4 5/6] line-range-highlight: onclick handler and range selection andy
2018-06-25  5:50   ` [PATCH v4 6/6] line-range-highlight: copy URL to clipboard UI andy
2018-06-26 11:25 ` [PATCH v5 0/6] line range highlight andy
2018-06-26 11:25   ` [PATCH v5 1/6] config: add js andy
2018-06-26 11:25   ` [PATCH v5 2/6] cgit.js: line range highlight: introduce javascript andy
2018-06-27 18:02     ` Jason
2018-06-27 21:45       ` andy
2018-06-28 23:58         ` [PATCH] cgit.css: add copyright lines andy
2018-06-26 11:25   ` [PATCH v5 3/6] cgit.js: line range highlight: make responsive to url changes andy
2018-06-26 11:25   ` [PATCH v5 4/6] cgit.js: line range highlight: improve vertical scroll logic andy
2018-06-26 11:25   ` [PATCH v5 5/6] line-range-highlight: onclick handler and range selection andy
2018-06-26 11:51     ` [PATCH v5-ninjaedit] " andy
2018-06-26 11:25   ` [PATCH v5 6/6] line-range-highlight: copy URL to clipboard UI andy
2018-06-27 18:07     ` Jason
2018-06-27 23:24       ` andy
2018-06-27 23:30         ` Jason
2018-06-27 23:38           ` andy
2018-06-29  1:39 ` [PATCH v6 0/7] line range highlight andy
2018-06-29  1:40   ` [PATCH v6 1/7] config: add js andy
2018-06-29  6:14     ` list
2018-06-29  6:16       ` [PATCH v6-ninjaedit] " andy
2018-06-29  6:31         ` [PATCH v6-ninjaedit2] " andy
2018-06-29  6:33         ` [PATCH v6-ninjaedit] " list
2018-06-29  1:40   ` [PATCH v6 2/7] cgit.js: line range highlight: introduce javascript andy
2018-06-29  1:40   ` [PATCH v6 3/7] cgit.js: line range highlight: make responsive to url changes andy
2018-06-29  1:40   ` [PATCH v6 4/7] cgit.js: line range highlight: improve vertical scroll logic andy
2018-06-29  1:40   ` [PATCH v6 5/7] line-range-highlight: onclick handler and range selection andy
2018-06-29  1:40   ` andy [this message]
2018-06-29  1:40   ` [PATCH v6 7/7] line-range-highlight: copy text andy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=153023644478.27756.16650094961912982381.stgit@mail.warmcat.com \
    --to=cgit@lists.zx2c4.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).