Skip to content

Commit 2d97bc4

Browse files
committed
fix(web): restore variable span and events lost during merge
1 parent 73f4e0f commit 2d97bc4

File tree

1 file changed

+112
-2
lines changed

1 file changed

+112
-2
lines changed

hanfor/static/js/requirements.js

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,8 +1043,118 @@ function load_requirement(row_idx) {
10431043

10441044
// Visible information
10451045
$("#requirement_modal_title").html(data.id + ": " + data.type)
1046-
$("#description_textarea").text(data.desc).change()
1047-
$("#add_guess_description").text(data.desc).change()
1046+
// Insert Markdown content
1047+
$('#description_textarea').html(data.desc);
1048+
initVariableSpanEvents();
1049+
1050+
function fetchUpdatedDescription() {
1051+
$.get(`/api/req/get/desc?id=${data.id}`)
1052+
.done((response) => {
1053+
// Update only the desc property of the existing data object
1054+
data.desc = response.desc;
1055+
1056+
// Update only the desc in the DOM
1057+
$('#description_textarea').html(data.desc);
1058+
1059+
// Rebind events to new content
1060+
initVariableSpanEvents();
1061+
})
1062+
.fail(() => {
1063+
alert('Failed to load updated description.');
1064+
});
1065+
}
1066+
1067+
1068+
function initVariableSpanEvents() {
1069+
// Handle left-click and right-click events on variable spans
1070+
$('#description_textarea').find('span[data-var-name]').each(function () {
1071+
const span = $(this);
1072+
span.css('cursor', 'pointer');
1073+
1074+
// Left click: copy variable name to clipboard
1075+
span.off('click').on('click', () => {
1076+
const varName = span.attr('data-var-name');
1077+
navigator.clipboard.writeText(varName).then(() => {
1078+
const originalBg = span.css('background-color');
1079+
span.css('background-color', 'yellow');
1080+
setTimeout(() => {
1081+
span.css('background-color', originalBg);
1082+
}, 1000);
1083+
});
1084+
});
1085+
1086+
// On right-click: open popup
1087+
span.off('contextmenu').on('contextmenu', (event) => {
1088+
event.preventDefault();
1089+
1090+
const originalVarName = span.attr('data-var-name');
1091+
$('#editVarPopup').css({
1092+
display: 'block',
1093+
top: event.clientY + 'px',
1094+
left: event.clientX + 'px'
1095+
});
1096+
1097+
$('#editVarInput').val(originalVarName).focus();
1098+
1099+
$('#cancelVarBtn').off('click').on('click', () => {
1100+
$('#editVarPopup').hide();
1101+
});
1102+
1103+
$('#saveSelectedBtn').off('click').on('click', () => {
1104+
const newVarName = $('#editVarInput').val().trim();
1105+
if (!newVarName || newVarName === originalVarName) {
1106+
$('#editVarPopup').hide();
1107+
return;
1108+
}
1109+
1110+
$.post('/api/req/variable_aliasing', {
1111+
type: 'single',
1112+
from: originalVarName,
1113+
to: newVarName,
1114+
rid: data.id
1115+
}).done(() => {
1116+
$('#editVarPopup').hide();
1117+
fetchUpdatedDescription();
1118+
}).fail(() => {
1119+
alert('Failed to update variable.');
1120+
});
1121+
});
1122+
1123+
$('#saveAllBtn').off('click').on('click', () => {
1124+
const newVarName = $('#editVarInput').val().trim();
1125+
if (!newVarName || newVarName === originalVarName) {
1126+
$('#editVarPopup').hide();
1127+
return;
1128+
}
1129+
1130+
$.post('/api/req/variable_aliasing', {
1131+
type: 'all',
1132+
from: originalVarName,
1133+
to: newVarName,
1134+
rid: data.id
1135+
}).done(() => {
1136+
$('#editVarPopup').hide();
1137+
fetchUpdatedDescription();
1138+
}).fail(() => {
1139+
alert('Failed to update all matching variables.');
1140+
});
1141+
});
1142+
});
1143+
});
1144+
}
1145+
1146+
// Close popup when clicking outside
1147+
$(document).on('mousedown', function (event) {
1148+
const $popup = $('#editVarPopup');
1149+
if (
1150+
$popup.is(':visible') &&
1151+
!$(event.target).closest('#editVarPopup, span[data-var-name]').length
1152+
) {
1153+
$popup.hide();
1154+
}
1155+
});
1156+
1157+
$('#add_guess_description').text(data.desc).change();
10481158

10491159
// Parse the formalizations
10501160
$("#formalization_accordion").html(data.formalizations_html)

0 commit comments

Comments
 (0)