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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. wn.ui.form.Layout = Class.extend({
  2. init: function(opts) {
  3. this.labelled_section_count = 0;
  4. this.ignore_types = ["Section Break", "Column Break"];
  5. $.extend(this, opts);
  6. this.make();
  7. this.render();
  8. },
  9. make: function() {
  10. this.wrapper = $('<div class="form-layout">').appendTo(this.parent);
  11. this.fields = wn.meta.get_docfields(this.frm.doctype, this.frm.docname);
  12. this.setup_tabbing();
  13. },
  14. refresh: function() {
  15. var me = this;
  16. $.each(this.frm.fields, function(i, fieldobj) {
  17. fieldobj.docname = me.frm.docname;
  18. fieldobj.df = wn.meta.get_docfield(me.frm.doctype,
  19. fieldobj.df.fieldname, me.frm.docname);
  20. fieldobj.refresh && fieldobj.refresh();
  21. })
  22. },
  23. render: function() {
  24. var me = this;
  25. this.section = null;
  26. this.column = null;
  27. if(this.fields[0] && this.fields[0].fieldtype!="Section Break") {
  28. this.make_section();
  29. }
  30. $.each(this.fields, function(i, df) {
  31. switch(df.fieldtype) {
  32. case "Section Break":
  33. me.make_section(df);
  34. break;
  35. case "Column Break":
  36. me.make_column(df);
  37. break;
  38. default:
  39. me.make_field(df);
  40. }
  41. });
  42. },
  43. make_column: function(df) {
  44. this.column = $('<div class="form-column">\
  45. <form>\
  46. <fieldset></fieldset>\
  47. </form>\
  48. </div>').appendTo(this.section.body)
  49. .find("form")
  50. .on("submit", function() { return false; })
  51. .find("fieldset");
  52. // distribute all columns equally
  53. var colspan = cint(12 / this.section.find(".form-column").length);
  54. this.section.find(".form-column").removeClass()
  55. .addClass("form-column")
  56. .addClass("col col-lg-" + colspan);
  57. },
  58. make_field: function(df, colspan) {
  59. !this.column && this.make_column();
  60. var fieldobj = make_field(df, this.doctype, this.column.get(0), this.frm);
  61. this.frm.fields.push(fieldobj);
  62. this.frm.fields_dict[df.fieldname] = fieldobj;
  63. fieldobj.perm = this.frm.perm;
  64. },
  65. make_section: function(df) {
  66. if(this.section) {
  67. //$("<hr>").appendTo(this.wrapper);
  68. }
  69. this.section = $('<div class="row">')
  70. .appendTo(this.wrapper);
  71. this.frm.sections.push(this.section);
  72. var section = this.section[0];
  73. section.df = df;
  74. if(df) {
  75. if(df.label) {
  76. this.labelled_section_count++;
  77. $('<h3 class="col col-lg-12">'
  78. + (df.options ? (' <i class="text-muted '+df.options+'"></i> ') : "")
  79. + '<span class="section-count-label">' + this.labelled_section_count + "</span>. "
  80. + wn._(df.label)
  81. + "</h3>")
  82. .css({
  83. "font-weight": "bold",
  84. })
  85. .appendTo(this.section);
  86. if(this.frm.sections.length > 1)
  87. this.section.css({
  88. "margin-top": "15px",
  89. "border-top": "1px solid #ddd"
  90. });
  91. }
  92. if(df.description) {
  93. $('<div class="col col-lg-12 small text-muted">' + df.description + '</div>').appendTo(this.section);
  94. }
  95. if(df.label || df.description) {
  96. // spacer
  97. $('<div class="col col-lg-12"></div>')
  98. .appendTo(this.section)
  99. .css({"height": "20px"});
  100. }
  101. this.frm.fields_dict[df.fieldname] = section;
  102. this.frm.fields.push(section);
  103. }
  104. // for bc
  105. this.section.body = $('<div style="padding: 0px 3%">').appendTo(this.section);
  106. section.row = {
  107. wrapper: section
  108. };
  109. section.refresh = function() {
  110. $(this).toggle(!this.df.hidden)
  111. }
  112. this.column = null;
  113. if(df && df.hidden) {
  114. this.section.toggle(false);
  115. }
  116. return this.section;
  117. },
  118. refresh_section_count: function() {
  119. this.wrapper.find(".section-count-label:visible").each(function(i) {
  120. $(this).html(i+1);
  121. });
  122. },
  123. setup_tabbing: function() {
  124. var me = this;
  125. this.wrapper.on("keydown", function(ev) {
  126. if(ev.which==9) {
  127. var current = $(ev.target).trigger("change"),
  128. doctype = current.attr("data-doctype"),
  129. fieldname = current.attr("data-fieldname");
  130. if(doctype)
  131. return me.handle_tab(doctype, fieldname, ev.shiftKey);
  132. }
  133. })
  134. },
  135. handle_tab: function(doctype, fieldname, shift) {
  136. var me = this,
  137. grid_row = null;
  138. prev = null,
  139. fields = me.frm.fields,
  140. in_grid = false;
  141. // in grid
  142. if(doctype != me.frm.doctype) {
  143. grid_row =me.get_open_grid_row()
  144. fields = grid_row.fields;
  145. }
  146. for(var i=0, len=fields.length; i < len; i++) {
  147. if(fields[i].df.fieldname==fieldname) {
  148. if(shift) {
  149. if(prev) {
  150. this.set_focus(prev)
  151. } else {
  152. $(cur_frm.wrapper).find(".btn-primary").focus();
  153. }
  154. break;
  155. }
  156. if(i==len-1) {
  157. // last field in this group
  158. if(grid_row) {
  159. // in grid
  160. if(grid_row.doc.idx==grid_row.grid.grid_rows.length) {
  161. // last row, close it and find next field
  162. grid_row.toggle_view(false, function() {
  163. me.handle_tab(grid_row.grid.df.parent, grid_row.grid.df.fieldname);
  164. })
  165. } else {
  166. // next row
  167. grid_row.grid.grid_rows[grid_row.doc.idx].toggle_view(true);
  168. }
  169. } else {
  170. $(cur_frm.wrapper).find(".btn-primary").focus();
  171. }
  172. } else {
  173. me.focus_on_next_field(i, fields);
  174. }
  175. break;
  176. }
  177. if(fields[i].disp_status==="Write")
  178. prev = fields[i];
  179. }
  180. return false;
  181. },
  182. focus_on_next_field: function(start_idx, fields) {
  183. // loop to find next eligible fields
  184. for(var i= start_idx + 1, len = fields.length; i < len; i++) {
  185. if(fields[i].disp_status==="Write" && !in_list(wn.model.no_value_type, fields[i].df.fieldtype)) {
  186. this.set_focus(fields[i]);
  187. break;
  188. }
  189. }
  190. },
  191. set_focus: function(field) {
  192. // next is table, show the table
  193. if(field.df.fieldtype=="Table") {
  194. if(!field.grid.grid_rows.length) {
  195. field.grid.add_new_row(1);
  196. } else {
  197. field.grid.grid_rows[0].toggle_view(true);
  198. }
  199. }
  200. else if(field.editor) {
  201. field.editor.set_focus();
  202. }
  203. else if(field.$input) {
  204. field.$input.focus();
  205. }
  206. },
  207. get_open_grid_row: function() {
  208. return $(".grid-row-open").data("grid_row");
  209. },
  210. // dashboard
  211. clear_dashboard: function() {
  212. this.dashboard.empty();
  213. },
  214. add_doctype_badge: function(doctype, fieldname) {
  215. if(wn.model.can_read(doctype)) {
  216. this.add_badge(wn._(doctype), function() {
  217. wn.route_options = {};
  218. wn.route_options[fieldname] = cur_frm.doc.name;
  219. wn.set_route("List", doctype);
  220. }).attr("data-doctype", doctype);
  221. }
  222. },
  223. add_badge: function(label, onclick) {
  224. var badge = $(repl('<div class="col col-lg-4">\
  225. <div class="alert alert-badge">\
  226. <a class="badge-link">%(label)s</a>\
  227. <span class="badge pull-right">-</span>\
  228. </div></div>', {label:label}))
  229. .appendTo(this.dashboard)
  230. badge.find(".badge-link").click(onclick);
  231. return badge.find(".alert-badge");
  232. },
  233. set_badge_count: function(data) {
  234. var me = this;
  235. $.each(data, function(doctype, count) {
  236. $(me.dashboard)
  237. .find(".alert-badge[data-doctype='"+doctype+"'] .badge")
  238. .html(cint(count));
  239. });
  240. },
  241. })