Skip to content

Commit 05c3b84

Browse files
Show edit page confirmation dialog on tree view file change (#36130)
Currently, when editing or deleting a file and the edit/commit form has changes, navigating the file tree will discard all changes without any warning. This PR prevents partial reloading when the edit form has unsaved changes, which will trigger a browser native warning dialog. --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
1 parent b4c9057 commit 05c3b84

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

web_src/js/components/ViewFileTreeItem.vue

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script lang="ts" setup>
22
import {SvgIcon} from '../svg.ts';
33
import {isPlainClick} from '../utils/dom.ts';
4+
import {shouldTriggerAreYouSure} from '../vendor/jquery.are-you-sure.ts';
45
import {shallowRef} from 'vue';
56
import type {createViewFileTreeStore, FileTreeItem} from './ViewFileTreeStore.ts';
67
@@ -27,9 +28,10 @@ const doLoadChildren = async () => {
2728
};
2829
2930
const onItemClick = (e: MouseEvent) => {
30-
// only handle the click event with page partial reloading if the user didn't press any special key
31-
// let browsers handle special keys like "Ctrl+Click"
32-
if (!isPlainClick(e)) return;
31+
// only handle the click event with partial page reloading if both
32+
// - the user didn't press any special key like "Ctrl+Click" (which may have custom browser behavior)
33+
// - the editor/commit form isn't dirty (a full page reload shows a confirmation dialog if the form contains unsaved changes)
34+
if (!isPlainClick(e) || shouldTriggerAreYouSure()) return;
3335
e.preventDefault();
3436
if (props.item.entryMode === 'tree') doLoadChildren();
3537
store.navigateTreeView(props.item.fullPath);

web_src/js/vendor/jquery.are-you-sure.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// * use export to make it work with ES6 modules.
44
// * the addition of `const` to make it strict mode compatible.
55
// * ignore forms with "ignore-dirty" class, ignore hidden forms (closest('.tw-hidden'))
6+
// * extract the dirty check logic into a separate function
67

78
/*!
89
* jQuery Plugin: Are-You-Sure (Dirty Form Detection)
@@ -16,6 +17,9 @@
1617
* Version: 1.9.0
1718
* Date: 13th August 2014
1819
*/
20+
21+
const dataKeyAysSettings = 'ays-settings';
22+
1923
export function initAreYouSure($) {
2024

2125
$.fn.areYouSure = function(options) {
@@ -124,6 +128,7 @@ export function initAreYouSure($) {
124128
$(fields).unbind(settings.fieldEvents, checkForm);
125129
$(fields).bind(settings.fieldEvents, checkForm);
126130
$form.data("ays-orig-field-count", $(fields).length);
131+
$form.data(dataKeyAysSettings, settings);
127132
setDirtyStatus($form, false);
128133
};
129134

@@ -162,9 +167,7 @@ export function initAreYouSure($) {
162167
if (!settings.silent && !window.aysUnloadSet) {
163168
window.aysUnloadSet = true;
164169
$(window).bind('beforeunload', function() {
165-
const $forms = $("form:not(.ignore-dirty)").filter('.' + settings.dirtyClass);
166-
const dirtyFormCount = Array.from($forms).reduce((res, form) => form.closest('.tw-hidden') ? res : res + 1, 0);
167-
if (dirtyFormCount === 0) return;
170+
if (!shouldTriggerAreYouSure(settings)) return;
168171

169172
// Prevent multiple prompts - seen on Chrome and IE
170173
if (navigator.userAgent.toLowerCase().match(/msie|chrome/)) {
@@ -210,3 +213,15 @@ export function ignoreAreYouSure(selectorOrEl: string|Element|$) {
210213
// because when using "enter" to submit a form, the "dirty" class will appear again before reloading.
211214
$(selectorOrEl).addClass('ignore-dirty');
212215
}
216+
217+
export function shouldTriggerAreYouSure(): boolean {
218+
const forms = document.querySelectorAll('form:not(.ignore-dirty)');
219+
for (const form of forms) {
220+
const settings = $(form).data(dataKeyAysSettings);
221+
if (!settings) continue;
222+
if (!form.matches('.' + settings.dirtyClass)) continue;
223+
if (form.closest('.tw-hidden')) continue;
224+
return true;
225+
}
226+
return false;
227+
}

0 commit comments

Comments
 (0)