[ADD] web_widget_child_selector

This commit is contained in:
Enric Tobella
2019-11-07 18:05:12 +01:00
committed by Olga Marco
parent d4704c67c4
commit 5e2530e535
15 changed files with 741 additions and 0 deletions

View File

@@ -0,0 +1,95 @@
odoo.define('web.web_widget_child_selector', function(require) {
"use strict";
var relational_fields = require('web.relational_fields');
var field_registry = require('web.field_registry');
var core = require('web.core');
var qweb = core.qweb;
var FieldMany2One = relational_fields.FieldMany2One;
var FieldChildSelector = FieldMany2One.extend({
template: "FieldChildSelector",
events: _.extend({}, FieldMany2One.prototype.events, {
'click .o_child_selection_button': '_onChildSelectionClick',
}),
start: function () {
// booleean 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).
this.$input_dropdown = this.$('.o_input_dropdown');
this.$input_value = this.$('.o_input_value')
return this._super.apply(this, arguments);
},
_renderReadonly: function () {
var value = _.escape((this.m2o_value || "").trim()).split("\n").join("<br/>");
this.$el.html(value);
if (!this.nodeOptions.no_open && this.value) {
this.$el.attr('href', _.str.sprintf('#id=%s&model=%s', this.value.res_id, this.field.relation));
this.$el.addClass('o_form_uri');
}
},
_set_childs: function() {
var self = this;
this.childs = {};
this.parents = {};
this.$input_dropdown.empty();
this.$input_value.empty();
var resources = [];
if (this.value.res_id)
resources = [this.value.res_id]
this._rpc({
model: this.field.relation,
method: 'get_record_direct_childs_parents',
args: [ resources, this.nodeOptions],
context: this.record.getContext(this.recordParams),
})
.then(function (data) {
_.each(data['parents'], function(parent, key) {
self.parents[key] = parent;
});
_.each(data['childs'], function(child, key) {
self.childs[key] = child;
});
self.$input_dropdown.append(qweb.render('FieldChildSelectorChild', {
'childs': self.childs,
}));
self.$input_value.append(qweb.render('FieldChildSelectorParent', {
'parents': self.parents,
}));
});
},
_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');
var value = (type === 'child') ? this.childs[index]: this.parents[index];
this._setValue({id: value[0], display_name: value[1]});
},
_renderEdit: function() {
this._set_childs();
},
});
field_registry.add('child_selector', FieldChildSelector);
return FieldChildSelector;
})

View File

@@ -0,0 +1,9 @@
.btn.o_child_selection_button {
white-space:normal;
width:100%;
margin:2px;
}
a.o_child_selection_button {
cursor: pointer,
}

View File

@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<templates id="template" xml:space="preserve">
<t t-name="FieldChildSelector">
<!--<div class="o_element"/>-->
<t t-if="widget.mode === 'readonly'">
<a t-if="!widget.nodeOptions.no_open" t-att-tabindex="widget.attrs.tabindex" class="o_form_uri" href="#"/>
<span t-if="widget.nodeOptions.no_open"/>
</t>
<div t-if="widget.mode === 'edit'" class="o_field_widget">
<span class="o_input_value"/>
<div class="o_input_dropdown"/>
</div>
</t>
<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-esc="childs[key][1]"/>
</button>
</div>
</div>
</t>
<t t-name="FieldChildSelectorParent">
<span>
<t t-foreach="parents" t-as="key">
<t t-if="key > 0"> / </t>
<a t-att-data-id="parents[key][0]" t-att-data-index="key"
data-type="parent" class="o_child_selection_button">
<t t-esc="parents[key][1]"/>
</a>
</t>
</span>
</t>
</templates>