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.
 
 
 
 
 
 

179 rivejä
4.5 KiB

  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. 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. var ajax_args = {
  62. url: opts.url || wn.request.url,
  63. data: opts.args,
  64. type: opts.type || 'POST',
  65. dataType: opts.dataType || 'json',
  66. success: function(r, xhr) {
  67. wn.request.cleanup(opts, r);
  68. opts.success && opts.success(r, xhr.responseText);
  69. },
  70. error: function(xhr, textStatus) {
  71. wn.request.cleanup(opts, {});
  72. show_alert(wn._("Unable to complete request: ") + textStatus)
  73. opts.error && opts.error(xhr)
  74. }
  75. };
  76. if(opts.progress_bar) {
  77. var interval = null;
  78. $.extend(ajax_args, {
  79. xhr: function() {
  80. var xhr = jQuery.ajaxSettings.xhr();
  81. interval = setInterval(function() {
  82. if(xhr.readyState > 2) {
  83. var total = parseInt(xhr.getResponseHeader('Original-Length') || 0) ||
  84. parseInt(xhr.getResponseHeader('Content-Length'));
  85. var completed = parseInt(xhr.responseText.length);
  86. var percent = (100.0 / total * completed).toFixed(2);
  87. opts.progress_bar.css('width', (percent < 10 ? 10 : percent) + '%');
  88. }
  89. }, 50);
  90. wn.last_xhr = xhr;
  91. return xhr;
  92. },
  93. complete: function() {
  94. opts.progress_bar.css('width', '100%');
  95. clearInterval(interval);
  96. }
  97. })
  98. }
  99. $.ajax(ajax_args);
  100. }
  101. // call execute serverside request
  102. wn.request.prepare = function(opts) {
  103. // btn indicator
  104. if(opts.btn) $(opts.btn).set_working();
  105. // navbar indicator
  106. if(opts.show_spinner) wn.set_loading();
  107. // freeze page
  108. if(opts.freeze) wn.dom.freeze();
  109. // no cmd?
  110. if(!opts.args.cmd) {
  111. console.log(opts)
  112. throw "Incomplete Request";
  113. }
  114. }
  115. wn.request.cleanup = function(opts, r) {
  116. // stop button indicator
  117. if(opts.btn) $(opts.btn).done_working();
  118. // hide button indicator
  119. if(opts.show_spinner) wn.done_loading();
  120. // un-freeze page
  121. if(opts.freeze) wn.dom.unfreeze();
  122. // session expired?
  123. if(r.session_expired) {
  124. if(!wn.app.logged_out) {
  125. msgprint(wn._('Session Expired. Logging you out'));
  126. wn.app.logout();
  127. }
  128. return;
  129. }
  130. // show messages
  131. if(r.server_messages) {
  132. r.server_messages = JSON.parse(r.server_messages)
  133. msgprint(r.server_messages);
  134. }
  135. // show errors
  136. if(r.exc) {
  137. r.exc = JSON.parse(r.exc);
  138. if(r.exc instanceof Array) {
  139. $.each(r.exc, function(i, v) {
  140. if(v)console.log(v);
  141. })
  142. } else {
  143. console.log(r.exc);
  144. }
  145. };
  146. if(r['403']) {
  147. wn.container.change_to('403');
  148. }
  149. if(r.docs) {
  150. wn.model.sync(r.docs);
  151. }
  152. if(r.__messages) {
  153. $.extend(wn._messages, r.__messages);
  154. }
  155. wn.last_response = r;
  156. }