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.
 
 
 
 
 
 

112 lines
3.0 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 = 'index.cgi';
  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.sid && wn.get_cookie('sid') != wn.boot.sid) {
  48. msgprint('Session expired');
  49. setTimeout('redirect_to_login()', 3000);
  50. return;
  51. }
  52. // show messages
  53. if(r.server_messages) msgprint(r.server_messages)
  54. // show errors
  55. if(r.exc) { errprint(r.exc); console.log(r.exc); };
  56. // sync docs
  57. if(r.docs) LocalDB.sync(r.docs);
  58. }
  59. wn.request.call = function(opts) {
  60. wn.request.prepare(opts);
  61. $.ajax({
  62. url: opts.url || wn.request.url,
  63. data: opts.args,
  64. type: opts.type || 'POST',
  65. dataType: opts.dataType || 'json',
  66. success: function(r, xhr) {
  67. wn.request.cleanup(opts, r);
  68. opts.success(r, xhr.responseText);
  69. },
  70. error: function(xhr, textStatus) {
  71. wn.request.cleanup(opts, {});
  72. msgprint('Unable to complete request: ' + textStatus)
  73. if(opts.error)opts.error(xhr)
  74. }
  75. })
  76. }
  77. // generic server call (call page, object)
  78. wn.call = function(opts) {
  79. var args = opts.args || {};
  80. // cmd
  81. if(opts.module && opts.page) {
  82. args.cmd = module+'.page.'+page+'.'+page+'.'+method
  83. } else if(opts.method) {
  84. args.cmd = opts.method;
  85. }
  86. wn.request.call({
  87. args: args,
  88. success: opts.callback,
  89. error: opts.error,
  90. btn: opts.btn,
  91. freeze: opts.freeze,
  92. show_spinner: !opts.no_spinner
  93. });
  94. }