Allow to hide actions (#2737)

Signed-off-by: Denis Bunakalya <denis.bunakalya@xored.com>
This commit is contained in:
Denis Bunakalya 2023-03-17 09:11:54 +03:00 committed by GitHub
parent aee24facdf
commit 26adf14966
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 3 deletions

View File

@ -62,8 +62,19 @@ export async function getActions (
const categories: Record<string, number> = { top: 1, filter: 50, tools: 100 }
let filteredActions = actions
let filteredActions = []
for (const action of actions) {
if (action.visibilityTester == null) {
filteredActions.push(action)
} else {
const visibilityTester = await getResource(action.visibilityTester)
if (await visibilityTester(doc)) {
filteredActions.push(action)
}
}
}
if (Array.isArray(doc)) {
for (const d of doc) {
filteredActions = filterActions(client, d, filteredActions, derived)

View File

@ -13,7 +13,7 @@
// limitations under the License.
-->
<script lang="ts">
import { WithLookup } from '@hcengineering/core'
import { WithLookup, Doc } from '@hcengineering/core'
import { getResource, translate } from '@hcengineering/platform'
import { createQuery, getClient } from '@hcengineering/presentation'
import ui, { Button, closePopup, Component, Icon, IconArrowLeft, Label } from '@hcengineering/ui'
@ -52,13 +52,32 @@
}
)
let visibleActions: WithLookup<Action>[] = []
let supportedActions: WithLookup<Action>[] = []
let filteredActions: WithLookup<Action>[] = []
async function filterVisibleActions (actions: WithLookup<Action>[], docs: Doc[]) {
const resultActions: WithLookup<Action>[] = []
for (const action of actions) {
if (!action.visibilityTester) {
resultActions.push(action)
} else {
const visibilityTester = await getResource(action.visibilityTester)
if (await visibilityTester(docs)) {
resultActions.push(action)
}
}
}
visibleActions = resultActions
}
$: filterVisibleActions(actions, getSelection($focusStore, $selectionStore))
const client = getClient()
$: {
let fActions: WithLookup<Action>[] = actions
let fActions: WithLookup<Action>[] = visibleActions
const docs = getSelection($focusStore, $selectionStore)
for (const d of docs) {

View File

@ -329,6 +329,9 @@ export interface Action<T extends Doc = Doc, P = Record<string, any>> extends Do
// Action is applicable only for objects matching criteria
query?: DocumentQuery<T>
// Action is shown only if the check is passed
visibilityTester?: Resource<(doc?: Doc | Doc[]) => Promise<boolean>>
// If defined, types should be matched to proposed list
inputProps?: Record<string, Ref<Class<Doc>>>