Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

print_format.js 17 KiB

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