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.
 
 
 
 
 
 

177 regels
4.4 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. // call execute serverside request
  26. wn.request.prepare = function(opts) {
  27. // btn indicator
  28. if(opts.btn) $(opts.btn).set_working();
  29. // navbar indicator
  30. if(opts.show_spinner) wn.set_loading();
  31. // freeze page
  32. if(opts.freeze) wn.dom.freeze();
  33. // no cmd?
  34. if(!opts.args.cmd) {
  35. console.log(opts)
  36. throw "Incomplete Request";
  37. }
  38. }
  39. wn.request.cleanup = function(opts, r) {
  40. // stop button indicator
  41. if(opts.btn) $(opts.btn).done_working();
  42. // hide button indicator
  43. if(opts.show_spinner) wn.done_loading();
  44. // un-freeze page
  45. if(opts.freeze) wn.dom.unfreeze();
  46. // session expired?
  47. if(r.session_expired) {
  48. if(!wn.app.logged_out) {
  49. msgprint('Session Expired. Logging you out');
  50. wn.app.logout();
  51. }
  52. return;
  53. }
  54. // show messages
  55. if(r.server_messages) {
  56. r.server_messages = JSON.parse(r.server_messages)
  57. msgprint(r.server_messages);
  58. }
  59. // show errors
  60. if(r.exc) {
  61. r.exc = JSON.parse(r.exc);
  62. if(r.exc instanceof Array) {
  63. $.each(r.exc, function(i, v) {
  64. if(v)console.log(v);
  65. })
  66. } else {
  67. console.log(r.exc);
  68. }
  69. };
  70. // 403
  71. if(r['403']) {
  72. wn.container.change_to('403');
  73. }
  74. // sync docs
  75. if(r.docs) {
  76. wn.model.sync(r.docs);
  77. }
  78. wn.last_response = r;
  79. }
  80. wn.request.call = function(opts) {
  81. wn.request.prepare(opts);
  82. var ajax_args = {
  83. url: opts.url || wn.request.url,
  84. data: opts.args,
  85. type: opts.type || 'POST',
  86. dataType: opts.dataType || 'json',
  87. success: function(r, xhr) {
  88. wn.request.cleanup(opts, r);
  89. opts.success && opts.success(r, xhr.responseText);
  90. },
  91. error: function(xhr, textStatus) {
  92. wn.request.cleanup(opts, {});
  93. show_alert('Unable to complete request: ' + textStatus)
  94. opts.error && opts.error(xhr)
  95. }
  96. };
  97. if(opts.progress_bar) {
  98. var interval = null;
  99. $.extend(ajax_args, {
  100. xhr: function() {
  101. var xhr = jQuery.ajaxSettings.xhr();
  102. interval = setInterval(function() {
  103. if(xhr.readyState > 2) {
  104. var total = parseInt(xhr.getResponseHeader('Original-Length') || 0) ||
  105. parseInt(xhr.getResponseHeader('Content-Length'));
  106. var completed = parseInt(xhr.responseText.length);
  107. var percent = (100.0 / total * completed).toFixed(2);
  108. opts.progress_bar.css('width', (percent < 10 ? 10 : percent) + '%');
  109. }
  110. }, 50);
  111. wn.last_xhr = xhr;
  112. return xhr;
  113. },
  114. complete: function() {
  115. opts.progress_bar.css('width', '100%');
  116. clearInterval(interval);
  117. }
  118. })
  119. }
  120. $.ajax(ajax_args);
  121. }
  122. // generic server call (call page, object)
  123. wn.call = function(opts) {
  124. var args = $.extend({}, opts.args);
  125. // cmd
  126. if(opts.module && opts.page) {
  127. args.cmd = opts.module+'.page.'+opts.page+'.'+opts.page+'.'+opts.method;
  128. } else if(opts.doc) {
  129. $.extend(args, {
  130. cmd: "runserverobj",
  131. docs: wn.model.compress(wn.model.get_doclist(opts.doc.doctype,
  132. opts.doc.name)),
  133. method: opts.method,
  134. args: opts.args,
  135. });
  136. } else if(opts.method) {
  137. args.cmd = opts.method;
  138. }
  139. // stringify args if required
  140. for(key in args) {
  141. if(args[key] && typeof args[key] != 'string') {
  142. args[key] = JSON.stringify(args[key]);
  143. }
  144. }
  145. wn.request.call({
  146. args: args,
  147. success: opts.callback,
  148. error: opts.error,
  149. btn: opts.btn,
  150. freeze: opts.freeze,
  151. show_spinner: !opts.no_spinner,
  152. progress_bar: opts.progress_bar
  153. });
  154. }