liudong
2023-05-29 340f156319b863525e50e900c58e59b86ecb3d5e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<script type="text/javascript">
    RED.nodes.registerType('ui_dropdown',{
        category: RED._("node-red-dashboard/ui_base:ui_base.label.category"),
        color: 'rgb(176, 223, 227)',
        defaults: {
            name: {value: ''},
            label: {value: ''},
            tooltip: {value: ''},
            place: {value: 'Select option'},
            group: {type: 'ui_group', required:true},
            order: {value: 0},
            width: {value: 0, validate: function(v) {
                    var width = v||0;
                    var currentGroup = $('#node-input-group').val()||this.group;
                    var groupNode = RED.nodes.node(currentGroup);
                    var valid = !groupNode || +width <= +groupNode.width;
                    $("#node-input-size").toggleClass("input-error",!valid);
                    return valid;
                }
            },
            height: {value: 0},
            passthru: {value: true},
            multiple: {value: false},
            options: {value:[{value: '', label : ''}],
                validate:function(v) {
                    var unique = new Set(v.map(function(o) {return o.value;}));
                    return v.length == unique.size;
                }},
            payload: {value: ''},
            topic: {value: 'topic', validate: (RED.validators.hasOwnProperty('typedInput')?RED.validators.typedInput('topicType'):function(v) { return true})},
            topicType: {value: 'msg'},
            className: {value: ''}
        },
        inputs:1,
        outputs:1,
        icon: "ui_dropdown.png",
        paletteLabel: 'dropdown',
        label: function() { return this.name || (~this.label.indexOf("{{") ? null : this.label) || 'dropdown'; },
        labelStyle: function() { return this.name?"node_label_italic":""; },
        oneditprepare: function() {
            if (this.multiple === undefined) {
                $("#node-input-multiple").prop('checked', false);;
            }
            $("#node-input-size").elementSizer({
                width: "#node-input-width",
                height: "#node-input-height",
                group: "#node-input-group"
            });
            var un = new Set(this.options.map(function(o) {return o.value}));
            if (this.options.length == un.size) { $("#valWarning").hide(); }
            else { $("#valWarning").show(); }
 
            function generateOption(i, option) {
                var container = $('<li/>',{style:"background: var(--red-ui-secondary-background, #fff); margin:0; padding:8px 0px 0px; border-bottom: 1px solid var(--red-ui-form-input-border-color, #ccc);"});
                var row = $('<div/>').appendTo(container);
                var row2 = $('<div/>',{style:"padding-top:5px; padding-left:175px;"}).appendTo(container);
                var row3 = $('<div/>',{style:"padding-top:5px; padding-left:120px;"}).appendTo(container);
 
                $('<i style="color: var(--red-ui-form-text-color, #eee); cursor:move; margin-left:3px;" class="node-input-option-handle fa fa-bars"></i>').appendTo(row);
 
                var valueField = $('<input/>',{class:"node-input-option-value",type:"text",style:"margin-left:7px; width:calc(50% - 32px);", placeholder: 'Value',value:option.value}).appendTo(row).typedInput({default:option.type||'str',types:['str','num','bool']});
                var labelField = $('<input/>',{class:"node-input-option-label",type:"text",style:"margin-left:7px; width:calc(50% - 32px);", placeholder: 'Label', value:option.label}).appendTo(row);
 
                var finalspan = $('<span/>',{style:"float:right; margin-right:8px;"}).appendTo(row);
                var deleteButton = $('<a/>',{href:"#",class:"editor-button editor-button-small", style:"margin-top:7px; margin-left:5px;"}).appendTo(finalspan);
                $('<i/>',{class:"fa fa-remove"}).appendTo(deleteButton);
 
                deleteButton.click(function() {
                    container.css({"background":"var(--red-ui-secondary-background-inactive, #fee)"});
                    container.fadeOut(300, function() {
                        $(this).remove();
                    });
                });
 
                $("#node-input-option-container").append(container);
            }
 
            $("#node-input-add-option").click(function() {
                generateOption($("#node-input-option-container").children().length+1, {});
                $("#node-input-option-container-div").scrollTop($("#node-input-option-container-div").get(0).scrollHeight);
            });
 
            for (var i=0; i<this.options.length; i++) {
                var option = this.options[i];
                generateOption(i+1,option);
            }
 
            $( "#node-input-option-container" ).sortable({
                axis: "y",
                handle:".node-input-option-handle",
                cursor: "move"
            });
 
            $('#node-input-topic').typedInput({
                default: 'str',
                typeField: $("#node-input-topicType"),
                types: ['str','msg','flow','global']
            });
        },
        oneditsave: function() {
            var options = $("#node-input-option-container").children();
            var node = this;
            node.options = [];
            options.each(function(i) {
                var option = $(this);
                var o = {
                    label: option.find(".node-input-option-label").val(),
                    value: option.find(".node-input-option-value").typedInput('value'),
                    type: option.find(".node-input-option-value").typedInput('type')
                };
                if (option.find(".node-input-option-value").typedInput('type') === "num") {
                    o.value = Number(o.value);
                }
                if (option.find(".node-input-option-value").typedInput('type') === "bool") {
                    o.value = (o.value == "true");
                }
                node.options.push(o);
            });
        },
        oneditresize: function() {
        }
    });
