/** * 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. **/ RED.library = (function() { var loadLibraryBrowser; var saveLibraryBrowser; var libraryEditor; var activeLibrary; var _libraryLookup = '
'+ '
'+ '
'+ '
'+ '
'+ '
'+ '
'+ '
'+ '
'+ '
'+ '
'+ '
'+ '
'+ '
'+ '
'+ '
'+ '
' var _librarySave = '
'+ '
'+ '
'+ '
'+ '
'+ ''+ '
'+ '
'+ '
'+ '
' function saveToLibrary() { var elementPrefix = activeLibrary.elementPrefix || "node-input-"; var name = $("#"+elementPrefix+"name").val().trim(); if (name === "") { name = RED._("library.unnamedType",{type:activeLibrary.type}); } var filename = $("#node-dialog-library-save-filename").val().trim() var selectedPath = saveLibraryBrowser.getSelected(); if (!selectedPath.children) { selectedPath = selectedPath.parent; } var queryArgs = []; var data = {}; for (var i=0; i 0 && !/[\/\\]/.test(filename); if (valid) { filenameInput.removeClass("input-error"); $("#node-dialog-library-save-button").button("enable"); } else { filenameInput.addClass("input-error"); $("#node-dialog-library-save-button").button("disable"); } },100); } function createUI(options) { var libraryData = {}; var elementPrefix = options.elementPrefix || "node-input-"; // Orion editor has set/getText // ACE editor has set/getValue // normalise to set/getValue if (options.editor.setText) { // Orion doesn't like having pos passed in, so proxy the call to drop it options.editor.setValue = function(text,pos) { options.editor.setText.call(options.editor,text); } } if (options.editor.getText) { options.editor.getValue = options.editor.getText; } // Add the library button to the name in the edit dialog $('#'+elementPrefix+"name").css("width","calc(100% - 52px)").after( '
'+ ' '+ '
' ); $('#node-input-'+options.type+'-menu-open-library').on("click", function(e) { activeLibrary = options; loadLibraryFolder("local",options.url, "", function(items) { var listing = [{ library: "local", type: options.url, icon: 'fa fa-hdd-o', label: RED._("library.types.local"), path: "", expanded: true, writable: false, children: [{ icon: 'fa fa-cube', label: options.type, path: options.type+"/", expanded: true, children: items }] }] loadLibraryBrowser.data(listing); }); libraryEditor = ace.edit('node-dialog-library-load-preview-text',{ useWorker: false }); libraryEditor.setTheme("ace/theme/tomorrow"); if (options.mode) { libraryEditor.getSession().setMode(options.mode); } libraryEditor.setOptions({ readOnly: true, highlightActiveLine: false, highlightGutterLine: false }); libraryEditor.renderer.$cursorLayer.element.style.opacity=0; libraryEditor.$blockScrolling = Infinity; $( "#node-dialog-library-load" ).dialog("option","title",RED._("library.typeLibrary", {type:options.type})).dialog( "open" ); e.preventDefault(); }); $('#node-input-'+options.type+'-menu-save-library').on("click", function(e) { activeLibrary = options; //var found = false; var name = $("#"+elementPrefix+"name").val().replace(/(^\s*)|(\s*$)/g,""); var filename = name.replace(/[^\w-]/g,"-"); if (filename === "") { filename = "unnamed-"+options.type; } $("#node-dialog-library-save-filename").attr("value",filename+".js"); loadLibraryFolder("local",options.url, "", function(items) { var listing = [{ icon: 'fa fa-archive', label: RED._("library.types.local"), path: "", expanded: true, writable: false, children: [{ icon: 'fa fa-cube', label: options.type, path: options.type+"/", expanded: true, children: items }] }] saveLibraryBrowser.data(listing); }); $( "#node-dialog-library-save" ).dialog( "open" ); e.preventDefault(); }); } function exportFlow() { console.warn("Deprecated call to RED.library.export"); } var menuOptionMenu; function createBrowser(options) { var panes = $('
').appendTo(options.container); var dirList = $("
").css({width: "100%", height: "100%"}).appendTo(panes) .treeList({}).on('treelistselect', function(event, item) { if (options.onselect) { options.onselect(item); } }); var itemTools = $("
").css({position: "absolute",bottom:"6px",right:"8px"}); var menuButton = $('') .on("click", function(evt) { evt.preventDefault(); evt.stopPropagation(); var elementPos = menuButton.offset(); var menuOptionMenu = RED.menu.init({id:"red-ui-library-browser-menu", options: [ {id:"red-ui-library-browser-menu-addFolder",label:"New folder", onselect: function() { var defaultFolderName = "new-folder"; var defaultFolderNameMatches = {}; var selected = dirList.treeList('selected'); if (!selected.children) { selected = selected.parent; } var complete = function() { selected.children.forEach(function(c) { if (/^new-folder/.test(c.label)) { defaultFolderNameMatches[c.label] = true } }); var folderIndex = 2; while(defaultFolderNameMatches[defaultFolderName]) { defaultFolderName = "new-folder-"+(folderIndex++) } selected.treeList.expand(); var input = $('').val(defaultFolderName); var newItem = { icon: "fa fa-folder-o", children:[], path: selected.path, element: input } var confirmAdd = function() { var val = input.val().trim(); if (val === "") { cancelAdd(); return; } else { for (var i=0;iType').appendTo(table); $(propRow.children()[1]).text(activeLibrary.type); if (file.props.hasOwnProperty('name')) { propRow = $('Name'+file.props.name+'').appendTo(table); $(propRow.children()[1]).text(file.props.name); } for (var p in file.props) { if (file.props.hasOwnProperty(p) && p !== 'name' && p !== 'fn') { propRow = $('').appendTo(table); $(propRow.children()[0]).text(p); RED.utils.createObjectElement(file.props[p]).appendTo(propRow.children()[1]); } } libraryEditor.setValue(data,-1); }); } else { libraryEditor.setValue("",-1); } } }); RED.panels.create({ container:$("#node-dialog-library-load-panes"), dir: "horizontal" }); RED.panels.create({ container:$("#node-dialog-library-load-preview"), dir: "vertical" }); }, create: createUI, createBrowser:createBrowser, export: exportFlow, loadLibraryFolder: loadLibraryFolder } })();