1- import {
2- AvatarMany2XAutocomplete ,
3- Many2XAutocomplete ,
4- } from "@web/views/fields/relational_utils" ;
5- import { Many2OneField , many2OneField } from "@web/views/fields/many2one/many2one_field" ;
6- import { Many2OneReferenceField } from "@web/views/fields/many2one_reference/many2one_reference_field" ;
1+ import * as many2OneField from "@web/views/fields/many2one/many2one_field" ;
72import { FormController } from "@web/views/form/form_controller" ;
3+ import { Many2One } from "@web/views/fields/many2one/many2one" ;
4+ import { Many2OneReferenceField } from "@web/views/fields/many2one_reference/many2one_reference_field" ;
5+ import { Many2XAutocomplete } from "@web/views/fields/relational_utils" ;
86import { evaluateBooleanExpr } from "@web/core/py_js/py" ;
97import { fieldColorProps } from "../views/fields/standard_field_props.esm" ;
108import { isX2Many } from "@web/views/utils" ;
@@ -13,10 +11,6 @@ import {patch} from "@web/core/utils/patch";
1311import { registry } from "@web/core/registry" ;
1412import { session } from "@web/session" ;
1513
16- AvatarMany2XAutocomplete . props = {
17- ...AvatarMany2XAutocomplete . props ,
18- ...fieldColorProps ,
19- } ;
2014Many2XAutocomplete . props = {
2115 ...Many2XAutocomplete . props ,
2216 ...fieldColorProps ,
@@ -26,96 +20,88 @@ function evaluateSystemParameterDefaultTrue(option) {
2620 const isOptionSet = session . web_m2x_options [ `web_m2x_options.${ option } ` ] ;
2721 return isOptionSet ? evaluateBooleanExpr ( isOptionSet ) : true ;
2822}
23+ function evaluateSystemParameterNumber ( option ) {
24+ const isOptionSet = session . web_m2x_options [ `web_m2x_options.${ option } ` ] ;
25+ return isOptionSet ? Number ( isOptionSet ) : false ;
26+ }
2927
30- patch ( many2OneField , {
31- m2o_options_props_create ( props , attrs , options ) {
32- const canQuickCreate = evaluateSystemParameterDefaultTrue ( "create" ) ;
33- if ( options . no_quick_create ) {
34- props . canQuickCreate = false ;
35- } else if ( "no_quick_create" in options ) {
36- props . canQuickCreate = attrs . can_create
37- ? evaluateBooleanExpr ( attrs . can_create )
38- : true ;
39- } else if ( ! canQuickCreate && props . canQuickCreate ) {
40- props . canQuickCreate = false ;
41- } else if ( canQuickCreate && ! props . canQuickCreate ) {
42- props . canQuickCreate = attrs . can_create
43- ? evaluateBooleanExpr ( attrs . can_create )
44- : true ;
45- }
46- return props ;
47- } ,
28+ export function m2o_options_props_create ( props , attrs , options ) {
29+ const canQuickCreate = evaluateSystemParameterDefaultTrue ( "create" ) ;
30+ if ( options . no_quick_create ) {
31+ props . canQuickCreate = false ;
32+ } else if ( "no_quick_create" in options ) {
33+ props . canQuickCreate = attrs . can_create
34+ ? evaluateBooleanExpr ( attrs . can_create )
35+ : true ;
36+ } else if ( ! canQuickCreate && props . canQuickCreate ) {
37+ props . canQuickCreate = false ;
38+ } else if ( canQuickCreate && ! props . canQuickCreate ) {
39+ props . canQuickCreate = attrs . can_create
40+ ? evaluateBooleanExpr ( attrs . can_create )
41+ : true ;
42+ }
43+ return props ;
44+ }
4845
49- m2o_options_props_create_edit ( props , attrs , options ) {
50- const canCreateEdit = evaluateSystemParameterDefaultTrue ( "create_edit" ) ;
51- if ( options . no_create_edit ) {
52- props . canCreateEdit = false ;
53- } else if ( "no_create_edit" in options ) {
54- // Same condition set in web/views/fields/many2one/many2one_field
55- props . canCreateEdit = attrs . can_create
56- ? evaluateBooleanExpr ( attrs . can_create )
57- : true ;
58- } else if ( ! canCreateEdit && props . canCreateEdit ) {
59- props . canCreateEdit = false ;
60- } else if ( canCreateEdit && ! props . canCreateEdit ) {
61- // Same condition set in web/views/fields/many2one/many2one_field
62- props . canCreateEdit = attrs . can_create
63- ? evaluateBooleanExpr ( attrs . can_create )
64- : true ;
65- }
66- return props ;
67- } ,
46+ export function m2o_options_props_create_edit ( props , attrs , options ) {
47+ const canCreateEdit = evaluateSystemParameterDefaultTrue ( "create_edit" ) ;
48+ if ( options . no_create_edit ) {
49+ props . canCreateEdit = false ;
50+ } else if ( "no_create_edit" in options ) {
51+ // Same condition set in web/views/fields/many2one/many2one_field
52+ props . canCreateEdit = attrs . can_create
53+ ? evaluateBooleanExpr ( attrs . can_create )
54+ : true ;
55+ } else if ( ! canCreateEdit && props . canCreateEdit ) {
56+ props . canCreateEdit = false ;
57+ } else if ( canCreateEdit && ! props . canCreateEdit ) {
58+ // Same condition set in web/views/fields/many2one/many2one_field
59+ props . canCreateEdit = attrs . can_create
60+ ? evaluateBooleanExpr ( attrs . can_create )
61+ : true ;
62+ }
63+ return props ;
64+ }
6865
69- m2o_options_props_limit ( props , attrs , options ) {
70- const ir_options = session . web_m2x_options ;
71- if ( Number ( options . limit ) ) {
72- props . searchLimit = Number ( options . limit ) ;
73- } else if ( Number ( ir_options [ "web_m2x_options.limit" ] ) ) {
74- props . searchLimit = Number ( ir_options [ "web_m2x_options.limit" ] ) ;
75- }
76- return props ;
77- } ,
66+ export function m2o_options_props_limit ( props , attrs , options ) {
67+ const ir_options = session . web_m2x_options ;
68+ if ( Number ( options . limit ) ) {
69+ props . searchLimit = Number ( options . limit ) ;
70+ } else if ( Number ( ir_options [ "web_m2x_options.limit" ] ) ) {
71+ props . searchLimit = Number ( ir_options [ "web_m2x_options.limit" ] ) ;
72+ }
73+ return props ;
74+ }
7875
79- m2o_options_props_search_more ( props , attrs , options ) {
80- const noSearchMore = ! evaluateSystemParameterDefaultTrue ( "search_more" ) ;
81- if ( options . search_more ) {
82- props . noSearchMore = false ;
83- } else if ( "search_more" in options ) {
84- props . noSearchMore = true ;
85- } else if ( ! noSearchMore && props . noSearchMore ) {
86- props . noSearchMore = false ;
87- } else if ( noSearchMore ) {
88- props . noSearchMore = true ;
89- }
90- return props ;
91- } ,
76+ export function m2o_options_props_open ( props , attrs , options ) {
77+ if ( ! ( "no_open" in options ) ) {
78+ props . canOpen = evaluateSystemParameterDefaultTrue ( "open" ) ;
79+ }
80+ return props ;
81+ }
9282
93- m2o_options_props_open ( props , attrs , options ) {
94- if ( ! ( "no_open" in options ) ) {
95- props . canOpen = evaluateSystemParameterDefaultTrue ( "open" ) ;
96- }
97- return props ;
98- } ,
83+ export function m2o_options_props ( props , attrs , options ) {
84+ let newProps = props ;
85+ newProps = m2o_options_props_create ( newProps , attrs , options ) ;
86+ newProps = m2o_options_props_create_edit ( newProps , attrs , options ) ;
87+ newProps = m2o_options_props_limit ( newProps , attrs , options ) ;
88+ newProps = m2o_options_props_open ( newProps , attrs , options ) ;
89+ newProps . fieldColor = options . field_color ;
90+ newProps . fieldColorOptions = options . colors ;
91+ return newProps ;
92+ }
9993
100- m2o_options_props ( props , attrs , options ) {
101- let newProps = props ;
102- newProps = this . m2o_options_props_create ( newProps , attrs , options ) ;
103- newProps = this . m2o_options_props_create_edit ( newProps , attrs , options ) ;
104- newProps = this . m2o_options_props_limit ( newProps , attrs , options ) ;
105- newProps = this . m2o_options_props_search_more ( newProps , attrs , options ) ;
106- newProps = this . m2o_options_props_open ( newProps , attrs , options ) ;
107- newProps . fieldColor = options . field_color ;
108- newProps . fieldColorOptions = options . colors ;
109- return newProps ;
110- } ,
111- extractProps ( { attrs, context, decorations, options, string} , dynamicInfo ) {
112- const props = super . extractProps (
113- { attrs, context, decorations, options, string} ,
114- dynamicInfo
115- ) ;
116- return this . m2o_options_props ( props , attrs , options ) ;
117- } ,
118- } ) ;
94+ const oldBuildM2OFieldDescription = many2OneField . buildM2OFieldDescription ;
95+ many2OneField . buildM2OFieldDescription = ( component ) => {
96+ const _super = oldBuildM2OFieldDescription ( component ) ;
97+ return {
98+ ..._super ,
99+ extractProps ( staticInfo , dynamicInfo ) {
100+ const props = many2OneField . extractM2OFieldProps ( staticInfo , dynamicInfo ) ;
101+ return m2o_options_props ( props , staticInfo . attrs , staticInfo . options ) ;
102+ } ,
103+ } ;
104+ } ;
119105
120106// FIXME: Many2OneReferenceField does not support m2o_options_props.
121107// This no-op prevents crashes, but proper option support is still missing.
@@ -127,20 +113,16 @@ patch(Many2OneReferenceField, {
127113 } ,
128114} ) ;
129115
130- patch ( Many2OneField . prototype , {
131- get Many2XAutocompleteProps ( ) {
132- const search_limit = this . props . searchLimit ;
133- const no_search_more = this . props . noSearchMore ;
116+ patch ( Many2One . prototype , {
117+ get many2XAutocompleteProps ( ) {
118+ const search_limit = evaluateSystemParameterNumber ( "limit" ) ;
134119 const field_color = this . props . fieldColor ;
135120 const field_color_options = this . props . fieldColorOptions ;
136- const props = super . Many2XAutocompleteProps ;
121+ const props = super . many2XAutocompleteProps ;
137122 const ret_props = { ...props } ;
138123 if ( Number ( search_limit ) && Number ( search_limit ) > 1 ) {
139124 ret_props . searchLimit = search_limit - 1 ;
140125 }
141- if ( no_search_more ) {
142- ret_props . noSearchMore = no_search_more ;
143- }
144126 if ( field_color && field_color_options ) {
145127 ret_props . fieldColor = field_color ;
146128 ret_props . fieldColorOptions = field_color_options ;
@@ -198,26 +180,11 @@ patch(many2ManyTagsField, {
198180 return props ;
199181 } ,
200182
201- m2m_options_props_search_more ( props , attrs , options ) {
202- const noSearchMore = ! evaluateSystemParameterDefaultTrue ( "search_more" ) ;
203- if ( options . search_more ) {
204- props . noSearchMore = false ;
205- } else if ( "search_more" in options ) {
206- props . noSearchMore = true ;
207- } else if ( ! noSearchMore && props . noSearchMore ) {
208- props . noSearchMore = false ;
209- } else if ( noSearchMore ) {
210- props . noSearchMore = true ;
211- }
212- return props ;
213- } ,
214-
215183 m2m_options_props ( props , attrs , options ) {
216184 let newProps = props ;
217185 newProps = this . m2m_options_props_create ( newProps , attrs , options ) ;
218186 newProps = this . m2m_options_props_create_edit ( newProps , attrs , options ) ;
219187 newProps = this . m2m_options_props_limit ( newProps , attrs , options ) ;
220- newProps = this . m2m_options_props_search_more ( newProps , attrs , options ) ;
221188 newProps . fieldColor = options . field_color ;
222189 newProps . fieldColorOptions = options . colors ;
223190 return newProps ;
@@ -317,12 +284,22 @@ patch(registry.category("fields").validationSchema, {
317284 m2o_options_props_create : { type : Function , optional : true } ,
318285 m2o_options_props_create_edit : { type : Function , optional : true } ,
319286 m2o_options_props_limit : { type : Function , optional : true } ,
320- m2o_options_props_search_more : { type : Function , optional : true } ,
321287 m2o_options_props_open : { type : Function , optional : true } ,
322288 m2o_options_props : { type : Function , optional : true } ,
323289 m2m_options_props_create : { type : Function , optional : true } ,
324290 m2m_options_props_create_edit : { type : Function , optional : true } ,
325291 m2m_options_props_limit : { type : Function , optional : true } ,
326- m2m_options_props_search_more : { type : Function , optional : true } ,
327292 m2m_options_props : { type : Function , optional : true } ,
328293} ) ;
294+
295+ registry . category ( "fields" ) . add (
296+ "many2one" ,
297+ {
298+ ...many2OneField . buildM2OFieldDescription ( many2OneField . Many2OneField ) ,
299+ extractProps ( staticInfo , dynamicInfo ) {
300+ const props = many2OneField . extractM2OFieldProps ( staticInfo , dynamicInfo ) ;
301+ return m2o_options_props ( props , staticInfo . attrs , staticInfo . options ) ;
302+ } ,
303+ } ,
304+ { force : true }
305+ ) ;
0 commit comments