BulkActions.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <template>
  2. <div>
  3. <modal title="Bulk Actions" class="bulk-actions-modal" size="slim">
  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. import { mapModalState } from "@/vuex_helpers";
  70. export default {
  71. components: { AutoSuggest },
  72. props: {
  73. modalUuid: { type: String, default: "" }
  74. },
  75. data() {
  76. return {
  77. method: "add",
  78. items: [],
  79. itemInput: null,
  80. allItems: []
  81. };
  82. },
  83. computed: {
  84. ...mapModalState("modals/bulkActions/MODAL_UUID", {
  85. type: state => state.type
  86. }),
  87. ...mapGetters({
  88. socket: "websockets/getSocket"
  89. })
  90. },
  91. beforeUnmount() {
  92. this.itemInput = null;
  93. this.items = [];
  94. // Delete the VueX module that was created for this modal, after all other cleanup tasks are performed
  95. this.$store.unregisterModule(["modals", "bulkActions", this.modalUuid]);
  96. },
  97. mounted() {
  98. ws.onConnect(this.init);
  99. },
  100. methods: {
  101. init() {
  102. if (this.type.autosuggest && this.type.autosuggestDataAction)
  103. this.socket.dispatch(this.type.autosuggestDataAction, res => {
  104. if (res.status === "success") {
  105. const { items } = res.data;
  106. this.allItems = items;
  107. } else {
  108. new Toast(res.message);
  109. }
  110. });
  111. },
  112. addItem() {
  113. if (!this.itemInput) return;
  114. if (this.type.regex && !this.type.regex.test(this.itemInput)) {
  115. new Toast(`Invalid ${this.type.name} format.`);
  116. } else if (this.items.includes(this.itemInput)) {
  117. new Toast(`Duplicate ${this.type.name} specified.`);
  118. } else {
  119. this.items.push(this.itemInput);
  120. this.itemInput = null;
  121. }
  122. },
  123. removeItem(index) {
  124. this.items.splice(index, 1);
  125. },
  126. applyChanges() {
  127. let toast;
  128. let id;
  129. let title;
  130. this.socket.dispatch(
  131. this.type.action,
  132. this.method,
  133. this.items,
  134. this.type.items,
  135. {
  136. cb: () => {
  137. // new Toast(res.message);
  138. // if (res.status === "success")
  139. // this.closeCurrentModal();
  140. },
  141. onProgress: res => {
  142. if (!toast) {
  143. toast = new Toast({
  144. content: res.message,
  145. persistent: true,
  146. interactable: false
  147. });
  148. } else {
  149. toast.content = res.message;
  150. }
  151. if (
  152. res.status === "success" ||
  153. res.status === "error"
  154. ) {
  155. setTimeout(() => {
  156. toast.destroy();
  157. }, 4000);
  158. }
  159. if (res.status === "started") {
  160. id = res.id;
  161. title = res.title;
  162. this.closeCurrentModal();
  163. }
  164. if (id)
  165. this.setJob({
  166. id,
  167. name: title,
  168. ...res
  169. });
  170. }
  171. }
  172. );
  173. },
  174. ...mapActions("modalVisibility", ["closeCurrentModal"]),
  175. ...mapActions("longJobs", ["setJob"])
  176. }
  177. };
  178. </script>
  179. <style lang="less" scoped>
  180. .label {
  181. text-transform: capitalize;
  182. }
  183. .control.input-with-button > div {
  184. flex: 1;
  185. }
  186. .pill {
  187. display: inline-flex;
  188. .remove-item {
  189. font-size: 16px;
  190. margin: auto 2px auto 5px;
  191. cursor: pointer;
  192. }
  193. }
  194. :deep(.autosuggest-container) {
  195. width: calc(100% - 40px);
  196. top: unset;
  197. }
  198. </style>