describe.js 13 KB

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