/// <summary>
 /// Block Library Controller Class
 /// </summary>
 /// <param name="blockLibraryName"> Desired name of Block Library, also used
 ///   to create the key for where it's stored in local storage.</param>
 /// <param name="opt_blockLibraryStorage"> Optional storage
 /// object that allows user to import a block library.</param>
 public BlockLibraryController(string blockLibraryName, BlockLibraryStorage opt_blockLibraryStorage = null)
 {
     this.name = blockLibraryName;
     // Create a new, empty Block Library Storage object, or load existing one.
     this.storage = opt_blockLibraryStorage ?? new BlockLibraryStorage(this.name);
     // The BlockLibraryView object handles the proper updating and formatting of
     // the block library dropdown.
     this.view = new BlockLibraryView();
 }
 /*
  * BlockExporter Controller Class
  * @param {!} blockLibStorage Block Library Storage.
  * @constructor
  */
 public BlockExporterController(BlockLibraryStorage blockLibStorage)
 {
     // BlockLibrary.Storage object containing user's saved blocks.
     this.blockLibStorage = blockLibStorage;
     // Utils for generating code to export.
     this.tools = new BlockExporterTools();
     // The ID of the block selector, a div element that will be populated with the
     // block options.
     this.selectorID = "blockSelector";
     // Map of block types stored in block library to their corresponding Block
     // Option objects.
     this.blockOptions = this.tools.createBlockSelectorFromLib(
         this.blockLibStorage, this.selectorID);
     // View provides the block selector and export settings UI.
     this.view = new BlockExporterView(this.blockOptions);
 }
