BulkActions.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <template>
  2. <div>
  3. <modal title="Bulk Actions" class="bulk-actions-modal">
  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 Modal from "../Modal.vue";
  68. import AutoSuggest from "@/components/AutoSuggest.vue";
  69. import ws from "@/ws";
  70. export default {
  71. components: { Modal, AutoSuggest },
  72. props: {
  73. type: {
  74. type: Object,
  75. default: () => {}
  76. }
  77. },
  78. data() {
  79. return {
  80. method: "add",
  81. items: [],
  82. itemInput: null,
  83. allItems: []
  84. };
  85. },
  86. computed: {
  87. ...mapGetters({
  88. socket: "websockets/getSocket"
  89. })
  90. },
  91. beforeUnmount() {
  92. this.itemInput = null;
  93. this.items = [];
  94. },
  95. mounted() {
  96. ws.onConnect(this.init);
  97. },
  98. methods: {
  99. init() {
  100. if (this.type.autosuggest && this.type.autosuggestDataAction)
  101. this.socket.dispatch(this.type.autosuggestDataAction, res => {
  102. if (res.status === "success") {
  103. const { items } = res.data;
  104. this.allItems = items;
  105. } else {
  106. new Toast(res.message);
  107. }
  108. });
  109. },
  110. addItem() {
  111. if (!this.itemInput) return;
  112. if (this.type.regex && !this.type.regex.test(this.itemInput)) {
  113. new Toast(`Invalid ${this.type.name} format.`);
  114. } else if (this.items.includes(this.itemInput)) {
  115. new Toast(`Duplicate ${this.type.name} specified.`);
  116. } else {
  117. this.items.push(this.itemInput);
  118. this.itemInput = null;
  119. }
  120. },
  121. removeItem(index) {
  122. this.items.splice(index, 1);
  123. },
  124. applyChanges() {
  125. this.socket.dispatch(
  126. this.type.action,
  127. this.method,
  128. this.items,
  129. this.type.items,
  130. res => {
  131. new Toast(res.message);
  132. this.closeModal("bulkActions");
  133. }
  134. );
  135. },
  136. ...mapActions("modalVisibility", ["closeModal"])
  137. }
  138. };
  139. </script>
  140. <style lang="scss" scoped>
  141. .label {
  142. text-transform: capitalize;
  143. }
  144. .select.is-expanded select {
  145. width: 100%;
  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>