Skip to content

Commit 2b3854f

Browse files
Remove button should always appear next to location/controlled vocab terms now, and should correctly allow for values to be removed on new and existing works. Adds placeholder text to location inputs after the first one. (#6720)
Co-authored-by: Dave <[email protected]>
1 parent 3542ca7 commit 2b3854f

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

app/assets/javascripts/hyrax/autocomplete/linked_data.es6

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ export default class LinkedData {
1818
let result = this.element.select2("data")
1919
this.element.select2("destroy")
2020
this.element.val(result.label).attr("readonly", "readonly")
21+
// Adding d-block class to the remove button to show it after a selection is made.
22+
let removeButton = this.element.closest('.field-wrapper').find('.input-group-btn.field-controls .remove')
23+
removeButton.addClass('d-block')
2124
this.setIdentifier(result.id)
2225
}
2326

app/assets/javascripts/hyrax/editor/controlled_vocabulary.es6

+38-5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ export default class ControlledVocabulary extends FieldManager {
3131
this.paramKey = paramKey
3232
this.fieldName = this.element.data('fieldName')
3333
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
3436
}
3537

3638
// Overrides FieldManager, because field manager uses the wrong selector
@@ -49,6 +51,15 @@ export default class ControlledVocabulary extends FieldManager {
4951
// this._manageFocus()
5052
// }
5153

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+
5263
// Overrides FieldManager in order to avoid doing a clone of the existing field
5364
createNewField($activeField) {
5465
let $newField = this._newFieldTemplate()
@@ -68,20 +79,24 @@ export default class ControlledVocabulary extends FieldManager {
6879
}
6980

7081
_newFieldTemplate() {
71-
let index = this._maxIndex()
82+
let index = this._maxIndex() + this.postRemovalAdjustment
7283
let rowTemplate = this._template()
7384
let controls = this.controls.clone()//.append(this.remover)
7485
let row = $(rowTemplate({ "paramKey": this.paramKey,
7586
"name": this.fieldName,
7687
"index": index,
77-
"class": "controlled_vocabulary" }))
88+
"class": "controlled_vocabulary",
89+
"placeholder": "Search for a location..." }))
7890
.append(controls)
91+
let removeButton = row.find('.remove');
92+
removeButton.removeClass('d-block')
93+
removeButton.removeClass('has-existing-value')
7994
return row
8095
}
8196

8297
get _source() {
8398
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}}\">" +
85100
"<input name=\"{{paramKey}}[{{name}}_attributes][{{index}}][id]\" value=\"\" id=\"{{paramKey}}_{{name}}_attributes_{{index}}_id\" type=\"hidden\" data-id=\"remote\">" +
86101
"<input name=\"{{paramKey}}[{{name}}_attributes][{{index}}][_destroy]\" id=\"{{paramKey}}_{{name}}_attributes_{{index}}__destroy\" value=\"\" data-destroy=\"true\" type=\"hidden\"></li>"
87102
}
@@ -114,9 +129,27 @@ export default class ControlledVocabulary extends FieldManager {
114129
// '_destroy' hidden parameter
115130
removeFromList( event ) {
116131
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+
}
117144
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+
}
120153
this.element.trigger("managed_field:remove", field)
121154
}
122155
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.controlled_vocabulary {
22
@extend .multi_value;
33

4-
.listing li:only-of-type .remove {
5-
display: block;
4+
.listing li .remove {
5+
display: none;
66
}
77
}

0 commit comments

Comments
 (0)