6
0

project-creation-window.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <template>
  2. <div class="project-creation-window">
  3. <div class="content">
  4. <h1 class="headline">Create Project</h1>
  5. <text-input placeholder="Name"
  6. v-model="name"
  7. :error="nameError"
  8. @clearError="nameError = false">
  9. Name
  10. </text-input>
  11. <textarea-input placeholder="Description"
  12. v-model="description"
  13. :error="descriptionError"
  14. @clearError="descriptionError = false">
  15. Description
  16. </textarea-input>
  17. <select-input :values="availableModels"
  18. v-model="model">
  19. Model
  20. </select-input>
  21. <model-abilities :model="currentModel"/>
  22. <select-input :values="availableLabels"
  23. v-model="label">
  24. Label Provider
  25. </select-input>
  26. <text-input placeholder="path to folder (leave empty to disable)"
  27. v-model="external"
  28. @change="checkDirectory">
  29. External Storage
  30. </text-input>
  31. <div class="external">
  32. <template v-if="externalPathInformation !== null">
  33. <template v-if="externalPathInformation.exists">
  34. The given directory contains {{ externalPathInformation.count }} files.<br>
  35. </template>
  36. <template v-else>
  37. The given directory is not valid.<br>
  38. </template>
  39. </template>
  40. The external storage mode disables file uploads and loads them from the given directory instead.
  41. </div>
  42. </div>
  43. <button-row class="footer">
  44. <button-input @click="createProject" type="primary"
  45. :disabled="externalPathInformation !== null && !externalPathInformation.exists">
  46. Create Project
  47. </button-input>
  48. <button-input @click="$emit('cancel', null)">
  49. Cancel
  50. </button-input>
  51. </button-row>
  52. </div>
  53. </template>
  54. <script>
  55. import TextInput from "@/components/base/text-input";
  56. import TextareaInput from "@/components/base/textarea-input";
  57. import SelectInput from "@/components/base/select-input";
  58. import ButtonInput from "@/components/base/button-input";
  59. import ButtonRow from "@/components/base/button-row";
  60. import ModelAbilities from "@/components/models/model-abilities";
  61. export default {
  62. name: "project-creation-window",
  63. components: {ModelAbilities, ButtonRow, ButtonInput, SelectInput, TextareaInput, TextInput},
  64. props: ['status', 'socket'],
  65. created: function () {
  66. // get data
  67. this.getModels();
  68. this.getLabels();
  69. // subscribe to changes
  70. this.$root.socket.on('connect', this.getModels);
  71. this.$root.socket.on('create-model', this.addModel);
  72. this.$root.socket.on('remove-model', this.removeModel);
  73. this.$root.socket.on('connect', this.getLabels);
  74. },
  75. destroyed: function () {
  76. this.$root.socket.off('connect', this.getModels);
  77. this.$root.socket.off('create-model', this.addModel);
  78. this.$root.socket.off('remove-model', this.removeModel);
  79. this.$root.socket.off('connect', this.getLabels);
  80. },
  81. data: function () {
  82. return {
  83. name: '',
  84. nameError: false,
  85. description: '',
  86. descriptionError: false,
  87. models: [],
  88. model: null,
  89. labels: [],
  90. label: null,
  91. external: '',
  92. externalPathInformation: null
  93. }
  94. },
  95. methods: {
  96. getModels: function () {
  97. this.models = [];
  98. this.$root.socket.get('/models')
  99. .then(response => response.json())
  100. .then(models => models.forEach(this.addModel));
  101. },
  102. addModel: function (model) {
  103. for (let m of this.models)
  104. if (m.identifier === model.identifier)
  105. return;
  106. this.models.push(model);
  107. },
  108. removeModel: function (model) {
  109. for (let i = 0; i < this.models.length; i++) {
  110. if (this.models[i].identifier === model.identifier) {
  111. this.models.splice(i, 1);
  112. break;
  113. }
  114. }
  115. if (model.identifier === parseInt(this.model) && this.models.length > 0) {
  116. this.model = this.models[0].identifier;
  117. }
  118. },
  119. getLabels: function () {
  120. this.$root.socket.get('/label_providers')
  121. .then(response => response.json())
  122. .then(labels => this.labels = labels);
  123. },
  124. checkDirectory: function (value) {
  125. if (!value) {
  126. this.externalPathInformation = null;
  127. return;
  128. }
  129. this.$root.socket.post('/folder', {
  130. 'folder': this.external
  131. })
  132. .then(response => response.json())
  133. .then(data => this.externalPathInformation = data);
  134. },
  135. createProject: function () {
  136. // check input
  137. this.nameError = !this.name;
  138. this.descriptionError = !this.description;
  139. if (this.nameError || this.descriptionError)
  140. return;
  141. // create project
  142. // TODO then / error
  143. this.$root.socket.post('/projects', {
  144. name: this.name,
  145. description: this.description,
  146. model: parseInt(this.model),
  147. label: parseInt(this.label),
  148. external: this.external === '' ? null : this.external
  149. });
  150. // close project creation window
  151. this.$emit('cancel', null);
  152. }
  153. },
  154. computed: {
  155. availableModels: function () {
  156. return this.models.map(m => {
  157. return {
  158. name: m.name,
  159. value: m.identifier,
  160. }
  161. }).sort((m1, m2) => m1.name < m2.name ? -1 : +1);
  162. },
  163. currentModel: function () {
  164. for (let model of this.models)
  165. if (model.identifier === parseInt(this.model))
  166. return model;
  167. return false;
  168. },
  169. availableLabels: function () {
  170. let result = [];
  171. // add label providers
  172. for (let label of this.labels) {
  173. result.push({
  174. name: label.name,
  175. value: label.identifier,
  176. });
  177. }
  178. // sort
  179. result.sort((a, b) => {
  180. if (a.name.includes(b.name))
  181. return +1;
  182. if (b.name.includes(a.name))
  183. return -1;
  184. if (a.name > b.name)
  185. return +1;
  186. else
  187. return -1;
  188. });
  189. // add `None` option
  190. result.unshift({
  191. name: 'None',
  192. value: null
  193. });
  194. return result;
  195. }
  196. },
  197. watch: {
  198. availableModels: function (newValue) {
  199. if (this.model === null && newValue.length > 0) {
  200. this.model = newValue[0].value;
  201. }
  202. },
  203. availableLabels: function (newValue) {
  204. if (this.label === null && newValue.length > 0) {
  205. this.label = newValue[0].value;
  206. }
  207. },
  208. }
  209. }
  210. </script>
  211. <style scoped>
  212. .project-creation-window {
  213. display: flex;
  214. flex-direction: column;
  215. width: 100%;
  216. height: 100%;
  217. }
  218. .content {
  219. overflow: auto;
  220. padding: 1rem;
  221. height: 100%;
  222. flex-grow: 1;
  223. }
  224. .content > :not(h1) {
  225. margin-bottom: 0.6rem;
  226. }
  227. /deep/ .content textarea {
  228. height: 4rem;
  229. }
  230. .model-abilities {
  231. font-size: 80%;
  232. }
  233. .external {
  234. font-size: 80%;
  235. }
  236. .footer {
  237. flex-grow: 0;
  238. padding: 1rem;
  239. display: flex;
  240. flex-direction: row;
  241. justify-content: flex-end;
  242. }
  243. </style>