svghmi/sprintf.js
author Edouard Tisserant
Thu, 24 Nov 2022 11:47:56 +0100
changeset 3690 f41733be17a8
parent 3486 0d5bb9038e5b
child 3837 efe0b5b21842
permissions -rw-r--r--
SVGHMI: move declaration of "xmlns" javascript const so that widgets can use it.

This was broken in earlier commit, when changing initial execution order because of implementing websocket reconnect.
3360
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     1
/* https://github.com/alexei/sprintf.js/blob/master/src/sprintf.js */
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     2
/* global window, exports, define */
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     3
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     4
!function() {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     5
    'use strict'
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     6
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     7
    var re = {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     8
        not_string: /[^s]/,
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     9
        not_bool: /[^t]/,
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    10
        not_type: /[^T]/,
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    11
        not_primitive: /[^v]/,
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    12
        number: /[diefg]/,
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    13
        numeric_arg: /[bcdiefguxX]/,
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    14
        json: /[j]/,
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    15
        not_json: /[^j]/,
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    16
        text: /^[^\x25]+/,
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    17
        modulo: /^\x25{2}/,
3451
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
    18
        placeholder: /^\x25(?:([1-9]\d*)\$|\(([^)]+)\))?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-gijostTuvxXD])/,
3360
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    19
        key: /^([a-z_][a-z_\d]*)/i,
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    20
        key_access: /^\.([a-z_][a-z_\d]*)/i,
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    21
        index_access: /^\[(\d+)\]/,
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    22
        sign: /^[+-]/
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    23
    }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    24
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    25
    function sprintf(key) {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    26
        // arguments is not an array, but should be fine for this call
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    27
        return sprintf_format(sprintf_parse(key), arguments)
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    28
    }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    29
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    30
    function vsprintf(fmt, argv) {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    31
        return sprintf.apply(null, [fmt].concat(argv || []))
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    32
    }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    33
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    34
    function sprintf_format(parse_tree, argv) {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    35
        var cursor = 1, tree_length = parse_tree.length, arg, output = '', i, k, ph, pad, pad_character, pad_length, is_positive, sign
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    36
        for (i = 0; i < tree_length; i++) {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    37
            if (typeof parse_tree[i] === 'string') {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    38
                output += parse_tree[i]
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    39
            }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    40
            else if (typeof parse_tree[i] === 'object') {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    41
                ph = parse_tree[i] // convenience purposes only
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    42
                if (ph.keys) { // keyword argument
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    43
                    arg = argv[cursor]
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    44
                    for (k = 0; k < ph.keys.length; k++) {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    45
                        if (arg == undefined) {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    46
                            throw new Error(sprintf('[sprintf] Cannot access property "%s" of undefined value "%s"', ph.keys[k], ph.keys[k-1]))
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    47
                        }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    48
                        arg = arg[ph.keys[k]]
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    49
                    }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    50
                }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    51
                else if (ph.param_no) { // positional argument (explicit)
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    52
                    arg = argv[ph.param_no]
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    53
                }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    54
                else { // positional argument (implicit)
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    55
                    arg = argv[cursor++]
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    56
                }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    57
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    58
                if (re.not_type.test(ph.type) && re.not_primitive.test(ph.type) && arg instanceof Function) {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    59
                    arg = arg()
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    60
                }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    61
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    62
                if (re.numeric_arg.test(ph.type) && (typeof arg !== 'number' && isNaN(arg))) {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    63
                    throw new TypeError(sprintf('[sprintf] expecting number but found %T', arg))
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    64
                }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    65
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    66
                if (re.number.test(ph.type)) {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    67
                    is_positive = arg >= 0
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    68
                }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    69
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    70
                switch (ph.type) {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    71
                    case 'b':
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    72
                        arg = parseInt(arg, 10).toString(2)
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    73
                        break
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    74
                    case 'c':
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    75
                        arg = String.fromCharCode(parseInt(arg, 10))
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    76
                        break
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    77
                    case 'd':
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    78
                    case 'i':
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    79
                        arg = parseInt(arg, 10)
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    80
                        break
3451
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
    81
                    case 'D':
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
    82
                        /*  
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
    83
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
    84
                            select date format with width
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
    85
                            select time format with precision
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
    86
                            %D  => 13:31 AM (default)
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
    87
                            %1D  => 13:31 AM
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
    88
                            %.1D  => 07/07/20
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
    89
                            %1.1D  => 07/07/20, 13:31 AM
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
    90
                            %1.2D  => 07/07/20, 13:31:55 AM
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
    91
                            %2.2D  => May 5, 2022, 9:29:16 AM
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
    92
                            %3.3D  => May 5, 2022 at 9:28:16 AM GMT+2
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
    93
                            %4.4D  => Thursday, May 5, 2022 at 9:26:59 AM Central European Summer Time
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
    94
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
    95
                            see meaning of DateTimeFormat's options "datestyle" and "timestyle" in MDN 
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
    96
                        */
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
    97
3472
2fb9849c6721 SVGHMI: fix syntax error in sprintf.js and force makefile to regen xslt files when sprintf.js changes
Edouard Tisserant
parents: 3451
diff changeset
    98
                        let [datestyle, timestyle] = [ph.width, ph.precision].map(val => ({
3451
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
    99
                            1: "short",
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
   100
                            2: "medium",
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
   101
                            3: "long",
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
   102
                            4: "full"
3472
2fb9849c6721 SVGHMI: fix syntax error in sprintf.js and force makefile to regen xslt files when sprintf.js changes
Edouard Tisserant
parents: 3451
diff changeset
   103
                        }[val]));
3451
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
   104
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
   105
                        if(timestyle === undefined && datestyle === undefined){
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
   106
                            timestyle = "short";
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
   107
                        }
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
   108
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
   109
                        let options = {
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
   110
                            dateStyle: datestyle,
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
   111
                            timeStyle: timestyle,
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
   112
                            hour12: false
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
   113
                        }
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
   114
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
   115
                        /* get lang from globals */
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
   116
                        let lang = get_current_lang_code();
3486
0d5bb9038e5b SVGHMI: prevent exception with date/time formating in sprintf.js if lang wasn't already set.
Edouard Tisserant
parents: 3472
diff changeset
   117
                        let f;
0d5bb9038e5b SVGHMI: prevent exception with date/time formating in sprintf.js if lang wasn't already set.
Edouard Tisserant
parents: 3472
diff changeset
   118
                        try{
0d5bb9038e5b SVGHMI: prevent exception with date/time formating in sprintf.js if lang wasn't already set.
Edouard Tisserant
parents: 3472
diff changeset
   119
                            f = new Intl.DateTimeFormat(lang, options);
0d5bb9038e5b SVGHMI: prevent exception with date/time formating in sprintf.js if lang wasn't already set.
Edouard Tisserant
parents: 3472
diff changeset
   120
                        } catch(e) {
0d5bb9038e5b SVGHMI: prevent exception with date/time formating in sprintf.js if lang wasn't already set.
Edouard Tisserant
parents: 3472
diff changeset
   121
                            f = new Intl.DateTimeFormat('en-US', options);
0d5bb9038e5b SVGHMI: prevent exception with date/time formating in sprintf.js if lang wasn't already set.
Edouard Tisserant
parents: 3472
diff changeset
   122
                        }
0d5bb9038e5b SVGHMI: prevent exception with date/time formating in sprintf.js if lang wasn't already set.
Edouard Tisserant
parents: 3472
diff changeset
   123
                        arg = f.format(arg);
0d5bb9038e5b SVGHMI: prevent exception with date/time formating in sprintf.js if lang wasn't already set.
Edouard Tisserant
parents: 3472
diff changeset
   124
3451
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
   125
                        /*    
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
   126
                            TODO: select with padding char
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
   127
                                  a: absolute time and date (default)
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
   128
                                  r: relative time
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
   129
                        */
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
   130
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3360
diff changeset
   131
                        break
3360
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   132
                    case 'j':
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   133
                        arg = JSON.stringify(arg, null, ph.width ? parseInt(ph.width) : 0)
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   134
                        break
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   135
                    case 'e':
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   136
                        arg = ph.precision ? parseFloat(arg).toExponential(ph.precision) : parseFloat(arg).toExponential()
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   137
                        break
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   138
                    case 'f':
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   139
                        arg = ph.precision ? parseFloat(arg).toFixed(ph.precision) : parseFloat(arg)
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   140
                        break
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   141
                    case 'g':
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   142
                        arg = ph.precision ? String(Number(arg.toPrecision(ph.precision))) : parseFloat(arg)
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   143
                        break
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   144
                    case 'o':
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   145
                        arg = (parseInt(arg, 10) >>> 0).toString(8)
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   146
                        break
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   147
                    case 's':
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   148
                        arg = String(arg)
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   149
                        arg = (ph.precision ? arg.substring(0, ph.precision) : arg)
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   150
                        break
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   151
                    case 't':
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   152
                        arg = String(!!arg)
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   153
                        arg = (ph.precision ? arg.substring(0, ph.precision) : arg)
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   154
                        break
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   155
                    case 'T':
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   156
                        arg = Object.prototype.toString.call(arg).slice(8, -1).toLowerCase()
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   157
                        arg = (ph.precision ? arg.substring(0, ph.precision) : arg)
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   158
                        break
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   159
                    case 'u':
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   160
                        arg = parseInt(arg, 10) >>> 0
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   161
                        break
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   162
                    case 'v':
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   163
                        arg = arg.valueOf()
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   164
                        arg = (ph.precision ? arg.substring(0, ph.precision) : arg)
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   165
                        break
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   166
                    case 'x':
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   167
                        arg = (parseInt(arg, 10) >>> 0).toString(16)
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   168
                        break
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   169
                    case 'X':
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   170
                        arg = (parseInt(arg, 10) >>> 0).toString(16).toUpperCase()
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   171
                        break
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   172
                }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   173
                if (re.json.test(ph.type)) {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   174
                    output += arg
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   175
                }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   176
                else {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   177
                    if (re.number.test(ph.type) && (!is_positive || ph.sign)) {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   178
                        sign = is_positive ? '+' : '-'
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   179
                        arg = arg.toString().replace(re.sign, '')
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   180
                    }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   181
                    else {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   182
                        sign = ''
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   183
                    }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   184
                    pad_character = ph.pad_char ? ph.pad_char === '0' ? '0' : ph.pad_char.charAt(1) : ' '
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   185
                    pad_length = ph.width - (sign + arg).length
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   186
                    pad = ph.width ? (pad_length > 0 ? pad_character.repeat(pad_length) : '') : ''
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   187
                    output += ph.align ? sign + arg + pad : (pad_character === '0' ? sign + pad + arg : pad + sign + arg)
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   188
                }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   189
            }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   190
        }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   191
        return output
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   192
    }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   193
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   194
    var sprintf_cache = Object.create(null)
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   195
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   196
    function sprintf_parse(fmt) {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   197
        if (sprintf_cache[fmt]) {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   198
            return sprintf_cache[fmt]
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   199
        }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   200
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   201
        var _fmt = fmt, match, parse_tree = [], arg_names = 0
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   202
        while (_fmt) {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   203
            if ((match = re.text.exec(_fmt)) !== null) {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   204
                parse_tree.push(match[0])
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   205
            }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   206
            else if ((match = re.modulo.exec(_fmt)) !== null) {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   207
                parse_tree.push('%')
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   208
            }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   209
            else if ((match = re.placeholder.exec(_fmt)) !== null) {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   210
                if (match[2]) {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   211
                    arg_names |= 1
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   212
                    var field_list = [], replacement_field = match[2], field_match = []
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   213
                    if ((field_match = re.key.exec(replacement_field)) !== null) {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   214
                        field_list.push(field_match[1])
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   215
                        while ((replacement_field = replacement_field.substring(field_match[0].length)) !== '') {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   216
                            if ((field_match = re.key_access.exec(replacement_field)) !== null) {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   217
                                field_list.push(field_match[1])
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   218
                            }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   219
                            else if ((field_match = re.index_access.exec(replacement_field)) !== null) {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   220
                                field_list.push(field_match[1])
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   221
                            }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   222
                            else {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   223
                                throw new SyntaxError('[sprintf] failed to parse named argument key')
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   224
                            }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   225
                        }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   226
                    }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   227
                    else {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   228
                        throw new SyntaxError('[sprintf] failed to parse named argument key')
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   229
                    }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   230
                    match[2] = field_list
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   231
                }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   232
                else {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   233
                    arg_names |= 2
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   234
                }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   235
                if (arg_names === 3) {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   236
                    throw new Error('[sprintf] mixing positional and named placeholders is not (yet) supported')
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   237
                }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   238
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   239
                parse_tree.push(
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   240
                    {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   241
                        placeholder: match[0],
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   242
                        param_no:    match[1],
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   243
                        keys:        match[2],
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   244
                        sign:        match[3],
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   245
                        pad_char:    match[4],
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   246
                        align:       match[5],
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   247
                        width:       match[6],
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   248
                        precision:   match[7],
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   249
                        type:        match[8]
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   250
                    }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   251
                )
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   252
            }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   253
            else {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   254
                throw new SyntaxError('[sprintf] unexpected placeholder')
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   255
            }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   256
            _fmt = _fmt.substring(match[0].length)
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   257
        }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   258
        return sprintf_cache[fmt] = parse_tree
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   259
    }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   260
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   261
    /**
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   262
     * export to either browser or node.js
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   263
     */
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   264
    /* eslint-disable quote-props */
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   265
    if (typeof exports !== 'undefined') {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   266
        exports['sprintf'] = sprintf
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   267
        exports['vsprintf'] = vsprintf
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   268
    }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   269
    if (typeof window !== 'undefined') {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   270
        window['sprintf'] = sprintf
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   271
        window['vsprintf'] = vsprintf
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   272
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   273
        if (typeof define === 'function' && define['amd']) {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   274
            define(function() {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   275
                return {
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   276
                    'sprintf': sprintf,
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   277
                    'vsprintf': vsprintf
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   278
                }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   279
            })
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   280
        }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   281
    }
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   282
    /* eslint-enable quote-props */
746e3e3f6537 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   283
}(); // eslint-disable-line