ajax.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. /**
  20. * Config for each ajax-request
  21. *
  22. * Fields example:
  23. * mock - testMode url
  24. * real - real url (without API prefix)
  25. * type - request type (also may be defined in the format method)
  26. * format - function for processing ajax params after default formatRequest. Return ajax-params object
  27. * testInProduction - can this request be executed on production tests (used only in tests)
  28. *
  29. * @type {Object}
  30. */
  31. var urls = {
  32. 'background_operations': {
  33. 'mock': '/data/background_operations/list_on_start.json',
  34. 'real': '/clusters/{clusterName}/requests/?fields=tasks/*',
  35. 'testInProduction': true
  36. },
  37. 'service.item.start_stop': {
  38. 'mock': '/data/wizard/deploy/poll_1.json',
  39. 'real': '/clusters/{clusterName}/services/{serviceName}',
  40. 'format': function (data, opt) {
  41. return {
  42. type: 'PUT',
  43. data: JSON.stringify({
  44. ServiceInfo: {
  45. state: data.state
  46. }
  47. })
  48. };
  49. }
  50. },
  51. 'service.item.smoke': {
  52. 'mock': '/data/wizard/deploy/poll_1.json',
  53. 'real': '/clusters/{clusterName}/services/{serviceName}/actions/{serviceName}_SERVICE_CHECK',
  54. 'type': 'POST'
  55. }
  56. };
  57. /**
  58. * Replace data-placeholders to its values
  59. *
  60. * @param {String} url
  61. * @param {Object} data
  62. * @return {String}
  63. */
  64. var formatUrl = function(url, data) {
  65. var keys = url.match(/\{\w+\}/g);
  66. keys.forEach(function(key){
  67. var raw_key = key.substr(1, key.length - 2);
  68. var replace;
  69. if (!data[raw_key]) {
  70. replace = '';
  71. }
  72. else {
  73. replace = data[raw_key];
  74. }
  75. url = url.replace(new RegExp(key, 'g'), replace);
  76. });
  77. return url;
  78. };
  79. /**
  80. * this = object from config
  81. * @return {Object}
  82. */
  83. var formatRequest = function(data) {
  84. var opt = {
  85. type : this.type || 'GET',
  86. timeout : App.timeout,
  87. dataType: 'json',
  88. statusCode: require('data/statusCodes')
  89. };
  90. if(App.testMode) {
  91. opt.url = this.mock;
  92. }
  93. else {
  94. opt.url = App.apiPrefix + formatUrl(this.real, data);
  95. }
  96. if(this.format) {
  97. jQuery.extend(opt, this.format(data, opt));
  98. }
  99. return opt;
  100. };
  101. /**
  102. * Wrapper for all ajax requests
  103. *
  104. * @type {Object}
  105. */
  106. App.ajax = {
  107. /**
  108. * Send ajax request
  109. *
  110. * @param {Object} config
  111. * @return {Boolean}
  112. *
  113. * config fields:
  114. * name - url-key in the urls-object *required*
  115. * sender - object that send request (need for proper callback initialization) *required*
  116. * data - object with data for url-format
  117. * success - method-name for ajax success response callback
  118. * error - method-name for ajax error response callback
  119. */
  120. send: function(config) {
  121. if (!config.sender) {
  122. console.warn('Ajax sender should be defined!');
  123. return false;
  124. }
  125. // default parameters
  126. var params = {
  127. clusterName: App.router.get('clusterController.clusterName')
  128. };
  129. // extend default parameters with provided
  130. if (config.data) {
  131. jQuery.extend(params, config.data);
  132. }
  133. var opt = {};
  134. opt = formatRequest.call(urls[config.name], params);
  135. // object sender should be provided for processing success and error responses
  136. opt.success = function(data) {
  137. if(config.success) {
  138. config.sender[config.success](data, opt);
  139. }
  140. };
  141. opt.error = function(request, ajaxOptions, error) {
  142. if (config.error) {
  143. config.sender[config.error](request, ajaxOptions, error, opt);
  144. }
  145. };
  146. $.ajax(opt);
  147. }
  148. }