Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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