@cybertale/form 2.0.3 → 2.0.5

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/copy-package.mjs CHANGED
@@ -9,8 +9,8 @@ const prompt = promptSync();
9
9
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
10
10
 
11
11
  // Correct the source and destination directory paths
12
- const sourceDir = path.resolve(__dirname, 'src');
13
- const destDir = path.resolve(process.cwd(), 'src', '@cybertale', 'form');
12
+ const sourceDir = path.join(__dirname, 'src');
13
+ const destDir = path.join(process.cwd(), 'src', '@cybertale', 'form');
14
14
 
15
15
  // Create the destination directory if it doesn't exist
16
16
  const ensureDir = async (dir) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cybertale/form",
3
- "version": "2.0.3",
3
+ "version": "2.0.5",
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",
@@ -40,7 +40,6 @@
40
40
  "vite": "^5.3.4"
41
41
  },
42
42
  "scripts": {
43
- "copy-package": "node copy-package.mjs",
44
- "postinstall": "npm run copy-package"
43
+ "copy-package": "node copy-package.mjs"
45
44
  }
46
45
  }
@@ -1,73 +1,125 @@
1
1
  <template v-if="renderComponent">
2
- <button data-bs-toggle="tooltip" data-bs-placement="top"
3
- :data-bs-title="tooltipCase()"
4
- :hidden="specialCase()"
5
- :disabled="attributeCheck(statTypeEnum.Disabled)"
6
- :class="getValue(statTypeEnum.Design)"
7
- @click.prevent='regionType.RegionTypes[object.Region].ObjectTypes[object.ObjectEnum].ChooseSubType(object)'>
8
- {{ getValue(statTypeEnum.Label) }}
2
+ <button
3
+ data-bs-toggle="tooltip"
4
+ data-bs-placement="top"
5
+ :data-bs-title="tooltipText"
6
+ :hidden="isHidden"
7
+ :disabled="isDisabled"
8
+ :class="buttonClass"
9
+ @click.prevent="handleClick"
10
+ >
11
+ <span class="order-2">{{ buttonLabel }}</span>
12
+ <i v-if="showIcon" :class="iconClass"></i>
9
13
  </button>
10
14
  <slot></slot>
11
15
  </template>
12
16
 
17
+
13
18
  <script lang="ts">
14
19
  import { Component, Prop, Vue } from 'vue-facing-decorator'
15
- import { ObjectTemplate, ObjectType, StatTypeEnum, ObjectTypeEnum, RegionType, RegionEnum } from '@cybertale/interface'
20
+ import { ObjectTemplate, ObjectTypeEnum, RegionEnum, RegionType, StatTypeEnum } from '@cybertale/interface'
21
+
22
+ interface LabelData {
23
+ iconClass: string;
24
+ title: string;
25
+ }
16
26
  @Component