</script>
 
<script type="text/html" data-template-name="ui_dropdown">
    <div class="form-row">
        <label for="node-input-group"><i class="fa fa-table"></i> Group</label>
        <input type="text" id="node-input-group">
    </div>
    <div class="form-row">
        <label><i class="fa fa-object-group"></i> Size</label>
        <input type="hidden" id="node-input-width">
        <input type="hidden" id="node-input-height">
        <button class="editor-button" id="node-input-size"></button>
    </div>
    <div class="form-row">
        <label for="node-input-label"><i class="fa fa-tag"></i> Label</label>
        <input type="text" id="node-input-label" placeholder="optional label">
    </div>
    <div class="form-row">
        <label for="node-input-tooltip"><i class="fa fa-info-circle"></i> Tooltip</label>
        <input type="text" id="node-input-tooltip" placeholder="optional tooltip">
    </div>
    <div class="form-row">
        <label for="node-input-place"><i class="fa fa-tag"></i> Placeholder</label>
        <input type="text" id="node-input-place" placeholder="optional placeholder">
    </div>
    <div class="form-row node-input-option-container-row" style="margin-bottom: 0px;width: 100%">
        <label for="node-input-width" style="vertical-align:top"><i class="fa fa-list-alt"></i> Options</label>
        <div id="node-input-option-container-div" style="box-sizing:border-box; border-radius:5px; height:257px; padding:5px; border:1px solid var(--red-ui-form-input-border-color, #ccc); overflow-y:scroll; display:inline-block; width:calc(70% + 15px);">
            <span id="valWarning" style="color: var(--red-ui-text-color-error, #910000)"><b>All Values must be unique.</b></span>
            <ol id="node-input-option-container" style="list-style-type:none; margin:0;"></ol>
        </div>
    </div>
    <div class="form-row">
        <a href="#" class="editor-button editor-button-small" id="node-input-add-option" style="margin-top:4px; margin-left:103px;"><i class="fa fa-plus"></i> <span>option</span></a>
    </div>
    <div class="form-row">
        <label style="width:auto" for="node-input-multiple"><i class="fa fa-th-list"></i> Allow multiple selections from list: </label>
        <input type="checkbox" checked id="node-input-multiple" style="display:inline-block; width:auto; vertical-align:top;">
    </div>
    <div class="form-row">
        <label style="width:auto" for="node-input-passthru"><i class="fa fa-arrow-right"></i> If <code>msg</code> arrives on input, pass through to output: </label>
        <input type="checkbox" checked id="node-input-passthru" style="display:inline-block; width:auto; vertical-align:top;">
    </div>
    <div class="form-row">
        <label for="node-input-topic"><i class="fa fa-tasks"></i> Topic</label>
        <input type="text" id="node-input-topic" placeholder="optional msg.topic">
        <input type="hidden" id="node-input-topicType">
    </div>
    <div class="form-row">
        <label for="node-input-className"><i class="fa fa-code"></i>  Class</label>
        <input type="text" id="node-input-className" placeholder="Optional CSS class name(s) for widget"/>
    </div>
    <div class="form-row">
        <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
        <input type="text" id="node-input-name">
    </div>
</script>