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.
 
 
 
 
 
 

195 lines
4.2 KiB

  1. // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. // MIT License. See license.txt
  3. // My HTTP Request
  4. wn.provide('wn.request');
  5. wn.request.url = '/';
  6. // generic server call (call page, object)
  7. wn.call = function(opts) {
  8. var args = $.extend({}, opts.args);
  9. // cmd
  10. if(opts.module && opts.page) {
  11. args.cmd = opts.module+'.page.'+opts.page+'.'+opts.page+'.'+opts.method;
  12. } else if(opts.doc) {
  13. $.extend(args, {
  14. cmd: "runserverobj",
  15. docs: wn.model.compress(wn.model.get_doclist(opts.doc.doctype,
  16. opts.doc.name)),
  17. method: opts.method,
  18. args: opts.args,
  19. });
  20. } else if(opts.method) {
  21. args.cmd = opts.method;
  22. }
  23. // stringify args if required
  24. for(key in args) {
  25. if(args[key] && typeof args[key] != 'string') {
  26. args[key] = JSON.stringify(args[key]);
  27. }
  28. }
  29. return wn.request.call({
  30. type: opts.type || "POST",
  31. args: args,
  32. success: opts.callback,
  33. error: opts.error,
  34. btn: opts.btn,
  35. freeze: opts.freeze,
  36. show_spinner: !opts.no_spinner,
  37. progress_bar: opts.progress_bar,
  38. async: opts.async
  39. });
  40. }
  41. wn.request.call = function(opts) {
  42. wn.request.prepare(opts);
  43. // all requests will be post, set _type as POST for commit
  44. opts.args._type = opts.type;
  45. var ajax_args = {
  46. url: opts.url || wn.request.url,
  47. data: opts.args,
  48. type: 'POST',
  49. dataType: opts.dataType || 'json',
  50. statusCode: {
  51. 404: function(xhr) {
  52. msgprint("Not Found");
  53. },
  54. 403: function(xhr) {
  55. msgprint("Not Permitted");
  56. },
  57. 200: function(data, xhr) {
  58. opts.success && opts.success(data, xhr.responseText);
  59. }
  60. },
  61. async: opts.async
  62. };
  63. wn.last_request = ajax_args.data;
  64. if(opts.progress_bar) {
  65. var interval = null;
  66. $.extend(ajax_args, {
  67. xhr: function() {
  68. var xhr = jQuery.ajaxSettings.xhr();
  69. interval = setInterval(function() {
  70. if(xhr.readyState > 2) {
  71. var total = parseInt(xhr.getResponseHeader('Original-Length') || 0) ||
  72. parseInt(xhr.getResponseHeader('Content-Length'));
  73. var completed = parseInt(xhr.responseText.length);
  74. var percent = (100.0 / total * completed).toFixed(2);
  75. opts.progress_bar.css('width', (percent < 10 ? 10 : percent) + '%');
  76. }
  77. }, 50);
  78. wn.last_xhr = xhr;
  79. return xhr;
  80. },
  81. complete: function() {
  82. opts.progress_bar.css('width', '100%');
  83. clearInterval(interval);
  84. }
  85. })
  86. }
  87. return $.ajax(ajax_args)
  88. .fail(function(xhr, textStatus) {
  89. opts.error && opts.error(xhr)
  90. })
  91. .always(function(data) {
  92. if(data.responseText) {
  93. data = JSON.parse(data.responseText);
  94. }
  95. wn.request.cleanup(opts, data);
  96. });
  97. }
  98. // call execute serverside request
  99. wn.request.prepare = function(opts) {
  100. // btn indicator
  101. if(opts.btn) $(opts.btn).set_working();
  102. // navbar indicator
  103. if(opts.show_spinner) wn.set_loading();
  104. // freeze page
  105. if(opts.freeze) wn.dom.freeze();
  106. // no cmd?
  107. if(!opts.args.cmd) {
  108. console.log(opts)
  109. throw "Incomplete Request";
  110. }
  111. }
  112. wn.request.cleanup = function(opts, r) {
  113. // stop button indicator
  114. if(opts.btn) $(opts.btn).done_working();
  115. // hide button indicator
  116. if(opts.show_spinner) wn.done_loading();
  117. // un-freeze page
  118. if(opts.freeze) wn.dom.unfreeze();
  119. // session expired? - Guest has no business here!
  120. if(r.session_expired || wn.get_cookie("sid")==="Guest") {
  121. if(!wn.app.logged_out) {
  122. msgprint(wn._('Session Expired. Logging you out'));
  123. wn.app.logout();
  124. }
  125. return;
  126. }
  127. // show messages
  128. if(r._server_messages) {
  129. r._server_messages = JSON.parse(r._server_messages)
  130. msgprint(r._server_messages);
  131. }
  132. // show errors
  133. if(r.exc) {
  134. r.exc = JSON.parse(r.exc);
  135. if(r.exc instanceof Array) {
  136. $.each(r.exc, function(i, v) {
  137. if(v)console.log(v);
  138. })
  139. } else {
  140. console.log(r.exc);
  141. }
  142. };
  143. // debug messages
  144. if(r._debug_messages) {
  145. console.log("-")
  146. console.log("-")
  147. console.log("-")
  148. if(opts.args) {
  149. console.log("<<<< arguments ");
  150. console.log(opts.args);
  151. console.log(">>>>")
  152. }
  153. $.each(JSON.parse(r._debug_messages), function(i, v) { console.log(v); });
  154. console.log("<<<< response");
  155. delete r._debug_messages;
  156. console.log(r);
  157. console.log(">>>>")
  158. console.log("-")
  159. console.log("-")
  160. console.log("-")
  161. }
  162. if(r.docs || r.docinfo) {
  163. r.docs = wn.model.sync(r);
  164. }
  165. if(r.__messages) {
  166. $.extend(wn._messages, r.__messages);
  167. }
  168. wn.last_response = r;
  169. }