AccountForm.vue 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <template>
  2. <form>
  3. <p><b>Schema version</b>: {{account.version}}</p>
  4. <field
  5. v-for="field in fields"
  6. v-if="dependencyChecksOut(field.fieldId)"
  7. :name="field.name"
  8. :minEntries="field.minEntries"
  9. :maxEntries="field.maxEntries"
  10. :initialEntries="account.fields[field.fieldId]"
  11. :autosuggest="autosuggest"
  12. :key="field.fieldId"
  13. :ref="field.fieldId"
  14. :onChange="onFieldChange(field.fieldId)"
  15. :readonly="readonly"
  16. :fieldTypes="field.fieldTypes"/>
  17. <button @click="submit()" type="button" class="button" v-if="!readonly">
  18. Submit
  19. </button>
  20. </form>
  21. </template>
  22. <script>
  23. import Field from '../components/Field.vue';
  24. import io from "../../io.js";
  25. export default {
  26. components: { Field },
  27. data: function() {
  28. return {
  29. fields: [],
  30. account: {},
  31. schema: {},
  32. autosuggest: {},
  33. dependencies: {}
  34. };
  35. },
  36. methods: {
  37. submit() {
  38. let account = JSON.parse(JSON.stringify(this.account));
  39. let fields = JSON.parse(JSON.stringify(this.templateAccount)).fields;
  40. Object.keys(account.fields).forEach(fieldId => {
  41. if (this.$refs[fieldId]) fields[fieldId] = this.$refs[fieldId][0].entries;
  42. });
  43. account.fields = fields;
  44. this.onSubmit(account);
  45. },
  46. dependencyChecksOut(fieldId) {
  47. if (!this.dependencies[fieldId]) return true;
  48. let dependency = this.dependencies[fieldId];
  49. let dependencyFieldId = dependency.fieldId;
  50. if (!this.dependencyChecksOut(dependencyFieldId)) return false;
  51. let dependencyEval = dependency.eval.replace("{fields}", "this.account.fields");
  52. try {
  53. return eval(dependencyEval);
  54. } catch(err) {
  55. console.log("Eval error", err);
  56. return false;
  57. }
  58. return false;
  59. },
  60. onFieldChange(fieldId) {
  61. return () => {
  62. this.$set(this.account.fields, fieldId, this.$refs[fieldId][0].entries);
  63. };
  64. }
  65. },
  66. props: {
  67. onSubmit: Function,
  68. initialAccount: Object,
  69. readonly: Boolean
  70. },
  71. mounted() {
  72. io.getSocket(socket => {
  73. this.socket = socket;
  74. socket.emit("accountSchema.getByVersion", this.initialAccount.version, res => {
  75. this.fields = res.schema.fields;
  76. this.dependencies = (res.schema.dependencies) ? res.schema.dependencies : {};
  77. this.account = this.initialAccount;
  78. this.templateAccount = this.initialAccount;
  79. });
  80. socket.emit("util.getAutosuggest", res => {
  81. this.autosuggest = res.autosuggest;
  82. });
  83. });
  84. }
  85. };
  86. </script>
  87. <style lang="scss" scoped>
  88. form {
  89. width: 400px;
  90. }
  91. </style>