UNPKG

8.48 kBJavaScriptView Raw
1var tcServes = null;
2var tcSelectedServe = null;
3
4function initServe(){
5
6 tcServes = new TableCreator();
7 tcServes.init({ elementId: "ServesDiv",
8 clickable: true,
9 //linesPerPage: 20,
10 showRecordsPerPageSelector: false,
11 showFieldsSelector: false,
12 orderByColumn: 0,
13 orderASC: true,
14 columns: [
15 {title: "Namespace", dataKey: "namespace"},
16 {title: "Name", dataKey: "name"},
17 {title: "Title", dataKey: "title"},
18 {title: "Arguments", dataKey: "arguments", width: "270px"},
19 {title: "Description", dataKey: "description"},
20 {title: "Return type", dataKey: "returntype"},
21 {title: "Default", dataKey: "default"}
22 ],
23 dataSource: async function(onData){
24 let response = await req('get-serve');
25 let serve = await response.json()
26
27 let nsFilter = $("#serve-namespace-select").val();
28 refreshServeNamespaceFilter(serve);
29
30 if(nsFilter && nsFilter != 'all'){
31 serve = serve.filter((s) => s.namespace == nsFilter)
32 }
33
34 for(let f of serve){
35 f.arguments = formatFunctionArgsAsString(f.args);
36 }
37 onData(serve);
38 },
39 createRecord: {
40 fields: [
41 {name: "name", title: "Name"},
42 {name: "title", title: "Title", placeholder: "optional"},
43 {name: "description", title: "Description", type: "textarea"},
44 {name: "returntype", title: "Return type", type: "select", values: dataTypesForSelect},
45 {name: "namespace", title: "Namespace"},
46 {name: "default", title: "Default for", type: "select", values: httpOperationsForSelect}
47 ],
48 validate: function(record){return record.name !== ""},
49 onCreate: async function(record, cb){
50 await req("add-serve", record);
51 cb();
52 }
53 },
54 deleteRecord: {
55 onDelete: async function(record, cb){await req("remove-serve", record);cb();}
56 },
57 editRecord: {
58 fields: [
59 {name: "name", title: "Name"},
60 {name: "title", title: "Title", placeholder: "optional"},
61 {name: "description", title: "Description", type: "textarea"},
62 {name: "returntype", title: "Return type", type: "select", values: dataTypesForSelect},
63 {name: "namespace", title: "Namespace"},
64 {name: "default", title: "Default for", type: "select", values: httpOperationsForSelect}
65 ],
66 validate: function(oldRecord, newRecord){return newRecord.name !== "";},
67 onEdit: async function(oldRecord, newRecord, cb){
68 newRecord.oldName = oldRecord.name;
69 newRecord.oldNamespace = oldRecord.namespace;
70 await req("update-serve", newRecord);
71 cb();
72 }
73 },
74 onClick: function(record){
75 showServe(record)
76 },
77 recordRightClickMenu: {actions: [
78 {title: "Call function", onClick: async function(a, r, cb){
79 let strWindowFeatures = "location=yes,height=570,width=520,scrollbars=yes,status=yes";
80 let URL = `/mscp/apibrowser?setup=1110000;${r.name}`;
81 let win = window.open(URL, "_blank", strWindowFeatures);
82 }}
83 ]}
84 })
85 tcServes.draw();
86
87 tcSelectedServe = new TableCreator();
88 tcSelectedServe.init(
89 {
90 elementId: "CurServeArgs",
91 showRecordsPerPageSelector: false,
92 showFieldsSelector: false,
93 columns: [
94 {title: "Name", dataKey: "name"},
95 {title: "Title", dataKey: "title"},
96 {title: "Type", dataKey: "type"},
97 {title: "Required", dataKey: "required"},
98 {title: "Description", dataKey: "description"}
99 ],
100 dataSource: async function(onData){
101 if(curSelectedServe != null){
102 let response = await req('get-serve-arguments', curSelectedServe);
103 let args = await response.json()
104 for(let i = 0; i < args.length; i++){
105 if(typeof args[i] === "string"){
106 args[i] = {name: args[i]};
107 }
108 args[i].required = (typeof args[i].required === "boolean") ? args[i].required : true;
109 args[i].type = (typeof args[i].type === "string") ? args[i].type : "*";
110 }
111 onData(args);
112 } else {
113 onData([]);
114 }
115 },
116 createRecord: {
117 fields: [
118 {name: "name", title: "Name"},
119 {name: "title", title: "Title", placeholder: "optional"},
120 {name: "type", title: "Type", type: "select", values: dataTypesForSelect},
121 {name: "required", title: "Required", type: "checkbox"},
122 {name: "description", title: "Description", type: "textarea"}
123 ],
124 validate: function(record){return record.name !== ""},
125 onCreate: async function(record, cb){
126 await req("add-serve-argument", {servename: curSelectedServe.name, servenamespace: curSelectedServe.namespace, record: record});
127 cb();
128 refreshServe();
129 }
130 },
131 deleteRecord: {
132 onDelete: async function(record, cb){await req("remove-serve-argument", {servename: curSelectedServe.name, servenamespace: curSelectedServe.namespace, record: record});cb();refreshServe();}
133 },
134 editRecord: {
135 fields: [
136 {name: "name", title: "Name"},
137 {name: "title", title: "Title", placeholder: "optional"},
138 {name: "type", title: "Type", type: "select", values: dataTypesForSelect},
139 {name: "required", title: "Required", type: "checkbox"},
140 {name: "description", title: "Description", type: "textarea"}
141 ],
142 validate: function(oldRecord, newRecord){return newRecord.name !== "";},
143 onEdit: async function(oldRecord, newRecord, cb){
144 newRecord.oldName = oldRecord.name;
145 await req("update-serve-argument", {servename: curSelectedServe.name, servenamespace: curSelectedServe.namespace, record: newRecord});
146 cb();
147 refreshServe();
148 }
149 }
150 })
151 tcSelectedServe.draw();
152
153 var curSelectedServe = null;
154 var showServe = function(serve){
155 $("#servecurrentfunctionname").html((serve.namespace?serve.namespace+".":"") + serve.name)
156 $("#selectedservecontainer").fadeIn();
157 curSelectedServe = serve;
158 tcSelectedServe.reloadData();
159 }
160
161 $("#GenHandlerBtn").click(async function(){
162 let response = await req('get-serve');
163 let serve = await response.json()
164
165 var handlerCode = "\"use strict\"\r\n\r\nclass Handler{\r\n"
166 + "\r\n async init(){\r\n // Initialize handler if necessary. Is called on every request\r\n }\r\n"
167 + "\r\n async initFirst(){\r\n // Initialize handler if necessary. Is called only once (before any requests).\r\n }\r\n";
168
169 for(let s of serve){
170 handlerCode += "\r\n async " + s.name + "("
171
172 let args = []
173 for(let a of (s.args || [])){
174 args.push(typeof a === "string" ? a : a.name)
175 }
176
177 handlerCode += args.join(', ')
178 handlerCode += "){\r\n return null\r\n }\r\n"
179 }
180
181 handlerCode += "}\r\n\r\nmodule.exports = Handler"
182
183 $("#generatedhandler").html(handlerCode)
184 })
185
186 $("#serve-namespace-select").on("change", () => {
187 tcServes.reloadData();
188 })
189}
190
191$(function() {
192 initServe();
193});
194
195function refreshServe(){
196 tcServes.reloadData();
197 tcSelectedServe.reloadData();
198}
199
200function refreshServeNamespaceFilter(serve){
201 if($("#serve-namespace-select option[value=all]").length < 1)
202 $("#serve-namespace-select").append(`<option value="all">All</option>`)
203
204 let namespacesAdded = new Set()
205 for(let s of serve.sort((a, b) => a.namespace < b.namespace ? -1 : 1).filter((v) => v.namespace ? true : false)){
206 if(!namespacesAdded.has(s.namespace) && $(`#serve-namespace-select option[value=${s.namespace}]`).length < 1){
207 $("#serve-namespace-select").append(`<option value="${s.namespace}">${s.namespace}</option>`)
208 namespacesAdded.add(s.namespace);
209 }
210 }
211}