-
-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2510 from sascha-karnatz/move-dirty.js-into-app-j…
…avascript Convert Dirty from Coffeescript to Javascript
- Loading branch information
Showing
4 changed files
with
82 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
function ElementDirtyObserver(selector) { | ||
$(selector) | ||
.find('input[type="text"], select') | ||
.change(function (event) { | ||
const $content = $(event.target) | ||
$content.addClass("dirty") | ||
setElementDirty($content.closest(".element-editor")) | ||
}) | ||
} | ||
|
||
function setElementDirty(element) { | ||
$(element).addClass("dirty") | ||
window.onbeforeunload = () => Alchemy.t("page_dirty_notice") | ||
} | ||
|
||
function setElementClean(element) { | ||
const $element = $(element) | ||
$element.removeClass("dirty") | ||
$element.find("> .element-body .dirty").removeClass("dirty") | ||
window.onbeforeunload = () => {} | ||
} | ||
|
||
function isPageDirty() { | ||
return $("#element_area").find(".element-editor.dirty").length > 0 | ||
} | ||
|
||
function isElementDirty(element) { | ||
return $(element).hasClass("dirty") | ||
} | ||
|
||
function checkPageDirtyness(element) { | ||
let callback = () => {} | ||
|
||
if ($(element).is("form")) { | ||
callback = function () { | ||
const $form = $( | ||
`<form action="${element.action}" method="POST" style="display: none" />` | ||
) | ||
$form.append($(element).find("input")) | ||
$form.appendTo("body") | ||
|
||
Alchemy.pleaseWaitOverlay() | ||
$form.submit() | ||
} | ||
} else if ($(element).is("a")) { | ||
callback = () => Turbo.visit(element.pathname) | ||
} | ||
|
||
if (isPageDirty()) { | ||
Alchemy.openConfirmDialog(Alchemy.t("page_dirty_notice"), { | ||
title: Alchemy.t("warning"), | ||
ok_label: Alchemy.t("ok"), | ||
cancel_label: Alchemy.t("cancel"), | ||
on_ok: function () { | ||
window.onbeforeunload = void 0 | ||
callback() | ||
} | ||
}) | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
function PageLeaveObserver() { | ||
$("#main_navi a").click(function (event) { | ||
if (!checkPageDirtyness(event.currentTarget)) { | ||
event.preventDefault() | ||
} | ||
}) | ||
} | ||
|
||
export default { | ||
ElementDirtyObserver, | ||
setElementDirty, | ||
setElementClean, | ||
isElementDirty, | ||
checkPageDirtyness, | ||
PageLeaveObserver | ||
} |