You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

12 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. //
  3. // MIT License (MIT)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a
  6. // copy of this software and associated documentation files (the "Software"),
  7. // to deal in the Software without restriction, including without limitation
  8. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. // and/or sell copies of the Software, and to permit persons to whom the
  10. // Software is furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. // OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. //
  22. // My HTTP Request
  23. wn.provide('wn.request');
  24. wn.request.url = 'server.py';
  25. // generic server call (call page, object)
  26. wn.call = function(opts) {
  27. var args = $.extend({}, opts.args);
  28. // cmd
  29. if(opts.module && opts.page) {
  30. args.cmd = opts.module+'.page.'+opts.page+'.'+opts.page+'.'+opts.method;
  31. } else if(opts.doc) {
  32. $.extend(args, {
  33. cmd: "runserverobj",
  34. docs: wn.model.compress(wn.model.get_doclist(opts.doc.doctype,
  35. opts.doc.name)),
  36. method: opts.method,
  37. args: opts.args,
  38. });
  39. } else if(opts.method) {
  40. args.cmd = opts.method;
  41. }
  42. // stringify args if required
  43. for(key in args) {
  44. if(args[key] && typeof args[key] != 'string') {
  45. args[key] = JSON.stringify(args[key]);
  46. }
  47. }
  48. return wn.request.call({
  49. type: opts.type || "POST",
  50. args: args,
  51. success: opts.callback,
  52. error: opts.error,
  53. btn: opts.btn,
  54. freeze: opts.freeze,
  55. show_spinner: !opts.no_spinner,
  56. progress_bar: opts.progress_bar
  57. });
  58. }
  59. wn.request.call = function(opts) {
  60. wn.request.prepare(opts);
  61. // all requests will be post, set _type as POST for commit
  62. opts.args._type = opts.type;
  63. var ajax_args = {
  64. url: opts.url || wn.request.url,
  65. data: opts.args,
  66. type: 'POST',
  67. dataType: opts.dataType || 'json',
  68. success: function(r, xhr) {
  69. wn.request.cleanup(opts, r);
  70. opts.success && opts.success(r, xhr.responseText);
  71. },
  72. error: function(xhr, textStatus) {
  73. wn.request.cleanup(opts, {});
  74. show_alert(wn._("Unable to complete request: ") + textStatus)
  75. opts.error && opts.error(xhr)
  76. }
  77. };
  78. if(opts.progress_bar) {
  79. var interval = null;
  80. $.extend(ajax_args, {
  81. xhr: function() {
  82. var xhr = jQuery.ajaxSettings.xhr();
  83. interval = setInterval(function() {
  84. if(xhr.readyState > 2) {
  85. var total = parseInt(xhr.getResponseHeader('Original-Length') || 0) ||
  86. parseInt(xhr.getResponseHeader('Content-Length'));
  87. var completed = parseInt(xhr.responseText.length);
  88. var percent = (100.0 / total * completed).toFixed(2);
  89. opts.progress_bar.css('width', (percent < 10 ? 10 : percent) + '%');
  90. }
  91. }, 50);
  92. wn.last_xhr = xhr;
  93. return xhr;
  94. },
  95. complete: function() {
  96. opts.progress_bar.css('width', '100%');
  97. clearInterval(interval);
  98. }
  99. })
  100. }
  101. return $.ajax(ajax_args);
  102. }
  103. // call execute serverside request
  104. wn.request.prepare = function(opts) {
  105. // btn indicator
  106. if(opts.btn) $(opts.btn).set_working();
  107. // navbar indicator
  108. if(opts.show_spinner) wn.set_loading();
  109. // freeze page
  110. if(opts.freeze) wn.dom.freeze();
  111. // no cmd?
  112. if(!opts.args.cmd) {
  113. console.log(opts)
  114. throw "Incomplete Request";
  115. }
  116. }
  117. wn.request.cleanup = function(opts, r) {
  118. // stop button indicator
  119. if(opts.btn) $(opts.btn).done_working();
  120. // hide button indicator
  121. if(opts.show_spinner) wn.done_loading();
  122. // un-freeze page
  123. if(opts.freeze) wn.dom.unfreeze();
  124. // session expired?
  125. if(r.session_expired) {
  126. if(!wn.app.logged_out) {
  127. msgprint(wn._('Session Expired. Logging you out'));
  128. wn.app.logout();
  129. }
  130. return;
  131. }
  132. // show messages
  133. if(r._server_messages) {
  134. r._server_messages = JSON.parse(r._server_messages)
  135. msgprint(r._server_messages);
  136. }
  137. // show errors
  138. if(r.exc) {
  139. r.exc = JSON.parse(r.exc);
  140. if(r.exc instanceof Array) {
  141. $.each(r.exc, function(i, v) {
  142. if(v)console.log(v);
  143. })
  144. } else {
  145. console.log(r.exc);
  146. }
  147. };
  148. // debug messages
  149. if(r._debug_messages) {
  150. console.log("-")
  151. console.log("-")
  152. console.log("-")
  153. if(opts.args) {
  154. console.log("<<<< arguments ");
  155. console.log(opts.args);
  156. console.log(">>>>")
  157. }
  158. $.each(JSON.parse(r._debug_messages), function(i, v) { console.log(v); });
  159. console.log("<<<< response");
  160. delete r._debug_messages;
  161. console.log(r);
  162. console.log(">>>>")
  163. console.log("-")
  164. console.log("-")
  165. console.log("-")
  166. }
  167. if(r['403']) {
  168. wn.set_route('403');
  169. }
  170. if(r.docs) {
  171. r.docs = wn.model.sync(r);
  172. }
  173. if(r.__messages) {
  174. $.extend(wn._messages, r.__messages);
  175. }
  176. wn.last_response = r;
  177. }