您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

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