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.
 
 
 
 
 
 

233 lines
6.1 KiB

  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. + this.labelled_section_count + ". "
  80. + 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. $('<div class="col col-lg-12"></div>')
  97. .appendTo(this.section)
  98. .css({"height": "20px"});
  99. }
  100. this.frm.fields_dict[df.fieldname] = section;
  101. this.frm.fields.push(section);
  102. }
  103. // for bc
  104. this.section.body = $('<div style="padding: 0px 3%">').appendTo(this.section);
  105. section.row = {
  106. wrapper: section
  107. };
  108. section.refresh = function() {
  109. $(this).toggle(!this.df.hidden)
  110. }
  111. this.column = null;
  112. if(df && df.hidden) {
  113. this.section.toggle(false);
  114. }
  115. return this.section;
  116. },
  117. setup_tabbing: function() {
  118. var me = this;
  119. this.wrapper.on("keydown", function(ev) {
  120. if(ev.which==9) {
  121. var current = $(ev.target).trigger("change"),
  122. doctype = current.attr("data-doctype"),
  123. fieldname = current.attr("data-fieldname");
  124. if(doctype)
  125. return me.handle_tab(doctype, fieldname);
  126. }
  127. })
  128. },
  129. handle_tab: function(doctype, fieldname) {
  130. var me = this,
  131. grid_row = null;
  132. next = null,
  133. fields = me.frm.fields,
  134. in_grid = false;
  135. // in grid
  136. if(doctype != me.frm.doctype) {
  137. grid_row =me.get_open_grid_row()
  138. fields = grid_row.fields;
  139. }
  140. for(var i=0, len=fields.length; i < len; i++) {
  141. if(fields[i].df.fieldname==fieldname) {
  142. if(i==len-1) {
  143. // last field in this group
  144. if(grid_row) {
  145. // in grid
  146. if(grid_row.doc.idx==grid_row.grid.grid_rows.length) {
  147. // last row, close it and find next field
  148. grid_row.toggle_view(false, function() {
  149. me.handle_tab(grid_row.grid.df.parent, grid_row.grid.df.fieldname);
  150. })
  151. } else {
  152. // next row
  153. grid_row.grid.grid_rows[grid_row.doc.idx].toggle_view(true);
  154. }
  155. } else {
  156. // last field - to title buttons
  157. }
  158. } else {
  159. me.focus_on_next_field(i, fields);
  160. }
  161. break;
  162. }
  163. }
  164. return false;
  165. },
  166. focus_on_next_field: function(start_idx, fields) {
  167. // loop to find next eligible fields
  168. for(var ii= start_idx + 1, len = fields.length; ii < len; ii++) {
  169. if(fields[ii].disp_status=="Write") {
  170. var next = fields[ii];
  171. // next is table, show the table
  172. if(next.df.fieldtype=="Table") {
  173. if(!next.grid.grid_rows.length) {
  174. next.grid.add_new_row(1);
  175. } else {
  176. next.grid.grid_rows[0].toggle_view(true);
  177. }
  178. }
  179. else if(next.editor) {
  180. next.editor.set_focus();
  181. }
  182. else if(next.$input) {
  183. next.$input.focus();
  184. }
  185. break;
  186. }
  187. }
  188. },
  189. get_open_grid_row: function() {
  190. return $(".grid-row-open").data("grid_row");
  191. },
  192. // dashboard
  193. clear_dashboard: function() {
  194. this.dashboard.empty();
  195. },
  196. add_doctype_badge: function(doctype, fieldname) {
  197. if(wn.model.can_read(doctype)) {
  198. this.add_badge(wn._(doctype), function() {
  199. wn.route_options = {};
  200. wn.route_options[fieldname] = cur_frm.doc.name;
  201. wn.set_route("List", doctype);
  202. }).attr("data-doctype", doctype);
  203. }
  204. },
  205. add_badge: function(label, onclick) {
  206. var badge = $(repl('<div class="col col-lg-4">\
  207. <div class="alert alert-badge">\
  208. <a class="badge-link">%(label)s</a>\
  209. <span class="badge pull-right">-</span>\
  210. </div></div>', {label:label}))
  211. .appendTo(this.dashboard)
  212. badge.find(".badge-link").click(onclick);
  213. return badge.find(".alert-badge");
  214. },
  215. set_badge_count: function(data) {
  216. var me = this;
  217. $.each(data, function(doctype, count) {
  218. $(me.dashboard)
  219. .find(".alert-badge[data-doctype='"+doctype+"'] .badge")
  220. .html(cint(count));
  221. });
  222. },
  223. })