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.
 
 
 
 
 
 

270 lines
7.3 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. wn.dom.freeze();
  41. $c('runserverobj',
  42. args={'method':method,
  43. 'docs':wn.model.compress(make_doclist(doc.doctype, doc.name)),
  44. 'arg':arg
  45. },
  46. function(r, rt) {
  47. wn.dom.unfreeze();
  48. if (r.message) {
  49. var d = locals[dt][dn];
  50. var field_dict = r.message;
  51. for(var key in field_dict) {
  52. d[key] = field_dict[key];
  53. if (table_field)
  54. refresh_field(key, d.name, table_field);
  55. else
  56. refresh_field(key);
  57. }
  58. }
  59. if(call_back){
  60. doc = locals[doc.doctype][doc.name];
  61. call_back(doc, dt, dn);
  62. }
  63. }
  64. );
  65. }
  66. set_multiple = function (dt, dn, dict, table_field) {
  67. var d = locals[dt][dn];
  68. for(var key in dict) {
  69. d[key] = dict[key];
  70. if (table_field) refresh_field(key, d.name, table_field);
  71. else refresh_field(key);
  72. }
  73. }
  74. refresh_many = function (flist, dn, table_field) {
  75. for(var i in flist) {
  76. if (table_field) refresh_field(flist[i], dn, table_field);
  77. else refresh_field(flist[i]);
  78. }
  79. }
  80. set_field_tip = function(n,txt) {
  81. var df = wn.meta.get_docfield(cur_frm.doctype, n, cur_frm.docname);
  82. if(df)df.description = txt;
  83. if(cur_frm && cur_frm.fields_dict) {
  84. if(cur_frm.fields_dict[n])
  85. cur_frm.fields_dict[n].comment_area.innerHTML = replace_newlines(txt);
  86. else
  87. console.log('[set_field_tip] Unable to set field tip: ' + n);
  88. }
  89. }
  90. refresh_field = function(n, docname, table_field) {
  91. // multiple
  92. if(typeof n==typeof [])
  93. refresh_many(n, docname, table_field);
  94. if(table_field && cur_frm.fields_dict[table_field].grid.grid_rows_by_docname) { // for table
  95. cur_frm.fields_dict[table_field].grid.grid_rows_by_docname[docname].refresh_field(n);
  96. } else if(cur_frm) {
  97. cur_frm.refresh_field(n)
  98. }
  99. }
  100. set_field_options = function(n, txt) {
  101. cur_frm.set_df_property(n, 'options', txt)
  102. }
  103. set_field_permlevel = function(n, level) {
  104. cur_frm.set_df_property(n, 'permlevel', level)
  105. }
  106. toggle_field = function(n, hidden) {
  107. var df = wn.meta.get_docfield(cur_frm.doctype, n, cur_frm.docname);
  108. if(df) {
  109. df.hidden = hidden;
  110. refresh_field(n);
  111. }
  112. else {
  113. console.log((hidden ? "hide_field" : "unhide_field") + " cannot find field " + n);
  114. }
  115. }
  116. hide_field = function(n) {
  117. if(cur_frm) {
  118. if(n.substr) toggle_field(n, 1);
  119. else { for(var i in n) toggle_field(n[i], 1) }
  120. }
  121. }
  122. unhide_field = function(n) {
  123. if(cur_frm) {
  124. if(n.substr) toggle_field(n, 0);
  125. else { for(var i in n) toggle_field(n[i], 0) }
  126. }
  127. }
  128. get_field_obj = function(fn) {
  129. return cur_frm.fields_dict[fn];
  130. }
  131. // set missing values in given doc
  132. set_missing_values = function(doc, dict) {
  133. // dict contains fieldname as key and "default value" as value
  134. var fields_to_set = {};
  135. $.each(dict, function(i, v) { if (!doc[i]) { fields_to_set[i] = v; } });
  136. if (fields_to_set) { set_multiple(doc.doctype, doc.name, fields_to_set); }
  137. }
  138. _f.Frm.prototype.get_doc = function() {
  139. return locals[this.doctype][this.docname];
  140. }
  141. _f.Frm.prototype.get_doclist = function() {
  142. return make_doclist(this.doctype, this.docname);
  143. }
  144. _f.Frm.prototype.field_map = function(fnames, fn) {
  145. if(typeof fnames==='string') {
  146. if(fnames == '*') {
  147. fnames = keys(this.fields_dict);
  148. } else {
  149. fnames = [fnames];
  150. }
  151. }
  152. $.each(fnames, function(i,f) {
  153. //var field = cur_frm.fields_dict[f]; - much better design
  154. var field = wn.meta.get_docfield(cur_frm.doctype, f, cur_frm.docname)
  155. if(field) {
  156. fn(field);
  157. cur_frm.refresh_field(f);
  158. };
  159. })
  160. }
  161. _f.Frm.prototype.set_df_property = function(fieldname, property, value) {
  162. var field = wn.meta.get_docfield(cur_frm.doctype, fieldname, cur_frm.docname)
  163. if(field) {
  164. field[property] = value;
  165. cur_frm.refresh_field(fieldname);
  166. };
  167. }
  168. _f.Frm.prototype.toggle_enable = function(fnames, enable) {
  169. cur_frm.field_map(fnames, function(field) {
  170. field.read_only = enable ? 0 : 1; });
  171. }
  172. _f.Frm.prototype.toggle_reqd = function(fnames, mandatory) {
  173. cur_frm.field_map(fnames, function(field) { field.reqd = mandatory ? true : false; });
  174. }
  175. _f.Frm.prototype.toggle_display = function(fnames, show) {
  176. cur_frm.field_map(fnames, function(field) { field.hidden = show ? 0 : 1; });
  177. }
  178. _f.Frm.prototype.call_server = function(method, args, callback) {
  179. $c_obj(cur_frm.get_doclist(), method, args, callback);
  180. }
  181. _f.Frm.prototype.get_files = function() {
  182. return cur_frm.attachments
  183. ? keys(cur_frm.attachments.get_attachments()).sort()
  184. : [] ;
  185. }
  186. _f.Frm.prototype.set_query = function(fieldname, opt1, opt2) {
  187. var func = (typeof opt1=="function") ? opt1 : opt2;
  188. if(opt2) {
  189. this.fields_dict[opt1].grid.get_field(fieldname).get_query = func;
  190. } else {
  191. this.fields_dict[fieldname].get_query = func;
  192. }
  193. }
  194. _f.Frm.prototype.set_value = function(field, value) {
  195. var me = this;
  196. var _set = function(f, v) {
  197. if(me.fields_dict[f]) {
  198. me.doc[f] = v;
  199. me.fields_dict[f].refresh();
  200. }
  201. }
  202. if(typeof field=="string") {
  203. _set(field, value)
  204. } else if($.isPlainObject(field)) {
  205. $.each(field, function(f, v) {
  206. _set(f, v);
  207. })
  208. }
  209. }
  210. _f.Frm.prototype.call = function(opts) {
  211. var me = this;
  212. if(!opts.doc) {
  213. if(opts.method.indexOf(".")===-1)
  214. opts.method = wn.model.get_server_module_name(me.doctype) + "." + opts.method;
  215. opts.original_callback = opts.callback;
  216. opts.callback = function(r) {
  217. if($.isPlainObject(r.message)) {
  218. if(opts.child) {
  219. // update child doc
  220. opts.child = locals[opts.child.doctype][opts.child.name];
  221. $.extend(opts.child, r.message);
  222. me.fields_dict[opts.child.parentfield].refresh();
  223. } else {
  224. // update parent doc
  225. me.set_value(r.message);
  226. }
  227. }
  228. opts.original_callback && opts.original_callback(r);
  229. }
  230. }
  231. wn.call(opts);
  232. }
  233. _f.Frm.prototype.get_field = function(field) {
  234. return cur_frm.fields_dict[field];
  235. }