Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

print_format.js 17 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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. script_list = $(container).find("script");
  255. for(var i=0; i<script_list.length; i++) {
  256. var element = script_list[i];
  257. var code = element.innerHTML;
  258. var new_html = code ? (eval(code) || "") : "";
  259. if(typeof new_html=="string")
  260. $(element).replaceWith(new_html);
  261. }
  262. // var parent = jslist[i].parentNode;
  263. // var span = $a(parent, 'span');
  264. // parent.replaceChild(span, jslist[i]);
  265. // var val = code ? eval(code) : '';
  266. // if(!val || typeof(val)=='object') { val = ''; }
  267. // span.innerHTML = val;
  268. },
  269. // Attach letterhead at top of container
  270. show_letterhead: function(container, args) {
  271. if(!(args.no_letterhead || args.only_body)) {
  272. container.innerHTML = '<div>' + _p.get_letter_head() + '</div>'
  273. + container.innerHTML;
  274. }
  275. },
  276. render_header_on_break: function(container, args) {
  277. var page_set = container.getElementsByClassName('page-settings');
  278. if(page_set.length) {
  279. for(var i = 0; i < page_set.length; i++) {
  280. var tmp = '';
  281. // if draft/archived, show draft/archived banner
  282. tmp += _p.show_draft(args);
  283. tmp += _p.show_archived(args);
  284. _p.show_letterhead(page_set[i], args);
  285. page_set[i].innerHTML = tmp + page_set[i].innerHTML;
  286. }
  287. }
  288. },
  289. // called by _p.render for final render of print
  290. render_final: function(style, stat, container, args) {
  291. var header = '<div class="page-settings">\n';
  292. var footer = '\n</div>';
  293. if(!args.only_body) {
  294. header = '<!DOCTYPE html>\n\
  295. <html>\
  296. <head>\
  297. <title>' + args.title + '</title>\
  298. <style>' + style + '</style>\
  299. </head>\
  300. <body>\n' + header;
  301. footer = footer + '\n</body>\n\
  302. </html>';
  303. }
  304. var finished = header
  305. + stat
  306. + container.innerHTML
  307. + footer;
  308. // replace relative links by absolute links
  309. var prefix = window.location.href.split("app.html")[0]
  310. $.each(finished.match(/src=['"]([^'"]*)['"]/) || [], function(i, v) {
  311. if(v.substr(0,4)!="src=") {
  312. if(v.substr(0,4)!="http")
  313. finished = finished.replace(v, prefix + v);
  314. }
  315. });
  316. return finished;
  317. },
  318. // fetches letter head from current doc or control panel
  319. get_letter_head: function() {
  320. var cp = wn.control_panel;
  321. var lh = '';
  322. if(cur_frm.doc.letter_head) {
  323. lh = cstr(wn.boot.letter_heads[cur_frm.doc.letter_head]);
  324. } else if (cp.letter_head) {
  325. lh = cp.letter_head;
  326. }
  327. return lh;
  328. },
  329. // common print style setting
  330. print_style: "\
  331. .datalabelcell { \
  332. padding: 2px 0px; \
  333. width: 38%; \
  334. vertical-align: top; \
  335. } \
  336. .datainputcell { \
  337. padding: 2px 0px; \
  338. width: 62%; \
  339. text-align: left; \
  340. }\
  341. .sectionHeading { \
  342. font-size: 16px; \
  343. font-weight: bold; \
  344. margin: 8px 0px; \
  345. } \
  346. .columnHeading { \
  347. font-size: 14px; \
  348. font-weight: bold; \
  349. margin: 8px 0px; \
  350. }",
  351. print_std: function(no_letterhead) {
  352. // Get doctype, docname, layout for a doctype
  353. var docname = cur_frm.docname;
  354. var doctype = cur_frm.doctype;
  355. var data = getchildren('DocField', doctype, 'fields', 'DocType');
  356. var layout = _p.add_layout(doctype);
  357. this.pf_list = [layout];
  358. var me = this;
  359. me.layout = layout;
  360. $.extend(this, {
  361. build_head: function(data, doctype, docname) {
  362. // Heading
  363. var h1_style = {
  364. fontSize: '22px',
  365. marginBottom: '8px'
  366. }
  367. var h1 = $a(me.layout.cur_row.header, 'h1', '', h1_style);
  368. // Get print heading
  369. if (cur_frm.pformat[docname]) {
  370. // first check in cur_frm.pformat
  371. h1.innerHTML = cur_frm.pformat[docname];
  372. } else {
  373. // then check if select print heading exists and has a value
  374. var val = null;
  375. for (var i = 0; i < data.length; i++) {
  376. if (data[i].fieldname === 'select_print_heading') {
  377. val = _f.get_value(doctype, docname, data[i].fieldname);
  378. break;
  379. }
  380. }
  381. // if not, just have doctype has heading
  382. h1.innerHTML = val ? val : get_doctype_label(doctype);
  383. }
  384. var h2_style = {
  385. fontSize: '16px',
  386. color: '#888',
  387. marginBottom: '8px',
  388. paddingBottom: '8px',
  389. borderBottom: (me.layout.with_border ? '0px' :
  390. '1px solid #000')
  391. }
  392. var h2 = $a(me.layout.cur_row.header, 'div', '', h2_style);
  393. h2.innerHTML = docname;
  394. },
  395. build_data: function(data, doctype, docname) {
  396. // Start with a row and a cell in that row
  397. if(data[0] && data[0].fieldtype != "Section Break") {
  398. me.layout.addrow();
  399. if(data[0].fieldtype != "Column Break") {
  400. me.layout.addcell();
  401. }
  402. }
  403. $.extend(this, {
  404. generate_custom_html: function(field, doctype, docname) {
  405. var container = $a(me.layout.cur_cell, 'div');
  406. container.innerHTML = cur_frm.pformat[field.fieldname](locals[doctype][docname]);
  407. },
  408. render_normal: function(field, data, i) {
  409. switch(field.fieldtype) {
  410. case 'Section Break':
  411. me.layout.addrow();
  412. // Add column if no column break after this field
  413. if(data[i+1] && data[i+1].fieldtype !=
  414. 'Column Break') {
  415. me.layout.addcell();
  416. }
  417. break;
  418. case 'Column Break':
  419. me.layout.addcell(field.width, field.label);
  420. break;
  421. case 'Table':
  422. var table = print_table(
  423. doctype, // dt
  424. docname, // dn
  425. field.fieldname,
  426. field.options, // tabletype
  427. null, // cols
  428. null, // head_labels
  429. null, // widths
  430. null); // condition
  431. me.layout = _p.print_std_add_table(table, me.layout, me.pf_list, doctype, no_letterhead);
  432. break;
  433. case 'HTML':
  434. var div = $a(me.layout.cur_cell, 'div');
  435. div.innerHTML = field.options;
  436. break;
  437. case 'Code':
  438. var div = $a(me.layout.cur_cell, 'div');
  439. var val = _f.get_value(doctype, docname,
  440. field.fieldname);
  441. div.innerHTML = '<div>' + field.label +
  442. ': </div><pre style="font-family: Courier, Fixed;">' + (val ? val : '') +
  443. '</pre>';
  444. break;
  445. case 'Text Editor':
  446. var div = $a(me.layout.cur_cell, 'div');
  447. var val = _f.get_value(doctype, docname,
  448. field.fieldname);
  449. div.innerHTML = val ? val : '';
  450. break;
  451. default:
  452. // Add Cell Data
  453. _p.print_std_add_field(doctype, docname, field, me.layout);
  454. break;
  455. }
  456. }
  457. });
  458. // Then build each field
  459. for(var i = 0; i < data.length; i++) {
  460. var fieldname = data[i].fieldname ? data[i].fieldname :
  461. data[i].label;
  462. var field = fieldname ?
  463. wn.meta.get_docfield(doctype, fieldname, docname) : data[i];
  464. if(!field.print_hide) {
  465. if(cur_frm.pformat[field.fieldname]) {
  466. // If there is a custom method to generate the HTML, then use it
  467. this.generate_custom_html(field, doctype, docname);
  468. } else {
  469. // Do the normal rendering
  470. this.render_normal(field, data, i);
  471. }
  472. }
  473. }
  474. me.layout.close_borders();
  475. },
  476. build_html: function() {
  477. var html = '';
  478. for(var i = 0; i < me.pf_list.length; i++) {
  479. if(me.pf_list[i].wrapper) {
  480. html += me.pf_list[i].wrapper.innerHTML;
  481. } else if(me.pf_list[i].innerHTML) {
  482. html += me.pf_list[i].innerHTML;
  483. } else {
  484. html += me.pf_list[i];
  485. }
  486. }
  487. this.pf_list = [];
  488. return html;
  489. }
  490. });
  491. this.build_head(data, doctype, docname);
  492. this.build_data(data, doctype, docname);
  493. var html = this.build_html();
  494. return html;
  495. },
  496. add_layout: function(doctype) {
  497. var layout = new Layout();
  498. layout.addrow();
  499. if(locals['DocType'][doctype].print_outline == 'Yes') {
  500. layout.with_border = 1
  501. }
  502. return layout;
  503. },
  504. print_std_add_table: function(t, layout, pf_list, dt, no_letterhead) {
  505. if(t.appendChild) {
  506. // If only one table is passed
  507. layout.cur_cell.appendChild(t);
  508. } else {
  509. page_break = '\n\
  510. <div style = "page-break-after: always;" \
  511. class = "page_break"></div><div class="page-settings"></div>';
  512. // If a list of tables is passed
  513. for(var i = 0; i < t.length-1; i++) {
  514. // add to current page
  515. layout.cur_cell.appendChild(t[i]);
  516. layout.close_borders();
  517. pf_list.push(page_break);
  518. // Create new page
  519. layout = _p.add_layout(dt, no_letterhead);
  520. pf_list.push(layout);
  521. layout.addrow();
  522. layout.addcell();
  523. var div = $a(layout.cur_cell, 'div');
  524. div.innerHTML = 'Continued from previous page...';
  525. div.style.padding = '4px';
  526. }
  527. // Append last table
  528. layout.cur_cell.appendChild(t[t.length-1]);
  529. }
  530. return layout;
  531. },
  532. print_std_add_field: function(dt, dn, f, layout) {
  533. var val = _f.get_value(dt, dn, f.fieldname);
  534. if(f.fieldtype!='Button') {
  535. if(val || in_list(['Float', 'Int', 'Currency'], f.fieldtype)) {
  536. // If value or a numeric type then proceed
  537. // Add field table
  538. row = _p.field_tab(layout.cur_cell);
  539. // Add label
  540. row.cells[0].innerHTML = f.label ? f.label : f.fieldname;
  541. $s(row.cells[1], val, f.fieldtype);
  542. // left align currency in normal display
  543. if(f.fieldtype == 'Currency') {
  544. $y(row.cells[1], { textAlign: 'left' });
  545. }
  546. }
  547. }
  548. },
  549. field_tab: function(layout_cell) {
  550. var tab = $a(layout_cell, 'table', '', {width:'100%'});
  551. var row = tab.insertRow(0);
  552. _p.row = row; // Don't know this line's purpose
  553. row.insertCell(0);
  554. row.insertCell(1);
  555. row.cells[0].className = 'datalabelcell';
  556. row.cells[1].className = 'datainputcell';
  557. return row;
  558. }
  559. });