From 11eec1df02156accf10fe1d7d7cc1e546615b8e9 Mon Sep 17 00:00:00 2001
From: Pierre-Yves Chibon <pingou@pingoured.fr>
Date: Jan 25 2016 11:15:37 +0000
Subject: [PATCH 1/3] Fix high-lighting a section of code in a PR


---

diff --git a/pagure/templates/pull_request.html b/pagure/templates/pull_request.html
index de8a826..3d14a7c 100644
--- a/pagure/templates/pull_request.html
+++ b/pagure/templates/pull_request.html
@@ -659,31 +659,6 @@ function setup_reply_btns() {
   setup_edit_btns();
   setup_reply_btns();
 
-  var cur_hash = null;
-
-  highlight_comment = function() {
-    var _hash = window.location.hash;
-    console.log(_hash);
-    if (_hash == '#commit_list' || _hash == '#request_diff') {
-        return false;
-    }
-    if (_hash != cur_hash) {
-      $( cur_hash ).css(
-        "background", "linear-gradient(to bottom, #ededed 0%, #fff 100%)"
-      );
-    };
-    cur_hash = _hash;
-    if ( _hash ) {
-      $( _hash ).css(
-        "background", "linear-gradient(to bottom, #eded98 0%, #fff 100%)"
-      );
-    };
-    return false;
-  };
-
-  $(window.onload=highlight_comment());
-  $(window).on('hashchange', highlight_comment);
-
   $(".comment_body").each(function(ind, obj) {
     var source = $(obj).html();
     var preview = emojione.toImage(source);
@@ -820,6 +795,39 @@ function try_async_comment(form, inline) {
 
 </script>
 
+<script type="text/javascript">
+function updateHighlight() {
+  var cls = "highlighted-line";
+  $('.' + cls).removeClass(cls)
+  if (location.hash !== '') {
+    var file = location.hash.substr(2).split(',')[0];
+    var lines = location.hash.substr(4).split('-').map(function (x) { return parseInt(x, 10) });
+    for (var i = lines[lines.length - 1]; i >= lines[0]; i--) {
+      $('#' + file + '_' + i).closest('tr').addClass(cls);
+    }
+  }
+}
+$(document).ready(updateHighlight);
+$(window).on('hashchange', updateHighlight);
+var selected = [];
+$("[data-line-number]").click(function (ev) {
+  var line = $(this).attr('data-line-number');
+  var file = $(this).attr('id').split('_')[0];
+  if (ev.shiftKey) {
+    selected = selected.slice(-1).concat(line);
+  } else {
+    selected = [line];
+  }
+
+  var hash = '_' + file + ',' + selected[0];
+  if (selected.length === 2) {
+    hash = '_' + file + ',' + Math.min(selected[0], selected[1]) + '-' + Math.max(selected[0], selected[1]);
+  }
+  window.location.hash = hash;
+  return false;
+});
+</script>
+
 
 {% if config['EVENTSOURCE_SOURCE'] and pull_request %}
 <script type="text/javascript"

From e3abb333ba46a04ef6e2f511b0e8e76a5579e7fa Mon Sep 17 00:00:00 2001
From: Pierre-Yves Chibon <pingou@pingoured.fr>
Date: Jan 25 2016 11:22:03 +0000
Subject: [PATCH 2/3] When loading a PR page, if there is a section selected, show the diff


This way, when one shares a link with someone to highlight a specific
section of the PR, the diff with the highlighted section is directly
visible to the person opening the page.

---

diff --git a/pagure/templates/pull_request.html b/pagure/templates/pull_request.html
index 3d14a7c..00c1b92 100644
--- a/pagure/templates/pull_request.html
+++ b/pagure/templates/pull_request.html
@@ -796,10 +796,16 @@ function try_async_comment(form, inline) {
 </script>
 
 <script type="text/javascript">
-function updateHighlight() {
+function updateHighlight(onload) {
   var cls = "highlighted-line";
   $('.' + cls).removeClass(cls)
   if (location.hash !== '') {
+    if (onload) {
+        $('#request_diff').addClass('active');
+        $('#comments').removeClass('active');
+        $('[href="#request_diff"]').addClass('active');
+        $('[href="#comments"]').removeClass('active');
+    }
     var file = location.hash.substr(2).split(',')[0];
     var lines = location.hash.substr(4).split('-').map(function (x) { return parseInt(x, 10) });
     for (var i = lines[lines.length - 1]; i >= lines[0]; i--) {
@@ -807,7 +813,7 @@ function updateHighlight() {
     }
   }
 }
-$(document).ready(updateHighlight);
+$(document).ready(updateHighlight(true));
 $(window).on('hashchange', updateHighlight);
 var selected = [];
 $("[data-line-number]").click(function (ev) {

From f92f160b5f90a6035633dbebde7b549a09feb383 Mon Sep 17 00:00:00 2001
From: Pierre-Yves Chibon <pingou@pingoured.fr>
Date: Jan 25 2016 14:31:12 +0000
Subject: [PATCH 3/3] Make sure the updateHighlight is called after the page finished loading


Thanks @ralph @threebean

---

diff --git a/pagure/templates/pull_request.html b/pagure/templates/pull_request.html
index 00c1b92..56e12b6 100644
--- a/pagure/templates/pull_request.html
+++ b/pagure/templates/pull_request.html
@@ -813,7 +813,7 @@ function updateHighlight(onload) {
     }
   }
 }
-$(document).ready(updateHighlight(true));
+$(document).ready(function () { updateHighlight(true) } );
 $(window).on('hashchange', updateHighlight);
 var selected = [];
 $("[data-line-number]").click(function (ev) {

