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.
 
 
 
 
 
 

308 line
7.6 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. // Load Report
  23. // -------------------------------------------------------------------------------
  24. function loadreport(dt, rep_name, onload, menuitem, reset_report) {
  25. wn.require('lib/js/legacy/report.compressed.js');
  26. dt = get_label_doctype(dt);
  27. var show_report_builder = function() {
  28. if(!_r.rb_con) {
  29. // first load
  30. _r.rb_con = new _r.ReportContainer();
  31. }
  32. _r.rb_con.set_dt(dt, function(rb) {
  33. if(rep_name) {
  34. var t = rb.current_loaded;
  35. rb.load_criteria(rep_name);
  36. // call onload
  37. if(onload)
  38. onload(rb);
  39. // if loaded, then run
  40. if((rb.dt) && (!rb.dt.has_data() || rb.current_loaded!=t))
  41. rb.dt.run();
  42. } else {
  43. // reset if from toolbar
  44. if(reset_report) {
  45. rb.reset_report();
  46. }
  47. }
  48. // show
  49. if(!rb.forbidden) {
  50. page_body.change_to('Report Builder');
  51. nav_obj.open_notify('Report',dt,rep_name);
  52. }
  53. } );
  54. }
  55. show_report_builder();
  56. }
  57. // Load Doc
  58. // -------------------------------------------------------------------------------
  59. var load_doc = loaddoc;
  60. function loaddoc(doctype, name, onload, menuitem, from_archive) {
  61. doctype = get_label_doctype(doctype);
  62. // validate
  63. if(frms['DocType'] && frms['DocType'].opendocs[doctype]) {
  64. msgprint("Cannot open an instance of \"" + doctype + "\" when the DocType is open.");
  65. return;
  66. }
  67. // reverse validation - do not open DocType when an instance is open
  68. if(doctype=='DocType' && frms[name]) {
  69. msgprint("Cannot open DocType \"" + name + "\" when its instance is open.");
  70. return;
  71. }
  72. var show_form = function(f) {
  73. // load the frm container
  74. if(!_f.frm_con) {
  75. _f.frm_con = new _f.FrmContainer(); //new _f.FrmContainer();
  76. }
  77. // case A - frm not loaded
  78. if(!frms[doctype]) {
  79. _f.add_frm(doctype, show_doc, name, from_archive);
  80. // case B - both loaded
  81. } else if(LocalDB.is_doc_loaded(doctype, name)) {
  82. show_doc();
  83. // case C - only frm loaded
  84. } else {
  85. $c('webnotes.widgets.form.load.getdoc', {'name':name, 'doctype':doctype, 'user':user, 'from_archive':(from_archive ? 1 : 0) }, show_doc, null, null); // onload
  86. }
  87. }
  88. var show_doc = function(r,rt) {
  89. if(locals[doctype] && locals[doctype][name]) {
  90. var frm = frms[doctype];
  91. // show
  92. frm.refresh(name);
  93. // notify for back button
  94. if(!frm.in_dialog)
  95. nav_obj.open_notify('Form',doctype,name);
  96. if(onload) onload();
  97. } else {
  98. // nothing, go home - there were errors
  99. if(r.exc) { msgprint('There were errors while loading ' + doctype + ' ' + name); }
  100. loadpage('_home');
  101. }
  102. }
  103. //// is libary loaded?
  104. show_form();
  105. }
  106. // New Doc
  107. // -------------------------------------------------------------------------------
  108. function new_doc(doctype, onload, in_dialog, on_save_callback, cdt, cdn, cnic) {
  109. // cnic = caller not in container (caller is a dialog)
  110. doctype = get_label_doctype(doctype);
  111. if(!doctype) {
  112. if(cur_frm)doctype = cur_frm.doctype; else return;
  113. }
  114. var show_doc = function() {
  115. frm = frms[doctype];
  116. if (frm.perm[0][CREATE]==1) {
  117. // load new doc - create the new doc (if single, just load it)
  118. if(frm.meta.issingle) {
  119. var dn = doctype;
  120. LocalDB.set_default_values(locals[doctype][doctype]);
  121. } else
  122. var dn = LocalDB.create(doctype);
  123. // call (optional) onload
  124. if(onload)onload(dn);
  125. if(frm.in_dialog) {
  126. // attach values so that the "new" value is set in the field from which it was set
  127. var fd = _f.frm_dialog;
  128. fd.cdt = cdt;
  129. fd.cdn = cdn;
  130. fd.cnic = cnic;
  131. fd.on_save_callback = on_save_callback;
  132. } else {
  133. nav_obj.open_notify('Form',doctype,dn);
  134. }
  135. // show the form
  136. frm.refresh(dn);
  137. } else {
  138. msgprint('error:Not Allowed To Create '+doctype+'\nContact your Admin for help');
  139. }
  140. }
  141. var show_form = function() {
  142. // load the frm container
  143. if(!_f.frm_con) {
  144. _f.frm_con = new _f.FrmContainer();
  145. }
  146. if(!frms[doctype])
  147. _f.add_frm(doctype, show_doc); // load
  148. else
  149. show_doc(frms[doctype]); // directly
  150. }
  151. show_form();
  152. }
  153. var newdoc = new_doc;
  154. //
  155. // Load Page
  156. //
  157. var pscript={};
  158. var cur_page;
  159. function loadpage(page_name, call_back, no_history) {
  160. if(!page_name) return;
  161. if(page_name=='_home')
  162. page_name = home_page;
  163. var fn = function(r,rt) {
  164. if(wn.pages[page_name]) {
  165. // loaded
  166. var p = wn.pages[page_name]
  167. // show
  168. page_body.change_to(page_name);
  169. } else {
  170. // new page
  171. var p = render_page(page_name);
  172. if(!p)return;
  173. }
  174. // execute callback
  175. cur_page = page_name;
  176. if(call_back)call_back();
  177. // scroll to top
  178. scroll(0,0);
  179. // update "back"
  180. pscript.update_page_history(page_name, no_history)
  181. // call refresh script
  182. try {
  183. if(pscript['refresh_'+page_name]) pscript['refresh_'+page_name](); // onload
  184. } catch(e) {
  185. console.log(e);
  186. }
  187. }
  188. if(get_local('Page', page_name) || wn.pages[page_name])
  189. fn();
  190. else {
  191. args = get_url_dict(); // send everything to the page
  192. args.name = page_name;
  193. $c('webnotes.widgets.page.getpage', args, fn);
  194. }
  195. }
  196. //
  197. // adds to the url (if called using loadpage and not the url)
  198. // - if i do not do this then it will overwrite
  199. // this is useful when an argument is passed to the page separated by a /
  200. //
  201. pscript.update_page_history = function(page_name, no_history) {
  202. var arg = null;
  203. var t = null;
  204. // get from page
  205. if(window.location.hash) {
  206. var t = nav_obj.get_page(window.location.hash)
  207. } else if(get_url_arg('page')) {
  208. var t = nav_obj.get_page(get_url_arg('page'))
  209. }
  210. if(t && t[1]==page_name) arg = t[2];
  211. nav_obj.open_notify('Page', page_name, arg, no_history);
  212. }
  213. //
  214. // Load Script
  215. //
  216. function loadscript(src, call_back) {
  217. set_loading();
  218. var script = $a('head','script');
  219. script.type = 'text/javascript';
  220. script.src = src;
  221. script.onload = function() {
  222. if(call_back)call_back(); hide_loading();
  223. }
  224. // IE 6 & 7
  225. script.onreadystatechange = function() {
  226. if (this.readyState == 'complete' || this.readyState == 'loaded') {
  227. hide_loading();
  228. call_back();
  229. }
  230. }
  231. }
  232. // Load DocBrowser
  233. // -------------------------------------------------------------------------------
  234. var doc_browser_page;
  235. function loaddocbrowser(dt, label, fields) {
  236. //wn.require('lib/js/wn/pages/doclistview.js');
  237. //wn.pages.doclistview.show(dt);
  238. //return;
  239. wn.require('lib/js/legacy/webpage/docbrowser.js');
  240. dt = get_label_doctype(dt);
  241. if(!doc_browser_page)
  242. doc_browser_page = new ItemBrowserPage();
  243. doc_browser_page.show(dt, label, fields);
  244. nav_obj.open_notify('List',dt,'');
  245. }
  246. function loaddocbrowser2(dt, label, fields) {
  247. wn.pages.doclistview.show(dt);
  248. return;
  249. }