EditRolesDialog.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <template>
  2. <base-dialog @cancel="cancelDialog">
  3. <div slot="header">{{params.title}}</div>
  4. <div slot="body">
  5. <dialog-selected-tips :name="$t('dictionary.project')" :count="params.data.length" :action="params.title" />
  6. <dialog-table :data="params.data" :columns="params.columns.slice(0, 3)" />
  7. <a-form
  8. :form="form.fc"
  9. v-bind="formItemLayout">
  10. <a-form-item :label="$t('dictionary.role')">
  11. <a-select
  12. :filterOption="filterOption"
  13. :loading="roleLoading"
  14. v-decorator="decorators.roles"
  15. mode="multiple"
  16. :placeholder="$t('rules.role')"
  17. @deselect="handleDeselect"
  18. @select="handleSelect">
  19. <a-select-option v-for="item in roleList" :key="item.id">
  20. {{item.name}}
  21. </a-select-option>
  22. </a-select>
  23. </a-form-item>
  24. </a-form>
  25. </div>
  26. <div slot="footer">
  27. <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t('dialog.ok') }}</a-button>
  28. <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
  29. </div>
  30. </base-dialog>
  31. </template>
  32. <script>
  33. import * as R from 'ramda'
  34. import DialogMixin from '@/mixins/dialog'
  35. import WindowsMixin from '@/mixins/windows'
  36. export default {
  37. name: 'GroupEditRolesDialog',
  38. mixins: [DialogMixin, WindowsMixin],
  39. data () {
  40. return {
  41. loading: false,
  42. domainId: this.params.data[0].domain_id,
  43. roleList: [],
  44. roleLoading: false,
  45. initialValue: this.params.data[0].roles.map(item => { return item.id }),
  46. deselectList: [],
  47. selectList: [],
  48. form: {
  49. fc: this.$form.createForm(this),
  50. },
  51. decorators: {
  52. roles: [
  53. 'roles',
  54. {
  55. rules: [
  56. { required: true, message: this.$t('rules.role') },
  57. ],
  58. },
  59. ],
  60. },
  61. formItemLayout: {
  62. wrapperCol: {
  63. span: 21,
  64. },
  65. labelCol: {
  66. span: 3,
  67. },
  68. },
  69. }
  70. },
  71. created () {
  72. this.manager = new this.$Manager('groups', 'v1')
  73. this.fetchRoles()
  74. },
  75. methods: {
  76. async fetchRoles () {
  77. const manager = new this.$Manager('roles', 'v1')
  78. const params = {
  79. domain_id: this.domainId,
  80. scope: this.$store.getters.scope,
  81. }
  82. this.roleLoading = true
  83. try {
  84. const { data = {} } = await manager.list({ params })
  85. this.roleList = data.data || []
  86. this.form.fc.getFieldDecorator('roles', { initialValue: this.initialValue })
  87. } catch (err) {
  88. throw err
  89. } finally {
  90. this.roleLoading = false
  91. }
  92. },
  93. filterOption (input, option) {
  94. return (
  95. option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
  96. )
  97. },
  98. handleDeselect (value, option) {
  99. if (R.includes(value, this.initialValue) && !R.includes(value, this.deselectList)) this.deselectList.push(value)
  100. if (!R.includes(value, this.initialValue) && R.includes(value, this.selectList)) {
  101. for (let i = 0; i < this.selectList.length; i++) {
  102. if (this.selectList[i] === value) {
  103. this.selectList.splice(i, 1)
  104. }
  105. }
  106. }
  107. },
  108. handleSelect (val) {
  109. if (!R.includes(val, this.initialValue) && !R.includes(val, this.selectList)) this.selectList.push(val)
  110. if (R.includes(val, this.initialValue) && R.includes(val, this.deselectList)) {
  111. for (let i = 0; i < this.deselectList.length; i++) {
  112. if (this.deselectList[i] === val) {
  113. this.deselectList.splice(i, 1)
  114. }
  115. }
  116. }
  117. },
  118. doJoint (roles) {
  119. return this.manager.performAction({
  120. id: this.params.uid,
  121. action: 'join',
  122. data: {
  123. projects: [this.params.data[0].id],
  124. roles: roles,
  125. },
  126. })
  127. },
  128. doLeave (roles) {
  129. return this.manager.performAction({
  130. id: this.params.uid,
  131. action: 'leave',
  132. data: {
  133. group: true,
  134. project_roles: roles,
  135. },
  136. })
  137. },
  138. async handleConfirm () {
  139. this.loading = true
  140. try {
  141. if (this.selectList.length > 0) {
  142. const { roles } = await this.form.fc.validateFields()
  143. await this.doJoint(roles)
  144. }
  145. if (this.deselectList.length > 0) {
  146. const roles = this.deselectList.map(item => ({
  147. project: this.params.data[0].id,
  148. role: item,
  149. }))
  150. await this.doLeave(roles)
  151. }
  152. this.cancelDialog()
  153. this.params.refresh()
  154. } catch (error) {
  155. this.loading = false
  156. throw error
  157. }
  158. },
  159. },
  160. }
  161. </script>