BulkActions.vue 4.1 KB

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