Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

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