/** * Copyright JS Foundation and other contributors, http://js.foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. **/ (function() { var template = ''; function stringToUTF8Array(str) { var data = []; var i=0, l = str.length; for (i=0; i> 6)); data.push(0x80 | (char & 0x3f)); } else if (char < 0xd800 || char >= 0xe000) { data.push(0xe0 | (char >> 12)); data.push(0x80 | ((char>>6) & 0x3f)); data.push(0x80 | (char & 0x3f)); } else { i++; char = 0x10000 + (((char & 0x3ff)<<10) | (str.charAt(i) & 0x3ff)); data.push(0xf0 | (char >>18)); data.push(0x80 | ((char>>12) & 0x3f)); data.push(0x80 | ((char>>6) & 0x3f)); data.push(0x80 | (char & 0x3f)); } } return data; } var definition = { show: function(options) { var value = options.value; var onComplete = options.complete; var type = "_buffer" if ($("script[data-template-name='"+type+"']").length === 0) { $(template).appendTo("#red-ui-editor-node-configs"); } RED.view.state(RED.state.EDITING); var bufferStringEditor = []; var bufferBinValue; var panels; var trayOptions = { title: options.title, width: "inherit", buttons: [ { id: "node-dialog-cancel", text: RED._("common.label.cancel"), click: function() { RED.tray.close(); } }, { id: "node-dialog-ok", text: RED._("common.label.done"), class: "primary", click: function() { onComplete(JSON.stringify(bufferBinValue)); RED.tray.close(); } } ], resize: function(dimensions) { var height = $("#dialog-form").height(); if (panels) { panels.resize(height); } }, open: function(tray) { var trayBody = tray.find('.red-ui-tray-body'); var dialogForm = RED.editor.buildEditForm(tray.find('.red-ui-tray-body'),'dialog-form',type,'editor'); bufferStringEditor = RED.editor.createEditor({ id: 'red-ui-editor-type-buffer-str', value: "", mode:"ace/mode/text" }); bufferStringEditor.getSession().setValue(value||"",-1); bufferBinEditor = RED.editor.createEditor({ id: 'red-ui-editor-type-buffer-bin', value: "", mode:"ace/mode/text", readOnly: true }); var changeTimer; var buildBuffer = function(data) { var valid = true; var isString = typeof data === 'string'; var binBuffer = []; if (isString) { bufferBinValue = stringToUTF8Array(data); } else { bufferBinValue = data; } var i=0,l=bufferBinValue.length; var c = 0; for(i=0;i 255)) { valid = false; break; } if (i>0) { if (i%8 === 0) { if (i%16 === 0) { binBuffer.push("\n"); } else { binBuffer.push(" "); } } else { binBuffer.push(" "); } } binBuffer.push((d<16?"0":"")+d.toString(16).toUpperCase()); } if (valid) { $("#red-ui-editor-type-buffer-type-string").toggle(isString); $("#red-ui-editor-type-buffer-type-array").toggle(!isString); bufferBinEditor.setValue(binBuffer.join(""),1); } return valid; } var bufferStringUpdate = function() { var value = bufferStringEditor.getValue(); var isValidArray = false; if (/^[\s]*\[[\s\S]*\][\s]*$/.test(value)) { isValidArray = true; try { var data = JSON.parse(value); isValidArray = buildBuffer(data); } catch(err) { isValidArray = false; } } if (!isValidArray) { buildBuffer(value); } } bufferStringEditor.getSession().on('change', function() { clearTimeout(changeTimer); changeTimer = setTimeout(bufferStringUpdate,200); }); bufferStringUpdate(); dialogForm.i18n(); panels = RED.panels.create({ id:"red-ui-editor-type-buffer-panels", resize: function(p1Height,p2Height) { var p1 = $("#red-ui-editor-type-buffer-panel-str"); p1Height -= $(p1.children()[0]).outerHeight(true); var editorRow = $(p1.children()[1]); p1Height -= (parseInt(editorRow.css("marginTop"))+parseInt(editorRow.css("marginBottom"))); $("#red-ui-editor-type-buffer-str").css("height",(p1Height-5)+"px"); bufferStringEditor.resize(); var p2 = $("#red-ui-editor-type-buffer-panel-bin"); editorRow = $(p2.children()[0]); p2Height -= (parseInt(editorRow.css("marginTop"))+parseInt(editorRow.css("marginBottom"))); $("#red-ui-editor-type-buffer-bin").css("height",(p2Height-5)+"px"); bufferBinEditor.resize(); } }); $(".red-ui-editor-type-buffer-type").on("click", function(e) { e.preventDefault(); RED.sidebar.help.set(RED._("bufferEditor.modeDesc")); }) }, close: function() { if (options.onclose) { options.onclose(); } bufferStringEditor.destroy(); bufferBinEditor.destroy(); }, show: function() {} } RED.tray.show(trayOptions); } } RED.editor.registerTypeEditor("_buffer", definition); })();