You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

368 lines
15KB

  1. /**
  2. * Copyright 2013 IBM Corp.
  3. *
  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
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  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.
  15. **/
  16. RED.library = (function() {
  17. function loadFlowLibrary() {
  18. $.getJSON("library/flows",function(data) {
  19. //console.log(data);
  20. var buildMenu = function(data,root) {
  21. var i;
  22. var li;
  23. var a;
  24. var ul = document.createElement("ul");
  25. ul.className = "dropdown-menu";
  26. if (data.d) {
  27. for (i in data.d) {
  28. if (data.d.hasOwnProperty(i)) {
  29. li = document.createElement("li");
  30. li.className = "dropdown-submenu pull-left";
  31. a = document.createElement("a");
  32. a.href="#";
  33. a.innerHTML = i;
  34. li.appendChild(a);
  35. li.appendChild(buildMenu(data.d[i],root+(root!==""?"/":"")+i));
  36. ul.appendChild(li);
  37. }
  38. }
  39. }
  40. if (data.f) {
  41. for (i in data.f) {
  42. if (data.f.hasOwnProperty(i)) {
  43. li = document.createElement("li");
  44. a = document.createElement("a");
  45. a.href="#";
  46. a.innerHTML = data.f[i];
  47. a.flowName = root+(root!==""?"/":"")+data.f[i];
  48. a.onclick = function() {
  49. $.get('library/flows/'+this.flowName, function(data) {
  50. RED.view.importNodes(data);
  51. });
  52. };
  53. li.appendChild(a);
  54. ul.appendChild(li);
  55. }
  56. }
  57. }
  58. return ul;
  59. };
  60. var menu = buildMenu(data,"");
  61. $("#flow-menu-parent>ul").replaceWith(menu);
  62. });
  63. }
  64. loadFlowLibrary();
  65. function createUI(options) {
  66. var libraryData = {};
  67. var selectedLibraryItem = null;
  68. var libraryEditor = null;
  69. function buildFileListItem(item) {
  70. var li = document.createElement("li");
  71. li.onmouseover = function(e) { $(this).addClass("list-hover"); };
  72. li.onmouseout = function(e) { $(this).removeClass("list-hover"); };
  73. return li;
  74. }
  75. function buildFileList(root,data) {
  76. var ul = document.createElement("ul");
  77. var li;
  78. for (var i=0;i<data.length;i++) {
  79. var v = data[i];
  80. if (typeof v === "string") {
  81. // directory
  82. li = buildFileListItem(v);
  83. li.onclick = (function () {
  84. var dirName = v;
  85. return function(e) {
  86. var bcli = $('<li class="active"><span class="divider">/</span> <a href="#">'+dirName+'</a></li>');
  87. $("a",bcli).click(function(e) {
  88. $(this).parent().nextAll().remove();
  89. $.getJSON("library/"+options.url+root+dirName,function(data) {
  90. $("#node-select-library").children().first().replaceWith(buildFileList(root+dirName+"/",data));
  91. });
  92. e.stopPropagation();
  93. });
  94. var bc = $("#node-dialog-library-breadcrumbs");
  95. $(".active",bc).removeClass("active");
  96. bc.append(bcli);
  97. $.getJSON("library/"+options.url+root+dirName,function(data) {
  98. $("#node-select-library").children().first().replaceWith(buildFileList(root+dirName+"/",data));
  99. });
  100. }
  101. })();
  102. li.innerHTML = '<i class="icon-folder-close"></i> '+v+"</i>";
  103. ul.appendChild(li);
  104. } else {
  105. // file
  106. li = buildFileListItem(v);
  107. li.innerHTML = v.name;
  108. li.onclick = (function() {
  109. var item = v;
  110. return function(e) {
  111. $(".list-selected",ul).removeClass("list-selected");
  112. $(this).addClass("list-selected");
  113. $.get("library/"+options.url+root+item.fn, function(data) {
  114. console.log(data);
  115. selectedLibraryItem = item;
  116. libraryEditor.setText(data);
  117. });
  118. }
  119. })();
  120. ul.appendChild(li);
  121. }
  122. }
  123. return ul;
  124. }
  125. $('#node-input-name').addClass('input-append-left').css("width","65%").after(
  126. '<div class="btn-group" style="margin-left: -5px;">'+
  127. '<button id="node-input-'+options.type+'-lookup" class="btn input-append-right" data-toggle="dropdown"><i class="icon-book"></i> <span class="caret"></span></button>'+
  128. '<ul class="dropdown-menu pull-right" role="menu">'+
  129. '<li><a id="node-input-'+options.type+'-menu-open-library" tabindex="-1" href="#">Open Library...</a></li>'+
  130. '<li><a id="node-input-'+options.type+'-menu-save-library" tabindex="-1" href="#">Save to Library...</a></li>'+
  131. '</ul></div>'
  132. );
  133. $('#node-input-'+options.type+'-menu-open-library').click(function(e) {
  134. $("#node-select-library").children().remove();
  135. var bc = $("#node-dialog-library-breadcrumbs");
  136. bc.children().first().nextAll().remove();
  137. libraryEditor.setText('');
  138. $.getJSON("library/"+options.url,function(data) {
  139. $("#node-select-library").append(buildFileList("/",data));
  140. $("#node-dialog-library-breadcrumbs a").click(function(e) {
  141. $(this).parent().nextAll().remove();
  142. $("#node-select-library").children().first().replaceWith(buildFileList("/",data));
  143. e.stopPropagation();
  144. });
  145. $( "#node-dialog-library-lookup" ).dialog( "open" );
  146. });
  147. e.preventDefault();
  148. });
  149. $('#node-input-'+options.type+'-menu-save-library').click(function(e) {
  150. //var found = false;
  151. var name = $("#node-input-name").val().replace(/(^\s*)|(\s*$)/g,"");
  152. //var buildPathList = function(data,root) {
  153. // var paths = [];
  154. // if (data.d) {
  155. // for (var i in data.d) {
  156. // var dn = root+(root==""?"":"/")+i;
  157. // var d = {
  158. // label:dn,
  159. // files:[]
  160. // };
  161. // for (var f in data.d[i].f) {
  162. // d.files.push(data.d[i].f[f].fn.split("/").slice(-1)[0]);
  163. // }
  164. // paths.push(d);
  165. // paths = paths.concat(buildPathList(data.d[i],root+(root==""?"":"/")+i));
  166. // }
  167. // }
  168. // return paths;
  169. //};
  170. $("#node-dialog-library-save-folder").attr("value","");
  171. var filename = name.replace(/[^\w-]/g,"-");
  172. if (filename === "") {
  173. filename = "unnamed-"+options.type;
  174. }
  175. $("#node-dialog-library-save-filename").attr("value",filename+".js");
  176. //var paths = buildPathList(libraryData,"");
  177. //$("#node-dialog-library-save-folder").autocomplete({
  178. // minLength: 0,
  179. // source: paths,
  180. // select: function( event, ui ) {
  181. // $("#node-dialog-library-save-filename").autocomplete({
  182. // minLength: 0,
  183. // source: ui.item.files
  184. // });
  185. // }
  186. //});
  187. $( "#node-dialog-library-save" ).dialog( "open" );
  188. e.preventDefault();
  189. });
  190. require(["orion/editor/edit"], function(edit) {
  191. libraryEditor = edit({
  192. parent:document.getElementById('node-select-library-text'),
  193. lang:"js",
  194. readonly: true
  195. });
  196. });
  197. $( "#node-dialog-library-lookup" ).dialog({
  198. title: options.type+" library",
  199. modal: true,
  200. autoOpen: false,
  201. width: 800,
  202. height: 450,
  203. buttons: [
  204. {
  205. text: "Ok",
  206. click: function() {
  207. if (selectedLibraryItem) {
  208. for (var i=0;i<options.fields.length;i++) {
  209. var field = options.fields[i];
  210. $("#node-input-"+field).val(selectedLibraryItem[field]);
  211. }
  212. options.editor.setText(libraryEditor.getText());
  213. }
  214. $( this ).dialog( "close" );
  215. }
  216. },
  217. {
  218. text: "Cancel",
  219. click: function() {
  220. $( this ).dialog( "close" );
  221. }
  222. }
  223. ],
  224. open: function(e) {
  225. var form = $("form",this);
  226. form.height(form.parent().height()-30);
  227. $("#node-select-library-text").height("100%");
  228. $(".form-row:last-child",form).children().height(form.height()-60);
  229. },
  230. resize: function(e) {
  231. var form = $("form",this);
  232. form.height(form.parent().height()-30);
  233. $(".form-row:last-child",form).children().height(form.height()-60);
  234. }
  235. });
  236. function saveToLibrary(overwrite) {
  237. var name = $("#node-input-name").val().replace(/(^\s*)|(\s*$)/g,"");
  238. if (name === "") {
  239. name = "Unnamed "+options.type;
  240. }
  241. var filename = $("#node-dialog-library-save-filename").val().replace(/(^\s*)|(\s*$)/g,"");
  242. var pathname = $("#node-dialog-library-save-folder").val().replace(/(^\s*)|(\s*$)/g,"");
  243. if (filename === "" || !/.+\.js$/.test(filename)) {
  244. RED.notify("Invalid filename","warning");
  245. return;
  246. }
  247. var fullpath = pathname+(pathname===""?"":"/")+filename;
  248. if (!overwrite) {
  249. //var pathnameParts = pathname.split("/");
  250. //var exists = false;
  251. //var ds = libraryData;
  252. //for (var pnp in pathnameParts) {
  253. // if (ds.d && pathnameParts[pnp] in ds.d) {
  254. // ds = ds.d[pathnameParts[pnp]];
  255. // } else {
  256. // ds = null;
  257. // break;
  258. // }
  259. //}
  260. //if (ds && ds.f) {
  261. // for (var f in ds.f) {
  262. // if (ds.f[f].fn == fullpath) {
  263. // exists = true;
  264. // break;
  265. // }
  266. // }
  267. //}
  268. //if (exists) {
  269. // $("#node-dialog-library-save-type").html(options.type);
  270. // $("#node-dialog-library-save-name").html(fullpath);
  271. // $("#node-dialog-library-save-confirm").dialog( "open" );
  272. // return;
  273. //}
  274. }
  275. var queryArgs = [];
  276. for (var i=0;i<options.fields.length;i++) {
  277. var field = options.fields[i];
  278. if (field == "name") {
  279. queryArgs.push("name="+encodeURIComponent(name));
  280. } else {
  281. queryArgs.push(encodeURIComponent(field)+"="+encodeURIComponent($("#node-input-"+field).val()));
  282. }
  283. }
  284. var queryString = queryArgs.join("&");
  285. var text = options.editor.getText();
  286. $.post("library/"+options.url+'/'+fullpath+"?"+queryString,text,function() {
  287. RED.notify("Saved "+options.type,"success");
  288. });
  289. }
  290. $( "#node-dialog-library-save-confirm" ).dialog({
  291. title: "Save to library",
  292. modal: true,
  293. autoOpen: false,
  294. width: 530,
  295. height: 230,
  296. buttons: [
  297. {
  298. text: "Ok",
  299. click: function() {
  300. saveToLibrary(true);
  301. $( this ).dialog( "close" );
  302. }
  303. },
  304. {
  305. text: "Cancel",
  306. click: function() {
  307. $( this ).dialog( "close" );
  308. }
  309. }
  310. ]
  311. });
  312. $( "#node-dialog-library-save" ).dialog({
  313. title: "Save to library",
  314. modal: true,
  315. autoOpen: false,
  316. width: 530,
  317. height: 230,
  318. buttons: [
  319. {
  320. text: "Ok",
  321. click: function() {
  322. saveToLibrary(false);
  323. $( this ).dialog( "close" );
  324. }
  325. },
  326. {
  327. text: "Cancel",
  328. click: function() {
  329. $( this ).dialog( "close" );
  330. }
  331. }
  332. ]
  333. });
  334. }
  335. return {
  336. create: createUI,
  337. loadFlowLibrary: loadFlowLibrary
  338. }
  339. })();