Skip to content

Commit f3a0ade

Browse files
committed
move dirty check to own function
1 parent 3bdf16a commit f3a0ade

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

web_src/js/components/ViewFileTreeItem.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {SvgIcon} from '../svg.ts';
33
import {isPlainClick} from '../utils/dom.ts';
44
import {shallowRef} from 'vue';
55
import {type createViewFileTreeStore} from './ViewFileTreeStore.ts';
6+
import {triggersAreYouSure} from '../vendor/jquery.are-you-sure.ts';
67
78
export type Item = {
89
entryName: string;
@@ -40,7 +41,7 @@ const onItemClick = (e: MouseEvent) => {
4041
// only handle the click event with partial page reloading if both
4142
// - the user didn't press any special key like "Ctrl+Click" (which may have custom browser behavior)
4243
// - the editor/commit form isn't dirty (a full page reload shows a confirmation dialog if the form contains unsaved changes)
43-
if (!isPlainClick(e) || document.querySelector('.repo-view-content .form.dirty')) return;
44+
if (!isPlainClick(e) || triggersAreYouSure()) return;
4445
e.preventDefault();
4546
if (props.item.entryMode === 'tree') doLoadChildren();
4647
store.navigateTreeView(props.item.fullPath);

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

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
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 default options to make it available to other functions
7+
// * check if any form is dirty
68

79
/*!
810
* jQuery Plugin: Are-You-Sure (Dirty Form Detection)
@@ -16,20 +18,21 @@
1618
* Version: 1.9.0
1719
* Date: 13th August 2014
1820
*/
21+
const defaultOptions = {
22+
'message': 'You have unsaved changes!',
23+
'dirtyClass': 'dirty',
24+
'change': null,
25+
'silent': false,
26+
'addRemoveFieldsMarksDirty': false,
27+
'fieldEvents': 'change keyup propertychange input',
28+
'fieldSelector': ":input:not(input[type=submit]):not(input[type=button])"
29+
};
30+
1931
export function initAreYouSure($) {
2032

2133
$.fn.areYouSure = function(options) {
2234

23-
var settings = $.extend(
24-
{
25-
'message' : 'You have unsaved changes!',
26-
'dirtyClass' : 'dirty',
27-
'change' : null,
28-
'silent' : false,
29-
'addRemoveFieldsMarksDirty' : false,
30-
'fieldEvents' : 'change keyup propertychange input',
31-
'fieldSelector': ":input:not(input[type=submit]):not(input[type=button])"
32-
}, options);
35+
var settings = $.extend({}, defaultOptions, options);
3336

3437
var getValue = function($field) {
3538
if ($field.hasClass('ays-ignore')
@@ -162,9 +165,7 @@ export function initAreYouSure($) {
162165
if (!settings.silent && !window.aysUnloadSet) {
163166
window.aysUnloadSet = true;
164167
$(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;
168+
if (!triggersAreYouSure(settings)) return;
168169

169170
// Prevent multiple prompts - seen on Chrome and IE
170171
if (navigator.userAgent.toLowerCase().match(/msie|chrome/)) {
@@ -210,3 +211,8 @@ export function ignoreAreYouSure(selectorOrEl: string|Element|$) {
210211
// because when using "enter" to submit a form, the "dirty" class will appear again before reloading.
211212
$(selectorOrEl).addClass('ignore-dirty');
212213
}
214+
215+
export function triggersAreYouSure(opts = {}) {
216+
const $forms = $('form:not(.ignore-dirty)').filter('.' + (opts.dirtyClass ?? defaultOptions.dirtyClass));
217+
return Array.from($forms).some((form) => form.closest('.tw-hidden') === null);
218+
}

0 commit comments

Comments
 (0)