1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
odoo.define('partner.autocomplete.many2one', function (require) {
'use strict';
var FieldMany2One = require('web.relational_fields').FieldMany2One;
var core = require('web.core');
var AutocompleteMixin = require('partner.autocomplete.Mixin');
var field_registry = require('web.field_registry');
var _t = core._t;
var PartnerField = FieldMany2One.extend(AutocompleteMixin, {
jsLibs: [
'/partner_autocomplete/static/lib/jsvat.js'
],
/**
* @override
*/
init: function () {
this._super.apply(this, arguments);
this._addAutocompleteSource(this._searchSuggestions, {
placeholder: _t('Searching Autocomplete...'),
order: 20,
validation: this._validateSearchTerm,
});
this.additionalContext['show_vat'] = true;
},
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
/**
* Action : create popup form with pre-filled values from Autocomplete
*
* @param {Object} company
* @returns {Promise}
* @private
*/
_createPartner: function (company) {
var self = this;
self.$('input').val('');
return self._getCreateData(company).then(function (data){
var context = {
'default_is_company': true
};
_.each(data.company, function (val, key) {
context['default_' + key] = val && val.id ? val.id : val;
});
// if(data.company.street_name && !data.company.street_number) context.default_street_number = '';
if (data.logo) context.default_image_1920 = data.logo;
return self._searchCreatePopup("form", false, context);
});
},
/**
* Returns the display_name from a string which contains it but was altered
* as a result of the show_vat option.
* Note that the split is done on a 'figuredash', not a standard dash.
*
* @private
* @param {string} value
* @returns {string} display_name without TaxID
*/
_getDisplayNameWithoutVAT: function (value) {
return value.split(' ‒ ')[0];
},
/**
* Modify autocomplete results rendering
* Add logo in the autocomplete results if logo is provided
*
* @private
*/
_modifyAutompleteRendering: function (){
var api = this.$input.data('ui-autocomplete');
// FIXME: bugfix to prevent traceback in mobile apps due to override
// of Many2one widget with native implementation.
if (!api) {
return;
}
api._renderItem = function(ul, item){
ul.addClass('o_partner_autocomplete_dropdown');
var $a = $('<a/>')["html"](item.label);
if (item.logo){
var $img = $('<img/>').attr('src', item.logo);
$a.append($img);
}
return $("<li></li>")
.data("item.autocomplete",item)
.append($a)
.appendTo(ul)
.addClass(item.classname);
};
},
/**
* @override
* @private
*/
_renderEdit: function (){
this.m2o_value = this._getDisplayNameWithoutVAT(this.m2o_value);
this._super.apply(this, arguments);
this._modifyAutompleteRendering();
},
/**
* Query Autocomplete and add results to the popup
*
* @override
* @param search_val {string}
* @returns {Promise}
* @private
*/
_searchSuggestions: function (search_val) {
var self = this;
return new Promise(function (resolve, reject) {
if (self._isOnline()) {
self._autocomplete(search_val).then(function (suggestions) {
var choices = [];
if (suggestions && suggestions.length) {
_.each(suggestions, function (suggestion) {
var label = '<i class="fa fa-magic text-muted"/> ';
label += _.str.sprintf('%s, <span class="text-muted">%s</span>', suggestion.label, suggestion.description);
choices.push({
label: label,
action: function () {
self._createPartner(suggestion);
},
logo: suggestion.logo,
classname: 'o_partner_autocomplete_dropdown_item',
});
});
}
resolve(choices);
});
} else {
resolve([]);
}
});
},
});
field_registry.add('res_partner_many2one', PartnerField);
return PartnerField;
});
|