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
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
Download
Patches
Plain diff
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
Andreas Hennings
requested to merge
issue/node_edit_protection-3427181:3427181-refactor-javascript
into
8.x-1.x
1 year ago
Overview
0
Commits
21
Pipelines
0
Changes
3
Expand
Closes
#3427181
0
0
Merge request reports
Compare
8.x-1.x
version 1
621db67d
1 year ago
8.x-1.x (base)
and
latest version
latest version
bd8e0dc0
21 commits,
1 year ago
version 1
621db67d
21 commits,
1 year ago
3 files
+
52
−
40
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
3
Search (e.g. *.vue) (Ctrl+P)
node-edit-protection.js
+
50
−
38
Options
@@ -3,57 +3,69 @@
* Stops page from changing when user is posting.
*/
(
function
(
$
,
Drupal
,
drupalSettings
)
{
Drupal
.
node_edit_protection
=
{};
var
click
=
false
;
(
function
(
$
,
Drupal
)
{
// Allow Submit/Edit button.
var
edit
=
false
;
var
click
=
false
;
// 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
=
{
attach
:
function
(
context
)
{
attach
:
function
(
context
)
{
// If they leave an input field, assume they changed it.
$
(
"
.node-form :input
"
).
each
(
function
()
{
$
(
this
).
blur
(
function
()
{
edit
=
true
;
})
;
// The ':input' selector only works in jQuery, therefore the $(...) is
// needed.
$
(
once
(
'
node-edit-protection-input
'
,
$
(
'
.node-form :input
'
),
context
)).
blur
(
function
()
{
edit
=
true
;
});
// Let all form submit buttons through.
$
(
"
.node-form input[type='submit'], .node-form button[type='submit']
"
).
each
(
function
()
{
$
(
this
).
addClass
(
'
node-edit-protection-processed
'
);
$
(
this
).
click
(
function
()
{
click
=
true
;
});
$
(
once
(
'
node-edit-protection-submit
'
,
'
.node-form :is(input,button)[type="submit"]
'
,
context
)).
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.
window
.
onbeforeunload
=
function
()
{
// Add CKEditor support.
if
(
typeof
(
CKEDITOR
)
!=
'
undefined
'
&&
typeof
(
CKEDITOR
.
instances
)
!=
'
undefined
'
)
{
for
(
var
i
in
CKEDITOR
.
instances
)
{
if
(
CKEDITOR
.
instances
[
i
].
checkDirty
())
{
edit
=
true
;
break
;
}
}
if
(
click
)
{
return
;
}
if
(
edit
&&
!
click
)
{
click
=
false
;
return
(
Drupal
.
t
(
"
You will lose all unsaved work.
"
));
if
(
unsavedChangesExist
())
{
return
(
Drupal
.
t
(
'
You will lose all unsaved work.
'
));
}
}
}
};
})(
jQuery
,
Drupal
,
drupalSettings
);
})(
jQuery
,
Drupal
);
Loading