@@ -31,6 +31,8 @@ export default class ControlledVocabulary extends FieldManager {
31
31
this . paramKey = paramKey
32
32
this . fieldName = this . element . data ( 'fieldName' )
33
33
this . searchUrl = this . element . data ( 'autocompleteUrl' )
34
+ // Used to prevent index collisions for existing words when removing and adding back in values.
35
+ this . postRemovalAdjustment = 0
34
36
}
35
37
36
38
// Overrides FieldManager, because field manager uses the wrong selector
@@ -49,6 +51,15 @@ export default class ControlledVocabulary extends FieldManager {
49
51
// this._manageFocus()
50
52
// }
51
53
54
+ // Overrides FieldManager in order to display Remove button for values that exist at initial load time
55
+ _createRemoveControl ( ) {
56
+ if ( this . element . find ( 'input.multi-text-field' ) . val ( ) ) {
57
+ this . remover . addClass ( 'd-block' )
58
+ this . remover . addClass ( 'has-existing-value' )
59
+ }
60
+ $ ( this . fieldWrapperClass + ' .field-controls' , this . element ) . append ( this . remover )
61
+ }
62
+
52
63
// Overrides FieldManager in order to avoid doing a clone of the existing field
53
64
createNewField ( $activeField ) {
54
65
let $newField = this . _newFieldTemplate ( )
@@ -68,20 +79,24 @@ export default class ControlledVocabulary extends FieldManager {
68
79
}
69
80
70
81
_newFieldTemplate ( ) {
71
- let index = this . _maxIndex ( )
82
+ let index = this . _maxIndex ( ) + this . postRemovalAdjustment
72
83
let rowTemplate = this . _template ( )
73
84
let controls = this . controls . clone ( ) //.append(this.remover)
74
85
let row = $ ( rowTemplate ( { "paramKey" : this . paramKey ,
75
86
"name" : this . fieldName ,
76
87
"index" : index ,
77
- "class" : "controlled_vocabulary" } ) )
88
+ "class" : "controlled_vocabulary" ,
89
+ "placeholder" : "Search for a location..." } ) )
78
90
. append ( controls )
91
+ let removeButton = row . find ( '.remove' ) ;
92
+ removeButton . removeClass ( 'd-block' )
93
+ removeButton . removeClass ( 'has-existing-value' )
79
94
return row
80
95
}
81
96
82
97
get _source ( ) {
83
98
return "<li class=\"field-wrapper input-group input-append\">" +
84
- "<input class=\"string {{class}} optional form-control {{paramKey}}_{{name}} form-control multi-text-field\" name=\"{{paramKey}}[{{name}}_attributes][{{index}}][hidden_label]\" value=\"\" id=\"{{paramKey}}_{{name}}_attributes_{{index}}_hidden_label\" data-attribute=\"{{name}}\" type=\"text\">" +
99
+ "<input class=\"string {{class}} optional form-control {{paramKey}}_{{name}} form-control multi-text-field\" name=\"{{paramKey}}[{{name}}_attributes][{{index}}][hidden_label]\" value=\"\" id=\"{{paramKey}}_{{name}}_attributes_{{index}}_hidden_label\" data-attribute=\"{{name}}\" type=\"text\" placeholder=\"{{placeholder}}\" >" +
85
100
"<input name=\"{{paramKey}}[{{name}}_attributes][{{index}}][id]\" value=\"\" id=\"{{paramKey}}_{{name}}_attributes_{{index}}_id\" type=\"hidden\" data-id=\"remote\">" +
86
101
"<input name=\"{{paramKey}}[{{name}}_attributes][{{index}}][_destroy]\" id=\"{{paramKey}}_{{name}}_attributes_{{index}}__destroy\" value=\"\" data-destroy=\"true\" type=\"hidden\"></li>"
87
102
}
@@ -114,9 +129,27 @@ export default class ControlledVocabulary extends FieldManager {
114
129
// '_destroy' hidden parameter
115
130
removeFromList ( event ) {
116
131
event . preventDefault ( )
132
+ // Changing behavior of the remove button to add a new field if the last field is removed
133
+ // Using querySelector to find elements with data-attribute="based_near"
134
+ const inputElements = this . element . find ( 'input' + this . inputTypeClass )
135
+ const parentsArray = Array . from ( inputElements ) . map ( element => element . parentElement )
136
+ const nonHiddenElements = parentsArray . filter ( element => element . style . display !== 'none' )
137
+ const nonHiddenCount = nonHiddenElements . length
138
+ if ( nonHiddenCount < 2 ) {
139
+ let $listing = $ ( event . target ) . closest ( this . inputTypeClass ) . find ( this . listClass )
140
+ let $activeField = $listing . children ( 'li' ) . last ( )
141
+ $listing . append ( this . createNewField ( $activeField ) )
142
+ this . postRemovalAdjustment += 1
143
+ }
117
144
let field = $ ( event . target ) . parents ( this . fieldWrapperClass )
118
- field . find ( '[data-destroy]' ) . val ( 'true' )
119
- field . hide ( )
145
+ // Removes field if a value hasn't been selected, otherwise marks it for destruction.
146
+ // Prevents bug caused by marking empty fields for destruction.
147
+ if ( field . find ( '.has-existing-value' ) . length > 0 ) {
148
+ field . find ( '[data-destroy]' ) . val ( 'true' )
149
+ field . hide ( )
150
+ } else {
151
+ field . remove ( )
152
+ }
120
153
this . element . trigger ( "managed_field:remove" , field )
121
154
}
122
155
}
0 commit comments