@cybertale/form 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cybertale/form",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "ECS interface for Web Development, CyberTale edition.",
5
5
  "author": "Joso Marich <jspmari@proton.me>",
6
6
  "license": "GPL-3.0-only",
@@ -4,24 +4,26 @@
4
4
  :required="attributeCheck(statTypeEnum.Required)"
5
5
  :disabled="attributeCheck(statTypeEnum.Disabled)"
6
6
  :type="returnIfExists(statTypeEnum.ElementType)"
7
- :value="`${object?.Stats[statTypeEnum.Value].Data !== null?object.Stats[statTypeEnum.Value].Data.name === undefined?valueName:object.Stats[statTypeEnum.Value].Data.name:''}`"
7
+ :value="`${object?.Stats[statTypeEnum.Value].Data !== null ? object.Stats[statTypeEnum.Value].Data.name === undefined ? valueName : object.Stats[statTypeEnum.Value].Data.name : ''}`"
8
8
  :placeholder="returnIfExists(statTypeEnum.Placeholder)"
9
9
  @input="inputEvent(object as ObjectTemplate, $event.target.value)">
10
- <datalist :id="object.Stats[statTypeEnum.BelongsTo].Data" v-if="dspStyle">
11
- <option v-for="(item, key, index) in JSON.parse(object.Stats[statTypeEnum.ItemList].Data)" :selected="check(item.name)" :key="`${ key }-${ index }`" :value="item.name">{{item.name}}</option>
10
+ <datalist :id="object.Stats[statTypeEnum.BelongsTo].Data" v-if="displayOptions">
11
+ <option v-for="(option, index) in options" :key="index" :value="option.name">{{ option.name }}</option>
12
12
  </datalist>
13
13
  <div class="invalid-feedback order-1">{{ returnIfExists(statTypeEnum.ErrorMessage) }}</div>
14
14
  </template>
15
-
16
15
  <script lang="ts">
17
16
  import { Options, Vue } from 'vue-class-component'
18
17
  import { ObjectTemplate, ObjectType, ObjectTypeEnum, RegionEnum, RegionType, StatTypeEnum } from '@cybertale/interface'
18
+ import http from '@/http-common'
19
19
 
20
20
  interface Option {
21
21
  id: number;
22
22
  name: string;
23
23
  }
24
24
 
25
+ const MIN_SEARCH_LENGTH = 3;
26
+
25
27
  @Options({
26
28
  computed: {
27
29
  ObjectTemplate () {
@@ -39,19 +41,36 @@ export default class DataListComponent extends Vue {
39
41
  regionType = RegionType
40
42
  regionEnum = RegionEnum
41
43
  object!: ObjectTemplate
42
- dspStyle = false
43
- options: Option[] = JSON.parse(this.object.Stats[this.statTypeEnum.ItemList].Data)
44
+ displayOptions = false
45
+ options: Option[] = []
44
46
 
45
47
  returnIfExists (tag: number): string {
46
- if (this.object.Stats[tag]) {
47
- return this.object.Stats[tag].Data
48
+ return this.object.Stats[tag]?.Data ?? ''
49
+ }
50
+
51
+ async created() {
52
+ await this.fetchOptions();
53
+ }
54
+
55
+ // Extract the logic for fetching options data into a separate method
56
+ async fetchOptions() {
57
+ try {
58
+ const parsedObject: { link?: string } = JSON.parse(this.object.Stats[this.statTypeEnum.ItemList].Data)[0];
59
+ if (parsedObject.link) {
60
+ const response = await http.get(parsedObject.link + '/' + this.object.Stats[this.statTypeEnum.Name].Data);
61
+ this.options = response.data;
62
+ this.displayOptions = true;
63
+ } else {
64
+ this.options = JSON.parse(this.object.Stats[this.statTypeEnum.ItemList].Data);
65
+ }
66
+ } catch (error: any) {
67
+ console.error('Error fetching data:', error.message);
48
68
  }
49
- return ''
50
69
  }
51
70
 
52
71
  getValue (statEnum: number, indexStatTypeEnum = StatTypeEnum.Option) : string {
53
72
  if (this.object.Stats[statEnum]) {
54
- if (this.object.Stats[indexStatTypeEnum] && this.object.Stats[statEnum] && this.isJSON(this.object.Stats[statEnum].Data)) {
73
+ if (this.object.Stats[indexStatTypeEnum] && this.object.Stats[statEnum] && isValidJSON(this.object.Stats[statEnum].Data)) {
55
74
  const data = JSON.parse(this.object.Stats[statEnum].Data)
56
75
  return data[Number(this.object.Stats[indexStatTypeEnum].Data)]
57
76
  } else {
@@ -61,26 +80,13 @@ export default class DataListComponent extends Vue {
61
80
  return ''
62
81
  }
63
82
 
64
- isJSON (str: string): boolean {
65
- let temp = null
66
- try {
67
- temp = JSON.parse(str)
68
- } catch (e) {
69
- return false
70
- }
71
- return Array.isArray(temp)
72
- }
73
-
74
83
  get valueName (): string {
75
84
  const temp = this.options.find((option: any) => option.id === this.getValue(StatTypeEnum.Value, StatTypeEnum.ValueIndices))
76
- if (!temp) { return this.getValue(StatTypeEnum.Value, StatTypeEnum.ValueIndices) }
77
- return temp?.name
85
+ return temp?.name ?? this.getValue(StatTypeEnum.Value, StatTypeEnum.ValueIndices)
78
86
  }
79
87
 
80
88
  attributeCheck (statType : number) : boolean | string {
81
- if (this.object.Stats[statType] === undefined) { return false }
82
- if (this.object.Stats[statType].Data === '') { return false }
83
- return this.object.Stats[statType].Data
89
+ return this.object.Stats[statType]?.Data ?? false
84
90
  }
85
91
 
86
92
  validate () : string {
@@ -93,17 +99,26 @@ export default class DataListComponent extends Vue {
93
99
  }
94
100
 
95
101
  inputEvent (object: ObjectTemplate, value: string) : void {
96
- this.dspStyle = value.length >= 3
102
+ this.displayOptions = value.length >= MIN_SEARCH_LENGTH
97
103
  this.regionType.RegionTypes[object.Region].ObjectTypes[object.ObjectEnum].ChooseSubType(object, value)
98
104
  }
99
105
 
100
- check (id : string) : boolean {
101
- if (this.object.Stats[this.statTypeEnum.Value] === undefined || id === undefined) { return false }
102
- return this.object.Stats[this.statTypeEnum.Value].Data === id.toString()
106
+ isSelected (id : string) : boolean { //previously check
107
+ return this.object.Stats[this.statTypeEnum.Value]?.Data === id.toString()
103
108
  }
109
+
104
110
  }
105
- </script>
106
111
 
112
+ // Utility function to check if a string is valid JSON
113
+ function isValidJSON(str: string): boolean {
114
+ try {
115
+ JSON.parse(str);
116
+ return true;
117
+ } catch (e) {
118
+ return false;
119
+ }
120
+ }
121
+ </script>
107
122
  <!-- Add "scoped" attribute to limit CSS to this component only -->
108
123
  <style scoped>
109
124
  </style>