Skip to content

Commit

Permalink
Warn if "Update DB" is used when the schema designer code editor has …
Browse files Browse the repository at this point in the history
…unsaved changes. Fixes #954
  • Loading branch information
mpscholten committed Aug 6, 2021
1 parent 40bebe0 commit 3d7e134
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
5 changes: 4 additions & 1 deletion IHP/IDE/SchemaDesigner/View/Layout.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ databaseControls = [hsx|
data-placement="bottom"
data-html="true"
title="Dumps DB to Fixtures.sql.<br><br>Delete the DB.<br><br>Recreate using Schema.sql and Fixtures.sql"
onclick="checkBeforeUnload()"
>Update DB</button>

<button type="button" class="btn btn-primary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Expand All @@ -59,6 +60,7 @@ databaseControls = [hsx|
data-placement="left"
data-html="true"
title="Saves the content of all tables to Application/Fixtures.sql"
onclick="checkBeforeUnload()"
>Save DB to Fixtures</button>
<button
type="submit"
Expand All @@ -68,6 +70,7 @@ databaseControls = [hsx|
data-placement="left"
data-html="true"
title="Delete the DB and recreate using Application/Schema.sql and Application/Fixture.sql<br><br><strong class=text-danger>Save DB to Fixtures before using this to avoid data loss</strong>"
onclick="checkBeforeUnload()"
>Push to DB</button>
</div>
</div>
Expand All @@ -85,7 +88,7 @@ findStatementByName statementName statements = find pred statements
visualNav :: Html
visualNav =
if isActivePath ShowCodeAction
then [hsx|<a class="custom-control custom-switch visual-switch" href={TablesAction}>
then [hsx|<a class="custom-control custom-switch visual-switch" href={TablesAction} onclick="checkBeforeUnload()">
<input type="checkbox" class="custom-control-input" id="visual-switch" checked="checked"/>
<label class="custom-control-label" for="visual-switch">Code Editor</label>
</a>|]
Expand Down
18 changes: 18 additions & 0 deletions lib/IHP/static/IDE/ihp-schemadesigner.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,22 @@ function closeModalOnEscape() {
const modalBackdrop = document.getElementById('modal-backdrop');
modalBackdrop?.click();
})
}

// Triggers a call to window.onbeforeunload if needed
// https://github.com/digitallyinduced/ihp/issues/954
function checkBeforeUnload() {
if (window.onbeforeunload) {
var r = window.onbeforeunload();
if (!r) {
return;
}

if (confirm(r)) {
event.preventDefault();
event.stopPropagation();
} else {
window.onbeforeunload = null;
}
}
}

0 comments on commit 3d7e134

Please sign in to comment.