Tidy editor code to pass jshint

This commit is contained in:
Nick O'Leary
2014-08-08 00:01:35 +01:00
parent fb2f307a26
commit 972e6fc6b3
16 changed files with 514 additions and 448 deletions

View File

@@ -13,35 +13,41 @@
* See the License for the specific language governing permissions and
* limitations under the License.
**/
RED.library = function() {
RED.library = (function() {
function loadFlowLibrary() {
$.getJSON("library/flows",function(data) {
//console.log(data);
//console.log(data);
var buildMenu = function(data,root) {
var ul = document.createElement("ul");
ul.className = "dropdown-menu";
if (data.d) {
for (var i in data.d) {
var li = document.createElement("li");
var buildMenu = function(data,root) {
var i;
var li;
var a;
var ul = document.createElement("ul");
ul.className = "dropdown-menu";
if (data.d) {
for (i in data.d) {
if (data.d.hasOwnProperty(i)) {
li = document.createElement("li");
li.className = "dropdown-submenu pull-left";
var a = document.createElement("a");
a = document.createElement("a");
a.href="#";
a.innerHTML = i;
li.appendChild(a);
li.appendChild(buildMenu(data.d[i],root+(root!=""?"/":"")+i));
li.appendChild(buildMenu(data.d[i],root+(root!==""?"/":"")+i));
ul.appendChild(li);
}
}
if (data.f) {
for (var i in data.f) {
var li = document.createElement("li");
var a = document.createElement("a");
}
if (data.f) {
for (i in data.f) {
if (data.f.hasOwnProperty(i)) {
li = document.createElement("li");
a = document.createElement("a");
a.href="#";
a.innerHTML = data.f[i];
a.flowName = root+(root!=""?"/":"")+data.f[i];
a.flowName = root+(root!==""?"/":"")+data.f[i];
a.onclick = function() {
$.get('library/flows/'+this.flowName, function(data) {
RED.view.importNodes(data);
@@ -51,10 +57,11 @@ RED.library = function() {
ul.appendChild(li);
}
}
return ul;
};
var menu = buildMenu(data,"");
$("#flow-menu-parent>ul").replaceWith(menu);
}
return ul;
};
var menu = buildMenu(data,"");
$("#flow-menu-parent>ul").replaceWith(menu);
});
}
loadFlowLibrary();
@@ -75,22 +82,22 @@ RED.library = function() {
function buildFileList(root,data) {
var ul = document.createElement("ul");
for (var i in data) {
var li;
for (var i=0;i<data.length;i++) {
var v = data[i];
if (typeof v === "string") {
// directory
var li = buildFileListItem(v);
li.onclick = function () {
li = buildFileListItem(v);
li.onclick = (function () {
var dirName = v;
return function(e) {
var bcli = $('<li class="active"><span class="divider">/</span> <a href="#">'+dirName+'</a></li>');
$("a",bcli).click(function(e) {
$(this).parent().nextAll().remove();
$.getJSON("library/"+options.url+root+dirName,function(data) {
$("#node-select-library").children().first().replaceWith(buildFileList(root+dirName+"/",data));
});
e.stopPropagation();
$(this).parent().nextAll().remove();
$.getJSON("library/"+options.url+root+dirName,function(data) {
$("#node-select-library").children().first().replaceWith(buildFileList(root+dirName+"/",data));
});
e.stopPropagation();
});
var bc = $("#node-dialog-library-breadcrumbs");
$(".active",bc).removeClass("active");
@@ -99,14 +106,14 @@ RED.library = function() {
$("#node-select-library").children().first().replaceWith(buildFileList(root+dirName+"/",data));
});
}
}();
})();
li.innerHTML = '<i class="icon-folder-close"></i> '+v+"</i>";
ul.appendChild(li);
} else {
// file
var li = buildFileListItem(v);
li = buildFileListItem(v);
li.innerHTML = v.name;
li.onclick = function() {
li.onclick = (function() {
var item = v;
return function(e) {
$(".list-selected",ul).removeClass("list-selected");
@@ -117,7 +124,7 @@ RED.library = function() {
libraryEditor.setText(data);
});
}
}();
})();
ul.appendChild(li);
}
}
@@ -136,132 +143,131 @@ RED.library = function() {
$('#node-input-'+options.type+'-menu-open-library').click(function(e) {
$("#node-select-library").children().remove();
var bc = $("#node-dialog-library-breadcrumbs");
bc.children().first().nextAll().remove();
libraryEditor.setText('');
$.getJSON("library/"+options.url,function(data) {
$("#node-select-library").append(buildFileList("/",data));
$("#node-dialog-library-breadcrumbs a").click(function(e) {
$(this).parent().nextAll().remove();
$("#node-select-library").children().first().replaceWith(buildFileList("/",data));
e.stopPropagation();
});
$( "#node-dialog-library-lookup" ).dialog( "open" );
$("#node-select-library").children().remove();
var bc = $("#node-dialog-library-breadcrumbs");
bc.children().first().nextAll().remove();
libraryEditor.setText('');
$.getJSON("library/"+options.url,function(data) {
$("#node-select-library").append(buildFileList("/",data));
$("#node-dialog-library-breadcrumbs a").click(function(e) {
$(this).parent().nextAll().remove();
$("#node-select-library").children().first().replaceWith(buildFileList("/",data));
e.stopPropagation();
});
e.preventDefault();
$( "#node-dialog-library-lookup" ).dialog( "open" );
});
e.preventDefault();
});
$('#node-input-'+options.type+'-menu-save-library').click(function(e) {
//var found = false;
var name = $("#node-input-name").val().replace(/(^\s*)|(\s*$)/g,"");
//var buildPathList = function(data,root) {
// var paths = [];
// if (data.d) {
// for (var i in data.d) {
// var dn = root+(root==""?"":"/")+i;
// var d = {
// label:dn,
// files:[]
// };
// for (var f in data.d[i].f) {
// d.files.push(data.d[i].f[f].fn.split("/").slice(-1)[0]);
// }
// paths.push(d);
// paths = paths.concat(buildPathList(data.d[i],root+(root==""?"":"/")+i));
// }
// }
// return paths;
//};
$("#node-dialog-library-save-folder").attr("value","");
var filename = name.replace(/[^\w-]/g,"-");
if (filename == "") {
filename = "unnamed-"+options.type;
}
$("#node-dialog-library-save-filename").attr("value",filename+".js");
//var paths = buildPathList(libraryData,"");
//$("#node-dialog-library-save-folder").autocomplete({
// minLength: 0,
// source: paths,
// select: function( event, ui ) {
// $("#node-dialog-library-save-filename").autocomplete({
// minLength: 0,
// source: ui.item.files
// });
// }
//});
$( "#node-dialog-library-save" ).dialog( "open" );
e.preventDefault();
//var found = false;
var name = $("#node-input-name").val().replace(/(^\s*)|(\s*$)/g,"");
//var buildPathList = function(data,root) {
// var paths = [];
// if (data.d) {
// for (var i in data.d) {
// var dn = root+(root==""?"":"/")+i;
// var d = {
// label:dn,
// files:[]
// };
// for (var f in data.d[i].f) {
// d.files.push(data.d[i].f[f].fn.split("/").slice(-1)[0]);
// }
// paths.push(d);
// paths = paths.concat(buildPathList(data.d[i],root+(root==""?"":"/")+i));
// }
// }
// return paths;
//};
$("#node-dialog-library-save-folder").attr("value","");
var filename = name.replace(/[^\w-]/g,"-");
if (filename === "") {
filename = "unnamed-"+options.type;
}
$("#node-dialog-library-save-filename").attr("value",filename+".js");
//var paths = buildPathList(libraryData,"");
//$("#node-dialog-library-save-folder").autocomplete({
// minLength: 0,
// source: paths,
// select: function( event, ui ) {
// $("#node-dialog-library-save-filename").autocomplete({
// minLength: 0,
// source: ui.item.files
// });
// }
//});
$( "#node-dialog-library-save" ).dialog( "open" );
e.preventDefault();
});
require(["orion/editor/edit"], function(edit) {
libraryEditor = edit({
parent:document.getElementById('node-select-library-text'),
lang:"js",
readonly: true
});
libraryEditor = edit({
parent:document.getElementById('node-select-library-text'),
lang:"js",
readonly: true
});
});
$( "#node-dialog-library-lookup" ).dialog({
title: options.type+" library",
modal: true,
autoOpen: false,
width: 800,
height: 450,
buttons: [
{
text: "Ok",
click: function() {
if (selectedLibraryItem) {
for (var i in options.fields) {
var field = options.fields[i];
$("#node-input-"+field).val(selectedLibraryItem[field]);
};
options.editor.setText(libraryEditor.getText());
title: options.type+" library",
modal: true,
autoOpen: false,
width: 800,
height: 450,
buttons: [
{
text: "Ok",
click: function() {
if (selectedLibraryItem) {
for (var i=0;i<options.fields.length;i++) {
var field = options.fields[i];
$("#node-input-"+field).val(selectedLibraryItem[field]);
}
$( this ).dialog( "close" );
}
},
{
text: "Cancel",
click: function() {
$( this ).dialog( "close" );
options.editor.setText(libraryEditor.getText());
}
$( this ).dialog( "close" );
}
],
open: function(e) {
var form = $("form",this);
form.height(form.parent().height()-30);
$("#node-select-library-text").height("100%");
$(".form-row:last-child",form).children().height(form.height()-60);
},
resize: function(e) {
var form = $("form",this);
form.height(form.parent().height()-30);
$(".form-row:last-child",form).children().height(form.height()-60);
{
text: "Cancel",
click: function() {
$( this ).dialog( "close" );
}
}
],
open: function(e) {
var form = $("form",this);
form.height(form.parent().height()-30);
$("#node-select-library-text").height("100%");
$(".form-row:last-child",form).children().height(form.height()-60);
},
resize: function(e) {
var form = $("form",this);
form.height(form.parent().height()-30);
$(".form-row:last-child",form).children().height(form.height()-60);
}
});
function saveToLibrary(overwrite) {
var name = $("#node-input-name").val().replace(/(^\s*)|(\s*$)/g,"");
if (name == "") {
if (name === "") {
name = "Unnamed "+options.type;
}
var filename = $("#node-dialog-library-save-filename").val().replace(/(^\s*)|(\s*$)/g,"");
var pathname = $("#node-dialog-library-save-folder").val().replace(/(^\s*)|(\s*$)/g,"");
if (filename == "" || !/.+\.js$/.test(filename)) {
if (filename === "" || !/.+\.js$/.test(filename)) {
RED.notify("Invalid filename","warning");
return;
}
var fullpath = pathname+(pathname==""?"":"/")+filename;
var fullpath = pathname+(pathname===""?"":"/")+filename;
if (!overwrite) {
//var pathnameParts = pathname.split("/");
//var exists = false;
@@ -290,7 +296,7 @@ RED.library = function() {
//}
}
var queryArgs = [];
for (var i in options.fields) {
for (var i=0;i<options.fields.length;i++) {
var field = options.fields[i];
if (field == "name") {
queryArgs.push("name="+encodeURIComponent(name));
@@ -306,48 +312,48 @@ RED.library = function() {
});
}
$( "#node-dialog-library-save-confirm" ).dialog({
title: "Save to library",
modal: true,
autoOpen: false,
width: 530,
height: 230,
buttons: [
{
text: "Ok",
click: function() {
saveToLibrary(true);
$( this ).dialog( "close" );
}
},
{
text: "Cancel",
click: function() {
$( this ).dialog( "close" );
}
title: "Save to library",
modal: true,
autoOpen: false,
width: 530,
height: 230,
buttons: [
{
text: "Ok",
click: function() {
saveToLibrary(true);
$( this ).dialog( "close" );
}
]
},
{
text: "Cancel",
click: function() {
$( this ).dialog( "close" );
}
}
]
});
$( "#node-dialog-library-save" ).dialog({
title: "Save to library",
modal: true,
autoOpen: false,
width: 530,
height: 230,
buttons: [
{
text: "Ok",
click: function() {
saveToLibrary(false);
$( this ).dialog( "close" );
}
},
{
text: "Cancel",
click: function() {
$( this ).dialog( "close" );
}
title: "Save to library",
modal: true,
autoOpen: false,
width: 530,
height: 230,
buttons: [
{
text: "Ok",
click: function() {
saveToLibrary(false);
$( this ).dialog( "close" );
}
]
},
{
text: "Cancel",
click: function() {
$( this ).dialog( "close" );
}
}
]
});
}
@@ -356,6 +362,6 @@ RED.library = function() {
create: createUI,
loadFlowLibrary: loadFlowLibrary
}
}();
})();