26 const "eltid","@id"; |
26 const "eltid","@id"; |
27 const "args" foreach "$widget/arg" > "«@value»"`if "position()!=last()" > ,` |
27 const "args" foreach "$widget/arg" > "«@value»"`if "position()!=last()" > ,` |
28 const "indexes" foreach "$widget/path" { |
28 const "indexes" foreach "$widget/path" { |
29 choose { |
29 choose { |
30 when "not(@index)" { |
30 when "not(@index)" { |
31 warning > Widget «$widget/@type» id="«$eltid»" : No match for path "«@value»" in HMI tree |
31 choose { |
|
32 when "not(@type)" |
|
33 error > Widget «$widget/@type» id="«$eltid»" : No match for path "«@value»" in HMI tree |
|
34 when "@type = 'PAGE_LOCAL'" |
|
35 > "«substring(1,@value)»"`if "position()!=last()" > ,` |
|
36 when "@type = 'HMI_LOCAL'" |
|
37 > hmi_local_index("«@value»")`if "position()!=last()" > ,` |
|
38 } |
32 } |
39 } |
33 otherwise { |
40 otherwise { |
34 > «@index»`if "position()!=last()" > ,` |
41 > «@index»`if "position()!=last()" > ,` |
35 } |
42 } |
36 } |
43 } |
60 result "$elts_with_type"; |
67 result "$elts_with_type"; |
61 } |
68 } |
62 } |
69 } |
63 } |
70 } |
64 |
71 |
|
72 emit "preamble:local-variable-indexes" { |
|
73 || |
|
74 let hmi_locals = {}; |
|
75 var last_remote_index = hmitree_types.length - 1; |
|
76 var next_available_index = hmitree_types.length; |
|
77 |
|
78 function page_local_index(varname, pagename){ |
|
79 let pagevars = hmi_locals[pagename]; |
|
80 if(pagevars == undefined){ |
|
81 let new_index = next_available_index++; |
|
82 hmi_locals[pagename] = {varname:new_index} |
|
83 return new_index; |
|
84 } else { |
|
85 let result = pagevars[varname]; |
|
86 if(result==undefined){ |
|
87 let new_index = next_available_index++; |
|
88 pagevars[varname] = new_index; |
|
89 return new_index; |
|
90 } |
|
91 return result; |
|
92 } |
|
93 } |
|
94 |
|
95 function hmi_local_index(varname){ |
|
96 return page_local_index(varname, "HMI_LOCAL"); |
|
97 } |
|
98 || |
|
99 } |
|
100 |
65 emit "preamble:widget-base-class" { |
101 emit "preamble:widget-base-class" { |
66 || |
102 || |
67 class Widget { |
103 class Widget { |
68 offset = 0; |
104 offset = 0; |
69 frequency = 10; /* FIXME arbitrary default max freq. Obtain from config ? */ |
105 frequency = 10; /* FIXME arbitrary default max freq. Obtain from config ? */ |
70 unsubscribable = false; |
106 unsubscribable = false; |
71 constructor(elt_id,args,indexes,members){ |
107 constructor(elt_id,args,indexes,members){ |
87 } |
123 } |
88 this.offset = 0; |
124 this.offset = 0; |
89 this.relativeness = undefined; |
125 this.relativeness = undefined; |
90 } |
126 } |
91 |
127 |
92 sub(new_offset=0, relativeness){ |
128 sub(new_offset=0, relativeness, container_id){ |
93 this.offset = new_offset; |
129 this.offset = new_offset; |
94 this.relativeness = relativeness; |
130 this.relativeness = relativeness; |
|
131 this.container_id = container_id ; |
95 /* add this's subsribers */ |
132 /* add this's subsribers */ |
96 if(!this.unsubscribable) |
133 if(!this.unsubscribable) |
97 for(let i = 0; i < this.indexes.length; i++) { |
134 for(let i = 0; i < this.indexes.length; i++) { |
98 let index = this.indexes[i]; |
135 let index = this.get_variable_index(i); |
99 if(relativeness[i]) |
136 if(index > last_remote_index) return; |
100 index += new_offset; |
|
101 subscribers[index].add(this); |
137 subscribers[index].add(this); |
102 } |
138 } |
103 need_cache_apply.push(this); |
139 need_cache_apply.push(this); |
104 } |
140 } |
105 |
141 |
106 apply_cache() { |
142 apply_cache() { |
107 if(!this.unsubscribable) for(let index of this.indexes){ |
143 if(!this.unsubscribable) for(let i = 0; i < this.indexes.length; i++) { |
108 /* dispatch current cache in newly opened page widgets */ |
144 /* dispatch current cache in newly opened page widgets */ |
109 let realindex = index+this.offset; |
145 let realindex = this.get_variable_index(i); |
110 let cached_val = cache[realindex]; |
146 let cached_val = cache[realindex]; |
111 if(cached_val != undefined) |
147 if(cached_val != undefined) |
112 this.new_hmi_value(realindex, cached_val, cached_val); |
148 this.new_hmi_value(realindex, cached_val, cached_val); |
113 } |
149 } |
114 } |
150 } |
115 |
151 |
116 get_idx(index) { |
152 get_variable_index(varnum) { |
117 let orig = this.indexes[index]; |
153 let index = this.indexes[varnum]; |
118 return this.relativeness[index] ? orig + this.offset : orig; |
154 if(typeof(index) == "string"){ |
|
155 let page = this.relativeness[varnum]; |
|
156 index = page_local_index(index, this.container_id); |
|
157 } else { |
|
158 if(this.relativeness[varnum]){ |
|
159 index += this.offset; |
|
160 } |
|
161 } |
|
162 return index; |
119 } |
163 } |
120 change_hmi_value(index,opstr) { |
164 change_hmi_value(index,opstr) { |
121 return change_hmi_value(this.get_idx(index), opstr); |
165 return change_hmi_value(this.get_variable_index(index), opstr); |
122 } |
166 } |
123 |
167 |
124 apply_hmi_value(index, new_val) { |
168 apply_hmi_value(index, new_val) { |
125 return apply_hmi_value(this.get_idx(0), new_val); |
169 return apply_hmi_value(this.get_variable_index(0), new_val); |
126 } |
170 } |
127 |
171 |
128 new_hmi_value(index, value, oldval) { |
172 new_hmi_value(index, value, oldval) { |
129 try { |
173 try { |
130 // TODO avoid searching, store index at sub() |
174 // TODO avoid searching, store index at sub() |
131 for(let i = 0; i < this.indexes.length; i++) { |
175 for(let i = 0; i < this.indexes.length; i++) { |
132 let refindex = this.indexes[i]; |
176 let refindex = this.get_variable_index(i); |
133 if(this.relativeness[i]) |
|
134 refindex += this.offset; |
|
135 |
177 |
136 if(index == refindex) { |
178 if(index == refindex) { |
137 let d = this.dispatch; |
179 let d = this.dispatch; |
138 if(typeof(d) == "function"){ |
180 if(typeof(d) == "function"){ |
139 d.call(this, value, oldval, i); |
181 d.call(this, value, oldval, i); |