printer.js.html 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: printer.js</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-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: printer.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>/**
  20. * An object for managing the console where printing and plotting is outputed.
  21. *
  22. * @constructor
  23. * @this {BlockPyEditor}
  24. * @param {Object} main - The main BlockPy instance
  25. * @param {HTMLElement} tag - The HTML object this is attached to.
  26. */
  27. function BlockPyPrinter(main, tag) {
  28. this.main = main;
  29. this.tag = tag;
  30. /** Keep printer settings available for interested parties */
  31. this.printerSettings = {};
  32. this.resetPrinter();
  33. };
  34. /**
  35. * Reset the status of the printer, including removing any text in it and
  36. * fixing its size.
  37. */
  38. BlockPyPrinter.prototype.resetPrinter = function() {
  39. this.tag.empty();
  40. this.main.model.execution.output.removeAll();
  41. this.printerSettings['width'] = Math.min(500, this.tag.width()-40);
  42. this.printerSettings['height'] = Math.min(500, this.tag.height()+40);
  43. Sk.TurtleGraphics = {'target': this.tag[0],
  44. 'width': this.printerSettings['width'],
  45. 'height': this.printerSettings['height']};
  46. }
  47. /**
  48. * Update and return the current configuration of the printer. This
  49. * involves calculating its size, among other operations.
  50. *
  51. * @returns {Object} Returns an object with information about the printer.
  52. */
  53. BlockPyPrinter.prototype.getConfiguration = function() {
  54. var printer = this;
  55. this.printerSettings['printHtml']= function(html, value) { printer.printHtml(html, value);};
  56. this.printerSettings['width']= Math.min(500, this.tag.width()-40);
  57. this.printerSettings['pngMode']= true;
  58. this.printerSettings['skipDrawing']= false;
  59. this.printerSettings['height']= Math.min(500, this.tag.height()+40);
  60. this.printerSettings['container']= this.tag[0];
  61. return this.printerSettings;
  62. }
  63. /**
  64. * Updates each printed element in the printer and makes it hidden
  65. * or visible, depending on what step we're on.
  66. *
  67. * @param {Number} step - The current step of the executed program that we're on; each element in the printer must be marked with a "data-step" property to resolve this.
  68. * @param {Number} page - Deprecated, not sure what this even does.
  69. */
  70. BlockPyPrinter.prototype.stepPrinter = function(step, page) {
  71. $(this.printer).find('.blockpy-printer-output').each(function() {
  72. if ($(this).attr("data-step") &lt;= step) {
  73. $(this).show();
  74. } else {
  75. $(this).hide();
  76. }
  77. });
  78. }
  79. /**
  80. * Print a successful line to the on-screen printer.
  81. * @param {String} lineText - A line of text to be printed out.
  82. */
  83. BlockPyPrinter.prototype.print = function(lineText) {
  84. // Should probably be accessing the model instead of a component...
  85. var stepNumber = this.main.components.engine.executionBuffer.step;
  86. var lineNumber = this.main.components.engine.executionBuffer.line_number;
  87. // Perform any necessary cleaning
  88. if (lineText !== "\n") {
  89. var encodedText = encodeHTML(lineText);
  90. this.main.model.execution.output.push(encodedText.trim());
  91. if (!(this.main.model.settings.mute_printer())) {
  92. var lineContainer = $("&lt;div class='blockpy-printer-output' >");
  93. var lineData = $("&lt;samp>&lt;/samp>", {
  94. 'data-toggle': 'tooltip',
  95. 'data-placement': 'left',
  96. 'data-step': stepNumber,
  97. "html": encodedText,
  98. 'title': "Step "+stepNumber + ", Line "+lineNumber,
  99. })
  100. lineContainer.append(lineData);
  101. // Append to the current text
  102. this.tag.append(lineContainer);
  103. lineData.tooltip();
  104. }
  105. }
  106. }
  107. /**
  108. * Prints a successful HTML blob to the printer. This is typically charts,
  109. * but it can actually be any kind of HTML. This will probably be useful for
  110. * doing Turtle and Processing stuff down the road.
  111. *
  112. * @param {HTML} html - A blob of HTML to render in the tag
  113. * @param {Anything} value - a value to push on the outputList for comparison. For instance, on charts this is typically the data of the chart.
  114. */
  115. BlockPyPrinter.prototype.printHtml = function(chart, value) {
  116. var step = this.main.model.execution.step();
  117. var line = this.main.model.execution.line_number();
  118. this.main.model.execution.output.push(value);
  119. if (!(this.main.model.settings.mute_printer())) {
  120. var outerDiv = $(Sk.console.png_mode ? chart : chart[0]);//.parent();
  121. outerDiv.parent().show();
  122. outerDiv.attr({
  123. "data-toggle": 'tooltip',
  124. "data-placement": 'left',
  125. //"data-container": '#'+chart.attr("id"),
  126. "class": "blockpy-printer-output",
  127. "data-step": step,
  128. "title": "Step "+step+", Line "+line
  129. });
  130. outerDiv.tooltip();
  131. }
  132. }</code></pre>
  133. </article>
  134. </section>
  135. </div>
  136. <nav>
  137. <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="BlockPy.html">BlockPy</a></li><li><a href="BlockPyCorgis.html">BlockPyCorgis</a></li><li><a href="BlockPyDialog.html">BlockPyDialog</a></li><li><a href="BlockPyEditor.html">BlockPyEditor</a></li><li><a href="BlockPyEngine.html">BlockPyEngine</a></li><li><a href="BlockPyEnglish.html">BlockPyEnglish</a></li><li><a href="BlockPyFeedback.html">BlockPyFeedback</a></li><li><a href="BlockPyHistory.html">BlockPyHistory</a></li><li><a href="BlockPyPresentation.html">BlockPyPresentation</a></li><li><a href="BlockPyPrinter.html">BlockPyPrinter</a></li><li><a href="BlockPyServer.html">BlockPyServer</a></li><li><a href="BlockPyToolbar.html">BlockPyToolbar</a></li><li><a href="LocalStorageWrapper.html">LocalStorageWrapper</a></li><li><a href="PythonToBlocks.html">PythonToBlocks</a></li></ul><h3>Global</h3><ul><li><a href="global.html#BlockPyInterface">BlockPyInterface</a></li><li><a href="global.html#cloneNode">cloneNode</a></li><li><a href="global.html#encodeHTML">encodeHTML</a></li><li><a href="global.html#expandArray">expandArray</a></li><li><a href="global.html#EXTENDED_ERROR_EXPLANATION">EXTENDED_ERROR_EXPLANATION</a></li><li><a href="global.html#indent">indent</a></li><li><a href="global.html#instructor_module">instructor_module</a></li><li><a href="global.html#prettyPrintDateTime">prettyPrintDateTime</a></li><li><a href="global.html#randomInteger">randomInteger</a></li><li><a href="global.html#set_button_loaded">set_button_loaded</a></li><li><a href="global.html#timerGuard">timerGuard</a></li></ul>
  138. </nav>
  139. <br class="clear">
  140. <footer>
  141. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Sun Mar 26 2017 09:45:03 GMT-0400 (Eastern Daylight Time)
  142. </footer>
  143. <script> prettyPrint(); </script>
  144. <script src="scripts/linenumber.js"> </script>
  145. </body>
  146. </html>