1
1
/** @odoo -module **/
2
2
3
- import { onMounted , useExternalListener , useState } from "@odoo/owl" ;
3
+ import { markup , useRef , useState } from "@odoo/owl" ;
4
4
import { TextField } from "@web/views/fields/text/text_field" ;
5
5
import { _t } from "@web/core/l10n/translation" ;
6
6
import { registry } from "@web/core/registry" ;
7
7
import { useService } from "@web/core/utils/hooks" ;
8
8
9
- export class JsonFieldWidget extends TextField {
9
+ export class G2PRegistryAddlInfoComponent extends TextField {
10
+ static template = "g2p_reg_addl_info_tpl" ;
11
+
10
12
setup ( ) {
11
13
super . setup ( ) ;
12
- this . state = useState ( { recordClicked : false , noValue : false } ) ;
14
+ this . state = useState ( { editingMode : false } ) ;
15
+ this . textareaRef = useRef ( "textarea" ) ;
13
16
this . notification = useService ( "notification" ) ;
14
- onMounted ( ( ) => this . validateValue ( ) ) ;
15
- useExternalListener ( window , "click" , this . onclick ) ;
16
- useExternalListener ( window , "mouseup" , this . onMouseup ) ;
17
17
}
18
18
19
- validateValue ( ) {
20
- const val = this . props . record . data . additional_g2p_info ;
21
- if ( val ) {
22
- if ( ( ! ( val . charAt ( 0 ) === "{" ) && ! ( val . charAt ( val . length - 1 ) === "}" ) ) || ! val ) {
23
- this . state . noValue = true ;
24
- }
25
- } else {
26
- this . state . noValue = true ;
19
+ editButtonClick ( ) {
20
+ const val = this . props . record . data [ this . props . name ] ;
21
+ if ( typeof val !== "string" ) {
22
+ this . props . record . data [ this . props . name ] = JSON . stringify ( val ) ;
27
23
}
24
+ this . state . editingMode = true ;
28
25
}
29
26
30
- onclick ( event ) {
31
- if ( this . editingRecord && event . target . closest ( ".json-widget" ) ) {
32
- this . state . recordClicked = true ;
33
- this . state . noValue = true ;
27
+ doneButtonClick ( ) {
28
+ let val = null ;
29
+ try {
30
+ val = JSON . parse ( this . textareaRef . el . value ) ;
31
+ } catch ( err ) {
32
+ this . notification . add ( _t ( "Registry Additional Info" ) , {
33
+ title : _t ( "Invalid Json Value" ) ,
34
+ type : "danger" ,
35
+ } ) ;
36
+ console . error ( err ) ;
37
+ return ;
34
38
}
35
- this . validateValue ( ) ;
36
- }
37
-
38
- onMouseup ( ev ) {
39
- if ( ! ev . target . closest ( ".o_field_g2p_registry_addl_info_widget textarea" ) ) {
40
- this . state . recordClicked = false ;
41
- this . state . noValue = false ;
39
+ try {
40
+ this . props . record . update ( { [ this . props . name ] : val } ) ;
41
+ this . state . editingMode = false ;
42
+ } catch ( err ) {
43
+ this . notification . add ( _t ( "Registry Additional Info" ) , {
44
+ title : _t ( "Error Updating Json" ) ,
45
+ type : "danger" ,
46
+ } ) ;
47
+ console . error ( err ) ;
42
48
}
43
- this . validateValue ( ) ;
44
- }
45
-
46
- get editingRecord ( ) {
47
- return ! this . props . readonly ;
48
49
}
49
50
50
51
renderjson ( ) {
52
+ const valuesJsonOrig = this . props . record . data [ this . props . name ] ;
51
53
try {
52
- const valuesJsonOrig = this . props . record . data . additional_g2p_info ;
53
- if ( typeof valuesJsonOrig === "string" || valuesJsonOrig instanceof String ) {
54
- const parsedValue = JSON . parse ( valuesJsonOrig ) ;
55
- return parsedValue ;
56
- }
57
-
58
54
if ( Array . isArray ( valuesJsonOrig ) ) {
59
55
const sectionsJson = { } ;
60
56
valuesJsonOrig . forEach ( ( element ) => {
@@ -65,31 +61,51 @@ export class JsonFieldWidget extends TextField {
65
61
const valuesJson = this . flattenJson ( valuesJsonOrig ) ;
66
62
return valuesJson ;
67
63
} catch ( err ) {
68
- this . notification . add ( _t ( "Additional Information" ) , {
64
+ console . error ( err ) ;
65
+ this . notification . add ( _t ( "Registry Additional Info" ) , {
69
66
title : _t ( "Invalid Json Value" ) ,
70
67
type : "danger" ,
71
68
} ) ;
72
- this . state . recordClicked = true ;
69
+ this . state . editingMode = true ;
73
70
return { } ;
74
71
}
75
72
}
76
73
77
74
flattenJson ( object ) {
78
- const jsonObject = JSON . parse ( JSON . stringify ( object ) ) ;
75
+ let jsonObject = object ;
76
+ if ( typeof object === "string" ) {
77
+ jsonObject = JSON . parse ( object ) ;
78
+ }
79
79
for ( const key in jsonObject ) {
80
- if ( typeof jsonObject [ key ] === "object" ) {
81
- jsonObject [ key ] = JSON . stringify ( jsonObject [ key ] ) ;
80
+ if ( ! jsonObject [ key ] ) {
81
+ continue ;
82
+ } else if (
83
+ Array . isArray ( jsonObject [ key ] ) &&
84
+ jsonObject [ key ] . length > 0 &&
85
+ typeof jsonObject [ key ] [ 0 ] === "object" &&
86
+ "document_id" in jsonObject [ key ] [ 0 ] &&
87
+ "document_slug" in jsonObject [ key ] [ 0 ]
88
+ ) {
89
+ var documentFiles = "" ;
90
+ for ( var i = 0 ; i < jsonObject [ key ] . length ; i ++ ) {
91
+ const document_slug = jsonObject [ key ] [ i ] . document_slug ;
92
+ const host = window . location . origin ;
93
+ if ( i > 0 ) {
94
+ documentFiles += `<br />` ;
95
+ }
96
+ documentFiles += `<a href="${ host } /storage.file/${ document_slug } " target="_blank">${ document_slug } <span class="fa fa-fw fa-external-link"></span></a>` ;
97
+ }
98
+ jsonObject [ key ] = markup ( documentFiles ) ;
99
+ } else if ( typeof jsonObject [ key ] === "object" ) {
100
+ jsonObject [ key ] = this . flattenJson ( jsonObject [ key ] ) ;
82
101
}
83
102
}
84
103
return jsonObject ;
85
104
}
86
105
}
87
106
88
- JsonFieldWidget . template = "addl_info_each_table" ;
89
-
90
- export const JsonField = {
91
- component : JsonFieldWidget ,
92
- supportedTypes : [ "json" , "text" , "html" ] ,
107
+ export const g2pRegistryAddlInfoWidget = {
108
+ component : G2PRegistryAddlInfoComponent ,
109
+ supportedTypes : [ "jsonb" , "text" , "html" ] ,
93
110
} ;
94
-
95
- registry . category ( "fields" ) . add ( "g2p_registry_addl_info_widget" , JsonField ) ;
111
+ registry . category ( "fields" ) . add ( "g2p_registry_addl_info_widget" , g2pRegistryAddlInfoWidget ) ;
0 commit comments