|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +<template> |
| 19 | + <span class="row-action-button"> |
| 20 | + <a-tooltip |
| 21 | + v-for="(action, actionIndex) in actions" |
| 22 | + :key="actionIndex" |
| 23 | + arrowPointAtCenter |
| 24 | + placement="bottomRight"> |
| 25 | + <template slot="title"> |
| 26 | + {{ $t(action.label) }} |
| 27 | + </template> |
| 28 | + <a-badge |
| 29 | + class="button-action-badge" |
| 30 | + :overflowCount="9" |
| 31 | + :count="actionBadge[action.api] ? actionBadge[action.api].badgeNum : 0" |
| 32 | + v-if="action.api in $store.getters.apis && |
| 33 | + action.showBadge && |
| 34 | + ((!dataView && (action.listView || action.groupAction && selectedRowKeys.length > 0)) || (dataView && action.dataView)) && |
| 35 | + ('show' in action ? action.show(resource, $store.getters.userInfo) : true)"> |
| 36 | + <a-button |
| 37 | + :icon="action.icon" |
| 38 | + :type="action.icon === 'delete' ? 'danger' : (action.icon === 'plus' ? 'primary' : 'default')" |
| 39 | + shape="circle" |
| 40 | + style="margin-right: 5px" |
| 41 | + @click="execAction(action)" /> |
| 42 | + </a-badge> |
| 43 | + <a-button |
| 44 | + v-if="action.api in $store.getters.apis && |
| 45 | + !action.showBadge && |
| 46 | + ((!dataView && (action.listView || action.groupAction && selectedRowKeys.length > 0)) || (dataView && action.dataView)) && |
| 47 | + ('show' in action ? action.show(resource, $store.getters.userInfo) : true)" |
| 48 | + :icon="action.icon" |
| 49 | + :type="action.icon === 'delete' ? 'danger' : (action.icon === 'plus' ? 'primary' : 'default')" |
| 50 | + shape="circle" |
| 51 | + style="margin-left: 5px" |
| 52 | + @click="execAction(action)" /> |
| 53 | + </a-tooltip> |
| 54 | + </span> |
| 55 | +</template> |
| 56 | + |
| 57 | +<script> |
| 58 | +import { api } from '@/api' |
| 59 | +
|
| 60 | +export default { |
| 61 | + name: 'ActionButton', |
| 62 | + data () { |
| 63 | + return { |
| 64 | + actionBadge: [] |
| 65 | + } |
| 66 | + }, |
| 67 | + mounted () { |
| 68 | + this.handleShowBadge() |
| 69 | + }, |
| 70 | + props: { |
| 71 | + actions: { |
| 72 | + type: Array, |
| 73 | + default () { |
| 74 | + return [] |
| 75 | + } |
| 76 | + }, |
| 77 | + resource: { |
| 78 | + type: Object, |
| 79 | + default () { |
| 80 | + return {} |
| 81 | + } |
| 82 | + }, |
| 83 | + dataView: { |
| 84 | + type: Boolean, |
| 85 | + default: false |
| 86 | + }, |
| 87 | + selectedRowKeys: { |
| 88 | + type: Array, |
| 89 | + default () { |
| 90 | + return [] |
| 91 | + } |
| 92 | + }, |
| 93 | + loading: { |
| 94 | + type: Boolean, |
| 95 | + default: false |
| 96 | + } |
| 97 | + }, |
| 98 | + watch: { |
| 99 | + resource (newItem, oldItem) { |
| 100 | + if (!newItem || !newItem.id) { |
| 101 | + return |
| 102 | + } |
| 103 | + this.handleShowBadge() |
| 104 | + } |
| 105 | + }, |
| 106 | + methods: { |
| 107 | + execAction (action) { |
| 108 | + this.$emit('exec-action', action) |
| 109 | + }, |
| 110 | + handleShowBadge () { |
| 111 | + const dataBadge = {} |
| 112 | + const arrAsync = [] |
| 113 | + const actionBadge = this.actions.filter(action => action.showBadge === true) |
| 114 | +
|
| 115 | + if (actionBadge && actionBadge.length > 0) { |
| 116 | + const dataLength = actionBadge.length |
| 117 | +
|
| 118 | + for (let i = 0; i < dataLength; i++) { |
| 119 | + const action = actionBadge[i] |
| 120 | +
|
| 121 | + arrAsync.push(new Promise((resolve, reject) => { |
| 122 | + api(action.api, action.param).then(json => { |
| 123 | + let responseJsonName |
| 124 | + const response = {} |
| 125 | +
|
| 126 | + response.api = action.api |
| 127 | + response.count = 0 |
| 128 | +
|
| 129 | + for (const key in json) { |
| 130 | + if (key.includes('response')) { |
| 131 | + responseJsonName = key |
| 132 | + break |
| 133 | + } |
| 134 | + } |
| 135 | +
|
| 136 | + if (json[responseJsonName].count && json[responseJsonName].count > 0) { |
| 137 | + response.count = json[responseJsonName].count |
| 138 | + } |
| 139 | +
|
| 140 | + resolve(response) |
| 141 | + }).catch(error => { |
| 142 | + reject(error) |
| 143 | + }) |
| 144 | + })) |
| 145 | + } |
| 146 | +
|
| 147 | + Promise.all(arrAsync).then(response => { |
| 148 | + for (let j = 0; j < response.length; j++) { |
| 149 | + this.$set(dataBadge, response[j].api, {}) |
| 150 | + this.$set(dataBadge[response[j].api], 'badgeNum', response[j].count) |
| 151 | + } |
| 152 | + }) |
| 153 | +
|
| 154 | + this.actionBadge = dataBadge |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | +} |
| 159 | +</script> |
| 160 | + |
| 161 | +<style scoped > |
| 162 | +.button-action-badge { |
| 163 | + margin-left: 5px; |
| 164 | +} |
| 165 | +
|
| 166 | +/deep/.button-action-badge .ant-badge-count { |
| 167 | + right: 10px; |
| 168 | + z-index: 8; |
| 169 | +} |
| 170 | +</style> |
0 commit comments