mirror of
https://github.com/google/blockly.git
synced 2026-01-30 04:00:10 +01:00
* Adding new minimap demo * Basic code style changes. Adding a few more comments. Return early if disableScrollChange in onScrollChange listener. * Adding horizontal scrolling. Changed scroll change callbacks from onScroll_ to setHandlePosition. onScroll_ is not challed when workspace is dragged. * Registering mousemove and mouseup listener in mousedown event. Mousemove and Mouseup events are now listening over document. * Adding the remove variable modal and functionality to accessible Blockly. (#1011) * Minimap position bug fix for browsers other than chrome. Added touch support. * Adding an add variable modal to accessible Blockly. (#1015) * Adding the remove variable modal and functionality to accessible Blockly. * Adding the add variable modal for accessible Blockly. * Block browser context menu in the toolbox and flyout * Add links to the dev registration form and contributor guidelines * Miscellaneous comment cleanup * Adding the common modal class. (#1017) Centralizes accessible modal behavior. * - Changed error message referencing 'procedure' instead of 'function' (#1019) - Added iOS specific UI messages - Fixed bug with js_to_json.py script where it didn't recognize ' character * - Allows use of Blockly's messaging format for category name, colour,… (#1028) ...in toolbox XML. - Updated code editor demo to use this message format - Re-built blockly_compressed.js * Making text_count use a text color (like text_length, which also returns a number). (#1027) * Enable google/blockly with continuous build on travis ci (#1023) (#1035) * create .travis for ci job * initial checkin for blocky-web travis ci job * rename file to .travis.yaml for typo * remove after_script * added cache * rename .travis.yaml to .travis.yml * Update .travis.yml * include build script * fix yaml file format issue * debug install part * debug build issue * Update .travis.yml * remove cache for now * Update .travis.yml * Update .travis.yml * Update .travis.yml * more debug info * Update .travis.yml * Update .travis.yml * fix typo * installing chrome browser * remove chrome setting config * run build.py as part of npm install * Update .travis.yml * update karma dependency * use karma as test runner * fix typo * remove karma test for now * Update .travis.yml * Update package.json * add npm test target * add browserstack-runner depdendency * update browser support * fix typo for test target * fix chrome typo * added closure dependency * add google-closure-library * include blockly_uncompressed.js and core.js dependency * uncomment out core/*.js files * add kama job as part of install * remove browserstack add on for now * fix karma config typo * add karma-closure * add os support * remove typo config * include more closure files * change os back to linux * use closure-library from node_modules * change log level back to INFO * change npm test target to use open browser command instead of karma * change travis test target to use open command instead of karma * list current directory * find what's in current dir * typo command * Update .travis.yml * typo again * open right index.html * use right path for index.html * xdg-open to open default browser on travis * exit browser after 5s wait * change timeout to 1 min * exit after opening up browser * use browser only * use karma * remove un-needed dependency * clean up script section * fix typo * update build status on readme * initial commit for selenium integration tests * update selenium jar path * fix test_runner.js typo * add more debug info * check java version * add && instead of 9288 * fix java path * add logic to check if selenium is running or not * add some deugging info * initial commit to get chromedriver * add chromedriver flag * add get_chromedriver.sh to package.json and .travel * change browser to chrome for now * fix path issue * update chromdriver path * fix path issue again * more debugging * add debug msg * fix typo * minor fix for getting chromedriver * install latest chrome browser * clean up pakcage.json * use npm target for test run * remove removing trailing comma * fix another trailing comma * updated travis test target * clean up scripts * not sure nmp run preinstall * redirect selenium log to tmp file * revert writing console log to file * update test summary * more clean up * minor clean up before pull request * resolved closure-library conflict 1. add closure-library to dependencies instead of devDependencies. 2. add lint back in scripts block * fix typo (adding comma) in script section * Renames Blockly.workspaceDragSurface to Blockly.WorkspaceDragSurface. Fixes #880. * Ensure useDragSurface is a boolean. Fixed #988 * use pretest instead of preinstall in package.json (#1043) * cherry pick for pretest fix * put pretest target to test_setup.sh * fix conflict * cherry pick for get_chromedriver.sh * add some sleep to wait download to finish * use node.js stable * use npm test target * field_angle renders degree symbol consistently. Fixes #973 * bumpNeighbours_ function moved to block_svg. Fixed #1009 * Update RegEx in js-to-json to match windowi eol (#1050) The current regex only works with the "\n" line endings as it expects no characters after the optional ";" at the end of the line. In windows, if it adds the "\r" it counts as a characters and is not part of the line terminator so it doesn't match. * Fix French translation of "colour with rgb" block (#1053) "colorier", which is currently used, is a verb and proposed "couleur" is a noun: the block in question does not change colour of anything, it creates new colour instead, thus noun is more applicable. Also, noun is used in French translation of "random colour" block: "couleur aléatoire". * Enforcing non-empty names on value inputs and statement inputs. (#1054) * Correcting #1054 (#1056) single quotes. better logic. * Created a variable model with name, id, and type. Created a jsunit test file for variable model. * Change how blockly handles cursors. The old way was quite slow becau… (#1057) * Change how blockly handles cursors. The old way was quite slow because it changed the stylesheet directly. See issue #981 for more details on implementation and tradeoffs. This changes makes the following high level changes: deprecate Blockly.Css.setCursor, use built in open and closed hand cursor instead of custom .cur files, add css to draggable objects to set the open and closed hand cursors. * Rebuild blockly_uncompressed to pick up a testing change to make travis happy. Fix a build warning from a multi-line string in the process. (#1059) * Merge master into develop (#1063) - pick up translation changes - clean up trailing spaces * use goog.string.startswith instead of string.startswith (#1065) * New jsinterpreter demo includes wait block. Both demos have improved UI for clarity. (#1001) Refactor of interpreter demo * Renamed demos/interpreter/index.html as demos/interpreter/step-execution.html (including redirect), and added demos/interpreter/async-execution.html. * Refactored code to automatically generate/parse the blocks, eliminating the need for a "Parse JavaScript" button. Code is still shown in alert upon stepping to the first statement. Print statements now write to output <textarea> instead of modal dialogs. * VariableMap and functions added. * Create separate file for VariableMap and its functions. * Fix #1069 (#1073) * VariableMap and functions added. * Fix #1051 (#1084) * Improve errors when validating JSON block definitions. (#1086) goog.asserts to not run from blockly_compressed.js. User data validation should always run. * Dragging changes, rebased on develop (#1078) * Add block drag surface translateSurfaceBy * Add dragged connection manager * Add gesture.js * Add GestureHandler * Implemented gesture skeleton * Most basic workspace dragging * Add dragged connection manager * cleanup * doc * more cleanup * Add gesture handler * Add translateSurfaceInternal * core/block_dragger.js * cleanup * Pull in changes to dragged connection manager * Pull in changes to dragged connection manager * comments * more annotations * Add workspace dragger * Add coordinate annotations * Start on block dragging * Limit number of concurrent gestures * Add some TODOs * start using dragged connection manager * Set origin correctly for dragging blocks * Connect or delete at the end of a block drag. * cleanup * handle field clicks and block + workspace right-clicks * move code into BlockDragger class, but still reach into Gesture internals a lot * Clean up block dragger * Call blockDragger constructor with correct arguments * Enable block dragging in a mutator workspace * Add workspace dragger * click todos * Drag flyout with background * more dragging from flyout * nit * fix dragging from flyouts * Remove unused code and rename gestureHandler to gestureDB * Rename gesture handler * Added some jsdoc in gesture.js * Update some docs * Move some code to block_svg and clean up code * Lots of coordinate annotations * Fix block dragging when zoomed. * Remove built files from branch * More dragging work (#1026) -- Drag bubbles while dragging blocks -- Use bindEventWithChecks to work in touch on Android. Not tested anywhere else yet. -- Handle dragging blocks while zoomed -- Handle dragging blocks in mutators -- Handle right-clicks (I hope) -- Removed lots of unused code * More dragging work (#1048) - Removed gestureDB - Removing uses of terminateDrag - Cleaned up disposal code * Dragging bugfixes (#1058) - Get rid of flyout.dragMode_ and blockly.dragMode_ - Make drags from the flyout start from the top block in the group - Block tooltips from being scheduled or shown during gestures - Don't resize mutator bubbles mid-drag * Fix events in new dragging (#1060) * rebuild for testing * unbuild * Fix events * rebuild * Fix up cursors * Use language files from develop * Remove handled TODOS * attempt to fix IE rerendering bug, and recalculate workspace positions on scroll * Rebuild all the things * Comment cleanup; annotations; delete unused variables. * Tidy up context menu code. (#1081) * add osx travis test run job (#1074) * Names are correctly fetched from VariableModels! * add more wait time for test setup (#1091) * Work around timing issue with travis osx issue (#1092) * add more wait time for test setup * increase selenium wait time * add more wait * Fix #1077 by adding a rule to cover the toolbox labels too. (#1099) * Assign variable UUID to field_variable dropdown. * Change registration link to a static one (#1106) This lets us redirect to a different form if we change it in the future. * Edit generators to read in Variable Models. * Add VariableMap requirement to workspace. * Changed parameter name in workspace for clarity. * Add type, id, and info to the generated xml. Add xml tests for fieldToDom. Update workspace tests to pass with new changes. * Fix apostrophe in tooltips and helpurls (#1111) * Click events on shadow blocks have the correct id (#1089) * Add image_onclick option (#1080) * Cleanup: semicolons, spacing, etc. (#1116) * Spelling. Spelling is hard. * Add variable info to xml generated in variables.js * Add missing CLAs info to the contributing file (#1119) * Add missing CLAs info to the contributing file * Added larger changes paragraph * Replacing latest prettifier hosted in repo with latest version at rawgit CDN. (#1120) * Forgot update code demo (#1121) * Move audio code to a new file (#1122) * move audio code to a new file * dispose * null check * Make flyout get variables from target workspace's variableMap. * Require VariableModel in field_variable.js. * Update contributing.md (#1126) * Include variables at top of serialization. * Move blockSvg.getHeightWidth to block_render_svg.js (#1118) * Deserialization variables at top. * Create grid object (#1131) * Create grid object * Doc * Units! Thanks @RoboErikG * Appease eslint: semicolons and such. * Deserialization variables at top. * Setting up accessible Blockly for Closure compilation. (#1134) Moving closure compilation into the build file. Fixing up goog.requires for accessible blockly. Adding accessible to the build script. * Fix #1109 * Adding command-line options to the buildscript. (#1136) * Fix for travis-ci testing failure (#1141) It looks like the default configuration for Travis might have changed. Adding a manual step to install webdriverio. * Convert text_join block to JSON + mutator format (#1140) * Clean up and create test utilities file. * Add BLOCK events and constructor tests. * Convert more text blocks to json (#1147) * Convert more text blocks to json Converts the mutator blocks for text_join and the text_append block to JSON format. * Fixing modals so they're announced on focus, and changing variables t… (#1030) * Fixing modals so they're announced on focus, and changing variables to only react to enter, not onChange. * Adding a temp index. * Whoops - added it in the wrong spot. * Adding automatically-generated variable lists to the accessible toolbox. (#1149) Fixing a bug with the core-only uncompressed file not finding its own directory. * Field Variable setValue() looks up variable. * Wrap Error in Try Finally Block. * Changing the build file to allow forced rebuilding of the msg files. (#1158) * Split flyout into flyout_base, flyout_horizontal, and flyou_vertical * Rename flyout to flyout_base * flyout_base minus horizontal and vertical code * Add flyout_vertical and flyout_horizontal * review fixes + toolbox and workspace use * Fix hat block case * rebuild uncompressed * Fix travis problem * Fix build problem * Add VAR events. * Correctly named block events called. * Fire VAR events and test. * Create utility function for checking variable values. * In DropdownCreate check for Msg.DELETE_VARIABLE. * Test Delete Variable Twice. * Convert more text blocks to JSON format (#1163) * Convert more text blocks to JSON format Converts text_charAt, text_indexOf, text_isEmpty, and text_length to JSON. Includes a rebuild to pick up message changes. * Fixing the accessible variable stuff so it interacts correctly with (#1170) variableMap. * createVariable in workspaceSvg takes in id and type. * Followup RefreshToolbox. * Fixing an error with block messages and ordering. (#1171) * Remove out of date todo comments. * Fix checkbox delete bug. * Fixing variable dropdowns so they select the correct option. (#1184) * Always open flyout fields are editable. * Fixing the tree service so it doesn't treat unknown block deletion (#1182) as an error, and turning off keypresses on the workspace when the variable modals are open. * Revamping mostly gesture tests. * Merge master to develop (#1189) * Merge master into develop (#1063) - pick up translation changes - clean up trailing spaces * Rebuild for translations * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Cherrypick a fix for #1069 and rebuild (#1075) * Fix #1069 (#1073) * rebuild * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Rebuild compressed files * Add some translatewiki annotations back into msg/messages.js * Rebuild msg files * Fix flyout dropdown bug. * Fix python and php procedures. * Dropdown Create does not create a missing variable twice. * Fire field variable change event with variable value, not name. * Fix #1160 (#1197) Fix "Connection UI Effect not playing on block connect" * Only add a block in the flyout if it is not disabled (#1204) * Only add a block in the flyout if it is not disabled * PR feedback * remove previous fix * Fix issue with compression stripping dropdown options (#1207) * Change the build process to be deterministic (#1154) * Change the Blockly build process to be deterministic across machines. * A couple more missed locations for deterministic ordering. * Modularize Rename Variable prompt and allow custom context menus for flyout. * Adding BlocklyDevTools.Analytics (#1217) Adding BlocklyDevTools.Analytics, an interface for integrating an analytics library to track basic usage, including: * navigation. * saving, importing, and exporting. * warnings and errors. * Modify the colour_rgb function to match other languages (#1210) There are two tests fail before modifying the color_rgb function because it behaves differently in Dart and in other languages. In Dart, this function takes parameters ranging from 0 to 1.0 while in other languages such as Lua the counterpart function takes parameters ranging from 0 to 100. Now I have modified it to let it behave the same as other languages. * Fix code generation for procedures (PHP, Python). * Typo in a deprecated variable's value (#1228) Misspelled variable name, should be Blockly.PROCEDURE_CATEGORY_NAME. * Use the same math for dragging blocks out of horizontal and vertical flyouts. (#1215) * Use the same math for dragging blocks out of horizontal and vertical flyouts. * Update flyout_base.js * Add VariableType checks. * Add opt_type to Blockly.Variables.CreateVariable. * Fix Blockly.Procedures.isNameUsed return values. Add tests for Procedures.isNameUsed() so this bug never happens again. * Compatibility for Closure Compiler. (#1240) * Rebuild all the things * Master to develop 07 31 17 (#1243) * Rebuild for master push * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Enable google/blockly with continuous build on travis ci (#1023) * create .travis for ci job * initial checkin for blocky-web travis ci job * rename file to .travis.yaml for typo * remove after_script * added cache * rename .travis.yaml to .travis.yml * Update .travis.yml * include build script * fix yaml file format issue * debug install part * debug build issue * Update .travis.yml * remove cache for now * Update .travis.yml * Update .travis.yml * Update .travis.yml * more debug info * Update .travis.yml * Update .travis.yml * fix typo * installing chrome browser * remove chrome setting config * run build.py as part of npm install * Update .travis.yml * update karma dependency * use karma as test runner * fix typo * remove karma test for now * Update .travis.yml * Update package.json * add npm test target * add browserstack-runner depdendency * update browser support * fix typo for test target * fix chrome typo * added closure dependency * add google-closure-library * include blockly_uncompressed.js and core.js dependency * uncomment out core/*.js files * add kama job as part of install * remove browserstack add on for now * fix karma config typo * add karma-closure * add os support * remove typo config * include more closure files * change os back to linux * use closure-library from node_modules * change log level back to INFO * change npm test target to use open browser command instead of karma * change travis test target to use open command instead of karma * list current directory * find what's in current dir * typo command * Update .travis.yml * typo again * open right index.html * use right path for index.html * xdg-open to open default browser on travis * exit browser after 5s wait * change timeout to 1 min * exit after opening up browser * use browser only * use karma * remove un-needed dependency * clean up script section * fix typo * update build status on readme * initial commit for selenium integration tests * update selenium jar path * fix test_runner.js typo * add more debug info * check java version * add && instead of 9288 * fix java path * add logic to check if selenium is running or not * add some deugging info * initial commit to get chromedriver * add chromedriver flag * add get_chromedriver.sh to package.json and .travel * change browser to chrome for now * fix path issue * update chromdriver path * fix path issue again * more debugging * add debug msg * fix typo * minor fix for getting chromedriver * install latest chrome browser * clean up pakcage.json * use npm target for test run * remove removing trailing comma * fix another trailing comma * updated travis test target * clean up scripts * not sure nmp run preinstall * redirect selenium log to tmp file * revert writing console log to file * update test summary * more clean up * minor clean up before pull request * resolved closure-library conflict 1. add closure-library to dependencies instead of devDependencies. 2. add lint back in scripts block * fix typo (adding comma) in script section * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Merge develop into master (#1064) * Adding new minimap demo * Basic code style changes. Adding a few more comments. Return early if disableScrollChange in onScrollChange listener. * Adding horizontal scrolling. Changed scroll change callbacks from onScroll_ to setHandlePosition. onScroll_ is not challed when workspace is dragged. * Registering mousemove and mouseup listener in mousedown event. Mousemove and Mouseup events are now listening over document. * Adding the remove variable modal and functionality to accessible Blockly. (#1011) * Minimap position bug fix for browsers other than chrome. Added touch support. * Adding an add variable modal to accessible Blockly. (#1015) * Adding the remove variable modal and functionality to accessible Blockly. * Adding the add variable modal for accessible Blockly. * Block browser context menu in the toolbox and flyout * Add links to the dev registration form and contributor guidelines * Miscellaneous comment cleanup * Adding the common modal class. (#1017) Centralizes accessible modal behavior. * - Changed error message referencing 'procedure' instead of 'function' (#1019) - Added iOS specific UI messages - Fixed bug with js_to_json.py script where it didn't recognize ' character * - Allows use of Blockly's messaging format for category name, colour,… (#1028) ...in toolbox XML. - Updated code editor demo to use this message format - Re-built blockly_compressed.js * Making text_count use a text color (like text_length, which also returns a number). (#1027) * Enable google/blockly with continuous build on travis ci (#1023) (#1035) * create .travis for ci job * initial checkin for blocky-web travis ci job * rename file to .travis.yaml for typo * remove after_script * added cache * rename .travis.yaml to .travis.yml * Update .travis.yml * include build script * fix yaml file format issue * debug install part * debug build issue * Update .travis.yml * remove cache for now * Update .travis.yml * Update .travis.yml * Update .travis.yml * more debug info * Update .travis.yml * Update .travis.yml * fix typo * installing chrome browser * remove chrome setting config * run build.py as part of npm install * Update .travis.yml * update karma dependency * use karma as test runner * fix typo * remove karma test for now * Update .travis.yml * Update package.json * add npm test target * add browserstack-runner depdendency * update browser support * fix typo for test target * fix chrome typo * added closure dependency * add google-closure-library * include blockly_uncompressed.js and core.js dependency * uncomment out core/*.js files * add kama job as part of install * remove browserstack add on for now * fix karma config typo * add karma-closure * add os support * remove typo config * include more closure files * change os back to linux * use closure-library from node_modules * change log level back to INFO * change npm test target to use open browser command instead of karma * change travis test target to use open command instead of karma * list current directory * find what's in current dir * typo command * Update .travis.yml * typo again * open right index.html * use right path for index.html * xdg-open to open default browser on travis * exit browser after 5s wait * change timeout to 1 min * exit after opening up browser * use browser only * use karma * remove un-needed dependency * clean up script section * fix typo * update build status on readme * initial commit for selenium integration tests * update selenium jar path * fix test_runner.js typo * add more debug info * check java version * add && instead of 9288 * fix java path * add logic to check if selenium is running or not * add some deugging info * initial commit to get chromedriver * add chromedriver flag * add get_chromedriver.sh to package.json and .travel * change browser to chrome for now * fix path issue * update chromdriver path * fix path issue again * more debugging * add debug msg * fix typo * minor fix for getting chromedriver * install latest chrome browser * clean up pakcage.json * use npm target for test run * remove removing trailing comma * fix another trailing comma * updated travis test target * clean up scripts * not sure nmp run preinstall * redirect selenium log to tmp file * revert writing console log to file * update test summary * more clean up * minor clean up before pull request * resolved closure-library conflict 1. add closure-library to dependencies instead of devDependencies. 2. add lint back in scripts block * fix typo (adding comma) in script section * Renames Blockly.workspaceDragSurface to Blockly.WorkspaceDragSurface. Fixes #880. * Ensure useDragSurface is a boolean. Fixed #988 * use pretest instead of preinstall in package.json (#1043) * cherry pick for pretest fix * put pretest target to test_setup.sh * fix conflict * cherry pick for get_chromedriver.sh * add some sleep to wait download to finish * use node.js stable * use npm test target * field_angle renders degree symbol consistently. Fixes #973 * bumpNeighbours_ function moved to block_svg. Fixed #1009 * Update RegEx in js-to-json to match windowi eol (#1050) The current regex only works with the "\n" line endings as it expects no characters after the optional ";" at the end of the line. In windows, if it adds the "\r" it counts as a characters and is not part of the line terminator so it doesn't match. * Fix French translation of "colour with rgb" block (#1053) "colorier", which is currently used, is a verb and proposed "couleur" is a noun: the block in question does not change colour of anything, it creates new colour instead, thus noun is more applicable. Also, noun is used in French translation of "random colour" block: "couleur aléatoire". * Enforcing non-empty names on value inputs and statement inputs. (#1054) * Correcting #1054 (#1056) single quotes. better logic. * Created a variable model with name, id, and type. Created a jsunit test file for variable model. * Change how blockly handles cursors. The old way was quite slow becau… (#1057) * Change how blockly handles cursors. The old way was quite slow because it changed the stylesheet directly. See issue #981 for more details on implementation and tradeoffs. This changes makes the following high level changes: deprecate Blockly.Css.setCursor, use built in open and closed hand cursor instead of custom .cur files, add css to draggable objects to set the open and closed hand cursors. * Rebuild blockly_uncompressed to pick up a testing change to make travis happy. Fix a build warning from a multi-line string in the process. (#1059) * Merge master into develop (#1063) - pick up translation changes - clean up trailing spaces * Rebuild for translations * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Cherrypick a fix for #1069 and rebuild (#1075) * Fix #1069 (#1073) * rebuild * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Develop to master (#1209) * Adding new minimap demo * Basic code style changes. Adding a few more comments. Return early if disableScrollChange in onScrollChange listener. * Adding horizontal scrolling. Changed scroll change callbacks from onScroll_ to setHandlePosition. onScroll_ is not challed when workspace is dragged. * Registering mousemove and mouseup listener in mousedown event. Mousemove and Mouseup events are now listening over document. * Adding the remove variable modal and functionality to accessible Blockly. (#1011) * Minimap position bug fix for browsers other than chrome. Added touch support. * Adding an add variable modal to accessible Blockly. (#1015) * Adding the remove variable modal and functionality to accessible Blockly. * Adding the add variable modal for accessible Blockly. * Block browser context menu in the toolbox and flyout * Add links to the dev registration form and contributor guidelines * Miscellaneous comment cleanup * Adding the common modal class. (#1017) Centralizes accessible modal behavior. * - Changed error message referencing 'procedure' instead of 'function' (#1019) - Added iOS specific UI messages - Fixed bug with js_to_json.py script where it didn't recognize ' character * - Allows use of Blockly's messaging format for category name, colour,… (#1028) ...in toolbox XML. - Updated code editor demo to use this message format - Re-built blockly_compressed.js * Making text_count use a text color (like text_length, which also returns a number). (#1027) * Enable google/blockly with continuous build on travis ci (#1023) (#1035) * create .travis for ci job * initial checkin for blocky-web travis ci job * rename file to .travis.yaml for typo * remove after_script * added cache * rename .travis.yaml to .travis.yml * Update .travis.yml * include build script * fix yaml file format issue * debug install part * debug build issue * Update .travis.yml * remove cache for now * Update .travis.yml * Update .travis.yml * Update .travis.yml * more debug info * Update .travis.yml * Update .travis.yml * fix typo * installing chrome browser * remove chrome setting config * run build.py as part of npm install * Update .travis.yml * update karma dependency * use karma as test runner * fix typo * remove karma test for now * Update .travis.yml * Update package.json * add npm test target * add browserstack-runner depdendency * update browser support * fix typo for test target * fix chrome typo * added closure dependency * add google-closure-library * include blockly_uncompressed.js and core.js dependency * uncomment out core/*.js files * add kama job as part of install * remove browserstack add on for now * fix karma config typo * add karma-closure * add os support * remove typo config * include more closure files * change os back to linux * use closure-library from node_modules * change log level back to INFO * change npm test target to use open browser command instead of karma * change travis test target to use open command instead of karma * list current directory * find what's in current dir * typo command * Update .travis.yml * typo again * open right index.html * use right path for index.html * xdg-open to open default browser on travis * exit browser after 5s wait * change timeout to 1 min * exit after opening up browser * use browser only * use karma * remove un-needed dependency * clean up script section * fix typo * update build status on readme * initial commit for selenium integration tests * update selenium jar path * fix test_runner.js typo * add more debug info * check java version * add && instead of 9288 * fix java path * add logic to check if selenium is running or not * add some deugging info * initial commit to get chromedriver * add chromedriver flag * add get_chromedriver.sh to package.json and .travel * change browser to chrome for now * fix path issue * update chromdriver path * fix path issue again * more debugging * add debug msg * fix typo * minor fix for getting chromedriver * install latest chrome browser * clean up pakcage.json * use npm target for test run * remove removing trailing comma * fix another trailing comma * updated travis test target * clean up scripts * not sure nmp run preinstall * redirect selenium log to tmp file * revert writing console log to file * update test summary * more clean up * minor clean up before pull request * resolved closure-library conflict 1. add closure-library to dependencies instead of devDependencies. 2. add lint back in scripts block * fix typo (adding comma) in script section * Renames Blockly.workspaceDragSurface to Blockly.WorkspaceDragSurface. Fixes #880. * Ensure useDragSurface is a boolean. Fixed #988 * use pretest instead of preinstall in package.json (#1043) * cherry pick for pretest fix * put pretest target to test_setup.sh * fix conflict * cherry pick for get_chromedriver.sh * add some sleep to wait download to finish * use node.js stable * use npm test target * field_angle renders degree symbol consistently. Fixes #973 * bumpNeighbours_ function moved to block_svg. Fixed #1009 * Update RegEx in js-to-json to match windowi eol (#1050) The current regex only works with the "\n" line endings as it expects no characters after the optional ";" at the end of the line. In windows, if it adds the "\r" it counts as a characters and is not part of the line terminator so it doesn't match. * Fix French translation of "colour with rgb" block (#1053) "colorier", which is currently used, is a verb and proposed "couleur" is a noun: the block in question does not change colour of anything, it creates new colour instead, thus noun is more applicable. Also, noun is used in French translation of "random colour" block: "couleur aléatoire". * Enforcing non-empty names on value inputs and statement inputs. (#1054) * Correcting #1054 (#1056) single quotes. better logic. * Created a variable model with name, id, and type. Created a jsunit test file for variable model. * Change how blockly handles cursors. The old way was quite slow becau… (#1057) * Change how blockly handles cursors. The old way was quite slow because it changed the stylesheet directly. See issue #981 for more details on implementation and tradeoffs. This changes makes the following high level changes: deprecate Blockly.Css.setCursor, use built in open and closed hand cursor instead of custom .cur files, add css to draggable objects to set the open and closed hand cursors. * Rebuild blockly_uncompressed to pick up a testing change to make travis happy. Fix a build warning from a multi-line string in the process. (#1059) * Merge master into develop (#1063) - pick up translation changes - clean up trailing spaces * use goog.string.startswith instead of string.startswith (#1065) * New jsinterpreter demo includes wait block. Both demos have improved UI for clarity. (#1001) Refactor of interpreter demo * Renamed demos/interpreter/index.html as demos/interpreter/step-execution.html (including redirect), and added demos/interpreter/async-execution.html. * Refactored code to automatically generate/parse the blocks, eliminating the need for a "Parse JavaScript" button. Code is still shown in alert upon stepping to the first statement. Print statements now write to output <textarea> instead of modal dialogs. * VariableMap and functions added. * Create separate file for VariableMap and its functions. * Fix #1069 (#1073) * VariableMap and functions added. * Fix #1051 (#1084) * Improve errors when validating JSON block definitions. (#1086) goog.asserts to not run from blockly_compressed.js. User data validation should always run. * Dragging changes, rebased on develop (#1078) * Add block drag surface translateSurfaceBy * Add dragged connection manager * Add gesture.js * Add GestureHandler * Implemented gesture skeleton * Most basic workspace dragging * Add dragged connection manager * cleanup * doc * more cleanup * Add gesture handler * Add translateSurfaceInternal * core/block_dragger.js * cleanup * Pull in changes to dragged connection manager * Pull in changes to dragged connection manager * comments * more annotations * Add workspace dragger * Add coordinate annotations * Start on block dragging * Limit number of concurrent gestures * Add some TODOs * start using dragged connection manager * Set origin correctly for dragging blocks * Connect or delete at the end of a block drag. * cleanup * handle field clicks and block + workspace right-clicks * move code into BlockDragger class, but still reach into Gesture internals a lot * Clean up block dragger * Call blockDragger constructor with correct arguments * Enable block dragging in a mutator workspace * Add workspace dragger * click todos * Drag flyout with background * more dragging from flyout * nit * fix dragging from flyouts * Remove unused code and rename gestureHandler to gestureDB * Rename gesture handler * Added some jsdoc in gesture.js * Update some docs * Move some code to block_svg and clean up code * Lots of coordinate annotations * Fix block dragging when zoomed. * Remove built files from branch * More dragging work (#1026) -- Drag bubbles while dragging blocks -- Use bindEventWithChecks to work in touch on Android. Not tested anywhere else yet. -- Handle dragging blocks while zoomed -- Handle dragging blocks in mutators -- Handle right-clicks (I hope) -- Removed lots of unused code * More dragging work (#1048) - Removed gestureDB - Removing uses of terminateDrag - Cleaned up disposal code * Dragging bugfixes (#1058) - Get rid of flyout.dragMode_ and blockly.dragMode_ - Make drags from the flyout start from the top block in the group - Block tooltips from being scheduled or shown during gestures - Don't resize mutator bubbles mid-drag * Fix events in new dragging (#1060) * rebuild for testing * unbuild * Fix events * rebuild * Fix up cursors * Use language files from develop * Remove handled TODOS * attempt to fix IE rerendering bug, and recalculate workspace positions on scroll * Rebuild all the things * Comment cleanup; annotations; delete unused variables. * Tidy up context menu code. (#1081) * add osx travis test run job (#1074) * Names are correctly fetched from VariableModels! * add more wait time for test setup (#1091) * Work around timing issue with travis osx issue (#1092) * add more wait time for test setup * increase selenium wait time * add more wait * Fix #1077 by adding a rule to cover the toolbox labels too. (#1099) * Assign variable UUID to field_variable dropdown. * Change registration link to a static one (#1106) This lets us redirect to a different form if we change it in the future. * Edit generators to read in Variable Models. * Add VariableMap requirement to workspace. * Changed parameter name in workspace for clarity. * Add type, id, and info to the generated xml. Add xml tests for fieldToDom. Update workspace tests to pass with new changes. * Fix apostrophe in tooltips and helpurls (#1111) * Click events on shadow blocks have the correct id (#1089) * Add image_onclick option (#1080) * Cleanup: semicolons, spacing, etc. (#1116) * Spelling. Spelling is hard. * Add variable info to xml generated in variables.js * Add missing CLAs info to the contributing file (#1119) * Add missing CLAs info to the contributing file * Added larger changes paragraph * Replacing latest prettifier hosted in repo with latest version at rawgit CDN. (#1120) * Forgot update code demo (#1121) * Move audio code to a new file (#1122) * move audio code to a new file * dispose * null check * Make flyout get variables from target workspace's variableMap. * Require VariableModel in field_variable.js. * Update contributing.md (#1126) * Include variables at top of serialization. * Move blockSvg.getHeightWidth to block_render_svg.js (#1118) * Deserialization variables at top. * Create grid object (#1131) * Create grid object * Doc * Units! Thanks @RoboErikG * Appease eslint: semicolons and such. * Deserialization variables at top. * Setting up accessible Blockly for Closure compilation. (#1134) Moving closure compilation into the build file. Fixing up goog.requires for accessible blockly. Adding accessible to the build script. * Fix #1109 * Adding command-line options to the buildscript. (#1136) * Fix for travis-ci testing failure (#1141) It looks like the default configuration for Travis might have changed. Adding a manual step to install webdriverio. * Convert text_join block to JSON + mutator format (#1140) * Clean up and create test utilities file. * Add BLOCK events and constructor tests. * Convert more text blocks to json (#1147) * Convert more text blocks to json Converts the mutator blocks for text_join and the text_append block to JSON format. * Fixing modals so they're announced on focus, and changing variables t… (#1030) * Fixing modals so they're announced on focus, and changing variables to only react to enter, not onChange. * Adding a temp index. * Whoops - added it in the wrong spot. * Adding automatically-generated variable lists to the accessible toolbox. (#1149) Fixing a bug with the core-only uncompressed file not finding its own directory. * Field Variable setValue() looks up variable. * Wrap Error in Try Finally Block. * Changing the build file to allow forced rebuilding of the msg files. (#1158) * Split flyout into flyout_base, flyout_horizontal, and flyou_vertical * Rename flyout to flyout_base * flyout_base minus horizontal and vertical code * Add flyout_vertical and flyout_horizontal * review fixes + toolbox and workspace use * Fix hat block case * rebuild uncompressed * Fix travis problem * Fix build problem * Add VAR events. * Correctly named block events called. * Fire VAR events and test. * Create utility function for checking variable values. * In DropdownCreate check for Msg.DELETE_VARIABLE. * Test Delete Variable Twice. * Convert more text blocks to JSON format (#1163) * Convert more text blocks to JSON format Converts text_charAt, text_indexOf, text_isEmpty, and text_length to JSON. Includes a rebuild to pick up message changes. * Fixing the accessible variable stuff so it interacts correctly with (#1170) variableMap. * createVariable in workspaceSvg takes in id and type. * Followup RefreshToolbox. * Fixing an error with block messages and ordering. (#1171) * Remove out of date todo comments. * Fix checkbox delete bug. * Fixing variable dropdowns so they select the correct option. (#1184) * Always open flyout fields are editable. * Fixing the tree service so it doesn't treat unknown block deletion (#1182) as an error, and turning off keypresses on the workspace when the variable modals are open. * Revamping mostly gesture tests. * Merge master to develop (#1189) * Merge master into develop (#1063) - pick up translation changes - clean up trailing spaces * Rebuild for translations * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Cherrypick a fix for #1069 and rebuild (#1075) * Fix #1069 (#1073) * rebuild * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Rebuild compressed files * Add some translatewiki annotations back into msg/messages.js * Rebuild msg files * Fix flyout dropdown bug. * Fix python and php procedures. * Dropdown Create does not create a missing variable twice. * Fire field variable change event with variable value, not name. * Fix #1160 (#1197) Fix "Connection UI Effect not playing on block connect" * Only add a block in the flyout if it is not disabled (#1204) * Only add a block in the flyout if it is not disabled * PR feedback * remove previous fix * Fix issue with compression stripping dropdown options (#1207) * Get some accessible files back from develop * rebuild * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Rebuild all the things * Fix Blockly.Procedures.isNameUsed return values. Add tests for Procedures.isNameUsed() so this bug never happens again. * rebuild * rebuild
1374 lines
50 KiB
JavaScript
1374 lines
50 KiB
JavaScript
/**
|
|
* @license
|
|
* Blockly Demos: Block Factory
|
|
*
|
|
* Copyright 2016 Google Inc.
|
|
* https://developers.google.com/blockly/
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
/**
|
|
* @fileoverview Contains the controller code for workspace factory. Depends
|
|
* on the model and view objects (created as internal variables) and interacts
|
|
* with previewWorkspace and toolboxWorkspace (internal references stored to
|
|
* both). Also depends on standard_categories.js for standard Blockly
|
|
* categories. Provides the functionality for the actions the user can initiate:
|
|
* - adding and removing categories
|
|
* - switching between categories
|
|
* - printing and downloading configuration xml
|
|
* - updating the preview workspace
|
|
* - changing a category name
|
|
* - moving the position of a category.
|
|
*
|
|
* @author Emma Dauterman (evd2014)
|
|
*/
|
|
|
|
goog.require('BlocklyDevTools.Analytics');
|
|
goog.require('FactoryUtils');
|
|
goog.require('StandardCategories');
|
|
|
|
|
|
/**
|
|
* Class for a WorkspaceFactoryController
|
|
* @param {string} toolboxName Name of workspace toolbox XML.
|
|
* @param {string} toolboxDiv Name of div to inject toolbox workspace in.
|
|
* @param {string} previewDiv Name of div to inject preview workspace in.
|
|
* @constructor
|
|
*/
|
|
WorkspaceFactoryController = function(toolboxName, toolboxDiv, previewDiv) {
|
|
// Toolbox XML element for the editing workspace.
|
|
this.toolbox = document.getElementById(toolboxName);
|
|
|
|
// Workspace for user to drag blocks in for a certain category.
|
|
this.toolboxWorkspace = Blockly.inject(toolboxDiv,
|
|
{grid:
|
|
{spacing: 25,
|
|
length: 3,
|
|
colour: '#ccc',
|
|
snap: true},
|
|
media: '../../media/',
|
|
toolbox: this.toolbox
|
|
});
|
|
|
|
// Workspace for user to preview their changes.
|
|
this.previewWorkspace = Blockly.inject(previewDiv,
|
|
{grid:
|
|
{spacing: 25,
|
|
length: 3,
|
|
colour: '#ccc',
|
|
snap: true},
|
|
media: '../../media/',
|
|
toolbox: '<xml></xml>',
|
|
zoom:
|
|
{controls: true,
|
|
wheel: true}
|
|
});
|
|
|
|
// Model to keep track of categories and blocks.
|
|
this.model = new WorkspaceFactoryModel();
|
|
// Updates the category tabs.
|
|
this.view = new WorkspaceFactoryView();
|
|
// Generates XML for categories.
|
|
this.generator = new WorkspaceFactoryGenerator(this.model);
|
|
// Tracks which editing mode the user is in. Toolbox mode on start.
|
|
this.selectedMode = WorkspaceFactoryController.MODE_TOOLBOX;
|
|
// True if key events are enabled, false otherwise.
|
|
this.keyEventsEnabled = true;
|
|
// True if there are unsaved changes in the toolbox, false otherwise.
|
|
this.hasUnsavedToolboxChanges = false;
|
|
// True if there are unsaved changes in the preloaded blocks, false otherwise.
|
|
this.hasUnsavedPreloadChanges = false;
|
|
};
|
|
|
|
// Toolbox editing mode. Changes the user makes to the workspace updates the
|
|
// toolbox.
|
|
WorkspaceFactoryController.MODE_TOOLBOX = 'toolbox';
|
|
// Pre-loaded workspace editing mode. Changes the user makes to the workspace
|
|
// udpates the pre-loaded blocks.
|
|
WorkspaceFactoryController.MODE_PRELOAD = 'preload';
|
|
|
|
/**
|
|
* Currently prompts the user for a name, checking that it's valid (not used
|
|
* before), and then creates a tab and switches to it.
|
|
*/
|
|
WorkspaceFactoryController.prototype.addCategory = function() {
|
|
// Transfers the user's blocks to a flyout if it's the first category created.
|
|
this.transferFlyoutBlocksToCategory();
|
|
|
|
// After possibly creating a category, check again if it's the first category.
|
|
var isFirstCategory = !this.model.hasElements();
|
|
// Get name from user.
|
|
var name = this.promptForNewCategoryName('Enter the name of your new category:');
|
|
if (!name) { // Exit if cancelled.
|
|
return;
|
|
}
|
|
// Create category.
|
|
this.createCategory(name);
|
|
// Switch to category.
|
|
this.switchElement(this.model.getCategoryIdByName(name));
|
|
|
|
// Sets the default options for injecting the workspace
|
|
// when there are categories if adding the first category.
|
|
if (isFirstCategory) {
|
|
this.view.setCategoryOptions(this.model.hasElements());
|
|
this.generateNewOptions();
|
|
}
|
|
// Update preview.
|
|
this.updatePreview();
|
|
};
|
|
|
|
/**
|
|
* Helper method for addCategory. Adds a category to the view given a name, ID,
|
|
* and a boolean for if it's the first category created. Assumes the category
|
|
* has already been created in the model. Does not switch to category.
|
|
* @param {string} name Name of category being added.
|
|
* @param {string} id The ID of the category being added.
|
|
*/
|
|
WorkspaceFactoryController.prototype.createCategory = function(name) {
|
|
// Create empty category
|
|
var category = new ListElement(ListElement.TYPE_CATEGORY, name);
|
|
this.model.addElementToList(category);
|
|
// Create new category.
|
|
var tab = this.view.addCategoryRow(name, category.id);
|
|
this.addClickToSwitch(tab, category.id);
|
|
};
|
|
|
|
/**
|
|
* Given a tab and a ID to be associated to that tab, adds a listener to
|
|
* that tab so that when the user clicks on the tab, it switches to the
|
|
* element associated with that ID.
|
|
* @param {!Element} tab The DOM element to add the listener to.
|
|
* @param {string} id The ID of the element to switch to when tab is clicked.
|
|
*/
|
|
WorkspaceFactoryController.prototype.addClickToSwitch = function(tab, id) {
|
|
var self = this;
|
|
var clickFunction = function(id) { // Keep this in scope for switchElement.
|
|
return function() {
|
|
self.switchElement(id);
|
|
};
|
|
};
|
|
this.view.bindClick(tab, clickFunction(id));
|
|
};
|
|
|
|
/**
|
|
* Transfers the blocks in the user's flyout to a new category if
|
|
* the user is creating their first category and their workspace is not
|
|
* empty. Should be called whenever it is possible to switch from single flyout
|
|
* to categories (not including importing).
|
|
*/
|
|
WorkspaceFactoryController.prototype.transferFlyoutBlocksToCategory =
|
|
function() {
|
|
// Saves the user's blocks from the flyout in a category if there is no
|
|
// toolbox and the user has dragged in blocks.
|
|
if (!this.model.hasElements() &&
|
|
this.toolboxWorkspace.getAllBlocks().length > 0) {
|
|
// Create the new category.
|
|
this.createCategory('Category 1', true);
|
|
// Set the new category as selected.
|
|
var id = this.model.getCategoryIdByName('Category 1');
|
|
this.model.setSelectedById(id);
|
|
this.view.setCategoryTabSelection(id, true);
|
|
// Allow user to use the default options for injecting with categories.
|
|
this.view.setCategoryOptions(this.model.hasElements());
|
|
this.generateNewOptions();
|
|
// Update preview here in case exit early.
|
|
this.updatePreview();
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Attached to "-" button. Checks if the user wants to delete
|
|
* the current element. Removes the element and switches to another element.
|
|
* When the last element is removed, it switches to a single flyout mode.
|
|
*/
|
|
WorkspaceFactoryController.prototype.removeElement = function() {
|
|
// Check that there is a currently selected category to remove.
|
|
if (!this.model.getSelected()) {
|
|
return;
|
|
}
|
|
|
|
// Check if user wants to remove current category.
|
|
var check = confirm('Are you sure you want to delete the currently selected '
|
|
+ this.model.getSelected().type + '?');
|
|
if (!check) { // If cancelled, exit.
|
|
return;
|
|
}
|
|
|
|
var selectedId = this.model.getSelectedId();
|
|
var selectedIndex = this.model.getIndexByElementId(selectedId);
|
|
// Delete element visually.
|
|
this.view.deleteElementRow(selectedId, selectedIndex);
|
|
// Delete element in model.
|
|
this.model.deleteElementFromList(selectedIndex);
|
|
|
|
// Find next logical element to switch to.
|
|
var next = this.model.getElementByIndex(selectedIndex);
|
|
if (!next && this.model.hasElements()) {
|
|
next = this.model.getElementByIndex(selectedIndex - 1);
|
|
}
|
|
var nextId = next ? next.id : null;
|
|
|
|
// Open next element.
|
|
this.clearAndLoadElement(nextId);
|
|
|
|
// If no element to switch to, display message, clear the workspace, and
|
|
// set a default selected element not in toolbox list in the model.
|
|
if (!nextId) {
|
|
alert('You currently have no categories or separators. All your blocks' +
|
|
' will be displayed in a single flyout.');
|
|
this.toolboxWorkspace.clear();
|
|
this.toolboxWorkspace.clearUndo();
|
|
this.model.createDefaultSelectedIfEmpty();
|
|
}
|
|
// Update preview.
|
|
this.updatePreview();
|
|
};
|
|
|
|
/**
|
|
* Gets a valid name for a new category from the user.
|
|
* @param {string} promptString Prompt for the user to enter a name.
|
|
* @param {string=} opt_oldName The current name.
|
|
* @return {string?} Valid name for a new category, or null if cancelled.
|
|
*/
|
|
WorkspaceFactoryController.prototype.promptForNewCategoryName =
|
|
function(promptString, opt_oldName) {
|
|
var defaultName = opt_oldName;
|
|
do {
|
|
var name = prompt(promptString, defaultName);
|
|
if (!name) { // If cancelled.
|
|
return null;
|
|
}
|
|
defaultName = name;
|
|
} while (this.model.hasCategoryByName(name));
|
|
return name;
|
|
};
|
|
|
|
/**
|
|
* Switches to a new tab for the element given by ID. Stores XML and blocks
|
|
* to reload later, updates selected accordingly, and clears the workspace
|
|
* and clears undo, then loads the new element.
|
|
* @param {string} id ID of tab to be opened, must be valid element ID.
|
|
*/
|
|
WorkspaceFactoryController.prototype.switchElement = function(id) {
|
|
// Disables events while switching so that Blockly delete and create events
|
|
// don't update the preview repeatedly.
|
|
Blockly.Events.disable();
|
|
// Caches information to reload or generate XML if switching to/from element.
|
|
// Only saves if a category is selected.
|
|
if (this.model.getSelectedId() != null && id != null) {
|
|
this.model.getSelected().saveFromWorkspace(this.toolboxWorkspace);
|
|
}
|
|
// Load element.
|
|
this.clearAndLoadElement(id);
|
|
// Enable Blockly events again.
|
|
Blockly.Events.enable();
|
|
};
|
|
|
|
/**
|
|
* Switches to a new tab for the element by ID. Helper for switchElement.
|
|
* Updates selected, clears the workspace and clears undo, loads a new element.
|
|
* @param {string} id ID of category to load.
|
|
*/
|
|
WorkspaceFactoryController.prototype.clearAndLoadElement = function(id) {
|
|
// Unselect current tab if switching to and from an element.
|
|
if (this.model.getSelectedId() != null && id != null) {
|
|
this.view.setCategoryTabSelection(this.model.getSelectedId(), false);
|
|
}
|
|
|
|
// If switching to another category, set category selection in the model and
|
|
// view.
|
|
if (id != null) {
|
|
// Set next category.
|
|
this.model.setSelectedById(id);
|
|
|
|
// Clears workspace and loads next category.
|
|
this.clearAndLoadXml_(this.model.getSelectedXml());
|
|
|
|
// Selects the next tab.
|
|
this.view.setCategoryTabSelection(id, true);
|
|
|
|
// Order blocks as shown in flyout.
|
|
this.toolboxWorkspace.cleanUp();
|
|
|
|
// Update category editing buttons.
|
|
this.view.updateState(this.model.getIndexByElementId
|
|
(this.model.getSelectedId()), this.model.getSelected());
|
|
} else {
|
|
// Update category editing buttons for no categories.
|
|
this.view.updateState(-1, null);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Tied to "Export" button. Gets a file name from the user and downloads
|
|
* the corresponding configuration XML to that file.
|
|
* @param {string} exportMode The type of file to export
|
|
* (WorkspaceFactoryController.MODE_TOOLBOX for the toolbox configuration,
|
|
* and WorkspaceFactoryController.MODE_PRELOAD for the pre-loaded workspace
|
|
* configuration)
|
|
*/
|
|
WorkspaceFactoryController.prototype.exportXmlFile = function(exportMode) {
|
|
// Get file name.
|
|
if (exportMode == WorkspaceFactoryController.MODE_TOOLBOX) {
|
|
var fileName = prompt('File Name for toolbox XML:', 'toolbox.xml');
|
|
} else {
|
|
var fileName = prompt('File Name for pre-loaded workspace XML:',
|
|
'workspace.xml');
|
|
}
|
|
if (!fileName) { // If cancelled.
|
|
return;
|
|
}
|
|
|
|
// Generate XML.
|
|
if (exportMode == WorkspaceFactoryController.MODE_TOOLBOX) {
|
|
// Export the toolbox XML.
|
|
var configXml = Blockly.Xml.domToPrettyText
|
|
(this.generator.generateToolboxXml());
|
|
this.hasUnsavedToolboxChanges = false;
|
|
} else if (exportMode == WorkspaceFactoryController.MODE_PRELOAD) {
|
|
// Export the pre-loaded block XML.
|
|
var configXml = Blockly.Xml.domToPrettyText
|
|
(this.generator.generateWorkspaceXml());
|
|
this.hasUnsavedPreloadChanges = false;
|
|
} else {
|
|
// Unknown mode. Throw error.
|
|
var msg = "Unknown export mode: " + exportMode;
|
|
BlocklyDevTools.Analytics.onError(msg);
|
|
throw new Error(msg);
|
|
}
|
|
|
|
// Download file.
|
|
var data = new Blob([configXml], {type: 'text/xml'});
|
|
this.view.createAndDownloadFile(fileName, data);
|
|
|
|
if (exportMode == WorkspaceFactoryController.MODE_TOOLBOX) {
|
|
BlocklyDevTools.Analytics.onExport(
|
|
BlocklyDevTools.Analytics.TOOLBOX,
|
|
{ format: BlocklyDevTools.Analytics.FORMAT_XML });
|
|
} else if (exportMode == WorkspaceFactoryController.MODE_PRELOAD) {
|
|
BlocklyDevTools.Analytics.onExport(
|
|
BlocklyDevTools.Analytics.WORKSPACE_CONTENTS,
|
|
{ format: BlocklyDevTools.Analytics.FORMAT_XML });
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Export the options object to be used for the Blockly inject call. Gets a
|
|
* file name from the user and downloads the options object to that file.
|
|
*/
|
|
WorkspaceFactoryController.prototype.exportInjectFile = function() {
|
|
var fileName = prompt('File Name for starter Blockly workspace code:',
|
|
'workspace.js');
|
|
if (!fileName) { // If cancelled.
|
|
return;
|
|
}
|
|
// Generate new options to remove toolbox XML from options object (if
|
|
// necessary).
|
|
this.generateNewOptions();
|
|
var printableOptions = this.generator.generateInjectString()
|
|
var data = new Blob([printableOptions], {type: 'text/javascript'});
|
|
this.view.createAndDownloadFile(fileName, data);
|
|
|
|
BlocklyDevTools.Analytics.onExport(
|
|
BlocklyDevTools.Analytics.STARTER_CODE,
|
|
{
|
|
format: BlocklyDevTools.Analytics.FORMAT_JS,
|
|
platform: BlocklyDevTools.Analytics.PLATFORM_WEB
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Tied to "Print" button. Mainly used for debugging purposes. Prints
|
|
* the configuration XML to the console.
|
|
*/
|
|
WorkspaceFactoryController.prototype.printConfig = function() {
|
|
// Capture any changes made by user before generating XML.
|
|
this.saveStateFromWorkspace();
|
|
// Print XML.
|
|
window.console.log(Blockly.Xml.domToPrettyText
|
|
(this.generator.generateToolboxXml()));
|
|
};
|
|
|
|
/**
|
|
* Updates the preview workspace based on the toolbox workspace. If switching
|
|
* from no categories to categories or categories to no categories, reinjects
|
|
* Blockly with reinjectPreview, otherwise just updates without reinjecting.
|
|
* Called whenever a list element is created, removed, or modified and when
|
|
* Blockly move and delete events are fired. Do not call on create events
|
|
* or disabling will cause the user to "drop" their current blocks. Make sure
|
|
* that no changes have been made to the workspace since updating the model
|
|
* (if this might be the case, call saveStateFromWorkspace).
|
|
*/
|
|
WorkspaceFactoryController.prototype.updatePreview = function() {
|
|
// Disable events to stop updatePreview from recursively calling itself
|
|
// through event handlers.
|
|
Blockly.Events.disable();
|
|
|
|
// Only update the toolbox if not in read only mode.
|
|
if (!this.model.options['readOnly']) {
|
|
// Get toolbox XML.
|
|
var tree = Blockly.Options.parseToolboxTree(
|
|
this.generator.generateToolboxXml());
|
|
|
|
// No categories, creates a simple flyout.
|
|
if (tree.getElementsByTagName('category').length == 0) {
|
|
// No categories, creates a simple flyout.
|
|
if (this.previewWorkspace.toolbox_) {
|
|
this.reinjectPreview(tree); // Switch to simple flyout, expensive.
|
|
} else {
|
|
this.previewWorkspace.updateToolbox(tree);
|
|
}
|
|
} else {
|
|
// Uses categories, creates a toolbox.
|
|
if (!this.previewWorkspace.toolbox_) {
|
|
this.reinjectPreview(tree); // Create a toolbox, expensive.
|
|
} else {
|
|
// Close the toolbox before updating it so that the user has to reopen
|
|
// the flyout and see their updated toolbox (open flyout doesn't update)
|
|
this.previewWorkspace.toolbox_.clearSelection();
|
|
this.previewWorkspace.updateToolbox(tree);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Update pre-loaded blocks in the preview workspace.
|
|
this.previewWorkspace.clear();
|
|
Blockly.Xml.domToWorkspace(this.generator.generateWorkspaceXml(),
|
|
this.previewWorkspace);
|
|
|
|
// Reenable events.
|
|
Blockly.Events.enable();
|
|
};
|
|
|
|
/**
|
|
* Saves the state from the workspace depending on the current mode. Should
|
|
* be called after making changes to the workspace.
|
|
*/
|
|
WorkspaceFactoryController.prototype.saveStateFromWorkspace = function() {
|
|
if (this.selectedMode == WorkspaceFactoryController.MODE_TOOLBOX) {
|
|
// If currently editing the toolbox.
|
|
// Update flags if toolbox has been changed.
|
|
if (this.model.getSelectedXml() !=
|
|
Blockly.Xml.workspaceToDom(this.toolboxWorkspace)) {
|
|
this.hasUnsavedToolboxChanges = true;
|
|
}
|
|
|
|
this.model.getSelected().saveFromWorkspace(this.toolboxWorkspace);
|
|
|
|
} else if (this.selectedMode == WorkspaceFactoryController.MODE_PRELOAD) {
|
|
// If currently editing the pre-loaded workspace.
|
|
// Update flags if preloaded blocks have been changed.
|
|
if (this.model.getPreloadXml() !=
|
|
Blockly.Xml.workspaceToDom(this.toolboxWorkspace)) {
|
|
this.hasUnsavedPreloadChanges = true;
|
|
}
|
|
|
|
this.model.savePreloadXml(
|
|
Blockly.Xml.workspaceToDom(this.toolboxWorkspace));
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Used to completely reinject the preview workspace. This should be used only
|
|
* when switching from simple flyout to categories, or categories to simple
|
|
* flyout. More expensive than simply updating the flyout or toolbox.
|
|
* @param {!Element} Tree of XML elements
|
|
* @package
|
|
*/
|
|
WorkspaceFactoryController.prototype.reinjectPreview = function(tree) {
|
|
this.previewWorkspace.dispose();
|
|
var injectOptions = this.readOptions_();
|
|
injectOptions['toolbox'] = Blockly.Xml.domToPrettyText(tree);
|
|
this.previewWorkspace = Blockly.inject('preview_blocks', injectOptions);
|
|
Blockly.Xml.domToWorkspace(this.generator.generateWorkspaceXml(),
|
|
this.previewWorkspace);
|
|
};
|
|
|
|
/**
|
|
* Tied to "change name" button. Changes the name of the selected category.
|
|
* Continues prompting the user until they input a category name that is not
|
|
* currently in use, exits if user presses cancel.
|
|
*/
|
|
WorkspaceFactoryController.prototype.changeCategoryName = function() {
|
|
var selected = this.model.getSelected();
|
|
// Return if a category is not selected.
|
|
if (selected.type != ListElement.TYPE_CATEGORY) {
|
|
return;
|
|
}
|
|
// Get new name from user.
|
|
window.foo = selected;
|
|
var newName = this.promptForNewCategoryName('What do you want to change this'
|
|
+ ' category\'s name to?', selected.name);
|
|
if (!newName) { // If cancelled.
|
|
return;
|
|
}
|
|
// Change category name.
|
|
selected.changeName(newName);
|
|
this.view.updateCategoryName(newName, this.model.getSelectedId());
|
|
// Update preview.
|
|
this.updatePreview();
|
|
};
|
|
|
|
/**
|
|
* Tied to arrow up and arrow down buttons. Swaps with the element above or
|
|
* below the currently selected element (offset categories away from the
|
|
* current element). Updates state to enable the correct element editing
|
|
* buttons.
|
|
* @param {number} offset The index offset from the currently selected element
|
|
* to swap with. Positive if the element to be swapped with is below, negative
|
|
* if the element to be swapped with is above.
|
|
*/
|
|
WorkspaceFactoryController.prototype.moveElement = function(offset) {
|
|
var curr = this.model.getSelected();
|
|
if (!curr) { // Return if no selected element.
|
|
return;
|
|
}
|
|
var currIndex = this.model.getIndexByElementId(curr.id);
|
|
var swapIndex = this.model.getIndexByElementId(curr.id) + offset;
|
|
var swap = this.model.getElementByIndex(swapIndex);
|
|
if (!swap) { // Return if cannot swap in that direction.
|
|
return;
|
|
}
|
|
// Move currently selected element to index of other element.
|
|
// Indexes must be valid because confirmed that curr and swap exist.
|
|
this.moveElementToIndex(curr, swapIndex, currIndex);
|
|
// Update element editing buttons.
|
|
this.view.updateState(swapIndex, this.model.getSelected());
|
|
// Update preview.
|
|
this.updatePreview();
|
|
};
|
|
|
|
/**
|
|
* Moves a element to a specified index and updates the model and view
|
|
* accordingly. Helper functions throw an error if indexes are out of bounds.
|
|
* @param {!Element} element The element to move.
|
|
* @param {number} newIndex The index to insert the element at.
|
|
* @param {number} oldIndex The index the element is currently at.
|
|
*/
|
|
WorkspaceFactoryController.prototype.moveElementToIndex = function(element,
|
|
newIndex, oldIndex) {
|
|
this.model.moveElementToIndex(element, newIndex, oldIndex);
|
|
this.view.moveTabToIndex(element.id, newIndex, oldIndex);
|
|
};
|
|
|
|
/**
|
|
* Changes the color of the selected category. Return if selected element is
|
|
* a separator.
|
|
* @param {string} color The color to change the selected category. Must be
|
|
* a valid CSS string.
|
|
*/
|
|
WorkspaceFactoryController.prototype.changeSelectedCategoryColor =
|
|
function(color) {
|
|
// Return if category is not selected.
|
|
if (this.model.getSelected().type != ListElement.TYPE_CATEGORY) {
|
|
return;
|
|
}
|
|
// Change color of selected category.
|
|
this.model.getSelected().changeColor(color);
|
|
this.view.setBorderColor(this.model.getSelectedId(), color);
|
|
this.updatePreview();
|
|
};
|
|
|
|
/**
|
|
* Tied to the "Standard Category" dropdown option, this function prompts
|
|
* the user for a name of a standard Blockly category (case insensitive) and
|
|
* loads it as a new category and switches to it. Leverages StandardCategories.
|
|
*/
|
|
WorkspaceFactoryController.prototype.loadCategory = function() {
|
|
// Prompt user for the name of the standard category to load.
|
|
do {
|
|
var name = prompt('Enter the name of the category you would like to import '
|
|
+ '(Logic, Loops, Math, Text, Lists, Colour, Variables, or Functions)');
|
|
if (!name) {
|
|
return; // Exit if cancelled.
|
|
}
|
|
} while (!this.isStandardCategoryName(name));
|
|
|
|
// Load category.
|
|
this.loadCategoryByName(name);
|
|
};
|
|
|
|
/**
|
|
* Loads a Standard Category by name and switches to it. Leverages
|
|
* StandardCategories. Returns if cannot load standard category.
|
|
* @param {string} name Name of the standard category to load.
|
|
*/
|
|
WorkspaceFactoryController.prototype.loadCategoryByName = function(name) {
|
|
// Check if the user can load that standard category.
|
|
if (!this.isStandardCategoryName(name)) {
|
|
return;
|
|
}
|
|
if (this.model.hasVariables() && name.toLowerCase() == 'variables') {
|
|
alert('A Variables category already exists. You cannot create multiple' +
|
|
' variables categories.');
|
|
return;
|
|
}
|
|
if (this.model.hasProcedures() && name.toLowerCase() == 'functions') {
|
|
alert('A Functions category already exists. You cannot create multiple' +
|
|
' functions categories.');
|
|
return;
|
|
}
|
|
// Check if the user can create a category with that name.
|
|
var standardCategory = StandardCategories.categoryMap[name.toLowerCase()]
|
|
if (this.model.hasCategoryByName(standardCategory.name)) {
|
|
alert('You already have a category with the name ' + standardCategory.name
|
|
+ '. Rename your category and try again.');
|
|
return;
|
|
}
|
|
// Transfers current flyout blocks to a category if it's the first category
|
|
// created.
|
|
this.transferFlyoutBlocksToCategory();
|
|
|
|
var isFirstCategory = !this.model.hasElements();
|
|
// Copy the standard category in the model.
|
|
var copy = standardCategory.copy();
|
|
|
|
// Add it to the model.
|
|
this.model.addElementToList(copy);
|
|
|
|
// Update the copy in the view.
|
|
var tab = this.view.addCategoryRow(copy.name, copy.id);
|
|
this.addClickToSwitch(tab, copy.id);
|
|
// Color the category tab in the view.
|
|
if (copy.color) {
|
|
this.view.setBorderColor(copy.id, copy.color);
|
|
}
|
|
// Switch to loaded category.
|
|
this.switchElement(copy.id);
|
|
// Convert actual shadow blocks to user-generated shadow blocks.
|
|
this.convertShadowBlocks();
|
|
// Save state from workspace before updating preview.
|
|
this.saveStateFromWorkspace();
|
|
if (isFirstCategory) {
|
|
// Allow the user to use the default options for injecting the workspace
|
|
// when there are categories.
|
|
this.view.setCategoryOptions(this.model.hasElements());
|
|
this.generateNewOptions();
|
|
}
|
|
// Update preview.
|
|
this.updatePreview();
|
|
};
|
|
|
|
/**
|
|
* Loads the standard Blockly toolbox into the editing space. Should only
|
|
* be called when the mode is set to toolbox.
|
|
*/
|
|
WorkspaceFactoryController.prototype.loadStandardToolbox = function() {
|
|
this.loadCategoryByName('Logic');
|
|
this.loadCategoryByName('Loops');
|
|
this.loadCategoryByName('Math');
|
|
this.loadCategoryByName('Text');
|
|
this.loadCategoryByName('Lists');
|
|
this.loadCategoryByName('Colour');
|
|
this.addSeparator();
|
|
this.loadCategoryByName('Variables');
|
|
this.loadCategoryByName('Functions');
|
|
};
|
|
|
|
/**
|
|
* Given the name of a category, determines if it's the name of a standard
|
|
* category (case insensitive).
|
|
* @param {string} name The name of the category that should be checked if it's
|
|
* in StandardCategories categoryMap
|
|
* @return {boolean} True if name is a standard category name, false otherwise.
|
|
*/
|
|
WorkspaceFactoryController.prototype.isStandardCategoryName = function(name) {
|
|
for (var category in StandardCategories.categoryMap) {
|
|
if (name.toLowerCase() == category) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
};
|
|
|
|
/**
|
|
* Connected to the "add separator" dropdown option. If categories already
|
|
* exist, adds a separator to the model and view. Does not switch to select
|
|
* the separator, and updates the preview.
|
|
*/
|
|
WorkspaceFactoryController.prototype.addSeparator = function() {
|
|
// If adding the first element in the toolbox, transfers the user's blocks
|
|
// in a flyout to a category.
|
|
this.transferFlyoutBlocksToCategory();
|
|
// Create the separator in the model.
|
|
var separator = new ListElement(ListElement.TYPE_SEPARATOR);
|
|
this.model.addElementToList(separator);
|
|
// Create the separator in the view.
|
|
var tab = this.view.addSeparatorTab(separator.id);
|
|
this.addClickToSwitch(tab, separator.id);
|
|
// Switch to the separator and update the preview.
|
|
this.switchElement(separator.id);
|
|
this.updatePreview();
|
|
};
|
|
|
|
/**
|
|
* Connected to the import button. Given the file path inputted by the user
|
|
* from file input, if the import mode is for the toolbox, this function loads
|
|
* that toolbox XML to the workspace, creating category and separator tabs as
|
|
* necessary. If the import mode is for pre-loaded blocks in the workspace,
|
|
* this function loads that XML to the workspace to be edited further. This
|
|
* function switches mode to whatever the import mode is. Catches errors from
|
|
* file reading and prints an error message alerting the user.
|
|
* @param {string} file The path for the file to be imported into the workspace.
|
|
* Should contain valid toolbox XML.
|
|
* @param {string} importMode The mode corresponding to the type of file the
|
|
* user is importing (WorkspaceFactoryController.MODE_TOOLBOX or
|
|
* WorkspaceFactoryController.MODE_PRELOAD).
|
|
*/
|
|
WorkspaceFactoryController.prototype.importFile = function(file, importMode) {
|
|
// Exit if cancelled.
|
|
if (!file) {
|
|
return;
|
|
}
|
|
|
|
Blockly.Events.disable();
|
|
var controller = this;
|
|
var reader = new FileReader();
|
|
|
|
// To be executed when the reader has read the file.
|
|
reader.onload = function() {
|
|
// Try to parse XML from file and load it into toolbox editing area.
|
|
// Print error message if fail.
|
|
try {
|
|
var tree = Blockly.Xml.textToDom(reader.result);
|
|
if (importMode == WorkspaceFactoryController.MODE_TOOLBOX) {
|
|
// Switch mode.
|
|
controller.setMode(WorkspaceFactoryController.MODE_TOOLBOX);
|
|
|
|
// Confirm that the user wants to override their current toolbox.
|
|
var hasToolboxElements = controller.model.hasElements() ||
|
|
controller.toolboxWorkspace.getAllBlocks().length > 0;
|
|
if (hasToolboxElements) {
|
|
var msg = 'Are you sure you want to import? You will lose your ' +
|
|
'current toolbox.';
|
|
BlocklyDevTools.Analytics.onWarning(msg);
|
|
var continueAnyway = confirm();
|
|
if (!continueAnyway) {
|
|
return;
|
|
}
|
|
}
|
|
// Import toolbox XML.
|
|
controller.importToolboxFromTree_(tree);
|
|
BlocklyDevTools.Analytics.onImport('Toolbox.xml');
|
|
|
|
} else if (importMode == WorkspaceFactoryController.MODE_PRELOAD) {
|
|
// Switch mode.
|
|
controller.setMode(WorkspaceFactoryController.MODE_PRELOAD);
|
|
|
|
// Confirm that the user wants to override their current blocks.
|
|
if (controller.toolboxWorkspace.getAllBlocks().length > 0) {
|
|
var msg = 'Are you sure you want to import? You will lose your ' +
|
|
'current workspace blocks.';
|
|
var continueAnyway = confirm(msg);
|
|
BlocklyDevTools.Analytics.onWarning(msg);
|
|
if (!continueAnyway) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
// Import pre-loaded workspace XML.
|
|
controller.importPreloadFromTree_(tree);
|
|
BlocklyDevTools.Analytics.onImport('WorkspaceContents.xml');
|
|
} else {
|
|
// Throw error if invalid mode.
|
|
throw new Error("Unknown import mode: " + importMode);
|
|
}
|
|
} catch(e) {
|
|
var msg = 'Cannot load XML from file.';
|
|
alert(msg);
|
|
BlocklyDevTools.Analytics.onError(msg);
|
|
console.log(e);
|
|
} finally {
|
|
Blockly.Events.enable();
|
|
}
|
|
}
|
|
|
|
// Read the file asynchronously.
|
|
reader.readAsText(file);
|
|
};
|
|
|
|
/**
|
|
* Given a XML DOM tree, loads it into the toolbox editing area so that the
|
|
* user can continue editing their work. Assumes that tree is in valid toolbox
|
|
* XML format. Assumes that the mode is MODE_TOOLBOX.
|
|
* @param {!Element} tree XML tree to be loaded to toolbox editing area.
|
|
* @private
|
|
*/
|
|
WorkspaceFactoryController.prototype.importToolboxFromTree_ = function(tree) {
|
|
// Clear current editing area.
|
|
this.model.clearToolboxList();
|
|
this.view.clearToolboxTabs();
|
|
|
|
if (tree.getElementsByTagName('category').length == 0) {
|
|
// No categories present.
|
|
// Load all the blocks into a single category evenly spaced.
|
|
Blockly.Xml.domToWorkspace(tree, this.toolboxWorkspace);
|
|
this.toolboxWorkspace.cleanUp();
|
|
|
|
// Convert actual shadow blocks to user-generated shadow blocks.
|
|
this.convertShadowBlocks();
|
|
|
|
// Add message to denote empty category.
|
|
this.view.addEmptyCategoryMessage();
|
|
|
|
} else {
|
|
// Categories/separators present.
|
|
for (var i = 0, item; item = tree.children[i]; i++) {
|
|
|
|
if (item.tagName == 'category') {
|
|
// If the element is a category, create a new category and switch to it.
|
|
this.createCategory(item.getAttribute('name'), false);
|
|
var category = this.model.getElementByIndex(i);
|
|
this.switchElement(category.id);
|
|
|
|
// Load all blocks in that category to the workspace to be evenly
|
|
// spaced and saved to that category.
|
|
for (var j = 0, blockXml; blockXml = item.children[j]; j++) {
|
|
Blockly.Xml.domToBlock(blockXml, this.toolboxWorkspace);
|
|
}
|
|
|
|
// Evenly space the blocks.
|
|
this.toolboxWorkspace.cleanUp();
|
|
|
|
// Convert actual shadow blocks to user-generated shadow blocks.
|
|
this.convertShadowBlocks();
|
|
|
|
// Set category color.
|
|
if (item.getAttribute('colour')) {
|
|
category.changeColor(item.getAttribute('colour'));
|
|
this.view.setBorderColor(category.id, category.color);
|
|
}
|
|
// Set any custom tags.
|
|
if (item.getAttribute('custom')) {
|
|
this.model.addCustomTag(category, item.getAttribute('custom'));
|
|
}
|
|
} else {
|
|
// If the element is a separator, add the separator and switch to it.
|
|
this.addSeparator();
|
|
this.switchElement(this.model.getElementByIndex(i).id);
|
|
}
|
|
}
|
|
}
|
|
this.view.updateState(this.model.getIndexByElementId
|
|
(this.model.getSelectedId()), this.model.getSelected());
|
|
|
|
this.saveStateFromWorkspace();
|
|
|
|
// Set default configuration options for a single flyout or multiple
|
|
// categories.
|
|
this.view.setCategoryOptions(this.model.hasElements());
|
|
this.generateNewOptions();
|
|
|
|
this.updatePreview();
|
|
};
|
|
|
|
/**
|
|
* Given a XML DOM tree, loads it into the pre-loaded workspace editing area.
|
|
* Assumes that tree is in valid XML format and that the selected mode is
|
|
* MODE_PRELOAD.
|
|
* @param {!Element} tree XML tree to be loaded to pre-loaded block editing
|
|
* area.
|
|
*/
|
|
WorkspaceFactoryController.prototype.importPreloadFromTree_ = function(tree) {
|
|
this.clearAndLoadXml_(tree);
|
|
this.model.savePreloadXml(tree);
|
|
this.updatePreview();
|
|
};
|
|
|
|
/**
|
|
* Given a XML DOM tree, loads it into the pre-loaded workspace editing area.
|
|
* Assumes that tree is in valid XML format and that the selected mode is
|
|
* MODE_PRELOAD.
|
|
* @param {!Element} tree XML tree to be loaded to pre-loaded block editing
|
|
* area.
|
|
*/
|
|
WorkspaceFactoryController.prototype.importPreloadFromTree_ = function(tree) {
|
|
this.clearAndLoadXml_(tree);
|
|
this.model.savePreloadXml(tree);
|
|
this.saveStateFromWorkspace();
|
|
this.updatePreview();
|
|
};
|
|
|
|
/**
|
|
* Given a XML DOM tree, loads it into the pre-loaded workspace editing area.
|
|
* Assumes that tree is in valid XML format and that the selected mode is
|
|
* MODE_PRELOAD.
|
|
* @param {!Element} tree XML tree to be loaded to pre-loaded block editing
|
|
* area.
|
|
*/
|
|
WorkspaceFactoryController.prototype.importPreloadFromTree_ = function(tree) {
|
|
this.clearAndLoadXml_(tree);
|
|
this.model.savePreloadXml(tree);
|
|
this.saveStateFromWorkspace();
|
|
this.updatePreview();
|
|
};
|
|
|
|
/**
|
|
* Clears the editing area completely, deleting all categories and all
|
|
* blocks in the model and view and all pre-loaded blocks. Tied to the
|
|
* "Clear" button.
|
|
*/
|
|
WorkspaceFactoryController.prototype.clearAll = function() {
|
|
var msg = 'Are you sure you want to clear all of your work in Workspace' +
|
|
' Factory?';
|
|
BlocklyDevTools.Analytics.onWarning(msg);
|
|
if (!confirm(msg)) {
|
|
return;
|
|
}
|
|
var hasCategories = this.model.hasElements();
|
|
this.model.clearToolboxList();
|
|
this.view.clearToolboxTabs();
|
|
this.model.savePreloadXml(Blockly.Xml.textToDom('<xml></xml>'));
|
|
this.view.addEmptyCategoryMessage();
|
|
this.view.updateState(-1, null);
|
|
this.toolboxWorkspace.clear();
|
|
this.toolboxWorkspace.clearUndo();
|
|
this.saveStateFromWorkspace();
|
|
this.hasUnsavedToolboxChanges = false;
|
|
this.hasUnsavedPreloadChanges = false;
|
|
this.view.setCategoryOptions(this.model.hasElements());
|
|
this.generateNewOptions();
|
|
this.updatePreview();
|
|
};
|
|
|
|
/*
|
|
* Makes the currently selected block a user-generated shadow block. These
|
|
* blocks are not made into real shadow blocks, but recorded in the model
|
|
* and visually marked as shadow blocks, allowing the user to move and edit
|
|
* them (which would be impossible with actual shadow blocks). Updates the
|
|
* preview when done.
|
|
*/
|
|
WorkspaceFactoryController.prototype.addShadow = function() {
|
|
// No block selected to make a shadow block.
|
|
if (!Blockly.selected) {
|
|
return;
|
|
}
|
|
// Clear any previous warnings on the block (would only have warnings on
|
|
// a non-shadow block if it was nested inside another shadow block).
|
|
Blockly.selected.setWarningText(null);
|
|
// Set selected block and all children as shadow blocks.
|
|
this.addShadowForBlockAndChildren_(Blockly.selected);
|
|
|
|
// Save and update the preview.
|
|
this.saveStateFromWorkspace();
|
|
this.updatePreview();
|
|
};
|
|
|
|
/**
|
|
* Sets a block and all of its children to be user-generated shadow blocks,
|
|
* both in the model and view.
|
|
* @param {!Blockly.Block} block The block to be converted to a user-generated
|
|
* shadow block.
|
|
* @private
|
|
*/
|
|
WorkspaceFactoryController.prototype.addShadowForBlockAndChildren_ =
|
|
function(block) {
|
|
// Convert to shadow block.
|
|
this.view.markShadowBlock(block);
|
|
this.model.addShadowBlock(block.id);
|
|
|
|
if (FactoryUtils.hasVariableField(block)) {
|
|
block.setWarningText('Cannot make variable blocks shadow blocks.');
|
|
}
|
|
|
|
// Convert all children to shadow blocks recursively.
|
|
var children = block.getChildren();
|
|
for (var i = 0; i < children.length; i++) {
|
|
this.addShadowForBlockAndChildren_(children[i]);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* If the currently selected block is a user-generated shadow block, this
|
|
* function makes it a normal block again, removing it from the list of
|
|
* shadow blocks and loading the workspace again. Updates the preview again.
|
|
*/
|
|
WorkspaceFactoryController.prototype.removeShadow = function() {
|
|
// No block selected to modify.
|
|
if (!Blockly.selected) {
|
|
return;
|
|
}
|
|
this.model.removeShadowBlock(Blockly.selected.id);
|
|
this.view.unmarkShadowBlock(Blockly.selected);
|
|
|
|
// If turning invalid shadow block back to normal block, remove warning.
|
|
Blockly.selected.setWarningText(null);
|
|
|
|
this.saveStateFromWorkspace();
|
|
this.updatePreview();
|
|
};
|
|
|
|
/**
|
|
* Given a unique block ID, uses the model to determine if a block is a
|
|
* user-generated shadow block.
|
|
* @param {string} blockId The unique ID of the block to examine.
|
|
* @return {boolean} True if the block is a user-generated shadow block, false
|
|
* otherwise.
|
|
*/
|
|
WorkspaceFactoryController.prototype.isUserGenShadowBlock = function(blockId) {
|
|
return this.model.isShadowBlock(blockId);
|
|
};
|
|
|
|
/**
|
|
* Call when importing XML containing real shadow blocks. This function turns
|
|
* all real shadow blocks loaded in the workspace into user-generated shadow
|
|
* blocks, meaning they are marked as shadow blocks by the model and appear as
|
|
* shadow blocks in the view but are still editable and movable.
|
|
*/
|
|
WorkspaceFactoryController.prototype.convertShadowBlocks = function() {
|
|
var blocks = this.toolboxWorkspace.getAllBlocks();
|
|
for (var i = 0, block; block = blocks[i]; i++) {
|
|
if (block.isShadow()) {
|
|
block.setShadow(false);
|
|
// Delete the shadow DOM attached to the block so that the shadow block
|
|
// does not respawn. Dependent on implementation details.
|
|
var parentConnection = block.outputConnection ?
|
|
block.outputConnection.targetConnection :
|
|
block.previousConnection.targetConnection;
|
|
if (parentConnection) {
|
|
parentConnection.setShadowDom(null);
|
|
}
|
|
this.model.addShadowBlock(block.id);
|
|
this.view.markShadowBlock(block);
|
|
}
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Sets the currently selected mode that determines what the toolbox workspace
|
|
* is being used to edit. Updates the view and then saves and loads XML
|
|
* to and from the toolbox and updates the help text.
|
|
* @param {string} tab The type of tab being switched to
|
|
* (WorkspaceFactoryController.MODE_TOOLBOX or
|
|
* WorkspaceFactoryController.MODE_PRELOAD).
|
|
*/
|
|
WorkspaceFactoryController.prototype.setMode = function(mode) {
|
|
// No work to change mode that's currently set.
|
|
if (this.selectedMode == mode) {
|
|
return;
|
|
}
|
|
|
|
// No work to change mode that's currently set.
|
|
if (this.selectedMode == mode) {
|
|
return;
|
|
}
|
|
|
|
// Set tab selection and display appropriate tab.
|
|
this.view.setModeSelection(mode);
|
|
|
|
// Update selected tab.
|
|
this.selectedMode = mode;
|
|
|
|
// Update help text above workspace.
|
|
this.view.updateHelpText(mode);
|
|
|
|
if (mode == WorkspaceFactoryController.MODE_TOOLBOX) {
|
|
// Open the toolbox editing space.
|
|
this.model.savePreloadXml
|
|
(Blockly.Xml.workspaceToDom(this.toolboxWorkspace));
|
|
this.clearAndLoadXml_(this.model.getSelectedXml());
|
|
this.view.disableWorkspace(this.view.shouldDisableWorkspace
|
|
(this.model.getSelected()));
|
|
} else {
|
|
// Open the pre-loaded workspace editing space.
|
|
if (this.model.getSelected()) {
|
|
this.model.getSelected().saveFromWorkspace(this.toolboxWorkspace);
|
|
}
|
|
this.clearAndLoadXml_(this.model.getPreloadXml());
|
|
this.view.disableWorkspace(false);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Clears the toolbox workspace and loads XML to it, marking shadow blocks
|
|
* as necessary.
|
|
* @private
|
|
* @param {!Element} xml The XML to be loaded to the workspace.
|
|
*/
|
|
WorkspaceFactoryController.prototype.clearAndLoadXml_ = function(xml) {
|
|
this.toolboxWorkspace.clear();
|
|
this.toolboxWorkspace.clearUndo();
|
|
Blockly.Xml.domToWorkspace(xml, this.toolboxWorkspace);
|
|
this.view.markShadowBlocks(this.model.getShadowBlocksInWorkspace
|
|
(this.toolboxWorkspace.getAllBlocks()));
|
|
this.warnForUndefinedBlocks_();
|
|
};
|
|
|
|
/**
|
|
* Sets the standard default options for the options object and updates
|
|
* the preview workspace. The default values depends on if categories are
|
|
* present.
|
|
*/
|
|
WorkspaceFactoryController.prototype.setStandardOptionsAndUpdate = function() {
|
|
this.view.setBaseOptions();
|
|
this.view.setCategoryOptions(this.model.hasElements());
|
|
this.generateNewOptions();
|
|
};
|
|
|
|
/**
|
|
* Generates a new options object for injecting a Blockly workspace based
|
|
* on user input. Should be called every time a change has been made to
|
|
* an input field. Updates the model and reinjects the preview workspace.
|
|
*/
|
|
WorkspaceFactoryController.prototype.generateNewOptions = function() {
|
|
this.model.setOptions(this.readOptions_());
|
|
|
|
this.reinjectPreview(Blockly.Options.parseToolboxTree
|
|
(this.generator.generateToolboxXml()));
|
|
};
|
|
|
|
/**
|
|
* Generates a new options object for injecting a Blockly workspace based on
|
|
* user input.
|
|
* @return {!Object} Blockly injection options object.
|
|
* @private
|
|
*/
|
|
WorkspaceFactoryController.prototype.readOptions_ = function() {
|
|
var optionsObj = Object.create(null);
|
|
|
|
// Add all standard options to the options object.
|
|
// Use parse int to get numbers from value inputs.
|
|
var readonly = document.getElementById('option_readOnly_checkbox').checked;
|
|
if (readonly) {
|
|
optionsObj['readOnly'] = true;
|
|
} else {
|
|
optionsObj['collapse'] =
|
|
document.getElementById('option_collapse_checkbox').checked;
|
|
optionsObj['comments'] =
|
|
document.getElementById('option_comments_checkbox').checked;
|
|
optionsObj['disable'] =
|
|
document.getElementById('option_disable_checkbox').checked;
|
|
if (document.getElementById('option_infiniteBlocks_checkbox').checked) {
|
|
optionsObj['maxBlocks'] = Infinity;
|
|
} else {
|
|
var maxBlocksValue =
|
|
document.getElementById('option_maxBlocks_number').value;
|
|
optionsObj['maxBlocks'] = typeof maxBlocksValue == 'string' ?
|
|
parseInt(maxBlocksValue) : maxBlocksValue;
|
|
}
|
|
optionsObj['trashcan'] =
|
|
document.getElementById('option_trashcan_checkbox').checked;
|
|
optionsObj['horizontalLayout'] =
|
|
document.getElementById('option_horizontalLayout_checkbox').checked;
|
|
optionsObj['toolboxPosition'] =
|
|
document.getElementById('option_toolboxPosition_checkbox').checked ?
|
|
'end' : 'start';
|
|
}
|
|
|
|
optionsObj['css'] = document.getElementById('option_css_checkbox').checked;
|
|
optionsObj['media'] = document.getElementById('option_media_text').value;
|
|
optionsObj['rtl'] = document.getElementById('option_rtl_checkbox').checked;
|
|
optionsObj['scrollbars'] =
|
|
document.getElementById('option_scrollbars_checkbox').checked;
|
|
optionsObj['sounds'] =
|
|
document.getElementById('option_sounds_checkbox').checked;
|
|
optionsObj['oneBasedIndex'] =
|
|
document.getElementById('option_oneBasedIndex_checkbox').checked;
|
|
|
|
// If using a grid, add all grid options.
|
|
if (document.getElementById('option_grid_checkbox').checked) {
|
|
var grid = Object.create(null);
|
|
var spacingValue =
|
|
document.getElementById('gridOption_spacing_number').value;
|
|
grid['spacing'] = typeof spacingValue == 'string' ?
|
|
parseInt(spacingValue) : spacingValue;
|
|
var lengthValue = document.getElementById('gridOption_length_number').value;
|
|
grid['length'] = typeof lengthValue == 'string' ?
|
|
parseInt(lengthValue) : lengthValue;
|
|
grid['colour'] = document.getElementById('gridOption_colour_text').value;
|
|
if (!readonly) {
|
|
grid['snap'] =
|
|
document.getElementById('gridOption_snap_checkbox').checked;
|
|
}
|
|
optionsObj['grid'] = grid;
|
|
}
|
|
|
|
// If using zoom, add all zoom options.
|
|
if (document.getElementById('option_zoom_checkbox').checked) {
|
|
var zoom = Object.create(null);
|
|
zoom['controls'] =
|
|
document.getElementById('zoomOption_controls_checkbox').checked;
|
|
zoom['wheel'] =
|
|
document.getElementById('zoomOption_wheel_checkbox').checked;
|
|
var startScaleValue =
|
|
document.getElementById('zoomOption_startScale_number').value;
|
|
zoom['startScale'] = typeof startScaleValue == 'string' ?
|
|
parseFloat(startScaleValue) : startScaleValue;
|
|
var maxScaleValue =
|
|
document.getElementById('zoomOption_maxScale_number').value;
|
|
zoom['maxScale'] = typeof maxScaleValue == 'string' ?
|
|
parseFloat(maxScaleValue) : maxScaleValue;
|
|
var minScaleValue =
|
|
document.getElementById('zoomOption_minScale_number').value;
|
|
zoom['minScale'] = typeof minScaleValue == 'string' ?
|
|
parseFloat(minScaleValue) : minScaleValue;
|
|
var scaleSpeedValue =
|
|
document.getElementById('zoomOption_scaleSpeed_number').value;
|
|
zoom['scaleSpeed'] = typeof scaleSpeedValue == 'string' ?
|
|
parseFloat(scaleSpeedValue) : scaleSpeedValue;
|
|
optionsObj['zoom'] = zoom;
|
|
}
|
|
|
|
return optionsObj;
|
|
};
|
|
|
|
/**
|
|
* Imports blocks from a file, generating a category in the toolbox workspace
|
|
* to allow the user to use imported blocks in the toolbox and in pre-loaded
|
|
* blocks.
|
|
* @param {!File} file File object for the blocks to import.
|
|
* @param {string} format The format of the file to import, either 'JSON' or
|
|
* 'JavaScript'.
|
|
*/
|
|
WorkspaceFactoryController.prototype.importBlocks = function(file, format) {
|
|
// Generate category name from file name.
|
|
var categoryName = file.name;
|
|
|
|
var controller = this;
|
|
var reader = new FileReader();
|
|
|
|
// To be executed when the reader has read the file.
|
|
reader.onload = function() {
|
|
try {
|
|
// Define blocks using block types from file.
|
|
var blockTypes = FactoryUtils.defineAndGetBlockTypes(reader.result,
|
|
format);
|
|
|
|
// If an imported block type is already defined, check if the user wants
|
|
// to override the current block definition.
|
|
if (controller.model.hasDefinedBlockTypes(blockTypes)) {
|
|
var msg = 'An imported block uses the same name as a block '
|
|
+ 'already in your toolbox. Are you sure you want to override the '
|
|
+ 'currently defined block?';
|
|
var continueAnyway = confirm(msg);
|
|
BlocklyDevTools.Analytics.onWarning(msg);
|
|
if (!continueAnyway) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
var blocks = controller.generator.getDefinedBlocks(blockTypes);
|
|
// Generate category XML and append to toolbox.
|
|
var categoryXml = FactoryUtils.generateCategoryXml(blocks, categoryName);
|
|
// Get random color for category between 0 and 360. Gives each imported
|
|
// category a different color.
|
|
var randomColor = Math.floor(Math.random() * 360);
|
|
categoryXml.setAttribute('colour', randomColor);
|
|
controller.toolbox.appendChild(categoryXml);
|
|
controller.toolboxWorkspace.updateToolbox(controller.toolbox);
|
|
// Update imported block types.
|
|
controller.model.addImportedBlockTypes(blockTypes);
|
|
// Reload current category to possibly reflect any newly defined blocks.
|
|
controller.clearAndLoadXml_
|
|
(Blockly.Xml.workspaceToDom(controller.toolboxWorkspace));
|
|
|
|
BlocklyDevTools.Analytics.onImport('BlockDefinitions' +
|
|
(format == 'JSON' ? '.json' : '.js'));
|
|
} catch (e) {
|
|
msg = 'Cannot read blocks from file.';
|
|
alert(msg);
|
|
BlocklyDevTools.Analytics.onError(msg);
|
|
window.console.log(e);
|
|
}
|
|
}
|
|
|
|
// Read the file asynchronously.
|
|
reader.readAsText(file);
|
|
};
|
|
|
|
/*
|
|
* Updates the block library category in the toolbox workspace toolbox.
|
|
* @param {!Element} categoryXml XML for the block library category.
|
|
* @param {!Array.<string>} libBlockTypes Array of block types from the block
|
|
* library.
|
|
*/
|
|
WorkspaceFactoryController.prototype.setBlockLibCategory =
|
|
function(categoryXml, libBlockTypes) {
|
|
var blockLibCategory = document.getElementById('blockLibCategory');
|
|
|
|
// Set category ID so that it can be easily replaced, and set a standard,
|
|
// arbitrary block library color.
|
|
categoryXml.setAttribute('id', 'blockLibCategory');
|
|
categoryXml.setAttribute('colour', 260);
|
|
|
|
// Update the toolbox and toolboxWorkspace.
|
|
this.toolbox.replaceChild(categoryXml, blockLibCategory);
|
|
this.toolboxWorkspace.toolbox_.clearSelection();
|
|
this.toolboxWorkspace.updateToolbox(this.toolbox);
|
|
|
|
// Update the block library types.
|
|
this.model.updateLibBlockTypes(libBlockTypes);
|
|
|
|
// Reload XML on page to account for blocks now defined or undefined in block
|
|
// library.
|
|
this.clearAndLoadXml_(Blockly.Xml.workspaceToDom(this.toolboxWorkspace));
|
|
};
|
|
|
|
/**
|
|
* Return the block types used in the custom toolbox and pre-loaded workspace.
|
|
* @return {!Array.<string>} Block types used in the custom toolbox and
|
|
* pre-loaded workspace.
|
|
*/
|
|
WorkspaceFactoryController.prototype.getAllUsedBlockTypes = function() {
|
|
return this.model.getAllUsedBlockTypes();
|
|
};
|
|
|
|
/**
|
|
* Determines if a block loaded in the workspace has a definition (if it
|
|
* is a standard block, is defined in the block library, or has a definition
|
|
* imported).
|
|
* @param {!Blockly.Block} block The block to examine.
|
|
*/
|
|
WorkspaceFactoryController.prototype.isDefinedBlock = function(block) {
|
|
return this.model.isDefinedBlockType(block.type);
|
|
};
|
|
|
|
/**
|
|
* Sets a warning on blocks loaded to the workspace that are not defined.
|
|
* @private
|
|
*/
|
|
WorkspaceFactoryController.prototype.warnForUndefinedBlocks_ = function() {
|
|
var blocks = this.toolboxWorkspace.getAllBlocks();
|
|
for (var i = 0, block; block = blocks[i]; i++) {
|
|
if (!this.isDefinedBlock(block)) {
|
|
block.setWarningText(block.type + ' is not defined (it is not a standard '
|
|
+ 'block, \nin your block library, or an imported block)');
|
|
}
|
|
}
|
|
};
|
|
|
|
/*
|
|
* Determines if a standard variable category is in the custom toolbox.
|
|
* @return {boolean} True if a variables category is in use, false otherwise.
|
|
*/
|
|
WorkspaceFactoryController.prototype.hasVariablesCategory = function() {
|
|
return this.model.hasVariables();
|
|
};
|
|
|
|
/**
|
|
* Determines if a standard procedures category is in the custom toolbox.
|
|
* @return {boolean} True if a procedures category is in use, false otherwise.
|
|
*/
|
|
WorkspaceFactoryController.prototype.hasProceduresCategory = function() {
|
|
return this.model.hasProcedures();
|
|
};
|
|
|
|
/**
|
|
* Determines if there are any unsaved changes in workspace factory.
|
|
* @return {boolean} True if there are unsaved changes, false otherwise.
|
|
*/
|
|
WorkspaceFactoryController.prototype.hasUnsavedChanges = function() {
|
|
return this.hasUnsavedToolboxChanges || this.hasUnsavedPreloadChanges;
|
|
};
|