4
4
*/
5
5
6
6
define ( [
7
+ 'Magento_Ui/js/form/element/abstract' ,
7
8
'underscore' ,
8
- 'Magento_Ui/js/form/element/textarea '
9
- ] , function ( _ , Textarea ) {
9
+ 'uiRegistry '
10
+ ] , function ( Abstract , _ , registry ) {
10
11
'use strict' ;
11
12
12
- return Textarea . extend ( {
13
+ return Abstract . extend ( {
13
14
defaults : {
14
15
allowImport : true ,
15
16
autoImportIfEmpty : false ,
16
- values : {
17
- 'name' : '' ,
18
- 'description' : '' ,
19
- 'sku' : '' ,
20
- 'color' : '' ,
21
- 'country_of_manufacture' : '' ,
22
- 'gender' : '' ,
23
- 'material' : '' ,
24
- 'short_description' : '' ,
25
- 'size' : ''
26
- } ,
27
- valueUpdate : 'input' ,
28
- mask : ''
17
+ values : { } ,
18
+ mask : '' ,
19
+ queryTemplate : 'ns = ${ $.ns }, index = '
29
20
} ,
30
21
31
- /**
32
- * Handle name value changes, if it's allowed
33
- *
34
- * @param {String } newValue
35
- */
36
- handleNameChanges : function ( newValue ) {
37
- this . values . name = newValue ;
38
- this . updateValue ( ) ;
39
- } ,
40
-
41
- /**
42
- * Handle description value changes, if it's allowed
43
- *
44
- * @param {String } newValue
45
- */
46
- handleDescriptionChanges : function ( newValue ) {
47
- this . values . description = newValue ;
48
- this . updateValue ( ) ;
49
- } ,
22
+ /** @inheritdoc */
23
+ initialize : function ( ) {
24
+ this . _super ( ) ;
50
25
51
- /**
52
- * Handle sku value changes, if it's allowed
53
- *
54
- * @param {String } newValue
55
- */
56
- handleSkuChanges : function ( newValue ) {
57
- if ( this . code !== 'sku' ) {
58
- this . values . sku = newValue ;
59
- this . updateValue ( ) ;
26
+ if ( this . allowImport ) {
27
+ this . setHandlers ( ) ;
60
28
}
61
29
} ,
62
30
63
31
/**
64
- * Handle color value changes, if it's allowed
65
- *
66
- * @param {String } newValue
67
- */
68
- handleColorChanges : function ( newValue ) {
69
- this . values . color = newValue ;
70
- this . updateValue ( ) ;
71
- } ,
72
-
73
- /**
74
- * Handle country value changes, if it's allowed
75
- *
76
- * @param {String } newValue
32
+ * Split mask placeholder and attach events to placeholder fields.
77
33
*/
78
- handleCountryChanges : function ( newValue ) {
79
- this . values . country = newValue ;
80
- this . updateValue ( ) ;
81
- } ,
34
+ setHandlers : function ( ) {
35
+ var str = this . mask || '' ,
36
+ placeholders ;
82
37
83
- /**
84
- * Handle gender value changes, if it's allowed
85
- *
86
- * @param {String } newValue
87
- */
88
- handleGenderChanges : function ( newValue ) {
89
- this . values . gender = newValue ;
90
- this . updateValue ( ) ;
91
- } ,
38
+ placeholders = str . match ( / { { ( .* ?) } } / g) ; // Get placeholders
92
39
93
- /**
94
- * Handle material value changes, if it's allowed
95
- *
96
- * @param {String } newValue
97
- */
98
- handleMaterialChanges : function ( newValue ) {
99
- this . values . material = newValue ;
100
- this . updateValue ( ) ;
101
- } ,
40
+ _ . each ( placeholders , function ( placeholder ) {
41
+ placeholder = placeholder . replace ( / [ { { } } ] / g, '' ) ; // Remove curly braces
102
42
103
- /**
104
- * Handle short description value changes, if it's allowed
105
- *
106
- * @param {String } newValue
107
- */
108
- handleShortDescriptionChanges : function ( newValue ) {
109
- this . values [ 'short_description' ] = newValue ;
110
- this . updateValue ( ) ;
43
+ registry . get ( this . queryTemplate + placeholder , function ( component ) {
44
+ this . values [ placeholder ] = component . getPreview ( ) ;
45
+ component . on ( 'value' , this . updateValue . bind ( this , placeholder , component ) ) ;
46
+ component . valueUpdate = 'keyup' ;
47
+ } . bind ( this ) ) ;
48
+ } , this ) ;
111
49
} ,
112
50
113
51
/**
114
- * Handle size value changes , if it's allowed
52
+ * Update field with mask value , if it's allowed.
115
53
*
116
- * @param {String } newValue
54
+ * @param {Object } placeholder
55
+ * @param {Object } component
117
56
*/
118
- handleSizeChanges : function ( newValue ) {
119
- this . values . size = newValue ;
120
- this . updateValue ( ) ;
121
- } ,
57
+ updateValue : function ( placeholder , component ) {
58
+ var string = this . mask || '' ,
59
+ nonEmptyValueFlag = false ;
122
60
123
- /**
124
- * Update field value, if it's allowed
125
- */
126
- updateValue : function ( ) {
127
- var str = this . mask || '' ,
128
- nonEmptyValueFlag = false ,
129
- tmpElement ;
61
+ if ( placeholder ) {
62
+ this . values [ placeholder ] = component . getPreview ( ) || '' ;
63
+ }
130
64
131
65
if ( ! this . allowImport ) {
132
66
return ;
133
67
}
134
68
135
- if ( str ) {
136
- _ . each ( this . values , function ( propertyValue , propertyName ) {
137
- str = str . replace ( '{{' + propertyName + '}}' , propertyValue ) ;
138
- nonEmptyValueFlag = nonEmptyValueFlag || ! ! propertyValue ;
139
- } ) ;
140
- }
141
-
142
- // strip tags
143
- tmpElement = document . createElement ( 'div' ) ;
144
- tmpElement . innerHTML = str ;
145
- str = tmpElement . textContent || tmpElement . innerText || '' ;
69
+ _ . each ( this . values , function ( propertyValue , propertyName ) {
70
+ string = string . replace ( '{{' + propertyName + '}}' , propertyValue ) ;
71
+ nonEmptyValueFlag = nonEmptyValueFlag || ! ! propertyValue ;
72
+ } ) ;
146
73
147
74
if ( nonEmptyValueFlag ) {
148
- this . value ( str ) ;
75
+ string = string . replace ( / ( < ( [ ^ > ] + ) > ) / ig, '' ) ; // Remove html tags
76
+ this . value ( string ) ;
77
+ } else {
78
+ this . value ( '' ) ;
149
79
}
150
80
} ,
151
81
@@ -169,13 +99,20 @@ define([
169
99
* and disallow/allow import value
170
100
*/
171
101
userChanges : function ( ) {
102
+
103
+ /**
104
+ * As userChanges is called before updateValue,
105
+ * we forced to get value from component by reference
106
+ */
107
+ var actualValue = arguments [ 1 ] . currentTarget . value ;
108
+
172
109
this . _super ( ) ;
173
110
174
- if ( this . value ( ) === '' ) {
111
+ if ( actualValue === '' ) {
175
112
this . allowImport = true ;
176
113
177
114
if ( this . autoImportIfEmpty ) {
178
- this . updateValue ( ) ;
115
+ this . updateValue ( null , null ) ;
179
116
}
180
117
} else {
181
118
this . allowImport = false ;
0 commit comments