svghmi/widget_foreach.ysl2
branchsvghmi
changeset 2952 2adfce8908b4
parent 2943 304e88bae115
child 2959 ae549ba1a116
equal deleted inserted replaced
2951:506fe9272660 2952:2adfce8908b4
    43     |           ]`if "position()!=last()" > ,`
    43     |           ]`if "position()!=last()" > ,`
    44     }
    44     }
    45     |         ]
    45     |         ]
    46     |     },
    46     |     },
    47     |     item_offset: 0,
    47     |     item_offset: 0,
    48     |     on_click: foreach_onclick,
       
    49 }
    48 }
    50 
    49 
    51 template "widget[@type='ForEach']", mode="widget_subscribe"{
    50 template "widget[@type='ForEach']", mode="widget_class"
    52     // param "hmi_element";
    51 ||
    53     |     sub: foreach_subscribe,
    52 class ForEachWidget extends Widget{
    54     |     unsub: foreach_unsubscribe,
    53     unsub(){
    55     |     apply_cache: foreach_apply_cache,
    54         for(let item of this.items){
    56 }
    55             for(let widget of item) {
       
    56                 widget.unsub();
       
    57             }
       
    58         }
       
    59         this.offset = 0;
       
    60     }
    57 
    61 
    58 emit "definitions:foreach"
    62     foreach_widgets_do(new_offset, todo){
    59 ||
    63         this.offset = new_offset;
    60 function foreach_unsubscribe(){
    64         for(let i = 0; i < this.items.length; i++) {
    61     for(let item of this.items){
    65             let item = this.items[i];
    62         for(let widget of item) {
    66             let orig_item_index = this.index_pool[i];
    63             unsubscribe.call(widget);
    67             let item_index = this.index_pool[i+this.item_offset];
       
    68             let item_index_offset = item_index - orig_item_index;
       
    69             for(let widget of item) {
       
    70                 todo(widget).call(widget, new_offset + item_index_offset);
       
    71             }
    64         }
    72         }
    65     }
    73     }
    66     this.offset = 0;
       
    67 }
       
    68 
    74 
    69 function foreach_widgets_do(new_offset, todo){
    75     sub(new_offset=0){
    70     this.offset = new_offset;
    76         this.foreach_widgets_do(new_offset, w=>w.sub);
    71     for(let i = 0; i < this.items.length; i++) {
    77     }
    72         let item = this.items[i];
    78 
    73         let orig_item_index = this.index_pool[i];
    79     apply_cache() {
    74         let item_index = this.index_pool[i+this.item_offset];
    80         this.foreach_widgets_do(this.offset, w=>w.apply_cache);
    75         let item_index_offset = item_index - orig_item_index;
    81     }
    76         for(let widget of item) {
    82 
    77             todo.call(widget, new_offset + item_index_offset);
    83     on_click(opstr, evt) {
       
    84         let new_item_offset = eval(String(this.item_offset)+opstr);
       
    85         if(new_item_offset + this.items.length > this.index_pool.length) {
       
    86             if(this.item_offset + this.items.length == this.index_pool.length)
       
    87                 new_item_offset = 0;
       
    88             else
       
    89                 new_item_offset = this.index_pool.length - this.items.length;
       
    90         } else if(new_item_offset < 0) {
       
    91             if(this.item_offset == 0)
       
    92                 new_item_offset = this.index_pool.length - this.items.length;
       
    93             else
       
    94                 new_item_offset = 0;
    78         }
    95         }
       
    96         this.item_offset = new_item_offset;
       
    97         this.unsub();
       
    98         this.sub(this.offset);
       
    99         update_subscriptions();
       
   100         need_cache_apply.push(this);
       
   101         jumps_need_update = true;
       
   102         requestHMIAnimation();
    79     }
   103     }
    80 }
   104 }
       
   105 ||
    81 
   106 
    82 function foreach_subscribe(new_offset=0){
       
    83     foreach_widgets_do.call(this, new_offset, subscribe);
       
    84 }
       
    85 
       
    86 function foreach_apply_cache() {
       
    87     foreach_widgets_do.call(this, this.offset, widget_apply_cache);
       
    88 }
       
    89 
       
    90 function foreach_onclick(opstr, evt) {
       
    91     new_item_offset = eval(String(this.item_offset)+opstr)
       
    92     if(new_item_offset + this.items.length > this.index_pool.length) {
       
    93         if(this.item_offset + this.items.length == this.index_pool.length)
       
    94             new_item_offset = 0;
       
    95         else
       
    96             new_item_offset = this.index_pool.length - this.items.length;
       
    97     } else if(new_item_offset < 0) {
       
    98         if(this.item_offset == 0)
       
    99             new_item_offset = this.index_pool.length - this.items.length;
       
   100         else
       
   101             new_item_offset = 0;
       
   102     }
       
   103     this.item_offset = new_item_offset;
       
   104     off = this.offset;
       
   105     foreach_unsubscribe.call(this);
       
   106     foreach_subscribe.call(this,off);
       
   107     update_subscriptions();
       
   108     need_cache_apply.push(this);
       
   109     jumps_need_update = true;
       
   110     requestHMIAnimation();
       
   111 }
       
   112 
       
   113 ||