BulkActions.vue 4.2 KB

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