describe.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. var Describe = function() {
  2. //20180730
  3. }
  4. Describe.splitValue = function(str) {
  5. if (str) {
  6. if (str.length > 0) {
  7. return str.split(',');
  8. }
  9. }
  10. return [];
  11. }
  12. Describe.joinValue = function(o, split) {
  13. var s = ',';
  14. if (split) {
  15. s = '' + split;
  16. }
  17. if (o) {
  18. if (toString.apply(o) === '[object Array]') {
  19. return o.join(s);
  20. }
  21. }
  22. return o;
  23. }
  24. Describe.doPost = function(address, m, data) {
  25. $('#url').html(address);
  26. if ((m.resultContentType) && m.resultContentType.indexOf('application/json') > -1) {
  27. $.ajax({
  28. url : address,
  29. type : 'POST',
  30. headers : {
  31. 'x-debugger' : true
  32. },
  33. contentType : (m.contentType.indexOf('application/json') > -1) ? m.contentType : false,
  34. processData : (m.contentType.indexOf('application/json') > -1) ? true : false,
  35. xhrFields : {
  36. 'withCredentials' : true
  37. },
  38. data : ((m.contentType.indexOf('application/json') > -1) ? JSON.stringify(data) : data)
  39. }).always(function(resultJson) {
  40. $('#result').html(JSON.stringify(resultJson, null, 4));
  41. Describe.writeOut(m.outs, resultJson);
  42. });
  43. } else {
  44. $.ajax({
  45. url : address,
  46. type : 'POST',
  47. headers : {
  48. 'x-debugger' : true
  49. },
  50. contentType : (m.contentType.indexOf('application/json') > -1) ? m.contentType : false,
  51. processData : (m.contentType.indexOf('application/json') > -1) ? true : false,
  52. xhrFields : {
  53. 'withCredentials' : true
  54. },
  55. data : ((m.contentType.indexOf('application/json') > -1) ? JSON.stringify(data) : data)
  56. });
  57. }
  58. }
  59. Describe.doPut = function(address, m, data) {
  60. $('#url').html(address);
  61. if ((m.resultContentType) && m.resultContentType.indexOf('application/json') > -1) {
  62. $.ajax({
  63. url : address,
  64. type : 'PUT',
  65. headers : {
  66. 'x-debugger' : true
  67. },
  68. contentType : (m.contentType.indexOf('application/json') > -1) ? m.contentType : false,
  69. processData : (m.contentType.indexOf('application/json') > -1) ? true : false,
  70. xhrFields : {
  71. 'withCredentials' : true
  72. },
  73. data : ((m.contentType.indexOf('application/json') > -1) ? JSON.stringify(data) : data)
  74. }).always(function(resultJson) {
  75. $('#result').html(JSON.stringify(resultJson, null, 4));
  76. Describe.writeOut(m.outs, resultJson);
  77. });
  78. } else {
  79. $.ajax({
  80. url : address,
  81. type : 'PUT',
  82. headers : {
  83. 'x-debugger' : true
  84. },
  85. contentType : (m.contentType.indexOf('application/json') > -1) ? m.contentType : false,
  86. processData : (m.contentType.indexOf('application/json') > -1) ? true : false,
  87. xhrFields : {
  88. 'withCredentials' : true
  89. },
  90. data : ((m.contentType.indexOf('application/json') > -1) ? JSON.stringify(data) : data)
  91. });
  92. }
  93. }
  94. Describe.doGet = function(address, m) {
  95. $('#url').html(address);
  96. if ((m.resultContentType) && m.resultContentType.indexOf('application/json') > -1) {
  97. $.ajax({
  98. type : 'GET',
  99. dataType : 'json',
  100. url : address,
  101. headers : {
  102. 'x-debugger' : true
  103. },
  104. contentType : m.contentType,
  105. xhrFields : {
  106. 'withCredentials' : true
  107. },
  108. crossDomain : true
  109. }).always(function(resultJson) {
  110. $('#result').html(JSON.stringify(resultJson, null, 4));
  111. Describe.writeOut(m.outs, resultJson);
  112. });
  113. } else {
  114. window.open(address, '_blank');
  115. }
  116. }
  117. Describe.doDelete = function(address, m) {
  118. $('#url').html(address);
  119. if ((m.resultContentType) && m.resultContentType.indexOf('application/json') > -1) {
  120. $.ajax({
  121. type : 'DELETE',
  122. dataType : 'json',
  123. url : address,
  124. headers : {
  125. 'x-debugger' : true
  126. },
  127. contentType : m.contentType,
  128. xhrFields : {
  129. 'withCredentials' : true
  130. },
  131. crossDomain : true
  132. }).always(function(resultJson) {
  133. $('#result').html(JSON.stringify(resultJson, null, 4));
  134. Describe.writeOut(m.outs, resultJson);
  135. });
  136. } else {
  137. $.ajax({
  138. type : 'DELETE',
  139. dataType : 'json',
  140. url : address,
  141. headers : {
  142. 'x-debugger' : true
  143. },
  144. contentType : m.contentType,
  145. xhrFields : {
  146. 'withCredentials' : true
  147. },
  148. crossDomain : true
  149. });
  150. }
  151. }
  152. Describe.writeOut = function(outs, json) {
  153. if (outs && (outs.length) && json && json.data) {
  154. $.each(Object.keys(json.data), function(i, k) {
  155. $('#out_' + k + '_out', '#outs').html(json.data[k]);
  156. });
  157. }
  158. }
  159. Describe.prototype = {
  160. "load" : function() {
  161. var str = '<ul>';
  162. $.getJSON('../describe/describe.json?rd=' + Math.random(), function(json) {
  163. $.each(json.jaxrs, function(ji, j) {
  164. str += '<li>' + j.name;
  165. $.each(j.methods, function(mi, m) {
  166. str += '<ul><li><a id ="' + j.name + '_' + m.name + '" href="#">' + m.name + '</a></li></ul>';
  167. });
  168. str += '</li>'
  169. });
  170. str += '</ul>';
  171. $("#menu").html(str);
  172. $.each(json.jaxrs, function(ji, j) {
  173. $.each(j.methods, function(mi, m) {
  174. $('#' + j.name + '_' + m.name).click(
  175. function() {
  176. $('#result').html('');
  177. var txt = '<fieldset id="method"><legend>Method</legend>';
  178. txt += '<table>';
  179. txt += '<tr><td>name:</td><td><a href="../describe/sources/' + m.className.replace(/\./g, '/') + '.java">' + m.name + '</a></td></tr>';
  180. txt += '<tr><td>path:</td><td>' + m.path + '</td></tr>';
  181. txt += '<tr><td>type:</td><td>' + m.type + '</td></tr>';
  182. txt += '<tr><td>description:</td><td>' + m.description + '</td></tr>';
  183. txt += '</table>';
  184. txt += '<button id="' + m.name + "_" + m.type + '">' + m.type + '</button>';
  185. txt += '<div id="url">&nbsp;</div>';
  186. txt += '</fieldset>';
  187. if (m.pathParameters && m.pathParameters.length > 0) {
  188. txt += '<fieldset id="pathParameters"><legend>Path Parameter</legend>';
  189. txt += '<table >';
  190. $.each(m.pathParameters, function(pi, p) {
  191. if (m.name == 'listNext' || m.name == 'listPrev') {
  192. switch (p.name) {
  193. case 'flag':
  194. case 'id':
  195. txt += '<tr><td><input type="text" id="' + p.name + '" style="width:600px; padding:1px; border:1px #000000 solid" value="(0)"/></td><td>' + p.name
  196. + ':' + p.description + '</td></tr>';
  197. break;
  198. case 'count':
  199. txt += '<tr><td><input type="text" id="' + p.name + '" style="width:600px; padding:1px; border:1px #000000 solid" value="20"/></td><td>' + p.name + ':'
  200. + p.description + '</td></tr>';
  201. break;
  202. default:
  203. txt += '<tr><td><input type="text" id="' + p.name + '" style="width:600px; padding:1px; border:1px #000000 solid"/></td><td>' + p.name + ':'
  204. + p.description + '</td></tr>';
  205. break
  206. }
  207. } else {
  208. txt += '<tr><td><input type="text" id="' + p.name + '" style="width:600px; padding:1px; border:1px #000000 solid"/></td><td>' + p.name + ':'
  209. + p.description + '</td></tr>';
  210. }
  211. });
  212. txt += '</table>';
  213. txt += '</fieldset>';
  214. }
  215. if (m.formParameters && m.formParameters.length > 0) {
  216. txt += '<fieldset id="formParameters"><legend>Form Parameter</legend>';
  217. txt += '<table >';
  218. $.each(m.formParameters, function(pi, p) {
  219. if (p.type == "File") {
  220. txt += '<tr><td><input type="file" name="' + p.name + '" id="' + p.name + '" style="width:600px; padding:1px; border:1px #000000 solid"/></td><td>'
  221. + p.name + ':' + p.description + '</td></tr>';
  222. } else {
  223. txt += '<tr><td><input type="text" id="' + p.name + '" style="width:600px; padding:1px; border:1px #000000 solid"/></td><td>' + p.name + ':'
  224. + p.description + '</td></tr>';
  225. }
  226. });
  227. txt += '</table>';
  228. txt += '</fieldset>';
  229. }
  230. if (m.queryParameters && m.queryParameters.length > 0) {
  231. txt += '<fieldset id="queryParameters"><legend>Query Parameter</legend>';
  232. txt += '<table >';
  233. $.each(m.queryParameters, function(pi, p) {
  234. txt += '<tr><td><input type="text" id="' + p.name + '" style="width:600px; padding:1px; border:1px #000000 solid"/></td><td>' + p.name + ':' + p.description
  235. + '</td></tr>';
  236. });
  237. txt += '</table>';
  238. txt += '</fieldset>';
  239. }
  240. if (m.ins && m.ins.length > 0) {
  241. txt += '<fieldset id="ins"><legend>In</legend>';
  242. txt += '<table>';
  243. $.each(m.ins, function(ii, i) {
  244. if (i.isCollection) {
  245. txt += '<tr><td><textarea id="' + i.name + '" style="width:600px; padding:1px; border:1px #000000 solid"/></td><td>' + i.name + ':' + i.description
  246. '</td></tr>';
  247. } else {
  248. txt += '<tr><td><input type="text" id="' + i.name + '" style="width:600px; padding:1px; border:1px #000000 solid"/></td><td>' + i.name + ':'
  249. + i.description
  250. '</td></tr>';
  251. }
  252. });
  253. txt += '</table>';
  254. txt += '</fieldset>';
  255. }
  256. if (m.useJsonElementParameter) {
  257. txt += '<fieldset><legend>JsonElement</legend>';
  258. txt += '<table><tr><td>';
  259. txt += '<textarea id="jsonElement" style="height:300px; width:600px; padding:1px; border:1px #000000 solid"/>';
  260. txt += '</td><td>json</td></tr>';
  261. txt += '</table>';
  262. txt += '</fieldset>';
  263. }
  264. if (m.outs && m.outs.length > 0) {
  265. txt += '<fieldset id="outs"><legend>Out</legend>';
  266. txt += '<table>';
  267. $.each(m.outs, function(oi, o) {
  268. txt += '<tr><td>' + o.name + '</td><td>' + o.type + '</td><td>' + (o.isCollection ? 'multi' : 'single') + '</td><td>' + o.description + '</td><td id="out_'
  269. + o.name + '_out">&nbsp;</td></tr>';
  270. });
  271. txt += '</table>';
  272. txt += '</fieldset>';
  273. }
  274. $('#content').html(txt);
  275. $('#' + m.name + '_' + m.type, '#method').click(function() {
  276. var address = '../' + m.path;
  277. if (m.pathParameters && m.pathParameters.length > 0) {
  278. $.each(m.pathParameters, function(pi, p) {
  279. address = address.replace('{' + p.name + '}', encodeURIComponent($('#' + p.name, '#pathParameters').val()));
  280. });
  281. }
  282. if (m.queryParameters && m.queryParameters.length > 0) {
  283. $.each(m.queryParameters, function(pi, p) {
  284. var query = p.name + '=' + encodeURIComponent($('#' + p.name, '#queryParameters').val());
  285. if (address.indexOf("?") > 0) {
  286. address += '&' + query;
  287. } else {
  288. address += '?' + query;
  289. }
  290. });
  291. }
  292. if (m.contentType.indexOf('application/json') > -1) {
  293. switch (m.type) {
  294. case 'POST':
  295. var data = {};
  296. if (m.ins && m.ins.length > 0) {
  297. $.each(m.ins, function(ii, i) {
  298. switch (i.type) {
  299. default:
  300. if (i.isBaseType) {
  301. if (i.isCollection) {
  302. data[i.name] = Describe.splitValue($('#' + i.name, '#ins').val());
  303. } else {
  304. data[i.name] = $('#' + i.name, '#ins').val();
  305. }
  306. } else {
  307. data[i.name] = $.parseJSON($('#' + i.name, '#ins').val());
  308. }
  309. }
  310. });
  311. } else if (m.useJsonElementParameter) {
  312. data = $.parseJSON($('#jsonElement').val());
  313. }
  314. Describe.doPost(address, m, data);
  315. break;
  316. case 'PUT':
  317. var data = {};
  318. if (m.ins && m.ins.length > 0) {
  319. $.each(m.ins, function(ii, i) {
  320. switch (i.type) {
  321. default:
  322. if (i.isBaseType) {
  323. if (i.isCollection) {
  324. data[i.name] = Describe.splitValue($('#' + i.name, '#ins').val());
  325. } else {
  326. data[i.name] = $('#' + i.name, '#ins').val();
  327. }
  328. } else {
  329. data[i.name] = $.parseJSON($('#' + i.name, '#ins').val());
  330. }
  331. }
  332. });
  333. } else if (m.useJsonElementParameter) {
  334. data = $.parseJSON($('#jsonElement').val());
  335. }
  336. Describe.doPut(address, m, data);
  337. break;
  338. case 'GET':
  339. Describe.doGet(address, m);
  340. break;
  341. case 'DELETE':
  342. Describe.doDelete(address, m);
  343. break;
  344. default:
  345. break;
  346. }
  347. } else {
  348. switch (m.type) {
  349. case 'POST':
  350. var formData = new FormData();
  351. if (m.formParameters && m.formParameters.length > 0) {
  352. $.each(m.formParameters, function(pi, p) {
  353. if (p.type == "File") {
  354. formData.append(p.name, $('input[type=file]', '#formParameters')[0].files[0]);
  355. } else {
  356. formData.append(p.name, $('#' + p.name, '#formParameters').val());
  357. }
  358. });
  359. }
  360. Describe.doPost(address, m, formData);
  361. break;
  362. case 'PUT':
  363. var formData = new FormData();
  364. if (m.formParameters && m.formParameters.length > 0) {
  365. $.each(m.formParameters, function(pi, p) {
  366. if (p.type == "File") {
  367. formData.append(p.name, $('input[type=file]', '#formParameters')[0].files[0]);
  368. } else {
  369. formData.append(p.name, $('#' + p.name, '#formParameters').val());
  370. }
  371. });
  372. }
  373. Describe.doPut(address, m, formData);
  374. break;
  375. case 'GET':
  376. Describe.doGet(address, m);
  377. break;
  378. case 'DELETE':
  379. Describe.doDelete(address, m);
  380. break;
  381. default:
  382. break;
  383. }
  384. }
  385. })
  386. });
  387. });
  388. });
  389. });
  390. }
  391. }