Skip to content

Run JS in strict mode, vendorize jquery.are-you-sure #26742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"escape-goat": "4.0.0",
"fast-glob": "3.3.1",
"jquery": "3.7.0",
"jquery.are-you-sure": "1.9.0",
"katex": "0.16.8",
"license-checker-webpack-plugin": "0.2.1",
"lightningcss-loader": "2.1.0",
Expand Down
2 changes: 1 addition & 1 deletion web_src/js/features/codeeditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export async function createMonaco(textarea, filename, editorOpts) {
const model = editor.getModel();
model.onDidChangeContent(() => {
textarea.value = editor.getValue();
textarea.dispatchEvent(new Event('change')); // seems to be needed for jquery-are-you-sure
textarea.dispatchEvent(new Event('change')); // seems to be needed for jquery.are-you-sure
});

exportEditor(editor);
Expand Down
2 changes: 1 addition & 1 deletion web_src/js/features/common-global.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import $ from 'jquery';
import 'jquery.are-you-sure';
import '../vendor/ays.js';
import {clippie} from 'clippie';
import {createDropzone} from './dropzone.js';
import {initCompColorPicker} from './comp/ColorPicker.js';
Expand Down
182 changes: 182 additions & 0 deletions web_src/js/vendor/ays.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
// copy of https://github.com/codedance/jquery.AreYouSure/ made to work in strict mode
// the only changes are the `const` before $fields and $dirtyForms variables
(function($) {

$.fn.areYouSure = function(options) {

var settings = $.extend(
{
'message' : 'You have unsaved changes!',
'dirtyClass' : 'dirty',
Comment on lines +6 to +10
Copy link
Member

@puni9869 puni9869 Aug 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we modify var by let

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are hundrets of lint errors in that file. We could try fixing them at the risk of breaking it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sense.

'change' : null,
'silent' : false,
'addRemoveFieldsMarksDirty' : false,
'fieldEvents' : 'change keyup propertychange input',
'fieldSelector': ":input:not(input[type=submit]):not(input[type=button])"
}, options);

var getValue = function($field) {
if ($field.hasClass('ays-ignore')
|| $field.hasClass('aysIgnore')
|| $field.attr('data-ays-ignore')
|| $field.attr('name') === undefined) {
return null;
}

if ($field.is(':disabled')) {
return 'ays-disabled';
}

var val;
var type = $field.attr('type');
if ($field.is('select')) {
type = 'select';
}

switch (type) {
case 'checkbox':
case 'radio':
val = $field.is(':checked');
break;
case 'select':
val = '';
$field.find('option').each(function(o) {
var $option = $(this);
if ($option.is(':selected')) {
val += $option.val();
}
});
break;
default:
val = $field.val();
}

return val;
};

var storeOrigValue = function($field) {
$field.data('ays-orig', getValue($field));
};

var checkForm = function(evt) {

var isFieldDirty = function($field) {
var origValue = $field.data('ays-orig');
if (undefined === origValue) {
return false;
}
return (getValue($field) != origValue);
};

var $form = ($(this).is('form'))
? $(this)
: $(this).parents('form');

// Test on the target first as it's the most likely to be dirty
if (isFieldDirty($(evt.target))) {
setDirtyStatus($form, true);
return;
}

const $fields = $form.find(settings.fieldSelector);

if (settings.addRemoveFieldsMarksDirty) {
// Check if field count has changed
var origCount = $form.data("ays-orig-field-count");
if (origCount != $fields.length) {
setDirtyStatus($form, true);
return;
}
}

// Brute force - check each field
var isDirty = false;
$fields.each(function() {
var $field = $(this);
if (isFieldDirty($field)) {
isDirty = true;
return false; // break
}
});

setDirtyStatus($form, isDirty);
};

var initForm = function($form) {
var fields = $form.find(settings.fieldSelector);
$(fields).each(function() { storeOrigValue($(this)); });
$(fields).unbind(settings.fieldEvents, checkForm);
$(fields).bind(settings.fieldEvents, checkForm);
$form.data("ays-orig-field-count", $(fields).length);
setDirtyStatus($form, false);
};

var setDirtyStatus = function($form, isDirty) {
var changed = isDirty != $form.hasClass(settings.dirtyClass);
$form.toggleClass(settings.dirtyClass, isDirty);

// Fire change event if required
if (changed) {
if (settings.change) settings.change.call($form, $form);

if (isDirty) $form.trigger('dirty.areYouSure', [$form]);
if (!isDirty) $form.trigger('clean.areYouSure', [$form]);
$form.trigger('change.areYouSure', [$form]);
}
};

var rescan = function() {
var $form = $(this);
var fields = $form.find(settings.fieldSelector);
$(fields).each(function() {
var $field = $(this);
if (!$field.data('ays-orig')) {
storeOrigValue($field);
$field.bind(settings.fieldEvents, checkForm);
}
});
// Check for changes while we're here
$form.trigger('checkform.areYouSure');
};

var reinitialize = function() {
initForm($(this));
}

if (!settings.silent && !window.aysUnloadSet) {
window.aysUnloadSet = true;
$(window).bind('beforeunload', function() {
const $dirtyForms = $("form").filter('.' + settings.dirtyClass);
if ($dirtyForms.length == 0) {
return;
}
// Prevent multiple prompts - seen on Chrome and IE
if (navigator.userAgent.toLowerCase().match(/msie|chrome/)) {
if (window.aysHasPrompted) {
return;
}
window.aysHasPrompted = true;
window.setTimeout(function() {window.aysHasPrompted = false;}, 900);
}
return settings.message;
});
}

return this.each(function(elem) {
if (!$(this).is('form')) {
return;
}
var $form = $(this);

$form.submit(function() {
$form.removeClass(settings.dirtyClass);
});
$form.bind('reset', function() { setDirtyStatus($form, false); });
// Add a custom events
$form.bind('rescan.areYouSure', rescan);
$form.bind('reinitialize.areYouSure', reinitialize);
$form.bind('checkform.areYouSure', checkForm);
initForm($form);
});
};
})(jQuery);
8 changes: 6 additions & 2 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {readFileSync} from 'node:fs';
import {env} from 'node:process';

const {EsbuildPlugin} = EsBuildLoader;
const {SourceMapDevToolPlugin, DefinePlugin} = webpack;
const {SourceMapDevToolPlugin, DefinePlugin, BannerPlugin} = webpack;
const formatLicenseText = (licenseText) => wrapAnsi(licenseText || '', 80).trim();

const glob = (pattern) => fastGlob.sync(pattern, {
Expand Down Expand Up @@ -187,6 +187,11 @@ export default {
new MonacoWebpackPlugin({
filename: 'js/monaco-[name].[contenthash:8].worker.js',
}),
new BannerPlugin({
banner: `'use strict';`,
raw: true,
test: /\.js$/,
}),
isProduction ? new LicenseCheckerWebpackPlugin({
outputFilename: 'js/licenses.txt',
outputWriter: ({dependencies}) => {
Expand All @@ -206,7 +211,6 @@ export default {
}).join('\n');
},
override: {
'jquery.are-you-sure@*': {licenseName: 'MIT'}, // https://github.com/codedance/jquery.AreYouSure/pull/147
'khroma@*': {licenseName: 'MIT'}, // https://github.com/fabiospampinato/khroma/pull/33
},
emitError: true,
Expand Down