| 1 |
import { registry } from "@web/core/registry"; |
| 2 |
import { SignatureDialog } from "@web/core/signature/signature_dialog"; |
| 3 |
import { useService } from "@web/core/utils/hooks"; |
| 4 |
import { standardWidgetProps } from "@web/views/widgets/standard_widget_props"; |
| 5 |
|
| 6 |
import { Component } from "@odoo/owl"; |
| 7 |
|
| 8 |
export class SignatureWidget extends Component { |
| 9 |
static template = "web.SignatureWidget"; |
| 10 |
static props = { |
| 11 |
...standardWidgetProps, |
| 12 |
fullName: { type: String, optional: true }, |
| 13 |
highlight: { type: Boolean, optional: true }, |
| 14 |
string: { type: String }, |
| 15 |
signatureField: { type: String, optional: true }, |
| 16 |
}; |
| 17 |
|
| 18 |
setup() { |
| 19 |
this.dialogService = useService("dialog"); |
| 20 |
this.orm = useService("orm"); |
| 21 |
} |
| 22 |
|
| 23 |
onClickSignature() { |
| 24 |
const nameAndSignatureProps = { |
| 25 |
mode: "draw", |
| 26 |
displaySignatureRatio: 3, |
| 27 |
signatureType: "signature", |
| 28 |
noInputName: true, |
| 29 |
}; |
| 30 |
const { fullName, record } = this.props; |
| 31 |
let defaultName = ""; |
| 32 |
if (fullName) { |
| 33 |
let signName; |
| 34 |
const fullNameData = record.data[fullName]; |
| 35 |
if (record.fields[fullName].type === "many2one") { |
| 36 |
signName = fullNameData && fullNameData.display_name; |
| 37 |
} else { |
| 38 |
signName = fullNameData; |
| 39 |
} |
| 40 |
defaultName = signName === "" ? undefined : signName; |
| 41 |
} |
| 42 |
|
| 43 |
nameAndSignatureProps.defaultFont = this.props.defaultFont; |
| 44 |
|
| 45 |
const dialogProps = { |
| 46 |
defaultName, |
| 47 |
nameAndSignatureProps, |
| 48 |
uploadSignature: (data) => this.uploadSignature(data), |
| 49 |
}; |
| 50 |
this.dialogService.add(SignatureDialog, dialogProps); |
| 51 |
} |
| 52 |
|
| 53 |
async uploadSignature({ signatureImage }) { |
| 54 |
const file = signatureImage.split(",")[1]; |
| 55 |
const { model, resModel, resId } = this.props.record; |
| 56 |
|
| 57 |
await this.env.services.orm.write(resModel, [resId], { |
| 58 |
[this.props.signatureField]: file, |
| 59 |
}); |
| 60 |
await this.props.record.load(); |
| 61 |
model.notify(); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
export const signatureWidget = { |
| 66 |
component: SignatureWidget, |
| 67 |
extractProps: ({ attrs }) => { |
| 68 |
const { full_name: fullName, highlight, signature_field, string } = attrs; |
| 69 |
return { |
| 70 |
fullName, |
| 71 |
highlight: !!highlight, |
| 72 |
string, |
| 73 |
signatureField: signature_field || "signature", |
| 74 |
}; |
| 75 |
}, |
| 76 |
}; |
| 77 |
|
| 78 |
registry.category("view_widgets").add("signature", signatureWidget); |