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.
 
 
 
 
 
 

149 lines
3.8 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) set_loading();
  31. // freeze page
  32. if(opts.freeze) 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) hide_loading();
  44. // un-freeze page
  45. if(opts.freeze) unfreeze();
  46. // session expired?
  47. if(wn.boot && wn.boot.sid && wn.get_cookie('sid') != wn.boot.sid) {
  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) msgprint(r.server_messages)
  56. // show errors
  57. if(r.exc) { console.log(r.exc); };
  58. // 403
  59. if(r['403']) {
  60. wn.container.change_to('403');
  61. }
  62. // sync docs
  63. if(r.docs) {
  64. LocalDB.sync(r.docs);
  65. }
  66. }
  67. wn.request.call = function(opts) {
  68. wn.request.prepare(opts);
  69. var args = {
  70. url: opts.url || wn.request.url,
  71. data: opts.args,
  72. type: opts.type || 'POST',
  73. dataType: opts.dataType || 'json',
  74. success: function(r, xhr) {
  75. wn.request.cleanup(opts, r);
  76. opts.success(r, xhr.responseText);
  77. },
  78. error: function(xhr, textStatus) {
  79. wn.request.cleanup(opts, {});
  80. show_alert('Unable to complete request: ' + textStatus)
  81. if(opts.error)opts.error(xhr)
  82. }
  83. };
  84. if(opts.progress_bar) {
  85. var interval = null;
  86. $.extend(args, {
  87. xhr: function() {
  88. var xhr = jquery.ajaxSettings.xhr();
  89. interval = setInterval(function() {
  90. if(xhr.readyState > 2) {
  91. var total = parseInt(xhr.getResponseHeader('Content-length'));
  92. var completed = parseInt(xhr.responseText.length);
  93. opts.progress_bar.css('width', (100.0 / total * completed).toFixed(2) + '%');
  94. }
  95. }, 50);
  96. },
  97. complete: function() {
  98. clearInterval(interval);
  99. }
  100. })
  101. }
  102. $.ajax(args)
  103. }
  104. // generic server call (call page, object)
  105. wn.call = function(opts) {
  106. var args = $.extend({}, opts.args)
  107. // cmd
  108. if(opts.module && opts.page) {
  109. args.cmd = opts.module+'.page.'+opts.page+'.'+opts.page+'.'+opts.method
  110. } else if(opts.method) {
  111. args.cmd = opts.method;
  112. }
  113. // stringify args if required
  114. for(key in args) {
  115. if(args[key] && typeof args[key] != 'string') {
  116. args[key] = JSON.stringify(args[key]);
  117. }
  118. }
  119. wn.request.call({
  120. args: args,
  121. success: opts.callback,
  122. error: opts.error,
  123. btn: opts.btn,
  124. freeze: opts.freeze,
  125. show_spinner: !opts.no_spinner
  126. });
  127. }