2 * Copyright 2013 IBM Corp.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 RED.library = (function() {
19 function loadFlowLibrary() {
20 $.getJSON("library/flows",function(data) {
23 var buildMenu = function(data,root) {
27 var ul = document.createElement("ul");
28 ul.id = "btn-import-library-submenu";
29 ul.className = "dropdown-menu";
32 if (data.d.hasOwnProperty(i)) {
33 li = document.createElement("li");
34 li.className = "dropdown-submenu pull-left";
35 a = document.createElement("a");
39 li.appendChild(buildMenu(data.d[i],root+(root!==""?"/":"")+i));
46 if (data.f.hasOwnProperty(i)) {
47 li = document.createElement("li");
48 a = document.createElement("a");
50 a.innerHTML = data.f[i];
51 a.flowName = root+(root!==""?"/":"")+data.f[i];
52 a.onclick = function() {
53 $.get('library/flows/'+this.flowName, function(data) {
54 RED.view.importNodes(data);
64 var menu = buildMenu(data,"");
65 //TODO: need an api in RED.menu for this
66 $("#btn-import-library-submenu").replaceWith(menu);
73 function createUI(options) {
75 var selectedLibraryItem = null;
76 var libraryEditor = null;
78 function buildFileListItem(item) {
79 var li = document.createElement("li");
80 li.onmouseover = function(e) { $(this).addClass("list-hover"); };
81 li.onmouseout = function(e) { $(this).removeClass("list-hover"); };
85 function buildFileList(root,data) {
86 var ul = document.createElement("ul");
88 for (var i=0;i<data.length;i++) {
90 if (typeof v === "string") {
92 li = buildFileListItem(v);
93 li.onclick = (function () {
96 var bcli = $('<li class="active"><span class="divider">/</span> <a href="#">'+dirName+'</a></li>');
97 $("a",bcli).click(function(e) {
98 $(this).parent().nextAll().remove();
99 $.getJSON("library/"+options.url+root+dirName,function(data) {
100 $("#node-select-library").children().first().replaceWith(buildFileList(root+dirName+"/",data));
104 var bc = $("#node-dialog-library-breadcrumbs");
105 $(".active",bc).removeClass("active");
107 $.getJSON("library/"+options.url+root+dirName,function(data) {
108 $("#node-select-library").children().first().replaceWith(buildFileList(root+dirName+"/",data));
112 li.innerHTML = '<i class="fa fa-folder"></i> '+v+"</i>";
116 li = buildFileListItem(v);
117 li.innerHTML = v.name;
118 li.onclick = (function() {
121 $(".list-selected",ul).removeClass("list-selected");
122 $(this).addClass("list-selected");
123 $.get("library/"+options.url+root+item.fn, function(data) {
124 selectedLibraryItem = item;
125 libraryEditor.setText(data);
136 //Commented this portion as is not used by the DGBuilder application
137 $('#node-input-name').addClass('input-append-left').css("width","65%").after(
138 '<div class="btn-group" style="margin-left: 0px;">'+
139 '<button id="node-input-'+options.type+'-lookup" class="btn input-append-right" data-toggle="dropdown"><i class="fa fa-book"></i> <i class="fa fa-caret-down"></i></button>'+
140 '<ul class="dropdown-menu pull-right" role="menu">'+
141 '<li><a id="node-input-'+options.type+'-menu-open-library" tabindex="-1" href="#">Open Library...</a></li>'+
142 '<li><a id="node-input-'+options.type+'-menu-save-library" tabindex="-1" href="#">Save to Library...</a></li>'+
148 $('#node-input-'+options.type+'-menu-open-library').click(function(e) {
149 $("#node-select-library").children().remove();
150 var bc = $("#node-dialog-library-breadcrumbs");
151 bc.children().first().nextAll().remove();
152 libraryEditor.setText('');
154 $.getJSON("library/"+options.url,function(data) {
155 $("#node-select-library").append(buildFileList("/",data));
156 $("#node-dialog-library-breadcrumbs a").click(function(e) {
157 $(this).parent().nextAll().remove();
158 $("#node-select-library").children().first().replaceWith(buildFileList("/",data));
161 $( "#node-dialog-library-lookup" ).dialog( "open" );
167 $('#node-input-'+options.type+'-menu-save-library').click(function(e) {
169 var name = $("#node-input-name").val().replace(/(^\s*)|(\s*$)/g,"");
171 //var buildPathList = function(data,root) {
174 // for (var i in data.d) {
175 // var dn = root+(root==""?"":"/")+i;
180 // for (var f in data.d[i].f) {
181 // d.files.push(data.d[i].f[f].fn.split("/").slice(-1)[0]);
184 // paths = paths.concat(buildPathList(data.d[i],root+(root==""?"":"/")+i));
189 $("#node-dialog-library-save-folder").attr("value","");
191 var filename = name.replace(/[^\w-]/g,"-");
192 if (filename === "") {
193 filename = "unnamed-"+options.type;
195 $("#node-dialog-library-save-filename").attr("value",filename+".js");
197 //var paths = buildPathList(libraryData,"");
198 //$("#node-dialog-library-save-folder").autocomplete({
201 // select: function( event, ui ) {
202 // $("#node-dialog-library-save-filename").autocomplete({
204 // source: ui.item.files
209 $( "#node-dialog-library-save" ).dialog( "open" );
212 require(["orion/editor/edit"], function(edit) {
213 libraryEditor = edit({
214 parent:document.getElementById('node-select-library-text'),
221 $( "#node-dialog-library-lookup" ).dialog({
222 title: options.type+" library",
231 if (selectedLibraryItem) {
232 for (var i=0;i<options.fields.length;i++) {
233 var field = options.fields[i];
234 $("#node-input-"+field).val(selectedLibraryItem[field]);
236 options.editor.setText(libraryEditor.getText());
238 $( this ).dialog( "close" );
244 $( this ).dialog( "close" );
249 var form = $("form",this);
250 form.height(form.parent().height()-30);
251 $("#node-select-library-text").height("100%");
252 $(".form-row:last-child",form).children().height(form.height()-60);
254 resize: function(e) {
255 var form = $("form",this);
256 form.height(form.parent().height()-30);
257 $(".form-row:last-child",form).children().height(form.height()-60);
262 function saveToLibrary(overwrite) {
263 var name = $("#node-input-name").val().replace(/(^\s*)|(\s*$)/g,"");
265 name = "Unnamed "+options.type;
267 var filename = $("#node-dialog-library-save-filename").val().replace(/(^\s*)|(\s*$)/g,"");
268 var pathname = $("#node-dialog-library-save-folder").val().replace(/(^\s*)|(\s*$)/g,"");
269 if (filename === "" || !/.+\.js$/.test(filename)) {
270 RED.notify("Invalid filename","warning");
273 var fullpath = pathname+(pathname===""?"":"/")+filename;
275 //var pathnameParts = pathname.split("/");
276 //var exists = false;
277 //var ds = libraryData;
278 //for (var pnp in pathnameParts) {
279 // if (ds.d && pathnameParts[pnp] in ds.d) {
280 // ds = ds.d[pathnameParts[pnp]];
287 // for (var f in ds.f) {
288 // if (ds.f[f].fn == fullpath) {
295 // $("#node-dialog-library-save-type").html(options.type);
296 // $("#node-dialog-library-save-name").html(fullpath);
297 // $("#node-dialog-library-save-confirm").dialog( "open" );
302 for (var i=0;i<options.fields.length;i++) {
303 var field = options.fields[i];
304 if (field == "name") {
305 queryArgs.push("name="+encodeURIComponent(name));
307 queryArgs.push(encodeURIComponent(field)+"="+encodeURIComponent($("#node-input-"+field).val()));
310 var queryString = queryArgs.join("&");
312 var text = options.editor.getText();
313 $.post("library/"+options.url+'/'+fullpath+"?"+queryString,text,function() {
314 RED.notify("Saved "+options.type,"success");
317 $( "#node-dialog-library-save-confirm" ).dialog({
318 title: "Save to library",
328 $( this ).dialog( "close" );
334 $( this ).dialog( "close" );
339 $( "#node-dialog-library-save" ).dialog({
340 title: "Save to library",
349 saveToLibrary(false);
350 $( this ).dialog( "close" );
356 $( this ).dialog( "close" );
366 loadFlowLibrary: loadFlowLibrary