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

print_format.js 17 KiB

12 年前
12 年前
12 年前
12 年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  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(in_list(["string", "number"], typeof new_html)) {
  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. // find unique matches
  328. var matches = $.unique(finished.match(/src=['"]([^'"]*)['"]/g) || []);
  329. $.each(matches, function(i, v) {
  330. if(v.substr(0,4)=="src=") {
  331. var v = v.substr(5, v.length-6);
  332. if(v.substr(0,4)!="http")
  333. finished = finished.split(v).join(prefix + v);
  334. }
  335. });
  336. return finished;
  337. },
  338. // fetches letter head from current doc or control panel
  339. get_letter_head: function() {
  340. var cp = wn.control_panel;
  341. var lh = '';
  342. if(cur_frm.doc.letter_head) {
  343. lh = cstr(wn.boot.letter_heads[cur_frm.doc.letter_head]);
  344. } else if (cp.letter_head) {
  345. lh = cp.letter_head;
  346. }
  347. return lh;
  348. },
  349. // common print style setting
  350. print_style: "\
  351. .datalabelcell { \
  352. padding: 2px 0px; \
  353. width: 38%; \
  354. vertical-align: top; \
  355. } \
  356. .datainputcell { \
  357. padding: 2px 0px; \
  358. width: 62%; \
  359. text-align: left; \
  360. }\
  361. .sectionHeading { \
  362. font-size: 16px; \
  363. font-weight: bold; \
  364. margin: 8px 0px; \
  365. } \
  366. .columnHeading { \
  367. font-size: 14px; \
  368. font-weight: bold; \
  369. margin: 8px 0px; \
  370. }",
  371. print_std: function(no_letterhead) {
  372. // Get doctype, docname, layout for a doctype
  373. var docname = cur_frm.docname;
  374. var doctype = cur_frm.doctype;
  375. var data = getchildren('DocField', doctype, 'fields', 'DocType');
  376. var layout = _p.add_layout(doctype);
  377. this.pf_list = [layout];
  378. var me = this;
  379. me.layout = layout;
  380. $.extend(this, {
  381. build_head: function(data, doctype, docname) {
  382. // Heading
  383. var h1_style = {
  384. fontSize: '22px',
  385. marginBottom: '8px'
  386. }
  387. var h1 = $a(me.layout.cur_row.header, 'h1', '', h1_style);
  388. // Get print heading
  389. if (cur_frm.pformat[docname]) {
  390. // first check in cur_frm.pformat
  391. h1.innerHTML = cur_frm.pformat[docname];
  392. } else {
  393. // then check if select print heading exists and has a value
  394. var val = null;
  395. for (var i = 0; i < data.length; i++) {
  396. if (data[i].fieldname === 'select_print_heading') {
  397. val = _f.get_value(doctype, docname, data[i].fieldname);
  398. break;
  399. }
  400. }
  401. // if not, just have doctype has heading
  402. h1.innerHTML = val ? val : wn._(doctype);
  403. }
  404. var h2_style = {
  405. fontSize: '16px',
  406. color: '#888',
  407. marginBottom: '8px',
  408. paddingBottom: '8px',
  409. borderBottom: (me.layout.with_border ? '0px' :
  410. '1px solid #000')
  411. }
  412. var h2 = $a(me.layout.cur_row.header, 'div', '', h2_style);
  413. h2.innerHTML = docname;
  414. if(cur_frm.state_fieldname) {
  415. $a(h2, 'br');
  416. var span = $a(h2, 'span', '',
  417. {padding: "3px", color: "#fff", backgroundColor: "#777",
  418. display:"inline-block"});
  419. span.innerHTML = cur_frm.doc[cur_frm.state_fieldname];
  420. }
  421. },
  422. build_data: function(data, doctype, docname) {
  423. // Start with a row and a cell in that row
  424. if(data[0] && data[0].fieldtype != "Section Break") {
  425. me.layout.addrow();
  426. if(data[0].fieldtype != "Column Break") {
  427. me.layout.addcell();
  428. }
  429. }
  430. $.extend(this, {
  431. generate_custom_html: function(field, doctype, docname) {
  432. var container = $a(me.layout.cur_cell, 'div');
  433. container.innerHTML = cur_frm.pformat[field.fieldname](locals[doctype][docname]);
  434. },
  435. render_normal: function(field, data, i) {
  436. switch(field.fieldtype) {
  437. case 'Section Break':
  438. me.layout.addrow();
  439. // Add column if no column break after this field
  440. if(data[i+1] && data[i+1].fieldtype !=
  441. 'Column Break') {
  442. me.layout.addcell();
  443. }
  444. break;
  445. case 'Column Break':
  446. me.layout.addcell(field.width, field.label);
  447. break;
  448. case 'Table':
  449. var table = print_table(
  450. doctype, // dt
  451. docname, // dn
  452. field.fieldname,
  453. field.options, // tabletype
  454. null, // cols
  455. null, // head_labels
  456. null, // widths
  457. null); // condition
  458. me.layout = _p.print_std_add_table(table, me.layout, me.pf_list, doctype, no_letterhead);
  459. break;
  460. case 'HTML':
  461. var div = $a(me.layout.cur_cell, 'div');
  462. div.innerHTML = field.options;
  463. break;
  464. case 'Code':
  465. var div = $a(me.layout.cur_cell, 'div');
  466. var val = _f.get_value(doctype, docname,
  467. field.fieldname);
  468. div.innerHTML = '<div>' + field.label +
  469. ': </div><pre style="font-family: Courier, Fixed;">' + (val ? val : '') +
  470. '</pre>';
  471. break;
  472. case 'Text Editor':
  473. var div = $a(me.layout.cur_cell, 'div');
  474. var val = _f.get_value(doctype, docname,
  475. field.fieldname);
  476. div.innerHTML = val ? val : '';
  477. break;
  478. default:
  479. // Add Cell Data
  480. _p.print_std_add_field(doctype, docname, field, me.layout);
  481. break;
  482. }
  483. }
  484. });
  485. // Then build each field
  486. for(var i = 0; i < data.length; i++) {
  487. var fieldname = data[i].fieldname ? data[i].fieldname :
  488. data[i].label;
  489. var field = fieldname ?
  490. wn.meta.get_docfield(doctype, fieldname, docname) : data[i];
  491. if(!field.print_hide) {
  492. if(cur_frm.pformat[field.fieldname]) {
  493. // If there is a custom method to generate the HTML, then use it
  494. this.generate_custom_html(field, doctype, docname);
  495. } else {
  496. // Do the normal rendering
  497. this.render_normal(field, data, i);
  498. }
  499. }
  500. }
  501. me.layout.close_borders();
  502. },
  503. build_html: function() {
  504. var html = '';
  505. for(var i = 0; i < me.pf_list.length; i++) {
  506. if(me.pf_list[i].wrapper) {
  507. html += me.pf_list[i].wrapper.innerHTML;
  508. } else if(me.pf_list[i].innerHTML) {
  509. html += me.pf_list[i].innerHTML;
  510. } else {
  511. html += me.pf_list[i];
  512. }
  513. }
  514. this.pf_list = [];
  515. return html;
  516. }
  517. });
  518. this.build_head(data, doctype, docname);
  519. this.build_data(data, doctype, docname);
  520. var html = this.build_html();
  521. return html;
  522. },
  523. add_layout: function(doctype) {
  524. var layout = new Layout();
  525. layout.addrow();
  526. if(locals['DocType'][doctype].print_outline == 'Yes') {
  527. layout.with_border = 1
  528. }
  529. return layout;
  530. },
  531. print_std_add_table: function(t, layout, pf_list, dt, no_letterhead) {
  532. if(t.appendChild) {
  533. // If only one table is passed
  534. layout.cur_cell.appendChild(t);
  535. } else {
  536. page_break = '\n\
  537. <div style = "page-break-after: always;" \
  538. class = "page_break"></div><div class="page-settings"></div>';
  539. // If a list of tables is passed
  540. for(var i = 0; i < t.length-1; i++) {
  541. // add to current page
  542. layout.cur_cell.appendChild(t[i]);
  543. layout.close_borders();
  544. pf_list.push(page_break);
  545. // Create new page
  546. layout = _p.add_layout(dt, no_letterhead);
  547. pf_list.push(layout);
  548. layout.addrow();
  549. layout.addcell();
  550. var div = $a(layout.cur_cell, 'div');
  551. div.innerHTML = 'Continued from previous page...';
  552. div.style.padding = '4px';
  553. }
  554. // Append last table
  555. layout.cur_cell.appendChild(t[t.length-1]);
  556. }
  557. return layout;
  558. },
  559. print_std_add_field: function(dt, dn, f, layout) {
  560. var val = _f.get_value(dt, dn, f.fieldname);
  561. if(f.fieldtype!='Button') {
  562. if(val || in_list(['Float', 'Int', 'Currency'], f.fieldtype)) {
  563. // If value or a numeric type then proceed
  564. // Add field table
  565. row = _p.field_tab(layout.cur_cell);
  566. // Add label
  567. row.cells[0].innerHTML = f.label ? f.label : f.fieldname;
  568. row.cells[1].innerHTML = wn.format(val, f, {for_print: true});
  569. // left align currency in normal display
  570. if(f.fieldtype == 'Currency') {
  571. $y(row.cells[1], { textAlign: 'left' });
  572. }
  573. }
  574. }
  575. },
  576. field_tab: function(layout_cell) {
  577. var tab = $a(layout_cell, 'table', '', {width:'100%'});
  578. var row = tab.insertRow(0);
  579. _p.row = row; // Don't know this line's purpose
  580. row.insertCell(0);
  581. row.insertCell(1);
  582. row.cells[0].className = 'datalabelcell';
  583. row.cells[1].className = 'datainputcell';
  584. return row;
  585. }
  586. });