BulkActions.vue 4.1 KB

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