|
| 1 | +<script setup lang="ts"> |
| 2 | +import {ref} from 'vue'; |
| 3 | +
|
| 4 | +const expanded = ref(false); |
| 5 | +const emit = defineEmits(['add']); |
| 6 | +
|
| 7 | +const addFilter = (event: MouseEvent) => { |
| 8 | + const target = event.target as HTMLElement; |
| 9 | + const filter = target.closest('[data-role=filter]') as HTMLInputElement; |
| 10 | + const fields = Array.from(filter.querySelectorAll('input')); |
| 11 | + let pattern = String(filter.dataset.pattern); |
| 12 | + const strip = filter.dataset.strip; |
| 13 | + let replaced = false; |
| 14 | +
|
| 15 | + for (const input of fields) { |
| 16 | + const key = input.name; |
| 17 | + let val = input.value.trim(); |
| 18 | + if (strip !== undefined) { |
| 19 | + val = val.replace(strip, ''); |
| 20 | + } |
| 21 | +
|
| 22 | + const escapeVal = (val.indexOf(' ') === -1 ? val : '"' + val + '"'); |
| 23 | + const matches = pattern.match('\\{' + key + '(=)?\\}'); |
| 24 | + if (matches !== null) { |
| 25 | + pattern = pattern.replace(matches[0], val === '' ? '' : escapeVal + (matches[1] ?? '')); |
| 26 | + replaced = replaced || val !== ''; |
| 27 | + } |
| 28 | + input.value = ''; |
| 29 | + } |
| 30 | +
|
| 31 | + if (replaced) { |
| 32 | + emit('add', pattern); |
| 33 | + } |
| 34 | +} |
| 35 | +</script> |
| 36 | + |
| 37 | +<template> |
| 38 | + <button ref="filterButton" |
| 39 | + class="btn btn-outline-secondary dropdown-toggle" |
| 40 | + type="button" |
| 41 | + :aria-expanded="expanded" |
| 42 | + @click="expanded = !expanded">Filter |
| 43 | + </button> |
| 44 | + <div class="dropdown-menu slv-dropdown-menu" :class="{'d-block': expanded}"> |
| 45 | + <div class="px-2"> |
| 46 | + <div class="input-group mb-1" data-role="filter" data-pattern="before:{value}"> |
| 47 | + <span class="slv-input-label input-group-text" id="filter-date-start">Before</span> |
| 48 | + <input name="value" type="datetime-local" class="form-control" aria-label="Before" aria-describedby="filter-date-start"> |
| 49 | + <button class="btn btn-outline-primary" type="button" @click="addFilter">Add</button> |
| 50 | + </div> |
| 51 | + <div class="input-group mb-1" data-role="filter" data-pattern="after:{value}"> |
| 52 | + <span class="slv-input-label input-group-text" id="filter-date-end">After</span> |
| 53 | + <input name="value" type="datetime-local" class="form-control" aria-label="After" aria-describedby="filter-date-end"> |
| 54 | + <button class="btn btn-outline-primary" type="button" @click="addFilter">Add</button> |
| 55 | + </div> |
| 56 | + <div class="input-group mb-1" data-role="filter" data-pattern="severity:{value}" data-strip=" "> |
| 57 | + <span class="slv-input-label input-group-text" id="filter-severity">Severity</span> |
| 58 | + <input name="value" |
| 59 | + type="text" |
| 60 | + class="form-control" |
| 61 | + placeholder="Separate multiple by pipe symbol" |
| 62 | + aria-label="Severity" |
| 63 | + aria-describedby="filter-severity"> |
| 64 | + <button class="btn btn-outline-primary" type="button" @click="addFilter">Add</button> |
| 65 | + </div> |
| 66 | + <div class="input-group mb-1" data-role="filter" data-pattern="channel:{value}" data-strip=" "> |
| 67 | + <span class="slv-input-label input-group-text" id="filter-severity">Channels</span> |
| 68 | + <input name="value" |
| 69 | + type="text" |
| 70 | + class="form-control" |
| 71 | + placeholder="Separate multiple by pipe symbol" |
| 72 | + aria-label="Severity" |
| 73 | + aria-describedby="filter-severity"> |
| 74 | + <button class="btn btn-outline-primary" type="button" @click="addFilter">Add</button> |
| 75 | + </div> |
| 76 | + <div class="input-group mb-1" data-role="filter" data-pattern="exclude:{value}"> |
| 77 | + <span class="slv-input-label input-group-text" id="filter-exclude">Exclude</span> |
| 78 | + <input name="value" type="text" class="form-control" aria-label="Exclude string" aria-describedby="filter-exclude"> |
| 79 | + <button class="btn btn-outline-primary" type="button" @click="addFilter">Add</button> |
| 80 | + </div> |
| 81 | + <div class="input-group mb-1" data-role="filter" data-pattern="context:{key=}{value}"> |
| 82 | + <span class="slv-input-label input-group-text" id="filter-context">Context</span> |
| 83 | + <input name="key" |
| 84 | + type="text" |
| 85 | + class="form-control" |
| 86 | + placeholder="key (optional)" |
| 87 | + aria-label="Context key (optional)" |
| 88 | + aria-describedby="filter-context"> |
| 89 | + <input name="value" type="text" class="form-control" placeholder="search" aria-label="Context" aria-describedby="filter-context"> |
| 90 | + <button class="btn btn-outline-primary" type="button" @click="addFilter">Add</button> |
| 91 | + </div> |
| 92 | + <div class="input-group mb-1" data-role="filter" data-pattern="extra:{key=}{value}"> |
| 93 | + <span class="slv-input-label input-group-text" id="filter-extra">Extra</span> |
| 94 | + <input name="key" |
| 95 | + type="text" |
| 96 | + class="form-control" |
| 97 | + placeholder="key (optional)" |
| 98 | + aria-label="Extra key (optional)" |
| 99 | + aria-describedby="filter-extra"> |
| 100 | + <input name=value type="text" class="form-control" placeholder="search" aria-label="Extra" aria-describedby="filter-extra"> |
| 101 | + <button class="btn btn-outline-primary" type="button" @click="addFilter">Add</button> |
| 102 | + </div> |
| 103 | + <div> |
| 104 | + <button class="btn btn-sm btn-primary float-end" type="button" @click="expanded = !expanded">Close</button> |
| 105 | + </div> |
| 106 | + </div> |
| 107 | + </div> |
| 108 | +</template> |
| 109 | + |
| 110 | +<style scoped> |
| 111 | +.slv-dropdown-menu { |
| 112 | + top: 37px; |
| 113 | +} |
| 114 | +
|
| 115 | +.slv-input-label { |
| 116 | + width: 100px; |
| 117 | +} |
| 118 | +</style> |
0 commit comments