BulkActions.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <template>
  2. <div>
  3. <modal title="Bulk Actions" class="bulk-actions-modal">
  4. <template #body>
  5. <label class="label">Method</label>
  6. <div class="control is-expanded select">
  7. <select v-model="method">
  8. <option value="add">Add</option>
  9. <option value="remove">Remove</option>
  10. <option value="replace">Replace</option>
  11. </select>
  12. </div>
  13. <label class="label">{{ type.name.slice(0, -1) }}</label>
  14. <div class="control is-grouped input-with-button">
  15. <auto-suggest
  16. v-model="itemInput"
  17. :placeholder="`Enter ${type.name} to ${method}`"
  18. :all-items="allItems"
  19. @submitted="addItem()"
  20. />
  21. <p class="control">
  22. <button
  23. class="button is-primary material-icons"
  24. @click="addItem()"
  25. >
  26. add
  27. </button>
  28. </p>
  29. </div>
  30. <label class="label"
  31. >{{ type.name }} to be
  32. {{ method === "add" ? `added` : `${method}d` }}</label
  33. >
  34. <div v-if="items.length > 0">
  35. <div
  36. v-for="(item, index) in items"
  37. :key="`item-${item}`"
  38. class="pill"
  39. >
  40. {{ item }}
  41. <span
  42. class="material-icons remove-item"
  43. @click="removeItem(index)"
  44. content="Remove item"
  45. v-tippy
  46. >highlight_off</span
  47. >
  48. </div>
  49. </div>
  50. <p v-else>No {{ type.name }} specified</p>
  51. </template>
  52. <template #footer>
  53. <button
  54. class="button is-primary"
  55. :disabled="items.length === 0"
  56. @click="applyChanges()"
  57. >
  58. Apply Changes
  59. </button>
  60. </template>
  61. </modal>
  62. </div>
  63. </template>
  64. <script>
  65. import { mapGetters, mapActions } from "vuex";
  66. import Toast from "toasters";
  67. import AutoSuggest from "@/components/AutoSuggest.vue";
  68. import ws from "@/ws";
  69. export default {
  70. components: { AutoSuggest },
  71. props: {
  72. type: {
  73. type: Object,
  74. default: () => {}
  75. }
  76. },
  77. data() {
  78. return {
  79. method: "add",
  80. items: [],
  81. itemInput: null,
  82. allItems: []
  83. };
  84. },
  85. computed: {
  86. ...mapGetters({
  87. socket: "websockets/getSocket"
  88. })
  89. },
  90. beforeUnmount() {
  91. this.itemInput = null;
  92. this.items = [];
  93. },
  94. mounted() {
  95. ws.onConnect(this.init);
  96. },
  97. methods: {
  98. init() {
  99. if (this.type.autosuggest && this.type.autosuggestDataAction)
  100. this.socket.dispatch(this.type.autosuggestDataAction, res => {
  101. if (res.status === "success") {
  102. const { items } = res.data;
  103. this.allItems = items;
  104. } else {
  105. new Toast(res.message);
  106. }
  107. });
  108. },
  109. addItem() {
  110. if (!this.itemInput) return;
  111. if (this.type.regex && !this.type.regex.test(this.itemInput)) {
  112. new Toast(`Invalid ${this.type.name} format.`);
  113. } else if (this.items.includes(this.itemInput)) {
  114. new Toast(`Duplicate ${this.type.name} specified.`);
  115. } else {
  116. this.items.push(this.itemInput);
  117. this.itemInput = null;
  118. }
  119. },
  120. removeItem(index) {
  121. this.items.splice(index, 1);
  122. },
  123. applyChanges() {
  124. this.socket.dispatch(
  125. this.type.action,
  126. this.method,
  127. this.items,
  128. this.type.items,
  129. res => {
  130. new Toast(res.message);
  131. if (res.status === "success")
  132. this.closeModal("bulkActions");
  133. }
  134. );
  135. },
  136. ...mapActions("modalVisibility", ["closeModal"])
  137. }
  138. };
  139. </script>
  140. <style lang="less" scoped>
  141. .label {
  142. text-transform: capitalize;
  143. }
  144. .control.input-with-button > div {
  145. flex: 1;
  146. }
  147. .pill {
  148. display: inline-flex;
  149. .remove-item {
  150. font-size: 16px;
  151. margin: auto 2px auto 5px;
  152. cursor: pointer;
  153. }
  154. }
  155. :deep(.autosuggest-container) {
  156. width: calc(100% - 40px);
  157. top: unset;
  158. }
  159. </style>