Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
N
node_edit_protection
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
node_edit_protection
Merge requests
!7
Issue
#3427181
: Rework the javascript.
Code
Review changes
Check out branch
Open in Workspace
Download
Patches
Plain diff
Expand sidebar
Open
Issue
#3427181
: Rework the javascript.
issue/node_edit_protection-3427181:3427181-refactor-javascript
into
8.x-1.x
Overview
0
Commits
21
Pipelines
0
Changes
3
Open
Issue #3427181: Rework the javascript.
Andreas Hennings
requested to merge
issue/node_edit_protection-3427181:3427181-refactor-javascript
into
8.x-1.x
Mar 11, 2024
Overview
0
Commits
21
Pipelines
0
Changes
3
Closes
#3427181
0
0
Merge request reports
Compare
8.x-1.x
version 1
621db67d
Mar 11, 2024
8.x-1.x (base)
and
latest version
latest version
bd8e0dc0
21 commits,
Apr 3, 2024
version 1
621db67d
21 commits,
Mar 11, 2024
3 files
+
52
−
40
Side-by-side
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
3
node-edit-protection.js
+
50
−
38
View file @ bd8e0dc0
Edit in single-file editor
Open in Web IDE
Show full file
@@ -3,57 +3,69 @@
@@ -3,57 +3,69 @@
* Stops page from changing when user is posting.
* Stops page from changing when user is posting.
*/
*/
(
function
(
$
,
Drupal
,
drupalSettings
)
{
(
function
(
$
,
Drupal
)
{
Drupal
.
node_edit_protection
=
{};
var
click
=
false
;
// Allow Submit/Edit button.
// Allow Submit/Edit button.
var
edit
=
false
;
var
click
=
false
;
// Dirty form flag.
// Dirty form flag.
var
edit
=
false
;
/**
* Checks if any elements on the page might have unsaved changes.
*
* @returns {boolean}
* TRUE if changes seem to exist.
*/
const
unsavedChangesExist
=
()
=>
{
if
(
edit
)
{
return
true
;
}
// Find modifications in CKEditor 5 instances.
if
(
document
.
querySelector
(
'
[data-ckeditor5-id][data-editor-value-is-changed=true]
'
))
{
return
true
;
}
// Find modifications in CKEditor 4 instances.
if
(
typeof
(
CKEDITOR
)
!==
'
undefined
'
&&
typeof
(
CKEDITOR
.
instances
)
!==
'
undefined
'
)
{
for
(
var
i
in
CKEDITOR
.
instances
)
{
if
(
CKEDITOR
.
instances
[
i
].
checkDirty
())
{
return
true
;
}
}
}
return
false
;
};
/**
* Adds a dialog to protect users from leaving a form with unsaved changes.
*
* @type {Drupal~behavior}
*
* @prop {Drupal~behaviorAttach} attach
* Attaches the behavior to new or updated elements.
*/
Drupal
.
behaviors
.
nodeEditProtection
=
{
Drupal
.
behaviors
.
nodeEditProtection
=
{
attach
:
function
(
context
)
{
attach
:
function
(
context
)
{
// If they leave an input field, assume they changed it.
// If they leave an input field, assume they changed it.
$
(
"
.node-form :input
"
).
each
(
function
()
{
// The ':input' selector only works in jQuery, therefore the $(...) is
$
(
this
).
blur
(
function
()
{
// needed.
edit
=
true
;
$
(
once
(
'
node-edit-protection-input
'
,
$
(
'
.node-form :input
'
),
context
)).
blur
(
function
()
{
})
;
edit
=
true
;
});
});
// Let all form submit buttons through.
// Let all form submit buttons through.
$
(
"
.node-form input[type='submit'], .node-form button[type='submit']
"
).
each
(
function
()
{
$
(
once
(
'
node-edit-protection-submit
'
,
'
.node-form :is(input,button)[type="submit"]
'
,
context
)).
click
(
function
()
{
$
(
this
).
addClass
(
'
node-edit-protection-processed
'
);
click
=
true
;
$
(
this
).
click
(
function
()
{
click
=
true
;
});
});
});
// Catch all links and buttons EXCEPT for "#" links.
$
(
"
a, button, input[type='submit']:not(.node-edit-protection-processed), button[type='submit']:not(.node-edit-protection-processed)
"
)
.
each
(
function
()
{
$
(
this
).
click
(
function
()
{
// Return when a "#" link is clicked so as to skip the
// window.onbeforeunload function.
if
(
edit
&&
$
(
this
).
attr
(
"
href
"
)
!=
"
#
"
)
{
return
0
;
}
});
});
// Handle backbutton, exit etc.
// Handle backbutton, exit etc.
window
.
onbeforeunload
=
function
()
{
window
.
onbeforeunload
=
function
()
{
// Add CKEditor support.
if
(
click
)
{
if
(
typeof
(
CKEDITOR
)
!=
'
undefined
'
&&
typeof
(
CKEDITOR
.
instances
)
!=
'
undefined
'
)
{
return
;
for
(
var
i
in
CKEDITOR
.
instances
)
{
if
(
CKEDITOR
.
instances
[
i
].
checkDirty
())
{
edit
=
true
;
break
;
}
}
}
}
if
(
edit
&&
!
click
)
{
if
(
unsavedChangesExist
())
{
click
=
false
;
return
(
Drupal
.
t
(
'
You will lose all unsaved work.
'
));
return
(
Drupal
.
t
(
"
You will lose all unsaved work.
"
));
}
}
}
}
}
}
};
};
})(
jQuery
,
Drupal
,
drupalSettings
);
})(
jQuery
,
Drupal
);
Loading