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

print_format.js 18 KiB

12 年前
12 年前
12 年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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, p { \
  24. font-family: inherit; \
  25. font-size: inherit; \
  26. }\
  27. .page-settings {\
  28. font-family: Arial, Helvetica Neue, Sans;\
  29. font-size: 9pt;\
  30. }\
  31. pre { margin:0; padding:0;}";
  32. _p.def_print_style_other = "\n.simpletable, .noborder { \
  33. border-collapse: collapse;\
  34. margin-bottom: 10px;\
  35. }\
  36. .simpletable td {\
  37. border: 1pt solid #777;\
  38. vertical-align: top;\
  39. padding: 4px;\
  40. }\
  41. .noborder td {\
  42. vertical-align: top;\
  43. }";
  44. _p.go = function(html) {
  45. var w = _p.preview(html);
  46. w.print();
  47. w.close();
  48. }
  49. _p.preview = function(html) {
  50. var w = window.open();
  51. if(!w) {
  52. msgprint(_("Please enable pop-ups"));
  53. return;
  54. }
  55. w.document.write(html);
  56. return w
  57. }
  58. // _p can be referenced as this inside $.extend
  59. $.extend(_p, {
  60. show_dialog: function() {
  61. if(!_p.dialog) {
  62. _p.make_dialog();
  63. }
  64. _p.dialog.show();
  65. },
  66. make_dialog: function() {
  67. // Prepare Dialog Box Layout
  68. var dialog = new wn.ui.Dialog({
  69. title: "Print Formats",
  70. fields: [
  71. {fieldtype:"Select", label:"Print Format", fieldname:"print_format", reqd:1},
  72. {fieldtype:"Check", label:"No Letter Head", fieldname:"no_letterhead"},
  73. {fieldtype:"HTML", options: '<p style="text-align: right;">\
  74. <button class="btn btn-primary btn-print">Print</button>\
  75. <button class="btn btn-default btn-preview">Preview</button>\
  76. </p>'},
  77. ]
  78. })
  79. dialog.$wrapper.find(".btn-print").click(function() {
  80. var args = dialog.get_values();
  81. _p.build(
  82. args.print_format, // fmtname
  83. _p.go, // onload
  84. args.no_letterhead // no_letterhead
  85. );
  86. });
  87. dialog.$wrapper.find(".btn-preview").click(function() {
  88. var args = dialog.get_values();
  89. _p.build(
  90. args.print_format, // fmtname
  91. _p.preview, // onload
  92. args.no_letterhead // no_letterhead
  93. );
  94. });
  95. dialog.onshow = function() {
  96. var $print = dialog.fields_dict.print_format.$input;
  97. $print.add_options(cur_frm.print_formats);
  98. if(cur_frm.$print_view_select && cur_frm.$print_view_select.val())
  99. $print.val(cur_frm.$print_view_select.val());
  100. }
  101. _p.dialog = dialog;
  102. },
  103. // Define formats dict
  104. formats: {},
  105. /* args dict can contain:
  106. + fmtname --> print format name
  107. + onload
  108. + no_letterhead
  109. + only_body
  110. */
  111. build: function(fmtname, onload, no_letterhead, only_body, no_heading) {
  112. if(!fmtname) {
  113. fmtname= "Standard";
  114. }
  115. args = {
  116. fmtname: fmtname,
  117. onload: onload,
  118. no_letterhead: no_letterhead,
  119. only_body: only_body
  120. };
  121. if(!cur_frm) {
  122. alert('No Document Selected');
  123. return;
  124. }
  125. // Get current doc (record)
  126. var doc = locals[cur_frm.doctype][cur_frm.docname];
  127. if(args.fmtname == 'Standard') {
  128. args.onload(_p.render({
  129. body: _p.print_std(args.no_letterhead, no_heading),
  130. style: _p.print_style,
  131. doc: doc,
  132. title: doc.name,
  133. no_letterhead: args.no_letterhead,
  134. no_heading: no_heading,
  135. only_body: args.only_body
  136. }));
  137. } else {
  138. var print_format_doc = locals["Print Format"][args.fmtname];
  139. if(!print_format_doc) {
  140. msgprint("Unknown Print Format: " + args.fmtname);
  141. return;
  142. }
  143. args.onload(_p.render({
  144. body: print_format_doc.html,
  145. style: '',
  146. doc: doc,
  147. title: doc.name,
  148. no_letterhead: args.no_letterhead,
  149. no_heading: no_heading,
  150. only_body: args.only_body
  151. }));
  152. }
  153. },
  154. render: function(args) {
  155. var container = document.createElement('div');
  156. var stat = '';
  157. if(!args.no_heading) {
  158. // if draft/archived, show draft/archived banner
  159. stat += _p.show_draft(args);
  160. stat += _p.show_archived(args);
  161. stat += _p.show_cancelled(args);
  162. }
  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. if(!args.only_body) {
  309. var header = '<!DOCTYPE html>\
  310. <html>\
  311. <head>\
  312. <meta charset="utf-8" />\
  313. <title>' + args.title + '</title>\
  314. <style>' + style + '</style>\
  315. </head>\
  316. <body>';
  317. var footer = '\
  318. </body>\
  319. </html>';
  320. } else {
  321. var header = '';
  322. var footer = '';
  323. }
  324. var finished = header
  325. + '<div class="page-settings">'
  326. + stat
  327. + container.innerHTML
  328. + '</div>'
  329. + footer;
  330. // replace relative links by absolute links
  331. var prefix = window.location.href.split("app.html")[0]
  332. // find unique matches
  333. var matches = $.unique(finished.match(/src=['"]([^'"]*)['"]/g) || []);
  334. $.each(matches, function(i, v) {
  335. if(v.substr(0,4)=="src=") {
  336. var v = v.substr(5, v.length-6);
  337. if(v.substr(0,4)!="http")
  338. finished = finished.split(v).join(prefix + v);
  339. }
  340. });
  341. return finished;
  342. },
  343. // fetches letter head from current doc or control panel
  344. get_letter_head: function() {
  345. var cp = wn.control_panel;
  346. var lh = '';
  347. if(cur_frm.doc.letter_head) {
  348. lh = cstr(wn.boot.letter_heads[cur_frm.doc.letter_head]);
  349. } else if (cp.letter_head) {
  350. lh = cp.letter_head;
  351. }
  352. return lh;
  353. },
  354. // common print style setting
  355. print_style: "\
  356. .datalabelcell { \
  357. padding: 2px 0px; \
  358. width: 38%; \
  359. vertical-align: top; \
  360. } \
  361. .datainputcell { \
  362. padding: 2px 0px; \
  363. width: 62%; \
  364. text-align: left; \
  365. }\
  366. .sectionHeading { \
  367. font-size: 16px; \
  368. font-weight: bold; \
  369. margin: 8px 0px; \
  370. } \
  371. .columnHeading { \
  372. font-size: 14px; \
  373. font-weight: bold; \
  374. margin: 8px 0px; \
  375. }",
  376. print_std: function(no_letterhead, no_heading) {
  377. // Get doctype, docname, layout for a doctype
  378. var docname = cur_frm.docname;
  379. var doctype = cur_frm.doctype;
  380. var data = getchildren('DocField', doctype, 'fields', 'DocType');
  381. var layout = _p.add_layout(doctype);
  382. this.pf_list = [layout];
  383. var me = this;
  384. me.layout = layout;
  385. $.extend(this, {
  386. build_head: function(data, doctype, docname) {
  387. // Heading
  388. var h1_style = {
  389. fontSize: '22px',
  390. marginBottom: '8px'
  391. }
  392. var h1 = $a(me.layout.cur_row.header, 'h1', '', h1_style);
  393. // Get print heading
  394. if (cur_frm.pformat[docname]) {
  395. // first check in cur_frm.pformat
  396. h1.innerHTML = cur_frm.pformat[docname];
  397. } else {
  398. // then check if select print heading exists and has a value
  399. var val = null;
  400. for (var i = 0; i < data.length; i++) {
  401. if (data[i].fieldname === 'select_print_heading') {
  402. val = _f.get_value(doctype, docname, data[i].fieldname);
  403. break;
  404. }
  405. }
  406. // if not, just have doctype has heading
  407. h1.innerHTML = val ? val : wn._(doctype);
  408. }
  409. var h2_style = {
  410. fontSize: '16px',
  411. color: '#888',
  412. marginBottom: '8px',
  413. paddingBottom: '8px',
  414. borderBottom: (me.layout.with_border ? '0px' :
  415. '1px solid #000')
  416. }
  417. var h2 = $a(me.layout.cur_row.header, 'div', '', h2_style);
  418. h2.innerHTML = docname;
  419. if(cur_frm.state_fieldname) {
  420. $a(h2, 'br');
  421. var span = $a(h2, 'span', '',
  422. {padding: "3px", color: "#fff", backgroundColor: "#777",
  423. display:"inline-block"});
  424. span.innerHTML = cur_frm.doc[cur_frm.state_fieldname];
  425. }
  426. },
  427. build_data: function(data, doctype, docname) {
  428. // Start with a row and a cell in that row
  429. if(data[0] && data[0].fieldtype != "Section Break") {
  430. me.layout.addrow();
  431. if(data[0].fieldtype != "Column Break") {
  432. me.layout.addcell();
  433. }
  434. }
  435. $.extend(this, {
  436. generate_custom_html: function(field, doctype, docname) {
  437. var container = $a(me.layout.cur_cell, 'div');
  438. container.innerHTML = cur_frm.pformat[field.fieldname](locals[doctype][docname]);
  439. },
  440. render_normal: function(field, data, i) {
  441. switch(field.fieldtype) {
  442. case 'Section Break':
  443. me.layout.addrow();
  444. // Add column if no column break after this field
  445. if(data[i+1] && data[i+1].fieldtype !=
  446. 'Column Break') {
  447. me.layout.addcell();
  448. }
  449. break;
  450. case 'Column Break':
  451. me.layout.addcell(field.width, field.label);
  452. break;
  453. case 'Table':
  454. var table = print_table(
  455. doctype, // dt
  456. docname, // dn
  457. field.fieldname,
  458. field.options, // tabletype
  459. null, // cols
  460. null, // head_labels
  461. null, // widths
  462. null); // condition
  463. me.layout = _p.print_std_add_table(table, me.layout, me.pf_list, doctype, no_letterhead);
  464. break;
  465. case 'HTML':
  466. var div = $a(me.layout.cur_cell, 'div');
  467. div.innerHTML = field.options;
  468. break;
  469. case 'Code':
  470. var div = $a(me.layout.cur_cell, 'div');
  471. var val = _f.get_value(doctype, docname,
  472. field.fieldname);
  473. div.innerHTML = '<div>' + field.label +
  474. ': </div><pre style="font-family: Courier, Fixed;">' + (val ? val : '') +
  475. '</pre>';
  476. break;
  477. case 'Text Editor':
  478. var div = $a(me.layout.cur_cell, 'div');
  479. var val = _f.get_value(doctype, docname,
  480. field.fieldname);
  481. div.innerHTML = val ? val : '';
  482. break;
  483. default:
  484. // Add Cell Data
  485. _p.print_std_add_field(doctype, docname, field, me.layout);
  486. break;
  487. }
  488. }
  489. });
  490. // Then build each field
  491. for(var i = 0; i < data.length; i++) {
  492. var fieldname = data[i].fieldname ? data[i].fieldname :
  493. data[i].label;
  494. var field = fieldname ?
  495. wn.meta.get_docfield(doctype, fieldname, docname) : data[i];
  496. if(!field.print_hide) {
  497. if(cur_frm.pformat[field.fieldname]) {
  498. // If there is a custom method to generate the HTML, then use it
  499. this.generate_custom_html(field, doctype, docname);
  500. } else {
  501. // Do the normal rendering
  502. this.render_normal(field, data, i);
  503. }
  504. }
  505. }
  506. me.layout.close_borders();
  507. },
  508. build_html: function() {
  509. var html = '';
  510. for(var i = 0; i < me.pf_list.length; i++) {
  511. if(me.pf_list[i].wrapper) {
  512. html += me.pf_list[i].wrapper.innerHTML;
  513. } else if(me.pf_list[i].innerHTML) {
  514. html += me.pf_list[i].innerHTML;
  515. } else {
  516. html += me.pf_list[i];
  517. }
  518. }
  519. this.pf_list = [];
  520. return html;
  521. }
  522. });
  523. if(!no_heading) {
  524. this.build_head(data, doctype, docname);
  525. }
  526. this.build_data(data, doctype, docname);
  527. var html = this.build_html();
  528. return html;
  529. },
  530. add_layout: function(doctype) {
  531. var layout = new Layout();
  532. layout.addrow();
  533. if(locals['DocType'][doctype].print_outline == 'Yes') {
  534. layout.with_border = 1
  535. }
  536. return layout;
  537. },
  538. print_std_add_table: function(t, layout, pf_list, dt, no_letterhead) {
  539. if(t.appendChild) {
  540. // If only one table is passed
  541. layout.cur_cell.appendChild(t);
  542. } else {
  543. page_break = '\n\
  544. <div style = "page-break-after: always;" \
  545. class = "page_break"></div><div class="page-settings"></div>';
  546. // If a list of tables is passed
  547. for(var i = 0; i < t.length-1; i++) {
  548. // add to current page
  549. layout.cur_cell.appendChild(t[i]);
  550. layout.close_borders();
  551. pf_list.push(page_break);
  552. // Create new page
  553. layout = _p.add_layout(dt, no_letterhead);
  554. pf_list.push(layout);
  555. layout.addrow();
  556. layout.addcell();
  557. var div = $a(layout.cur_cell, 'div');
  558. div.innerHTML = 'Continued from previous page...';
  559. div.style.padding = '4px';
  560. }
  561. // Append last table
  562. layout.cur_cell.appendChild(t[t.length-1]);
  563. }
  564. return layout;
  565. },
  566. print_std_add_field: function(dt, dn, f, layout) {
  567. var val = _f.get_value(dt, dn, f.fieldname);
  568. if(f.fieldtype!='Button') {
  569. if(val || in_list(['Float', 'Int', 'Currency'], f.fieldtype)) {
  570. // If value or a numeric type then proceed
  571. // Add field table
  572. row = _p.field_tab(layout.cur_cell);
  573. // Add label
  574. row.cells[0].innerHTML = f.label ? f.label : f.fieldname;
  575. row.cells[1].innerHTML = wn.format(val, f, {for_print: true});
  576. // left align currency in normal display
  577. if(f.fieldtype == 'Currency') {
  578. $y(row.cells[1], { textAlign: 'left' });
  579. }
  580. }
  581. }
  582. },
  583. field_tab: function(layout_cell) {
  584. var tab = $a(layout_cell, 'table', '', {width:'100%'});
  585. var row = tab.insertRow(0);
  586. _p.row = row; // Don't know this line's purpose
  587. row.insertCell(0);
  588. row.insertCell(1);
  589. row.cells[0].className = 'datalabelcell';
  590. row.cells[0].style.width = "38%";
  591. row.cells[1].className = 'datainputcell';
  592. return row;
  593. }
  594. });