UNPKG

node-red-contrib-mockingbird

Version:
119 lines (109 loc) 7.48 kB
<script type="text/x-red" data-template-name="mockingbird"> <div class="form-row"> <label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="node-red:common.label.name"></span></label> <input type="text" id="node-input-name" data-i18n="[placeholder]node-red:common.label.name"> </div> <div class="form-row"> <label for="node-input-field"><i class="fa fa-edit"></i> <span data-i18n="node-red:template.label.property"></span></label> <input type="text" id="node-input-field" placeholder="payload" style="width:275px;"> <input type="hidden" id="node-input-fieldType"> </div> <div class="form-row"> <label for="node-input-optionField"><i class="fa fa-edit"></i> <span data-i18n="mockingbird.label.option"></span></label> <input type="text" id="node-input-optionField" placeholder="MBOption" style="width:275px;"> <input type="hidden" id="node-input-optionFieldType"> </div> <div class="form-row" style="position: relative; margin-bottom: 0px;"> <label for="node-input-template"><i class="fa fa-file-code-o"></i> <span data-i18n="node-red:template.label.template"></span></label> <input type="hidden" id="node-input-template" autofocus="autofocus"> </div> <div class="form-row node-text-editor-row"> <div style="height:250px;" class="node-text-editor" id="node-input-template-editor"></div> </div> <div class="form-row"> <label for="node-input-syntax"><i class="fa fa-code"></i> <span data-i18n="mockingbird.label.syntax"></span></label> <select id="node-input-syntax" style="width:180px;"> <option value="text" data-i18n="mockingbird.label.text"></option> <option value="json" data-i18n="mockingbird.label.json"></option> </select> </div> </script> <script type="text/x-red" data-help-name="mockingbird"> <p>Creates dummy data strings based on a handlebars-style template.</p> <p>Uses the <i><a href="https://github.com/webroo/dummy-json" target="_new">dummy-json</a></i> module, which can create rich sets of dummy data for testing or other uses.</p> <p>Will build a string or a parsed JSON object, creating values based on the helper names below: <pre style="word-break:normal">title, firstname, lastname, company, domain, tld, email, street, city, country, countryCode, zipcode, postcode, lat, long, phone, color, hexColor, guid, ipv4, ipv6, lorem, date, time, lowercase, uppercase, int, float, boolean</pre> <p>Multiple values can be generated by use of the <i>repeat</i> syntax.</p> <p>In addition any properties passed in on <code>msg</code> can also be used - for example {{payload}}.</p> <p>Finally <code>msg.seed</code> can be used to preset the pseudo-random seed to ensure repeatability across calls.</p> Additional Custome helper: <pre>randomItem 'item1' 'item2' '...' </pre> Example flow <pre>[{"id":"10189452.18ef3c","type":"tab","label":"流程1"},{"id":"ed7643.abe789c","type":"inject","z":"10189452.18ef3c","name":"","topic":"","payload":"","payloadType":"str","repeat":"3","crontab":"","once":false,"x":270,"y":240,"wires":[["6c645b2e.b7f0f4"]]},{"id":"75379d17.68c494","type":"debug","z":"10189452.18ef3c","name":"","active":true,"console":"false","complete":"false","x":673,"y":240,"wires":[]},{"id":"6c645b2e.b7f0f4","type":"mockingbird","z":"10189452.18ef3c","name":"","field":"payload","fieldType":"msg","optionField":"MBOption","optionFieldType":"flow","syntax":"json","template":"{\n \"ID\": \"{{guid}}\",\n \"value\": {{float 20 80 '0.00'}},\n \"manufacturerID\": \"{{randomItem 'IBM' 'SONY' 'APPLE'}}\",\n \"tags\":[\n {{#repeat 0 3}}\n \"{{tag}}\"\n {{/repeat}}\n ],\n \"location\":{\n \"city\": \"{{city}}\",\n \"zone\": \"{{randomItem 'A区' 'B区' 'C区'}}\",\n \"building\": \"{{randomItem '1号楼' '2号楼'}}\",\n \"floor\": \"{{randomItem '1F' '2F' '3F'}}\",\n \"room\": \"{{randomItem '01' '02' '03'}}\"\n }\n}","x":450,"y":240,"wires":[["75379d17.68c494"]]},{"id":"9f723d02.3aaa3","type":"function","z":"10189452.18ef3c","name":"option","func":"function randomArrayItem(arr){\n return arr[Math.floor(Math.random() * arr.length)]\n}\nflow.set('MBOption', {\n mockdata:{\n cities: ['上海','北京','广州','武汉']\n },\n helpers:{\n tag: function (){\n return 'Tag-' + Math.floor(Math.random() * 99);\n }\n }\n\n})","outputs":1,"noerr":0,"x":330,"y":140,"wires":[[]]},{"id":"425de352.e346cc","type":"inject","z":"10189452.18ef3c","name":"","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":true,"x":150,"y":140,"wires":[["9f723d02.3aaa3"]]}]</pre> </script> <script type="text/javascript"> RED.nodes.registerType('mockingbird',{ color:"rgb(0, 231, 255)", category: 'function', paletteLabel:"mockingbird", defaults: { name: {value:""}, field: {value:"payload"}, fieldType: {value:"msg"}, optionField: {value:"MBOption"}, optionFieldType: {value:"flow"}, syntax: {value:"text"}, template: {value:"{\n \"name\": \"{{firstName}} {{lastName}}\",\n \"work\": \"{{company}}\",\n \"email\": \"{{email}}\",\n \"address\": \"{{int 1 100}} {{street}}\",\n \"country\": \"{{country}}\"\n}"} }, inputs:1, outputs:1, icon: "template.png", label: function() { return this.name || "mockingbird"; }, oneditprepare: function() { var that = this; if (!this.fieldType) { this.fieldType = 'msg'; } $("#node-input-field").typedInput({ default: 'msg', types: ['msg','flow','global'], typeField: $("#node-input-fieldType") }); $("#node-input-optionField").typedInput({ default: 'flow', types: ['msg','flow','global'], typeField: $("#node-input-optionFieldType") }); this.editor = RED.editor.createEditor({ id: 'node-input-template-editor', mode: 'ace/mode/handlebars', value: $("#node-input-template").val() }); RED.library.create({ url:"functions", // where to get the data from type:"function", // the type of object the library is for editor:that.editor, // the field name the main text body goes to fields:['name','outputs'] }); this.editor.focus(); }, oneditsave: function() { $("#node-input-template").val(this.editor.getValue()) delete this.editor; }, oneditresize: function(size) { var rows = $("#dialog-form>div:not(.node-text-editor-row)"); var height = $("#dialog-form").height(); for (var i=0;i<rows.size();i++) { height -= $(rows[i]).outerHeight(true); } var editorRow = $("#dialog-form>div.node-text-editor-row"); height -= (parseInt(editorRow.css("marginTop"))+parseInt(editorRow.css("marginBottom"))); $(".node-text-editor").css("height",height+"px"); this.editor.resize(); } }); </script>