BulkActions.vue 3.9 KB

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