[11.0][IMP] child_selection_clear

pull/1430/head
Jaime Arroyo 2019-12-16 11:35:41 +01:00 committed by Enric Tobella
parent 57789bb60f
commit d3fbfc9a35
5 changed files with 41 additions and 48 deletions

View File

@ -1,38 +1,31 @@
# Copyright 2019 Creu Blanca
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import models, _
from odoo import models
class Base(models.AbstractModel):
_inherit = 'base'
def get_record_parent(self):
self.ensure_one()
if not hasattr(self, self._parent_name):
return False
parent = getattr(self, self._parent_name)
if not parent:
return False
return parent.name_get()[0]
def _get_record_parents(self, field):
if not self:
return [(False, _('Root'))]
return []
return getattr(
self, self._parent_name
)._get_record_parents(field) + [(self.id, str(getattr(self, field)))]
def _get_record_direct_childs(self, field):
def _get_record_direct_childs(self, field, domain):
if not hasattr(self, self._parent_name):
return []
return [(r.id, str(getattr(r, field))) for r in self.search([(
self._parent_name, '=', self.id or False
)])]
)] + domain)]
def get_record_direct_childs_parents(self, options):
def get_record_direct_childs_parents(self, options, domain=False):
if not domain:
domain = []
field = options.get('child_selection_field', 'display_name')
return {
'childs': self._get_record_direct_childs(field),
'childs': self._get_record_direct_childs(field, domain),
'parents': self._get_record_parents(field)
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.4 KiB

After

Width:  |  Height:  |  Size: 6.0 KiB

View File

@ -13,7 +13,7 @@ odoo.define('web.web_widget_child_selector', function(require) {
'click .o_child_selection_button': '_onChildSelectionClick',
}),
start: function () {
// booleean indicating that the content of the input isn't synchronized
// boolean indicating that the content of the input isn't synchronized
// with the current m2o value (for instance, the user is currently
// typing something in the input, and hasn't selected a value yet).
@ -41,7 +41,11 @@ odoo.define('web.web_widget_child_selector', function(require) {
this._rpc({
model: this.field.relation,
method: 'get_record_direct_childs_parents',
args: [ resources, this.nodeOptions],
args: [
resources,
this.nodeOptions,
this.record.getDomain({fieldName: this.name}),
],
context: this.record.getContext(this.recordParams),
})
.then(function (data) {
@ -59,31 +63,17 @@ odoo.define('web.web_widget_child_selector', function(require) {
}));
});
},
_onChildSelectionParent: function(event) {
var self = this;
this._rpc({
model: this.field.relation,
method: 'get_record_parent',
args: [[this.value.res_id || false]],
context: this.record.getContext(this.recordParams),
})
.then(function (parent) {
if (parent)
self._setValue({
id: parent[0], display_name: parent[1]
})
else
self._setValue({
id: false, display_name: false
});
});
},
_onChildSelectionClick: function(event) {
var target = $(event.target);
var index = target.data('index');
var type = target.data('type');
if (type === 'clear') {
this._setValue({id: false});
}
else {
var index = target.data('index');
var value = (type === 'child') ? this.childs[index]: this.parents[index];
this._setValue({id: value[0], display_name: value[1]});
}
},
_renderEdit: function() {
this._set_childs();

View File

@ -1,9 +1,14 @@
.btn.o_child_selection_button {
white-space:normal;
width:100%;
margin:2px;
}
a.o_child_selection_button {
.o_child_selection_button {
cursor: pointer,
}
.o_child_selection_label {
cursor: pointer,
}
.o_child_selection_clear {
opacity: 0.6;
&:hover {
opacity: 1;
}
}

View File

@ -15,10 +15,12 @@
<t t-name="FieldChildSelectorChild">
<div class="row">
<div t-foreach="childs" t-as="key" class="col-xs-12">
<button t-att-data-id="childs[key][0]" t-att-data-index="key"
data-type="child" class="o_child_selection_button btn">
<t t-set="id_for_label" t-value="'o_child_hierarchy_selector_' + _.uniqueId()"/>
<input type="radio" t-att-data-id="childs[key][0]" t-att-data-index="key"
t-att-id="id_for_label" data-type="child" class="o_child_selection_button"/>
<label class="o_form_label o_child_selection_label" t-att-for="id_for_label">
<t t-esc="childs[key][1]"/>
</button>
</label>
</div>
</div>
</t>
@ -31,6 +33,9 @@
<t t-esc="parents[key][1]"/>
</a>
</t>
<t t-if="Object.keys(parents).length > 0">
<i class="fa fa-times o_child_selection_button o_child_selection_clear" aria-hidden="true" data-type="clear"/>
</t>
</span>
</t>
</templates>