Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,12 @@ class AnalysisBoardPage extends BasePage {
// init import moves modal
UI.preloadModal(Modals.IMPORT_MOVES);
this.#moveTreeWidget.importMovesCallback = () => {
UI.showModalByName(Modals.IMPORT_MOVES, () => {
new ImportMovesHandler(this.#boardGui, this.#moveTreeWidget);
});
UI.openWithConfirmation(
!this.#moveTreeWidget.isEmpty(),
'Importing moves will erase the current game history. Continue?',
'import',
() => UI.showModalByName(Modals.IMPORT_MOVES, () => new ImportMovesHandler(this.#boardGui, this.#moveTreeWidget))
);
};

// move history drop down menu (to import moves, etc.)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,21 @@ class MoveHistoryDropDownMenuWidget extends DropDownMenu {

// callbacks
this.addSimpleItem('Import moves', () => {
UI.showModalByName(Modals.IMPORT_MOVES, () => {
new ImportMovesHandler(boardGui, moveTreeWidget);
});
UI.openWithConfirmation(
!moveTreeWidget.isEmpty(),
'Importing moves will erase the current game history. Continue?',
'import',
() => UI.showModalByName(Modals.IMPORT_MOVES, () => new ImportMovesHandler(boardGui, moveTreeWidget))
);
});

this.addSimpleItem('Edit start position', () => {
UI.showModalByName(Modals.POSITION_EDITOR, () => {
new PositionEditorHandler(getCurrentStartFenCb, selectedFenCb);
});
UI.openWithConfirmation(
!moveTreeWidget.isEmpty(),
'Editing the start position will erase the current game history. Continue?',
'continue',
() => UI.showModalByName(Modals.POSITION_EDITOR, () => new PositionEditorHandler(getCurrentStartFenCb, selectedFenCb))
);
});

// register
Expand Down
17 changes: 17 additions & 0 deletions webapp/src/main/resources/public/js/modules/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,23 @@ class UI {
});
}

/**
* Open a modal, showing a confirmation dialog first if shouldConfirm is true.
*
* @param shouldConfirm {boolean}
* @param confirmText {string}
* @param yesButtonText {string}
* @param openFn {function}
*/
static openWithConfirmation(shouldConfirm, confirmText, yesButtonText, openFn) {
if (shouldConfirm) {
const text = buildSimpleSpan(confirmText);
UI.showConfirmationModal(text, openFn, yesButtonText, () => UI.hideModal(null), 'cancel');
} else {
openFn();
}
}

/**
* @param modalName {string}
* @param loadedCallback {function}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1559,6 +1559,13 @@ class MoveTreeWidget {
}
}

/**
* @return {boolean}
*/
isEmpty() {
return this.#moveTree.isEmpty();
}

/**
* Append to the end or branch off (depending on which node is selected)
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,9 +388,12 @@ class SettingsGui {
enableEditPositionButton(getCurrentStartFenCb, selectedFenCb) {
this.#editPositionItem.style.display = 'block';
this.#editPositionButton.onclick = () => {
UI.showModalByName(Modals.POSITION_EDITOR, () => {
new PositionEditorHandler(getCurrentStartFenCb, selectedFenCb);
});
UI.openWithConfirmation(
this.#moveTreeWidget != null && !this.#moveTreeWidget.isEmpty(),
'Editing the start position will erase the current game history. Continue?',
'continue',
() => UI.showModalByName(Modals.POSITION_EDITOR, () => new PositionEditorHandler(getCurrentStartFenCb, selectedFenCb))
);
};
addToolTip(this.#editPositionButton, 'Edit start position');

Expand Down