util.js.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: util.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: util.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>/**
  20. * Return a random integer between [`min`, `max`].
  21. *
  22. * @param {number} min - The lowest possible integer.
  23. * @param {number} max - The highest possible integer (inclusive).
  24. * @returns {number} A random integer.
  25. */
  26. function randomInteger(min,max) {
  27. return Math.floor(Math.random()*(max-min+1)+min);
  28. }
  29. /**
  30. * Indents the given string
  31. * @param {string} str The string to be indented.
  32. * @param {number} numOfIndents The amount of indentations to place at the
  33. * beginning of each line of the string.
  34. * @param {number=} opt_spacesPerIndent Optional. If specified, this should be
  35. * the number of spaces to be used for each tab that would ordinarily be
  36. * used to indent the text. These amount of spaces will also be used to
  37. * replace any tab characters that already exist within the string.
  38. * @return {string} The new string with each line beginning with the desired
  39. * amount of indentation.
  40. */
  41. function indent(str, numOfIndents, opt_spacesPerIndent) {
  42. str = str.replace(/^(?=.)/gm, new Array(numOfIndents + 1).join('\t'));
  43. numOfIndents = new Array(opt_spacesPerIndent + 1 || 0).join(' '); // re-use
  44. return opt_spacesPerIndent
  45. ? str.replace(/^\t+/g, function(tabs) {
  46. return tabs.replace(/./g, numOfIndents);
  47. })
  48. : str;
  49. }
  50. /**
  51. * Encodes some text so that it can be safely written into an HTML box.
  52. * This includes replacing special HTML characters (&amp;, &lt;, >, etc.).
  53. *
  54. * @param {string} str - The text to be converted.
  55. * @return {string} The HTML-safe text.
  56. */
  57. function encodeHTML(str) {
  58. return str.replace(/&amp;/g, '&amp;amp;')
  59. .replace(/&lt;/g, '&amp;lt;')
  60. .replace(/>/g, '&amp;gt;')
  61. .replace(/"/g, '&amp;quot;')
  62. .replace(/'/g, '&amp;apos;');
  63. }
  64. /**
  65. * Shuffle the blocks in the workspace
  66. */
  67. Blockly.WorkspaceSvg.prototype.shuffle = function() {
  68. var metrics = this.getMetrics();
  69. var width = metrics.viewWidth / 2,
  70. height = metrics.viewHeight;
  71. var blocks = this.getTopBlocks(false);
  72. var y = 5, x = 0,
  73. maximal_increase = height/blocks.length;
  74. for (var i = 0; i &lt; blocks.length; i++){
  75. // Get a block
  76. var block = blocks[i];
  77. var properties = block.getRelativeToSurfaceXY();
  78. if (i == 0) {
  79. x = 5;
  80. } else {
  81. x = -properties.x+randomInteger(10, width);
  82. }
  83. block.moveBy(x,
  84. -properties.y+y);
  85. y = y + randomInteger(5, maximal_increase);
  86. }
  87. }
  88. </code></pre>
  89. </article>
  90. </section>
  91. </div>
  92. <nav>
  93. <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>
  94. </nav>
  95. <br class="clear">
  96. <footer>
  97. 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)
  98. </footer>
  99. <script> prettyPrint(); </script>
  100. <script src="scripts/linenumber.js"> </script>
  101. </body>
  102. </html>