Changeset View
Changeset View
Standalone View
Standalone View
swh/web/assets/src/bundles/browse/swh-ids-utils.js
Show All 10 Lines | |||||
import {BREAKPOINT_SM} from 'utils/constants'; | import {BREAKPOINT_SM} from 'utils/constants'; | ||||
export function swhIdObjectTypeToggled(event) { | export function swhIdObjectTypeToggled(event) { | ||||
event.preventDefault(); | event.preventDefault(); | ||||
$(event.target).tab('show'); | $(event.target).tab('show'); | ||||
} | } | ||||
export function swhIdOptionOriginToggled(event) { | export function swhIdContextOptionToggled(event) { | ||||
event.stopPropagation(); | event.stopPropagation(); | ||||
let swhIdElt = $(event.target).closest('.swh-id-ui').find('.swh-id'); | let swhIdElt = $(event.target).closest('.swh-id-ui').find('.swh-id'); | ||||
let originPart = ';origin=' + $(event.target).data('swh-origin'); | let swhIdWithContext = $(event.target).data('swhid-with-context'); | ||||
let currentSwhId = swhIdElt.text(); | let currentSwhId = swhIdElt.text(); | ||||
if ($(event.target).prop('checked')) { | if ($(event.target).prop('checked')) { | ||||
if (currentSwhId.indexOf(originPart) === -1) { | currentSwhId = swhIdWithContext; | ||||
currentSwhId += originPart; | |||||
} | |||||
} else { | } else { | ||||
currentSwhId = currentSwhId.replace(originPart, ''); | const pos = currentSwhId.indexOf(';'); | ||||
if (pos !== -1) { | |||||
currentSwhId = currentSwhId.slice(0, pos); | |||||
} | |||||
} | } | ||||
swhIdElt.text(currentSwhId); | swhIdElt.text(currentSwhId); | ||||
swhIdElt.attr('href', '/' + currentSwhId + '/'); | swhIdElt.attr('href', '/' + currentSwhId + '/'); | ||||
addLinesInfo(); | |||||
} | } | ||||
function setIdLinesPart(elt) { | function addLinesInfo() { | ||||
let swhIdElt = $(elt).closest('.swh-id-ui').find('.swh-id'); | let swhIdElt = $('#swh-id-tab-content').find('.swh-id'); | ||||
let currentSwhId = swhIdElt.text(); | let currentSwhId = swhIdElt.text(); | ||||
let lines = []; | let lines = []; | ||||
let linesPart = ';lines='; | let linesPart = ';lines='; | ||||
let linesRegexp = new RegExp(/L(\d+)/g); | let linesRegexp = new RegExp(/L(\d+)/g); | ||||
let line = linesRegexp.exec(window.location.hash); | let line = linesRegexp.exec(window.location.hash); | ||||
while (line) { | while (line) { | ||||
lines.push(parseInt(line[1])); | lines.push(parseInt(line[1])); | ||||
line = linesRegexp.exec(window.location.hash); | line = linesRegexp.exec(window.location.hash); | ||||
} | } | ||||
if (lines.length > 0) { | if (lines.length > 0) { | ||||
linesPart += lines[0]; | linesPart += lines[0]; | ||||
} | } | ||||
if (lines.length > 1) { | if (lines.length > 1) { | ||||
linesPart += '-' + lines[1]; | linesPart += '-' + lines[1]; | ||||
} | } | ||||
if ($(elt).prop('checked')) { | |||||
if ($('#swh-id-context-option-content').prop('checked')) { | |||||
currentSwhId = currentSwhId.replace(/;lines=\d+-*\d*/g, ''); | currentSwhId = currentSwhId.replace(/;lines=\d+-*\d*/g, ''); | ||||
if (lines.length > 0) { | |||||
currentSwhId += linesPart; | currentSwhId += linesPart; | ||||
} else { | |||||
currentSwhId = currentSwhId.replace(linesPart, ''); | |||||
} | } | ||||
swhIdElt.text(currentSwhId); | swhIdElt.text(currentSwhId); | ||||
swhIdElt.attr('href', '/' + currentSwhId + '/'); | swhIdElt.attr('href', '/' + currentSwhId + '/'); | ||||
} | } | ||||
export function swhIdOptionLinesToggled(event) { | |||||
event.stopPropagation(); | |||||
if (!window.location.hash) { | |||||
return; | |||||
} | |||||
setIdLinesPart(event.target); | |||||
} | } | ||||
$(document).ready(() => { | $(document).ready(() => { | ||||
new ClipboardJS('.btn-swh-id-copy', { | new ClipboardJS('.btn-swh-id-copy', { | ||||
text: trigger => { | text: trigger => { | ||||
let swhId = $(trigger).closest('.swh-id-ui').find('.swh-id').text(); | let swhId = $(trigger).closest('.swh-id-ui').find('.swh-id').text(); | ||||
return swhId; | return swhId; | ||||
} | } | ||||
Show All 27 Lines | if (window.innerHeight < 600 || window.innerWidth < 500) { | ||||
tabSlideOptions['otherOffset'] = '20px'; | tabSlideOptions['otherOffset'] = '20px'; | ||||
} | } | ||||
// initiate the sliding identifiers tab | // initiate the sliding identifiers tab | ||||
$('#swh-identifiers').tabSlideOut(tabSlideOptions); | $('#swh-identifiers').tabSlideOut(tabSlideOptions); | ||||
// set the tab visible once the close animation is terminated | // set the tab visible once the close animation is terminated | ||||
$('#swh-identifiers').css('display', 'block'); | $('#swh-identifiers').css('display', 'block'); | ||||
$('.swh-id-option-origin').trigger('click'); | $('.swh-id-context-option').trigger('click'); | ||||
$('.swh-id-option-lines').trigger('click'); | |||||
// highlighted code lines changed | |||||
$(window).on('hashchange', () => { | $(window).on('hashchange', () => { | ||||
setIdLinesPart('.swh-id-option-lines'); | addLinesInfo(); | ||||
}); | |||||
// highlighted code lines removed | |||||
$('body').click(() => { | |||||
addLinesInfo(); | |||||
}); | }); | ||||
}); | }); |