Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

375 Zeilen
10 KiB

  1. jQuery(function($) {
  2. // this.baseUrl = "http://swagr.api.wordnik.com/v4";
  3. // this.baseUrl = "http://petstore.swagger.wordnik.com/api";
  4. // this.apiKey = "special-key";
  5. var ApiSelectionController = Spine.Controller.create({
  6. proxied: ["showApi"],
  7. baseUrlList: new Array(),
  8. init: function() {
  9. if (this.supportsLocalStorage()) {
  10. var baseUrl = localStorage.getItem("com.wordnik.swagger.ui.baseUrl");
  11. var apiKey = localStorage.getItem("com.wordnik.swagger.ui.apiKey");
  12. if (baseUrl && baseUrl.length > 0)
  13. $("#input_baseUrl").val(baseUrl);
  14. if (apiKey && apiKey.length > 0)
  15. $("#input_apiKey").val(apiKey);
  16. } else {
  17. log("localStorage not supported, user will need to specifiy the api url");
  18. }
  19. $("a#explore").click(this.showApi);
  20. this.adaptToScale();
  21. $(window).resize(function() {
  22. apiSelectionController.adaptToScale();
  23. });
  24. },
  25. adaptToScale: function() {
  26. // var form_width = $('form#api_selector').width();
  27. // var inputs_width = 0;
  28. // $('form#api_selector div.input').each( function(){ inputs_width += $(this).outerWidth(); });
  29. //
  30. // // Update with of baseUrl input
  31. // var free_width = form_width - inputs_width;
  32. // $('#input_baseUrl').width($('#input_baseUrl').width() + free_width - 50);
  33. },
  34. slapOn: function() {
  35. // messageController.showMessage("Please enter the base URL of the API that you wish to explore.");
  36. $("#content_message").show();
  37. $("#resources_container").hide();
  38. this.showApi();
  39. },
  40. supportsLocalStorage: function() {
  41. try {
  42. return 'localStorage' in window && window['localStorage'] !== null;
  43. } catch(e) {
  44. return false;
  45. }
  46. },
  47. showApi: function() {
  48. var baseUrl = jQuery.trim($("#input_baseUrl").val());
  49. var apiKey = jQuery.trim($("#input_apiKey").val());
  50. if (baseUrl.length == 0) {
  51. $("#input_baseUrl").wiggle();
  52. } else {
  53. if (this.supportsLocalStorage()) {
  54. localStorage.setItem("com.wordnik.swagger.ui.apiKey", apiKey);
  55. localStorage.setItem("com.wordnik.swagger.ui.baseUrl", baseUrl);
  56. }
  57. var resourceListController = ResourceListController.init({
  58. baseUrl: baseUrl,
  59. apiKey: apiKey
  60. });
  61. }
  62. }
  63. });
  64. var MessageController = Spine.Controller.create({
  65. showMessage: function(msg) {
  66. if (msg) {
  67. $("#content_message").html(msg);
  68. $("#content_message").show();
  69. } else {
  70. $("#content_message").html("");
  71. $("#content_message").hide();
  72. }
  73. },
  74. clearMessage: function() {
  75. this.showMessage();
  76. }
  77. });
  78. var messageController = MessageController.init();
  79. // The following heirarchy is followed by these view controllers
  80. // ResourceListController
  81. // >>> ResourceController
  82. // >>> ApiController
  83. // >>> OperationController
  84. var ResourceListController = Spine.Controller.create({
  85. proxied: ["addAll", "addOne"],
  86. ApiResource: null,
  87. init: function() {
  88. if (this.baseUrl == null) {
  89. throw new Error("A baseUrl must be passed to ResourceListController");
  90. }
  91. $("#content_message").hide();
  92. $("#resources_container").hide();
  93. $("#resources").html("");
  94. // create and initialize SwaggerService
  95. var swaggerService = new SwaggerService(this.baseUrl, this.apiKey,
  96. function(msg) {
  97. if (msg)
  98. messageController.showMessage(msg);
  99. else
  100. messageController.showMessage("Fetching remote JSON...");
  101. });
  102. // $("#api_host_url").html(swaggerService.apiHost());
  103. swaggerService.init();
  104. // Create convenience references to Spine models
  105. this.ApiResource = swaggerService.ApiResource();
  106. this.ApiResource.bind("refresh", this.addAll);
  107. },
  108. addAll: function() {
  109. this.ApiResource.each(this.addOne);
  110. messageController.clearMessage();
  111. $("#resources_container").slideDown(function() {
  112. setTimeout(function() {
  113. Docs.shebang();
  114. },
  115. 400);
  116. });
  117. },
  118. addOne: function(apiResource) {
  119. ResourceController.init({
  120. item: apiResource,
  121. container: "#resources"
  122. });
  123. }
  124. });
  125. var ResourceController = Spine.Controller.create({
  126. proxied: ["renderApi", "renderOperation"],
  127. templateName: "#resourceTemplate",
  128. apiResource: null,
  129. apiList: null,
  130. modelList: null,
  131. init: function() {
  132. this.render();
  133. this.apiResource = this.item;
  134. this.apiList = this.apiResource.apiList;
  135. this.modelList = this.apiResource.modelList;
  136. // log("------------- apiResource : " + this.apiResource.name);
  137. // this.apiList.logAll();
  138. // this.modelList.logAll();
  139. this.apiList.each(this.renderApi);
  140. },
  141. render: function() {
  142. $(this.templateName).tmpl(this.item).appendTo(this.container);
  143. $('#colophon').fadeIn();
  144. },
  145. renderApi: function(api) {
  146. var resourceApisContainer = "#" + this.apiResource.name + "_endpoint_list";
  147. ApiController.init({
  148. item: api,
  149. container: resourceApisContainer
  150. });
  151. }
  152. });
  153. var ApiController = Spine.Controller.create({
  154. proxied: ["renderOperation"],
  155. api: null,
  156. templateName: "#apiTemplate",
  157. init: function() {
  158. this.render();
  159. this.api = this.item;
  160. this.api.operations.each(this.renderOperation);
  161. },
  162. render: function() {
  163. $(this.templateName).tmpl(this.item).appendTo(this.container);
  164. },
  165. renderOperation: function(operation) {
  166. var operationsContainer = "#" + this.api.name + "_endpoint_operations";
  167. OperationController.init({
  168. item: operation,
  169. container: operationsContainer
  170. });
  171. }
  172. });
  173. // Param Model
  174. // ----------------------------------------------------------------------------------------------
  175. var Param = Spine.Model.setup(
  176. "Param",
  177. ["name", "defaultValue", 'description', 'required', 'dataType', 'allowableValues', 'paramType', 'allowMultiple', "readOnly"]
  178. );
  179. Param.include({
  180. cleanup: function() {
  181. this.defaultValue = this.defaultValue || '';
  182. },
  183. templateName: function(){
  184. var n = "#paramTemplate";
  185. if (this.allowableValues && this.allowableValues.length > 0) {
  186. n += "Select";
  187. } else {
  188. if (this.required) n += "Required";
  189. if (this.readOnly) n += "ReadOnly";
  190. }
  191. return(n);
  192. }
  193. });
  194. var OperationController = Spine.Controller.create({
  195. proxied: ["submitOperation", "showResponse", "showErrorStatus", "showCompleteStatus"],
  196. operation: null,
  197. templateName: "#operationTemplate",
  198. elementScope: "#operationTemplate",
  199. init: function() {
  200. this.render();
  201. this.operation = this.item;
  202. this.isGetOperation = (this.operation.httpMethodLowercase == "get");
  203. this.elementScope = "#" + this.operation.apiName + "_" + this.operation.nickname + "_" + this.operation.httpMethod;
  204. this.renderParams();
  205. },
  206. render: function() {
  207. $(this.templateName).tmpl(this.item).appendTo(this.container);
  208. },
  209. renderParams: function() {
  210. if (this.operation.parameters && this.operation.parameters.count() > 0) {
  211. var operationParamsContainer = this.elementScope + "_params";
  212. for (var p = 0; p < this.operation.parameters.count(); p++) {
  213. var param = Param.init(this.operation.parameters.all()[p]);
  214. // Only GET operations display forms..
  215. param.readOnly = !this.isGetOperation;
  216. param.cleanup();
  217. $(param.templateName()).tmpl(param).appendTo(operationParamsContainer);
  218. }
  219. }
  220. var submitButtonId = this.elementScope + "_content_sandbox_response_button";
  221. if (this.isGetOperation) {
  222. $(submitButtonId).click(this.submitOperation);
  223. } else {
  224. $(submitButtonId).hide();
  225. var valueHeader = this.elementScope + "_value_header";
  226. $(valueHeader).html("Default Value");
  227. }
  228. },
  229. submitOperation: function() {
  230. var form = $(this.elementScope + "_form");
  231. var error_free = true;
  232. var missing_input = null;
  233. // Cycle through the form's required inputs
  234. form.find("input.required").each(function() {
  235. // Remove any existing error styles from the input
  236. $(this).removeClass('error');
  237. // Tack the error style on if the input is empty..
  238. if ($(this).val() == '') {
  239. if (missing_input == null)
  240. missing_input = $(this);
  241. $(this).addClass('error');
  242. $(this).wiggle();
  243. error_free = false;
  244. }
  245. });
  246. if (error_free) {
  247. var invocationUrl = this.operation.invocationUrl(form.serializeArray());
  248. $(".request_url", this.elementScope + "_content_sandbox_response").html("<pre>" + invocationUrl + "</pre>");
  249. $.getJSON(invocationUrl, this.showResponse).complete(this.showCompleteStatus).error(this.showErrorStatus);
  250. }
  251. },
  252. showResponse: function(response) {
  253. // log(response);
  254. var prettyJson = JSON.stringify(response, null, "\t").replace(/\n/g, "<br>");
  255. // log(prettyJson);
  256. $(".response_body", this.elementScope + "_content_sandbox_response").html(prettyJson);
  257. $(this.elementScope + "_content_sandbox_response").slideDown();
  258. },
  259. showErrorStatus: function(data) {
  260. // log("error " + data.status);
  261. this.showStatus(data);
  262. $(this.elementScope + "_content_sandbox_response").slideDown();
  263. },
  264. showCompleteStatus: function(data) {
  265. // log("complete " + data.status);
  266. this.showStatus(data);
  267. },
  268. showStatus: function(data) {
  269. // log(data);
  270. // log(data.getAllResponseHeaders());
  271. var response_body = "<pre>" + JSON.stringify(JSON.parse(data.responseText), null, 2).replace(/\n/g, "<br>") + "</pre>";
  272. $(".response_code", this.elementScope + "_content_sandbox_response").html("<pre>" + data.status + "</pre>");
  273. $(".response_body", this.elementScope + "_content_sandbox_response").html(response_body);
  274. $(".response_headers", this.elementScope + "_content_sandbox_response").html("<pre>" + data.getAllResponseHeaders() + "</pre>");
  275. }
  276. });
  277. // Attach controller to window
  278. window.apiSelectionController = ApiSelectionController.init();
  279. if (this.baseUrl) {
  280. window.resourceListController = ResourceListController.init({
  281. baseUrl: this.baseUrl,
  282. apiKey: this.apiKey
  283. });
  284. } else {
  285. apiSelectionController.slapOn();
  286. }
  287. });