svghmi/widget_keypad.ysl2
changeset 3302 c89fc366bebd
parent 3241 fe945f1f48b7
child 3508 14d696d7d54e
equal deleted inserted replaced
2744:577118ebd179 3302:c89fc366bebd
       
     1 // widget_keypad.ysl2
       
     2 
       
     3 widget_desc("Keypad") {
       
     4     longdesc
       
     5     ||
       
     6     Keypad - to be written
       
     7     ||
       
     8 
       
     9     shortdesc > Keypad 
       
    10 
       
    11     arg name="supported_types" accepts="string" > keypad can input those types 
       
    12     
       
    13 }
       
    14 
       
    15 emit "declarations:keypad" {
       
    16     |
       
    17     | var keypads = {
       
    18     foreach "$keypads_descs"{
       
    19         const "keypad_id","@id";
       
    20         foreach "arg"{
       
    21             const "g", "$geometry[@Id = $keypad_id]";
       
    22     |     "«@value»":["«$keypad_id»", «$g/@x», «$g/@y»],
       
    23         }
       
    24     }
       
    25     | }
       
    26 }
       
    27 
       
    28 widget_class("Keypad")
       
    29     ||
       
    30          on_key_click(symbols) {
       
    31              var syms = symbols.split(" ");
       
    32              this.shift |= this.caps;
       
    33              this.editstr += syms[this.shift?syms.length-1:0];
       
    34              this.shift = false;
       
    35              this.update();
       
    36          }
       
    37 
       
    38          on_Esc_click() {
       
    39              end_modal.call(this);
       
    40          }
       
    41 
       
    42          on_Enter_click() {
       
    43              let coercedval = (typeof this.initial) == "number" ? Number(this.editstr) : this.editstr;
       
    44              if(typeof coercedval == 'number' && isNaN(coercedval)){
       
    45                  // revert to initial so it explicitely shows input was ignored
       
    46                  this.editstr = String(this.initial);
       
    47                  this.update();
       
    48              } else { 
       
    49                  let callback_obj = this.result_callback_obj;
       
    50                  end_modal.call(this);
       
    51                  callback_obj.edit_callback(coercedval);
       
    52              }
       
    53          }
       
    54 
       
    55          on_BackSpace_click() {
       
    56              this.editstr = this.editstr.slice(0,this.editstr.length-1);
       
    57              this.update();
       
    58          }
       
    59 
       
    60          on_Sign_click() {
       
    61              if(this.editstr[0] == "-")
       
    62                  this.editstr = this.editstr.slice(1,this.editstr.length);
       
    63              else
       
    64                  this.editstr = "-" + this.editstr;
       
    65              this.update();
       
    66          }
       
    67 
       
    68          on_NumDot_click() {
       
    69              if(this.editstr.indexOf(".") == "-1"){
       
    70                  this.editstr += ".";
       
    71                  this.update();
       
    72              }
       
    73          }
       
    74 
       
    75          on_Space_click() {
       
    76              this.editstr += " ";
       
    77              this.update();
       
    78          }
       
    79 
       
    80          caps = false;
       
    81          _caps = undefined;
       
    82          on_CapsLock_click() {
       
    83              this.caps = !this.caps;
       
    84              this.update();
       
    85          }
       
    86 
       
    87          shift = false;
       
    88          _shift = undefined;
       
    89          on_Shift_click() {
       
    90              this.shift = !this.shift;
       
    91              this.caps = false;
       
    92              this.update();
       
    93          }
       
    94          editstr = "";
       
    95          _editstr = undefined;
       
    96          result_callback_obj = undefined;
       
    97          start_edit(info, valuetype, callback_obj, initial,size) {
       
    98              show_modal.call(this,size);
       
    99              this.editstr = String(initial);
       
   100              this.result_callback_obj = callback_obj;
       
   101              this.Info_elt.textContent = info;
       
   102              this.shift = false;
       
   103              this.caps = false;
       
   104              this.initial = initial;
       
   105 
       
   106              this.update();
       
   107          }
       
   108 
       
   109          update() {
       
   110              if(this.editstr != this._editstr){
       
   111                  this._editstr = this.editstr;
       
   112                  this.Value_elt.textContent = this.editstr;
       
   113              }
       
   114              if(this.Shift_sub && this.shift != this._shift){
       
   115                  this._shift = this.shift;
       
   116                  (this.shift?this.activate_activable:this.inactivate_activable)(this.Shift_sub);
       
   117              }
       
   118              if(this.CapsLock_sub && this.caps != this._caps){
       
   119                  this._caps = this.caps;
       
   120                  (this.caps?this.activate_activable:this.inactivate_activable)(this.CapsLock_sub);
       
   121              }
       
   122          }
       
   123     ||
       
   124 
       
   125 widget_defs("Keypad") {
       
   126     labels("Esc Enter BackSpace Keys Info Value");
       
   127     optional_labels("Sign Space NumDot");
       
   128     activable_labels("CapsLock Shift");
       
   129     |     init: function() {
       
   130     foreach "$hmi_element/*[@inkscape:label = 'Keys']/*" {
       
   131     |         id("«@id»").setAttribute("onclick", "hmi_widgets['«$hmi_element/@id»'].on_key_click('«func:escape_quotes(@inkscape:label)»')");
       
   132     }
       
   133     foreach "str:split('Esc Enter BackSpace Sign Space NumDot CapsLock Shift')" {
       
   134     |         if(this.«.»_elt)
       
   135     |             this.«.»_elt.setAttribute("onclick", "hmi_widgets['«$hmi_element/@id»'].on_«.»_click()");
       
   136     }
       
   137     |     },
       
   138     |
       
   139     const "g", "$geometry[@Id = $hmi_element/@id]"; 
       
   140     |     coordinates: [«$g/@x», «$g/@y»],
       
   141 }