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.

print_format.js 22 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  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. // default print style
  23. _p.def_print_style_body = "html, body, div, span, td { \
  24. font-family: Arial, Helvetica; font-size: 12px; }\
  25. pre { margin:0; padding:0;}"
  26. _p.def_print_style_other = "\n.simpletable, .noborder { \
  27. border-collapse: collapse; margin-bottom: 10px;}\
  28. .simpletable td {border: 1pt solid #777; vertical-align: top; padding: 4px; }\
  29. .noborder td { vertical-align: top; }"
  30. _p.go = function(html) {
  31. var d = document.createElement('div')
  32. d.innerHTML = html
  33. $(d).printElement();
  34. }
  35. _p.preview = function(html) {
  36. var w = window.open('');
  37. if(!w) return;
  38. w.document.write(html)
  39. w.document.close();
  40. }
  41. // _p can be referenced as this inside $.extend
  42. $.extend(_p, {
  43. show_dialog: function() {
  44. if(!_p.dialog) {
  45. _p.make_dialog();
  46. }
  47. _p.dialog.show();
  48. },
  49. make_dialog: function() {
  50. // Prepare Dialog Box Layout
  51. var d = new Dialog(
  52. 360, // w
  53. 140, // h
  54. 'Print Formats', // title
  55. [ // content
  56. ['HTML', 'Select'],
  57. ['Check', 'No Letterhead'],
  58. ['HTML', 'Buttons']
  59. ]);
  60. //d.widgets['No Letterhead'].checked = 1;
  61. // Print Button
  62. $btn(d.widgets.Buttons, 'Print', function() {
  63. _p.build(
  64. sel_val(cur_frm.print_sel), // fmtname
  65. _p.go, // onload
  66. d.widgets['No Letterhead'].checked // no_letterhead
  67. );
  68. },
  69. {
  70. cssFloat: 'right',
  71. marginBottom: '16px',
  72. marginLeft: '7px'
  73. }, 'green');
  74. // Print Preview
  75. $btn(d.widgets.Buttons, 'Preview', function() {
  76. _p.build(
  77. sel_val(cur_frm.print_sel), // fmtname
  78. _p.preview, // onload
  79. d.widgets['No Letterhead'].checked // no_letterhead
  80. );
  81. },
  82. {
  83. cssFloat: 'right',
  84. marginBottom: '16px'
  85. }, '');
  86. // Delete previous print format select list and Reload print format list from current form
  87. d.onshow = function() {
  88. var c = _p.dialog.widgets['Select'];
  89. if(c.cur_sel && c.cur_sel.parentNode == c) {
  90. c.removeChild(c.cur_sel);
  91. }
  92. c.appendChild(cur_frm.print_sel);
  93. c.cur_sel = cur_frm.print_sel;
  94. }
  95. _p.dialog = d;
  96. },
  97. // Define formats dict
  98. formats: {},
  99. /* args dict can contain:
  100. + fmtname --> print format name
  101. + onload
  102. + no_letterhead
  103. + only_body
  104. */
  105. build: function(fmtname, onload, no_letterhead, only_body) {
  106. if(!fmtname) {
  107. fmtname= "Standard";
  108. }
  109. args = {
  110. fmtname: fmtname,
  111. onload: onload,
  112. no_letterhead: no_letterhead,
  113. only_body: only_body
  114. };
  115. if(!cur_frm) {
  116. alert('No Document Selected');
  117. return;
  118. }
  119. // Get current doc (record)
  120. var doc = locals[cur_frm.doctype][cur_frm.docname];
  121. if(args.fmtname == 'Standard') {
  122. args.onload(_p.render({
  123. body: _p.print_std(args.no_letterhead),
  124. style: _p.print_style,
  125. doc: doc,
  126. title: doc.name,
  127. no_letterhead: args.no_letterhead,
  128. only_body: args.only_body
  129. }));
  130. } else {
  131. var print_format_doc = locals["Print Format"][args.fmtname];
  132. if(!print_format_doc) {
  133. msgprint("Unknown Print Format: " + args.fmtname);
  134. return;
  135. }
  136. args.onload(_p.render({
  137. body: print_format_doc.html,
  138. style: '',
  139. doc: doc,
  140. title: doc.name,
  141. no_letterhead: args.no_letterhead,
  142. only_body: args.only_body
  143. }));
  144. }
  145. },
  146. render: function(args) {
  147. var container = document.createElement('div');
  148. var stat = '';
  149. // if draft/archived, show draft/archived banner
  150. stat += _p.show_draft(args);
  151. stat += _p.show_archived(args);
  152. stat += _p.show_cancelled(args);
  153. // Append args.body's content as a child of container
  154. container.innerHTML = args.body;
  155. // Show letterhead?
  156. _p.show_letterhead(container, args);
  157. _p.run_embedded_js(container, args.doc);
  158. var style = _p.consolidate_css(container, args);
  159. _p.render_header_on_break(container, args);
  160. return _p.render_final(style, stat, container, args);
  161. },
  162. head_banner_format: function() {
  163. return "\
  164. <div style = '\
  165. text-align: center; \
  166. padding: 8px; \
  167. background-color: #CCC;'> \
  168. <div style = '\
  169. font-size: 20px; \
  170. font-weight: bold;'>\
  171. {{HEAD}}\
  172. </div>\
  173. {{DESCRIPTION}}\
  174. </div>"
  175. },
  176. /*
  177. Check if doc's status is not submitted (docstatus == 0)
  178. and submission is pending
  179. Display draft in header if true
  180. */
  181. show_draft: function(args) {
  182. var is_doctype_submittable = 0;
  183. var plist = locals['DocPerm'];
  184. for(var perm in plist) {
  185. var p = plist[perm];
  186. if((p.parent==args.doc.doctype) && (p.submit==1)){
  187. is_doctype_submittable = 1;
  188. break;
  189. }
  190. }
  191. if(args.doc && cint(args.doc.docstatus)==0 && is_doctype_submittable) {
  192. draft = _p.head_banner_format();
  193. draft = draft.replace("{{HEAD}}", "DRAFT");
  194. draft = draft.replace("{{DESCRIPTION}}", "This box will go away after the document is submitted.");
  195. return draft;
  196. } else {
  197. return "";
  198. }
  199. },
  200. /*
  201. Check if doc is archived
  202. Display archived in header if true
  203. */
  204. show_archived: function(args) {
  205. if(args.doc && args.doc.__archived) {
  206. archived = _p.head_banner_format();
  207. archived = archived.replace("{{HEAD}}", "ARCHIVED");
  208. archived = archived.replace("{{DESCRIPTION}}", "You must restore this document to make it editable.");
  209. return archived;
  210. } else {
  211. return "";
  212. }
  213. },
  214. /*
  215. Check if doc is cancelled
  216. Display cancelled in header if true
  217. */
  218. show_cancelled: function(args) {
  219. if(args.doc && args.doc.docstatus==2) {
  220. cancelled = _p.head_banner_format();
  221. cancelled = cancelled.replace("{{HEAD}}", "CANCELLED");
  222. cancelled = cancelled.replace("{{DESCRIPTION}}", "You must amend this document to make it editable.");
  223. return cancelled;
  224. } else {
  225. return "";
  226. }
  227. },
  228. consolidate_css: function(container, args) {
  229. // Extract <style> content from container
  230. var body_style = '';
  231. var style_list = container.getElementsByTagName('style');
  232. while(style_list && style_list.length>0) {
  233. for(i in style_list) {
  234. if(style_list[i] && style_list[i].innerHTML) {
  235. body_style += style_list[i].innerHTML;
  236. var parent = style_list[i].parentNode;
  237. if(parent) {
  238. parent.removeChild(style_list[i]);
  239. } else {
  240. container.removeChild(style_list[i]);
  241. }
  242. }
  243. }
  244. style_list = container.getElementsByTagName('style');
  245. }
  246. // Concatenate all styles
  247. //style_concat = _p.def_print_style_other + args.style + body_style;
  248. style_concat = (args.only_body ? '' : _p.def_print_style_body)
  249. + _p.def_print_style_other + args.style + body_style;
  250. return style_concat;
  251. },
  252. // This is used to calculate and substitude values in the HTML
  253. run_embedded_js: function(container, doc) {
  254. var jslist = container.getElementsByTagName('script');
  255. while(jslist && jslist.length > 0) {
  256. for(i in jslist) {
  257. if(jslist[i] && jslist[i].innerHTML) {
  258. var code = jslist[i].innerHTML;
  259. var parent = jslist[i].parentNode;
  260. var span = $a(parent, 'span');
  261. parent.replaceChild(span, jslist[i]);
  262. var val = code ? eval(code) : '';
  263. if(!val || typeof(val)=='object') { val = ''; }
  264. span.innerHTML = val;
  265. }
  266. }
  267. jslist = container.getElementsByTagName('script');
  268. }
  269. },
  270. // Attach letterhead at top of container
  271. show_letterhead: function(container, args) {
  272. if(!(args.no_letterhead || args.only_body)) {
  273. container.innerHTML = '<div>' + _p.get_letter_head() + '</div>'
  274. + container.innerHTML;
  275. }
  276. },
  277. render_header_on_break: function(container, args) {
  278. var page_set = container.getElementsByClassName('page-settings');
  279. if(page_set.length) {
  280. for(var i = 0; i < page_set.length; i++) {
  281. var tmp = '';
  282. // if draft/archived, show draft/archived banner
  283. tmp += _p.show_draft(args);
  284. tmp += _p.show_archived(args);
  285. _p.show_letterhead(page_set[i], args);
  286. page_set[i].innerHTML = tmp + page_set[i].innerHTML;
  287. }
  288. }
  289. },
  290. // called by _p.render for final render of print
  291. render_final: function(style, stat, container, args) {
  292. var header = '<div class="page-settings">\n';
  293. var footer = '\n</div>';
  294. if(!args.only_body) {
  295. header = '<!DOCTYPE html>\n\
  296. <html>\
  297. <head>\
  298. <title>' + args.title + '</title>\
  299. <style>' + style + '</style>\
  300. </head>\
  301. <body>\n' + header;
  302. footer = footer + '\n</body>\n\
  303. </html>';
  304. }
  305. var finished = header
  306. + stat
  307. + container.innerHTML.replace(/<div/g, '\n<div').replace(/<td/g, '\n<td')
  308. + footer;
  309. // replace relative links by absolute links
  310. var prefix = window.location.href.split("app.html")[0]
  311. $.each(finished.match(/src=['"]([^'"]*)['"]/) || [], function(i, v) {
  312. if(v.substr(0,4)!="src=") {
  313. if(v.substr(0,4)!="http")
  314. finished = finished.replace(v, prefix + v);
  315. }
  316. });
  317. return finished;
  318. },
  319. // fetches letter head from current doc or control panel
  320. get_letter_head: function() {
  321. var cp = wn.control_panel;
  322. var lh = '';
  323. if(cur_frm.doc.letter_head) {
  324. lh = cstr(wn.boot.letter_heads[cur_frm.doc.letter_head]);
  325. } else if (cp.letter_head) {
  326. lh = cp.letter_head;
  327. }
  328. return lh;
  329. },
  330. // common print style setting
  331. print_style: "\
  332. .datalabelcell { \
  333. padding: 2px 0px; \
  334. width: 38%; \
  335. vertical-align: top; \
  336. } \
  337. .datainputcell { \
  338. padding: 2px 0px; \
  339. width: 62%; \
  340. text-align: left; \
  341. }\
  342. .sectionHeading { \
  343. font-size: 16px; \
  344. font-weight: bold; \
  345. margin: 8px 0px; \
  346. } \
  347. .columnHeading { \
  348. font-size: 14px; \
  349. font-weight: bold; \
  350. margin: 8px 0px; \
  351. }",
  352. print_std: function(no_letterhead) {
  353. // Get doctype, docname, layout for a doctype
  354. var docname = cur_frm.docname;
  355. var doctype = cur_frm.doctype;
  356. var data = getchildren('DocField', doctype, 'fields', 'DocType');
  357. var layout = _p.add_layout(doctype);
  358. this.pf_list = [layout];
  359. var me = this;
  360. me.layout = layout;
  361. $.extend(this, {
  362. build_head: function(data, doctype, docname) {
  363. // Heading
  364. var h1_style = {
  365. fontSize: '22px',
  366. marginBottom: '8px'
  367. }
  368. var h1 = $a(me.layout.cur_row.header, 'h1', '', h1_style);
  369. // Get print heading
  370. if (cur_frm.pformat[docname]) {
  371. // first check in cur_frm.pformat
  372. h1.innerHTML = cur_frm.pformat[docname];
  373. } else {
  374. // then check if select print heading exists and has a value
  375. var val = null;
  376. for (var i = 0; i < data.length; i++) {
  377. if (data[i].fieldname === 'select_print_heading') {
  378. val = _f.get_value(doctype, docname, data[i].fieldname);
  379. break;
  380. }
  381. }
  382. // if not, just have doctype has heading
  383. h1.innerHTML = val ? val : get_doctype_label(doctype);
  384. }
  385. var h2_style = {
  386. fontSize: '16px',
  387. color: '#888',
  388. marginBottom: '8px',
  389. paddingBottom: '8px',
  390. borderBottom: (me.layout.with_border ? '0px' :
  391. '1px solid #000')
  392. }
  393. var h2 = $a(me.layout.cur_row.header, 'div', '', h2_style);
  394. h2.innerHTML = docname;
  395. },
  396. build_data: function(data, doctype, docname) {
  397. // Start with a row and a cell in that row
  398. if(data[0] && data[0].fieldtype != "Section Break") {
  399. me.layout.addrow();
  400. if(data[0].fieldtype != "Column Break") {
  401. me.layout.addcell();
  402. }
  403. }
  404. $.extend(this, {
  405. generate_custom_html: function(field, doctype, docname) {
  406. var container = $a(me.layout.cur_cell, 'div');
  407. container.innerHTML = cur_frm.pformat[field.fieldname](locals[doctype][docname]);
  408. },
  409. render_normal: function(field, data, i) {
  410. switch(field.fieldtype) {
  411. case 'Section Break':
  412. me.layout.addrow();
  413. // Add column if no column break after this field
  414. if(data[i+1] && data[i+1].fieldtype !=
  415. 'Column Break') {
  416. me.layout.addcell();
  417. }
  418. break;
  419. case 'Column Break':
  420. me.layout.addcell(field.width, field.label);
  421. break;
  422. case 'Table':
  423. var table = print_table(
  424. doctype, // dt
  425. docname, // dn
  426. field.fieldname,
  427. field.options, // tabletype
  428. null, // cols
  429. null, // head_labels
  430. null, // widths
  431. null); // condition
  432. me.layout = _p.print_std_add_table(table, me.layout, me.pf_list, doctype, no_letterhead);
  433. break;
  434. case 'HTML':
  435. var div = $a(me.layout.cur_cell, 'div');
  436. div.innerHTML = field.options;
  437. break;
  438. case 'Code':
  439. var div = $a(me.layout.cur_cell, 'div');
  440. var val = _f.get_value(doctype, docname,
  441. field.fieldname);
  442. div.innerHTML = '<div>' + field.label +
  443. ': </div><pre style="font-family: Courier, Fixed;">' + (val ? val : '') +
  444. '</pre>';
  445. break;
  446. case 'Text Editor':
  447. var div = $a(me.layout.cur_cell, 'div');
  448. var val = _f.get_value(doctype, docname,
  449. field.fieldname);
  450. div.innerHTML = val ? val : '';
  451. break;
  452. default:
  453. // Add Cell Data
  454. _p.print_std_add_field(doctype, docname, field, me.layout);
  455. break;
  456. }
  457. }
  458. });
  459. // Then build each field
  460. for(var i = 0; i < data.length; i++) {
  461. var fieldname = data[i].fieldname ? data[i].fieldname :
  462. data[i].label;
  463. var field = fieldname ?
  464. wn.meta.get_docfield(doctype, fieldname, docname) : data[i];
  465. if(!field.print_hide) {
  466. if(cur_frm.pformat[field.fieldname]) {
  467. // If there is a custom method to generate the HTML, then use it
  468. this.generate_custom_html(field, doctype, docname);
  469. } else {
  470. // Do the normal rendering
  471. this.render_normal(field, data, i);
  472. }
  473. }
  474. }
  475. me.layout.close_borders();
  476. },
  477. build_html: function() {
  478. var html = '';
  479. for(var i = 0; i < me.pf_list.length; i++) {
  480. if(me.pf_list[i].wrapper) {
  481. html += me.pf_list[i].wrapper.innerHTML;
  482. } else if(me.pf_list[i].innerHTML) {
  483. html += me.pf_list[i].innerHTML;
  484. } else {
  485. html += me.pf_list[i];
  486. }
  487. }
  488. this.pf_list = [];
  489. return html;
  490. }
  491. });
  492. this.build_head(data, doctype, docname);
  493. this.build_data(data, doctype, docname);
  494. var html = this.build_html();
  495. return html;
  496. },
  497. add_layout: function(doctype) {
  498. var layout = new Layout();
  499. layout.addrow();
  500. if(locals['DocType'][doctype].print_outline == 'Yes') {
  501. layout.with_border = 1
  502. }
  503. return layout;
  504. },
  505. print_std_add_table: function(t, layout, pf_list, dt, no_letterhead) {
  506. if(t.appendChild) {
  507. // If only one table is passed
  508. layout.cur_cell.appendChild(t);
  509. } else {
  510. page_break = '\n\
  511. <div style = "page-break-after: always;" \
  512. class = "page_break"></div><div class="page-settings"></div>';
  513. // If a list of tables is passed
  514. for(var i = 0; i < t.length-1; i++) {
  515. // add to current page
  516. layout.cur_cell.appendChild(t[i]);
  517. layout.close_borders();
  518. pf_list.push(page_break);
  519. // Create new page
  520. layout = _p.add_layout(dt, no_letterhead);
  521. pf_list.push(layout);
  522. layout.addrow();
  523. layout.addcell();
  524. var div = $a(layout.cur_cell, 'div');
  525. div.innerHTML = 'Continued from previous page...';
  526. div.style.padding = '4px';
  527. }
  528. // Append last table
  529. layout.cur_cell.appendChild(t[t.length-1]);
  530. }
  531. return layout;
  532. },
  533. print_std_add_field: function(dt, dn, f, layout) {
  534. var val = _f.get_value(dt, dn, f.fieldname);
  535. if(f.fieldtype!='Button') {
  536. if(val || in_list(['Float', 'Int', 'Currency'], f.fieldtype)) {
  537. // If value or a numeric type then proceed
  538. // Add field table
  539. row = _p.field_tab(layout.cur_cell);
  540. // Add label
  541. row.cells[0].innerHTML = f.label ? f.label : f.fieldname;
  542. $s(row.cells[1], val, f.fieldtype);
  543. // left align currency in normal display
  544. if(f.fieldtype == 'Currency') {
  545. $y(row.cells[1], { textAlign: 'left' });
  546. }
  547. }
  548. }
  549. },
  550. field_tab: function(layout_cell) {
  551. var tab = $a(layout_cell, 'table', '', {width:'100%'});
  552. var row = tab.insertRow(0);
  553. _p.row = row; // Don't know this line's purpose
  554. row.insertCell(0);
  555. row.insertCell(1);
  556. row.cells[0].className = 'datalabelcell';
  557. row.cells[1].className = 'datainputcell';
  558. return row;
  559. }
  560. });
  561. print_table = function(dt, dn, fieldname, tabletype, cols, head_labels, widths, condition, cssClass, modifier, hide_empty) {
  562. var me = this;
  563. $.extend(this, {
  564. flist: (function() {
  565. var f_list = [];
  566. var fl = wn.meta.docfield_list[tabletype];
  567. if(fl) {
  568. for(var i=0; i<fl.length; i++) {
  569. f_list.push(copy_dict(fl[i]));
  570. }
  571. }
  572. return f_list;
  573. })(),
  574. data: function() {
  575. var children = getchildren(
  576. tabletype, // child_dt
  577. dn, // parent
  578. fieldname, // parentfield
  579. dt // parenttype
  580. );
  581. var data = []
  582. for(var i=0; i<children.length; i++) {
  583. data.push(copy_dict(children[i]));
  584. }
  585. return data;
  586. }(),
  587. cell_style: {
  588. border: '1px solid #999',
  589. padding: '3px',
  590. verticalAlign: 'top'
  591. },
  592. head_cell_style: {
  593. border: '1px solid #999',
  594. padding: '3px',
  595. verticalAlign: 'top',
  596. backgroundColor: '#ddd',
  597. fontWeight: 'bold'
  598. },
  599. table_style: {
  600. width: '100%',
  601. borderCollapse: 'collapse',
  602. marginBottom: '10px',
  603. marginTop: '10px'
  604. },
  605. remove_empty_cols: function(flist) {
  606. var non_empty_cols = []
  607. for(var i=0; i<me.data.length; i++) {
  608. for(var c=0; c<flist.length; c++) {
  609. if(flist[c].print_hide || !inList(['', null], me.data[i][flist[c].fieldname])) {
  610. if(!inList(non_empty_cols, flist[c])) {
  611. non_empty_cols.push(flist[c]);
  612. }
  613. }
  614. }
  615. }
  616. for(var c=0; c<flist.length; c++) {
  617. if(!inList(non_empty_cols, flist[c])) {
  618. flist.splice(c, 1);
  619. c = c - 1;
  620. }
  621. }
  622. },
  623. /*
  624. This function prepares a list of columns to be displayed and calls make_print_table to create a table with these columns
  625. */
  626. prepare_col_heads: function(flist) {
  627. var new_flist = [];
  628. if(!cols || (cols && cols.length && hide_empty)) {
  629. me.remove_empty_cols(flist);
  630. }
  631. // Make a list of column headings
  632. if(cols && cols.length) {
  633. // If cols to be displayed are passed in print_table
  634. if(cols[0] == 'SR') { new_flist.push('SR') }
  635. for(var i = 0; i < cols.length; i++) {
  636. for(var j = 0; j < flist.length; j++) {
  637. if(flist[j].fieldname == cols[i]) {
  638. new_flist.push(flist[j]);
  639. break;
  640. }
  641. }
  642. }
  643. } else {
  644. // Default action: remove hidden cols
  645. new_flist.push('SR');
  646. for(var i = 0; i < flist.length; i++) {
  647. if(!flist[i].print_hide) {
  648. new_flist.push(flist[i]);
  649. }
  650. }
  651. }
  652. // Changing me.flist so that it could be used to hide data
  653. me.flist = new_flist;
  654. },
  655. // This function makes a new table with its heading rows
  656. make_print_table: function(flist) {
  657. // Make a table
  658. var wrapper = document.createElement('div');
  659. var table = $a(wrapper, 'table', '', me.table_style);
  660. table.wrapper = wrapper;
  661. // Make Head Row
  662. table.insertRow(0);
  663. var col_start = 0;
  664. // If 'SR' exists in flist, then create its heading column cell
  665. if(flist[0]=='SR') {
  666. var cell = table.rows[0].insertCell(0);
  667. cell.innerHTML = head_labels?head_labels[0]:'<b>SR</b>';
  668. $y(cell, { width: '30px' });
  669. $y(cell, me.head_cell_style);
  670. col_start++;
  671. }
  672. for(var c = col_start; c < flist.length; c++) {
  673. var cell = table.rows[0].insertCell(c);
  674. $y(cell, me.head_cell_style);
  675. cell.innerHTML = head_labels?head_labels[c]:flist[c].label;
  676. if(flist[c].width) { $y(cell, {width: flist[c].width}); }
  677. if(widths) { $y(cell, {width: widths[c]}); }
  678. if(in_list(['Currency', 'Float'], flist[c].fieldtype)) {
  679. $y(cell, { textAlign: 'right' });
  680. }
  681. }
  682. return table;
  683. },
  684. // Populate table with data
  685. populate_table: function(table, data) {
  686. for(var r = 0; r < data.length; r++) {
  687. if((!condition) || (condition(data[r]))) {
  688. // Check for page break
  689. if(data[r].page_break) {
  690. table = me.make_print_table(me.flist);
  691. me.table_list.push(table.wrapper);
  692. }
  693. var row = table.insertRow(table.rows.length);
  694. // Add serial number if required
  695. if(me.flist[0] == 'SR') {
  696. var cell = row.insertCell(0);
  697. cell.innerHTML = r + 1;
  698. $y(cell, me.cell_style);
  699. }
  700. for(var c=me.flist.indexOf('SR')+1; c<me.flist.length; c++){
  701. var cell = row.insertCell(c);
  702. $y(cell, me.cell_style);
  703. if(modifier && me.flist[c].fieldname in modifier) {
  704. data[r][me.flist[c].fieldname] = modifier[me.flist[c].fieldname](data[r]);
  705. }
  706. $s(cell, data[r][me.flist[c].fieldname],
  707. me.flist[c].fieldtype);
  708. if(in_list(['Currency', 'Float'], me.flist[c].fieldtype)) {
  709. cell.style.textAlign = 'right';
  710. }
  711. }
  712. }
  713. }
  714. }
  715. });
  716. // If no data, do not create table
  717. if(!this.data.length) { return document.createElement('div'); }
  718. this.prepare_col_heads(this.flist);
  719. var table = me.make_print_table(this.flist);
  720. this.table_list = [table.wrapper];
  721. this.populate_table(table, this.data);
  722. // If multiple tables exists, send whole list, else send only one table
  723. return (me.table_list.length > 1) ? me.table_list : me.table_list[0];
  724. }