@@ -12,38 +12,54 @@ export default (params) => ({
12
12
hashError : true ,
13
13
showEditable : false ,
14
14
editableInput : '' ,
15
+
15
16
init ( ) {
16
17
if ( this . content . length === 0 && this . fallback ) {
17
18
this . content = this . htmlSpecialChars ( this . fallback ) ;
18
19
}
19
20
20
- this . hash = this . dataField + '-' + this . id
21
+ this . hash = this . dataField + '-' + this . id ;
22
+
23
+ window . addEventListener ( 'toggle-' + this . hash , ( ) => {
24
+ this . observe (
25
+ ( ) => document . getElementById ( 'clickable-' + this . hash ) ,
26
+ ( clickableElement ) => {
27
+ clickableElement . click ( ) ;
28
+ this . observe (
29
+ ( ) => document . getElementById ( 'editable-' + this . hash ) ,
30
+ ( editableElement ) => {
31
+ this . setFocusToEnd ( editableElement ) ;
32
+ }
33
+ ) ;
34
+ }
35
+ ) ;
36
+ } ) ;
21
37
22
38
this . $watch ( 'editable' , ( value ) => {
23
39
if ( value ) {
24
- let showEditable = false
25
- this . showEditable = false
40
+ let showEditable = false ;
41
+ this . showEditable = false ;
26
42
this . content = this . htmlSpecialChars ( this . content ) ;
27
43
this . oldContent = this . content ;
28
- this . hashError = this . store ( ) . notContains ( this . hash )
29
-
30
- setTimeout ( ( ) => {
31
- const editableElement = document . getElementById ( 'editable-' + this . hash )
32
-
33
- if ( this . store ( ) . getTextContent ( this . hash ) && editableElement ) {
34
- editableElement . textContent = this . store ( ) . getTextContent ( this . hash )
44
+ this . hashError = this . store ( ) . notContains ( this . hash ) ;
45
+
46
+ this . observe (
47
+ ( ) => document . getElementById ( 'editable-' + this . hash ) ,
48
+ ( editableElement ) => {
49
+ if ( this . store ( ) . getTextContent ( this . hash ) ) {
50
+ editableElement . textContent = this . store ( ) . getTextContent ( this . hash ) ;
51
+ }
35
52
}
36
- } , 220 )
53
+ ) ;
37
54
38
55
if ( this . hashError ) {
39
- const pendingHash = this . store ( ) . get ( this . hash )
40
- const clickableElement = document . getElementById ( 'clickable-' + pendingHash )
41
-
56
+ const pendingHash = this . store ( ) . get ( this . hash ) ;
57
+ const clickableElement = document . getElementById ( 'clickable-' + pendingHash ) ;
42
58
if ( clickableElement ) {
43
- clickableElement . click ( )
59
+ clickableElement . click ( ) ;
44
60
}
45
61
} else {
46
- showEditable = true
62
+ showEditable = true ;
47
63
}
48
64
49
65
this . editableInput = `
@@ -62,78 +78,104 @@ export default (params) => ({
62
78
</div>` ;
63
79
64
80
this . $nextTick ( ( ) => setTimeout ( ( ) => {
65
- this . showEditable = showEditable
66
- this . focus ( )
67
- } , 150 ) )
81
+ this . showEditable = showEditable ;
82
+ this . focus ( ) ;
83
+ } , 150 ) ) ;
68
84
}
69
- } )
85
+ } ) ;
70
86
71
87
this . content = this . htmlSpecialChars ( this . content ) ;
72
88
} ,
73
89
74
90
store ( ) {
75
- return window . editOnClickValidation
91
+ return window . editOnClickValidation ;
76
92
} ,
77
93
78
94
save ( ) {
79
- this . store ( ) . clear ( )
80
- this . store ( ) . set ( this . hash , this . $el . textContent )
81
-
82
- setTimeout ( ( ) => {
83
- document . getElementById ( 'clickable-' + this . hash ) . textContent =
84
- this . $el . textContent
85
- } , 230 )
86
-
87
- setTimeout ( ( ) => {
88
- window . addEventListener ( 'pg:editable-close-' + this . id , ( ) => {
89
- this . store ( ) . clear ( )
90
- this . editable = false ;
91
- this . showEditable = false ;
92
- } )
93
-
94
- if ( ! this . store ( ) . has ( this . hash ) ) {
95
- this . store ( ) . set ( this . hash , this . $el . textContent )
95
+ this . store ( ) . clear ( ) ;
96
+ this . store ( ) . set ( this . hash , this . $el . textContent ) ;
96
97
98
+ this . observe (
99
+ ( ) => document . getElementById ( 'clickable-' + this . hash ) ,
100
+ ( clickableElement ) => {
101
+ clickableElement . textContent = this . $el . textContent ;
97
102
}
103
+ ) ;
98
104
99
- this . $wire . dispatch ( 'pg:editable-' + this . $wire . tableName , {
100
- id : this . id ,
101
- value : this . $el . textContent ,
102
- field : this . dataField
103
- } )
105
+ window . addEventListener ( 'pg:editable-close- ' + this . id , ( ) => {
106
+ this . store ( ) . clear ( ) ;
107
+ this . editable = false ;
108
+ this . showEditable = false ;
109
+ } ) ;
104
110
105
- this . oldContent = this . store ( ) . getTextContent ( this . hash )
111
+ if ( ! this . store ( ) . has ( this . hash ) ) {
112
+ this . store ( ) . set ( this . hash , this . $el . textContent ) ;
113
+ }
114
+
115
+ this . $wire . dispatch ( 'pg:editable-' + this . $wire . tableName , {
116
+ id : this . id ,
117
+ value : this . $el . textContent ,
118
+ field : this . dataField
119
+ } ) ;
106
120
107
- this . $nextTick ( ( ) => setTimeout ( ( ) => {
108
- this . focus ( )
109
- setTimeout ( ( ) => this . $el . setAttribute ( 'value' , '' ) , 200 )
110
- } , 100 ) )
121
+ this . oldContent = this . store ( ) . getTextContent ( this . hash ) ;
111
122
112
- } , 100 )
123
+ this . $nextTick ( ( ) => {
124
+ this . focus ( ) ;
125
+ this . $el . setAttribute ( 'value' , '' ) ;
126
+ } ) ;
113
127
114
- this . content = this . htmlSpecialChars ( this . $el . textContent )
128
+ this . content = this . htmlSpecialChars ( this . $el . textContent ) ;
115
129
} ,
116
130
117
131
focus ( ) {
118
- const selection = window . getSelection ( ) ;
119
- const range = document . createRange ( ) ;
120
- selection . removeAllRanges ( ) ;
121
- range . selectNodeContents ( this . $el ) ;
122
- range . collapse ( false ) ;
123
- selection . addRange ( range ) ;
124
- this . $el . focus ( ) ;
132
+ this . setFocusToEnd ( this . $el ) ;
125
133
} ,
126
134
127
135
cancel ( ) {
136
+ this . store ( ) . clear ( ) ;
128
137
this . $refs . editable . textContent = this . oldContent ;
129
138
this . content = this . oldContent ;
130
139
this . editable = false ;
131
140
this . showEditable = false ;
141
+
142
+ if ( this . $refs . error ) {
143
+ this . $refs . error . innerHTML = '' ;
144
+ }
132
145
} ,
133
146
134
147
htmlSpecialChars ( string ) {
135
148
const el = document . createElement ( 'div' ) ;
136
149
el . innerHTML = string ;
137
150
return el . textContent ;
151
+ } ,
152
+
153
+ observe ( elementFinder , action ) {
154
+ const observer = new MutationObserver ( ( mutationsList , observer ) => {
155
+ for ( let mutation of mutationsList ) {
156
+ if ( mutation . type === 'childList' ) {
157
+ const element = elementFinder ( ) ;
158
+ if ( element ) {
159
+ action ( element ) ;
160
+ observer . disconnect ( ) ;
161
+ break ;
162
+ }
163
+ }
164
+ }
165
+ } ) ;
166
+
167
+ observer . observe ( document . body , { childList : true , subtree : true } ) ;
168
+ } ,
169
+
170
+ setFocusToEnd ( element ) {
171
+ const selection = window . getSelection ( ) ;
172
+ const range = document . createRange ( ) ;
173
+ range . selectNodeContents ( element ) ;
174
+ range . collapse ( false ) ;
175
+
176
+ selection . removeAllRanges ( ) ;
177
+ selection . addRange ( range ) ;
178
+
179
+ element . focus ( ) ;
138
180
}
139
- } )
181
+ } ) ;
0 commit comments