Skip to content

Commit 3354fb9

Browse files
committed
Migrate DOM ready code from util.js to init.js
util.js is not supposed to contain event listeners.
1 parent 434bc95 commit 3354fb9

File tree

2 files changed

+216
-218
lines changed

2 files changed

+216
-218
lines changed

share/static/js/init.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,3 +1061,100 @@ jQuery(document).on('change', 'form[name=BuildQuery] select[name^=SelectCustomFi
10611061
}
10621062
initializeSelectElements(row.get(0));
10631063
});
1064+
1065+
// Inline edit listeners
1066+
jQuery(document).on('click', 'table.inline-edit div.editable .edit-icon', function (e) {
1067+
var cell = jQuery(this).closest('div.editable');
1068+
if ( jQuery('div.editable.editing form').length ) {
1069+
cancelInlineEdit(jQuery('div.editable.editing form'));
1070+
}
1071+
const modal_info = cell.get(0).querySelector('.inline-edit-modal[data-link]');
1072+
if ( modal_info ) {
1073+
htmx.ajax('GET', modal_info.getAttribute('data-link'), '#dynamic-modal').then(() => {
1074+
bootstrap.Modal.getOrCreateInstance('#dynamic-modal').show();
1075+
jQuery(document).off('change', '#dynamic-modal form :input').on('change', '#dynamic-modal form :input', function () {
1076+
jQuery(this).closest('form').data('changed', true);
1077+
});
1078+
jQuery(document).off('click', '#dynamic-modal form .submit').on('click', '#dynamic-modal form .submit', function (evt) {
1079+
evt.preventDefault();
1080+
document.querySelectorAll('#dynamic-modal form textarea.richtext').forEach((textarea) => {
1081+
const name = textarea.name;
1082+
if ( RT.CKEditor.instances[name] ) {
1083+
if ( RT.CKEditor.instances[name].getData() !== textarea.value ) {
1084+
RT.CKEditor.instances[name].updateSourceElement();
1085+
jQuery(textarea.closest('form')).data('changed', true);
1086+
}
1087+
}
1088+
});
1089+
if ( jQuery('#dynamic-modal form').data('changed') ) {
1090+
cell.addClass('editing');
1091+
submitInlineEdit(jQuery('#dynamic-modal form'), cell);
1092+
}
1093+
else {
1094+
bootstrap.Modal.getInstance('#dynamic-modal')?.hide();
1095+
}
1096+
});
1097+
});
1098+
}
1099+
else {
1100+
beginInlineEdit(cell);
1101+
}
1102+
});
1103+
1104+
jQuery(document).on('mouseenter', 'table.inline-edit div.editable .edit-icon', function (e) {
1105+
const owner_dropdown_delay = jQuery(this).closest('.editable').find('div.select-owner-dropdown-delay:not(.loaded)');
1106+
loadOwnerDropdownDelay(owner_dropdown_delay);
1107+
});
1108+
1109+
jQuery(document).on('change', 'div.editable.editing form :input', function () {
1110+
jQuery(this).closest('form').data('changed', true);
1111+
});
1112+
1113+
jQuery(document).on('click', 'div.editable .cancel', function (e) {
1114+
cancelInlineEdit(jQuery(this).closest('form'));
1115+
});
1116+
1117+
jQuery(document).on('click', 'div.editable .submit', function (e) {
1118+
submitInlineEdit(jQuery(this).closest('form'));
1119+
});
1120+
1121+
// We want to call submitInlineEdit to do some pre-checks and massage
1122+
// css classes before making htmx requests. Can't bind it to form.submit
1123+
// event as preventDefault() there can't stop htmx actions.
1124+
jQuery(document).on('keydown', 'div.editable.editing form input[type=text], div.editable.editing form input:not([type])', function (e) {
1125+
if (e.key === 'Enter') {
1126+
e.preventDefault();
1127+
submitInlineEdit(jQuery(this).closest('form'));
1128+
}
1129+
});
1130+
1131+
jQuery(document).on('change', 'div.editable.editing form select:not([multiple])', function () {
1132+
submitInlineEdit(jQuery(this).closest('form'));
1133+
});
1134+
1135+
jQuery(function () {
1136+
1137+
// Make actions dropdown scrollable in case screen is too short
1138+
jQuery(window).resize(function() {
1139+
jQuery('#li-page-actions > ul').css('max-height', jQuery(window).height() - jQuery('#rt-header-container').height());
1140+
}).resize();
1141+
1142+
let expandCalendarResizeTimeout;
1143+
window.addEventListener('resize', () => {
1144+
clearTimeout(expandCalendarResizeTimeout);
1145+
expandCalendarResizeTimeout = setTimeout(() => {
1146+
expandCalendar(document);
1147+
}, 150);
1148+
});
1149+
1150+
const html = document.querySelector('html');
1151+
if ( html.getAttribute('data-bs-theme') === 'auto' ) {
1152+
if ( window.matchMedia("(prefers-color-scheme:dark)").matches ) {
1153+
html.setAttribute('data-bs-theme', 'dark');
1154+
}
1155+
else {
1156+
html.setAttribute('data-bs-theme', 'light');
1157+
}
1158+
}
1159+
1160+
});

0 commit comments

Comments
 (0)