edouard@2883: // widget_display.ysl2 Edouard@2792: Edouard@2790: edouard@2998: template "widget[@type='Display']", mode="widget_class" edouard@2998: || edouard@2998: class DisplayWidget extends Widget{ edouard@2998: frequency = 5; edouard@3008: dispatch(value, oldval, index) { edouard@3008: this.fields[index] = value; edouard@3142: this.request_animate(); edouard@2998: } edouard@2998: } edouard@2998: || edouard@2998: edouard@2883: template "widget[@type='Display']", mode="widget_defs" { edouard@2883: param "hmi_element"; edouard@3142: edouard@3142: const "format" optional_labels("format"); edouard@3142: const "has_format","string-length($format)>0"; edouard@3142: value "$format"; edouard@3142: edouard@3142: if "$hmi_element[not(self::svg:text)] and not($has_format)" edouard@3142: error > Display Widget id="«$hmi_element/@id»" must be a svg::text element itself or a group containing a svg:text element labelled "format" edouard@3008: Edouard@3022: const "field_initializer" foreach "path" { Edouard@3022: choose{ Edouard@3022: when "@type='HMI_STRING'" > "" Edouard@3065: otherwise > 0 Edouard@3022: } Edouard@3022: if "position()!=last()" > , Edouard@3022: } Edouard@3022: | fields: [«$field_initializer»], edouard@3142: | animate: function(){ edouard@3142: choose { edouard@3142: when "$has_format" { edouard@3142: | if(this.format_elt.getAttribute("lang")) { edouard@3142: | this.format = svg_text_to_multiline(this.format_elt); edouard@3142: | this.format_elt.removeAttribute("lang"); edouard@3142: | } edouard@3142: | let str = vsprintf(this.format,this.fields); edouard@3142: | multiline_to_svg_text(this.format_elt, str); edouard@3142: } edouard@3142: otherwise { edouard@3142: | let str = this.args.length == 1 ? vsprintf(this.args[0],this.fields) : this.fields.join(' '); edouard@3142: | multiline_to_svg_text(this.element, str); edouard@3142: } edouard@3142: } edouard@3142: | }, edouard@3142: | edouard@3142: if "$has_format" { edouard@3142: | init: function() { edouard@3142: | this.format = svg_text_to_multiline(this.format_elt); edouard@3142: | }, edouard@3142: } Edouard@2753: } edouard@3008: edouard@3008: emit "preamble:display" edouard@3008: || edouard@3008: /* https://github.com/alexei/sprintf.js/blob/master/src/sprintf.js */ edouard@3008: /* global window, exports, define */ edouard@3008: edouard@3008: !function() { edouard@3008: 'use strict' edouard@3008: edouard@3008: var re = { edouard@3008: not_string: /[^s]/, edouard@3008: not_bool: /[^t]/, edouard@3008: not_type: /[^T]/, edouard@3008: not_primitive: /[^v]/, edouard@3008: number: /[diefg]/, edouard@3008: numeric_arg: /[bcdiefguxX]/, edouard@3008: json: /[j]/, edouard@3008: not_json: /[^j]/, edouard@3008: text: /^[^\x25]+/, edouard@3008: modulo: /^\x25{2}/, edouard@3008: placeholder: /^\x25(?:([1-9]\d*)\$|\(([^)]+)\))?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-gijostTuvxX])/, edouard@3008: key: /^([a-z_][a-z_\d]*)/i, edouard@3008: key_access: /^\.([a-z_][a-z_\d]*)/i, edouard@3008: index_access: /^\[(\d+)\]/, edouard@3008: sign: /^[+-]/ edouard@3008: } edouard@3008: edouard@3008: function sprintf(key) { edouard@3008: // `arguments` is not an array, but should be fine for this call edouard@3008: return sprintf_format(sprintf_parse(key), arguments) edouard@3008: } edouard@3008: edouard@3008: function vsprintf(fmt, argv) { edouard@3008: return sprintf.apply(null, [fmt].concat(argv || [])) edouard@3008: } edouard@3008: edouard@3008: function sprintf_format(parse_tree, argv) { edouard@3008: var cursor = 1, tree_length = parse_tree.length, arg, output = '', i, k, ph, pad, pad_character, pad_length, is_positive, sign edouard@3008: for (i = 0; i < tree_length; i++) { edouard@3008: if (typeof parse_tree[i] === 'string') { edouard@3008: output += parse_tree[i] edouard@3008: } edouard@3008: else if (typeof parse_tree[i] === 'object') { edouard@3008: ph = parse_tree[i] // convenience purposes only edouard@3008: if (ph.keys) { // keyword argument edouard@3008: arg = argv[cursor] edouard@3008: for (k = 0; k < ph.keys.length; k++) { edouard@3008: if (arg == undefined) { edouard@3008: throw new Error(sprintf('[sprintf] Cannot access property "%s" of undefined value "%s"', ph.keys[k], ph.keys[k-1])) edouard@3008: } edouard@3008: arg = arg[ph.keys[k]] edouard@3008: } edouard@3008: } edouard@3008: else if (ph.param_no) { // positional argument (explicit) edouard@3008: arg = argv[ph.param_no] edouard@3008: } edouard@3008: else { // positional argument (implicit) edouard@3008: arg = argv[cursor++] edouard@3008: } edouard@3008: edouard@3008: if (re.not_type.test(ph.type) && re.not_primitive.test(ph.type) && arg instanceof Function) { edouard@3008: arg = arg() edouard@3008: } edouard@3008: edouard@3008: if (re.numeric_arg.test(ph.type) && (typeof arg !== 'number' && isNaN(arg))) { edouard@3008: throw new TypeError(sprintf('[sprintf] expecting number but found %T', arg)) edouard@3008: } edouard@3008: edouard@3008: if (re.number.test(ph.type)) { edouard@3008: is_positive = arg >= 0 edouard@3008: } edouard@3008: edouard@3008: switch (ph.type) { edouard@3008: case 'b': edouard@3008: arg = parseInt(arg, 10).toString(2) edouard@3008: break edouard@3008: case 'c': edouard@3008: arg = String.fromCharCode(parseInt(arg, 10)) edouard@3008: break edouard@3008: case 'd': edouard@3008: case 'i': edouard@3008: arg = parseInt(arg, 10) edouard@3008: break edouard@3008: case 'j': edouard@3008: arg = JSON.stringify(arg, null, ph.width ? parseInt(ph.width) : 0) edouard@3008: break edouard@3008: case 'e': edouard@3008: arg = ph.precision ? parseFloat(arg).toExponential(ph.precision) : parseFloat(arg).toExponential() edouard@3008: break edouard@3008: case 'f': edouard@3008: arg = ph.precision ? parseFloat(arg).toFixed(ph.precision) : parseFloat(arg) edouard@3008: break edouard@3008: case 'g': edouard@3008: arg = ph.precision ? String(Number(arg.toPrecision(ph.precision))) : parseFloat(arg) edouard@3008: break edouard@3008: case 'o': edouard@3008: arg = (parseInt(arg, 10) >>> 0).toString(8) edouard@3008: break edouard@3008: case 's': edouard@3008: arg = String(arg) edouard@3008: arg = (ph.precision ? arg.substring(0, ph.precision) : arg) edouard@3008: break edouard@3008: case 't': edouard@3008: arg = String(!!arg) edouard@3008: arg = (ph.precision ? arg.substring(0, ph.precision) : arg) edouard@3008: break edouard@3008: case 'T': edouard@3008: arg = Object.prototype.toString.call(arg).slice(8, -1).toLowerCase() edouard@3008: arg = (ph.precision ? arg.substring(0, ph.precision) : arg) edouard@3008: break edouard@3008: case 'u': edouard@3008: arg = parseInt(arg, 10) >>> 0 edouard@3008: break edouard@3008: case 'v': edouard@3008: arg = arg.valueOf() edouard@3008: arg = (ph.precision ? arg.substring(0, ph.precision) : arg) edouard@3008: break edouard@3008: case 'x': edouard@3008: arg = (parseInt(arg, 10) >>> 0).toString(16) edouard@3008: break edouard@3008: case 'X': edouard@3008: arg = (parseInt(arg, 10) >>> 0).toString(16).toUpperCase() edouard@3008: break edouard@3008: } edouard@3008: if (re.json.test(ph.type)) { edouard@3008: output += arg edouard@3008: } edouard@3008: else { edouard@3008: if (re.number.test(ph.type) && (!is_positive || ph.sign)) { edouard@3008: sign = is_positive ? '+' : '-' edouard@3008: arg = arg.toString().replace(re.sign, '') edouard@3008: } edouard@3008: else { edouard@3008: sign = '' edouard@3008: } edouard@3008: pad_character = ph.pad_char ? ph.pad_char === '0' ? '0' : ph.pad_char.charAt(1) : ' ' edouard@3008: pad_length = ph.width - (sign + arg).length edouard@3008: pad = ph.width ? (pad_length > 0 ? pad_character.repeat(pad_length) : '') : '' edouard@3008: output += ph.align ? sign + arg + pad : (pad_character === '0' ? sign + pad + arg : pad + sign + arg) edouard@3008: } edouard@3008: } edouard@3008: } edouard@3008: return output edouard@3008: } edouard@3008: edouard@3008: var sprintf_cache = Object.create(null) edouard@3008: edouard@3008: function sprintf_parse(fmt) { edouard@3008: if (sprintf_cache[fmt]) { edouard@3008: return sprintf_cache[fmt] edouard@3008: } edouard@3008: edouard@3008: var _fmt = fmt, match, parse_tree = [], arg_names = 0 edouard@3008: while (_fmt) { edouard@3008: if ((match = re.text.exec(_fmt)) !== null) { edouard@3008: parse_tree.push(match[0]) edouard@3008: } edouard@3008: else if ((match = re.modulo.exec(_fmt)) !== null) { edouard@3008: parse_tree.push('%') edouard@3008: } edouard@3008: else if ((match = re.placeholder.exec(_fmt)) !== null) { edouard@3008: if (match[2]) { edouard@3008: arg_names |= 1 edouard@3008: var field_list = [], replacement_field = match[2], field_match = [] edouard@3008: if ((field_match = re.key.exec(replacement_field)) !== null) { edouard@3008: field_list.push(field_match[1]) edouard@3008: while ((replacement_field = replacement_field.substring(field_match[0].length)) !== '') { edouard@3008: if ((field_match = re.key_access.exec(replacement_field)) !== null) { edouard@3008: field_list.push(field_match[1]) edouard@3008: } edouard@3008: else if ((field_match = re.index_access.exec(replacement_field)) !== null) { edouard@3008: field_list.push(field_match[1]) edouard@3008: } edouard@3008: else { edouard@3008: throw new SyntaxError('[sprintf] failed to parse named argument key') edouard@3008: } edouard@3008: } edouard@3008: } edouard@3008: else { edouard@3008: throw new SyntaxError('[sprintf] failed to parse named argument key') edouard@3008: } edouard@3008: match[2] = field_list edouard@3008: } edouard@3008: else { edouard@3008: arg_names |= 2 edouard@3008: } edouard@3008: if (arg_names === 3) { edouard@3008: throw new Error('[sprintf] mixing positional and named placeholders is not (yet) supported') edouard@3008: } edouard@3008: edouard@3008: parse_tree.push( edouard@3008: { edouard@3008: placeholder: match[0], edouard@3008: param_no: match[1], edouard@3008: keys: match[2], edouard@3008: sign: match[3], edouard@3008: pad_char: match[4], edouard@3008: align: match[5], edouard@3008: width: match[6], edouard@3008: precision: match[7], edouard@3008: type: match[8] edouard@3008: } edouard@3008: ) edouard@3008: } edouard@3008: else { edouard@3008: throw new SyntaxError('[sprintf] unexpected placeholder') edouard@3008: } edouard@3008: _fmt = _fmt.substring(match[0].length) edouard@3008: } edouard@3008: return sprintf_cache[fmt] = parse_tree edouard@3008: } edouard@3008: edouard@3008: /** edouard@3008: * export to either browser or node.js edouard@3008: */ edouard@3008: /* eslint-disable quote-props */ edouard@3008: if (typeof exports !== 'undefined') { edouard@3008: exports['sprintf'] = sprintf edouard@3008: exports['vsprintf'] = vsprintf edouard@3008: } edouard@3008: if (typeof window !== 'undefined') { edouard@3008: window['sprintf'] = sprintf edouard@3008: window['vsprintf'] = vsprintf edouard@3008: edouard@3008: if (typeof define === 'function' && define['amd']) { edouard@3008: define(function() { edouard@3008: return { edouard@3008: 'sprintf': sprintf, edouard@3008: 'vsprintf': vsprintf edouard@3008: } edouard@3008: }) edouard@3008: } edouard@3008: } edouard@3008: /* eslint-enable quote-props */ edouard@3008: }(); // eslint-disable-line edouard@3008: ||