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.
 
 
 
 
 
 

157 lines
4.9 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. // Client Side Scripting API
  23. // ======================================================================================
  24. $c_get_values = function(args, doc, dt, dn, user_callback) {
  25. var call_back = function(r,rt) {
  26. if(!r.message)return;
  27. if(user_callback) user_callback(r.message);
  28. var fl = args.fields.split(',');
  29. for(var i in fl) {
  30. locals[dt][dn][fl[i]] = r.message[fl[i]]; // set value
  31. if(args.table_field)
  32. refresh_field(fl[i], dn, args.table_field);
  33. else
  34. refresh_field(fl[i]);
  35. }
  36. }
  37. $c('webnotes.widgets.form.utils.get_fields',args,call_back);
  38. }
  39. get_server_fields = function(method, arg, table_field, doc, dt, dn, allow_edit, call_back) {
  40. if(!allow_edit)freeze('Fetching Data...');
  41. $c('runserverobj', args={'method':method, 'docs':compress_doclist(make_doclist(doc.doctype, doc.name)), 'arg':arg},
  42. function(r, rt) {
  43. if (r.message) {
  44. var d = locals[dt][dn];
  45. var field_dict = r.message;
  46. for(var key in field_dict) {
  47. d[key] = field_dict[key];
  48. if (table_field) refresh_field(key, d.name, table_field);
  49. else refresh_field(key);
  50. }
  51. }
  52. if(call_back){
  53. doc = locals[doc.doctype][doc.name];
  54. call_back(doc, dt, dn);
  55. }
  56. if(!allow_edit)unfreeze();
  57. }
  58. );
  59. }
  60. set_multiple = function (dt, dn, dict, table_field) {
  61. var d = locals[dt][dn];
  62. for(var key in dict) {
  63. d[key] = dict[key];
  64. if (table_field) refresh_field(key, d.name, table_field);
  65. else refresh_field(key);
  66. }
  67. }
  68. refresh_many = function (flist, dn, table_field) {
  69. for(var i in flist) {
  70. if (table_field) refresh_field(flist[i], dn, table_field);
  71. else refresh_field(flist[i]);
  72. }
  73. }
  74. set_field_tip = function(n,txt) {
  75. var df = get_field(cur_frm.doctype, n, cur_frm.docname);
  76. if(df)df.description = txt;
  77. if(cur_frm && cur_frm.fields_dict) {
  78. if(cur_frm.fields_dict[n])
  79. cur_frm.fields_dict[n].comment_area.innerHTML = replace_newlines(txt);
  80. else
  81. errprint('[set_field_tip] Unable to set field tip: ' + n);
  82. }
  83. }
  84. refresh_field = function(n, docname, table_field) {
  85. // multiple
  86. if(typeof n==typeof []) refresh_many(n, docname, table_field);
  87. if(table_field) { // for table
  88. if(_f.frm_dialog && _f.frm_dialog.display) {
  89. // in dialog
  90. if(_f.frm_dialog.cur_frm.fields_dict[n] && _f.frm_dialog.cur_frm.fields_dict[n].refresh)
  91. _f.frm_dialog.cur_frm.fields_dict[n].refresh();
  92. } else {
  93. var g = _f.cur_grid_cell;
  94. if(g) var hc = g.grid.head_row.cells[g.cellIndex];
  95. if(g && hc && hc.fieldname==n && g.row.docname==docname) {
  96. hc.template.refresh(); // if active
  97. } else {
  98. cur_frm.fields_dict[table_field].grid.refresh_cell(docname, n);
  99. }
  100. }
  101. } else if(cur_frm && cur_frm.fields_dict) {
  102. if(cur_frm.fields_dict[n] && cur_frm.fields_dict[n].refresh)
  103. cur_frm.fields_dict[n].refresh();
  104. }
  105. }
  106. set_field_options = function(n, txt) {
  107. var df = get_field(cur_frm.doctype, n, cur_frm.docname);
  108. if(df)df.options = txt;
  109. refresh_field(n);
  110. }
  111. set_field_permlevel = function(n, level) {
  112. var df = get_field(cur_frm.doctype, n, cur_frm.docname);
  113. if(df)df.permlevel = level;
  114. refresh_field(n);
  115. }
  116. hide_field = function(n) {
  117. function _hide_field(n,hidden) {
  118. var df = get_field(cur_frm.doctype, n, cur_frm.docname);
  119. if(df) { df.hidden = hidden; refresh_field(n); }
  120. else { console.log("hide_field cannot find field " + n); }
  121. }
  122. if(cur_frm) {
  123. if(typeof n == 'string') _hide_field(n,1);
  124. else { for(var i in n)_hide_field(n[i],1) }
  125. }
  126. }
  127. unhide_field = function(n) {
  128. function _hide_field(n,hidden) {
  129. var df = get_field(cur_frm.doctype, n, cur_frm.docname);
  130. if(df) {df.hidden = hidden; refresh_field(n); }
  131. else { console.log("unhide_field cannot find field " + n); }
  132. }
  133. if(cur_frm) {
  134. if(typeof n == 'string') _hide_field(n,0);
  135. else { for(var i in n)_hide_field(n[i],0) }
  136. }
  137. }
  138. get_field_obj = function(fn) {
  139. return cur_frm.fields_dict[fn];
  140. }