Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

130 linhas
2.8 KiB

  1. <template>
  2. <div class="h-100">
  3. <div class="row">
  4. <div class="col">
  5. <div ref="doc-select"></div>
  6. </div>
  7. <div class="col">
  8. <div ref="preview-type"></div>
  9. </div>
  10. <div class="col d-flex">
  11. <a
  12. v-if="url"
  13. class="btn btn-default btn-sm btn-new-tab"
  14. target="_blank"
  15. :href="url"
  16. >
  17. {{ __("Open in a new tab") }}
  18. </a>
  19. <button
  20. v-if="url"
  21. class="ml-3 btn btn-default btn-sm btn-new-tab"
  22. @click="refresh"
  23. >
  24. {{ __("Refresh") }}
  25. </button>
  26. </div>
  27. </div>
  28. <div v-if="url && !preview_loaded">Generating preview...</div>
  29. <iframe
  30. ref="iframe"
  31. :src="url"
  32. v-if="url"
  33. v-show="preview_loaded"
  34. class="preview-iframe"
  35. @load="preview_loaded = true"
  36. ></iframe>
  37. </div>
  38. </template>
  39. <script>
  40. import { storeMixin } from "./store";
  41. export default {
  42. name: "Preview",
  43. mixins: [storeMixin],
  44. data() {
  45. return {
  46. type: "PDF",
  47. docname: null,
  48. preview_loaded: false
  49. };
  50. },
  51. mounted() {
  52. this.doc_select = frappe.ui.form.make_control({
  53. parent: this.$refs["doc-select"],
  54. df: {
  55. label: __("Select {0}", [__(this.doctype)]),
  56. fieldname: "docname",
  57. fieldtype: "Link",
  58. options: this.doctype,
  59. change: () => {
  60. this.docname = this.doc_select.get_value();
  61. }
  62. },
  63. render_input: true
  64. });
  65. this.preview_type = frappe.ui.form.make_control({
  66. parent: this.$refs["preview-type"],
  67. df: {
  68. label: __("Preview type"),
  69. fieldname: "docname",
  70. fieldtype: "Select",
  71. options: ["PDF", "HTML"],
  72. change: () => {
  73. this.type = this.preview_type.get_value();
  74. }
  75. },
  76. render_input: true
  77. });
  78. this.preview_type.set_value(this.type);
  79. this.get_default_docname().then(
  80. docname => docname && this.doc_select.set_value(docname)
  81. );
  82. this.$store.$on("after_save", () => {
  83. this.refresh();
  84. });
  85. },
  86. methods: {
  87. refresh() {
  88. this.$refs.iframe.contentWindow.location.reload();
  89. },
  90. get_default_docname() {
  91. return frappe.db.get_list(this.doctype, { limit: 1 }).then(doc => {
  92. return doc.length > 0 ? doc[0].name : null;
  93. });
  94. }
  95. },
  96. computed: {
  97. doctype() {
  98. return this.print_format.doc_type;
  99. },
  100. url() {
  101. if (!this.docname) return null;
  102. let params = new URLSearchParams();
  103. params.append("doctype", this.doctype);
  104. params.append("name", this.docname);
  105. params.append("print_format", this.print_format.name);
  106. if (this.$store.letterhead) {
  107. params.append("letterhead", this.$store.letterhead.name);
  108. }
  109. let url =
  110. this.type == "PDF"
  111. ? `/api/method/frappe.utils.weasyprint.download_pdf`
  112. : "/printpreview";
  113. return `${url}?${params.toString()}`;
  114. }
  115. }
  116. };
  117. </script>
  118. <style scoped>
  119. .preview-iframe {
  120. width: 100%;
  121. height: 96%;
  122. border: none;
  123. border-radius: var(--border-radius);
  124. }
  125. .btn-new-tab {
  126. margin-top: auto;
  127. margin-bottom: 1.2rem;
  128. }
  129. </style>