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.

request.js 4.1 KiB

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