Skip to content

Commit

Permalink
fix tag field on media add/edit form
Browse files Browse the repository at this point in the history
  • Loading branch information
brookgagnon committed Jul 1, 2024
1 parent 0a11f98 commit 69dc300
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.3.3-20240630
5.3.3-20240701
23 changes: 16 additions & 7 deletions js/media/addedit.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,6 @@ OB.Media.mediaAddeditForm = function(id,title,editing)
});
}

// set default
if (metadata.settings && metadata.settings.default) {
metadataField.querySelector('.metadata_name_field').value = metadata.settings.default;
}

// change field name and description values
metadataField.querySelector('label').innerText = metadata.description;
metadataField.querySelector('.metadata_name_field').setAttribute('class', 'metadata_' + metadata.name + '_field');
Expand All @@ -172,6 +167,16 @@ OB.Media.mediaAddeditForm = function(id,title,editing)
let form = document.querySelector('#media_addedit_' + id);
let reference = form.querySelector('.addedit_form_container .copyright_field').parentElement;
reference.before(metadataField);

// set default
if (metadata.settings && metadata.settings.default) {
document.querySelector('#media_addedit_' + id + ' .metadata_' + metadata.name + '_field').value = metadata.settings.default;
}

// set suggestions
if (metadata.type=='tags' && metadata?.settings?.suggestions) {
document.querySelector('#media_addedit_' + id + ' .metadata_' + metadata.name + '_field').suggestions = metadata.settings.suggestions;
}
}
});

Expand Down Expand Up @@ -582,9 +587,13 @@ OB.Media.editPage = function(ids)
$.each(OB.Settings.media_metadata, function(index, metadata)
{
if(metadata.type=='tags') {
$form.find('.metadata_'+metadata.name+'_field').val( media['metadata_'+metadata.name].split(',') );
let newValue = [];
if (media['metadata_'+metadata.name]) {
newValue = media['metadata_'+metadata.name].split(',');
}
$form[0].querySelector('.metadata_'+metadata.name+'_field').value = newValue;
}
else $form.find('.metadata_'+metadata.name+'_field').val( media['metadata_'+metadata.name] );
else $form[0].querySelector('.metadata_'+metadata.name+'_field').value = media['metadata_'+metadata.name];
});

items_selected = true;
Expand Down
31 changes: 23 additions & 8 deletions ui/fields/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ class OBFieldTags extends OBField {

this.renderComponent();
}

renderEdit() {
if (! this.#tags) {
if (!this.#tags) {
this.#tags = [];
}
if (! this.#suggestions) {

if (!this.#suggestions) {
this.#suggestions = [];
}

Expand All @@ -43,9 +43,9 @@ class OBFieldTags extends OBField {
<span id="current">${this.#currentTag}</span>
</div>
</div>
${this.#suggestions.filter((tag) => ! this.#tags.includes(tag)).length > 0 && html`
${this.#suggestions.filter((tag) => !this.#tags.includes(tag)).length > 0 && html`
<div id="suggestions" tabindex="0">
${this.#suggestions.filter((tag) => ! this.#tags.includes(tag)).map((tag) => html`
${this.#suggestions.filter((tag) => !this.#tags.includes(tag)).map((tag) => html`
<span class="suggestion" onclick=${(e) => this.tagsAdd(tag)}>${tag}</span>
`)}
</div>
Expand Down Expand Up @@ -108,7 +108,7 @@ class OBFieldTags extends OBField {
}
#suggestions {
display: flex;
display: none;
align-items: center;
gap: 0.3em;
flex-wrap: wrap;
Expand All @@ -126,13 +126,17 @@ class OBFieldTags extends OBField {
word-wrap: anywhere;
}
}
#root:focus-within {
#suggestions { display: flex; }
}
}
`;
}

tagsInput(event) {
let keyCode = (event.key.length === 1) ? event.key.charCodeAt(0) : false;

if ((keyCode >= 65 && keyCode <= 90) || (keyCode >= 97 && keyCode <= 122)) {
// A-Z and a-z
this.#currentTag += event.key;
Expand Down Expand Up @@ -191,6 +195,17 @@ class OBFieldTags extends OBField {
}
}

set suggestions(value) {
if (Array.isArray(value)) {
this.#suggestions = [...new Set(value)];
this.renderComponent();
}
}

get suggestions() {
return this.#suggestions;
}

#loadInnerTags() {
var tags = [];

Expand Down

0 comments on commit 69dc300

Please sign in to comment.