|
@ -1,3 +1,5 @@ |
|
|
|
|
|
/** @odoo-module **/ |
|
|
|
|
|
|
|
|
import { patch } from "@web/core/utils/patch"; |
|
|
import { patch } from "@web/core/utils/patch"; |
|
|
import { ViewButton } from "@web/views/view_button/view_button"; |
|
|
import { ViewButton } from "@web/views/view_button/view_button"; |
|
|
import { SettingsConfirmationDialog } from "@web/webclient/settings_form_view/settings_confirmation_dialog"; |
|
|
import { SettingsConfirmationDialog } from "@web/webclient/settings_form_view/settings_confirmation_dialog"; |
|
@ -9,95 +11,70 @@ patch(ViewButton.prototype, { |
|
|
setup() { |
|
|
setup() { |
|
|
super.setup(...arguments); |
|
|
super.setup(...arguments); |
|
|
this.dialogService = useService("dialog"); |
|
|
this.dialogService = useService("dialog"); |
|
|
|
|
|
this.actionService = useService("action"); // use this instead of old DOM
|
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
async onClick(ev) { |
|
|
async onClick(ev) { |
|
|
const proceed = await this._confirmSave(); |
|
|
const model = this.props.record || this.props.model; |
|
|
|
|
|
const hasUnsavedChanges = |
|
|
|
|
|
model && typeof model.isDirty === "function" && (await model.isDirty()); |
|
|
|
|
|
|
|
|
if (!proceed) { |
|
|
if (hasUnsavedChanges) { |
|
|
return; |
|
|
const proceed = await this._confirmSave(model); |
|
|
|
|
|
if (!proceed) return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Normal button behavior
|
|
|
if (this.props.tag === "a") { |
|
|
if (this.props.tag === "a") { |
|
|
ev.preventDefault(); |
|
|
ev.preventDefault(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (this.props.onClick) { |
|
|
if (this.props.onClick) { |
|
|
return this.props.onClick(); |
|
|
return this.props.onClick(); |
|
|
} |
|
|
} |
|
|
}, |
|
|
if (this.props.clickParams) { |
|
|
|
|
|
return this.env.onClickViewButton({ |
|
|
discard() { |
|
|
clickParams: this.props.clickParams, |
|
|
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: () => |
|
|
getResParams: () => |
|
|
pick(model, "context", "evalContext", "resModel", "resId", "resIds"), |
|
|
pick(model, "context", "evalContext", "resModel", "resId", "resIds"), |
|
|
}); |
|
|
}); |
|
|
|
|
|
} |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
async _confirmSave() { |
|
|
async _confirmSave(model) { |
|
|
let _continue = true; |
|
|
let proceed = true; |
|
|
|
|
|
|
|
|
await new Promise((resolve) => { |
|
|
await new Promise((resolve) => { |
|
|
this.dialogService.add(SettingsConfirmationDialog, { |
|
|
this.dialogService.add(SettingsConfirmationDialog, { |
|
|
body: _t("Would you like to save your changes?"), |
|
|
body: _t("Would you like to save your changes?"), |
|
|
confirm: async () => { |
|
|
confirm: async () => { |
|
|
// Check if `clickParams` is available
|
|
|
try { |
|
|
const clickParams = this.clickParams || { |
|
|
await model.save(); |
|
|
name: "execute", |
|
|
// use Action Service instead of onClickViewButton (avoids `el=null`)
|
|
|
|
|
|
if (this.props.clickParams) { |
|
|
|
|
|
this.actionService.doActionButton({ |
|
|
|
|
|
name: this.props.clickParams.name, |
|
|
type: "object", |
|
|
type: "object", |
|
|
}; |
|
|
resModel: model.resModel, |
|
|
|
|
|
resId: model.resId, |
|
|
const model = this.props.record || this.props.model; |
|
|
context: model.context, |
|
|
if (model) { |
|
|
|
|
|
await this.env.onClickViewButton({ |
|
|
|
|
|
clickParams, |
|
|
|
|
|
getResParams: () => |
|
|
|
|
|
pick( |
|
|
|
|
|
model, |
|
|
|
|
|
"context", |
|
|
|
|
|
"evalContext", |
|
|
|
|
|
"resModel", |
|
|
|
|
|
"resId", |
|
|
|
|
|
"resIds" |
|
|
|
|
|
), |
|
|
|
|
|
}); |
|
|
}); |
|
|
} else { |
|
|
|
|
|
console.warn("No model available to execute save."); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
} catch (e) { |
|
|
_continue = true; |
|
|
console.error("Save failed:", e); |
|
|
|
|
|
} |
|
|
resolve(); |
|
|
resolve(); |
|
|
}, |
|
|
}, |
|
|
cancel: async () => { |
|
|
cancel: async () => { |
|
|
const model = this.props.record || this.props.model; |
|
|
if (model?.discard) await model.discard(); |
|
|
if (model) { |
|
|
|
|
|
await model?.discard?.(); |
|
|
|
|
|
await model?.save?.(); |
|
|
|
|
|
} else { |
|
|
|
|
|
console.warn("No model available to discard or save."); |
|
|
|
|
|
} |
|
|
|
|
|
_continue = true; |
|
|
|
|
|
resolve(); |
|
|
resolve(); |
|
|
}, |
|
|
}, |
|
|
stayHere: () => { |
|
|
stayHere: () => { |
|
|
_continue = false; |
|
|
proceed = false; |
|
|
resolve(); |
|
|
resolve(); |
|
|
}, |
|
|
}, |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
return _continue; |
|
|
return proceed; |
|
|
}, |
|
|
}, |
|
|
}); |
|
|
}); |
|
|