Skip to content

Commit

Permalink
[MIG] sale_layout_category_hide_detail: Migration to 16.0
Browse files Browse the repository at this point in the history
Change the fa icon Widget to new owl Component
  • Loading branch information
Yadier-Tecnativa committed Jun 23, 2023
1 parent 8150927 commit 0d77cd7
Show file tree
Hide file tree
Showing 12 changed files with 224 additions and 211 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ env:
# See https://github.com/OCA/odoo-community.org/issues/37#issuecomment-470686449
parserOptions:
ecmaVersion: 2019
sourceType: module
allowImportExportEverywhere: true

overrides:
- files:
- "**/*.esm.js"
parserOptions:
sourceType: module
allowImportExportEverywhere: true

# Globals available in Odoo that shouldn't produce errorings
globals:
Expand Down
9 changes: 4 additions & 5 deletions sale_layout_category_hide_detail/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"name": "Sale layout category hide detail",
"summary": "Hide details for sections in sale orders and invoices for "
"reports and customer portal",
"version": "15.0.1.0.0",
"version": "16.0.1.0.0",
"category": "Sales Management",
"website": "https://github.com/OCA/sale-reporting",
"author": "Tecnativa, " "Odoo Community Association (OCA)",
Expand All @@ -20,10 +20,9 @@
],
"assets": {
"web.assets_backend": [
"/sale_layout_category_hide_detail/static/src/js/"
"boolean_fa_icon_widget.js",
"/sale_layout_category_hide_detail/static/src/js/"
"sale_layout_category_hide_detail.js",
"sale_layout_category_hide_detail/static/src/js/*.js",
"sale_layout_category_hide_detail/static/src/xml/*.xml",
"sale_layout_category_hide_detail/static/src/css/*.scss",
],
},
"application": False,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
div.fa-icon-widget > input.form-check-input,
div.fa_icon_tooltip {
display: none;
visibility: hidden;
}
div.o_field_widget.o_field_boolean_fa_icon:hover > div > label > div.fa_icon_tooltip {
position: absolute;
visibility: visible;
display: block;
z-index: 1;
cursor: pointer;
}
div.fa_icon_tooltip > span.tip_text {
background: #fff;
padding: 4px;
border: solid #2e2e2e 1px;
margin-left: -60px;
z-index: 1;
top: 0%;
left: 50%;
box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
}
th.align-middle.cursor-default.o_boolean_fa_icon_cell.opacity-trigger-hover {
width: 45px !important;
}
div.o-checkbox.form-check.fa-icon-widget {
padding-left: 8px !important;
}
Original file line number Diff line number Diff line change
@@ -1,101 +1,108 @@
/* Copyright 2019 Tecnativa - Ernesto Tejeda
* Copyright 2022 Tecnativa - Víctor Martínez
* License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
*/
odoo.define(
"sale_layout_category_hide_detail.boolean_fa_icon_widget",
function (require) {
"use strict";
/** @odoo-module **/
const {useState} = owl;
import {BooleanField} from "@web/views/fields/boolean/boolean_field";
import {_lt} from "@web/core/l10n/translation";
import {registry} from "@web/core/registry";

var core = require("web.core");
var AbstractField = require("web.AbstractField");
var registry = require("web.field_registry");
const iconTrue = "fa-check-square-o";
const iconFalse = "fa-square-o";
const tooltipTrue = "Switch to hidden";
const tooltipFalse = "Switch to show";

var _t = core._t;
export class BooleanFaIconWidget extends BooleanField {
setup() {
super.setup();
this.state = useState({
fa_class: this.getFaIconClass(this.props.value),
text_tooltip: this.getTextTooltip(this.props.value),
});
}

var BooleanFaIconWidget = AbstractField.extend({
className: "o_boolean_fa_icon_widget",
events: {
click: "_toggleValue",
},
supportedFieldTypes: ["boolean"],
getFaIconClass(currentValue) {
var fa_icons = this.props.fa_icons;
var icon_true = fa_icons.icon_true || iconTrue;
var icon_false = fa_icons.icon_false || iconFalse;
return currentValue ? icon_true : icon_false;
}

// --------------------------------------------------------------------------
// Public
// --------------------------------------------------------------------------
getTextTooltip(currentValue) {
var show_tooltip = this.props.terminology;
var tooltip_true = show_tooltip.hover_true || tooltipTrue;
var tooltip_false = show_tooltip.hover_false || tooltipFalse;
return currentValue ? tooltip_true : tooltip_false;
}

/**
* A boolean field is always set since false is a valid value.
*
* @override
*/
isSet: function () {
return true;
},
isAllowEdit() {
var resModel = this.props.record.resModel;
var state = this.props.record.data.state;
if (typeof state === "undefined" || state === null) {
state = this.props.record.data.parent_state;
}
if (
["account.move.line", "sale.order.line"].includes(resModel) &&
state !== "draft"
) {
this.props.allow = false;
this.props.readonly = true;
}
return this.props.allow;
}

// --------------------------------------------------------------------------
// Private
// --------------------------------------------------------------------------
get isReadonly() {
return this.props.readonly;
}

_allowEdit: function () {
var allow = true;
if (
(this.model === "account.move.line" &&
this.recordData.parent_state !== "draft") ||
(this.model === "sale.order.line" &&
this.recordData.state !== "draft")
) {
allow = false;
}
return allow;
},
get allow() {
return this.props.allow;
}

/**
* Render font-awesome icon based on state
*
* @override
* @private
*/
get fa_icons() {
return this.props.fa_icons;
}

_render: function () {
// Set icon class
var fa_icons = this.attrs.options.fa_icons;
var icon_true = (fa_icons && fa_icons.icon_true) || "fa-check-square-o";
var icon_false = (fa_icons && fa_icons.icon_false) || "fa-square-o";
var fa_class = this.value ? icon_true : icon_false;
// Set tip message
var terminology = this.attrs.options.terminology;
var hover_true =
(terminology && _t(terminology.hover_true)) ||
_t("Click to uncheck");
var hover_false =
(terminology && _t(terminology.hover_false)) ||
_t("Click to check");
var tip = this.value ? hover_true : hover_false;
var style = this._allowEdit() ? "" : "cursor:default";
// Set template and add it to $el
var template = "<span class='fa %s' title='%s' style='%s'></span>";
this.$el.empty().append(_.str.sprintf(template, fa_class, tip, style));
},
get terminology() {
return this.props.terminology;
}

// --------------------------------------------------------------------------
// Handlers
// --------------------------------------------------------------------------
onChange(newValue) {
if (this.props.readonly || !this.isAllowEdit()) {
return;
}
super.onChange(newValue);
this.state.fa_class = this.getFaIconClass(newValue);
this.state.text_tooltip = this.getTextTooltip(newValue);
}
}

/**
* Toggle value
*
* @private
* @param {MouseEvent} event
*/
_toggleValue: function (event) {
event.preventDefault();
event.stopPropagation();
if (this._allowEdit()) this._setValue(!this.value);
},
});
BooleanFaIconWidget.template = "sale_layout_category.BooleanFaIconWidget";
BooleanFaIconWidget.displayName = _lt("Toggle");

registry.add("boolean_fa_icon", BooleanFaIconWidget);
return BooleanFaIconWidget;
}
);
BooleanFaIconWidget.defaultProps = {
fa_icons: {
icon_true: "fa-check-square-o",
icon_false: "fa-square-o",
},
terminology: {
hover_true: "Switch to: details hidden",
hover_false: "Switch to: details shown",
},
allow: true,
readonly: true,
};

BooleanFaIconWidget.props = {
...BooleanField.props,
fa_icons: {type: Object, optional: true},
terminology: {type: Object, optional: true},
allow: {type: Boolean, optional: true},
};

// Extract props from the attributes
BooleanFaIconWidget.extractProps = ({attrs}) => {
return {
fa_icons: attrs.options.fa_icons,
terminology: attrs.options.terminology,
};
};

registry.category("fields").add("boolean_fa_icon", BooleanFaIconWidget);

This file was deleted.

Loading

0 comments on commit 0d77cd7

Please sign in to comment.