Skip to content

Commit

Permalink
TermInput: Allow to arrange terms vertically
Browse files Browse the repository at this point in the history
  • Loading branch information
nilmerg committed Aug 10, 2023
1 parent 8de7d69 commit 1755dea
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 6 deletions.
15 changes: 15 additions & 0 deletions asset/css/compat.less
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,26 @@ form.icinga-form .control-group {
form.icinga-form .control-group {
> .term-input-area {
flex: 1 1 auto;

width: auto;
&.vertical {
width: 0;
}

input[type="text"] {
flex: unset;
width: 100%;
}
}
}

// SearchBar styles

.icinga-module.module-icingadb .controls .search-bar .filter-input-area {
label {
&::after,
input {
padding: 0 .5em;
}
}
}
33 changes: 30 additions & 3 deletions asset/css/search-base.less
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Style

.search-bar .filter-input-area,
.term-input-area {
.term-input-area:not(.vertical) {
// Scrollbar style

// Firefox
Expand Down Expand Up @@ -116,7 +116,7 @@

// Layout
.search-bar .filter-input-area,
.term-input-area {
.term-input-area:not(.vertical) {
overflow: auto hidden;
overflow-x: overlay; // Not invalid, but proprietary feature by chrome/webkit
display: flex;
Expand All @@ -134,7 +134,7 @@
&::after,
input {
width: auto;
padding: 0 .5em;
padding: .25em .5em;
resize: none;
}

Expand Down Expand Up @@ -174,6 +174,33 @@
}
}

.term-input-area.vertical {
display: flex;
flex-direction: column-reverse;

> .terms {
@gap: 1px;
@termsPerRow: 2;

display: flex;
flex-wrap: wrap;
gap: @gap;
margin-top: @gap;

label {
@termWidth: 100%/@termsPerRow;
@totalGapWidthPerRow: (@termsPerRow - 1) * @gap;

min-width: ~"calc(@{termWidth} - (@{totalGapWidthPerRow} / @{termsPerRow}))";
flex: 1 1 auto;

input {
text-overflow: ellipsis;
}
}
}
}

.term-input-area {
label input:focus {
@labelPad: 7/12em;
Expand Down
34 changes: 32 additions & 2 deletions asset/js/widget/BaseInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,10 @@ define(["../notjQuery", "Completer"], function ($, Completer) {
}

togglePlaceholder() {
if (this.isTermDirectionVertical()) {
return;
}

let placeholder = '';

if (! this.hasTerms()) {
Expand Down Expand Up @@ -619,6 +623,10 @@ define(["../notjQuery", "Completer"], function ($, Completer) {
);
}

isTermDirectionVertical() {
return this.input.dataset.termDirection === 'vertical';
}

moveFocusForward(from = null) {
let toFocus;

Expand Down Expand Up @@ -774,7 +782,9 @@ define(["../notjQuery", "Completer"], function ($, Completer) {
case 'Backspace':
removedTerms = this.clearSelectedTerms();

if (termIndex >= 0 && ! input.value) {
if (this.isTermDirectionVertical()) {
// pass
} else if (termIndex >= 0 && ! input.value) {
let removedTerm = this.removeTerm(input.parentNode);
if (removedTerm !== false) {
input = this.moveFocusBackward(termIndex);
Expand Down Expand Up @@ -806,7 +816,7 @@ define(["../notjQuery", "Completer"], function ($, Completer) {
case 'Delete':
removedTerms = this.clearSelectedTerms();

if (termIndex >= 0 && ! input.value) {
if (! this.isTermDirectionVertical() && termIndex >= 0 && ! input.value) {
let removedTerm = this.removeTerm(input.parentNode);
if (removedTerm !== false) {
input = this.moveFocusForward(termIndex - 1);
Expand Down Expand Up @@ -845,6 +855,26 @@ define(["../notjQuery", "Completer"], function ($, Completer) {
this.moveFocusForward();
}
break;
case 'ArrowUp':
if (this.isTermDirectionVertical()
&& input.selectionStart === 0
&& this.hasTerms()
&& (this.completer === null || ! this.completer.isBeingCompleted(input))
) {
event.preventDefault();
this.moveFocusBackward();
}
break;
case 'ArrowDown':
if (this.isTermDirectionVertical()
&& input.selectionStart === input.value.length
&& this.hasTerms()
&& (this.completer === null || ! this.completer.isBeingCompleted(input))
) {
event.preventDefault();
this.moveFocusForward();
}
break;
case 'a':
if ((event.ctrlKey || event.metaKey) && ! this.readPartialTerm(input)) {
this.selectTerms();
Expand Down
4 changes: 4 additions & 0 deletions asset/js/widget/FilterInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,10 @@ define(["../notjQuery", "BaseInput"], function ($, BaseInput) {
return termData;
}

isTermDirectionVertical() {
return false;
}

highlightTerm(label, highlightedBy = null) {
label.classList.add('highlighted');

Expand Down
30 changes: 29 additions & 1 deletion src/FormElement/TermInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class TermInput extends FieldsetElement
/** @var Url The suggestion url */
protected $suggestionUrl;

/** @var bool Whether term direction is vertical */
protected $verticalTermDirection = false;

/** @var array Changes to transmit to the client */
protected $changes = [];

Expand Down Expand Up @@ -76,6 +79,30 @@ public function getSuggestionUrl(): ?Url
return $this->suggestionUrl;
}

/**
* Set whether term direction should be vertical
*
* @param bool $state
*
* @return $this
*/
public function setVerticalTermDirection(bool $state = true): self
{
$this->verticalTermDirection = $state;

return $this;
}

/**
* Get the desired term direction
*
* @return ?string
*/
public function getTermDirection(): ?string
{
return $this->verticalTermDirection ? 'vertical' : null;
}

/**
* Set terms
*
Expand Down Expand Up @@ -386,6 +413,7 @@ public function getValueAttribute()
'data-term-separator' => ',',
'data-enrichment-type' => 'terms',
'data-no-auto-submit-on-remove' => true,
'data-term-direction' => $this->getTermDirection(),
'data-data-input' => '#' . $dataInputId,
'data-term-input' => '#' . $termInputId,
'data-term-container' => '#' . $termContainer->getAttribute('id')->getValue(),
Expand All @@ -407,7 +435,7 @@ public function getValueAttribute()

$mainInput->prependWrapper((new HtmlElement(
'div',
Attributes::create(['class' => 'term-input-area']),
Attributes::create(['class' => ['term-input-area', $this->getTermDirection()]]),
$termContainer,
new HtmlElement('label', null, $mainInput)
)));
Expand Down

0 comments on commit 1755dea

Please sign in to comment.