corgis.js.html 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: corgis.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: corgis.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>_IMPORTED_DATASETS = {};
  20. /**
  21. * Module that connects to the CORGIS datasets and manages interactions
  22. * with them. This includes loading in datasets at launch and on-the-fly.
  23. * Note that this has no presence on screen, so it does not have a tag.
  24. *
  25. * @constructor
  26. * @this {BlockPyCorgis}
  27. * @param {Object} main - The main BlockPy instance
  28. */
  29. function BlockPyCorgis(main) {
  30. this.main = main;
  31. this.loadedDatasets = [];
  32. // Load in each the datasets
  33. var corgis = this;
  34. var imports = [];
  35. this.main.model.assignment.modules().forEach(function(name) {
  36. var post_prefix = name.substring(7).replace(/\s/g, '_').toLowerCase();
  37. if (!(name in BlockPyEditor.CATEGORY_MAP)) {
  38. imports.push.apply(imports, corgis.importDataset(post_prefix, name));
  39. }
  40. });
  41. // When datasets are loaded, update the toolbox.
  42. $.when.apply($, imports).done(function() {
  43. if (main.model.settings.editor() == "Blocks") {
  44. main.components.editor.updateBlocksFromModel();
  45. }
  46. main.components.editor.updateToolbox(true);
  47. }).fail(function(e) {
  48. console.error(e);
  49. }).always(function() {
  50. main.components.server.finalizeSubscriptions();
  51. });
  52. }
  53. /**
  54. * Loads the definitions for a dataset into the environment, including
  55. * the dataset (as a JS file), the skulpt bindings, and the blockly
  56. * bindings. This requires access to a CORGIS server, and occurs
  57. * asynchronously. The requests are fired and their deferred objects
  58. * are returned - callers can use this information to perform an action
  59. * on completion of the import.
  60. *
  61. * @param {String} slug - The URL safe version of the dataset name
  62. * @param {String} name - The user-friendly version of the dataset name.
  63. * @returns {Array.&lt;Deferred>} - Returns the async requests as deferred objects.
  64. */
  65. BlockPyCorgis.prototype.importDataset = function(slug, name) {
  66. var url_retrievals = [];
  67. if (this.main.model.server_is_connected('import_datasets')) {
  68. var root = this.main.model.constants.urls.import_datasets+'blockpy/'+slug+'/'+slug;
  69. this.main.model.status.dataset_loading.push(name);
  70. // Actually get data
  71. var get_dataset = $.getScript(root+'_dataset.js');
  72. var get_skulpt = $.get(root+'_skulpt.js', function(data) {
  73. Sk.builtinFiles['files']['src/lib/'+slug+'/__init__.js'] = data;
  74. });
  75. var get_blockly = $.getScript(root+'_blockly.js');
  76. // On completion, update menus.
  77. var corgis = this;
  78. $.when(get_dataset, get_skulpt, get_blockly).done(function() {
  79. corgis.loadedDatasets.push(slug);
  80. corgis.main.model.assignment.modules.push(name);
  81. corgis.main.components.editor.addAvailableModule(name);
  82. corgis.main.model.status.dataset_loading.pop();
  83. });
  84. url_retrievals.push(get_dataset, get_skulpt, get_blockly);
  85. }
  86. return url_retrievals;
  87. }
  88. /**
  89. * Opens a dialog box to present the user with the datasets available
  90. * through the CORGIS server. This requires a call, so this method
  91. * completes asynchronously. The dialog is composed of a table with
  92. * buttons to load the datasets (More than one dataset can be loaded
  93. * from within the dialog at a time).
  94. *
  95. * @param {String} name - The name of the dataset to open. This is basically the user friendly version of the name, though it will be mangled into a slug.
  96. */
  97. BlockPyCorgis.prototype.openDialog = function(name) {
  98. var corgis = this;
  99. if (this.main.model.server_is_connected('import_datasets')) {
  100. var root = this.main.model.constants.urls.import_datasets;
  101. $.getJSON(root+'index.json', function(data) {
  102. // Make up the Body
  103. var datasets = data.blockpy.datasets;
  104. var start = $("&lt;p>Documentation is available at url&lt;/p>");
  105. var body = $('&lt;table>&lt;/table>', {'class': 'table-bordered table-condensed table-striped'});
  106. Object.keys(datasets).map(function(name) {
  107. var title_name = name;
  108. name = name.replace(/\s/g, '_').toLowerCase();
  109. var btn = $('&lt;button type="button" class="btn btn-primary" data-toggle="button" aria-pressed="false" autocomplete="off">Load&lt;/button>');
  110. if (corgis.loadedDatasets.indexOf(name) > -1) {
  111. set_button_loaded(btn);
  112. } else {
  113. btn.click(function() {
  114. corgis.importDataset(name.toLowerCase(), 'Data - '+title_name);
  115. set_button_loaded(btn);
  116. });
  117. }
  118. $("&lt;tr>&lt;/tr>")
  119. .append($("&lt;td class='col-md-4'>"+title_name+"&lt;/td>"))
  120. .append($("&lt;td>"+datasets[title_name]['short']+"&lt;/td>"))
  121. .append($("&lt;td class='col-md-2'>&lt;/td>").append(btn))
  122. .appendTo(body);
  123. });
  124. // Show the actual dialog
  125. var editor = corgis.main.components.editor;
  126. corgis.main.components.dialog.show("Import Datasets", body, function() {
  127. if (editor.main.model.settings.editor() == "Blocks") {
  128. editor.updateBlocksFromModel();
  129. }
  130. });
  131. });
  132. }
  133. };
  134. /**
  135. * This is a very simplistic helper function that will transform
  136. * a given button into a "Loaded" state (disabled, pressed state, etc.).
  137. *
  138. * @param {HTMLElement} btn - An HTML element to change the text of.
  139. */
  140. var set_button_loaded = function(btn) {
  141. btn.addClass("active")
  142. .addClass('btn-success')
  143. .removeClass('btn-primary')
  144. .prop("disabled", true)
  145. .text("Loaded")
  146. .attr("aria-pressed", "true");
  147. }</code></pre>
  148. </article>
  149. </section>
  150. </div>
  151. <nav>
  152. <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>
  153. </nav>
  154. <br class="clear">
  155. <footer>
  156. 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)
  157. </footer>
  158. <script> prettyPrint(); </script>
  159. <script src="scripts/linenumber.js"> </script>
  160. </body>
  161. </html>