Beispiel #3
0
        /// <summary>
        /// Tied to the "Import Block Library" button. Imports block library from file to
        /// Block Factory. Expects user to upload a single file of JSON mapping each
        /// block type to its XML text representation.
        /// </summary>
        public void importBlockLibraryFromFile()
        {
            var self  = this;
            var files = (HTMLInputElement)Document.GetElementById("files");

            // If the file list is empty, the user likely canceled in the dialog.
            if (files.files.Length > 0)
            {
                // The input tag doesn't have the "multiple" attribute
                // so the user can only choose 1 file.
                var file       = files.files[0];
                var fileReader = new FileReader();

                // Create a map of block type to XML text from the file when it has been
                // read.
                fileReader.AddEventListener("load", new Action <Event>((@event) => {
                    var fileContents = ((FileReader)@event.Target).Result;
                    // Create empty object to hold the read block library information.
                    var blockXmlTextMap = new Dictionary <string, object>();
                    try {
                        // Parse the file to get map of block type to XML text.
                        blockXmlTextMap = self.formatBlockLibraryForImport_(fileContents);
                    }
                    catch (Exception) {
                        var message = "Could not load your block library file.\n";
                        Window.Alert(message + "\nFile Name: " + file.Name);
                        return;
                    }

                    // Create a new block library storage object with inputted block library.
                    var blockLibStorage = new BlockLibraryStorage(
                        self.blockLibraryName, blockXmlTextMap);

                    // Update block library controller with the new block library
                    // storage.
                    self.blockLibraryController.setBlockLibraryStorage(blockLibStorage);
                    // Update the block library dropdown.
                    self.blockLibraryController.populateBlockLibrary();
                    // Update the exporter's block library storage.
                    self.exporter.setBlockLibraryStorage(blockLibStorage);
                }), false);
                // Read the file.
                fileReader.ReadAsText(file);
            }
        }
        /// <summary>
        /// Generate XML for the workspace factory's category from imported block
        /// definitions.
        /// </summary>
        /// <param name="blockLibStorage">Block Library Storage object.</param>
        /// <returns>XML representation of a category.</returns>
        public Element generateCategoryFromBlockLib(BlockLibraryStorage blockLibStorage)
        {
            var allBlockTypes = blockLibStorage.getBlockTypes();
            // Object mapping block type to XML.
            var blockXmlMap = blockLibStorage.getBlockXmlMap(allBlockTypes);

            // Define the custom blocks in order to be able to create instances of
            // them in the exporter workspace.
            this.addBlockDefinitions(blockXmlMap);

            // Get array of defined blocks.
            var blocks = new JsArray <Blockly.Block>();

            foreach (var blockType in blockXmlMap.Keys)
            {
                var block = FactoryUtils.getDefinedBlock(blockType, this.hiddenWorkspace);
                blocks.Push(block);
            }

            return(FactoryUtils.generateCategoryXml(blocks, "Block Library"));
        }
        /// <summary>
        /// Generate selector dom from block library storage. For each block in the
        /// library, it has a block option, which consists of a checkbox, a label,
        /// and a fixed size preview workspace.
        /// </summary>
        /// <param name="blockLibStorage"> Block Library Storage object.</param>
        /// <param name="blockSelectorId"> ID of the div element that will contain
        /// the block options.</param>
        /// <returns>Map of block type to Block Option object.</returns>
        public Dictionary <string, BlockOption> createBlockSelectorFromLib(BlockLibraryStorage blockLibStorage, string blockSelectorId)
        {
            // Object mapping each stored block type to XML.
            var allBlockTypes = blockLibStorage.getBlockTypes();
            var blockXmlMap   = blockLibStorage.getBlockXmlMap(allBlockTypes);

            // Define the custom blocks in order to be able to create instances of
            // them in the exporter workspace.
            this.addBlockDefinitions(blockXmlMap);

            var blockSelector = Document.GetElementById(blockSelectorId);
            // Clear the block selector.
            Node child;

            while ((child = blockSelector.FirstChild) != null)
            {
                blockSelector.RemoveChild(child);
            }

            // Append each block option's dom to the selector.
            var blockOptions = new Dictionary <string, BlockOption>();

            foreach (var blockType in blockXmlMap.Keys)
            {
                // Get preview block's XML.
                var block           = FactoryUtils.getDefinedBlock(blockType, this.hiddenWorkspace);
                var previewBlockXml = Blockly.Xml.workspaceToDom(this.hiddenWorkspace);

                // Create block option, inject block into preview workspace, and append
                // option to block selector.
                var blockOpt = new BlockOption(blockSelector, blockType, previewBlockXml);
                blockOpt.createDom();
                blockSelector.AppendChild(blockOpt.dom);
                blockOpt.showPreviewBlock();
                blockOptions[blockType] = blockOpt;
            }
            return(blockOptions);
        }
        /// <summary>
        /// Pulls information about all blocks in the block library to generate XML
        /// for the selector workpace's toolbox.
        /// </summary>
        /// <param name="blockLibStorage"> Block Library Storage object.</param>
        /// <returns>XML representation of the toolbox.</returns>
        Element generateToolboxFromLibrary(BlockLibraryStorage blockLibStorage)
        {
            // Create DOM for XML.
            var xmlDom = goog.dom.createDom("xml", new Dictionary <string, string> {
                { "id", "blockExporterTools_toolbox" },
                { "style", "display:none" }
            });

            var allBlockTypes = blockLibStorage.getBlockTypes();
            // Object mapping block type to XML.
            var blockXmlMap = blockLibStorage.getBlockXmlMap(allBlockTypes);

            // Define the custom blocks in order to be able to create instances of
            // them in the exporter workspace.
            this.addBlockDefinitions(blockXmlMap);

            foreach (var blockType in blockXmlMap.Keys)
            {
                // Get block.
                var block    = FactoryUtils.getDefinedBlock(blockType, this.hiddenWorkspace);
                var category = FactoryUtils.generateCategoryXml(new JsArray <Blockly.Block> {
                    block
                }, blockType);
                xmlDom.AppendChild(category);
            }

            // If there are no blocks in library and the map is empty, append dummy
            // category.
            if (blockXmlMap.Count == 0)
            {
                var category = goog.dom.createDom("category");
                category.SetAttribute("name", "Next Saved Block");
                xmlDom.AppendChild(category);
            }
            return(xmlDom);
        }
 /// <summary>
 /// Get the block library storage object from which exporter exports.
 /// </summary>
 /// <returns>blockLibStorage Block Library Storage object
 ///    that stores the blocks.</returns>
 public BlockLibraryStorage getBlockLibraryStorage(BlockLibraryStorage blockLibStorage)
 {
     return(this.blockLibStorage);
 }
 /// <summary>
 /// Set the block library storage object from which exporter exports.
 /// </summary>
 /// <param name="blockLibStorage"> Block Library Storage object
 /// that stores the blocks.</param>
 public void setBlockLibraryStorage(BlockLibraryStorage blockLibStorage)
 {
     this.blockLibStorage = blockLibStorage;
 }