17
27
  export default class ButtonComponent extends Vue {
18
28
  @Prop() object!: ObjectTemplate
19
29
 
20
- statTypeEnum = StatTypeEnum
21
- objectTypeEnum = ObjectTypeEnum
22
- objectType = ObjectType
23
- regionType = RegionType
24
- regionEnum = RegionEnum
25
- renderComponent= false
26
-
27
- getValue (statEnum: number) : string {
28
- if (this.object) {
29
- if (this.object.Stats[this.statTypeEnum.Option] && this.isJSON(this.object.Stats[statEnum].Data)) {
30
- const data = JSON.parse(this.object.Stats[statEnum].Data)
31
- return data[Number(this.object.Stats[this.statTypeEnum.Option].Data)]
32
- } else {
33
- return this.object.Stats[statEnum].Data
34
- }
30
+ // Enums
31
+ StatTypeEnum = StatTypeEnum
32
+ ObjectTypeEnum = ObjectTypeEnum
33
+ RegionEnum = RegionEnum
34
+ btnClass = ''
35
+
36
+ renderComponent = false
37
+
38
+ // Computed properties
39
+ get tooltipText(): string | undefined {
40
+ return this.getStatData(StatTypeEnum.Tooltip)
41
+ }
42
+
43
+ get isHidden(): boolean {
44
+ return this.getStatData(StatTypeEnum.ElementType) === 'hidden'
45
+ }
46
+
47
+ get isDisabled(): boolean {
48
+ return !!this.getStatData(StatTypeEnum.Disabled, 'boolean')
49
+ }
50
+
51
+ get buttonClass(): string {
52
+ const temp = this.getValue(StatTypeEnum.Design) as string
53
+ return temp
54
+ }
55
+
56
+ get buttonLabel(): string {
57
+ if(this.showIcon){
58
+ const value = this.getValue(StatTypeEnum.Label) as LabelData
59
+ return value.title
35
60
  }
36
- return ''
61
+ return this.getValue(StatTypeEnum.Label) as string
37
62
  }
38
63
 
39
- isJSON (str: string): boolean {
40
- let temp = null
41
- try {
42
- temp = JSON.parse(str)
43
- } catch (e) {
44
- return false
64
+ get showIcon(): boolean {
65
+ return this.getStatData(StatTypeEnum.ElementType) === 'icon'
66
+ }
67
+
68
+ get iconClass(): string {
69
+ if(this.showIcon){
70
+ const value = this.getValue(StatTypeEnum.Label) as LabelData
71
+ return value.iconClass
45
72
  }
46
- return Array.isArray(temp)
73
+ return this.getValue(StatTypeEnum.Label)
74
+ }
75
+
76
+ // Methods
77
+ handleClick(): void {
78
+ const { Region, ObjectEnum } = this.object
79
+ RegionType.RegionTypes[Region].ObjectTypes[ObjectEnum].ChooseSubType(this.object)
47
80
  }
48
81
 
49
- tooltipCase () {
50
- if (this.object) {
51
- if (this.object.Stats[this.statTypeEnum.Tooltip]) {
52
- return this.object.Stats[this.statTypeEnum.Tooltip].Data
82
+ getValue( statEnum: StatTypeEnum, indexStatTypeEnum = StatTypeEnum.Option): string | LabelData {
83
+ const tempData:string = this.getStatData(statEnum) as string
84
+ if (!tempData) return '';
85
+
86
+ if (this.statIsDefined(indexStatTypeEnum) && this.isJSON(tempData)) {
87
+ const data = JSON.parse(tempData);
88
+ const optionData = this.getStatData(indexStatTypeEnum) as string
89
+ if(this.isJSON(optionData)){
90
+ const parsedOptionData = JSON.parse(optionData)[Number(this.object.Stats[StatTypeEnum.OptionIndices].Data)]
91
+ return data[Number(parsedOptionData)] || '';
53
92
  }
93
+ return data[Number(this.getStatData(indexStatTypeEnum))] || '';
54
94
  }
95
+
96
+ return tempData;
55
97
  }
56
98
 
57
- specialCase () : boolean {
58
- if (this.object.Stats[this.statTypeEnum.ElementType] === undefined) { return false }
59
- return this.object.Stats[this.statTypeEnum.ElementType].Data === 'hidden'
99
+ statIsDefined (statType: StatTypeEnum): boolean {
100
+ return !!this.object.Stats[statType]
60
101
  }
61
102
 
62
- attributeCheck (statType : number) : boolean | string {
63
- if (this.object.Stats[statType] === undefined) { return false }
64
- if (this.object.Stats[statType].Data === '') { return false }
65
- return this.object.Stats[statType].Data
103
+ getStatData(statType: StatTypeEnum, returnType: 'boolean' | 'string' = 'string'): boolean | string {
104
+ try {
105
+ const data = this.object.Stats[statType]?.Data ?? ''
106
+ return returnType === 'boolean' ? !!data : data
107
+ } catch (error) {
108
+ console.error(`Error accessing data for statType ${statType}:`, error)
109
+ return returnType === 'boolean' ? false : ''
110
+ }
111
+ }
112
+
113
+ isJSON(str: string): boolean {
114
+ try {
115
+ return Array.isArray(JSON.parse(str))
116
+ } catch {
117
+ return false
118
+ }
66
119
  }
67
120
  }
68
121
  </script>
69
122
 
70
- <!-- Add "scoped" attribute to limit CSS to this component only -->
71
123
  <style scoped>
72
-
124
+ /* Add your scoped styles here */
73
125
  </style>
@@ -1,15 +1,19 @@
1
1
  <template>
2
- <input class="form-control"
3
- :id="object?.Stats[statTypeEnum.Tag].Data"
4
- :required="attributeCheck(statTypeEnum.Required)"
5
- :disabled="attributeCheck(statTypeEnum.Disabled)"
6
- :autocomplete="returnIfExists(statTypeEnum.AutoComplete)"
7
- :class="object?.Stats[statTypeEnum.Design].Data+' '+validate()"
8
- :type="getValue(statTypeEnum.ElementType)"
9
- :value="labelToValue()"
10
- :placeholder="returnIfExists(statTypeEnum.Placeholder)"
11
- @input="regionType.RegionTypes[object?.Region].ObjectTypes[object?.ObjectEnum].ChooseSubType(object as ObjectTemplate, $event.target.value)">
12
- <div class="invalid-feedback">{{ returnIfExists(statTypeEnum.ErrorMessage) }}</div>
2
+ <input
3
+ class="form-control"
4
+ :id="getStatData(StatTypeEnum.Tag)"
5
+ :required="getStatData(StatTypeEnum.Required, 'boolean')"
6
+ :disabled="getStatData(StatTypeEnum.Disabled, 'boolean')"
7
+ :autocomplete="getStatData(StatTypeEnum.AutoComplete)"
8
+ :class="[getStatData(StatTypeEnum.Design), validationClass]"
9
+ :type="getInputType()"
10
+ :value="computedValue"
11
+ :placeholder="getStatData(StatTypeEnum.Placeholder)"
12
+ @input="handleInput"
13
+ />
14
+ <div v-if="showErrorMessage" class="invalid-feedback">
15
+ {{ getStatData(StatTypeEnum.ErrorMessage) }}
16
+ </div>
13
17
  </template>
14
18
 
15
19
  <script lang="ts">
@@ -20,74 +24,85 @@ import { ObjectTemplate, ObjectType, ObjectTypeEnum, RegionEnum, RegionType, Sta
20
24
  export default class FieldComponent extends Vue {
21
25
  @Prop() object!: ObjectTemplate
22
26
 
23
- statTypeEnum = StatTypeEnum
24
- objectTypeEnum = ObjectTypeEnum
25
- objectType = ObjectType
26
- regionType = RegionType
27
- regionEnum = RegionEnum
27
+ // Enums
28
+ StatTypeEnum = StatTypeEnum
29
+ ObjectTypeEnum = ObjectTypeEnum
30
+ RegionEnum = RegionEnum
28
31
 
29
- labelToValue(): string {
30
- if (this.returnIfExists(this.statTypeEnum.Tag).includes('label') && this.attributeCheck(this.statTypeEnum.Disabled)) {
31
- return this.returnIfExists(this.statTypeEnum.Label)
32
+ // Computed properties
33
+ get computedValue(): string {
34
+ if (this.isLabelDisabled) {
35
+ return this.getStatData(StatTypeEnum.Label)
32
36
  }
33
37
  return this.getValue(StatTypeEnum.Value, StatTypeEnum.ValueIndices)
34
38
  }
35
39
 
36
- getValue(statEnum: number, indexStatTypeEnum = StatTypeEnum.Option): string {
37
- if (this.object.Stats[statEnum]) {
38
- if (this.object.Stats[indexStatTypeEnum] && this.object.Stats[statEnum] && this.isJSON(this.object.Stats[statEnum].Data)) {
39
- const data = JSON.parse(this.object.Stats[statEnum].Data)
40
- return data[Number(this.object.Stats[indexStatTypeEnum].Data)]
41
- } else {
42
- return this.object.Stats[statEnum].Data
43
- }
44
- }
40
+ get isLabelDisabled(): boolean {
41
+ return this.getStatData(StatTypeEnum.Tag).includes('label') &&
42
+ this.getStatData(StatTypeEnum.Disabled, 'boolean')
43
+ }
44
+
45
+ get validationClass(): string {
46
+ const isValid = this.getStatData(StatTypeEnum.IsValid, 'boolean')
47
+ const errorMessage = this.getStatData(StatTypeEnum.ErrorMessage)
48
+
49
+ if (isValid === undefined || isValid === '') return ''
50
+ if (isValid) return 'is-valid'
51
+ if (errorMessage) return 'is-invalid'
45
52
  return ''
46
53
  }
47
54
 
48
- isJSON(str: string): boolean {
49
- try {
50
- const temp = JSON.parse(str)
51
- return Array.isArray(temp)
52
- } catch (e) {
53
- return false
55
+ get showErrorMessage(): boolean {
56
+ return this.validationClass === 'is-invalid'
57
+ }
58
+
59
+ // Methods
60
+ getInputType(): string {
61
+ return this.getValue(StatTypeEnum.ElementType)
62
+ }
63
+
64
+ handleInput(event: Event): void {
65
+ const target = event.target as HTMLInputElement
66
+ const { Region, ObjectEnum } = this.object
67
+ console.log(target.value)
68
+ RegionType.RegionTypes[Region].ObjectTypes[ObjectEnum].ChooseSubType(this.object, target.value)
69
+ }
70
+
71
+ getValue( statEnum: StatTypeEnum, indexStatTypeEnum = StatTypeEnum.Option): string {
72
+ const tempData:string = this.getStatData(statEnum)
73
+ if (!tempData) return '';
74
+
75
+ if (this.statIsDefined(indexStatTypeEnum) && this.isJSON(tempData)) {
76
+ const data = JSON.parse(tempData);
77
+ return data[Number(this.getStatData(indexStatTypeEnum))] || '';
54
78
  }
79
+
80
+ return tempData;
55
81
  }
56
82
 
57
- returnIfExists(tag: number): string {
58
- return this.object.Stats[tag]?.Data ?? ''
83
+ statIsDefined (statType: StatTypeEnum): boolean {
84
+ return !!this.object.Stats[statType]
59
85
  }
60
86
 
61
- validate(): string {
62
- if (this.object.Stats[this.statTypeEnum.IsValid] === undefined) { return '' }
63
- if (this.object.Stats[this.statTypeEnum.IsValid].Data === '') { return '' }
64
- if (this.object.Stats[this.statTypeEnum.IsValid].Data) { return 'is-valid' }
65
- if (this.object.Stats[this.statTypeEnum.ErrorMessage].Data === null) { return '' }
66
- if (this.object.Stats[this.statTypeEnum.ErrorMessage].Data !== '') { return 'is-invalid' }
67
- return ''
87
+ getStatData(statType: StatTypeEnum, returnType: 'boolean' | 'string' = 'string'): boolean | string {
88
+ try {
89
+ const data = this.object.Stats[statType]?.Data ?? ''
90
+ return returnType === 'boolean' ? !!data : data
91
+ } catch (error) {
92
+ return returnType === 'boolean' ? false : ''
93
+ }
68
94
  }
69
95
 
70
- attributeCheck(statType: number): boolean | string {
71
- if (this.object.Stats[statType] === undefined) { return false }
72
- if (this.object.Stats[statType].Data === '') { return false }
73
- return this.object.Stats[statType].Data
96
+ isJSON(str: string): boolean {
97
+ try {
98
+ return Array.isArray(JSON.parse(str))
99
+ } catch {
100
+ return false
101
+ }
74
102
  }
75
103
 
76
104
  tooltipCase(): string | undefined {
77
- return this.object?.Stats[this.statTypeEnum.Tooltip]?.Data
105
+ return this.object?.Stats[StatTypeEnum.Tooltip]?.Data
78
106
  }
79
107
  }
80
108
  </script>
81
-
82
- <style scoped>
83
- .form-check .form-check-input {
84
- float: none;
85
- }
86
- .form-check-input {
87
- margin-right: 1%;
88
- }
89
- .form-check-input:checked {
90
- background-color: #606467;
91
- border-color: #606467;
92
- }
93
- </style>