4 changed files with 152 additions and 26 deletions
@ -1,33 +1,50 @@ |
|||||
/** @odoo-module */ |
/** @odoo-module */ |
||||
import { FormController } from "@web/views/form/form_controller"; |
import { FormController } from "@web/views/form/form_controller"; |
||||
import { patch } from "@web/core/utils/patch"; |
import { patch } from "@web/core/utils/patch"; |
||||
import { useSetupView } from "@web/views/view_hook"; |
import { useSetupAction } from "@web/webclient/actions/action_hook"; |
||||
patch(FormController.prototype, { |
import { _t } from "@web/core/l10n/translation"; |
||||
|
import { SettingsConfirmationDialog } from "@web/webclient/settings_form_view/settings_confirmation_dialog"; |
||||
|
|
||||
/* Patch FormController to restrict auto save in form views */ |
/* Patch FormController to restrict auto save in form views */ |
||||
|
|
||||
|
patch(FormController.prototype, { |
||||
setup(){ |
setup(){ |
||||
super.setup(...arguments); |
super.setup(...arguments); |
||||
this.beforeLeaveHook = false |
|
||||
useSetupView({ |
|
||||
beforeLeave: () => this.beforeLeave(), |
|
||||
beforeUnload: (ev) => this.beforeUnload(ev), |
|
||||
}); |
|
||||
}, |
}, |
||||
|
|
||||
async beforeLeave() { |
async beforeLeave() { |
||||
/* function will work before leave the form */ |
const dirty = await this.model.root.isDirty(); |
||||
if(this.model.root.isDirty && this.beforeLeaveHook == false){ |
if (dirty) { |
||||
if (confirm("Do you want to save changes before leaving?")) { |
return this._confirmSave(); |
||||
this.beforeLeaveHook = true |
} |
||||
await this.model.root.save({ |
}, |
||||
reload: false, |
|
||||
onError: this.onSaveError.bind(this), |
beforeUnload() {}, |
||||
}); |
|
||||
} else { |
async _confirmSave() { |
||||
this.beforeLeaveHook = true |
let _continue = true; |
||||
this.model.root.discard(); |
await new Promise((resolve) => { |
||||
} |
this.dialogService.add(SettingsConfirmationDialog, { |
||||
} |
body: _t("Would you like to save your changes?"), |
||||
}, |
confirm: async () => { |
||||
beforeUnload: async (ev) => { |
await this.save(); |
||||
ev.preventDefault(); |
// It doesn't make sense to do the action of the button
|
||||
} |
// as the res.config.settings `execute` method will trigger a reload.
|
||||
|
_continue = true; |
||||
|
resolve(); |
||||
|
}, |
||||
|
cancel: async () => { |
||||
|
await this.model.root.discard(); |
||||
|
await this.model.root.save(); |
||||
|
_continue = true; |
||||
|
resolve(); |
||||
|
}, |
||||
|
stayHere: () => { |
||||
|
_continue = false; |
||||
|
resolve(); |
||||
|
}, |
||||
|
}); |
||||
|
}); |
||||
|
return _continue; |
||||
|
} |
||||
}); |
}); |
||||
|
@ -0,0 +1,103 @@ |
|||||
|
/** @odoo-module */ |
||||
|
import { patch } from "@web/core/utils/patch"; |
||||
|
import { ViewButton } from "@web/views/view_button/view_button"; |
||||
|
import { SettingsConfirmationDialog } from "@web/webclient/settings_form_view/settings_confirmation_dialog"; |
||||
|
import { useService } from "@web/core/utils/hooks"; |
||||
|
import { _t } from "@web/core/l10n/translation"; |
||||
|
import { pick } from "@web/core/utils/objects"; |
||||
|
|
||||
|
patch(ViewButton.prototype, { |
||||
|
setup() { |
||||
|
super.setup(...arguments); |
||||
|
this.dialogService = useService("dialog"); |
||||
|
}, |
||||
|
|
||||
|
async onClick(ev) { |
||||
|
const proceed = await this._confirmSave(); |
||||
|
|
||||
|
if (!proceed) { |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
if (this.props.tag === "a") { |
||||
|
ev.preventDefault(); |
||||
|
} |
||||
|
|
||||
|
if (this.props.onClick) { |
||||
|
return this.props.onClick(); |
||||
|
} |
||||
|
}, |
||||
|
|
||||
|
discard() { |
||||
|
const model = this.props.record || this.props.model; |
||||
|
|
||||
|
if (!model) { |
||||
|
console.warn("No model available to discard or save."); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
this.env.onClickViewButton({ |
||||
|
clickParams: { |
||||
|
name: "cancel", |
||||
|
type: "object", |
||||
|
special: "cancel", |
||||
|
}, |
||||
|
getResParams: () => |
||||
|
pick(model, "context", "evalContext", "resModel", "resId", "resIds"), |
||||
|
}); |
||||
|
}, |
||||
|
|
||||
|
async _confirmSave() { |
||||
|
let _continue = true; |
||||
|
await new Promise((resolve) => { |
||||
|
this.dialogService.add(SettingsConfirmationDialog, { |
||||
|
body: _t("Would you like to save your changes?"), |
||||
|
confirm: async () => { |
||||
|
// Check if `clickParams` is available
|
||||
|
const clickParams = this.clickParams || { |
||||
|
name: "execute", |
||||
|
type: "object", |
||||
|
}; |
||||
|
|
||||
|
const model = this.props.record || this.props.model; |
||||
|
if (model) { |
||||
|
await this.env.onClickViewButton({ |
||||
|
clickParams, |
||||
|
getResParams: () => |
||||
|
pick( |
||||
|
model, |
||||
|
"context", |
||||
|
"evalContext", |
||||
|
"resModel", |
||||
|
"resId", |
||||
|
"resIds" |
||||
|
), |
||||
|
}); |
||||
|
} else { |
||||
|
console.warn("No model available to execute save."); |
||||
|
} |
||||
|
|
||||
|
_continue = true; |
||||
|
resolve(); |
||||
|
}, |
||||
|
cancel: async () => { |
||||
|
const model = this.props.record || this.props.model; |
||||
|
if (model) { |
||||
|
await model?.discard?.(); |
||||
|
await model?.save?.(); |
||||
|
} else { |
||||
|
console.warn("No model available to discard or save."); |
||||
|
} |
||||
|
_continue = true; |
||||
|
resolve(); |
||||
|
}, |
||||
|
stayHere: () => { |
||||
|
_continue = false; |
||||
|
resolve(); |
||||
|
}, |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
return _continue; |
||||
|
}, |
||||
|
}); |
Loading…
Reference in new issue