diff options
author | Alfredo Sumaran <alfredo@gitlab.com> | 2017-03-13 19:35:18 +0000 |
---|---|---|
committer | Alfredo Sumaran <alfredo@gitlab.com> | 2017-03-13 19:35:18 +0000 |
commit | 08f7e49d2980dcb23902cec3324a61dd8ba9c0e9 (patch) | |
tree | a7f023e704242bd944bc7d463562c5e77a4aa549 /app/assets/javascripts/blob | |
parent | cc4ca1f3fe2e2f7470c4fae9a9c5676bbebb18b6 (diff) | |
parent | c0242485393fe93397ee18889bc5345b67d5ea0d (diff) | |
download | gitlab-ce-08f7e49d2980dcb23902cec3324a61dd8ba9c0e9.tar.gz |
Merge branch '19742-permalink-blame-button-line-number-hash-links' into 'master'
Update permalink/blame buttons with line number fragment hash
Closes #19742
See merge request !9461
Diffstat (limited to 'app/assets/javascripts/blob')
-rw-r--r-- | app/assets/javascripts/blob/blob_line_permalink_updater.js | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/app/assets/javascripts/blob/blob_line_permalink_updater.js b/app/assets/javascripts/blob/blob_line_permalink_updater.js new file mode 100644 index 00000000000..c8f68860fbd --- /dev/null +++ b/app/assets/javascripts/blob/blob_line_permalink_updater.js @@ -0,0 +1,35 @@ +const lineNumberRe = /^L[0-9]+/; + +const updateLineNumbersOnBlobPermalinks = (linksToUpdate) => { + const hash = gl.utils.getLocationHash(); + if (hash && lineNumberRe.test(hash)) { + const hashUrlString = `#${hash}`; + + [].concat(Array.prototype.slice.call(linksToUpdate)).forEach((permalinkButton) => { + const baseHref = permalinkButton.getAttribute('data-original-href') || (() => { + const href = permalinkButton.getAttribute('href'); + permalinkButton.setAttribute('data-original-href', href); + return href; + })(); + permalinkButton.setAttribute('href', `${baseHref}${hashUrlString}`); + }); + } +}; + +function BlobLinePermalinkUpdater(blobContentHolder, lineNumberSelector, elementsToUpdate) { + const updateBlameAndBlobPermalinkCb = () => { + // Wait for the hash to update from the LineHighlighter callback + setTimeout(() => { + updateLineNumbersOnBlobPermalinks(elementsToUpdate); + }, 0); + }; + + blobContentHolder.addEventListener('click', (e) => { + if (e.target.matches(lineNumberSelector)) { + updateBlameAndBlobPermalinkCb(); + } + }); + updateBlameAndBlobPermalinkCb(); +} + +export default BlobLinePermalinkUpdater; |