svghmi/widget_jump.ysl2
changeset 3302 c89fc366bebd
parent 3242 f037e901a17c
child 3512 fce3d407bb46
equal deleted inserted replaced
2744:577118ebd179 3302:c89fc366bebd
       
     1 // widget_jump.ysl2
       
     2 
       
     3 widget_desc("Jump") {
       
     4     longdesc
       
     5     ||
       
     6     Jump widget brings focus to a different page. Mandatory single argument
       
     7     gives name of the page.
       
     8 
       
     9     Optional single path is used as new reference when jumping to a relative
       
    10     page, it must point to a HMI_NODE.
       
    11 
       
    12     "active"+"inactive" labeled elements can be provided and reflect current
       
    13     page being shown.
       
    14 
       
    15     "disabled" labeled element, if provided, is shown instead of "active" or
       
    16     "inactive" widget when pointed HMI_NODE is null.
       
    17     ||
       
    18 
       
    19     shortdesc > Jump to given page
       
    20 
       
    21     arg name="page" accepts="string" > name of page to jump to
       
    22 
       
    23     path name="reference" count="optional" accepts="HMI_NODE" > reference for relative jump
       
    24 }
       
    25 
       
    26 widget_class("Jump") {
       
    27 ||
       
    28         activable = false;
       
    29         active = false;
       
    30         disabled = false;
       
    31         frequency = 2;
       
    32 
       
    33         update_activity() {
       
    34             if(this.active) {
       
    35                  /* show active */ 
       
    36                  this.active_elt.style.display = "";
       
    37                  /* hide inactive */ 
       
    38                  this.inactive_elt.style.display = "none";
       
    39             } else {
       
    40                  /* show inactive */ 
       
    41                  this.inactive_elt.style.display = "";
       
    42                  /* hide active */ 
       
    43                  this.active_elt.style.display = "none";
       
    44             }
       
    45         }
       
    46 
       
    47         update_disability() {
       
    48             if(this.disabled) {
       
    49                 /* show disabled */ 
       
    50                 this.disabled_elt.style.display = "";
       
    51                 /* hide inactive */ 
       
    52                 this.inactive_elt.style.display = "none";
       
    53                 /* hide active */ 
       
    54                 this.active_elt.style.display = "none";
       
    55             } else {
       
    56                 /* hide disabled */ 
       
    57                 this.disabled_elt.style.display = "none";
       
    58                 this.update_activity();
       
    59             }
       
    60         }
       
    61 
       
    62         make_on_click() {
       
    63             let that = this;
       
    64             const name = this.args[0];
       
    65             return function(evt){
       
    66                 /* TODO: in order to allow jumps to page selected through for exemple a dropdown,
       
    67                    support path pointing to local variable whom value 
       
    68                    would be an HMI_TREE index and then jump to a relative page not hard-coded in advance */
       
    69 
       
    70                 if(!that.disabled) {
       
    71                     const index = that.indexes.length > 0 ? that.indexes[0] + that.offset : undefined;
       
    72                     switch_page(name, index);
       
    73                 }
       
    74             }
       
    75         }
       
    76 
       
    77         notify_page_change(page_name, index) {
       
    78             if(this.activable) {
       
    79                 const ref_index = this.indexes.length > 0 ? this.indexes[0] + this.offset : undefined;
       
    80                 const ref_name = this.args[0];
       
    81                 this.active = ((ref_name == undefined || ref_name == page_name) && index == ref_index);
       
    82                 this.update_state();
       
    83             }
       
    84         }
       
    85 
       
    86         dispatch(value) {
       
    87             this.disabled = !Number(value);
       
    88             this.update_state();
       
    89         }
       
    90 ||
       
    91 }
       
    92 
       
    93 widget_defs("Jump") {
       
    94     // TODO: ensure both active and inactive are provided
       
    95     const "activity" optional_labels("active inactive");
       
    96     const "have_activity","string-length($activity)>0";
       
    97     value "$activity";
       
    98 
       
    99     const "disability" optional_labels("disabled");
       
   100     const "have_disability","$have_activity and string-length($disability)>0";
       
   101     value "$disability";
       
   102 
       
   103     |     init: function() {
       
   104     |         this.element.onclick = this.make_on_click();
       
   105     if "$have_activity" {
       
   106     |         this.activable = true;
       
   107     }
       
   108     if "not($have_disability)" {
       
   109     |         this.unsubscribable = true;
       
   110     }
       
   111     >         this.update_state = 
       
   112     choose {
       
   113         when "$have_disability" {
       
   114             > this.update_disability
       
   115         }
       
   116         when "$have_activity" {
       
   117             > this.update_activity
       
   118         }
       
   119         otherwise > null
       
   120     }
       
   121     > ;\n
       
   122     |     },
       
   123 
       
   124 }
       
   125 
       
   126 widget_page("Jump"){
       
   127     param "page_desc";
       
   128     /* check that given path is compatible with page's reference path */
       
   129     if "path" {
       
   130         /* TODO: suport local variable containing an HMI_TREE index to jump to a relative page */
       
   131         /* when no page name provided, check for same page */
       
   132         const "target_page_name" choose {
       
   133             when "arg" value "arg[1]/@value";
       
   134             otherwise value "$page_desc/arg[1]/@value";
       
   135         }
       
   136         const "target_page_path" choose {
       
   137             when "arg" value "$hmi_pages_descs[arg[1]/@value = $target_page_name]/path[1]/@value";
       
   138             otherwise value "$page_desc/path[1]/@value";
       
   139         }
       
   140 
       
   141         if "not(func:same_class_paths($target_page_path, path[1]/@value))"
       
   142             error > Jump id="«@id»" to page "«$target_page_name»" with incompatible path "«path[1]/@value» (must be same class as "«$target_page_path»")
       
   143     }
       
   144 }
       
   145 
       
   146 emit "declarations:jump"
       
   147 ||
       
   148 var jumps_need_update = false;
       
   149 var jump_history = [[default_page, undefined]];
       
   150 
       
   151 function update_jumps() {
       
   152     page_desc[current_visible_page].jumps.map(w=>w.notify_page_change(current_visible_page,current_page_index));
       
   153     jumps_need_update = false;
       
   154 };
       
   155 
       
   156 ||
       
   157