svghmi/widget_display.ysl2
branchsvghmi
changeset 3008 dabad70db1bf
parent 2998 e8f707d99dc3
child 3022 f6fe42b7ce60
equal deleted inserted replaced
3007:360300a8b995 3008:dabad70db1bf
     3 
     3 
     4 template "widget[@type='Display']", mode="widget_class"
     4 template "widget[@type='Display']", mode="widget_class"
     5     ||
     5     ||
     6     class DisplayWidget extends Widget{
     6     class DisplayWidget extends Widget{
     7         frequency = 5;
     7         frequency = 5;
     8         dispatch(value) {
     8         dispatch(value, oldval, index) {
     9             this.element.textContent = String(value);
     9             this.fields[index] = value;    
       
    10             console.log(value, index);
       
    11             this.element.textContent = this.args.length == 1 ? vsprintf(this.args[0],this.fields) : this.fields.join(' ');
    10         }
    12         }
    11     }
    13     }
    12     ||
    14     ||
    13 
    15 
    14 template "widget[@type='Display']", mode="widget_defs" {
    16 template "widget[@type='Display']", mode="widget_defs" {
    15     param "hmi_element";
    17     param "hmi_element";
    16     if "$hmi_element[not(self::svg:text)]"
    18     if "$hmi_element[not(self::svg:text)]"
    17         error > Display Widget id="«$hmi_element/@id»" is not a svg::text element
    19         error > Display Widget id="«$hmi_element/@id»" is not a svg::text element
       
    20 
       
    21     |     fields: [],
    18 }
    22 }
       
    23 
       
    24 emit "preamble:display"
       
    25 ||
       
    26 /* https://github.com/alexei/sprintf.js/blob/master/src/sprintf.js */
       
    27 /* global window, exports, define */
       
    28 
       
    29 !function() {
       
    30     'use strict'
       
    31 
       
    32     var re = {
       
    33         not_string: /[^s]/,
       
    34         not_bool: /[^t]/,
       
    35         not_type: /[^T]/,
       
    36         not_primitive: /[^v]/,
       
    37         number: /[diefg]/,
       
    38         numeric_arg: /[bcdiefguxX]/,
       
    39         json: /[j]/,
       
    40         not_json: /[^j]/,
       
    41         text: /^[^\x25]+/,
       
    42         modulo: /^\x25{2}/,
       
    43         placeholder: /^\x25(?:([1-9]\d*)\$|\(([^)]+)\))?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-gijostTuvxX])/,
       
    44         key: /^([a-z_][a-z_\d]*)/i,
       
    45         key_access: /^\.([a-z_][a-z_\d]*)/i,
       
    46         index_access: /^\[(\d+)\]/,
       
    47         sign: /^[+-]/
       
    48     }
       
    49 
       
    50     function sprintf(key) {
       
    51         // `arguments` is not an array, but should be fine for this call
       
    52         return sprintf_format(sprintf_parse(key), arguments)
       
    53     }
       
    54 
       
    55     function vsprintf(fmt, argv) {
       
    56         return sprintf.apply(null, [fmt].concat(argv || []))
       
    57     }
       
    58 
       
    59     function sprintf_format(parse_tree, argv) {
       
    60         var cursor = 1, tree_length = parse_tree.length, arg, output = '', i, k, ph, pad, pad_character, pad_length, is_positive, sign
       
    61         for (i = 0; i < tree_length; i++) {
       
    62             if (typeof parse_tree[i] === 'string') {
       
    63                 output += parse_tree[i]
       
    64             }
       
    65             else if (typeof parse_tree[i] === 'object') {
       
    66                 ph = parse_tree[i] // convenience purposes only
       
    67                 if (ph.keys) { // keyword argument
       
    68                     arg = argv[cursor]
       
    69                     for (k = 0; k < ph.keys.length; k++) {
       
    70                         if (arg == undefined) {
       
    71                             throw new Error(sprintf('[sprintf] Cannot access property "%s" of undefined value "%s"', ph.keys[k], ph.keys[k-1]))
       
    72                         }
       
    73                         arg = arg[ph.keys[k]]
       
    74                     }
       
    75                 }
       
    76                 else if (ph.param_no) { // positional argument (explicit)
       
    77                     arg = argv[ph.param_no]
       
    78                 }
       
    79                 else { // positional argument (implicit)
       
    80                     arg = argv[cursor++]
       
    81                 }
       
    82 
       
    83                 if (re.not_type.test(ph.type) && re.not_primitive.test(ph.type) && arg instanceof Function) {
       
    84                     arg = arg()
       
    85                 }
       
    86 
       
    87                 if (re.numeric_arg.test(ph.type) && (typeof arg !== 'number' && isNaN(arg))) {
       
    88                     throw new TypeError(sprintf('[sprintf] expecting number but found %T', arg))
       
    89                 }
       
    90 
       
    91                 if (re.number.test(ph.type)) {
       
    92                     is_positive = arg >= 0
       
    93                 }
       
    94 
       
    95                 switch (ph.type) {
       
    96                     case 'b':
       
    97                         arg = parseInt(arg, 10).toString(2)
       
    98                         break
       
    99                     case 'c':
       
   100                         arg = String.fromCharCode(parseInt(arg, 10))
       
   101                         break
       
   102                     case 'd':
       
   103                     case 'i':
       
   104                         arg = parseInt(arg, 10)
       
   105                         break
       
   106                     case 'j':
       
   107                         arg = JSON.stringify(arg, null, ph.width ? parseInt(ph.width) : 0)
       
   108                         break
       
   109                     case 'e':
       
   110                         arg = ph.precision ? parseFloat(arg).toExponential(ph.precision) : parseFloat(arg).toExponential()
       
   111                         break
       
   112                     case 'f':
       
   113                         arg = ph.precision ? parseFloat(arg).toFixed(ph.precision) : parseFloat(arg)
       
   114                         break
       
   115                     case 'g':
       
   116                         arg = ph.precision ? String(Number(arg.toPrecision(ph.precision))) : parseFloat(arg)
       
   117                         break
       
   118                     case 'o':
       
   119                         arg = (parseInt(arg, 10) >>> 0).toString(8)
       
   120                         break
       
   121                     case 's':
       
   122                         arg = String(arg)
       
   123                         arg = (ph.precision ? arg.substring(0, ph.precision) : arg)
       
   124                         break
       
   125                     case 't':
       
   126                         arg = String(!!arg)
       
   127                         arg = (ph.precision ? arg.substring(0, ph.precision) : arg)
       
   128                         break
       
   129                     case 'T':
       
   130                         arg = Object.prototype.toString.call(arg).slice(8, -1).toLowerCase()
       
   131                         arg = (ph.precision ? arg.substring(0, ph.precision) : arg)
       
   132                         break
       
   133                     case 'u':
       
   134                         arg = parseInt(arg, 10) >>> 0
       
   135                         break
       
   136                     case 'v':
       
   137                         arg = arg.valueOf()
       
   138                         arg = (ph.precision ? arg.substring(0, ph.precision) : arg)
       
   139                         break
       
   140                     case 'x':
       
   141                         arg = (parseInt(arg, 10) >>> 0).toString(16)
       
   142                         break
       
   143                     case 'X':
       
   144                         arg = (parseInt(arg, 10) >>> 0).toString(16).toUpperCase()
       
   145                         break
       
   146                 }
       
   147                 if (re.json.test(ph.type)) {
       
   148                     output += arg
       
   149                 }
       
   150                 else {
       
   151                     if (re.number.test(ph.type) && (!is_positive || ph.sign)) {
       
   152                         sign = is_positive ? '+' : '-'
       
   153                         arg = arg.toString().replace(re.sign, '')
       
   154                     }
       
   155                     else {
       
   156                         sign = ''
       
   157                     }
       
   158                     pad_character = ph.pad_char ? ph.pad_char === '0' ? '0' : ph.pad_char.charAt(1) : ' '
       
   159                     pad_length = ph.width - (sign + arg).length
       
   160                     pad = ph.width ? (pad_length > 0 ? pad_character.repeat(pad_length) : '') : ''
       
   161                     output += ph.align ? sign + arg + pad : (pad_character === '0' ? sign + pad + arg : pad + sign + arg)
       
   162                 }
       
   163             }
       
   164         }
       
   165         return output
       
   166     }
       
   167 
       
   168     var sprintf_cache = Object.create(null)
       
   169 
       
   170     function sprintf_parse(fmt) {
       
   171         if (sprintf_cache[fmt]) {
       
   172             return sprintf_cache[fmt]
       
   173         }
       
   174 
       
   175         var _fmt = fmt, match, parse_tree = [], arg_names = 0
       
   176         while (_fmt) {
       
   177             if ((match = re.text.exec(_fmt)) !== null) {
       
   178                 parse_tree.push(match[0])
       
   179             }
       
   180             else if ((match = re.modulo.exec(_fmt)) !== null) {
       
   181                 parse_tree.push('%')
       
   182             }
       
   183             else if ((match = re.placeholder.exec(_fmt)) !== null) {
       
   184                 if (match[2]) {
       
   185                     arg_names |= 1
       
   186                     var field_list = [], replacement_field = match[2], field_match = []
       
   187                     if ((field_match = re.key.exec(replacement_field)) !== null) {
       
   188                         field_list.push(field_match[1])
       
   189                         while ((replacement_field = replacement_field.substring(field_match[0].length)) !== '') {
       
   190                             if ((field_match = re.key_access.exec(replacement_field)) !== null) {
       
   191                                 field_list.push(field_match[1])
       
   192                             }
       
   193                             else if ((field_match = re.index_access.exec(replacement_field)) !== null) {
       
   194                                 field_list.push(field_match[1])
       
   195                             }
       
   196                             else {
       
   197                                 throw new SyntaxError('[sprintf] failed to parse named argument key')
       
   198                             }
       
   199                         }
       
   200                     }
       
   201                     else {
       
   202                         throw new SyntaxError('[sprintf] failed to parse named argument key')
       
   203                     }
       
   204                     match[2] = field_list
       
   205                 }
       
   206                 else {
       
   207                     arg_names |= 2
       
   208                 }
       
   209                 if (arg_names === 3) {
       
   210                     throw new Error('[sprintf] mixing positional and named placeholders is not (yet) supported')
       
   211                 }
       
   212 
       
   213                 parse_tree.push(
       
   214                     {
       
   215                         placeholder: match[0],
       
   216                         param_no:    match[1],
       
   217                         keys:        match[2],
       
   218                         sign:        match[3],
       
   219                         pad_char:    match[4],
       
   220                         align:       match[5],
       
   221                         width:       match[6],
       
   222                         precision:   match[7],
       
   223                         type:        match[8]
       
   224                     }
       
   225                 )
       
   226             }
       
   227             else {
       
   228                 throw new SyntaxError('[sprintf] unexpected placeholder')
       
   229             }
       
   230             _fmt = _fmt.substring(match[0].length)
       
   231         }
       
   232         return sprintf_cache[fmt] = parse_tree
       
   233     }
       
   234 
       
   235     /**
       
   236      * export to either browser or node.js
       
   237      */
       
   238     /* eslint-disable quote-props */
       
   239     if (typeof exports !== 'undefined') {
       
   240         exports['sprintf'] = sprintf
       
   241         exports['vsprintf'] = vsprintf
       
   242     }
       
   243     if (typeof window !== 'undefined') {
       
   244         window['sprintf'] = sprintf
       
   245         window['vsprintf'] = vsprintf
       
   246 
       
   247         if (typeof define === 'function' && define['amd']) {
       
   248             define(function() {
       
   249                 return {
       
   250                     'sprintf': sprintf,
       
   251                     'vsprintf': vsprintf
       
   252                 }
       
   253             })
       
   254         }
       
   255     }
       
   256     /* eslint-enable quote-props */
       
   257 }(); // eslint-disable-line    
       
   258 ||