@@ -56,6 +56,7 @@ export default {
56
56
widget: OhInputDefinition,
57
57
data () {
58
58
return {
59
+ item: null ,
59
60
pendingUpdate: null
60
61
}
61
62
},
@@ -78,17 +79,32 @@ export default {
78
79
} else if (this .config .item ) {
79
80
const item = this .context .store [this .config .item ]
80
81
if (item .state !== ' NULL' && item .state !== ' UNDEF' && item .state !== ' Invalid Date' ) {
81
- let value = this .config .useDisplayState ? item .displayState || item .state : item .state
82
- if (this .unit ) {
83
- value = value .split (' ' )[0 ]
84
- }
85
- return value
82
+ const value = this .config .useDisplayState && item .displayState || item .state
83
+ return this .config .type === ' number' ? this .extractValue (value) : value
86
84
}
87
85
}
88
86
return this .config .defaultValue
89
87
},
88
+ // Returns the unit from the item's displayState, state description pattern or the item's unit symbol
90
89
unit () {
91
- return this .config .type === ' number' && this .config .item && this .context .store [this .config .item ].unit
90
+ if (this .config .type !== ' number' ) return null
91
+ if (! this .item ? .unitSymbol ) return null
92
+
93
+ const storeItem = this .context .store [this .config .item ]
94
+ // When the state of a dimensioned item is UNDEF/NULL, item.displayState is undefined
95
+ // so we need to pull it out of the item's state description pattern
96
+ if (this .config .useDisplayState ) {
97
+ const unit = this .extractUnit (storeItem .displayState || this .item .stateDescription ? .pattern )
98
+ return unit === ' %unit%' ? this .item .unitSymbol : unit
99
+ }
100
+ return this .item .unitSymbol
101
+ },
102
+ // Returns the index of the last pattern in the stateDescription
103
+ // Example: displayState = "Some label %0.1f footext °C", returns 2
104
+ // Returns -1 if no pattern is found
105
+ valueIndexInDisplayState () {
106
+ const parts = this .item ? .stateDescription ? .pattern ? .trim ()? .split (/ \s + / ) || []
107
+ return parts .findLastIndex (part => part .startsWith (' %' ) && part !== ' %unit%' && part !== ' %%' )
92
108
},
93
109
calendarParams () {
94
110
if (this .config .type !== ' datepicker' ) return null
@@ -121,6 +137,18 @@ export default {
121
137
}
122
138
}
123
139
},
140
+ mounted () {
141
+ if (this .config .item ) {
142
+ this .$oh .api .get (` /rest/items/${ this .config .item } ?recursive=false` ).then ((item ) => {
143
+ this .item = item
144
+ if (this .config .useDisplayState ) {
145
+ this .config .min || = item .stateDescription ? .minimum
146
+ this .config .max || = item .stateDescription ? .maximum
147
+ this .config .step || = item .stateDescription ? .step
148
+ }
149
+ })
150
+ }
151
+ },
124
152
methods: {
125
153
updated (value ) {
126
154
if (this .config .type === ' texteditor' ) {
@@ -174,6 +202,21 @@ export default {
174
202
this .$store .dispatch (' sendCommand' , { itemName: this .config .item , cmd })
175
203
this .$set (this , ' pendingUpdate' , null )
176
204
}
205
+ },
206
+ extractUnit (pattern ) {
207
+ if (! pattern) return null
208
+ return pattern .trim ().split (/ \s + / ).pop ()
209
+ },
210
+ extractValue (pattern ) {
211
+ if (! pattern) return null
212
+
213
+ const parts = pattern .trim ().split (/ \s + / )
214
+ switch (parts .length ) {
215
+ case 0 : return null
216
+ case 1 : return pattern
217
+ case 2 : return parts[0 ]
218
+ default : return parts[this .valueIndexInDisplayState ]
219
+ }
177
220
}
178
221
}
179
222
}
0 commit comments