examples_js_test_harness.js.html 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>examples/js/test_harness.js - Documentation</title>
  6. <script src="scripts/prettify/prettify.js"></script>
  7. <script src="scripts/prettify/lang-css.js"></script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
  13. </head>
  14. <body>
  15. <input type="checkbox" id="nav-trigger" class="nav-trigger" />
  16. <label for="nav-trigger" class="navicon-button x">
  17. <div class="navicon"></div>
  18. </label>
  19. <label for="nav-trigger" class="overlay"></label>
  20. <nav>
  21. <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="FontObject.html">FontObject</a></li><li><a href="jsPDF.html">jsPDF</a></li><li><a href="PubSub.html">PubSub</a></li></ul><h3>Global</h3><ul><li><a href="global.html#addFont">addFont</a></li><li><a href="global.html#addMetadata">addMetadata</a></li><li><a href="global.html#addPage">addPage</a></li><li><a href="global.html#CapJoinStyles">CapJoinStyles</a></li><li><a href="global.html#circle">circle</a></li><li><a href="global.html#CssColors">CssColors</a></li><li><a href="global.html#ellipse">ellipse</a></li><li><a href="global.html#getFontList">getFontList</a></li><li><a href="global.html#html2pdf">html2pdf</a></li><li><a href="global.html#init">init</a></li><li><a href="global.html#jsPDFEditor">jsPDFEditor</a></li><li><a href="global.html#lines">lines</a></li><li><a href="global.html#output">output</a></li><li><a href="global.html#pdf_test_harness_init">pdf_test_harness_init</a></li><li><a href="global.html#rect">rect</a></li><li><a href="global.html#requirejs">requirejs</a></li><li><a href="global.html#reset">reset</a></li><li><a href="global.html#roundedRect">roundedRect</a></li><li><a href="global.html#save">save</a></li><li><a href="global.html#setDrawColor">setDrawColor</a></li><li><a href="global.html#setFillColor">setFillColor</a></li><li><a href="global.html#setFont">setFont</a></li><li><a href="global.html#setFontSize">setFontSize</a></li><li><a href="global.html#setFontStyle">setFontStyle</a></li><li><a href="global.html#setLineCap">setLineCap</a></li><li><a href="global.html#setLineJoin">setLineJoin</a></li><li><a href="global.html#setLineWidth">setLineWidth</a></li><li><a href="global.html#setProperties">setProperties</a></li><li><a href="global.html#setTextColor">setTextColor</a></li><li><a href="global.html#text">text</a></li><li><a href="global.html#toUrl">toUrl</a></li><li><a href="global.html#triangle">triangle</a></li><li><a href="global.html#triggerEvent">triggerEvent</a></li><li><a href="global.html#update">update</a></li></ul>
  22. </nav>
  23. <div id="main">
  24. <h1 class="page-title">examples/js/test_harness.js</h1>
  25. <section>
  26. <article>
  27. <pre class="prettyprint source linenums"><code>/**
  28. * jsPDF PDF Test Harness
  29. * Copyright (c) 2014 Steven Spungin (TwelveTone LLC) steven@twelvetone.tv
  30. *
  31. * Licensed under the MIT License.
  32. * http://opensource.org/licenses/mit-license
  33. */
  34. /**
  35. * An easy way to view PDF and PDF source code side by side.
  36. */
  37. pdf_test_harness_init = function(pdf, message) {
  38. var harness = new pdf_test_harness();
  39. var body = document.getElementsByTagName('body')[0];
  40. body.style.display = 'flex';
  41. var div = document.createElement('div');
  42. div.setAttribute('style', 'position:fixed;height:20px;left:0;right:0;background:lightblue');
  43. body.appendChild(div);
  44. harness.header = div;
  45. var div2 = document.createElement('div');
  46. div2.setAttribute('style', 'position:fixed;display:flex;top:20px; bottom:0;left:0;right:0');
  47. body.appendChild(div2);
  48. harness.body = div2;
  49. var btn1 = document.createElement('input');
  50. btn1.setAttribute('type', 'radio');
  51. btn1.setAttribute('name', 'view');
  52. div.appendChild(btn1);
  53. btn1.checked = true;
  54. var lbl1 = document.createElement('label');
  55. lbl1.setAttribute('for', 'btn1');
  56. lbl1.innerHTML = 'PDF'
  57. div.appendChild(lbl1);
  58. var btn2 = document.createElement('input');
  59. btn2.setAttribute('type', 'radio');
  60. btn2.setAttribute('name', 'view');
  61. div.appendChild(btn2);
  62. var lbl2 = document.createElement('label');
  63. lbl2.setAttribute('for', 'btn2');
  64. lbl2.innerHTML = 'Source'
  65. div.appendChild(lbl2);
  66. var btn3 = document.createElement('input');
  67. btn3.setAttribute('type', 'radio');
  68. btn3.setAttribute('name', 'view');
  69. div.appendChild(btn3);
  70. var lbl3 = document.createElement('label');
  71. lbl3.setAttribute('for', 'btn3');
  72. lbl3.innerHTML = 'Both'
  73. div.appendChild(lbl3);
  74. harness.source = document.createElement('pre');
  75. harness.source.setAttribute('style', 'margin-top:0;width:100%;height:100%;position:absolute;top:0px;bottom:0px;overflow:auto');
  76. div2.appendChild(harness.source);
  77. harness.iframe = document.createElement('iframe');
  78. harness.iframe.setAttribute('style', 'width:100%;height:100%;position:absolute;overflow:auto;top:0px;bottom:0px');
  79. div2.appendChild(harness.iframe);
  80. //if (pdf_test_harness.onload) {
  81. //harness.pdf = pdf_test_harness.onload(harness);
  82. if (message) {
  83. message += "&lt;p style='text-align:center;font-style:italic;font-size:.8em'>click to close&lt;/p>";
  84. var popup = document.createElement('div');
  85. popup.setAttribute('style', 'z-index:100;margin:100px auto;cursor:pointer;font-size:1.3em;top:50px;background-color:rgb(243, 224, 141);padding:1em;border:1px solid black');
  86. popup.innerHTML = message;
  87. body.appendChild(popup);
  88. popup.onclick = function() {
  89. popup.parentNode.removeChild(popup);
  90. }
  91. }
  92. //}
  93. harness.pdf = pdf;
  94. harness.render('pdf');
  95. btn1.onclick = function() {
  96. harness.render('pdf');
  97. }
  98. btn2.onclick = function() {
  99. harness.render('source');
  100. }
  101. btn3.onclick = function() {
  102. harness.render('both');
  103. }
  104. return harness;
  105. }
  106. pdf_test_harness = function(pdf) {
  107. this.pdf = pdf;
  108. this.onload = undefined;
  109. this.iframe = undefined;
  110. this.entityMap = {
  111. "&amp;" : "&amp;amp;",
  112. "&lt;" : "&amp;lt;",
  113. ">" : "&amp;gt;",
  114. '"' : '&amp;quot;',
  115. "'" : '&amp;#39;',
  116. "/" : '&amp;#x2F;'
  117. };
  118. this.escapeHtml = function(string) {
  119. return String(string).replace(/[&amp;&lt;>"'\/]/g, function(s) {
  120. return this.entityMap[s];
  121. }.bind(this));
  122. };
  123. this.getParameterByName = function(name) {
  124. name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
  125. var regex = new RegExp("[\\?&amp;]" + name + "=([^&amp;#]*)"), results = regex.exec(location.search);
  126. return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
  127. };
  128. this.setPdf = function(pdf) {
  129. this.pdf = pdf;
  130. this.rendered = undefined;
  131. this.render(this.view);
  132. };
  133. // generate the pdf, the source code, or both
  134. this.render = function(view) {
  135. this.view = view;
  136. //Current code only lets us render one time.
  137. if (!this.rendered) {
  138. this.rendered = this.pdf.output('datauristring');
  139. this.iframe.src = this.rendered;
  140. var raw = this.pdf.output();
  141. raw = this.escapeHtml(raw);
  142. this.source.innerHTML = raw;
  143. }
  144. if ('pdf' === view) {
  145. this.source.style.display = 'none';
  146. this.iframe.style.display = 'block';
  147. this.iframe.style.width = '100%';
  148. } else if ('source' === view) {
  149. this.iframe.style.display = 'none';
  150. this.source.style.display = 'block';
  151. this.source.style.width = '100%';
  152. }
  153. if ('both' === view) {
  154. raw = this.escapeHtml(raw);
  155. this.iframe.style.width = '50%';
  156. this.iframe.style.position = 'relative';
  157. this.iframe.style.display = 'inline-block';
  158. this.source.style.width = '50%';
  159. this.source.style.position = 'relative';
  160. this.source.style.display = 'inline-block';
  161. }
  162. }
  163. }
  164. </code></pre>
  165. </article>
  166. </section>
  167. </div>
  168. <br class="clear">
  169. <footer>
  170. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.1</a> on Mon Oct 03 2016 12:36:18 GMT+0100 (BST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
  171. </footer>
  172. <script>prettyPrint();</script>
  173. <script src="scripts/linenumber.js"></script>
  174. </body>
  175. </html>