Commit 0341af0c authored by Evan W. Patton's avatar Evan W. Patton

Allow component blocks and property getters in global declarations

It is often the case that people will want to construct a list of
components or store some information about a copmonent so that it can
be restored later. this currently requires putting all of the
initialization into the Screen's Initialize event handler. However,
many people intuitively expect that they can construct a list of
components as part of the global declaration. This changes allows for
component blocks and property getters to be used in a blocks tree with
a global declaration root node.

Change-Id: I69d26716ebf4bfcdd52465f15eaf75af1c4170cf
parent 4ccbeea4
...@@ -1284,7 +1284,7 @@ Blockly.Blocks.component_component_block = { ...@@ -1284,7 +1284,7 @@ Blockly.Blocks.component_component_block = {
this.appendDummyInput().appendField(this.componentDropDown, Blockly.ComponentBlock.COMPONENT_SELECTOR); this.appendDummyInput().appendField(this.componentDropDown, Blockly.ComponentBlock.COMPONENT_SELECTOR);
//this.componentDropDown.setValue(this.instanceName); //this.componentDropDown.setValue(this.instanceName);
this.setOutput(true, [this.typeName,"COMPONENT"]); this.setOutput(true, [this.typeName,"COMPONENT"]);
this.errors = [{name:"checkIfUndefinedBlock"},{name:"checkIsInDefinition"},{name:"checkComponentNotExistsError"}]; this.errors = [{name:"checkIfUndefinedBlock"},{name:"checkComponentNotExistsError"}];
}, },
// Renames the block's instanceName, type, and reset its title // Renames the block's instanceName, type, and reset its title
rename : function(oldname, newname) { rename : function(oldname, newname) {
......
...@@ -293,6 +293,10 @@ Blockly.WarningHandler.prototype.checkErrors = function(block) { ...@@ -293,6 +293,10 @@ Blockly.WarningHandler.prototype.checkErrors = function(block) {
//Check if the block is inside of a variable declaration block, if so, create an error //Check if the block is inside of a variable declaration block, if so, create an error
Blockly.WarningHandler.prototype["checkIsInDefinition"] = function(block){ Blockly.WarningHandler.prototype["checkIsInDefinition"] = function(block){
// Allow property getters as they should be pure.
if (block.type === 'component_set_get' && block.setOrGet === 'get') {
return false;
}
var rootBlock = block.getRootBlock(); var rootBlock = block.getRootBlock();
if(rootBlock.type == "global_declaration"){ if(rootBlock.type == "global_declaration"){
var errorMessage = Blockly.Msg.ERROR_BLOCK_CANNOT_BE_IN_DEFINTION; var errorMessage = Blockly.Msg.ERROR_BLOCK_CANNOT_BE_IN_DEFINTION;
......
...@@ -551,7 +551,7 @@ ...@@ -551,7 +551,7 @@
var-val-pairs)) var-val-pairs))
;; Create each component and set its corresponding field ;; Create each component and set its corresponding field
(define (init-components component-descriptors) (define (create-components component-descriptors)
(for-each (lambda (component-info) (for-each (lambda (component-info)
(let ((component-name (caddr component-info)) (let ((component-name (caddr component-info))
(init-thunk (cadddr component-info)) (init-thunk (cadddr component-info))
...@@ -566,14 +566,10 @@ ...@@ -566,14 +566,10 @@
;; Add the mapping from component name -> component object to the ;; Add the mapping from component name -> component object to the
;; form-environment ;; form-environment
(add-to-form-environment component-name component-object)))) (add-to-form-environment component-name component-object))))
component-descriptors) component-descriptors))
;; Now that all the components are constructed we can call
;; their init-thunk and their Initialize methods. We need ;; Initialize all of the components
;; to do this after all the construction steps because the (define (init-components component-descriptors)
;; init-thunk (i.e. design-time initializations) and
;; Initialize methods may contain references to other
;; components.
;;
;; First all the init-thunks ;; First all the init-thunks
(for-each (lambda (component-info) (for-each (lambda (component-info)
(let ((component-name (caddr component-info)) (let ((component-name (caddr component-info))
...@@ -618,12 +614,13 @@ ...@@ -618,12 +614,13 @@
(register-events events-to-register) (register-events events-to-register)
(try-catch (try-catch
(begin (let ((components (reverse components-to-create)))
;; We need this binding because the block parser sends this symbol ;; We need this binding because the block parser sends this symbol
;; to represent an uninitialized value ;; to represent an uninitialized value
;; We have to explicity write #!null here, rather than ;; We have to explicity write #!null here, rather than
;; *the-null-value* because that external defintion hasn't happened yet ;; *the-null-value* because that external defintion hasn't happened yet
(add-to-global-vars '*the-null-value* (lambda () #!null)) (add-to-global-vars '*the-null-value* (lambda () #!null))
(create-components components)
;; These next three clauses need to be in this order: ;; These next three clauses need to be in this order:
;; Properties can't be set until after the global variables are ;; Properties can't be set until after the global variables are
;; assigned. And some properties can't be set after the components are ;; assigned. And some properties can't be set after the components are
...@@ -631,7 +628,13 @@ ...@@ -631,7 +628,13 @@
;; components have been installed. (This gives an error.) ;; components have been installed. (This gives an error.)
(init-global-variables (reverse global-vars-to-create)) (init-global-variables (reverse global-vars-to-create))
(for-each force (reverse form-do-after-creation)) (for-each force (reverse form-do-after-creation))
(init-components (reverse components-to-create))) ;; Now that all the components are constructed we can call
;; their init-thunk and their Initialize methods. We need
;; to do this after all the construction steps because the
;; init-thunk (i.e. design-time initializations) and
;; Initialize methods may contain references to other
;; components.
(init-components components))
(exception com.google.appinventor.components.runtime.errors.YailRuntimeError (exception com.google.appinventor.components.runtime.errors.YailRuntimeError
;;(android-log-form "Caught exception in define-form ") ;;(android-log-form "Caught exception in define-form ")
(process-exception exception)))))))) (process-exception exception))))))))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment