mirror of https://github.com/OCA/web.git
[11.0][IMP] child_selection_clear
parent
57789bb60f
commit
d3fbfc9a35
|
@ -1,38 +1,31 @@
|
||||||
# Copyright 2019 Creu Blanca
|
# Copyright 2019 Creu Blanca
|
||||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||||
|
|
||||||
from odoo import models, _
|
from odoo import models
|
||||||
|
|
||||||
|
|
||||||
class Base(models.AbstractModel):
|
class Base(models.AbstractModel):
|
||||||
_inherit = 'base'
|
_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):
|
def _get_record_parents(self, field):
|
||||||
if not self:
|
if not self:
|
||||||
return [(False, _('Root'))]
|
return []
|
||||||
return getattr(
|
return getattr(
|
||||||
self, self._parent_name
|
self, self._parent_name
|
||||||
)._get_record_parents(field) + [(self.id, str(getattr(self, field)))]
|
)._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):
|
if not hasattr(self, self._parent_name):
|
||||||
return []
|
return []
|
||||||
return [(r.id, str(getattr(r, field))) for r in self.search([(
|
return [(r.id, str(getattr(r, field))) for r in self.search([(
|
||||||
self._parent_name, '=', self.id or False
|
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')
|
field = options.get('child_selection_field', 'display_name')
|
||||||
return {
|
return {
|
||||||
'childs': self._get_record_direct_childs(field),
|
'childs': self._get_record_direct_childs(field, domain),
|
||||||
'parents': self._get_record_parents(field)
|
'parents': self._get_record_parents(field)
|
||||||
}
|
}
|
||||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 9.4 KiB After Width: | Height: | Size: 6.0 KiB |
|
@ -13,7 +13,7 @@ odoo.define('web.web_widget_child_selector', function(require) {
|
||||||
'click .o_child_selection_button': '_onChildSelectionClick',
|
'click .o_child_selection_button': '_onChildSelectionClick',
|
||||||
}),
|
}),
|
||||||
start: function () {
|
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
|
// with the current m2o value (for instance, the user is currently
|
||||||
// typing something in the input, and hasn't selected a value yet).
|
// 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({
|
this._rpc({
|
||||||
model: this.field.relation,
|
model: this.field.relation,
|
||||||
method: 'get_record_direct_childs_parents',
|
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),
|
context: this.record.getContext(this.recordParams),
|
||||||
})
|
})
|
||||||
.then(function (data) {
|
.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) {
|
_onChildSelectionClick: function(event) {
|
||||||
var target = $(event.target);
|
var target = $(event.target);
|
||||||
var index = target.data('index');
|
|
||||||
var type = target.data('type');
|
var type = target.data('type');
|
||||||
var value = (type === 'child') ? this.childs[index]: this.parents[index];
|
if (type === 'clear') {
|
||||||
this._setValue({id: value[0], display_name: value[1]});
|
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() {
|
_renderEdit: function() {
|
||||||
this._set_childs();
|
this._set_childs();
|
||||||
|
|
|
@ -1,9 +1,14 @@
|
||||||
.btn.o_child_selection_button {
|
.o_child_selection_button {
|
||||||
white-space:normal;
|
|
||||||
width:100%;
|
|
||||||
margin:2px;
|
|
||||||
}
|
|
||||||
a.o_child_selection_button {
|
|
||||||
cursor: pointer,
|
cursor: pointer,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.o_child_selection_label {
|
||||||
|
cursor: pointer,
|
||||||
|
}
|
||||||
|
|
||||||
|
.o_child_selection_clear {
|
||||||
|
opacity: 0.6;
|
||||||
|
&:hover {
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -15,10 +15,12 @@
|
||||||
<t t-name="FieldChildSelectorChild">
|
<t t-name="FieldChildSelectorChild">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div t-foreach="childs" t-as="key" class="col-xs-12">
|
<div t-foreach="childs" t-as="key" class="col-xs-12">
|
||||||
<button t-att-data-id="childs[key][0]" t-att-data-index="key"
|
<t t-set="id_for_label" t-value="'o_child_hierarchy_selector_' + _.uniqueId()"/>
|
||||||
data-type="child" class="o_child_selection_button btn">
|
<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]"/>
|
<t t-esc="childs[key][1]"/>
|
||||||
</button>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</t>
|
</t>
|
||||||
|
@ -31,6 +33,9 @@
|
||||||
<t t-esc="parents[key][1]"/>
|
<t t-esc="parents[key][1]"/>
|
||||||
</a>
|
</a>
|
||||||
</t>
|
</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>
|
</span>
|
||||||
</t>
|
</t>
|
||||||
</templates>
|
</templates>
|
||||||
|
|
Loading…
Reference in New Issue