Ejemplo n.º 1
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);
            }
        }