svghmi/widgets_common.ysl2
branchsvghmi
changeset 3025 48e7e336c052
parent 3024 0a9f6f29b7dd
child 3026 d454ed480c0f
equal deleted inserted replaced
3024:0a9f6f29b7dd 3025:48e7e336c052
    30             when "not(@index)" {
    30             when "not(@index)" {
    31                 choose {
    31                 choose {
    32                     when "not(@type)" 
    32                     when "not(@type)" 
    33                         error > Widget «$widget/@type» id="«$eltid»" : No match for path "«@value»" in HMI tree
    33                         error > Widget «$widget/@type» id="«$eltid»" : No match for path "«@value»" in HMI tree
    34                     when "@type = 'PAGE_LOCAL'" 
    34                     when "@type = 'PAGE_LOCAL'" 
    35                         > "«substring(@value, 1)»"`if "position()!=last()" > ,`
    35                         > "«@value»"`if "position()!=last()" > ,`
    36                     when "@type = 'HMI_LOCAL'" 
    36                     when "@type = 'HMI_LOCAL'" 
    37                         > hmi_local_index("«@value»")`if "position()!=last()" > ,`
    37                         > hmi_local_index("«@value»")`if "position()!=last()" > ,`
    38                 }
    38                 }
    39             }
    39             }
    40             otherwise {
    40             otherwise {
    73     ||
    73     ||
    74     let hmi_locals = {};
    74     let hmi_locals = {};
    75     var last_remote_index = hmitree_types.length - 1;
    75     var last_remote_index = hmitree_types.length - 1;
    76     var next_available_index = hmitree_types.length;
    76     var next_available_index = hmitree_types.length;
    77 
    77 
       
    78     const local_defaults = {
       
    79     ||
       
    80     foreach "$parsed_widgets/widget[@type = 'VarInit']"{
       
    81         if "count(path) != 1" error > VarInit «@id» must have only one variable given.
       
    82         if "path/@type != 'PAGE_LOCAL' and path/@type != 'HMI_LOCAL'" error > VarInit «@id» only applies to HMI variable.
       
    83         | "«path/@value»":«arg[1]/@value»`if "position()!=last()" > ,`
       
    84     }
       
    85     ||
       
    86     };
    78     var cache = hmitree_types.map(_ignored => undefined);
    87     var cache = hmitree_types.map(_ignored => undefined);
    79 
    88 
    80     function page_local_index(varname, pagename){
    89     function page_local_index(varname, pagename){
    81         let pagevars = hmi_locals[pagename];
    90         let pagevars = hmi_locals[pagename];
    82         let new_index;
    91         let new_index;
    90             }
    99             }
    91 
   100 
    92             new_index = next_available_index++;
   101             new_index = next_available_index++;
    93             pagevars[varname] = new_index;
   102             pagevars[varname] = new_index;
    94         }
   103         }
       
   104         let defaultval = local_defaults[varname];
       
   105         if(defaultval != undefined) 
       
   106             cache[new_index] = defaultval; 
    95         return new_index;
   107         return new_index;
    96     }
   108     }
    97 
   109 
    98     function hmi_local_index(varname){
   110     function hmi_local_index(varname){
    99         return page_local_index(varname, "HMI_LOCAL");
   111         return page_local_index(varname, "HMI_LOCAL");
   144                 }
   156                 }
   145             need_cache_apply.push(this); 
   157             need_cache_apply.push(this); 
   146         }
   158         }
   147 
   159 
   148         apply_cache() {
   160         apply_cache() {
   149             if(!this.unsubscribable) for(let index of this.indexes){
   161             let dispatch = this.dispatch;
       
   162             if(dispatch == undefined) return;
       
   163             if(!this.unsubscribable) for(let index in this.indexes){
   150                 /* dispatch current cache in newly opened page widgets */
   164                 /* dispatch current cache in newly opened page widgets */
   151                 let realindex = this.get_variable_index(index);
   165                 let realindex = this.get_variable_index(index);
   152                 let cached_val = cache[realindex];
   166                 let cached_val = cache[realindex];
   153                 if(cached_val != undefined)
   167                 if(cached_val != undefined)
   154                     this.new_hmi_value(realindex, cached_val, cached_val);
   168                     try {
       
   169                         dispatch.call(this, cached_val, cached_val, index);
       
   170                     } catch(err) {
       
   171                         console.log(err);
       
   172                     }
   155             }
   173             }
   156         }
   174         }
   157 
   175 
   158         get_variable_index(varnum) {
   176         get_variable_index(varnum) {
   159             let index = this.indexes[varnum];
   177             let index = this.indexes[varnum];
   173         apply_hmi_value(index, new_val) {
   191         apply_hmi_value(index, new_val) {
   174             return apply_hmi_value(this.get_variable_index(0), new_val);
   192             return apply_hmi_value(this.get_variable_index(0), new_val);
   175         }
   193         }
   176 
   194 
   177         new_hmi_value(index, value, oldval) {
   195         new_hmi_value(index, value, oldval) {
   178             try {
   196             // TODO avoid searching, store index at sub()
   179                 // TODO avoid searching, store index at sub()
   197             let dispatch = this.dispatch;
   180                 for(let i = 0; i < this.indexes.length; i++) {
   198             if(dispatch == undefined) return;
   181                     let refindex = this.get_variable_index(i);
   199             for(let i = 0; i < this.indexes.length; i++) {
   182 
   200                 let refindex = this.get_variable_index(i);
   183                     if(index == refindex) {
   201 
   184                         let d = this.dispatch;
   202                 if(index == refindex) {
   185                         if(typeof(d) == "function"){
   203                     try {
   186                             d.call(this, value, oldval, i);
   204                         dispatch.call(this, value, oldval, i);
   187                         }
   205                     } catch(err) {
   188                         else if(typeof(d) == "object"){
   206                         console.log(err);
   189                             d[i].call(this, value, oldval);
       
   190                         }
       
   191                         /* else dispatch_0, ..., dispatch_n ? */
       
   192                         /*else {
       
   193                             throw new Error("Dunno how to dispatch to widget at index = " + index);
       
   194                         }*/
       
   195                         break;
       
   196                     }
   207                     }
   197                 }
   208                     break;
   198             } catch(err) {
   209                 }
   199                 console.log(err);
       
   200             }
   210             }
   201         }
   211         }
   202         
   212         
   203         _animate(){
   213         _animate(){
   204             this.animate();
   214             this.animate();