Update runtime apis to support multiple libraries

This commit is contained in:
Nick O'Leary
2019-04-25 11:32:09 +01:00
parent 5e43a02cd3
commit b581e33611
15 changed files with 553 additions and 713 deletions

View File

@@ -78,7 +78,7 @@ RED.clipboard = (function() {
var filename = $("#clipboard-dialog-tab-library-name").val().trim();
var saveFlow = function() {
$.ajax({
url:'library/flows/'+selectedPath.path + filename,
url:'library/'+selectedPath.library+'/'+selectedPath.type+'/'+selectedPath.path + filename,
type: "POST",
data: flowToExport,
contentType: "application/json; charset=utf-8"
@@ -139,7 +139,7 @@ RED.clipboard = (function() {
} else {
var selectedPath = libraryBrowser.getSelected();
if (selectedPath.path) {
$.get('library/flows/'+selectedPath.path, function(data) {
$.get('library/'+selectedPath.library+'/'+selectedPath.type+'/'+selectedPath.path, function(data) {
RED.view.importNodes(data,addNewFlow);
});
}
@@ -609,59 +609,39 @@ RED.clipboard = (function() {
$("#clipboard-dialog-download").show();
}
function transformFlowList(list,label,root,includeExamples) {
var result = {
icon: root===""?"fa fa-archive":'fa fa-folder',
label: label,
path: root
};
result.children = [];
if (list.f) {
list.f.forEach(function(f) {
result.children.push({
icon: 'fa fa-file-o',
label: f,
path: root+f
});
});
}
if (list.d) {
for (var l in list.d) {
if (list.d.hasOwnProperty(l)) {
if (root+l !== "_examples_") {
result.children.push(transformFlowList(list.d[l], l,root+l+"/",includeExamples))
} else if (includeExamples) {
result._examples = transformFlowList(list.d[l], l,root+l+"/",includeExamples)
}
}
}
}
result.children.sort(function(A,B){
if (A.children && !B.children) {
return -1;
} else if (!A.children && B.children) {
return 1;
} else {
return A.label.localeCompare(B.label);
}
});
return result;
}
function loadFlowLibrary(browser,includeExamples) {
$.getJSON("library/flows", function(data) {
var listing = [transformFlowList(data,RED._("library.types.local"),"",includeExamples)];
listing[0].expanded = true;
if (includeExamples && listing[0]._examples) {
var examples = listing[0]._examples;
delete listing[0]._examples;
examples.label = RED._("library.types.examples");
examples.icon = "fa fa-archive";
listing.unshift(examples)
var listing = [];
if (includeExamples) {
listing.push({
library: "_examples_",
type: "flows",
icon: 'fa fa-hdd-o',
label: RED._("library.types.examples"),
path: "",
children: function(item,done) {
RED.library.loadLibraryFolder("_examples_","flows","",function(children) {
item.children = children;
done(children);
})
}
})
}
listing.push({
library: "local",
type: "flows",
icon: 'fa fa-hdd-o',
label: RED._("library.types.local"),
path: "",
expanded: true,
children: function(item,done) {
RED.library.loadLibraryFolder("local","flows","",function(children) {
item.children = children;
done(children);
})
}
browser.data(listing);
});
})
browser.data(listing);
}
function hideDropTarget() {

View File

@@ -58,7 +58,6 @@ RED.library = (function() {
}
var filename = $("#node-dialog-library-save-filename").val().trim()
var selectedPath = saveLibraryBrowser.getSelected();
console.log(selectedPath);
if (!selectedPath.children) {
selectedPath = selectedPath.parent;
}
@@ -75,7 +74,7 @@ RED.library = (function() {
data.text = activeLibrary.editor.getValue();
var saveFlow = function() {
$.ajax({
url:"library/"+activeLibrary.url+'/'+selectedPath.path + filename,
url:"library/"+selectedPath.library+'/'+selectedPath.type+'/'+selectedPath.path + filename,
type: "POST",
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8"
@@ -89,8 +88,6 @@ RED.library = (function() {
}
});
}
console.log(filename);
console.log(selectedPath);
if (selectedPath.children) {
var exists = false;
selectedPath.children.forEach(function(f) {
@@ -125,16 +122,18 @@ RED.library = (function() {
}
}
function loadLibraryFolder(url,root,done) {
$.getJSON("library/"+url+"/"+root,function(data) {
function loadLibraryFolder(library,type,root,done) {
$.getJSON("library/"+library+"/"+type+"/"+root,function(data) {
var items = data.map(function(d) {
if (typeof d === "string") {
return {
library: library,
type: type,
icon: 'fa fa-folder',
label: d,
path: root+d+"/",
children: function(item,done) {
loadLibraryFolder(url,root+d+"/", function(children) {
loadLibraryFolder(library,type,root+d+"/", function(children) {
item.children = children; // TODO: should this be done by treeList for us
done(children);
})
@@ -142,6 +141,8 @@ RED.library = (function() {
};
} else {
return {
library: library,
type: type,
icon: 'fa fa-file-o',
label: d.fn,
path: root+d.fn,
@@ -209,9 +210,11 @@ RED.library = (function() {
$('#node-input-'+options.type+'-menu-open-library').click(function(e) {
activeLibrary = options;
loadLibraryFolder(options.url, "", function(items) {
loadLibraryFolder("local",options.url, "", function(items) {
var listing = [{
icon: 'fa fa-archive',
library: "local",
type: options.url,
icon: 'fa fa-hdd-o',
label: RED._("library.types.local"),
path: "",
expanded: true,
@@ -255,7 +258,7 @@ RED.library = (function() {
}
$("#node-dialog-library-save-filename").attr("value",filename+".js");
loadLibraryFolder(options.url, "", function(items) {
loadLibraryFolder("local",options.url, "", function(items) {
var listing = [{
icon: 'fa fa-archive',
label: RED._("library.types.local"),
@@ -483,7 +486,7 @@ RED.library = (function() {
var table = $("#node-dialog-library-load-preview-details-table").empty();
selectedLibraryItem = file.props;
if (file && file.label && !file.children) {
$.get("library/"+activeLibrary.url+"/"+file.path, function(data) {
$.get("library/"+file.library+"/"+file.type+"/"+file.path, function(data) {
//TODO: nls + sanitize
var propRow = $('<tr class="node-info-node-row"><td>Type</td><td></td></tr>').appendTo(table);
$(propRow.children()[1]).text(activeLibrary.type);
@@ -516,6 +519,7 @@ RED.library = (function() {
},
create: createUI,
createBrowser:createBrowser,
export: exportFlow
export: exportFlow,
loadLibraryFolder: loadLibraryFolder
}
})();