-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #187 from dibik96/17.0-develop
G2P-1692,G2P-1640: g2p_assessment widget migrated,entitlement creatio…
- Loading branch information
Showing
18 changed files
with
246 additions
and
619 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
223 changes: 125 additions & 98 deletions
223
g2p_program_assessment/static/src/js/g2p_assessment.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,112 +1,139 @@ | ||
/** @odoo-module **/ | ||
|
||
import AbstractField from "web.AbstractField"; | ||
import fieldsRegistry from "web.field_registry"; | ||
import {qweb} from "web.core"; | ||
import session from "web.session"; | ||
|
||
// eslint-disable-next-line no-undef | ||
const markup = QWeb2.tools.markup; | ||
|
||
// The following to be replaced with Widget instead of FieldText | ||
var G2PAssessmentWizardWidget = AbstractField.extend({ | ||
className: "o_field_g2p_program_assessment", | ||
tagName: "div", | ||
events: { | ||
"click button.o_ChatterTopbar_buttonAddAssess": "triggerAddAssessment", | ||
"click button.o_ChatterTopbar_buttonAddComment": "triggerAddComment", | ||
"click button.o_Composer_buttonCancel": "triggerAddAssessment", | ||
"click button.o_Composer_buttonSubmit": "submitAssessment", | ||
// "click button.o_Composer_buttonSubmit": "submitComment", | ||
"keyup textarea.o_ComposerTextInput_textarea": "triggerButtonSubmitEnable", | ||
}, | ||
init: function () { | ||
this._super.apply(this, arguments); | ||
this.resModel = this.nodeOptions.res_model ? this.nodeOptions.res_model : this.value.model; | ||
this.resId = this.nodeOptions.res_id_field | ||
? this.recordData[this.nodeOptions.res_id_field] | ||
: this.value.data.id; | ||
this.readonly = false; | ||
if (this.nodeOptions.readonly) { | ||
this.readonly = this.nodeOptions.readonly; | ||
} else if (this.nodeOptions.readonly_field) { | ||
this.readonly = this.recordData[this.nodeOptions.readonly_field]; | ||
} else if (this.nodeOptions.not_readonly_field) { | ||
this.readonly = !this.recordData[this.nodeOptions.not_readonly_field]; | ||
} | ||
this.showAddCommentsButton = this.nodeOptions.show_add_comments_button != false; | ||
this.showAddAssessmentsButton = this.nodeOptions.show_add_assessments_button != false; | ||
console.log(this); | ||
import {Component, markup, onWillStart, useExternalListener, useState} from "@odoo/owl"; | ||
import {renderToElement} from "@web/core/utils/render"; | ||
import {registry} from "@web/core/registry"; | ||
import {session} from "@web/session"; | ||
import {useService} from "@web/core/utils/hooks"; | ||
|
||
export class G2PAssessmentWizardWidget extends Component { | ||
setup() { | ||
super.setup(); | ||
this.orm = useService("orm"); | ||
useExternalListener(document, "keydown", this.handleKeyUpAndInput.bind(this)); | ||
useExternalListener(document, "click", this.handleButtonClick.bind(this)); | ||
this.resModel = this.props.resModel | ||
? this.props.resModel | ||
: this.props.record.evalContext.context.active_model; | ||
this.resId = this.props.record.resId | ||
? this.props.record.resId | ||
: this.props.record.evalContext.context.active_id; | ||
this.readonly = this.props.record.readonly; | ||
this.rpc = useService("rpc"); | ||
|
||
this.assessmentAddMode = 0; | ||
this.assessmentAddModeStarted = 0; | ||
}, | ||
_render: async function () { | ||
return this.$el.html(qweb.render("g2p_assessments_list", await this._getRenderingContext())); | ||
}, | ||
triggerAddComment: function (e) { | ||
return this.triggerAddAssessment(e, true); | ||
}, | ||
triggerAddAssessment: function (e, is_comment = false) { | ||
this.state = useState({ | ||
assessment: [], | ||
res_model: false, | ||
res_id_field: false, | ||
}); | ||
|
||
onWillStart(async () => { | ||
await this._getRenderingContext(); | ||
console.log("Options:", this.props.record, this.props.record.resModel, this.props.record.resId); | ||
}); | ||
} | ||
|
||
handleButtonClick(ev) { | ||
const targetButton = $(ev.target).closest(".o_Composer_buttonSubmit"); | ||
if (targetButton.length) { | ||
this.submitAssessment(); | ||
} | ||
|
||
const cancelButton = $(ev.target).closest(".o_Composer_buttonCancel"); | ||
if (cancelButton.length) { | ||
this.triggerAddAssessment(); | ||
} | ||
} | ||
|
||
handleKeyUpAndInput() { | ||
const textarea = $("textarea.o_ComposerTextInput_textarea"); | ||
|
||
textarea.on("keyup", () => { | ||
this.triggerButtonSubmitEnable(); | ||
}); | ||
|
||
textarea.on("input", () => { | ||
this.triggerButtonSubmitEnable(); | ||
}); | ||
} | ||
|
||
submitAssessment() { | ||
let textareaBody = $("textarea.o_ComposerTextInput_textarea")[0].value; | ||
textareaBody = textareaBody.replace(/(?:\r\n|\r|\n)/g, "<br />"); | ||
const record = this.orm.call( | ||
"g2p.program.assessment", | ||
"post_assessment", | ||
[textareaBody, this.resModel, this.resId], | ||
{} | ||
); | ||
record.then(async () => { | ||
await this._getRenderingContext(); | ||
this.assessmentAddMode = 0; | ||
this.assessmentAddModeStarted = 1; | ||
$(".o_Composer_Assessment").addClass("o_invisible_modifier"); | ||
$(".o_ChatterTopbar_buttonAddAssess").removeClass("o_invisible_modifier"); | ||
}); | ||
} | ||
|
||
async _getRenderingContext() { | ||
try { | ||
const res = await this.rpc("/web/dataset/call_kw/g2p.program.assessment/get_rendering_context", { | ||
model: "g2p.program.assessment", | ||
method: "get_rendering_context", | ||
args: [this.resModel, this.resId], | ||
kwargs: {}, | ||
}); | ||
res.assessments.forEach((element) => { | ||
element.body = markup(element.body); | ||
}); | ||
this.state.assessments = res.assessments; | ||
} catch (error) { | ||
console.error("Error fetching assessments:", error); | ||
} | ||
} | ||
|
||
triggerAddAssessment() { | ||
if (this.assessmentAddModeStarted === 0) { | ||
this.assessmentAddMode = 1; | ||
this.assessmentAddModeStarted = 1; | ||
this.$(".o_Composer_header_buttons").addClass("o_invisible_modifier"); | ||
this.$(".o_Composer").replaceWith( | ||
qweb.render("g2p_assessments_add", {author_partner_id: session.partner_id}) | ||
$(".o_ChatterTopbar_buttonAddAssess").addClass("o_invisible_modifier"); | ||
$(".o_Composer").replaceWith( | ||
renderToElement("g2p_assessments_add", {author_partner_id: session.partner_id}) | ||
); | ||
} else if (this.assessmentAddMode === 0) { | ||
return; | ||
} | ||
if (this.assessmentAddMode === 0) { | ||
this.assessmentAddMode = 1; | ||
this.$(".o_Composer_Assessment").removeClass("o_invisible_modifier"); | ||
this.$(".o_Composer_header_buttons").addClass("o_invisible_modifier"); | ||
$(".o_Composer_Assessment").removeClass("o_invisible_modifier"); | ||
$(".o_ChatterTopbar_buttonAddAssess").addClass("o_invisible_modifier"); | ||
} else { | ||
this.assessmentAddMode = 0; | ||
this.$(".o_Composer_Assessment").addClass("o_invisible_modifier"); | ||
this.$(".o_Composer_header_buttons").removeClass("o_invisible_modifier"); | ||
$(".o_Composer_Assessment").addClass("o_invisible_modifier"); | ||
$(".o_ChatterTopbar_buttonAddAssess").removeClass("o_invisible_modifier"); | ||
} | ||
console.log("trigger ", is_comment); | ||
if (is_comment) this.$(".o_Composer_Assessment").addClass("o_Composer_Comment"); | ||
else this.$(".o_Composer_Assessment").removeClass("o_Composer_Comment"); | ||
}, | ||
submitAssessment: function () { | ||
var self = this; | ||
const is_comment = this.$(".o_Composer_Assessment").hasClass("o_Composer_Comment"); | ||
console.log(is_comment); | ||
let textareaBody = this.$("textarea.o_ComposerTextInput_textarea")[0].value; | ||
textareaBody = textareaBody.replace(/(?:\r\n|\r|\n)/g, "<br />"); | ||
this._rpc({ | ||
model: "g2p.program.assessment", | ||
method: "post_assessment", | ||
args: [textareaBody, this.resModel, this.resId, is_comment], | ||
}).then(function () { | ||
self.assessmentAddMode = 0; | ||
self.assessmentAddModeStarted = 0; | ||
self._render(); | ||
}); | ||
}, | ||
triggerButtonSubmitEnable: function () { | ||
this.$("button.o_Composer_buttonSubmit").prop( | ||
"disabled", | ||
this.$("textarea.o_ComposerTextInput_textarea")[0].value === "" | ||
); | ||
}, | ||
_getRenderingContext: async function () { | ||
const res = await this._rpc({ | ||
model: "g2p.program.assessment", | ||
method: "get_rendering_context", | ||
args: [this.resModel, this.resId], | ||
}); | ||
// To ask odoo to consider the body as html | ||
res.assessments.forEach((element) => { | ||
element.body = markup(element.body); | ||
}); | ||
res.readonly = this.readonly; | ||
res.resModel = this.resModel; | ||
res.showAddCommentsButton = this.showAddCommentsButton; | ||
res.showAddAssessmentsButton = this.showAddAssessmentsButton; | ||
return res; | ||
}, | ||
}); | ||
} | ||
|
||
triggerButtonSubmitEnable() { | ||
const textareaValue = $("textarea.o_ComposerTextInput_textarea").val(); | ||
const isButtonDisabled = textareaValue.trim() === ""; | ||
$("button.o_Composer_buttonSubmit").prop("disabled", isButtonDisabled); | ||
} | ||
} | ||
|
||
fieldsRegistry.add("g2p_assess_widget", G2PAssessmentWizardWidget); | ||
G2PAssessmentWizardWidget.template = "g2p_assessments_list"; | ||
|
||
export const assessmentwizard = { | ||
component: G2PAssessmentWizardWidget, | ||
extractProps(fieldInfo) { | ||
const {options} = fieldInfo; | ||
const {res_model: resModel, res_id_field: resIdField} = options; | ||
return { | ||
resModel, | ||
resIdField, | ||
}; | ||
}, | ||
}; | ||
|
||
export {G2PAssessmentWizardWidget}; | ||
registry.category("fields").add("g2p_assess_widget", assessmentwizard); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.