/**
* 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 = $("#red-ui-library-dialog-save-filename").val().trim()
var selectedPath = saveLibraryBrowser.getSelected();
if (!selectedPath.children) {
selectedPath = selectedPath.parent;
}
var queryArgs = [];
var data = {};
for (var i=0; i < activeLibrary.fields.length; i++) {
var field = activeLibrary.fields[i];
if (field === "name") {
data.name = name;
} else if (typeof(field) === 'object') {
data[field.name] = field.get();
} else {
data[field] = $("#" + elementPrefix + field).val();
}
}
data.text = activeLibrary.editor.getValue();
var saveFlow = function() {
$.ajax({
url:"library/"+selectedPath.library+'/'+selectedPath.type+'/'+selectedPath.path + filename,
type: "POST",
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8"
}).done(function(data,textStatus,xhr) {
RED.notify(RED._("library.savedType", {type:activeLibrary.type}),"success");
}).fail(function(xhr,textStatus,err) {
if (xhr.status === 401) {
RED.notify(RED._("library.saveFailed",{message:RED._("user.notAuthorized")}),"error");
} else {
RED.notify(RED._("library.saveFailed",{message:xhr.responseText}),"error");
}
});
}
if (selectedPath.children) {
var exists = false;
selectedPath.children.forEach(function(f) {
if (f.label === filename) {
exists = true;
}
});
if (exists) {
$( "#red-ui-library-dialog-save" ).dialog("close");
var notification = RED.notify(RED._("clipboard.export.exists",{file:RED.utils.sanitize(filename)}),{
type: "warning",
fixed: true,
buttons: [{
text: RED._("common.label.cancel"),
click: function() {
notification.hideNotification()
$( "#red-ui-library-dialog-save" ).dialog( "open" );
}
},{
text: RED._("clipboard.export.overwrite"),
click: function() {
notification.hideNotification()
saveFlow();
}
}]
});
} else {
saveFlow();
}
} else {
saveFlow();
}
}
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(done, item) {
loadLibraryFolder(library,type,root+d+"/", function(children) {
item.children = children; // TODO: should this be done by treeList for us
done(children);
})
}
};
} else {
return {
library: library,
type: type,
icon: 'fa fa-file-o',
label: d.fn,
path: root+d.fn,
props: d
};
}
});
items.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);
}
});
done(items);
});
}
var validateExportFilenameTimeout;
function validateExportFilename(filenameInput) {
if (validateExportFilenameTimeout) {
clearTimeout(validateExportFilenameTimeout);
}
validateExportFilenameTimeout = setTimeout(function() {
var filename = filenameInput.val().trim();
var valid = filename.length > 0 && !/[\/\\]/.test(filename);
if (valid) {
filenameInput.removeClass("input-error");
$("#red-ui-library-dialog-save-button").button("enable");
} else {
filenameInput.addClass("input-error");
$("#red-ui-library-dialog-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(
'